c27b6d9e056ae92bf1bf7a48bfbb96854718a673
[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                 *..*)
15                         first=${1%%..*}
16                         rest=${1#*..}
17                         first=`processarg "$first"`
18                         rest=`processarg "$rest"`
19                         echo "$first..$rest"
20                         ;;
21                 *@\{*)
22                         first=${1%%@\{*}
23                         rest=${1#*@\{}
24                         first=`processarg "$first"`
25                         echo "$first@{$rest"
26                         ;;
27                 *^*)
28                         first=${1%%^*}
29                         rest=${1#*^}
30                         first=`processarg "$first"`
31                         echo "$first^$rest"
32                         ;;
33                 *~*)
34                         first=${1%%~*}
35                         rest=${1#*~}
36                         first=`processarg "$first"`
37                         echo "$first~$rest"
38                         ;;
39                 *:*)
40                         first=${1%%:*}
41                         rest=${1#*:}
42                         first=`processarg "$first"`
43                         echo "$first:$rest"
44                         ;;
45                 ^*)
46                         first=${1#^}
47                         first=`processarg "$first"`
48                         echo "^$first"
49                         ;;
50                 *$SEPARATOR*)
51                         first=${1%%$SEPARATOR*}
52                         rest=${1#*$SEPARATOR}
53                         if git rev-parse "$first" >/dev/null 2>&1; then
54                                 echo "$first"
55                         else
56                                 processarg "$rest"
57                         fi
58                         ;;
59                 *)
60                         echo "$1"
61                         ;;
62         esac
63 }
64 processargs()
65 {
66         first=true
67         for X in "$@"; do
68                 if $first; then
69                         first=false
70                         # clear arg list
71                         set --
72                 fi
73                 set -- "$@" "`processarg "$X"`"
74         done
75         echo >&2 "In `pwd`: $*"
76         "$@"
77 }
78
79 # recurse through all sub-repos
80 status=0
81 for GITDIR in `find . -type d -name \*.git`; do
82         # TODO I would LIKE to do this, but then some commands (like pull) fail
83         #export GIT_DIR="$GITDIR"
84         #export GIT_WORK_TREE="${GITDIR%/.git}"
85         # so I will have to chdir instead
86
87         ( cd "$GITDIR/.." && processargs git "$@" )
88         if [ "$?" -gt "$status" ]; then
89                 status=$?
90         fi
91 done
92 exit "$status"