threads / patch / 18012

patch, 3 partsRe: [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:)

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:)

## tl;dr

5 messages between Feb 25, 2009 and Feb 25, 2009. Diffs are folded; open one to read it.

replies: 4people: 1as markdown or json

Dirk Wallenstein· Feb 25, 2009, 10:17 UTC · lore

[PATCH 0/3] git-branch-head: recognize all refs pointing to the current branch head as such

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· Feb 25, 2009, 10:17 UTC · re: Dirk Wallenstein · lore

[PATCH 1/3] git-show-branch: allow for multiple branch head columns

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(-)
Show changes to builtin-show-branch.c +6 −4
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· Feb 25, 2009, 10:17 UTC · re: Dirk Wallenstein · lore

[PATCH 2/3] git-show-branch: sha1 based branch head determination

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(-)
Show changes to builtin-show-branch.c +9 −2
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· Feb 25, 2009, 10:17 UTC · re: Dirk Wallenstein · lore

[PATCH 3/3] git-show-branch: avoid any column to be flagged as branch head if head is detached

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(-)
Show changes to builtin-show-branch.c +3 −0
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

← back to recent threads