aboutsummaryrefslogtreecommitdiffstats
path: root/t/common.sh
blob: 38d28458b892c0009ab026f48d21147bbfe51baa (plain) (blame)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
set -eu

tmp=testdir.$$
command="../git-rediff"

interactive () {
	test $# = 0
	command="../git-rediff.t"
}

cleanup () {
	cd -- ..
	rm -rf -- "${tmp}"
}

trap cleanup 0 HUP INT TERM

mkdir -- "${tmp}"
cd -- "${tmp}"
git init -q .
git config user.email test@example.invalid
git config user.name test

set_marker_variables () {
	test $# = 1
	HEAD="$(printf '%*.s\n' "$1" '' | tr ' ' '<')"
	PARENT="$(printf '%*.s\n' "$1" '' | tr ' ' '|')"
	TAIL="$(printf '%*.s\n' "$1" '' | tr ' ' '=')"
	END="$(printf '%*.s\n' "$1" '' | tr ' ' '>')"
}

set_marker_variables 7

set_conflict_marker_size () {
	test $# = 2
	# $1 = marker size
	# $2 = affected file
	set_marker_variables "$1"
	printf '%s conflict-marker-size=%s\n' "$2" "$1" >> .gitattributes
}

again_command=''

save_files () {
	while test ! $# = 0; do
		if test "$1" = - || test "$1" = --; then
			shift 1
			break
		elif printf '%s\n' "$1" | grep -q '^-'; then
			shift 1
		else
			break
		fi
	done
	for f; do
		if test ! "$f" = -; then
			cp -- "$f" "$f".original
		fi
	done
}

no_error () {
	test -z "${again_command}"
	again_command="$*"
	shift 1
	save_files "$@"
	set +e
	"$command" "$@"
	status=$?
	set -e
	while test ! $# = 0; do
		if test "$1" = - || test "$1" = --; then
			shift 1
			break
		elif printf '%s\n' "$1" | grep -q '^-'; then
			shift 1
		else
			break
		fi
	done
	for f; do
		if test ! "$f" = -; then
			diff -u -- "$f" "$f".expected >&2
			cp -- "$f".original "$f"
		fi
	done
}

normal () {
	no_error normal "$@"
	test $status = 0
}

conflict () {
	no_error conflict "$@"
	test $status = 1
}

error () {
	set +e
	"$command" "$@" 2>/dev/null
	status=$?
	set -e
	test $status = 2
}

also_interactive () {
	test $# = 1
	test -n "${again_command}"
	cmd="${again_command}"
	again_command=''
	interactive
	printf '%s' "$1" > i-input
	${cmd} 8<i-input 9>/dev/null
}

interactive_line () {
	test $# = 2
	printf '\e[%sm%s\n\e[0m' "$1" "$2"
}

interactive_marker () {
	test $# = 1
	interactive_line 36 "$1"
}

interactive_side () {
	test $# = 1
	interactive_line 32 "$1"
}

interactive_base () {
	test $# = 1
	interactive_line 31 "$1"
}

interactive_filename () {
	test $# = 1
	printf '\e[1m%s\e[0m\n' "$1"
}

interactive_prompt () {
	test $# = 2
	printf '\e[1;34m(%s/%s) Resolve this conflict [h,t,b,m,y,r,e,s,d,q,?]? \e[0m\n' "$1" "$2"
}

interactive_help () {
	test $# = 1
	printf '\e[1;31m'
	printf '%s\n' \
		'h - select head' \
		't - select tail'
	if test "$1" = 3; then
		printf '%s\n' 'b - remove base'
	fi
	printf '%s\n' \
		'm - merge' \
		'y - select symmetric branches' \
		'r - reduce' \
		'e - manually edit this hunk' \
		's - skip this hunk' \
		'd - skip this hunk and all later hunks in the file' \
		'q - quit; skip this hunk and all remaining hunks' \
		'? - print help'
	printf '\e[0m'
}