chiark / gitweb /
d4183f1e95f1dcf4a3490e6ce388bec34b12873d
[dgit.git] / tests / gitrepo-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 2.-*)           fail "no options understood"                    ;;
8 *)              fail "usage: gitrepo-edit edit|done DIRECTORY"  ;;
9 esac
10
11 case "$arg" in
12 *.git.tar)      base=${arg%.tar}                        ;;
13 *.git.edit)     base=${arg%.edit}                       ;;
14 *.git)          base=${arg}                             ;;
15 *)              fail "arg must end in .git[.tar|.edit]" ;;
16 esac
17
18 tryat_pre () {
19         local b="$1"
20         rm -rf "$b.tmp"
21         if test -f "$b.tar" && test -f "$b.edit"; then
22                 echo "$b.edit exists, deleting possibly-obsolete $b.tar"
23                 rm "$b.tar"
24         fi
25 }
26
27 tryat_edit () {
28         local b="$1"
29         if test -d "$b.edit"; then
30                 echo "$b.edit already exists"
31                 exit 0
32         fi
33         if test -f "$b.tar"; then
34                 mkdir "$b.tmp"
35                 (set -e; cd "$b.tmp"; tar xf "$b.tar")
36                 mv "$b.tmp" "$b.edit"
37                 rm "$b.tar"
38                 echo "$b.edit ready"
39                 exit 0
40         fi
41 }
42
43 tryat_done () {
44         local b="$1"
45         if test -d "$b.edit"; then
46                 (set -e; cd "$b.edit"; tar cf "$b.tmp" *)
47                 mv "$b.tmp" "$b.tar"
48                 mv "$b.edit" "$b.tmp"
49                 rm -rf "$b.tmp"
50                 echo "$b.tar regenerated"
51                 exit 0
52         fi
53         if test -f "$b.tar"; then
54                 echo "$b.tar already exists and $b.edit doesn't"
55                 exit 0
56         fi
57 }
58
59 tryat () {
60         local b="$1"
61         if ! test -f "$b.tar" && ! test -d "$b.edit"; then
62                 return
63         fi
64         tryat_pre "$b"
65         tryat_$mode "$b"
66         fail "unexpected situation in $b.*"
67 }
68
69 case "$arg" in
70 /*)             tryat "$base"
71                 ;;
72 *)
73                 pwd=`pwd`
74                 tryat "$pwd/$base"
75                 tryat "$pwd/git-srcs/$base"
76                 tryat "$pwd/tests/git-srcs/$base"
77                 fail "could not find $base..."
78                 ;;
79 esac