chiark / gitweb /
Test suite: tartree-edit: break out git_manip_play (nfc)
[dgit.git] / tests / tartree-edit
1 #!/bin/sh
2 set -e
3 fail () { echo >&2 "$0: $*"; exit 1; }
4
5 play=.git/tartree-edit-work
6
7 git_manip_play () {
8         local wd=$(pwd)
9         case "$wd" in
10         *.edit) fail "bad idea to run gitfetchinfo into a .edit tree!" ;;
11         esac
12         rm -rf $play
13         mkdir $play
14 }
15
16 case "$#.$1" in
17 2.edit|2.done)  mode="$1"; arg="$2" ;;
18 3.gitfetchinfo) mode="$1"; arg="$2"; remote="$3" ;;
19 ?.-*)   fail "no options understood"                    ;;
20 *)      fail "usage:
21     tartree-edit edit|done DIRECTORY|TARBALL
22     tartree-edit gitfetchinfo DIRECTORY|TARBALL REMOTE" ;;
23 esac
24
25 case "$arg" in
26 *.tar)          base=${arg%.tar}                        ;;
27 *.edit)         base=${arg%.edit}                       ;;
28 *)              base=${arg}                             ;;
29 esac
30
31 tryat_pre () {
32         local b="$1"
33         rm -rf "$b.tmp"
34         if test -f "$b.tar" && test -f "$b.edit"; then
35                 echo "$b.edit exists, deleting possibly-obsolete $b.tar"
36                 rm "$b.tar"
37         fi
38 }
39
40 tryat_edit () {
41         local b="$1"
42         if test -d "$b.edit"; then
43                 echo "$b.edit already exists"
44                 exit 0
45         fi
46         if test -f "$b.tar"; then
47                 mkdir "$b.tmp"
48                 (set -e; cd "$b.tmp"; tar xf "$b.tar")
49                 mv "$b.tmp" "$b.edit"
50                 rm "$b.tar"
51                 echo "$b.edit ready"
52                 exit 0
53         fi
54 }
55
56 gitfetchinfo_perhaps_commit () {
57         local m="$1"
58         set +e
59         git diff --cached --quiet --exit-code HEAD
60         local rc=$?
61         set -e
62         case "$rc" in
63         0)   return ;;
64         1)   git commit --allow-empty --author='tartree-edit <>' -m "$m" ;;
65         *)   fail "git diff failed ($rc)" ;;
66         esac
67 }
68
69 tryat_gitfetchinfo () {
70         git_manip_play
71         if test -d "$b.edit"; then
72                 cp -a "$b.edit"/. "$play"/.
73         else
74                 exec 3<"$b.tar"
75                 tar -C $play -f - <&3 -x
76                 exec 3<&-
77         fi
78         local innerwd=$play/*
79         git remote remove "$remote" 2>/dev/null ||:
80         git remote add "$remote" $innerwd
81         git fetch --no-tags -p "$remote" \
82                 +"HEAD:refs/remotes/$remote/HEAD"
83         cd $innerwd
84         git checkout -b WORKTREE
85         gitfetchinfo_perhaps_commit INDEX
86         git add -Af .
87         gitfetchinfo_perhaps_commit WORKTREE
88         cd ../../..
89         git fetch --no-tags "$remote" --refmap \
90                 +"refs/*:refs/remotes/$remote/*" \
91                 +"refs/*:refs/remotes/$remote/*"
92         exit 0
93 }
94
95 tryat_done () {
96         local b="$1"
97         if test -d "$b.edit"; then
98                 (set -e; cd "$b.edit"; tar cf "$b.tmp" *)
99                 mv "$b.tmp" "$b.tar"
100                 mv "$b.edit" "$b.tmp"
101                 rm -rf "$b.tmp"
102                 echo "$b.tar regenerated"
103                 exit 0
104         fi
105         if test -f "$b.tar"; then
106                 echo "$b.tar already exists and $b.edit doesn't"
107                 exit 0
108         fi
109 }
110
111 tryat () {
112         local b="$1"
113         if ! test -f "$b.tar" && ! test -d "$b.edit"; then
114                 return
115         fi
116         tryat_pre "$b"
117         tryat_$mode "$b"
118         fail "unexpected situation in $b.*"
119 }
120
121 case "$arg" in
122 /*)             tryat "$base"
123                 ;;
124 *)
125                 pwd=`pwd`
126                 tryat "$pwd/$base"
127                 tryat "$pwd/git-srcs/$base"
128                 tryat "$pwd/tests/git-srcs/$base"
129                 fail "could not find $base..."
130                 ;;
131 esac