scripts: "export VAR=VALUE" construct is not portable
[git] / git-remote-testgit.sh
1 #!/bin/sh
2 # Copyright (c) 2012 Felipe Contreras
3
4 alias=$1
5 url=$2
6
7 dir="$GIT_DIR/testgit/$alias"
8 prefix="refs/testgit/$alias"
9
10 default_refspec="refs/heads/*:${prefix}/heads/*"
11
12 refspec="${GIT_REMOTE_TESTGIT_REFSPEC-$default_refspec}"
13
14 test -z "$refspec" && prefix="refs"
15
16 GIT_DIR="$url/.git"
17 export GIT_DIR
18
19 mkdir -p "$dir"
20
21 if test -z "$GIT_REMOTE_TESTGIT_NO_MARKS"
22 then
23         gitmarks="$dir/git.marks"
24         testgitmarks="$dir/testgit.marks"
25         test -e "$gitmarks" || >"$gitmarks"
26         test -e "$testgitmarks" || >"$testgitmarks"
27 fi
28
29 while read line
30 do
31         case $line in
32         capabilities)
33                 echo 'import'
34                 echo 'export'
35                 test -n "$refspec" && echo "refspec $refspec"
36                 if test -n "$gitmarks"
37                 then
38                         echo "*import-marks $gitmarks"
39                         echo "*export-marks $gitmarks"
40                 fi
41                 test -n "$GIT_REMOTE_TESTGIT_SIGNED_TAGS" && echo "signed-tags"
42                 test -n "$GIT_REMOTE_TESTGIT_NO_PRIVATE_UPDATE" && echo "no-private-update"
43                 echo
44                 ;;
45         list)
46                 git for-each-ref --format='? %(refname)' 'refs/heads/'
47                 head=$(git symbolic-ref HEAD)
48                 echo "@$head HEAD"
49                 echo
50                 ;;
51         import*)
52                 # read all import lines
53                 while true
54                 do
55                         ref="${line#* }"
56                         refs="$refs $ref"
57                         read line
58                         test "${line%% *}" != "import" && break
59                 done
60
61                 if test -n "$gitmarks"
62                 then
63                         echo "feature import-marks=$gitmarks"
64                         echo "feature export-marks=$gitmarks"
65                 fi
66
67                 if test -n "$GIT_REMOTE_TESTGIT_FAILURE"
68                 then
69                         echo "feature done"
70                         exit 1
71                 fi
72
73                 echo "feature done"
74                 git fast-export \
75                         ${testgitmarks:+"--import-marks=$testgitmarks"} \
76                         ${testgitmarks:+"--export-marks=$testgitmarks"} \
77                         $refs |
78                 sed -e "s#refs/heads/#${prefix}/heads/#g"
79                 echo "done"
80                 ;;
81         export)
82                 if test -n "$GIT_REMOTE_TESTGIT_FAILURE"
83                 then
84                         # consume input so fast-export doesn't get SIGPIPE;
85                         # git would also notice that case, but we want
86                         # to make sure we are exercising the later
87                         # error checks
88                         while read line; do
89                                 test "done" = "$line" && break
90                         done
91                         exit 1
92                 fi
93
94                 before=$(git for-each-ref --format=' %(refname) %(objectname) ')
95
96                 git fast-import \
97                         ${testgitmarks:+"--import-marks=$testgitmarks"} \
98                         ${testgitmarks:+"--export-marks=$testgitmarks"} \
99                         --quiet
100
101                 # figure out which refs were updated
102                 git for-each-ref --format='%(refname) %(objectname)' |
103                 while read ref a
104                 do
105                         case "$before" in
106                         *" $ref $a "*)
107                                 continue ;;     # unchanged
108                         esac
109                         if test -z "$GIT_REMOTE_TESTGIT_PUSH_ERROR"
110                         then
111                                 echo "ok $ref"
112                         else
113                                 echo "error $ref $GIT_REMOTE_TESTGIT_PUSH_ERROR"
114                         fi
115                 done
116
117                 echo
118                 ;;
119         '')
120                 exit
121                 ;;
122         esac
123 done