Skip to content

Commit 3d9e4ce

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]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 949af06 commit 3d9e4ce

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>]
@@ -250,6 +250,11 @@ start-point is either a local or remote-tracking branch.
250250
--points-at <object>::
251251
Only list branches of the given object.
252252

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+
253258
Examples
254259
--------
255260

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

@@ -364,14 +365,14 @@ static char *build_format(struct ref_filter *filter, int maxwidth, const char *r
364365
return strbuf_detach(&fmt, NULL);
365366
}
366367

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)
368369
{
369370
int i;
370371
struct ref_array array;
371372
int maxwidth = 0;
372373
const char *remote_prefix = "";
373374
struct strbuf out = STRBUF_INIT;
374-
char *format;
375+
char *to_free = NULL;
375376

376377
/*
377378
* 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
388389
if (filter->verbose)
389390
maxwidth = calc_maxwidth(&array, strlen(remote_prefix));
390391

391-
format = build_format(filter, maxwidth, remote_prefix);
392+
if (!format)
393+
format = to_free = build_format(filter, maxwidth, remote_prefix);
392394
verify_ref_format(format);
393395

394396
ref_array_sort(sorting, &array);
@@ -407,7 +409,7 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin
407409
}
408410

409411
ref_array_clear(&array);
410-
free(format);
412+
free(to_free);
411413
}
412414

413415
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)
528530
struct ref_filter filter;
529531
int icase = 0;
530532
static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
533+
const char *format = NULL;
531534

532535
struct option options[] = {
533536
OPT_GROUP(N_("Generic options")),
@@ -569,6 +572,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
569572
N_("print only branches of the object"), 0, parse_opt_object_name
570573
},
571574
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")),
572576
OPT_END(),
573577
};
574578

@@ -641,7 +645,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
641645
if (!sorting)
642646
sorting = ref_default_sorting();
643647
sorting->ignore_case = icase;
644-
print_ref_list(&filter, sorting);
648+
print_ref_list(&filter, sorting, format);
645649
print_columns(&output, colopts, NULL);
646650
string_list_clear(&output, 0);
647651
return 0;

t/t3203-branch-output.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,18 @@ test_expect_success 'sort branches, ignore case' '
225225
)
226226
'
227227

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+
228242
test_done

0 commit comments

Comments
 (0)