summaryrefslogtreecommitdiff
path: root/tests/gitrepo-edit
blob: 27be703a64b5747973fdb70dfdffb9f4d0848c44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/bin/sh
set -e
fail () { echo >&2 "$0: $*"; exit 1; }

case "$#.$1" in
2.edit|2.done)	mode="$1"; arg="$2" ;;
2.-*)		fail "no options understood"			;;
*)		fail "usage: gitrepo-edit edit|done DIRECTORY"	;;
esac

case "$arg" in
*.git.tar)	base=${arg%.tar}			;;
*.git.edit)	base=${arg%.edit}			;;
*.git)		base=${arg}				;;
*)		fail "arg must end in .git[.tar]"	;;
esac

tryat_pre () {
	local b="$1"
	rm -rf "$b.tmp"
	if test -f "$b.tar" && test -f "$b.edit"; then
		echo "$b.edit exists, deleting possibly-obsolete $b.tar"
		rm "$b.tar"
	fi
}

tryat_edit () {
	local b="$1"
	local i="$2"
	if test -d "$b.edit"; then
		echo "$b.edit already exists"
		exit 0
	fi
	if test -f "$b.tar"; then
		mkdir "$b.tmp"
		(set -e; cd "$b.tmp"; tar xf "$b.tar")
		mv "$b.tmp/$i" "$b.edit"
		rm "$b.tar"
		rm -rf "$b.tmp"
		echo "$b.edit ready"
		exit 0
	fi
}

tryat_done () {
	local b="$1"
	local i="$2"
	if test -d "$b.edit"; then
		mkdir "$b.tmp"
		cp -al "$b.edit" "$b.tmp/$i"
		(set -e; cd "$b.tmp"; tar cf "$b.tmp/tar $i")
		mv "$b.tmp/tar" "$b.tar"
		rm -rf "$b.tmp"
		mv "$b.edit" "$b.tmp"
		rm -rf "$b.tmp"
		echo "$b.tar regenerated"
		exit 0
	fi
	if test -f "$b.tar"; then
		echo "$b.tar already exists and $b.edit doesn't"
		exit 0
	fi
}

tryat () {
	local b="$1"
	local i="${b##*/}"
	case "$i" in *_*.git) i="${i%_*.git}.git";; esac
	if ! test -f "$b.tar" && ! test -f "$b.edit"; then
		return
	fi
	tryat_pre "$b" "$i"
	tryat_$mode "$b" "$i"
	fail "unexpected situation in $b.*"
}

case "$arg" in
/*)		tryat "$base"
		;;
*)
		pwd=`pwd`
		tryat "$pwd/$base"
		tryat "$pwd/git-srcs/$base"
		tryat "$pwd/tests/git-srcs/$base"
		fail "could not find $base..."
		;;
esac