git-recurse: comments
[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 # recurse through all sub-repos
83 status=0
84 for GITDIR in `find . -type d -name \*.git`; do
85         # TODO I would LIKE to do this, but then some commands (like pull) fail
86         #export GIT_DIR="$GITDIR"
87         #export GIT_WORK_TREE="${GITDIR%/.git}"
88         # so I will have to chdir instead
89
90         ( cd "$GITDIR/.." && processargs git "$@" )
91         if [ "$?" -gt "$status" ]; then
92                 status=$?
93         fi
94 done
95 exit "$status"