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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
May 8, 2023 at 15:01 UTC
b1c8ac3996e25c5764431f5ede122684474415c5
1 file changed
+22
-16
t/t7001-mv.sh
+22
-16
@@ -4,6 +4,11 @@ test_description='git mv in subdirs'
4
. ./test-lib.sh
5
. "$TEST_DIRECTORY"/lib-diff-data.sh
6
7
+index_at_path () {
8
+ entry=$(git ls-files --stage "$@") &&
9
+ echo "$entry" | cut -f 1
10
+}
11
+
12
test_expect_success 'mv -f refreshes updated index entry' '
13
echo test >bar &&
14
git add bar &&
@@ -187,7 +192,8 @@ test_expect_success "Michael Cassar's test case" '
192
git mv papers/unsorted/Thesis.pdf papers/all-papers/moo-blah.pdf &&
193
194
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
197
'
198
199
rm -fr papers partA path?
@@ -260,12 +266,12 @@ test_expect_success 'git mv should not change sha1 of moved cache entry' '
266
git init &&
267
echo 1 >dirty &&
268
git add dirty &&
263
- entry="$(git ls-files --stage dirty | cut -f 1)" &&
269
+ entry="$(index_at_path dirty)" &&
270
git mv dirty dirty2 &&
265
- test "$entry" = "$(git ls-files --stage dirty2 | cut -f 1)" &&
271
+ test "$entry" = "$(index_at_path dirty2)" &&
272
echo 2 >dirty2 &&
273
git mv dirty2 dirty &&
268
- test "$entry" = "$(git ls-files --stage dirty | cut -f 1)"
274
+ test "$entry" = "$(index_at_path dirty)"
275
'
276
277
rm -f dirty dirty2
@@ -342,7 +348,7 @@ test_expect_success 'git mv cannot move a submodule in a file' '
348
'
349
350
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)" &&
352
git rm .gitmodules &&
353
(
354
cd sub &&
@@ -353,7 +359,7 @@ test_expect_success 'git mv moves a submodule with a .git directory and no .gitm
359
mkdir mod &&
360
git mv sub mod/sub &&
361
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)" &&
363
git -C mod/sub status &&
364
git update-index --refresh &&
365
git diff-files --quiet
@@ -363,7 +369,7 @@ test_expect_success 'git mv moves a submodule with a .git directory and .gitmodu
369
rm -rf mod &&
370
git reset --hard &&
371
git submodule update &&
366
- entry="$(git ls-files --stage sub | cut -f 1)" &&
372
+ entry="$(index_at_path sub)" &&
373
(
374
cd sub &&
375
rm -f .git &&
@@ -373,7 +379,7 @@ test_expect_success 'git mv moves a submodule with a .git directory and .gitmodu
379
mkdir mod &&
380
git mv sub mod/sub &&
381
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)" &&
383
git -C mod/sub status &&
384
echo mod/sub >expected &&
385
git config -f .gitmodules submodule.sub.path >actual &&
@@ -386,11 +392,11 @@ test_expect_success 'git mv moves a submodule with gitfile' '
392
rm -rf mod &&
393
git reset --hard &&
394
git submodule update &&
389
- entry="$(git ls-files --stage sub | cut -f 1)" &&
395
+ entry="$(index_at_path sub)" &&
396
mkdir mod &&
397
git -C mod mv ../sub/ . &&
398
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)" &&
400
git -C mod/sub status &&
401
echo mod/sub >expected &&
402
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' '
410
git reset --hard &&
411
git submodule update &&
412
git rm .gitmodules &&
407
- entry="$(git ls-files --stage sub | cut -f 1)" &&
413
+ entry="$(index_at_path sub)" &&
414
mkdir mod &&
415
git mv sub mod/sub 2>actual.err &&
416
test_must_be_empty actual.err &&
417
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)" &&
419
git -C mod/sub status &&
420
git update-index --refresh &&
421
git diff-files --quiet
@@ -420,7 +426,7 @@ test_expect_success 'mv will error out on a modified .gitmodules file unless sta
426
git reset --hard &&
427
git submodule update &&
428
git config -f .gitmodules foo.bar true &&
423
- entry="$(git ls-files --stage sub | cut -f 1)" &&
429
+ entry="$(index_at_path sub)" &&
430
mkdir mod &&
431
test_must_fail git mv sub mod/sub 2>actual.err &&
432
test_file_not_empty actual.err &&
@@ -430,7 +436,7 @@ test_expect_success 'mv will error out on a modified .gitmodules file unless sta
436
git mv sub mod/sub 2>actual.err &&
437
test_must_be_empty actual.err &&
438
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)" &&
440
git -C mod/sub status &&
441
git update-index --refresh &&
442
git diff-files --quiet
@@ -442,13 +448,13 @@ test_expect_success 'mv issues a warning when section is not found in .gitmodule
448
git submodule update &&
449
git config -f .gitmodules --remove-section submodule.sub &&
450
git add .gitmodules &&
445
- entry="$(git ls-files --stage sub | cut -f 1)" &&
451
+ entry="$(index_at_path sub)" &&
452
echo "warning: Could not find section in .gitmodules where path=sub" >expect.err &&
453
mkdir mod &&
454
git mv sub mod/sub 2>actual.err &&
455
test_cmp expect.err actual.err &&
456
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)" &&
458
git -C mod/sub status &&
459
git update-index --refresh &&
460
git diff-files --quiet