threads / patch / 45235

patcht6300: avoid creating refs/heads/HEAD

Subject: [PATCH] t6300: avoid creating refs/heads/HEAD

## tl;dr

8 messages between Feb 27, 2017 and Feb 27, 2017. Diffs are folded; open one to read it.

replies: 7people: 2as markdown or json

Jeff King· Feb 27, 2017, 09:29 UTC · lore

In one test, we use "git checkout --orphan HEAD" to create an unborn branch. Confusingly, the resulting branch is named "refs/heads/HEAD". The original probably meant something like:

  git checkout --orphan orphaned-branch HEAD

Let's just use "orphaned-branch" here to make this less confusing. Putting HEAD in the second argument is already implied.

Signed-off-by: Jeff King <peff@peff.net>
---
This comes originally from Junio's 84679d470. I cannot see how naming
the new branch HEAD would make any difference to the test, but perhaps I
am missing something.

I noticed this while digging on a nearby issue around "git branch -m @". This does happen to be the only test that checks that we can make a branch called refs/heads/HEAD, and I found it because it triggers if you try to disallow "git branch -m HEAD". :)

If we care about that, though, I think we should make an explicit test for "git branch HEAD". But I'm not sure we _do_ care about that. Making a branch called HEAD is moderately insane, and I don't think it would be unreasonable for us to outlaw it at some point.

 t/t6300-for-each-ref.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Show changes to t/t6300-for-each-ref.sh +1 −1
diff --git a/t/t6300-for-each-ref.sh b/t/t6300-for-each-ref.sh
index aea1dfc71..a468041c5 100755
--- a/t/t6300-for-each-ref.sh
+++ b/t/t6300-for-each-ref.sh
@@ -558,7 +558,7 @@ test_expect_success 'do not dereference NULL upon %(HEAD) on unborn branch' '
 	test_when_finished "git checkout master" &&
 	git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
 	sed -e "s/^\* /  /" actual >expect &&
-	git checkout --orphan HEAD &&
+	git checkout --orphan orphaned-branch &&
 	git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
 	test_cmp expect actual
 '
-- 
2.12.0.624.gbb1b07a2c
Junio C Hamano· Feb 27, 2017, 19:33 UTC · re: Jeff King · lore

Re: [PATCH] t6300: avoid creating refs/heads/HEAD

Jeff King <peff@peff.net> writes:
> This comes originally from Junio's 84679d470. I cannot see how naming
> the new branch HEAD would make any difference to the test, but perhaps I
> am missing something.

Nah, I think it was just a random string that came to mind and the topic being "ah we blindly dereference something when showing %(HEAD)" it was plausible I thought of "H E A D" as that random string before I used my usual other random strings like frotz ;-)

> I noticed this while digging on a nearby issue around "git branch -m @".
> This does happen to be the only test that checks that we can make a
> branch called refs/heads/HEAD, and I found it because it triggers if you
> try to disallow "git branch -m HEAD". :)

About that "nearby" one, does it even make sense to do the interpret thing on the <new> name? I can understand "please rename the branch I was previously on to this new name" wanting to say @{-1} when the user does not recall the exact spelling of a long name, but I do not quite see how "to this new name" part benefits by the "interpret branch name" magic in the first place.

> If we care about that, though, I think we should make an explicit test
> for "git branch HEAD". But I'm not sure we _do_ care about that. Making
> a branch called HEAD is moderately insane, and I don't think it would be
> unreasonable for us to outlaw it at some point.
Yeah, at that point we would have "test_must_fail git branch HEAD".
Show 16 quoted lines
>  t/t6300-for-each-ref.sh | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/t/t6300-for-each-ref.sh b/t/t6300-for-each-ref.sh
> index aea1dfc71..a468041c5 100755
> --- a/t/t6300-for-each-ref.sh
> +++ b/t/t6300-for-each-ref.sh
> @@ -558,7 +558,7 @@ test_expect_success 'do not dereference NULL upon %(HEAD) on unborn branch' '
>  	test_when_finished "git checkout master" &&
>  	git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
>  	sed -e "s/^\* /  /" actual >expect &&
> -	git checkout --orphan HEAD &&
> +	git checkout --orphan orphaned-branch &&
>  	git for-each-ref --format="%(HEAD) %(refname:short)" refs/heads/ >actual &&
>  	test_cmp expect actual
>  '
Jeff King· Feb 27, 2017, 20:51 UTC · re: Junio C Hamano · lore

Re: [PATCH] t6300: avoid creating refs/heads/HEAD

On Mon, Feb 27, 2017 at 11:33:23AM -0800, Junio C Hamano wrote:
Show 10 quoted lines
> Jeff King <peff@peff.net> writes:
> 
> > This comes originally from Junio's 84679d470. I cannot see how naming
> > the new branch HEAD would make any difference to the test, but perhaps I
> > am missing something.
> 
> Nah, I think it was just a random string that came to mind and the
> topic being "ah we blindly dereference something when showing %(HEAD)"
> it was plausible I thought of "H E A D" as that random string before
> I used my usual other random strings like frotz ;-)
OK, thanks for confirming.
Show 11 quoted lines
> > I noticed this while digging on a nearby issue around "git branch -m @".
> > This does happen to be the only test that checks that we can make a
> > branch called refs/heads/HEAD, and I found it because it triggers if you
> > try to disallow "git branch -m HEAD". :)
> 
> About that "nearby" one, does it even make sense to do the interpret
> thing on the <new> name?  I can understand "please rename the branch
> I was previously on to this new name" wanting to say @{-1} when the
> user does not recall the exact spelling of a long name, but I do not
> quite see how "to this new name" part benefits by the "interpret
> branch name" magic in the first place.

Yeah, it's arguable whether the "new" side of a rename should do any interpretation at all. At the same time, the bug is in the underlying function that assumes you can slap "refs/heads/" in front of the results of interpret_branch_name(). And that function gets used in a lot of places, including the "old" side of a rename. So:

  git branch @{-1} foo
should clearly work. Doing:
  git branch @{upstream} foo

is more debatable. It _does_ work, but only if your upstream is actually a local branch (otherwise it tries to rename refs/heads/origin/master or some such nonsense. It happens to fail most of the time because you probably don't have such a branch, but it's still wrong to even look at that).

I suspect there are a lot of other places that are less clear cut. E.g., I think just:

  git branch foo bar
will put "foo" through the same interpretation. So you could do:
  git branch -f @{-1} bar
Is that insane? Maybe. But it does work now.
-Peff
Junio C Hamano· Feb 27, 2017, 21:19 UTC · re: Jeff King · lore

Re: [PATCH] t6300: avoid creating refs/heads/HEAD

Jeff King <peff@peff.net> writes:
Show 10 quoted lines
> I suspect there are a lot of other places that are less clear cut. E.g.,
> I think just:
>
>   git branch foo bar
>
> will put "foo" through the same interpretation. So you could do:
>
>   git branch -f @{-1} bar
>
> Is that insane? Maybe. But it does work now.
No, it _is_ very sensible, so is "git checkout -B @{-1} <someplace>"

Perhaps interpret-branch-name that does not error out when given "@" is what is broken? I suspect that calling interpret_empty_at() from that function is fundamentally flawed. The "@" end user types never means refs/heads/HEAD, and HEAD@{either reflog or -1} would not mean anything that should be taken as a branch_name, either.

So perhaps what interpret_empty_at() does is necessary for the "four capital letters is too many to type, so just type one key while holding a shift", but it should be called from somewhere else, and not from interpret_branch_name()?

Jeff King· Feb 27, 2017, 21:41 UTC · re: Junio C Hamano · lore

Re: [PATCH] t6300: avoid creating refs/heads/HEAD

On Mon, Feb 27, 2017 at 01:19:29PM -0800, Junio C Hamano wrote:
Show 25 quoted lines
> Jeff King <peff@peff.net> writes:
> 
> > I suspect there are a lot of other places that are less clear cut. E.g.,
> > I think just:
> >
> >   git branch foo bar
> >
> > will put "foo" through the same interpretation. So you could do:
> >
> >   git branch -f @{-1} bar
> >
> > Is that insane? Maybe. But it does work now.
> 
> No, it _is_ very sensible, so is "git checkout -B @{-1} <someplace>"
> 
> Perhaps interpret-branch-name that does not error out when given "@"
> is what is broken?  I suspect that calling interpret_empty_at() from
> that function is fundamentally flawed.  The "@" end user types never
> means refs/heads/HEAD, and HEAD@{either reflog or -1} would not mean
> anything that should be taken as a branch_name, either.
> 
> So perhaps what interpret_empty_at() does is necessary for the "four
> capital letters is too many to type, so just type one key while
> holding a shift", but it should be called from somewhere else, and
> not from interpret_branch_name()?

I think _most_ of interpret_branch_name() is in the same boat. The "@{upstream}" mark is not likely to give you a branch in refs/heads either.

So in practice, I think strbuf_check_branch_ref() could probably get by with just calling interpret_nth_prior_checkout(). Or if you prefer, to rip everything out of interpret_branch_name() except that. :) But that other stuff has to go somewhere, and there are some challenges with the recursion from reinterpret().

The "other" stuff could sometimes be useful, I guess. It's not _always_ wrong to do:

  git branch -f @{upstream} foo

It depends on what your @{upstream} resolves to. Switching to just using interpret_nth_prior_checkout() would break the case when it resolves to a local branch. I'm not sure if we're OK with that or not. If we want to keep all the existing cases working, I think we need something like the "not_in_refs_heads" patch I posted elsewhere.

-Peff
Junio C Hamano· Feb 27, 2017, 22:18 UTC · re: Jeff King · lore

Re: [PATCH] t6300: avoid creating refs/heads/HEAD

Jeff King <peff@peff.net> writes:
Show 10 quoted lines
> The "other" stuff could sometimes be useful, I guess. It's not _always_
> wrong to do:
>
>   git branch -f @{upstream} foo
>
> It depends on what your @{upstream} resolves to. Switching to just using
> interpret_nth_prior_checkout() would break the case when it resolves to
> a local branch. I'm not sure if we're OK with that or not. If we want to
> keep all the existing cases working, I think we need something like the
> "not_in_refs_heads" patch I posted elsewhere.

I haven't seen that patch, but yes, telling the caller if the returned value is meant to be used inside refs/heads/ is the right approach and makes it possible for "@{upstream}" (and just "@") to be handled sensibly in "git branch -m @{that at-mark thing}".

Junio C Hamano· Feb 27, 2017, 21:44 UTC · re: Junio C Hamano · lore

Re: [PATCH] t6300: avoid creating refs/heads/HEAD

Junio C Hamano <gitster@pobox.com> writes:
> ...  I suspect that calling interpret_empty_at() from
> that function is fundamentally flawed.  The "@" end user types never
> means refs/heads/HEAD, and HEAD@{either reflog or -1} would not mean
> anything that should be taken as a branch_name, either.  
The latter should read "HEAD@{either reflog or -1 or 'upstream'}"

Or do we make HEAD@{upstream} to mean "deref HEAD to learn the current branch name and then take its upstream"? If so @@{upstream} might logically make sense, but I do not see why @{upstream} without HEAD or @ is not sufficient to begin with, so...

Jeff King· Feb 27, 2017, 21:46 UTC · re: Junio C Hamano · lore

Re: [PATCH] t6300: avoid creating refs/heads/HEAD

On Mon, Feb 27, 2017 at 01:44:26PM -0800, Junio C Hamano wrote:
Show 13 quoted lines
> Junio C Hamano <gitster@pobox.com> writes:
> 
> > ...  I suspect that calling interpret_empty_at() from
> > that function is fundamentally flawed.  The "@" end user types never
> > means refs/heads/HEAD, and HEAD@{either reflog or -1} would not mean
> > anything that should be taken as a branch_name, either.  
> 
> The latter should read "HEAD@{either reflog or -1 or 'upstream'}"
> 
> Or do we make HEAD@{upstream} to mean "deref HEAD to learn the
> current branch name and then take its upstream"?  If so @@{upstream}
> might logically make sense, but I do not see why @{upstream} without
> HEAD or @ is not sufficient to begin with, so...

Yes, HEAD@{upstream} and @@{upstream} are both resolved to the actual branch name. I also was puzzled whether there was any real use over just @{upstream}. But it does work, and if you had a script which looked for, say, $branch@{upstream}, you'd probably want branch=HEAD to keep working.

The "branch=@" case I am less sympathetic to, as it was mainly supposed to be a command-line convenience. But it _does_ work now.

-Peff

← back to recent threads