git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [RFC] branch: list branches by single remote

From
MSMichael Schubert <mschub@elegosoft.com>
Date
Aug 24, 2011, 15:14 UTC
Message-ID
<4E551548.9090807@elegosoft.com>
In-Reply-To
<20110816151448.GA5152@sigill.intra.peff.net>
On 08/16/2011 05:14 PM, Jeff King wrote:
Show 16 quoted lines
> On Tue, Aug 16, 2011 at 03:37:33PM +0200, Michael J Gruber wrote:
> 
>>> This isn't right. You are assuming that a remote called "foo" will have
>>> all of its branches in refs/remotes/foo. That's true under the default
>>> configuration, but technically speaking, the remote tracking branches of
>>> "foo" are defined by the right-hand side of foo's fetch refspecs.
>>
>> You are 100% right here, but...
>>
>>> So I think you want something more like this:
>>
>> ...the op still might want to filter simply by the remote name.
> 
> That is a perfectly reasonable approach. It just should be called
> "--glob" or something, and not "remote".  git-tag allows patterns to an
> explicit "tag -l", but "-l" is already taken for git-branch.
As suggested, I've just called it "--glob" for now.
--- 8< ---
Subject: [PATCH] branch: add option "glob" to filter listed branches

When calling "git branch" with either "-r" or "-a", there is no way to further limit the output. Add an option "--glob=<pattern>" to allow limiting the output to matching branch names.

Signed-off-by: Michael Schubert <mschub@elegosoft.com>
---
 Documentation/git-branch.txt |    8 ++++++--
 builtin/branch.c             |   13 ++++++++++---
 2 files changed, 16 insertions(+), 5 deletions(-)
diff --git a/Documentation/git-branch.txt b/Documentation/git-branch.txt
index c50f189..5de3c79 100644
--- a/Documentation/git-branch.txt
+++ b/Documentation/git-branch.txt
@@ -8,7 +8,7 @@ git-branch - List, create, or delete branches
 SYNOPSIS
 --------
 [verse]
-'git branch' [--color[=<when>] | --no-color] [-r | -a]
+'git branch' [--color[=<when>] | --no-color] [(-r | -a) [--glob=<pattern>]]
 	[-v [--abbrev=<length> | --no-abbrev]]
 	[(--merged | --no-merged | --contains) [<commit>]]
 'git branch' [--set-upstream | --track | --no-track] [-l] [-f] <branchname> [<start-point>]
@@ -20,7 +20,8 @@ DESCRIPTION
 
 With no arguments, existing branches are listed and the current branch will
 be highlighted with an asterisk.  Option `-r` causes the remote-tracking
-branches to be listed, and option `-a` shows both.
+branches to be listed, option `-a` shows both and `--glob` limits the
+output to branches matching <pattern>.
 
 With `--contains`, shows only the branches that contain the named commit
 (in other words, the branches whose tip commits are descendants of the
@@ -105,6 +106,9 @@ OPTIONS
 -a::
 	List both remote-tracking branches and local branches.
 
+--glob=<pattern>::
+	List only branches matching pattern.
+
 -v::
 --verbose::
 	Show sha1 and commit subject line for each head, along with
diff --git a/builtin/branch.c b/builtin/branch.c
index 3142daa..74730ad 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -17,7 +17,7 @@
 #include "revision.h"
 
 static const char * const builtin_branch_usage[] = {
-	"git branch [options] [-r | -a] [--merged | --no-merged]",
+	"git branch [options] [(-r | -a) [--glob=<pattern>]] [--merged | --no-merged]",
 	"git branch [options] [-l] [-f] <branchname> [<start-point>]",
 	"git branch [options] [-r] (-d | -D) <branchname>...",
 	"git branch [options] (-m | -M) [<oldbranch>] <newbranch>",
@@ -260,6 +260,7 @@ static char *resolve_symref(const char *src, const char *prefix)
 
 struct append_ref_cb {
 	struct ref_list *ref_list;
+	const char *glob;
 	int ret;
 };
 
@@ -297,6 +298,9 @@ static int append_ref(const char *refname, const unsigned char *sha1, int flags,
 	if ((kind & ref_list->kinds) == 0)
 		return 0;
 
+	if (cb->glob && fnmatch(cb->glob, refname, 0))
+		return 0;
+
 	commit = NULL;
 	if (ref_list->verbose || ref_list->with_commit || merge_filter != NO_FILTER) {
 		commit = lookup_commit_reference_gently(sha1, 1);
@@ -492,7 +496,7 @@ static void show_detached(struct ref_list *ref_list)
 	}
 }
 
-static int print_ref_list(int kinds, int detached, int verbose, int abbrev, struct commit_list *with_commit)
+static int print_ref_list(int kinds, int detached, int verbose, int abbrev, struct commit_list *with_commit, const char *glob)
 {
 	int i;
 	struct append_ref_cb cb;
@@ -506,6 +510,7 @@ static int print_ref_list(int kinds, int detached, int verbose, int abbrev, stru
 	if (merge_filter != NO_FILTER)
 		init_revisions(&ref_list.revs, NULL);
 	cb.ref_list = &ref_list;
+	cb.glob = glob;
 	cb.ret = 0;
 	for_each_rawref(append_ref, &cb);
 	if (merge_filter != NO_FILTER) {
@@ -618,6 +623,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 	enum branch_track track;
 	int kinds = REF_LOCAL_BRANCH;
 	struct commit_list *with_commit = NULL;
+	char *glob = NULL;
 
 	struct option options[] = {
 		OPT_GROUP("Generic options"),
@@ -647,6 +653,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 		OPT_GROUP("Specific git-branch actions:"),
 		OPT_SET_INT('a', NULL, &kinds, "list both remote-tracking and local branches",
 			REF_REMOTE_BRANCH | REF_LOCAL_BRANCH),
+		OPT_STRING(0, "glob", &glob, "pattern", "list only branches matching the pattern"),
 		OPT_BIT('d', NULL, &delete, "delete fully merged branch", 1),
 		OPT_BIT('D', NULL, &delete, "delete branch (even if not merged)", 2),
 		OPT_BIT('m', NULL, &rename, "move/rename a branch and its reflog", 1),
@@ -699,7 +706,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 	if (delete)
 		return delete_branches(argc, argv, delete > 1, kinds);
 	else if (argc == 0)
-		return print_ref_list(kinds, detached, verbose, abbrev, with_commit);
+		return print_ref_list(kinds, detached, verbose, abbrev, with_commit, glob);
 	else if (rename && (argc == 1))
 		rename_branch(head, argv[0], rename > 1);
 	else if (rename && (argc == 2))
-- 
1.7.6.577.g8d918.dirty
Previous: Jeff KingNext: Michael J Gruber
Message 6 of 77 in “[RFC] branch: list branches by single remote”
  1. Michael SchubertAug 2, 2011
  2. Jeff KingAug 4, 2011
  3. Michael J GruberAug 16, 2011
  4. Michael SchubertAug 16, 2011
  5. Jeff KingAug 16, 2011
  6. Michael SchubertAug 24, 2011
  7. Michael J GruberAug 24, 2011
  8. Michael SchubertAug 24, 2011
  9. Junio C HamanoAug 24, 2011
  10. Thiago FarinaAug 25, 2011
  11. Junio C HamanoAug 25, 2011
  12. Michael J GruberAug 25, 2011
  13. 0/5 RFC: patterns for branch listMichael J Gruber, Aug 25, 2011
  14. Jeff KingAug 25, 2011
  15. Michael J GruberAug 26, 2011
  16. Junio C HamanoAug 26, 2011
  17. Jeff KingSep 7, 2011
  18. Michael J GruberSep 8, 2011
  19. 0/5 patterns for branch listMichael J Gruber, Aug 26, 2011
  20. 0/5 patterns for branch listMichael J Gruber, Aug 28, 2011
  21. 1/5 t6040: test branch -vvMichael J Gruber, Aug 28, 2011
  22. 2/5 git-tag: introduce long forms for the optionsMichael J Gruber, Aug 28, 2011
  23. 3/5 git-branch: introduce missing long forms for the optionsMichael J Gruber, Aug 28, 2011
  24. 4/5 branch: introduce --list optionMichael J Gruber, Aug 28, 2011
  25. Junio C HamanoAug 29, 2011
  26. Michael J GruberAug 29, 2011
  27. Junio C HamanoAug 29, 2011
  28. 5/5 branch: allow pattern argumentsMichael J Gruber, Aug 28, 2011
  29. Michael SchubertSep 6, 2011
  30. Michael J GruberSep 6, 2011
  31. Sverre RabbelierSep 6, 2011
  32. Michael J GruberSep 6, 2011
  33. 1/5 t6040: test branch -vvMichael J Gruber, Aug 26, 2011
  34. 2/5 git-tag: introduce long forms for the optionsMichael J Gruber, Aug 26, 2011
  35. Junio C HamanoAug 26, 2011
  36. Michael J GruberAug 28, 2011
  37. 3/5 git-branch: introduce missing long forms for the optionsMichael J Gruber, Aug 26, 2011
  38. Junio C HamanoAug 26, 2011
  39. Michael J GruberAug 28, 2011
  40. 4/5 branch: introduce --list optionMichael J Gruber, Aug 26, 2011
  41. Junio C HamanoAug 26, 2011
  42. Michael J GruberAug 28, 2011
  43. Jeff KingSep 7, 2011
  44. Michael J GruberSep 8, 2011
  45. 0/5 mg/branch-list amendmentMichael J Gruber, Sep 8, 2011
  46. 4/5 branch: introduce --list optionMichael J Gruber, Sep 8, 2011
  47. 5/5 branch: allow pattern argumentsMichael J Gruber, Sep 8, 2011
  48. Junio C HamanoSep 8, 2011
  49. Jeff KingSep 8, 2011
  50. Junio C HamanoSep 8, 2011
  51. Jeff KingSep 9, 2011
  52. Michael J GruberSep 9, 2011
  53. Junio C HamanoSep 9, 2011
  54. Michael J GruberSep 9, 2011
  55. Jeff KingSep 9, 2011
  56. t3200: test branch creation with -vMichael J Gruber, Sep 9, 2011
  57. Jeff KingSep 9, 2011
  58. Jeff KingSep 9, 2011
  59. Michael J GruberSep 10, 2011
  60. Jeff KingSep 13, 2011
  61. Michael J GruberSep 13, 2011
  62. t3200: clean up checks for file existenceJeff King, Sep 13, 2011
  63. Junio C HamanoSep 13, 2011
  64. Jeff KingSep 13, 2011
  65. 5/5 branch: allow pattern argumentsMichael J Gruber, Aug 26, 2011
  66. Junio C HamanoAug 26, 2011
  67. 1/5 branch: allow pattern argumentsMichael J Gruber, Aug 25, 2011
  68. Jeff KingAug 25, 2011
  69. Junio C HamanoAug 25, 2011
  70. Junio C HamanoAug 25, 2011
  71. 2/5 branch: introduce --list argumentMichael J Gruber, Aug 25, 2011
  72. Junio C HamanoAug 25, 2011
  73. Junio C HamanoAug 25, 2011
  74. 3/5 t6040; test branch -vvMichael J Gruber, Aug 25, 2011
  75. 4/5 branch: restructure -v vs. -vvMichael J Gruber, Aug 25, 2011
  76. Junio C HamanoAug 25, 2011
  77. 5/5 branch: give patchsame count with -vvvMichael J Gruber, Aug 25, 2011

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.