Skip to content

Commit 53c2525

Browse files
KarthikNayakgitster
authored andcommitted
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 <[email protected]> Mentored-by: Matthieu Moy <[email protected]> Signed-off-by: Karthik Nayak <[email protected]>
1 parent c56529d commit 53c2525

File tree

3 files changed

+29
-6
lines changed

3 files changed

+29
-6
lines changed

Documentation/git-branch.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ SYNOPSIS
1212
[--list] [-v [--abbrev=<length> | --no-abbrev]]
1313
[--column[=<options>] | --no-column]
1414
[(--merged | --no-merged | --contains) [<commit>]] [--sort=<key>]
15-
[--points-at <object>] [<pattern>...]
15+
[--points-at <object>] [--format=<format>] [<pattern>...]
1616
'git branch' [--set-upstream | --track | --no-track] [-l] [-f] <branchname> [<start-point>]
1717
'git branch' (--set-upstream-to=<upstream> | -u <upstream>) [<branchname>]
1818
'git branch' --unset-upstream [<branchname>]
@@ -246,6 +246,11 @@ start-point is either a local or remote-tracking branch.
246246
--points-at <object>::
247247
Only list branches of the given object.
248248

249+
--format <format>::
250+
A string that interpolates `%(fieldname)` from the object
251+
pointed at by a ref being shown. The format is the same as
252+
that of linkgit:git-for-each-ref[1].
253+
249254
Examples
250255
--------
251256

builtin/branch.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ static const char * const builtin_branch_usage[] = {
2828
N_("git branch [<options>] [-r] (-d | -D) <branch-name>..."),
2929
N_("git branch [<options>] (-m | -M) [<old-branch>] <new-branch>"),
3030
N_("git branch [<options>] [-r | -a] [--points-at]"),
31+
N_("git branch [<options>] [-r | -a] [--format]"),
3132
NULL
3233
};
3334

@@ -340,14 +341,14 @@ static char *build_format(struct ref_filter *filter, int maxwidth, const char *r
340341
return strbuf_detach(&fmt, NULL);
341342
}
342343

343-
static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sorting)
344+
static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sorting, const char *format)
344345
{
345346
int i;
346347
struct ref_array array;
347348
int maxwidth = 0;
348349
const char *remote_prefix = "";
349350
struct strbuf out = STRBUF_INIT;
350-
char *format;
351+
char *to_free = NULL;
351352

352353
/*
353354
* If we are listing more than just remote branches,
@@ -364,7 +365,8 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin
364365
if (filter->verbose)
365366
maxwidth = calc_maxwidth(&array, strlen(remote_prefix));
366367

367-
format = build_format(filter, maxwidth, remote_prefix);
368+
if (!format)
369+
format = to_free = build_format(filter, maxwidth, remote_prefix);
368370
verify_ref_format(format);
369371

370372
/*
@@ -392,7 +394,7 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin
392394
}
393395

394396
ref_array_clear(&array);
395-
free(format);
397+
free(to_free);
396398
}
397399

398400
static void rename_branch(const char *oldname, const char *newname, int force)
@@ -491,6 +493,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
491493
enum branch_track track;
492494
struct ref_filter filter;
493495
static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
496+
const char *format = NULL;
494497

495498
struct option options[] = {
496499
OPT_GROUP(N_("Generic options")),
@@ -531,6 +534,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
531534
OPTION_CALLBACK, 0, "points-at", &filter.points_at, N_("object"),
532535
N_("print only branches of the object"), 0, parse_opt_object_name
533536
},
537+
OPT_STRING( 0 , "format", &format, N_("format"), N_("format to use for the output")),
534538
OPT_END(),
535539
};
536540

@@ -591,7 +595,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
591595
if ((filter.kind & FILTER_REFS_BRANCHES) && filter.detached)
592596
filter.kind |= FILTER_REFS_DETACHED_HEAD;
593597
filter.name_patterns = argv;
594-
print_ref_list(&filter, sorting);
598+
print_ref_list(&filter, sorting, format);
595599
print_columns(&output, colopts, NULL);
596600
string_list_clear(&output, 0);
597601
return 0;

t/t3203-branch-output.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,18 @@ test_expect_success 'local-branch symrefs shortened properly' '
196196
test_cmp expect actual
197197
'
198198

199+
test_expect_success 'git branch --format option' '
200+
cat >expect <<-\EOF &&
201+
Refname is (HEAD detached from fromtag)
202+
Refname is refs/heads/ambiguous
203+
Refname is refs/heads/branch-one
204+
Refname is refs/heads/branch-two
205+
Refname is refs/heads/master
206+
Refname is refs/heads/ref-to-branch
207+
Refname is refs/heads/ref-to-remote
208+
EOF
209+
git branch --format="Refname is %(refname)" >actual &&
210+
test_cmp expect actual
211+
'
212+
199213
test_done

0 commit comments

Comments
 (0)