Skip to content

Commit d94c5e2

Browse files
martinvonzgitster
authored andcommitted
reset $pathspec: exit with code 0 if successful
"git reset $pathspec" currently exits with a non-zero exit code if the worktree is dirty after resetting, which is inconsistent with reset without pathspec, and it makes it harder to know whether the command really failed. Change it to exit with code 0 regardless of whether the worktree is dirty so that non-zero indicates an error. This makes the 4 "disambiguation" test cases in t7102 clearer since they all used to "fail", 3 of which "failed" due to changes in the work tree. Now only the ambiguous one fails. Signed-off-by: Martin von Zweigbergk <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 10746a3 commit d94c5e2

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

builtin/reset.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,19 +119,17 @@ static void print_new_head_line(struct commit *commit)
119119

120120
static int update_index_refresh(int fd, struct lock_file *index_lock, int flags)
121121
{
122-
int result;
123-
124122
if (!index_lock) {
125123
index_lock = xcalloc(1, sizeof(struct lock_file));
126124
fd = hold_locked_index(index_lock, 1);
127125
}
128126

129-
result = refresh_index(&the_index, (flags), NULL, NULL,
130-
_("Unstaged changes after reset:")) ? 1 : 0;
127+
refresh_index(&the_index, (flags), NULL, NULL,
128+
_("Unstaged changes after reset:"));
131129
if (write_cache(fd, active_cache, active_nr) ||
132130
commit_locked_index(index_lock))
133131
return error ("Could not refresh index");
134-
return result;
132+
return 0;
135133
}
136134

137135
static void update_index_from_diff(struct diff_queue_struct *q,

t/t2013-checkout-submodule.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ test_expect_success '"reset <submodule>" updates the index' '
2323
git update-index --refresh &&
2424
git diff-files --quiet &&
2525
git diff-index --quiet --cached HEAD &&
26-
test_must_fail git reset HEAD^ submodule &&
26+
git reset HEAD^ submodule &&
2727
test_must_fail git diff-files --quiet &&
2828
git reset submodule &&
2929
git diff-files --quiet

t/t7102-reset.sh

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,8 @@ test_expect_success 'test --mixed <paths>' '
388388
echo 4 > file4 &&
389389
echo 5 > file1 &&
390390
git add file1 file3 file4 &&
391-
test_must_fail git reset HEAD -- file1 file2 file3 &&
391+
git reset HEAD -- file1 file2 file3 &&
392+
test_must_fail git diff --quiet &&
392393
git diff > output &&
393394
test_cmp output expect &&
394395
git diff --cached > output &&
@@ -402,7 +403,8 @@ test_expect_success 'test resetting the index at give paths' '
402403
>sub/file2 &&
403404
git update-index --add sub/file1 sub/file2 &&
404405
T=$(git write-tree) &&
405-
test_must_fail git reset HEAD sub/file2 &&
406+
git reset HEAD sub/file2 &&
407+
test_must_fail git diff --quiet &&
406408
U=$(git write-tree) &&
407409
echo "$T" &&
408410
echo "$U" &&
@@ -440,7 +442,8 @@ test_expect_success 'resetting specific path that is unmerged' '
440442
echo "100644 $F3 3 file2"
441443
} | git update-index --index-info &&
442444
git ls-files -u &&
443-
test_must_fail git reset HEAD file2 &&
445+
git reset HEAD file2 &&
446+
test_must_fail git diff --quiet &&
444447
git diff-index --exit-code --cached HEAD
445448
'
446449

@@ -449,7 +452,8 @@ test_expect_success 'disambiguation (1)' '
449452
git reset --hard &&
450453
>secondfile &&
451454
git add secondfile &&
452-
test_must_fail git reset secondfile &&
455+
git reset secondfile &&
456+
test_must_fail git diff --quiet -- secondfile &&
453457
test -z "$(git diff --cached --name-only)" &&
454458
test -f secondfile &&
455459
test ! -s secondfile
@@ -474,7 +478,8 @@ test_expect_success 'disambiguation (3)' '
474478
>secondfile &&
475479
git add secondfile &&
476480
rm -f secondfile &&
477-
test_must_fail git reset HEAD secondfile &&
481+
git reset HEAD secondfile &&
482+
test_must_fail git diff --quiet &&
478483
test -z "$(git diff --cached --name-only)" &&
479484
test ! -f secondfile
480485
@@ -486,7 +491,8 @@ test_expect_success 'disambiguation (4)' '
486491
>secondfile &&
487492
git add secondfile &&
488493
rm -f secondfile &&
489-
test_must_fail git reset -- secondfile &&
494+
git reset -- secondfile &&
495+
test_must_fail git diff --quiet &&
490496
test -z "$(git diff --cached --name-only)" &&
491497
test ! -f secondfile
492498
'

0 commit comments

Comments
 (0)