Skip to content

Commit

Permalink
reset $pathspec: exit with code 0 if successful
Browse files Browse the repository at this point in the history
"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 <martinvonz@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Martin von Zweigbergk authored and Junio C Hamano committed Jan 15, 2013
1 parent 10746a3 commit d94c5e2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
8 changes: 3 additions & 5 deletions builtin/reset.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,17 @@ static void print_new_head_line(struct commit *commit)

static int update_index_refresh(int fd, struct lock_file *index_lock, int flags)
{
int result;

if (!index_lock) {
index_lock = xcalloc(1, sizeof(struct lock_file));
fd = hold_locked_index(index_lock, 1);
}

result = refresh_index(&the_index, (flags), NULL, NULL,
_("Unstaged changes after reset:")) ? 1 : 0;
refresh_index(&the_index, (flags), NULL, NULL,
_("Unstaged changes after reset:"));
if (write_cache(fd, active_cache, active_nr) ||
commit_locked_index(index_lock))
return error ("Could not refresh index");
return result;
return 0;
}

static void update_index_from_diff(struct diff_queue_struct *q,
Expand Down
2 changes: 1 addition & 1 deletion t/t2013-checkout-submodule.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ test_expect_success '"reset <submodule>" updates the index' '
git update-index --refresh &&
git diff-files --quiet &&
git diff-index --quiet --cached HEAD &&
test_must_fail git reset HEAD^ submodule &&
git reset HEAD^ submodule &&
test_must_fail git diff-files --quiet &&
git reset submodule &&
git diff-files --quiet
Expand Down
18 changes: 12 additions & 6 deletions t/t7102-reset.sh
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,8 @@ test_expect_success 'test --mixed <paths>' '
echo 4 > file4 &&
echo 5 > file1 &&
git add file1 file3 file4 &&
test_must_fail git reset HEAD -- file1 file2 file3 &&
git reset HEAD -- file1 file2 file3 &&
test_must_fail git diff --quiet &&
git diff > output &&
test_cmp output expect &&
git diff --cached > output &&
Expand All @@ -402,7 +403,8 @@ test_expect_success 'test resetting the index at give paths' '
>sub/file2 &&
git update-index --add sub/file1 sub/file2 &&
T=$(git write-tree) &&
test_must_fail git reset HEAD sub/file2 &&
git reset HEAD sub/file2 &&
test_must_fail git diff --quiet &&
U=$(git write-tree) &&
echo "$T" &&
echo "$U" &&
Expand Down Expand Up @@ -440,7 +442,8 @@ test_expect_success 'resetting specific path that is unmerged' '
echo "100644 $F3 3 file2"
} | git update-index --index-info &&
git ls-files -u &&
test_must_fail git reset HEAD file2 &&
git reset HEAD file2 &&
test_must_fail git diff --quiet &&
git diff-index --exit-code --cached HEAD
'

Expand All @@ -449,7 +452,8 @@ test_expect_success 'disambiguation (1)' '
git reset --hard &&
>secondfile &&
git add secondfile &&
test_must_fail git reset secondfile &&
git reset secondfile &&
test_must_fail git diff --quiet -- secondfile &&
test -z "$(git diff --cached --name-only)" &&
test -f secondfile &&
test ! -s secondfile
Expand All @@ -474,7 +478,8 @@ test_expect_success 'disambiguation (3)' '
>secondfile &&
git add secondfile &&
rm -f secondfile &&
test_must_fail git reset HEAD secondfile &&
git reset HEAD secondfile &&
test_must_fail git diff --quiet &&
test -z "$(git diff --cached --name-only)" &&
test ! -f secondfile
Expand All @@ -486,7 +491,8 @@ test_expect_success 'disambiguation (4)' '
>secondfile &&
git add secondfile &&
rm -f secondfile &&
test_must_fail git reset -- secondfile &&
git reset -- secondfile &&
test_must_fail git diff --quiet &&
test -z "$(git diff --cached --name-only)" &&
test ! -f secondfile
'
Expand Down

0 comments on commit d94c5e2

Please sign in to comment.