threads / discuss / 32304

exit code from git reset

Subject: exit code from git reset

## tl;dr

3 messages between Dec 9, 2012 and Dec 9, 2012.

replies: 2people: 2as markdown or json

Martin von Zweigbergk· Dec 9, 2012, 22:23 UTC · lore
Hi,

"git reset" currently returns 0 (if successful) while "git reset $pathspec" returns 0 iff the index matches HEAD after resetting (on all paths, not just those matching $pathspec). The exit code doesn't seem to be documented. Is there a reason they should behave differently? If not, what would be the better behavior? I don't see when the "git reset $pathspec" behavior" would be useful and it seems safer for any existing scripts out there not to change the status code for "git reset" (w/o paths). Scripts that run "git reset $pathspec" probably already know to ignore (or use?) the exit code. Also, having any non-zero exit code signal failure might actually be useful.

Changing "git reset $pathspec" to return 0 on success, regardless of diff between HEAD and index, breaks 10 test cases (in t2013-checkout-submodule.sh and t7102-reset.sh). These seem to do "test_must_fail git reset $pathspec", but I have not been able to find any motivation for expecting the failure.

It seems like this behavior has been there at least since the tests were added in 359048d (Add tests for documented features of "git reset"., 2007-09-11), just before reset became built-in.

Would a patch to change the exit code from "git reset $pathspec" be appreciated?
Martin
Junio C Hamano· Dec 9, 2012, 23:04 UTC · re: Martin von Zweigbergk · lore

Re: exit code from git reset

Martin von Zweigbergk <martinvonz@gmail.com> writes:
> "git reset" currently returns 0 (if successful) while "git reset
> $pathspec" returns 0 iff the index matches HEAD after resetting (on
> all paths, not just those matching $pathspec).

So in short, you observed that either of them reports with its exit code if the resulting index (not just any subpart, but always the entire thing) matches the HEAD, e.g. "do we have change that will be listed on 'will be committed' section in git status output?"

Sounds like one sane and consistent semantics to me. I am not saying that there cannot be other behaviours that are internally consistent (e.g. the error code could have matched the number of paths that are different between the index and the HED, or the error code could have been zero for successful reset, non-zero for some failure), but I am saying that the current behaviour gives _one_ sane and consistent meanings regardless of how you ran the command.

> The exit code doesn't seem to be documented.
Please make it so.
Show 5 quoted lines
> Changing "git reset $pathspec" to return 0 on success, regardless of
> diff between HEAD and index, breaks 10 test cases (in
> t2013-checkout-submodule.sh and t7102-reset.sh). These seem to do
> "test_must_fail git reset $pathspec", but I have not been able to find
> any motivation for expecting the failure.
See above.
Martin von Zweigbergk· Dec 9, 2012, 23:12 UTC · re: Junio C Hamano · lore

Re: exit code from git reset

On Sun, Dec 9, 2012 at 3:04 PM, Junio C Hamano <gitster@pobox.com> wrote:
Show 12 quoted lines
> Martin von Zweigbergk <martinvonz@gmail.com> writes:
>
>> "git reset" currently returns 0 (if successful) while "git reset
>> $pathspec" returns 0 iff the index matches HEAD after resetting (on
>> all paths, not just those matching $pathspec).
>
> So in short, you observed that either of them reports with its exit
> code if the resulting index (not just any subpart, but always the
> entire thing) matches the HEAD, e.g. "do we have change that will be
> listed on 'will be committed' section in git status output?"
>
> Sounds like one sane and consistent semantics to me.

Heh, true, the behavior according to my description does make sense, it's just that my description was wrong; sorry :-(.

What "git reset $pathspec" returns is not "whether HEAD differs from index" (as I wrote), but "whether worktree differs from index". So "git reset" and "git reset ." will return different exit codes if there are changes in the worktree as compared to HEAD before the invocation. I hope that's clearer.

← back to recent threads