threads / patch / 43241

patchAdd --remotes option to show-branch

Subject: [PATCH] Add --remotes option to show-branch

## tl;dr

4 messages between Dec 18, 2006 and Dec 19, 2006. Diffs are folded; open one to read it.

replies: 3people: 2as markdown or json

Brian Gernhardt· Dec 18, 2006, 14:53 UTC · lore

We had --heads for refs/heads and --tags for refs/tags, but no easy way to specify refs/remotes, which is going to be more common now that it's the default layout for clone.

Also add remotes to --all and update documentation.
Signed-off-by: Brian Gernhardt <benji@silverinsanity.com>
---
 Another patch in my "features I wasted time looking for" series.
 I figure that patches are more useful than feature requests, and
 that seems to be proven by the fact my patches are getting applied.
 This is my first serious foray into git's C code.  I simply copied
 append_head_ref to get append_remote_ref, since the goal was similar.
 If there are additional checks that would need to be done in there,
 feel free to fix it, or tell me what I should look for.
 As a side note, there's no documentation for the --topics option for
 show-branch.  If I figure out what it does, I'll write it, but I'd bet
 someone else already knows and would love to help a neophite git coder
 figure it out.  ;-)
 Documentation/git-show-branch.txt |   18 +++++++++---------
 builtin-show-branch.c             |   31 ++++++++++++++++++++++++++-----
 2 files changed, 35 insertions(+), 14 deletions(-)
Show changes to 2 files +35 −12

Documentation/git-show-branch.txt, builtin-show-branch.c

diff --git a/Documentation/git-show-branch.txt b/Documentation/git-show-branch.txt
index a2445a4..25136b0 100644
--- a/Documentation/git-show-branch.txt
+++ b/Documentation/git-show-branch.txt
@@ -8,9 +8,9 @@ git-show-branch - Show branches and their commits
 SYNOPSIS
 --------
 [verse]
-'git-show-branch' [--all] [--heads] [--tags] [--topo-order] [--current]
+'git-show-branch' [--all] [--heads] [--remotes] [--tags] [--topo-order]
 		[--more=<n> | --list | --independent | --merge-base]
-		[--no-name | --sha1-name] [<rev> | <glob>]...
+		[--current] [--no-name | --sha1-name] [<rev> | <glob>]...
 
 DESCRIPTION
 -----------
@@ -37,14 +37,9 @@ OPTIONS
 	branches under $GIT_DIR/refs/heads/topic, giving
 	`topic/*` would show all of them.
 
---all --heads --tags::
+--all --heads --remotes --tags::
 	Show all refs under $GIT_DIR/refs, $GIT_DIR/refs/heads,
-	and $GIT_DIR/refs/tags, respectively.
-
---current::
-	With this option, the command includes the current
-	branch to the list of revs to be shown when it is not
-	given on the command line.
+	$GIT_DIR/refs/remotes, and $GIT_DIR/refs/tags, respectively.
 
 --topo-order::
         By default, the branches and their commits are shown in
@@ -77,6 +72,11 @@ OPTIONS
 	Among the <reference>s given, display only the ones that
 	cannot be reached from any other <reference>.
 
+--current::
+	With this option, the command includes the current
+	branch to the list of revs to be shown when it is not
+	given on the command line.
+
 --no-name::
 	Do not show naming strings for each commit.
 
diff --git a/builtin-show-branch.c b/builtin-show-branch.c
index fb1a400..91251f9 100644
--- a/builtin-show-branch.c
+++ b/builtin-show-branch.c
@@ -6,7 +6,7 @@
 #include "builtin.h"
 
 static const char show_branch_usage[] =
-"git-show-branch [--sparse] [--current] [--all] [--heads] [--tags] [--topo-order] [--more=count | --list | --independent | --merge-base ] [--topics] [<refs>...]";
+"git-show-branch [--sparse] [--current] [--all] [--heads] [--remotes] [--tags] [--topo-order] [--more=count | --list | --independent | --merge-base ] [--topics] [<refs>...]";
 
 static int default_num;
 static int default_alloc;
@@ -383,6 +383,20 @@ static int append_head_ref(const char *refname, const unsigned char *sha1, int f
 	return append_ref(refname + ofs, sha1, flag, cb_data);
 }
 
+static int append_remote_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
+{
+	unsigned char tmp[20];
+	int ofs = 13;
+	if (strncmp(refname, "refs/remotes/", ofs))
+		return 0;
+	/* If both remotes/_/foo and tags/foo exists, get_sha1 would
+	 * get confused.
+	 */
+	if (get_sha1(refname + ofs, tmp) || hashcmp(tmp, sha1))
+		ofs = 5;
+	return append_ref(refname + ofs, sha1, flag, cb_data);
+}
+
 static int append_tag_ref(const char *refname, const unsigned char *sha1, int flag, void *cb_data)
 {
 	if (strncmp(refname, "refs/tags/", 10))
@@ -423,13 +437,18 @@ static int append_matching_ref(const char *refname, const unsigned char *sha1, i
 	return append_ref(refname, sha1, flag, cb_data);
 }
 
-static void snarf_refs(int head, int tag)
+static void snarf_refs(int head, int tag, int remote)
 {
 	if (head) {
 		int orig_cnt = ref_name_cnt;
 		for_each_ref(append_head_ref, NULL);
 		sort_ref_range(orig_cnt, ref_name_cnt);
 	}
+	if (remote) {
+		int orig_cnt = ref_name_cnt;
+		for_each_ref(append_remote_ref, NULL);
+		sort_ref_range(orig_cnt, ref_name_cnt);
+	}
 	if (tag) {
 		int orig_cnt = ref_name_cnt;
 		for_each_ref(append_tag_ref, NULL);
@@ -554,7 +573,7 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
 	struct commit_list *list = NULL, *seen = NULL;
 	unsigned int rev_mask[MAX_REVS];
 	int num_rev, i, extra = 0;
-	int all_heads = 0, all_tags = 0;
+	int all_heads = 0, all_tags = 0, all_remotes;
 	int all_mask, all_revs;
 	int lifo = 1;
 	char head[128];
@@ -586,9 +605,11 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
 			break;
 		}
 		else if (!strcmp(arg, "--all"))
-			all_heads = all_tags = 1;
+			all_heads = all_remotes = all_tags = 1;
 		else if (!strcmp(arg, "--heads"))
 			all_heads = 1;
+		else if (!strcmp(arg, "--remotes"))
+			all_remotes = 1;
 		else if (!strcmp(arg, "--tags"))
 			all_tags = 1;
 		else if (!strcmp(arg, "--more"))
@@ -630,7 +651,7 @@ int cmd_show_branch(int ac, const char **av, const char *prefix)
 		all_heads = 1;
 
 	if (all_heads + all_tags)
-		snarf_refs(all_heads, all_tags);
+		snarf_refs(all_heads, all_tags, all_remotes);
 	while (0 < ac) {
 		append_one_rev(*av);
 		ac--; av++;
-- 
1.4.4.1.GIT
Brian Gernhardt· Dec 19, 2006, 05:17 UTC · lore

Re: [PATCH] Add --remotes option to show-branch

On Dec 18, 2006, at 3:45 PM, Junio C Hamano wrote:
Show 18 quoted lines
> I've never used --heads and --tags myself because
>
> 	git show-branch heads/*
>         git show-branch tags/*
>
> works perfectly well for me, and it often is not very useful to
> look at _all_ the heads (tags are even much less interesting) in
> a topic heavy repository anyway.  IOW, I more often do something
> like:
>
> 	git show-branch --topics master heads/js/*
>
> to see what Johannes has relative to "master".
>
> I actually was thinking about removing these flags, not adding
> --remotes, because
>
> 	git show-branch remotes/origin/*

I agree that tags/* is fairly useless. Way too much data. And if you want to remote --heads and --tags, then --remotes doesn't have much place. I was simply poking around and thought that if we have those two, then the third seemed natural and odd to miss.

However if --all meant all heads, local or remote, I'd be fairly pleased. heads/* is just as easy as --heads (and leaving it blank is even easier), but --remotes is significantly easier than "remotes/ origin/* remotes/partner/*". And sometimes I'd really like to know where all the branches are in relation to each other.

Pardon if this doesn't make much sense.  It's been a looooong day...
Junio C Hamano· Dec 19, 2006, 05:50 UTC · re: Brian Gernhardt · lore

Re: [PATCH] Add --remotes option to show-branch

Brian Gernhardt <benji@silverinsanity.com> writes:
Show 8 quoted lines
>> I actually was thinking about removing these flags, not adding
>> --remotes, because
>>
>> 	git show-branch remotes/origin/*
>
> ... but --remotes is significantly easier than "remotes/
> origin/* remotes/partner/*".  And sometimes I'd really like to know
> where all the branches are in relation to each other.

Actually, git show-branch origin/* would work equally well if you do not have local "heads/origin/*" branches.

Brian Gernhardt· Dec 19, 2006, 18:59 UTC · re: Junio C Hamano · lore

Re: [PATCH] Add --remotes option to show-branch

On Dec 19, 2006, at 12:50 AM, Junio C Hamano wrote:
> Actually, git show-branch origin/* would work equally well if
> you do not have local "heads/origin/*" branches.

Fair enough. I'm not strongly attached to the options for the various refs. And there is always showbranch.default. I still think that --all would be good to keep around as meaning refs/heads/* and refs/remotes/*/*, however (and NOT refs/tags/*). If nothing else, so a small-ish repository can compare everything fairly easily.

← back to recent threads