git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Michael Schubert <mschub@elegosoft.com>
To: Jeff King <peff@peff.net>
Cc: Michael J Gruber <git@drmicha.warpmail.net>, git@vger.kernel.org
Subject: Re: [RFC] branch: list branches by single remote
Date: Wed, 24 Aug 2011 17:14:16 +0200	[thread overview]
Message-ID: <4E551548.9090807@elegosoft.com> (raw)
In-Reply-To: <20110816151448.GA5152@sigill.intra.peff.net>

On 08/16/2011 05:14 PM, Jeff King wrote:
> 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

  reply	other threads:[~2011-08-24 15:14 UTC|newest]

Thread overview: 77+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-08-02 17:17 [RFC] branch: list branches by single remote Michael Schubert
2011-08-04  4:06 ` Jeff King
2011-08-16 13:37   ` Michael J Gruber
2011-08-16 14:19     ` Michael Schubert
2011-08-16 15:14     ` Jeff King
2011-08-24 15:14       ` Michael Schubert [this message]
2011-08-24 15:37         ` Michael J Gruber
2011-08-24 16:02           ` Michael Schubert
2011-08-24 18:34           ` Junio C Hamano
2011-08-25  2:31             ` Thiago Farina
2011-08-25 18:02               ` Junio C Hamano
2011-08-25  8:29             ` Michael J Gruber
2011-08-25  8:30               ` [PATCH 0/5] RFC: patterns for branch list Michael J Gruber
2011-08-25  8:30                 ` [PATCH 1/5] branch: allow pattern arguments Michael J Gruber
2011-08-25 17:54                   ` Jeff King
2011-08-25 18:32                     ` Junio C Hamano
2011-08-25 19:29                       ` Junio C Hamano
2011-08-25  8:30                 ` [PATCH 2/5] branch: introduce --list argument Michael J Gruber
2011-08-25 18:34                   ` Junio C Hamano
2011-08-25 19:52                   ` Junio C Hamano
2011-08-25  8:30                 ` [PATCH 3/5] t6040; test branch -vv Michael J Gruber
2011-08-25  8:30                 ` [PATCH 4/5] branch: restructure -v vs. -vv Michael J Gruber
2011-08-25 19:02                   ` Junio C Hamano
2011-08-25  8:30                 ` [PATCH 5/5] branch: give patchsame count with -vvv Michael J Gruber
2011-08-25 17:53                 ` [PATCH 0/5] RFC: patterns for branch list Jeff King
2011-08-26  8:30                   ` Michael J Gruber
2011-08-26 16:55                     ` Junio C Hamano
2011-09-07 19:53                       ` Jeff King
2011-09-08  9:20                         ` Michael J Gruber
2011-08-26 14:05                   ` [PATCHv2 0/5] " Michael J Gruber
2011-08-26 14:05                     ` [PATCHv2 1/5] t6040: test branch -vv Michael J Gruber
2011-08-26 14:05                     ` [PATCHv2 2/5] git-tag: introduce long forms for the options Michael J Gruber
2011-08-26 17:11                       ` Junio C Hamano
2011-08-28 14:03                         ` Michael J Gruber
2011-08-26 14:05                     ` [PATCHv2 3/5] git-branch: introduce missing " Michael J Gruber
2011-08-26 17:13                       ` Junio C Hamano
2011-08-28 14:05                         ` Michael J Gruber
2011-08-26 14:05                     ` [PATCHv2 4/5] branch: introduce --list option Michael J Gruber
2011-08-26 17:43                       ` Junio C Hamano
2011-08-28 14:37                         ` Michael J Gruber
2011-09-07 19:56                           ` Jeff King
2011-09-08  9:24                             ` Michael J Gruber
2011-09-08 14:25                               ` [PATCHv4 0/5] mg/branch-list amendment Michael J Gruber
2011-09-08 14:25                                 ` [PATCHv4 4/5] branch: introduce --list option Michael J Gruber
2011-09-08 14:25                                 ` [PATCHv4 5/5] branch: allow pattern arguments Michael J Gruber
2011-09-08 21:03                               ` [PATCHv2 4/5] branch: introduce --list option Junio C Hamano
2011-09-08 21:11                                 ` Jeff King
2011-09-08 21:17                                 ` Junio C Hamano
2011-09-09  1:08                                   ` Jeff King
2011-09-09  6:54                                   ` Michael J Gruber
2011-09-09 16:02                                     ` Junio C Hamano
2011-09-09 19:29                                       ` Michael J Gruber
2011-09-09 19:30                                         ` Jeff King
2011-09-09 19:40                                           ` [PATCH] t3200: test branch creation with -v Michael J Gruber
2011-09-09 19:43                                             ` Jeff King
2011-09-09 19:45                                               ` Jeff King
2011-09-10 13:29                                               ` Michael J Gruber
2011-09-13  3:57                                                 ` Jeff King
2011-09-13 12:12                                                   ` Michael J Gruber
2011-09-13 16:13                                                     ` [PATCH] t3200: clean up checks for file existence Jeff King
2011-09-13 17:13                                                       ` Junio C Hamano
2011-09-13 17:16                                                         ` Jeff King
2011-08-26 14:05                     ` [PATCHv2 5/5] branch: allow pattern arguments Michael J Gruber
2011-08-26 18:45                       ` Junio C Hamano
2011-08-28 14:54                     ` [PATCHv3 0/5] patterns for branch list Michael J Gruber
2011-08-28 14:54                       ` [PATCHv3 1/5] t6040: test branch -vv Michael J Gruber
2011-08-28 14:54                       ` [PATCHv3 2/5] git-tag: introduce long forms for the options Michael J Gruber
2011-08-28 14:54                       ` [PATCHv3 3/5] git-branch: introduce missing " Michael J Gruber
2011-08-28 14:54                       ` [PATCHv3 4/5] branch: introduce --list option Michael J Gruber
2011-08-29  5:55                         ` Junio C Hamano
2011-08-29  6:35                           ` Michael J Gruber
2011-08-29  6:51                             ` Junio C Hamano
2011-08-28 14:54                       ` [PATCHv3 5/5] branch: allow pattern arguments Michael J Gruber
2011-09-06 13:10                         ` Michael Schubert
2011-09-06 14:21                           ` Michael J Gruber
2011-09-06 14:26                             ` Sverre Rabbelier
2011-09-06 16:11                               ` Michael J Gruber

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4E551548.9090807@elegosoft.com \
    --to=mschub@elegosoft.com \
    --cc=git@drmicha.warpmail.net \
    --cc=git@vger.kernel.org \
    --cc=peff@peff.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).