RFC/POC: patterns for branch list
cover RFC/POC: patterns for branch list
This mini series is about introducing patterns to the list mode of 'git branch' much like the pattern for 'git tag -l'. There are several things which I would like to do but would like to rfc first:
'-l' is the natural short option name for '--list'. This is taken for the rarely used or needed 'create reflog' option. I'd change the latter to '-g,--create-reflog' (cmp. log) and take '-l,--list' but know the reaction already.
'-v' and '-vv' both take considerable time (because they need to walk). It makes more sense to have '-v' display cheap output (upstream name) and '-vv' add expensive output (ahead/behind info). '-vvv' could add super expensive info (ahead/equivalent/behind a la cherry-mark).
But all of the above are changes to current (ui) behaviour. In the two patches below, only the automatic activation of the list mode with -v/-vv is a change of (undocumented) behaviour.
And yes, git-for-each-ref can do all this and more, but that is plumbing.
Michael J Gruber (2):
branch: allow pattern arguments
branch: introduce --list argument
builtin/branch.c | 21 +++++++++++++++------ 1 files changed, 15 insertions(+), 6 deletions(-)
Signature · 1 line
1.7.5.270.gafca7
PATCH 1/2 branch: allow pattern arguments
Allow pattern arguments for the list mode just like for git tag -l.
--- builtin/branch.c | 13 +++++++++---- 1 files changed, 9 insertions(+), 4 deletions(-)
builtin/branch.c +9 -4
@@ -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 *pattern;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->pattern &&fnmatch (cb->pattern, 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 );
@@ -494,7 +498,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 *pattern) {int i;struct append_ref_cb cb;
@@ -508,6 +512,7 @@ static int print_ref_list(int kinds, int detached, int verbose, int abbrev, struif (merge_filter !=NO_FILTER )init_revisions (&ref_list.revs,NULL ); cb.ref_list = &ref_list; + cb.pattern = pattern; cb.ret =0 ;for_each_rawref (append_ref, &cb);if (merge_filter !=NO_FILTER ) {
@@ -525,7 +530,7 @@ static int print_ref_list(int kinds, int detached, int verbose, int abbrev, struqsort (ref_list.list, ref_list.index,sizeof (struct ref_item ), ref_cmp); detached = (detached && (kinds &REF_LOCAL_BRANCH )); -if (detached) +if (detached && (!pattern || !fnmatch (pattern,"HEAD" ,0 )))show_detached (&ref_list);for (i =0 ; i < ref_list.index; i++) {
@@ -700,8 +705,8 @@ 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); +else if (argc ==0 || (verbose && argc ==1 )) +return print_ref_list (kinds, detached, verbose, abbrev, with_commit, argc ? argv[0 ] :NULL );else if (rename && (argc ==1 ))rename_branch (head, argv[0 ], rename >1 );else if (rename && (argc ==2 ))
Signature · 1 line
1.7.5.270.gafca7
Quoted 2 lines · Michael J Gruber
This mini series is about introducing patterns to the list mode of 'git branch' much like the pattern for 'git tag -l'.
I do find myself often typing
git branch | grep mg/
so this would be a welcome addition, even though the above is not too ugly nor cumbersome.
Quoted 4 lines
'-l' is the natural short option name for '--list'. This is taken for the rarely used or needed 'create reflog' option. I'd change the latter to '-g,--create-reflog' (cmp. log) and take '-l,--list' but know the reaction already.
My reaction would be "As the eventual goal, it is the right thing to do---if we were writing git from scratch today, we would probably have done so. Present a reasonable migration plan.". Perhaps like this?
1. Introduce "--create-reflog", and deprecate "-l". Make "branch -l" emit warning and advise to use "--create-reflog" instead, but still act as a synonym for "--create-reflog".
Introduce "--list" but without any short option.
Ship 1.7.6 with these changes.
2. Introduce "-g" as synonym for "--create-reflog". Make "branch -l" die and advise to use "-g" or "--create-reflog" instead.
Ship 1.7.X series after 1.7.7 with these changes.
3. In 1.8.0, make "-l" a synonym for "--list".
Quoted 4 lines
'-v' and '-vv' both take considerable time (because they need to walk). It makes more sense to have '-v' display cheap output (upstream name) and '-vv' add expensive output (ahead/behind info). '-vvv' could add super expensive info (ahead/equivalent/behind a la cherry-mark).
Probably. Would it be a solution to deprecate "git branch -v" and make the behaviour of "git branch -l -v{1,3}" as you described?
PATCH 2/2 branch: introduce --list argument
In reply to Michael J GruberCurrently, there is no way to invoke the list mode with a pattern because this is interpreted as branch creation.
Introduce a --list argument which invokes the list mode.
--- builtin/branch.c | 10 +++++++--- 1 files changed, 7 insertions(+), 3 deletions(-)
builtin/branch.c +7 -3
@@ -619,7 +619,7 @@ static int opt_parse_merge_filter(const struct option *opt, const char *arg, intint cmd_branch (int argc,const char **argv,const char *prefix) { -int delete =0 , rename =0 , force_create =0 ; +int delete =0 , rename =0 , force_create =0 , list =0 ;int verbose =0 , abbrev =DEFAULT_ABBREV , detached =0 ;int reflog =0 ;enum branch_track track;
@@ -658,6 +658,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)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 ),OPT_BIT ('M' ,NULL , &rename,"move/rename a branch, even if target exists" ,2 ), +OPT_BOOLEAN (0 ,"list" , &list,"list branch names" ),OPT_BOOLEAN ('l' ,NULL , &reflog,"create the branch's reflog" ), OPT__FORCE(&force_create, "force creation (when already exists)"), {
@@ -700,12 +701,15 @@ int cmd_branch(int argc, const char **argv, const char *prefix) argc =parse_options (argc, argv, prefix, options, builtin_branch_usage,0 ); -if (!!delete + !!rename + !!force_create >1 ) +if (!!delete + !!rename + !!force_create + !!list >1 )usage_with_options (builtin_branch_usage, options); +if (argc ==0 || (verbose && argc ==1 )) + list =1 ; +if (delete)return delete_branches (argc, argv, delete >1 , kinds); -else if (argc ==0 || (verbose && argc ==1 )) +else if (list)return print_ref_list (kinds, detached, verbose, abbrev, with_commit, argc ? argv[0 ] :NULL );else if (rename && (argc ==1 ))rename_branch (head, argv[0 ], rename >1 );
Signature · 1 line
1.7.5.270.gafca7