threads / rfc / 37424

[RFC] add detached HEAD to --all listing

Subject: [RFC] add detached HEAD to --all listing

## tl;dr

8 messages between Aug 27, 2014 and Sep 3, 2014.

replies: 7people: 3as markdown or json

Max Kirillov· Aug 27, 2014, 14:18 UTC · lore
Hello.

Could HEAD be added to list of heads while using --all switch? Detached heads are not something very unusual and incorrect, in submodules for example, or for some scripts. Having to specify it additionally when I meet such checkout feels like some flaw.

What are opinions on that, could it be changed?
-- 
Max
Jeff King· Aug 27, 2014, 14:22 UTC · re: Max Kirillov · lore

Re: [RFC] add detached HEAD to --all listing

On Wed, Aug 27, 2014 at 05:18:21PM +0300, Max Kirillov wrote:
> Could HEAD be added to list of heads while using --all switch?
To which command?

If you mean "git branch", I think the detached HEAD is already mentioned:

  $ git branch
  * (detached from 1290ebd)
    master
If you mean "git log", I think it is included there, too:
  $ git log --decorate --oneline --all
  685450f (HEAD) more
  1290ebd (master) foo
Is there some other command you have in mind? git-push, maybe?
-Peff
Max Kirillov· Aug 27, 2014, 14:33 UTC · re: Jeff King · lore

Re: [RFC] add detached HEAD to --all listing

On Wed, Aug 27, 2014 at 5:22 PM, Jeff King <peff@peff.net> wrote:
Show 5 quoted lines
> If you mean "git log", I think it is included there, too:
>
>   $ git log --decorate --oneline --all
>   685450f (HEAD) more
>   1290ebd (master) foo

I meant "git log", did not know it's there. Where I actually would like to see it in gitk --all, apparently it performs its own filtering.

Thanks.
-- 
Max
Max Kirillov· Aug 30, 2014, 22:24 UTC · re: Max Kirillov · lore

[PATCH] rev-parse: include HEAD in --all output

for_each_ref() does not include it itself, and without the hash the detached HEAD may be missed by some frontends (like gitk).

Add test which verifies the head is returned

Update test t6018-rev-list-glob.sh which relied on exact list of returned hashes.

Signed-off-by: Max Kirillov <max@max630.net>
---
 builtin/rev-parse.c           |  1 +
 t/t1514-rev-parse-detached.sh | 21 +++++++++++++++++++++
 t/t6018-rev-list-glob.sh      |  4 ++--
 3 files changed, 24 insertions(+), 2 deletions(-)
 create mode 100755 t/t1514-rev-parse-detached.sh
diff --git a/builtin/rev-parse.c b/builtin/rev-parse.c
index d85e08c..42f468c 100644
--- a/builtin/rev-parse.c
+++ b/builtin/rev-parse.c
@@ -642,6 +642,7 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix)
 				continue;
 			}
 			if (!strcmp(arg, "--all")) {
+				head_ref(show_reference, NULL);
 				for_each_ref(show_reference, NULL);
 				continue;
 			}
diff --git a/t/t1514-rev-parse-detached.sh b/t/t1514-rev-parse-detached.sh
new file mode 100755
index 0000000..eeb2318
--- /dev/null
+++ b/t/t1514-rev-parse-detached.sh
@@ -0,0 +1,21 @@
+#!/bin/sh
+
+test_description='test git rev-parse references'
+
+. ./test-lib.sh
+
+test_expect_success 'make repo' '
+	git commit --allow-empty -m commit1 &&
+	git commit --allow-empty -m commit2 &&
+	git checkout  --detach master &&
+	git commit --allow-empty -m commit3
+'
+
+head_sha1=`git rev-parse HEAD`
+
+test_expect_success 'HEAD should be listed in rev-parse --all' '
+	git rev-parse --all >all_refs &&
+	grep -q "$head_sha1" all_refs
+'
+
+test_done
diff --git a/t/t6018-rev-list-glob.sh b/t/t6018-rev-list-glob.sh
index d00f7db..ba0b89c 100755
--- a/t/t6018-rev-list-glob.sh
+++ b/t/t6018-rev-list-glob.sh
@@ -134,11 +134,11 @@ test_expect_success 'rev-parse --exclude with --branches' '
 '
 
 test_expect_success 'rev-parse --exclude with --all' '
-	compare rev-parse "--exclude=refs/remotes/* --all" "--branches --tags"
+	compare rev-parse "--exclude=refs/remotes/* --all" "HEAD --branches --tags"
 '
 
 test_expect_success 'rev-parse accumulates multiple --exclude' '
-	compare rev-parse "--exclude=refs/remotes/* --exclude=refs/tags/* --all" --branches
+	compare rev-parse "--exclude=refs/remotes/* --exclude=refs/tags/* --all" "HEAD --branches"
 '
 
 test_expect_success 'rev-list --glob=refs/heads/subspace/*' '
-- 
2.0.1.1697.g73c6810
Jeff King· Aug 31, 2014, 15:30 UTC · re: Max Kirillov · lore

Re: [PATCH] rev-parse: include HEAD in --all output

On Sun, Aug 31, 2014 at 01:24:48AM +0300, Max Kirillov wrote:
Show 7 quoted lines
> for_each_ref() does not include it itself, and without the hash
> the detached HEAD may be missed by some frontends (like gitk).
> 
> Add test which verifies the head is returned
> 
> Update test t6018-rev-list-glob.sh which relied on exact list of
> returned hashes.

I think the missing bit of the justification here is that "--all" _does_ include HEAD in other contexts (like in git-log), and rev-parse should probably match it.

This is probably the right thing to do. It's possible that some caller of rev-parse really depends on "--all" meaning "just the refs", but I kind of doubt it. Being in sync with the revision.c parser seems saner.

-Peff
Max Kirillov· Aug 31, 2014, 21:36 UTC · re: Jeff King · lore

Re: [PATCH] rev-parse: include HEAD in --all output

On Sun, Aug 31, 2014 at 11:30:54AM -0400, Jeff King wrote:
Show 19 quoted lines
> On Sun, Aug 31, 2014 at 01:24:48AM +0300, Max Kirillov wrote:
> 
>> for_each_ref() does not include it itself, and without
>> the hash the detached HEAD may be missed by some
>> frontends (like gitk).
>> 
>> Add test which verifies the head is returned
>> 
>> Update test t6018-rev-list-glob.sh which relied on exact
>> list of returned hashes.
> 
> I think the missing bit of the justification here is that
> "--all" _does_ include HEAD in other contexts (like in
> git-log), and rev-parse should probably match it.
> 
> This is probably the right thing to do. It's possible that
> some caller of rev-parse really depends on "--all" meaning
> "just the refs", but I kind of doubt it. Being in sync
> with the revision.c parser seems saner.

Actually, yes, this is a bit incompatible change, and while I'm pretty sure that rev-parse returning hashes should include detached HEAD, returning HEAD when it's called with something like "--symbolic" might be questioned. It could depend on the output mode (add HEAD only if printing hashes) but this kind of logic does not look good. So maybe some more opinions should be asked for.

btw, manpage for git-rev-parse says "Show all refs found in refs/.", should it also be changed?

-- 
Max
Junio C Hamano· Sep 2, 2014, 16:59 UTC · re: Max Kirillov · lore

Re: [PATCH] rev-parse: include HEAD in --all output

Max Kirillov <max@max630.net> writes:
> btw, manpage for git-rev-parse says "Show all refs found in
> refs/.", should it also be changed?

I would rather see "rev-parse --all" not to include HEAD, especially if it has been documented that way.

Max Kirillov· Sep 3, 2014, 16:18 UTC · re: Junio C Hamano · lore

Re: [PATCH] rev-parse: include HEAD in --all output

On Tue, Sep 02, 2014 at 09:59:17AM -0700, Junio C Hamano wrote:
> 
> I would rather see "rev-parse --all" not to include HEAD,
> especially if it has been documented that way.
Ok, then probably I'll want to change it in gitk.

But, with the "multiple working trees" feature, I would also want to get other HEADs in view, even if they are detached. I could add it to --all when it returns detached HEAD (like in git-rev-list), but if it does not, probably some new dedicated option should be for that purpose. Something like "git rev-parse --heads", for example.

-- 
Max

← back to recent threads