Repository navigation
Expand file tree
/
Copy pathtest.sh
More file actions
executable file
·285 lines (252 loc) · 7.59 KB
/
Copy pathtest.sh
File metadata and controls
executable file
·285 lines (252 loc) · 7.59 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/env bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PASS=0
FAIL=0
TMPDIR=""
cleanup() {
if [ -n "$TMPDIR" ] && [ -d "$TMPDIR" ]; then
rm -rf "$TMPDIR"
fi
}
trap cleanup EXIT
setup_repo() {
TMPDIR="$(mktemp -d)"
cd "$TMPDIR"
git init -q
git config user.email "test@test.com"
git config user.name "Test"
cat > file.txt <<'CONTENT'
line 1
line 2
line 3
line 4
line 5
line 6
line 7
line 8
line 9
line 10
line 11
line 12
line 13
line 14
line 15
line 16
line 17
line 18
line 19
line 20
CONTENT
git add file.txt
git commit -q -m "initial"
}
make_two_hunks() {
sed -i '3s/.*/line 3 MODIFIED/' file.txt
sed -i '18s/.*/line 18 MODIFIED/' file.txt
}
assert_eq() {
local desc="$1" expected="$2" actual="$3"
if [ "$expected" = "$actual" ]; then
echo " PASS: $desc"
PASS=$((PASS + 1))
else
echo " FAIL: $desc"
echo " expected: $expected"
echo " actual: $actual"
FAIL=$((FAIL + 1))
fi
}
assert_contains() {
local desc="$1" needle="$2" haystack="$3"
if echo "$haystack" | grep -qF "$needle"; then
echo " PASS: $desc"
PASS=$((PASS + 1))
else
echo " FAIL: $desc"
echo " expected to contain: $needle"
echo " got: $haystack"
FAIL=$((FAIL + 1))
fi
}
assert_not_contains() {
local desc="$1" needle="$2" haystack="$3"
if echo "$haystack" | grep -qF "$needle"; then
echo " FAIL: $desc"
echo " should not contain: $needle"
FAIL=$((FAIL + 1))
else
echo " PASS: $desc"
PASS=$((PASS + 1))
fi
}
assert_exit() {
local desc="$1" expected="$2" actual="$3"
assert_eq "$desc (exit code)" "$expected" "$actual"
}
add_hunk() {
python3 "$SCRIPT_DIR/git-add-hunk" "$@"
}
checkout_hunk() {
python3 "$SCRIPT_DIR/git-checkout-hunk" "$@"
}
reset_hunk() {
python3 "$SCRIPT_DIR/git-reset-hunk" "$@"
}
extract_hunk_ids() {
git diff "$@" | grep -oP '@@ -\d+,\d+ \+\d+,\d+' | grep -oP '\+\d+,\d+'
}
extract_cached_hunk_ids() {
git diff --cached "$@" | grep -oP '@@ -\d+,\d+ \+\d+,\d+' | grep -oP '\+\d+,\d+'
}
# --- Tests ---
echo "=== add-hunk: stages single hunk ==="
setup_repo
make_two_hunks
HUNK1="$(extract_hunk_ids | head -1)"
HUNK2="$(extract_hunk_ids | tail -1)"
add_hunk file.txt "$HUNK1"
staged="$(git diff --cached)"
unstaged="$(git diff)"
assert_contains "staged has first change" "+line 3 MODIFIED" "$staged"
assert_not_contains "staged does not have second change" "+line 18 MODIFIED" "$staged"
assert_contains "unstaged still has second change" "+line 18 MODIFIED" "$unstaged"
echo "=== add-hunk: stages multiple hunks ==="
cleanup
setup_repo
make_two_hunks
HUNK1="$(extract_hunk_ids | head -1)"
HUNK2="$(extract_hunk_ids | tail -1)"
add_hunk file.txt "$HUNK1" "$HUNK2"
staged="$(git diff --cached)"
assert_contains "staged has first change" "+line 3 MODIFIED" "$staged"
assert_contains "staged has second change" "+line 18 MODIFIED" "$staged"
unstaged="$(git diff)"
assert_eq "nothing left unstaged" "" "$unstaged"
echo "=== add-hunk: unknown hunk ID exits 1 ==="
cleanup
setup_repo
make_two_hunks
HUNK1="$(extract_hunk_ids | head -1)"
exit_code=0
output="$(add_hunk file.txt +99,1 2>&1 || true)"
add_hunk file.txt +99,1 2>/dev/null || exit_code=$?
assert_exit "unknown hunk" "1" "$exit_code"
assert_contains "names the bad ID" "unknown hunk ID: +99,1" "$output"
assert_contains "lists available hunks" "$HUNK1" "$output"
assert_not_contains "no raw @@ header" "@@" "$output"
echo "=== add-hunk: minus-prefix hint ==="
cleanup
setup_repo
make_two_hunks
output="$(add_hunk file.txt -1,6 2>&1 || true)"
assert_contains "hints about old vs new side" "OLD file side" "$output"
echo "=== add-hunk: no args shows usage ==="
output="$(add_hunk 2>&1 || true)"
assert_contains "usage explains format" "+offset,count" "$output"
echo "=== add-hunk: file not in diff exits 1 ==="
cleanup
setup_repo
make_two_hunks
exit_code=0
output="$(add_hunk nosuchfile.txt +1,1 2>&1 || true)"
add_hunk nosuchfile.txt +1,1 2>/dev/null || exit_code=$?
assert_exit "file not in diff" "1" "$exit_code"
assert_contains "error message" "no unstaged changes" "$output"
echo "=== checkout-hunk: discards single hunk ==="
cleanup
setup_repo
make_two_hunks
HUNK1="$(extract_hunk_ids | head -1)"
checkout_hunk file.txt "$HUNK1"
remaining="$(git diff)"
assert_not_contains "first change discarded" "+line 3 MODIFIED" "$remaining"
assert_contains "second change kept" "+line 18 MODIFIED" "$remaining"
echo "=== checkout-hunk: discards all hunks ==="
cleanup
setup_repo
make_two_hunks
HUNK1="$(extract_hunk_ids | head -1)"
HUNK2="$(extract_hunk_ids | tail -1)"
checkout_hunk file.txt "$HUNK1" "$HUNK2"
remaining="$(git diff)"
assert_eq "no changes remain" "" "$remaining"
echo "=== checkout-hunk: unknown hunk ID exits 1 ==="
cleanup
setup_repo
make_two_hunks
exit_code=0
output="$(checkout_hunk file.txt +99,1 2>&1 || true)"
checkout_hunk file.txt +99,1 2>/dev/null || exit_code=$?
assert_exit "unknown hunk" "1" "$exit_code"
assert_contains "names the bad ID" "unknown hunk ID: +99,1" "$output"
assert_not_contains "no raw @@ header" "@@" "$output"
echo "=== checkout-hunk: minus-prefix hint ==="
cleanup
setup_repo
make_two_hunks
output="$(checkout_hunk file.txt -1,6 2>&1 || true)"
assert_contains "hints about old vs new side" "OLD file side" "$output"
echo "=== checkout-hunk: no args shows usage ==="
output="$(checkout_hunk 2>&1 || true)"
assert_contains "usage explains format" "+offset,count" "$output"
echo "=== checkout-hunk: file not in diff exits 1 ==="
cleanup
setup_repo
make_two_hunks
exit_code=0
output="$(checkout_hunk nosuchfile.txt +1,1 2>&1 || true)"
checkout_hunk nosuchfile.txt +1,1 2>/dev/null || exit_code=$?
assert_exit "file not in diff" "1" "$exit_code"
assert_contains "error message" "no unstaged changes" "$output"
echo "=== reset-hunk: unstages single hunk ==="
cleanup
setup_repo
make_two_hunks
git add file.txt
HUNK1="$(extract_cached_hunk_ids | head -1)"
HUNK2="$(extract_cached_hunk_ids | tail -1)"
reset_hunk file.txt "$HUNK1"
staged="$(git diff --cached)"
unstaged="$(git diff)"
assert_not_contains "first change unstaged" "+line 3 MODIFIED" "$staged"
assert_contains "second change still staged" "+line 18 MODIFIED" "$staged"
assert_contains "first change back in working tree" "+line 3 MODIFIED" "$unstaged"
echo "=== reset-hunk: unstages all hunks ==="
cleanup
setup_repo
make_two_hunks
git add file.txt
HUNK1="$(extract_cached_hunk_ids | head -1)"
HUNK2="$(extract_cached_hunk_ids | tail -1)"
reset_hunk file.txt "$HUNK1" "$HUNK2"
staged="$(git diff --cached)"
assert_eq "nothing left staged" "" "$staged"
unstaged="$(git diff)"
assert_contains "first change back unstaged" "+line 3 MODIFIED" "$unstaged"
assert_contains "second change back unstaged" "+line 18 MODIFIED" "$unstaged"
echo "=== reset-hunk: unknown hunk ID exits 1 ==="
cleanup
setup_repo
make_two_hunks
git add file.txt
exit_code=0
output="$(reset_hunk file.txt +99,1 2>&1 || true)"
reset_hunk file.txt +99,1 2>/dev/null || exit_code=$?
assert_exit "unknown hunk" "1" "$exit_code"
assert_contains "names the bad ID" "unknown hunk ID: +99,1" "$output"
echo "=== reset-hunk: file not staged exits 1 ==="
cleanup
setup_repo
make_two_hunks
exit_code=0
output="$(reset_hunk file.txt +1,1 2>&1 || true)"
reset_hunk file.txt +1,1 2>/dev/null || exit_code=$?
assert_exit "file not staged" "1" "$exit_code"
assert_contains "error message" "no staged changes" "$output"
echo "=== reset-hunk: no args shows usage ==="
output="$(reset_hunk 2>&1 || true)"
assert_contains "usage explains format" "+offset,count" "$output"
echo ""
echo "Results: $PASS passed, $FAIL failed"
[ "$FAIL" -eq 0 ]