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