checkout: do not corrupt HEAD on empty repo
PATCH 1/1 checkout: do not corrupt HEAD on empty repo
In abe1998 ("git checkout -b: allow switching out of an unborn branch"), a code-path overly-optimisticly assumed that a branch-name was specified. This is not always the case, and as a result a NULL-pointer was attempted printed to .git/HEAD.
This could lead to at least two different failure modes:
1) The CRT formated the NULL-string as something useful (e.g
"(null)")
2) The CRT crasheed.
Neither were very convenient for formatting a new HEAD-reference.
To fix this, reintroduce some strictness so we only take this new codepath if a banch-name was specified.
--- builtin/checkout.c | 2 +- t/t2015-checkout-unborn.sh | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-)
builtin/checkout.c +1 -1
@@ -1091,7 +1091,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix)if (opts.writeout_stage)die (_ ("--ours/--theirs is incompatible with switching branches." )); -if (!new.commit) { +if (!new.commit && opts.new_branch) {unsigned char rev[20 ];int flag;
t/t2015-checkout-unborn.sh +11 -0
@@ -46,4 +46,15 @@ test_expect_success 'checking out another branch from unborn state' ' test_cmp expect actual ' +test_expect_success 'checking out in a newly created repo' ' + test_create_repo empty && + ( + cd empty && + cat .git/HEAD >expect && + test_must_fail git checkout && + cat .git/HEAD >actual && + test_cmp expect actual + ) +' + test_done
Signature · 1 line
1.7.10.msysgit.1
Quoted 9 lines · Erik Faye-Lund
In abe1998 ("git checkout -b: allow switching out of an unborn branch"), a code-path overly-optimisticly assumed that a branch-name was specified. This is not always the case, and as a result a NULL-pointer was attempted printed to .git/HEAD.
This could lead to at least two different failure modes:
1) The CRT formated the NULL-string as something useful (e.g"(null)")2) The CRT crasheed.
Just a quick question. What does a cathode ray tube have to do with this (or "are people expected to know what CRT you are talking about?")?
Otherwise the explanation, the patch and the added test makes sense.
Thanks.
Quoted 7 lines · Junio C Hamano
Quoted 5 lines · Erik Faye-Lund
In abe1998 ("git checkout -b: allow switching out of an unborn branch"), a code-path overly-optimisticly assumed that a branch-name was specified. This is not always the case, and as a result a NULL-pointer was attempted printed to .git/HEAD.
Quoted 8 lines
Quoted 4 lines
This could lead to at least two different failure modes:
1) The CRT formated the NULL-string as something useful (e.g "(null)")
2) The CRT crasheed.Just a quick question. What does a cathode ray tube have to do with this (or "are people expected to know what CRT you are talking about?")?
Feel free to change it to "the C runtime", or even "the vsnprintf implementation" if you feel like it :)
Quoted 4 lines · Erik Faye-Lund
Quoted 1 line
(or "are people expected to know what CRT you are talking about?")?
Feel free to change it to "the C runtime", or even "the vsnprintf implementation" if you feel like it :)
Ahh, "C RunTime" was what you meant. OK, it somehow immediately did not "click".
Quoted 9 lines · Junio C Hamano
Quoted 4 lines · Erik Faye-Lund
Quoted 1 line
(or "are people expected to know what CRT you are talking about?")?
Feel free to change it to "the C runtime", or even "the vsnprintf implementation" if you feel like it :)
Ahh, "C RunTime" was what you meant. OK, it somehow immediately did not "click".
It's the name we Windows users tend to use for what you unix-guys tend to call "libc" ;)
Probably because Microsoft Visual C names their MSVCRT.dll.
Quoted 2 lines · Erik Faye-Lund
It's the name we Windows users tend to use for what you unix-guys tend to call "libc" ;)
I think it originated in Unix, viz /usr/lib/crt0.o.
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 53 lines · Erik Faye-Lund
In abe1998 ("git checkout -b: allow switching out of an unborn branch"), a code-path overly-optimisticly assumed that a branch-name was specified. This is not always the case, and as a result a NULL-pointer was attempted printed to .git/HEAD.
This could lead to at least two different failure modes:
1) The CRT formated the NULL-string as something useful (e.g "(null)")
2) The CRT crasheed.Neither were very convenient for formatting a new HEAD-reference.
To fix this, reintroduce some strictness so we only take this new codepath if a banch-name was specified.
Signed-off-by Erik Faye-Lund <[email protected]>---
builtin/checkout.c | 2 +-
t/t2015-checkout-unborn.sh | 11 +++++++++++2 files changed, 12 insertions(+), 1 deletion(-)builtin/checkout.c +1
-1@@ -1091,7 +1091,7 @@ int cmd_checkout(int argc, const char **argv, const char *prefix) if (opts.writeout_stage) die(_("--ours/--theirs is incompatible with switching branches.")); - if (!new.commit) { + if (!new.commit && opts.new_branch) { unsigned char rev[20]; int flag;t/t2015-checkout-unborn.sh +0
-0@@ -46,4 +46,15 @@ test_expect_success 'checking out another branch from unborn state' 'test_cmp expect actual
't/t2015-checkout-unborn.sh +11
-0+test_expect_success 'checking out in a newly created repo' ' + test_create_repo empty && + ( + cd empty && + cat .git/HEAD >expect && + test_must_fail git checkout && + cat .git/HEAD >actual && + test_cmp expect actual + ) +' +
Quoted 1 line
test_done
Hmh, this is needlessly cluttery. The following should cover it:
---
t/t2015-checkout-unborn.sh +1 -4
@@ -50,10 +50,7 @@ test_expect_success 'checking out in a newly created repo' ' test_create_repo empty && ( cd empty && - cat .git/HEAD >expect && - test_must_fail git checkout && - cat .git/HEAD >actual && - test_cmp expect actual + test_must_fail git checkout ) '
Quoted 8 lines · Erik Faye-Lund
Hmh, this is needlessly cluttery. The following should cover it:
---
t/t2015-checkout-unborn.sh +0
-0
t/t2015-checkout-unborn.sh test_expect_success 'checking out in a newly created repo' ' · line 55
Quoted 10 lines
t/t2015-checkout-unborn.sh +1
-4@@ -50,10 +50,7 @@ test_expect_success 'checking out in a newly created repo' ' test_create_repo empty && ( cd empty && - cat .git/HEAD >expect && - test_must_fail git checkout && - cat .git/HEAD >actual && - test_cmp expect actual + test_must_fail git checkout ) '
Hrm, I am of two minds. Yes, we may want checkout to fail, but at the same time, we would want to make sure that a failed checkout does not corrupt the HEAD. Perhaps it would make it more palatable if you replaced "cat .git/HEAD" with "git symbolic-ref HEAD" in the original?
Quoted 16 lines · Junio C Hamano
Quoted 14 lines · Erik Faye-Lund
Hmh, this is needlessly cluttery. The following should cover it:
---
t/t2015-checkout-unborn.sh +0
-0@@ -50,10 +50,7 @@ test_expect_success 'checking out in a newly created repo' '
test_create_repoempty &&
(
cd empty &&
- cat .git/HEAD >expect &&
-test_must_failgit checkout &&
- cat .git/HEAD >actual &&
Quoted 8 lines
Quoted 4 lines
-
test_cmpexpect actual
+test_must_failgit checkout
)
'Hrm, I am of two minds. Yes, we may want checkout to fail, but at the same time, we would want to make sure that a failed checkout does not corrupt the HEAD.
Good point.
Quoted 2 lines
Perhaps it would make it more palatable if you replaced "cat .git/HEAD" with "git symbolic-ref HEAD" in the original?
Ah, yes. That's much better. Do you want me to resend (improving the test and replacing "CRT" with "vsnprintf")? I also spotted a typo in the commit message ("crasheed" vs "crashed")...
Quoted 6 lines · Erik Faye-Lund
On Tue, May 8, 2012 at 6:43 PM, Junio C Hamano <[email protected]> wrote:
...Quoted 3 lines
Hrm, I am of two minds. Yes, we may want checkout to fail, but at the same time, we would want to make sure that a failed checkout does not corrupt the HEAD.
Quoted 8 lines
Good point.
Quoted 2 lines
Perhaps it would make it more palatable if you replaced "cat .git/HEAD" with "git symbolic-ref HEAD" in the original?
Ah, yes. That's much better. Do you want me to resend (improving the test and replacing "CRT" with "vsnprintf")? I also spotted a typo in the commit message ("crasheed" vs "crashed")...
Surely.
By the way, notice I said "we *may* want checkout to fail"? With the discussion "why is it wrong to allow 'git checkout' to be no-op in a freshly created repository" stemming from your other "I do not claim it is correct but it makes the test pass" message, we may actually want to make that 'checkout' in the test pass, and then 'test_must_fail git checkout' in the test would have to go when that happens.