branch: add optional parameter to -r to specify remote
The following patch adds an optional parameter to branch -r to use as -r=<remote> in order to list branches on a particular remote.
'git branch -d -r one/another' can also be done with 'git branch -d -r=one another'.
I haven't found tests specially crafted for 'git branch -r -d <branch>'. I hope the tests I have added are in the correct places.
add '--remote' as long version for '-r'
update documentation
add tests
--- Documentation/git-branch.txt | 10 +++-- builtin/branch.c | 72 ++++++++++++++++++++++++++++++++---------- t/t3200-branch.sh | 34 ++++++++++++++++++++ t/t3203-branch-output.sh | 59 ++++++++++++++++++++++++++++++++++ 4 files changed, 154 insertions(+), 21 deletions(-)
Documentation/git-branch.txt +6 -4
@@ -8,12 +8,12 @@ git-branch - List, create, or delete branches SYNOPSIS -------- [verse] -'git branch' [--color[=<when>] | --no-color] [-r | -a] +'git branch' [--color[=<when>] | --no-color] [-r[=<remote>] | -a] [-v [--abbrev=<length> | --no-abbrev]] [(--merged | --no-merged | --contains) [<commit>]] 'git branch' [--set-upstream | --track | --no-track] [-l] [-f] <branchname> [<start-point>] 'git branch' (-m | -M) [<oldbranch>] <newbranch> -'git branch' (-d | -D) [-r] <branchname>... +'git branch' (-d | -D) [-r[=<remote>]] <branchname>... DESCRIPTION -----------
@@ -99,8 +99,10 @@ OPTIONS default to color output. Same as `--color=never`. --r:: - List or delete (if used with -d) the remote-tracking branches. +-r[=<remote>]:: +--remote[=<remote>]:: + List or delete (if used with -d) the remote-tracking branches from + <remote> if specified. -a:: List both remote-tracking branches and local branches.
builtin/branch.c +55 -17
165 lines not on this page · raw message
t/t3200-branch.sh +34 -0
@@ -10,6 +10,23 @@ handled. Specifically, that a bogus branch is not created. ' . ./test-lib.sh +setup_repository () { + mkdir "$1" && ( + cd "$1" && + git init && + >file && + git add file && + test_tick && + git commit -m "Initial" && + git checkout -b side && + >elif && + git add elif && + test_tick && + git commit -m "Second" && + git checkout master + ) +} + test_expect_success \ 'prepare a trivial repository' \ 'echo Hello > A &&
@@ -542,4 +559,21 @@ test_expect_success 'attempt to delete a branch merged to its base' ' test_must_fail git branch -d my10 ' +test_expect_success 'git branch -d -r=one another should delete another branch from remote one' ' + setup_repository one && + setup_repository onelong && + ( + cd one && git branch another + ) && + git remote add -f one one && + git remote add -f onelong onelong && + git branch -d -r=one another && + test ! -f .git/refs/remotes/one/another && + test ! -f .git/logs/refs/remotes/one/another' + +test_expect_success 'git branch -d --remote=one master should delete master branch from remote one' ' + git branch -d --remote=one master && + test ! -f .git/refs/remotes/one/master && + test ! -f .git/logs/refs/remotes/one/master' + test_done
t/t3203-branch-output.sh +59 -0
75 lines not on this page · raw message
Signature · 1 line
1.7.6.rc2.4.g36bfb.dirty
Quoted 3 lines · Boris Faure
add '--remote' as long version for '-r'
update documentation
add tests
(style) Sentences begin with a capital letter and ends with a period.
This commit does a lot more than the above, no? It adds an optional remote name parameter to the existing "-r" option and limits the output to the remote tracking branches of the remote when it is specified.
Quoted 1 line
---
Sign-off?
Quoted 6 lines
Documentation/git-branch.txt +0
-0
Documentation/git-branch.txt OPTIONS · line 105
Quoted 9 lines
Documentation/git-branch.txt +4
-2@@ -99,8 +99,10 @@ OPTIONS default to color output. Same as `--color=never`. --r:: - List or delete (if used with -d) the remote-tracking branches. +-r[=<remote>]:: +--remote[=<remote>]:: + List or delete (if used with -d) the remote-tracking branches from + <remote> if specified.
It is now unspecified what the command would do when the optional <remote> is left unspecified.
Quoted 10 lines
builtin/branch.c +1
-1@@ -17,15 +17,18 @@#include "revision.h" static const char *const builtin_branch_usage[] = { -"git branch [options] [-r | -a] [--merged | --no-merged]" , +"git branch [options] [-r[=<remote>] | -a] [--merged | --no-merged]" ,
builtin/branch.c line 29
Quoted 10 lines
builtin/branch.c +2
-1"git branch [options] [-l] [-f] <branchname> [<start-point>]" , -"git branch [options] [-r] (-d | -D) <branchname>" , +"git branch [options] [-r[=<remote>]] (-d | -D) <branchname>" ,"git branch [options] (-m | -M) [<oldbranch>] <newbranch>" , NULL };#define REF_LOCAL_BRANCH 0x01#define REF_REMOTE_BRANCH 0x02 +static int kinds =REF_LOCAL_BRANCH ;
This used to be nicely scoped out of global space and got passed around as parameter, but now it has become a global? I do not see a good reason for this change.
builtin/branch.c line 31
Quoted 1 line
builtin/branch.c +1
-0+static const char *remote =NULL ;
Two issues.
1. Presumably you wanted to have this change because you have too many remotes, way more than two, and wanted to filter the output from remotes that you are not interested in. Is it entirely implausible that you might be interested in not just one, but two remotes out of many remotes you have? A single string variable would not suffice for that but you should be able to make this an array of strings.
2. The name suck for a global variable (same for the "kinds" that is now global), and caused the patch to become unnecessarily big by renaming concisely named "remote" to ugly "remote_trans" in delete_branches() function. In general, local variables are scoped narrower and readers can rely on the context to tell what the variable is _for_, so it is easy for them to see a variable called "remote" and understand what aspects of "remote" the variable is trying to express. On the other hand, global variables are used in wider context and has to have more descriptive names. This is about "filtering" set of remote tracking branches we deal with to the subset specified by this variable, so it at least has to have "filter" or "limit" or something to that effect in its name.
builtin/branch.c static int branch_merged(int kind, const char *name, · line 155
Quoted 12 lines
builtin/branch.c +2
-2@@ -144,12 +147,12 @@ static int branch_merged(int kind, const char *name,return merged; } -static int delete_branches (int argc,const char **argv,int force,int kinds) +static int delete_branches (int argc,const char **argv,int force) {struct commit *rev, *head_rev =NULL ;unsigned char sha1[20 ];char *name =NULL ; -const char *fmt, *remote; +const char *fmt, *remote_trans;
Unwarranted change caused by a poor choice of the new global variable above.
builtin/branch.c static int opt_parse_merge_filter(const struct option *opt, const char *arg, int · line 639
Quoted 10 lines
builtin/branch.c +6
-0@@ -610,13 +631,28 @@ static int opt_parse_merge_filter(const struct option *opt, const char *arg, intreturn 0 ; } +static int parse_opt_remote_cb (const struct option *opt,const char *arg, +int unset) +{ + kinds =REF_REMOTE_BRANCH ; +if (unset) + kinds =REF_LOCAL_BRANCH ;
What is this "unset" check about? Wouldn't that be an error if the command line said "--no-remote"?
And you do not return but proceed to look at "arg", presumably to handle a case where the command line said "--no-remote=foobar"?
builtin/branch.c static int opt_parse_merge_filter(const struct option *opt, const char *arg, int · line 641
Quoted 2 lines
builtin/branch.c +2
-0+if (arg) { +if ( *arg =='=' )
(style) Unwanted SP after an open parenthesis.
builtin/branch.c static int opt_parse_merge_filter(const struct option *opt, const char *arg, int · line 642
Quoted 1 line
builtin/branch.c +1
-0+ remote = arg +1 ;/* skip '=' */
(style) It is clear enough what this does without the extra comment.
Does this forbid remote names that begin with a "="? I.e.
$ git branch -r =temporary
As to the design of the new feature, I see you tried to make it possible to perform what
$ git branch -d -r origin/master
does with
$ git branch -d --remote=origin master
I do not think it is particularly a good idea. Adding yet another way to do the same thing, unless that new way is vastly superiour (e.g. easier to use, easier to explain, more efficient to perform, etc.), does not add much value to the system.
It would make much more sense to restrict this feature to the "listing" side of the branches. It would be nice if you can do:
$ git branch -r --match alice --match bob
to show only remote tracking branches under refs/remotes/{alice,bob} and also
$ git branch --match "jk/*"
to show only local topic branches whose names match the given blob.
Am 6/20/2011 0:32, schrieb Junio C Hamano:
Quoted 2 lines
It would make much more sense to restrict this feature to the "listing" side of the branches. It would be nice if you can do:
I agree with everything you said earlier and the first sentence above. And I would have needed the possibility to limit the branch listing to a particular remote a lot in the past. But...
Quoted 8 lines
$ git branch -r --match alice --match bobto show only remote tracking branches under refs/remotes/{alice,bob} and also
$ git branch --match "jk/*"to show only local topic branches whose names match the given blob.
I would hate having to learn a new syntax '--match "jk/*"' when we can already say
$ git log --remotes
$ git log --remotes=alice --remotes=bob
$ git log --remotes="jk/*"IMO, it is the right approach to have a long option --remotes with an optional argument.
-- Hannes
Quoted 7 lines · Johannes Sixt
Quoted 3 lines
$ git branch --match "jk/*"to show only local topic branches whose names match the given blob.
I would hate having to learn a new syntax '--match "jk/*"' when we can already say
Quoted 6 lines
$ git log --remotes $ git log --remotes=alice --remotes=bob $ git log --remotes="jk/*"IMO, it is the right approach to have a long option --remotes with an optional argument.
For that matter, --match should be spelled "--glob", as we already have:
$ git log --glob='jk/*'
I think having the ref-selection for "git branch" match that of the revision walker makes sense.
-Peff
Jeff King venit, vidit, dixit 20.06.2011 09:03:
Quoted 16 lines · Johannes Sixt
Quoted 8 lines
Quoted 4 lines
$ git branch --match "jk/*"to show only local topic branches whose names match the given blob.
I would hate having to learn a new syntax '--match "jk/*"' when we can already say
Quoted 6 lines
$ git log --remotes $ git log --remotes=alice --remotes=bob $ git log --remotes="jk/*"IMO, it is the right approach to have a long option --remotes with an optional argument.
Quoted 8 lines
For that matter, --match should be spelled "--glob", as we already have:
$ git log --glob='jk/*'I think having the ref-selection for "git branch" match that of the revision walker makes sense.
-Peff
Well, "branch" is about refs, and "log" about revs. I'd rather have "branch" similar to "tag" in that respect (i.e. '-l'). I'm still meaning to revive that series:
http://permalink.gmane.org/gmane.comp.version-control.git/172228
I've reworked a few more things compared to the RFC from back then but was caught by lack of time and the rc phase. I'll send what I have asap.
Michael
Quoted 8 lines · Michael J Gruber
Quoted 6 lines
For that matter, --match should be spelled "--glob", as we already have:
$ git log --glob='jk/*'I think having the ref-selection for "git branch" match that of the revision walker makes sense.
Well, "branch" is about refs, and "log" about revs.
Sure, and I wouldn't expect "git branch --list 1234abcd" to do anything useful. But naming refs is a subset of naming revs. Certainly it seems worth it to make the shorthands like "--remotes" behave the same way where applicable.
I do agree that "git branch -l 'jk/*'" is less typing than "--glob"; it may be worth supporting both forms to provide the least surprise to the user (i.e., even though it may not be the shortest, users may expect the same syntax to work in both places, and it costs us very little to accept either).
Quoted 5 lines
I'd rather have
"branch" similar to "tag" in that respect (i.e. '-l'). I'm still meaning to revive that series:http://permalink.gmane.org/gmane.comp.version-control.git/172228
Modulo Junio's comments on the "-l" transition, I like the idea. One thing jumped out at me:
Review
Quoted 4 lines
(quoted diff) +2
-2- else if (argc == 0) - return print_ref_list(kinds, detached, verbose, abbrev, with_commit); + else if (argc == 0 || (verbose && argc == 1)) + return print_ref_list(kinds, detached, verbose, abbrev, with_commit, argc ? argv[0] : NULL);
Is there any reason not to accept:
git branch --list jk/* mg/*
? For "tag -l", we seem to silently ignore any arguments past the first:
$ git tag -l 'v1.7.4.*' 'v1.7.5.*' v1.7.4.1 v1.7.4.2 v1.7.4.3 v1.7.4.4 v1.7.4.5
We should at least warn and say "your second argument is being ignored" or show the usage message. But perhaps it is even friendlier to accept a list of patterns.
-Peff
Quoted 11 lines · Jeff King
? For "tag -l", we seem to silently ignore any arguments past the first:
$ git tag -l 'v1.7.4.*' 'v1.7.5.*' v1.7.4.1 v1.7.4.2 v1.7.4.3 v1.7.4.4 v1.7.4.5We should at least warn and say "your second argument is being ignored" or show the usage message.
I think it is just a bug; no need for "being ignored" warning.
Quoted 2 lines
But perhaps it is even friendlier to accept
a list of patterns.
Yes indeed; please make it so ;-)
PATCH 1/1 tag: accept multiple patterns for --list
Quoted 14 lines · Junio C Hamano
Quoted 11 lines · Jeff King
? For "tag -l", we seem to silently ignore any arguments past the first:
$ git tag -l 'v1.7.4.*' 'v1.7.5.*' v1.7.4.1 v1.7.4.2 v1.7.4.3 v1.7.4.4 v1.7.4.5We should at least warn and say "your second argument is being ignored" or show the usage message.
Quoted 6 lines
I think it is just a bug; no need for "being ignored" warning.
Quoted 2 lines
But perhaps it is even friendlier to accept
a list of patterns.Yes indeed; please make it so ;-)
Here's a patch.
-- >8 --
Subject: [PATCH] tag: accept multiple patterns for --list
Until now, "git tag -l foo* bar*" would silently ignore the second argument, showing only refs starting with "foo". It's not just unfriendly not to take a second pattern; we actually generated subtly wrong results (from the user's perspective) because some of the requested tags were omitted.
This patch allows an arbitrary number of patterns on the command line; if any of them matches, the ref is shown.
While we're tweaking the documentation, let's also make it clear that the pattern is fnmatch.
--- Documentation/git-tag.txt | 9 ++++++--- builtin/tag.c | 26 +++++++++++++++++--------- t/t7004-tag.sh | 5 +++++ 3 files changed, 28 insertions(+), 12 deletions(-)
Documentation/git-tag.txt +6 -3
@@ -12,7 +12,7 @@ SYNOPSIS 'git tag' [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>] <tagname> [<commit> | <object>] 'git tag' -d <tagname>... -'git tag' [-n[<num>]] -l [--contains <commit>] [<pattern>] +'git tag' [-n[<num>]] -l [--contains <commit>] [<pattern>...] 'git tag' -v <tagname>... DESCRIPTION
@@ -69,8 +69,11 @@ OPTIONS If the tag is not annotated, the commit message is displayed instead. -l <pattern>:: - List tags with names that match the given pattern (or all if no pattern is given). - Typing "git tag" without arguments, also lists all tags. + List tags with names that match the given pattern (or all if no + pattern is given). Running "git tag" without arguments also + lists all tags. The pattern is a shell wildcard (i.e., matched + using fnmatch(3)). Multiple patterns may be given; if any of + them matches, the tag is shown. --contains <commit>:: Only list tags which contain the specified commit.
builtin/tag.c +17 -9
@@ -16,7 +16,7 @@static const char *const git_tag_usage[] = {"git tag [-a|-s|-u <key-id>] [-f] [-m <msg>|-F <file>] <tagname> [<head>]" ,"git tag -d <tagname>..." , -"git tag -l [-n[<num>]] [<pattern>]" , +"git tag -l [-n[<num>]] [<pattern>...]" ,"git tag -v <tagname>..." ,NULL };
@@ -24,17 +24,28 @@ static const char * const git_tag_usage[] = {static char signingkey[1000 ];struct tag_filter { -const char *pattern; +const char **patterns;int lines;struct commit_list *with_commit; }; +static int match_pattern (const char **patterns,const char *ref) +{ +/* no pattern means match everything */ +if (!*patterns) +return 1 ; +for (; *patterns; patterns++) +if (!fnmatch (*patterns, ref,0 )) +return 1 ; +return 0 ; +} +static int show_reference (const char *refname,const unsigned char *sha1,int flag,void *cb_data) {struct tag_filter *filter = cb_data; -if (!fnmatch (filter->pattern, refname,0 )) { +if (match_pattern (filter->patterns, refname)) {int i;unsigned long size;enum object_type type;
@@ -88,15 +99,12 @@ static int show_reference(const char *refname, const unsigned char *sha1,return 0 ; } -static int list_tags (const char *pattern,int lines, +static int list_tags (const char **patterns,int lines,struct commit_list *with_commit) {struct tag_filter filter; -if (pattern ==NULL ) - pattern ="*" ; - - filter.pattern = pattern; + filter.patterns = patterns; filter.lines = lines; filter.with_commit = with_commit;
@@ -425,7 +433,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)if (list + delete + verify >1 )usage_with_options (git_tag_usage, options);if (list) -return list_tags (argv[0 ], lines ==-1 ?0 : lines, +return list_tags (argv, lines ==-1 ?0 : lines, with_commit);if (lines !=-1 )die (_ ("-n option is only allowed with -l." ));
t/t7004-tag.sh +5 -0
@@ -257,6 +257,11 @@ test_expect_success \ test_cmp expect actual ' +test_expect_success 'tag -l can accept multiple patterns' ' + git tag -l "v1*" "v0*" >actual && + test_cmp expect actual +' + # creating and verifying lightweight tags: test_expect_success \
Signature · 1 line
1.7.6.rc1.38.g97f64.dirty
Quoted 10 lines · Michael J Gruber
Quoted 8 lines
For that matter, --match should be spelled "--glob", as we already have:
$ git log --glob='jk/*'I think having the ref-selection for "git branch" match that of the revision walker makes sense.
-Peff
Well, "branch" is about refs, and "log" about revs.
Not really. What "log" output by traversing is about revisions, but we are discussing how the command line argument discover the set of refs to feed into the underlying machinery that does different things in each of the command (branch just lists them without any traversal, log shows history leading to the commits). So I do not see any difference and I think it would be appropriate if we can use the same notation to specify both "I want to see all branches that match this pattern" and "I want to see history leading to all refs that match this pattern".
Quoted 2 lines
I'd rather have
"branch" similar to "tag" in that respect (i.e. '-l').
And "tag --glob='*'" also falls into the same class from that point of
view: "I want to see all tags that match this pattern".
Thanks.
Quoted 9 lines · Jeff King
Quoted 7 lines · Johannes Sixt
Quoted 3 lines
$ git branch --match "jk/*"to show only local topic branches whose names match the given blob.
I would hate having to learn a new syntax '--match "jk/*"' when we can already say
Quoted 6 lines
Quoted 6 lines
$ git log --remotes $ git log --remotes=alice --remotes=bob $ git log --remotes="jk/*"IMO, it is the right approach to have a long option --remotes with an optional argument.
Yeah, it was just me being sloppy. I didn't mean to suggest a brand new option name "--match"; I was merely saying that it should be something that specifies filtering. Except for the last one, if you are naming remotes, that also would make sense.
But not the last one at least in the context of the example I gave in my message. I wanted to see local topic branches whose names match jk/* so using --remotes is actively wrong.
Quoted 6 lines
For that matter, --match should be spelled "--glob", as we already have:
$ git log --glob='jk/*'I think having the ref-selection for "git branch" match that of the revision walker makes sense.
Yes, again I was just being sloppy and did not mean to introduce a brand new --match; --glob indeed is a lot more appropriate in the examples I gave in the message.
Here are two other examples of "listing" mode of the branch command:
$ git branch --no-merged --glob="jc/*" pu $ git branch --with jc/some-topic --glob="maint-*" --glob=maint
I sometimes wish if they had such filtering.
Quoted 8 lines · Junio C Hamano
Quoted 3 lines · Boris Faure
add '--remote' as long version for '-r'
update documentation
add tests(style) Sentences begin with a capital letter and ends with a period.
Quoted 7 lines
This commit does a lot more than the above, no? It adds an optional remote name parameter to the existing "-r" option and limits the output to the remote tracking branches of the remote when it is specified.
Quoted 1 line
---
Sign-off?
I missed it.
Quoted 9 lines
Quoted 9 lines
Documentation/git-branch.txt +0
-0@@ -99,8 +99,10 @@ OPTIONSdefault to color output.
Same as--color=never.
Quoted 9 lines
Quoted 6 lines
--r::
- List or delete (if used with -d) the remote-tracking branches.+-r[=<remote>]:: +--remote[=<remote>]::+ List or delete (if used with -d) the remote-tracking branches from
+ <remote> if specified.It is now unspecified what the command would do when the optional <remote> is left unspecified.
My english is not excellent and for sure a better wording can be found.
Quoted 9 lines
Quoted 3 lines
#define
REF_LOCAL_BRANCH0x01
#defineREF_REMOTE_BRANCH0x02
+static int kinds =REF_LOCAL_BRANCH;This used to be nicely scoped out of global space and got passed around as parameter, but now it has become a global? I do not see a good reason for this change.
Quoted 1 line
+static const char *remote = NULL;
I put those variables in global space in order to access them within the parse_opt callback.
Quoted 8 lines
Two issues.
1. Presumably you wanted to have this change because you have too many remotes, way more than two, and wanted to filter the output from remotes that you are not interested in. Is it entirely implausible that you might be interested in not just one, but two remotes out of many remotes you have? A single string variable would not suffice for that but you should be able to make this an array of strings.
I wanted to replace a git branch -r | grep 'someRemoteName' to filter in fact one remote in particular.
Quoted 10 lines
Documentation/git-branch.txt
Quoted 6 lines
Documentation/git-branch.txt +6
-0+static int parse_opt_remote_cb(const struct option *opt, const char *arg, + int unset) +{ + kinds = REF_REMOTE_BRANCH; + if (unset) + kinds = REF_LOCAL_BRANCH;What is this "unset" check about? Wouldn't that be an error if the command line said "--no-remote"?
Quoted 2 lines
And you do not return but proceed to look at "arg", presumably to handle a case where the command line said "--no-remote=foobar"?
It's just a missing return.
Quoted 5 lines
Quoted 2 lines
+ if (arg) {
+ if ( *arg == '=')(style) Unwanted SP after an open parenthesis.
Quoted 7 lines
Quoted 1 line
+ remote = arg + 1; /* skip '=' */
(style) It is clear enough what this does without the extra comment.
Does this forbid remote names that begin with a "="? I.e.
$ git branch -r =temporary
arg is '=temporary' if called with git branch -r=temporary but 'temporary' if called with git branch --remote=temporary. I didn't know to check whether the option triggered was from the long or the short version and skip '=' accordingly.
Quoted 17 lines
As to the design of the new feature, I see you tried to make it possible to perform what
$ git branch -d -r origin/masterdoes with
$ git branch -d --remote=origin masterI do not think it is particularly a good idea. Adding yet another way to do the same thing, unless that new way is vastly superiour (e.g. easier to use, easier to explain, more efficient to perform, etc.), does not add much value to the system.
It would make much more sense to restrict this feature to the "listing" side of the branches. It would be nice if you can do:
Quoted 8 lines
$ git branch -r --match alice --match bobto show only remote tracking branches under refs/remotes/{alice,bob} and also
$ git branch --match "jk/*"to show only local topic branches whose names match the given blob.
I agree that it doesn't make much sense with -d option. I added the feature with '-r=<remote>' so that it works with '-d'.
I would have preferred to just list branches from a given remote 'aa' with 'git branch -r aa' but I'll see where the discussion ends up.
Signature · 1 line
Boris Faure