Skip to content

Commit b1c8ac3

Browse files
peffgitster
authored andcommitted
t7001: avoid git on upstream of pipe
We generally avoid git on the left-hand side of a pipe, because it loses the exit code of the command (and thus we'd miss things like segfaults or unexpected failures). In the cases in t7001, we wouldn't expect failures (they are just inspecting the repository state, and are not the main point of the test), but it doesn't hurt to be careful. In all but one case here we're piping "ls-files --stage" to cut off the pathname (since we compare entries before and after moving). Let's pull that into a helper function to avoid repeating the slightly awkward replacement. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 69c7866 commit b1c8ac3

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

t/t7001-mv.sh

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ test_description='git mv in subdirs'
44
. ./test-lib.sh
55
. "$TEST_DIRECTORY"/lib-diff-data.sh
66

7+
index_at_path () {
8+
entry=$(git ls-files --stage "$@") &&
9+
echo "$entry" | cut -f 1
10+
}
11+
712
test_expect_success 'mv -f refreshes updated index entry' '
813
echo test >bar &&
914
git add bar &&
@@ -187,7 +192,8 @@ test_expect_success "Michael Cassar's test case" '
187192
git mv papers/unsorted/Thesis.pdf papers/all-papers/moo-blah.pdf &&
188193
189194
T=$(git write-tree) &&
190-
git ls-tree -r $T | verbose grep partA/outline.txt
195+
git ls-tree -r $T >out &&
196+
verbose grep partA/outline.txt out
191197
'
192198

193199
rm -fr papers partA path?
@@ -260,12 +266,12 @@ test_expect_success 'git mv should not change sha1 of moved cache entry' '
260266
git init &&
261267
echo 1 >dirty &&
262268
git add dirty &&
263-
entry="$(git ls-files --stage dirty | cut -f 1)" &&
269+
entry="$(index_at_path dirty)" &&
264270
git mv dirty dirty2 &&
265-
test "$entry" = "$(git ls-files --stage dirty2 | cut -f 1)" &&
271+
test "$entry" = "$(index_at_path dirty2)" &&
266272
echo 2 >dirty2 &&
267273
git mv dirty2 dirty &&
268-
test "$entry" = "$(git ls-files --stage dirty | cut -f 1)"
274+
test "$entry" = "$(index_at_path dirty)"
269275
'
270276

271277
rm -f dirty dirty2
@@ -342,7 +348,7 @@ test_expect_success 'git mv cannot move a submodule in a file' '
342348
'
343349

344350
test_expect_success 'git mv moves a submodule with a .git directory and no .gitmodules' '
345-
entry="$(git ls-files --stage sub | cut -f 1)" &&
351+
entry="$(index_at_path sub)" &&
346352
git rm .gitmodules &&
347353
(
348354
cd sub &&
@@ -353,7 +359,7 @@ test_expect_success 'git mv moves a submodule with a .git directory and no .gitm
353359
mkdir mod &&
354360
git mv sub mod/sub &&
355361
test_path_is_missing sub &&
356-
test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
362+
test "$entry" = "$(index_at_path mod/sub)" &&
357363
git -C mod/sub status &&
358364
git update-index --refresh &&
359365
git diff-files --quiet
@@ -363,7 +369,7 @@ test_expect_success 'git mv moves a submodule with a .git directory and .gitmodu
363369
rm -rf mod &&
364370
git reset --hard &&
365371
git submodule update &&
366-
entry="$(git ls-files --stage sub | cut -f 1)" &&
372+
entry="$(index_at_path sub)" &&
367373
(
368374
cd sub &&
369375
rm -f .git &&
@@ -373,7 +379,7 @@ test_expect_success 'git mv moves a submodule with a .git directory and .gitmodu
373379
mkdir mod &&
374380
git mv sub mod/sub &&
375381
test_path_is_missing sub &&
376-
test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
382+
test "$entry" = "$(index_at_path mod/sub)" &&
377383
git -C mod/sub status &&
378384
echo mod/sub >expected &&
379385
git config -f .gitmodules submodule.sub.path >actual &&
@@ -386,11 +392,11 @@ test_expect_success 'git mv moves a submodule with gitfile' '
386392
rm -rf mod &&
387393
git reset --hard &&
388394
git submodule update &&
389-
entry="$(git ls-files --stage sub | cut -f 1)" &&
395+
entry="$(index_at_path sub)" &&
390396
mkdir mod &&
391397
git -C mod mv ../sub/ . &&
392398
test_path_is_missing sub &&
393-
test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
399+
test "$entry" = "$(index_at_path mod/sub)" &&
394400
git -C mod/sub status &&
395401
echo mod/sub >expected &&
396402
git config -f .gitmodules submodule.sub.path >actual &&
@@ -404,12 +410,12 @@ test_expect_success 'mv does not complain when no .gitmodules file is found' '
404410
git reset --hard &&
405411
git submodule update &&
406412
git rm .gitmodules &&
407-
entry="$(git ls-files --stage sub | cut -f 1)" &&
413+
entry="$(index_at_path sub)" &&
408414
mkdir mod &&
409415
git mv sub mod/sub 2>actual.err &&
410416
test_must_be_empty actual.err &&
411417
test_path_is_missing sub &&
412-
test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
418+
test "$entry" = "$(index_at_path mod/sub)" &&
413419
git -C mod/sub status &&
414420
git update-index --refresh &&
415421
git diff-files --quiet
@@ -420,7 +426,7 @@ test_expect_success 'mv will error out on a modified .gitmodules file unless sta
420426
git reset --hard &&
421427
git submodule update &&
422428
git config -f .gitmodules foo.bar true &&
423-
entry="$(git ls-files --stage sub | cut -f 1)" &&
429+
entry="$(index_at_path sub)" &&
424430
mkdir mod &&
425431
test_must_fail git mv sub mod/sub 2>actual.err &&
426432
test_file_not_empty actual.err &&
@@ -430,7 +436,7 @@ test_expect_success 'mv will error out on a modified .gitmodules file unless sta
430436
git mv sub mod/sub 2>actual.err &&
431437
test_must_be_empty actual.err &&
432438
test_path_is_missing sub &&
433-
test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
439+
test "$entry" = "$(index_at_path mod/sub)" &&
434440
git -C mod/sub status &&
435441
git update-index --refresh &&
436442
git diff-files --quiet
@@ -442,13 +448,13 @@ test_expect_success 'mv issues a warning when section is not found in .gitmodule
442448
git submodule update &&
443449
git config -f .gitmodules --remove-section submodule.sub &&
444450
git add .gitmodules &&
445-
entry="$(git ls-files --stage sub | cut -f 1)" &&
451+
entry="$(index_at_path sub)" &&
446452
echo "warning: Could not find section in .gitmodules where path=sub" >expect.err &&
447453
mkdir mod &&
448454
git mv sub mod/sub 2>actual.err &&
449455
test_cmp expect.err actual.err &&
450456
test_path_is_missing sub &&
451-
test "$entry" = "$(git ls-files --stage mod/sub | cut -f 1)" &&
457+
test "$entry" = "$(index_at_path mod/sub)" &&
452458
git -C mod/sub status &&
453459
git update-index --refresh &&
454460
git diff-files --quiet

0 commit comments

Comments
 (0)