# Re: [PATCH 0/3] git-branch-head: recognize all refs pointing to the current branch head as such (Ah! topic is supposed to be git-show-branch:)

5 messages from 2009-02-25 to 2009-02-25. Participants: Halsmit, Dirk Wallenstein.
Thread: https://gitlist.dev/t/18012

## Dirk Wallenstein, 2009-02-25 10:17

Subject: [PATCH 0/3] git-branch-head: recognize all refs pointing to the current branch head as such
Message-ID: <cover.1235499771.git.halsmit@t-online.de>
URL: https://gitlist.dev/e/cover.1235499771.git.halsmit%40t-online.de

```
With git-show-branch I would like to specify "HEAD" or "<branchname>" on 
the command line and have it flagged with '*'. A prerequisite for this 
is that multiple columns can be flagged with '*'. 
That is what the first patch does.

Currently the following is true:
* if you have a detached head and specify "HEAD" it is flagged with '*'
* on a branch, the only ref argument that is flagged with '*' is the 
  name of the branch (like "master"), and not 'HEAD' nor `git rev-parse HEAD`.
* no arguments (neither heads nor non-heads) are reduced to only one ref if 
  they point to the same object (except for identical refs)

The only problem of a sha1-only based solution for branch head determination 
(comparing the sha1 of the current head and the specified ref) that I can 
think about is that, if some other branch head shares the sha1 of the current
branch head (immediately after creation), one could name the other branch and
it would be flagged as current branch head.

If that is too much of a problem, I propose an exceptions for the case when
exactly "HEAD" is specified, and optionally if a sha1 is specified and matches
head (`git rev-parse HEAD`). To not mix up different solutions in the 
posted patches I go with the sha1-only based proposal. 
That is the second patch.

Maybe, with a detached head, nothing should be flagged as branch head. 
If that is the case, the problem seems to be the retval from resolve_ref(), 
which returns its argument unmodified if called with "HEAD" (and not NULL). 
A local fix for that is in the third patch, which simply checks for that case.

The switch '--current' uses rev_is_head() to find out if the branchname has 
been specified on the command line, and adds such a column if not. That seems
alright, as with this patch, you can now easily spot all columns that refer to
the branch head.

Some example calls of rev_is_head() while being on branch "callDemo":
Normal:
	head=refs/heads/callDemo ; headlen=19 ; name=HEAD     ; head_sha1=968d629 ; sha1=968d629
	head=refs/heads/callDemo ; headlen=19 ; name=callDemo ; head_sha1=968d629 ; sha1=968d629
With '--current' (this adds a "<branchname>" column if it is not specified on the command line) :
	head=refs/heads/callDemo ; headlen=19 ; name=HEAD ; head_sha1=968d629 ; sha1=<none>
	head=refs/heads/callDemo ; headlen=19 ; name=callDemo ; head_sha1=968d629 ; sha1=<none>
With a detached head, with and without '--current' (without the third patch of 
this thread. With that patch, head is a pointer to an empty string):
	head=HEAD ; headlen=4 ; name=HEAD ; head_sha1=14706b5 ; sha1=<none>
	head=HEAD ; headlen=4 ; name=HEAD ; head_sha1=14706b5 ; sha1=14706b5
	head=HEAD ; headlen=4 ; name=callDemo ; head_sha1=14706b5 ; sha1=968d629


And an example of what would be the outcome of these patches while being on 
branch "sha1BasedOnly":
    git (sha1BasedOnly) $ ./git-show-branch --current HEAD^^ HEAD HEAD^ `git rev-parse HEAD` master
	! [HEAD^^] git-show-branch: allow for multiple branch head columns
	 * [HEAD] git-show-branch: avoid any column to be flagged as branch head if head is detached
	  ! [HEAD^] git-show-branch: sha1 based branch head determination
	   * [8e3c6fcd00a6a809bf1cca383a09c8d077c945d6] git-show-branch: avoid any column to be flagged as branch head if head is detached
	    ! [master] Merge branch 'for-junio' of git://source.winehq.org/~julliard/git/git
	     * [sha1BasedOnly] git-show-branch: avoid any column to be flagged as branch head if head is detached
	------
	 * * * [HEAD] git-show-branch: avoid any column to be flagged as branch head if head is detached
	 *+* * [HEAD^] git-show-branch: sha1 based branch head determination
	+*+* * [HEAD^^] git-show-branch: allow for multiple branch head columns
	------ [master] Merge branch 'for-junio' of git://source.winehq.org/~julliard/git/git




Dirk Wallenstein (3):
  git-show-branch: allow for multiple branch head columns
  git-show-branch: sha1 based branch head determination
  git-show-branch: avoid any column to be flagged as branch head if
    head is detached

 builtin-show-branch.c |   24 ++++++++++++++++++------
 1 files changed, 18 insertions(+), 6 deletions(-)

```

## Dirk Wallenstein, 2009-02-25 10:17

Subject: [PATCH 1/3] git-show-branch: allow for multiple branch head columns
Message-ID: <fa82452302afa700e7cbbacbf2d58a8c188a0c62.1235499771.git.halsmit@t-online.de>
URL: https://gitlist.dev/e/fa82452302afa700e7cbbacbf2d58a8c188a0c62.1235499771.git.halsmit%40t-online.de
In-Reply-To: <cover.1235499771.git.halsmit@t-online.de>

```
If multiple refs point to the current branch head, each of them
should show a '*' in the corresponding column. This commit changes
this information about columns from an absolute index to a bitmask.
---
 builtin-show-branch.c |   10 ++++++----
 1 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/builtin-show-branch.c b/builtin-show-branch.c
index 306b850..df83491 100644
--- a/builtin-show-branch.c
+++ b/builtin-show-branch.c
@@ -604,7 +604,7 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
 	int sha1_name = 0;
 	int shown_merge_point = 0;
 	int with_current_branch = 0;
-	int head_at = -1;
+	unsigned int head_at = 0;
 	int topics = 0;
 	int dense = 1;
 	int reflog = 0;
@@ -855,8 +855,10 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
 			else
 				puts(reflog_msg[i]);
 
-			if (is_head)
-				head_at = i;
+			if (is_head) {
+				assert(MAX_REVS <= (sizeof(head_at) * 8));
+				head_at |= (1UL << i);
+			}
 		}
 		if (0 <= extra) {
 			for (i = 0; i < num_rev; i++)
@@ -900,7 +902,7 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
 					mark = ' ';
 				else if (is_merge)
 					mark = '-';
-				else if (i == head_at)
+				else if (head_at & (1UL << i))
 					mark = '*';
 				else
 					mark = '+';
-- 
1.6.1

```

## Dirk Wallenstein, 2009-02-25 10:17

Subject: [PATCH 2/3] git-show-branch: sha1 based branch head determination
Message-ID: <14706b525c71ab0628e54f7bd24b3303bf422698.1235499771.git.halsmit@t-online.de>
URL: https://gitlist.dev/e/14706b525c71ab0628e54f7bd24b3303bf422698.1235499771.git.halsmit%40t-online.de
In-Reply-To: <cover.1235499771.git.halsmit@t-online.de>

```
Any ref that points to the same object as the one that is
currently pointed at by HEAD, is recognized as head, which is
then marked with a '*' in the corresponding column.
---
 builtin-show-branch.c |   11 +++++++++--
 1 files changed, 9 insertions(+), 2 deletions(-)

diff --git a/builtin-show-branch.c b/builtin-show-branch.c
index df83491..96ae3cb 100644
--- a/builtin-show-branch.c
+++ b/builtin-show-branch.c
@@ -460,9 +460,16 @@ static void snarf_refs(int head, int remotes)
 static int rev_is_head(char *head, int headlen, char *name,
 		       unsigned char *head_sha1, unsigned char *sha1)
 {
-	if ((!head[0]) ||
-	    (head_sha1 && sha1 && hashcmp(head_sha1, sha1)))
+	if (!head[0])
 		return 0;
+	if (head_sha1 && sha1) {
+		if (hashcmp(head_sha1, sha1)) {
+			return 0;
+		} else {
+			return 1;
+		}
+	}
+
 	if (!prefixcmp(head, "refs/heads/"))
 		head += 11;
 	if (!prefixcmp(name, "refs/heads/"))
-- 
1.6.1

```

## Dirk Wallenstein, 2009-02-25 10:17

Subject: [PATCH 3/3] git-show-branch: avoid any column to be flagged as branch head if head is detached
Message-ID: <8e3c6fcd00a6a809bf1cca383a09c8d077c945d6.1235499771.git.halsmit@t-online.de>
URL: https://gitlist.dev/e/8e3c6fcd00a6a809bf1cca383a09c8d077c945d6.1235499771.git.halsmit%40t-online.de
In-Reply-To: <cover.1235499771.git.halsmit@t-online.de>

```
For a detached head, resolve_ref() returns the argument "HEAD"
unmodified. That case is checked for and pretended to be NULL,
which consequently avoids any column to be flagged as branch head.
---
 builtin-show-branch.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)

diff --git a/builtin-show-branch.c b/builtin-show-branch.c
index 96ae3cb..b073a31 100644
--- a/builtin-show-branch.c
+++ b/builtin-show-branch.c
@@ -766,6 +766,9 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
 	}
 
 	head_p = resolve_ref("HEAD", head_sha1, 1, NULL);
+	if (head_p && !strcmp(head_p, "HEAD")) {
+		head_p = NULL;
+	}
 	if (head_p) {
 		head_len = strlen(head_p);
 		memcpy(head, head_p, head_len + 1);
-- 
1.6.1

```

## Halsmit, 2009-02-25 10:29

Subject: Re: [PATCH 0/3] git-branch-head: recognize all refs pointing to the current branch head as such (Ah! topic is supposed to be git-show-branch:)
Message-ID: <200902251129.30588.halsmit@t-online.de>
URL: https://gitlist.dev/e/200902251129.30588.halsmit%40t-online.de
In-Reply-To: <cover.1235499771.git.halsmit@t-online.de>

```
Topic is supposed to be git-show-branch:

```
