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