# [RFC] add detached HEAD to --all listing

8 messages from 2014-08-27 to 2014-09-03. Participants: Max Kirillov, Jeff King, Junio C Hamano.
Thread: https://gitlist.dev/t/37424

## Max Kirillov, 2014-08-27 14:18

Subject: [RFC] add detached HEAD to --all listing
Message-ID: <CAF7_NFRKzU0_5Eq4kgtW3RChW3z5q8QBjWvvN+TSyOhORkf8hQ@mail.gmail.com>
URL: https://gitlist.dev/e/CAF7_NFRKzU0_5Eq4kgtW3RChW3z5q8QBjWvvN%2BTSyOhORkf8hQ%40mail.gmail.com

```
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, 2014-08-27 14:22

Subject: Re: [RFC] add detached HEAD to --all listing
Message-ID: <20140827142249.GC31879@peff.net>
URL: https://gitlist.dev/e/20140827142249.GC31879%40peff.net
In-Reply-To: <CAF7_NFRKzU0_5Eq4kgtW3RChW3z5q8QBjWvvN+TSyOhORkf8hQ@mail.gmail.com>

```
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, 2014-08-27 14:33

Subject: Re: [RFC] add detached HEAD to --all listing
Message-ID: <CAF7_NFRz6Zc-wTDSFdkW4N2wRATZ8-g05j6sFu1t7DB0X72dkg@mail.gmail.com>
URL: https://gitlist.dev/e/CAF7_NFRz6Zc-wTDSFdkW4N2wRATZ8-g05j6sFu1t7DB0X72dkg%40mail.gmail.com
In-Reply-To: <20140827142249.GC31879@peff.net>

```
On Wed, Aug 27, 2014 at 5:22 PM, Jeff King <peff@peff.net> wrote:
> 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, 2014-08-30 22:24

Subject: [PATCH] rev-parse: include HEAD in --all output
Message-ID: <1409437488-25233-1-git-send-email-max@max630.net>
URL: https://gitlist.dev/e/1409437488-25233-1-git-send-email-max%40max630.net
In-Reply-To: <CAF7_NFRz6Zc-wTDSFdkW4N2wRATZ8-g05j6sFu1t7DB0X72dkg@mail.gmail.com>

```
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, 2014-08-31 15:30

Subject: Re: [PATCH] rev-parse: include HEAD in --all output
Message-ID: <20140831153054.GD17449@peff.net>
URL: https://gitlist.dev/e/20140831153054.GD17449%40peff.net
In-Reply-To: <1409437488-25233-1-git-send-email-max@max630.net>

```
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.

-Peff

```

## Max Kirillov, 2014-08-31 21:36

Subject: Re: [PATCH] rev-parse: include HEAD in --all output
Message-ID: <20140831213606.GB6385@wheezy.local>
URL: https://gitlist.dev/e/20140831213606.GB6385%40wheezy.local
In-Reply-To: <20140831153054.GD17449@peff.net>

```
On Sun, Aug 31, 2014 at 11:30:54AM -0400, Jeff King wrote:
> 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, 2014-09-02 16:59

Subject: Re: [PATCH] rev-parse: include HEAD in --all output
Message-ID: <xmqqsika2c2i.fsf@gitster.dls.corp.google.com>
URL: https://gitlist.dev/e/xmqqsika2c2i.fsf%40gitster.dls.corp.google.com
In-Reply-To: <20140831213606.GB6385@wheezy.local>

```
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, 2014-09-03 16:18

Subject: Re: [PATCH] rev-parse: include HEAD in --all output
Message-ID: <20140903161856.GC6385@wheezy.local>
URL: https://gitlist.dev/e/20140903161856.GC6385%40wheezy.local
In-Reply-To: <xmqqsika2c2i.fsf@gitster.dls.corp.google.com>

```
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

```
