Skip to content

Commit

Permalink
branch: implement '--format' option
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
KarthikNayak authored and gitster committed Jan 31, 2017
1 parent 949af06 commit 3d9e4ce
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
7 changes: 6 additions & 1 deletion Documentation/git-branch.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ SYNOPSIS
[--list] [-v [--abbrev=<length> | --no-abbrev]]
[--column[=<options>] | --no-column]
[(--merged | --no-merged | --contains) [<commit>]] [--sort=<key>]
[--points-at <object>] [<pattern>...]
[--points-at <object>] [--format=<format>] [<pattern>...]
'git branch' [--set-upstream | --track | --no-track] [-l] [-f] <branchname> [<start-point>]
'git branch' (--set-upstream-to=<upstream> | -u <upstream>) [<branchname>]
'git branch' --unset-upstream [<branchname>]
Expand Down Expand Up @@ -250,6 +250,11 @@ start-point is either a local or remote-tracking branch.
--points-at <object>::
Only list branches of the given object.

--format <format>::
A string that interpolates `%(fieldname)` from the object
pointed at by a ref being shown. The format is the same as
that of linkgit:git-for-each-ref[1].

Examples
--------

Expand Down
14 changes: 9 additions & 5 deletions builtin/branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ static const char * const builtin_branch_usage[] = {
N_("git branch [<options>] [-r] (-d | -D) <branch-name>..."),
N_("git branch [<options>] (-m | -M) [<old-branch>] <new-branch>"),
N_("git branch [<options>] [-r | -a] [--points-at]"),
N_("git branch [<options>] [-r | -a] [--format]"),
NULL
};

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

static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sorting)
static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sorting, const char *format)
{
int i;
struct ref_array array;
int maxwidth = 0;
const char *remote_prefix = "";
struct strbuf out = STRBUF_INIT;
char *format;
char *to_free = NULL;

/*
* If we are listing more than just remote branches,
Expand All @@ -388,7 +389,8 @@ static void print_ref_list(struct ref_filter *filter, struct ref_sorting *sortin
if (filter->verbose)
maxwidth = calc_maxwidth(&array, strlen(remote_prefix));

format = build_format(filter, maxwidth, remote_prefix);
if (!format)
format = to_free = build_format(filter, maxwidth, remote_prefix);
verify_ref_format(format);

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

ref_array_clear(&array);
free(format);
free(to_free);
}

static void reject_rebase_or_bisect_branch(const char *target)
Expand Down Expand Up @@ -528,6 +530,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
struct ref_filter filter;
int icase = 0;
static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
const char *format = NULL;

struct option options[] = {
OPT_GROUP(N_("Generic options")),
Expand Down Expand Up @@ -569,6 +572,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
N_("print only branches of the object"), 0, parse_opt_object_name
},
OPT_BOOL('i', "ignore-case", &icase, N_("sorting and filtering are case insensitive")),
OPT_STRING( 0 , "format", &format, N_("format"), N_("format to use for the output")),
OPT_END(),
};

Expand Down Expand Up @@ -641,7 +645,7 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
if (!sorting)
sorting = ref_default_sorting();
sorting->ignore_case = icase;
print_ref_list(&filter, sorting);
print_ref_list(&filter, sorting, format);
print_columns(&output, colopts, NULL);
string_list_clear(&output, 0);
return 0;
Expand Down
14 changes: 14 additions & 0 deletions t/t3203-branch-output.sh
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,18 @@ test_expect_success 'sort branches, ignore case' '
)
'

test_expect_success 'git branch --format option' '
cat >expect <<-\EOF &&
Refname is (HEAD detached from fromtag)
Refname is refs/heads/ambiguous
Refname is refs/heads/branch-one
Refname is refs/heads/branch-two
Refname is refs/heads/master
Refname is refs/heads/ref-to-branch
Refname is refs/heads/ref-to-remote
EOF
git branch --format="Refname is %(refname)" >actual &&
test_cmp expect actual
'

test_done

0 comments on commit 3d9e4ce

Please sign in to comment.