]> git.xonotic.org Git - xonotic/div0-gittools.git/blob - git-recurse
git-branch-manager: funny feature --skip that will ignore the content of merged/unmer...
[xonotic/div0-gittools.git] / git-recurse
1 #!/bin/sh
2
3 SEPARATOR=@@
4
5 # TODO this logic should probably rather be in rev-parse itself
6 # what it does:
7 #   whenever an expression foo@@bar is used as an argument
8 #   it checks whether foo is a valid reference, and if not, bar is used instead
9 processarg()
10 {
11         prefix=
12         suffix=
13         case "$1" in
14                 # have to detect some rev-parse syntax
15                 *..*)
16                         first=${1%%..*}
17                         rest=${1#*..}
18                         first=`processarg "$first"`
19                         rest=`processarg "$rest"`
20                         echo "$first..$rest"
21                         ;;
22                 *@\{*)
23                         first=${1%%@\{*}
24                         rest=${1#*@\{}
25                         first=`processarg "$first"`
26                         echo "$first@{$rest"
27                         ;;
28                 *^*)
29                         first=${1%%^*}
30                         rest=${1#*^}
31                         first=`processarg "$first"`
32                         echo "$first^$rest"
33                         ;;
34                 *~*)
35                         first=${1%%~*}
36                         rest=${1#*~}
37                         first=`processarg "$first"`
38                         echo "$first~$rest"
39                         ;;
40                 *:*)
41                         first=${1%%:*}
42                         rest=${1#*:}
43                         first=`processarg "$first"`
44                         echo "$first:$rest"
45                         ;;
46                 ^*)
47                         first=${1#^}
48                         first=`processarg "$first"`
49                         echo "^$first"
50                         ;;
51                 # handle foo@@bar so that if foo exists, foo stays, otherwise bar
52                 *$SEPARATOR*)
53                         first=${1%%$SEPARATOR*}
54                         rest=${1#*$SEPARATOR}
55                         if git rev-parse "$first" >/dev/null 2>&1; then
56                                 echo "$first"
57                         else
58                                 processarg "$rest"
59                         fi
60                         ;;
61                 # other args stay as is
62                 *)
63                         echo "$1"
64                         ;;
65         esac
66 }
67 processargs()
68 {
69         first=true
70         for X in "$@"; do
71                 if $first; then
72                         first=false
73                         # clear arg list
74                         set --
75                 fi
76                 set -- "$@" "`processarg "$X"`"
77         done
78         echo >&2 "In `pwd`: $*"
79         "$@"
80 }
81
82 # save stdin
83 exec 3<&0
84
85 # recurse through all sub-repos
86 status=0
87 # TODO is there a better way to identify all sub-repos?
88 find . -type d -name \*.git -prune | while IFS= read -r GITDIR; do
89         # TODO I would LIKE to do this, but then some commands (like pull) fail
90         #export GIT_DIR="$GITDIR"
91         #export GIT_WORK_TREE="${GITDIR%/.git}"
92         # so I will have to chdir instead
93
94         ( cd "$GITDIR/.." && processargs git "$@" <&3 3<&- ) # use restored stdin
95         if [ "$?" -gt "$status" ]; then
96                 status=$?
97         fi
98 done
99 exit "$status"