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