branch: implement '--format' option

Implement the '--format' option provided by 'ref-filter'. This lets the user list branches as per desired format similar to the implementation in 'git for-each-ref'. Add tests and documentation for the same. Mentored-by: Christian Couder <christian.couder@gmail.com> Mentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr> Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karthik Nayak committed Jan 10, 2017 at 14:19 UTC 3d9e4ce3ebef4f5aa47dad49f730e085f32b98da
3 files changed +29 -6
Documentation/git-branch.txt
+6 -1
@@ -12,7 +12,7 @@ SYNOPSIS
12 [--list] [-v [--abbrev=<length> | --no-abbrev]]
13 [--column[=<options>] | --no-column]
14 [(--merged | --no-merged | --contains) [<commit>]] [--sort=<key>]
15 - [--points-at <object>] [<pattern>...]
15 + [--points-at <object>] [--format=<format>] [<pattern>...]
16 'git branch' [--set-upstream | --track | --no-track] [-l] [-f] <branchname> [<start-point>]
17 'git branch' (--set-upstream-to=<upstream> | -u <upstream>) [<branchname>]
18 'git branch' --unset-upstream [<branchname>]
@@ -250,6 +250,11 @@ start-point is either a local or remote-tracking branch.
250 --points-at <object>::
251 Only list branches of the given object.
252
253 +--format <format>::
254 + A string that interpolates `%(fieldname)` from the object
255 + pointed at by a ref being shown. The format is the same as
256 + that of linkgit:git-for-each-ref[1].
257 +
258 Examples
259 --------
260
builtin/branch.c
+9 -5
@@ -28,6 +28,7 @@ static const char * const builtin_branch_usage[] = {
28 N_("git branch [<options>] [-r] (-d | -D) <branch-name>..."),
29 N_("git branch [<options>] (-m | -M) [<old-branch>] <new-branch>"),
30 N_("git branch [<options>] [-r | -a] [--points-at]"),
31 + N_("git branch [<options>] [-r | -a] [--format]"),
32 NULL
33 };
34
@@ -364,14 +365,14 @@ static char *build_format(struct ref_filter *filter, int maxwidth, const char *r
365 return strbuf_detach(&fmt, NULL);
366 }
367
367 -static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sorting)
368 +static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sorting, const char *format)
369 {
370 int i;
371 struct ref_array array;
372 int maxwidth = 0;
373 const char *remote_prefix = "";
374 struct strbuf out = STRBUF_INIT;
374 - char *format;
375 + char *to_free = NULL;
376
377 /*
378 * If we are listing more than just remote branches,
@@ -388,7 +389,8 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin
389 if (filter->verbose)
390 maxwidth = calc_maxwidth(&array, strlen(remote_prefix));
391
391 - format = build_format(filter, maxwidth, remote_prefix);
392 + if (!format)
393 + format = to_free = build_format(filter, maxwidth, remote_prefix);
394 verify_ref_format(format);
395
396 ref_array_sort(sorting, &array);
@@ -407,7 +409,7 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin
409 }
410
411 ref_array_clear(&array);
410 - free(format);
412 + free(to_free);
413 }
414
415 static void reject_rebase_or_bisect_branch(const char *target)
@@ -528,6 +530,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
530 struct ref_filter filter;
531 int icase = 0;
532 static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
533 + const char *format = NULL;
534
535 struct option options[] = {
536 OPT_GROUP(N_("Generic options")),
@@ -569,6 +572,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
572 N_("print only branches of the object"), 0, parse_opt_object_name
573 },
574 OPT_BOOL('i', "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
575 + OPT_STRING( 0 , "format", &format, N_("format"), N_("format to use for the output")),
576 OPT_END(),
577 };
578
@@ -641,7 +645,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
645 if (!sorting)
646 sorting = ref_default_sorting();
647 sorting->ignore_case = icase;
644 - print_ref_list(&filter, sorting);
648 + print_ref_list(&filter, sorting, format);
649 print_columns(&output, colopts, NULL);
650 string_list_clear(&output, 0);
651 return 0;
t/t3203-branch-output.sh
+14
@@ -225,4 +225,18 @@ test_expect_success 'sort branches, ignore case' '
225 )
226 '
227
228 +test_expect_success 'git branch --format option' '
229 + cat >expect <<-\EOF &&
230 + Refname is (HEAD detached from fromtag)
231 + Refname is refs/heads/ambiguous
232 + Refname is refs/heads/branch-one
233 + Refname is refs/heads/branch-two
234 + Refname is refs/heads/master
235 + Refname is refs/heads/ref-to-branch
236 + Refname is refs/heads/ref-to-remote
237 + EOF
238 + git branch --format="Refname is %(refname)" >actual &&
239 + test_cmp expect actual
240 +'
241 +
242 test_done