Merge branch 'fc/transport-helper-error-reporting'
[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                 test -n "$GIT_REMOTE_TESTGIT_SIGNED_TAGS" && echo "signed-tags"
42                 echo
43                 ;;
44         list)
45                 git for-each-ref --format='? %(refname)' 'refs/heads/'
46                 head=$(git symbolic-ref HEAD)
47                 echo "@$head HEAD"
48                 echo
49                 ;;
50         import*)
51                 # read all import lines
52                 while true
53                 do
54                         ref="${line#* }"
55                         refs="$refs $ref"
56                         read line
57                         test "${line%% *}" != "import" && break
58                 done
59
60                 if test -n "$gitmarks"
61                 then
62                         echo "feature import-marks=$gitmarks"
63                         echo "feature export-marks=$gitmarks"
64                 fi
65
66                 if test -n "$GIT_REMOTE_TESTGIT_FAILURE"
67                 then
68                         echo "feature done"
69                         exit 1
70                 fi
71
72                 echo "feature done"
73                 git fast-export "${testgitmarks_args[@]}" $refs |
74                 sed -e "s#refs/heads/#${prefix}/heads/#g"
75                 echo "done"
76                 ;;
77         export)
78                 if test -n "$GIT_REMOTE_TESTGIT_FAILURE"
79                 then
80                         # consume input so fast-export doesn't get SIGPIPE;
81                         # git would also notice that case, but we want
82                         # to make sure we are exercising the later
83                         # error checks
84                         while read line; do
85                                 test "done" = "$line" && break
86                         done
87                         exit 1
88                 fi
89
90                 before=$(git for-each-ref --format='%(refname) %(objectname)')
91
92                 git fast-import "${testgitmarks_args[@]}" --quiet
93
94                 after=$(git for-each-ref --format='%(refname) %(objectname)')
95
96                 # figure out which refs were updated
97                 join -e 0 -o '0 1.2 2.2' -a 2 <(echo "$before") <(echo "$after") |
98                 while read ref a b
99                 do
100                         test $a == $b && continue
101                         if test -z "$GIT_REMOTE_TESTGIT_PUSH_ERROR"
102                         then
103                                 echo "ok $ref"
104                         else
105                                 echo "error $ref $GIT_REMOTE_TESTGIT_PUSH_ERROR"
106                         fi
107                 done
108
109                 echo
110                 ;;
111         '')
112                 exit
113                 ;;
114         esac
115 done