difftool: honor --trust-exit-code for builtin tools
PATCH 1/1 difftool: honor --trust-exit-code for builtin tools
run_merge_tool() was not setting $status, which prevented the exit code for builtin tools from being forwarded to the caller.
Capture the exit status and add a test to guarantee the behavior.
--- git-mergetool--lib.sh | 1 + t/t7800-difftool.sh | 5 +++++ 2 files changed, 6 insertions(+)
git-mergetool--lib.sh +1 -0
@@ -221,6 +221,7 @@ run_merge_tool () { else run_diff_cmd "$1" fi + status=$? return $status }
t/t7800-difftool.sh +5 -0
@@ -86,6 +86,11 @@ test_expect_success PERL 'difftool forwards exit code with --trust-exit-code' ' test_must_fail git difftool -y --trust-exit-code -t error branch ' +test_expect_success PERL 'difftool forwards exit code with --trust-exit-code for built-ins' ' + test_config difftool.vimdiff.path false && + test_must_fail git difftool -y --trust-exit-code -t vimdiff branch +' + test_expect_success PERL 'difftool honors difftool.trustExitCode = true' ' test_config difftool.error.cmd false && test_config difftool.trustExitCode true &&
Signature · 1 line
2.2.0.rc1.23.gf570943.dirty
Quoted 17 lines · David Aguilar
run_merge_tool()was not setting $status, which prevented the exit code for builtin tools from being forwarded to the caller.Capture the exit status and add a test to guarantee the behavior.
Reported-by Adria Farres <[email protected]>Signed-off-by David Aguilar <[email protected]>--- git-mergetool--lib.sh | 1 + t/t7800-difftool.sh | 5 +++++ 2 files changed, 6 insertions(+)git-mergetool--lib.sh +0
-0
git-mergetool--lib.sh run_merge_tool () { · line 225
Quoted 5 lines
git-mergetool--lib.sh +1
-0@@ -221,6 +221,7 @@ run_merge_tool () { else run_diff_cmd "$1" fi + status=$? return $status
If you want to return the last exit status at the end of a function you don't need any return at all.
Andreas.
Signature · 3 lines
Andreas Schwab, [email protected] GPG Key fingerprint = 58CA 54C7 6D53 942B 1756 01D3 44D5 214B 8276 4ED5 "And now for something completely different."
Quoted 17 lines · David Aguilar
run_merge_tool()was not setting $status, which prevented the exit code for builtin tools from being forwarded to the caller.Capture the exit status and add a test to guarantee the behavior.
Reported-by Adria Farres <[email protected]>Signed-off-by David Aguilar <[email protected]>--- git-mergetool--lib.sh | 1 + t/t7800-difftool.sh | 5 +++++ 2 files changed, 6 insertions(+)git-mergetool--lib.sh +0
-0
git-mergetool--lib.sh run_merge_tool () { · line 226
Quoted 6 lines
git-mergetool--lib.sh +1
-0@@ -221,6 +221,7 @@ run_merge_tool () { else run_diff_cmd "$1" fi + status=$? return $status }
Thanks for a quick turn-around. As a hot-fix for what is already in -rc I am fine with this fix but the patch makes me wonder if $status as a global shell variable has any significance.
I see that this shell function in its early part does this:
status=0
setup_tool "$1" || return 1which means that the caller of this function, instead of checking what is returned as the return value of the function like:
if run_merge_tool ...
then
...relies on the value of $status in its later part of the code like:
run_merge_tool ... ... if test "$status" = 0 then ...
then we are already in trouble. And the latter form, if we had such a flow in the code, is simply a bad taste.
A cleaner fix might be to get rid of the extra $status variable from this function and let the function return the result of its last command, either run_merge_cmd or run_diff_cmd, by either explicitly having "return $?" at the end, or not having that "return $status" line. But that relies on us not having any caller that relies on the $status carried as a global variable around, so it will be more work to convince ourselves that such a fix is correctly done. From my cursory look, what I suggested above should be safe and correct, but I do not want to risk an unnecessary and silly breakage this late in the cycle.
So I'll queue this patch as-is for upcoming 2.2, but I think we would want to revisit this issue after the release is done.
Quoted 64 lines · Junio C Hamano
Quoted 17 lines · David Aguilar
run_merge_tool()was not setting $status, which prevented the exit code for builtin tools from being forwarded to the caller.Capture the exit status and add a test to guarantee the behavior.
Reported-by Adria Farres <[email protected]>Signed-off-by David Aguilar <[email protected]>--- git-mergetool--lib.sh | 1 + t/t7800-difftool.sh | 5 +++++ 2 files changed, 6 insertions(+)git-mergetool--lib.sh +0
-0git-mergetool--lib.sh run_merge_tool () { · line 226
Quoted 6 lines
git-mergetool--lib.sh +1
-0@@ -221,6 +221,7 @@ run_merge_tool () { else run_diff_cmd "$1" fi + status=$? return $status }Thanks for a quick turn-around. As a hot-fix for what is already in -rc I am fine with this fix but the patch makes me wonder if $status as a global shell variable has any significance.
I see that this shell function in its early part does this:
status=0 setup_tool "$1" || return 1which means that the caller of this function, instead of checking what is returned as the return value of the function like:
if run_merge_tool ... then ...relies on the value of $status in its later part of the code like:
run_merge_tool ... ... if test "$status" = 0 then ...then we are already in trouble. And the latter form, if we had such a flow in the code, is simply a bad taste.
A cleaner fix might be to get rid of the extra $status variable from this function and let the function return the result of its last command, either
run_merge_cmdorrun_diff_cmd, by either explicitly having "return $?" at the end, or not having that "return $status" line. But that relies on us not having any caller that relies on the $status carried as a global variable around, so it will be more work to convince ourselves that such a fix is correctly done. From my cursory look, what I suggested above should be safe and correct, but I do not want to risk an unnecessary and silly breakage this late in the cycle.
Quoted 2 lines
So I'll queue this patch as-is for upcoming 2.2, but I think we would want to revisit this issue after the release is done.
Thanks for the sug, I totally agree with that. I'll put a $status audit/rework for mergetool+difftool on my todo list.
cheers,
Signature · 1 line
David
Quoted 14 lines · Junio C Hamano
Quoted 12 lines · David Aguilar
run_merge_tool()was not setting $status, which prevented the exit code for builtin tools from being forwarded to the caller.Capture the exit status and add a test to guarantee the behavior.
Reported-by Adria Farres <[email protected]>Signed-off-by David Aguilar <[email protected]>--- git-mergetool--lib.sh | 1 + t/t7800-difftool.sh | 5 +++++ 2 files changed, 6 insertions(+)
Quoted 15 lines
git-mergetool--lib.sh run_merge_tool () { · line 226
Quoted 11 lines
git-mergetool--lib.sh +1
-0@@ -221,6 +221,7 @@ run_merge_tool () { else run_diff_cmd "$1" fi + status=$? return $status }Thanks for a quick turn-around. As a hot-fix for what is already in -rc I am fine with this fix but the patch makes me wonder if $status as a global shell variable has any significance.
$status is an alias for $? in zsh, and so cannot be assigned to. But other than that I don't think it holds any meaning and should be fine in a .sh script.
Signature · 1 line
Mikael Magnusson
Quoted 31 lines · Mikael Magnusson
Quoted 14 lines · Junio C Hamano
Quoted 12 lines · David Aguilar
run_merge_tool()was not setting $status, which prevented the exit code for builtin tools from being forwarded to the caller.Capture the exit status and add a test to guarantee the behavior.
Reported-by Adria Farres <[email protected]>Signed-off-by David Aguilar <[email protected]>--- git-mergetool--lib.sh | 1 + t/t7800-difftool.sh | 5 +++++ 2 files changed, 6 insertions(+)Quoted 15 lines
git-mergetool--lib.sh run_merge_tool () { · line 226
Quoted 11 lines
git-mergetool--lib.sh +1
-0@@ -221,6 +221,7 @@ run_merge_tool () { else run_diff_cmd "$1" fi + status=$? return $status }Thanks for a quick turn-around. As a hot-fix for what is already in -rc I am fine with this fix but the patch makes me wonder if $status as a global shell variable has any significance.
Quoted 3 lines
$status is an alias for $? in zsh, and so cannot be assigned to. But other than that I don't think it holds any meaning and should be fine in a .sh script.
Thanks for the heads-up ~ this is even more reason to cleanup the script a bit.
If we still need a local variable for it in a few places then I'll call it $rc instead, but it'll only be used for local things rather than its current global usage.
Signature · 1 line
David
Quoted 16 lines · Mikael Magnusson
Quoted 7 lines
Quoted 7 lines
git-mergetool--lib.sh +0
-0@@ -221,6 +221,7 @@ run_merge_tool () { else run_diff_cmd "$1"Quoted 8 lines
git-mergetool--lib.sh run_merge_tool () { · line 226
Quoted 4 lines
git-mergetool--lib.sh +1
-0fi + status=$? return $status }Thanks for a quick turn-around. As a hot-fix for what is already in -rc I am fine with this fix but the patch makes me wonder if $status as a global shell variable has any significance.
Quoted 3 lines
$status is an alias for $? in zsh, and so cannot be assigned to. But other than that I don't think it holds any meaning and should be fine in a .sh script.
That is not what I meant by "global ... significance".
The question was if the codepath in the caller depends on this setting the global variable here, or nobody looks at and depends on the global variable we are setting here after this function returns.
It does not have any significance that a random shell implementation is not POSIX compliant. That would merely mean that such a shell cannot be used to run POSIX shell scripts like our Porcelain. I would suspect that zsh has more "posixly correct" mode, with which it _can_ run POSIX shell scripts, and I would imagine that this "$status is an alias $?" business is disabled in that mode?
My quick glance across the codepaths in the callers of this funciton indicated that it should be safe not using this global variable, so my answer to my original question was "no there is no significance". I think we can safely remove any mention of status from this shell function, i.e. if we remove initial assignment to 0, remove this new assignment and then remove the "return $status" at the end, the caller would still be happy.
Quoted 3 lines · Junio C Hamano
It does not have any significance that a random shell implementation is not POSIX compliant. That would merely mean that such a shell cannot be used to run POSIX shell scripts like our Porcelain.
Right, and I suspect that it's very rare for zsh to be used as /bin/sh. I've heard of people doing it just to see what would fail, but not of anybody doing that for regular use.
Quoted 2 lines
I would suspect that zsh has more "posixly correct" mode, with which
it _can_ run POSIX shell scripts, and I would imagine that this "$status is an alias $?" business is disabled in that mode?
Yes, if zsh is invoked as either "sh" or "ksh" it attempts to emulate the usual semantics of the named shell. One of the differences is that $status isn't special in the emulation modes.