transport-helper: report errors properly
[git] / git-remote-testgit
1 #!/usr/bin/env bash
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         testgitmarks_args=( "--"{import,export}"-marks=$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                 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 "${testgitmarks_args[@]}" $refs |
73                 sed -e "s#refs/heads/#${prefix}/heads/#g"
74                 echo "done"
75                 ;;
76         export)
77                 if test -n "$GIT_REMOTE_TESTGIT_FAILURE"
78                 then
79                         # consume input so fast-export doesn't get SIGPIPE;
80                         # git would also notice that case, but we want
81                         # to make sure we are exercising the later
82                         # error checks
83                         while read line; do
84                                 test "done" = "$line" && break
85                         done
86                         exit 1
87                 fi
88
89                 before=$(git for-each-ref --format='%(refname) %(objectname)')
90
91                 git fast-import "${testgitmarks_args[@]}" --quiet
92
93                 after=$(git for-each-ref --format='%(refname) %(objectname)')
94
95                 # figure out which refs were updated
96                 join -e 0 -o '0 1.2 2.2' -a 2 <(echo "$before") <(echo "$after") |
97                 while read ref a b
98                 do
99                         test $a == $b && continue
100                         echo "ok $ref"
101                 done
102
103                 echo
104                 ;;
105         '')
106                 exit
107                 ;;
108         esac
109 done