git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Karthik Nayak <karthik.188@gmail.com>
To: git@vger.kernel.org
Cc: jacob.keller@gmail.com, gitster@pobox.com,
	Karthik Nayak <karthik.188@gmail.com>
Subject: [PATCH v10 20/20] branch: implement '--format' option
Date: Tue, 10 Jan 2017 14:19:53 +0530	[thread overview]
Message-ID: <20170110084953.15890-21-Karthik.188@gmail.com> (raw)
In-Reply-To: <20170110084953.15890-1-Karthik.188@gmail.com>

From: Karthik Nayak <karthik.188@gmail.com>

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>
---
 Documentation/git-branch.txt |  7 ++++++-
 builtin/branch.c             | 14 +++++++++-----
 t/t3203-branch-output.sh     | 14 ++++++++++++++
 3 files changed, 29 insertions(+), 6 deletions(-)

diff --git a/Documentation/git-branch.txt b/Documentation/git-branch.txt
index 5516a47b5..1fae4eeee 100644
--- a/Documentation/git-branch.txt
+++ b/Documentation/git-branch.txt
@@ -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>]
@@ -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
 --------
 
diff --git a/builtin/branch.c b/builtin/branch.c
index f293ee5b0..cbaa6d03c 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -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
 };
 
@@ -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,
@@ -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);
@@ -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)
@@ -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")),
@@ -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(),
 	};
 
@@ -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;
diff --git a/t/t3203-branch-output.sh b/t/t3203-branch-output.sh
index 45213280a..5778c0afe 100755
--- a/t/t3203-branch-output.sh
+++ b/t/t3203-branch-output.sh
@@ -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
-- 
2.11.0


  parent reply	other threads:[~2017-01-10  8:49 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-10  8:49 [PATCH v10 00/20] port branch.c to use ref-filter's printing options Karthik Nayak
2017-01-10  8:49 ` [PATCH v10 01/20] ref-filter: implement %(if), %(then), and %(else) atoms Karthik Nayak
2017-01-10  8:49 ` [PATCH v10 02/20] ref-filter: include reference to 'used_atom' within 'atom_value' Karthik Nayak
2017-01-10  8:49 ` [PATCH v10 03/20] ref-filter: implement %(if:equals=<string>) and %(if:notequals=<string>) Karthik Nayak
2017-01-10 20:45   ` Junio C Hamano
2017-01-14 10:02     ` Karthik Nayak
2017-01-10  8:49 ` [PATCH v10 04/20] ref-filter: modify "%(objectname:short)" to take length Karthik Nayak
2017-01-10  8:49 ` [PATCH v10 05/20] ref-filter: move get_head_description() from branch.c Karthik Nayak
2017-01-10  8:49 ` [PATCH v10 06/20] ref-filter: introduce format_ref_array_item() Karthik Nayak
2017-01-10  8:49 ` [PATCH v10 07/20] ref-filter: make %(upstream:track) prints "[gone]" for invalid upstreams Karthik Nayak
2017-01-10  8:49 ` [PATCH v10 08/20] ref-filter: add support for %(upstream:track,nobracket) Karthik Nayak
2017-01-10  8:49 ` [PATCH v10 09/20] ref-filter: make "%(symref)" atom work with the ':short' modifier Karthik Nayak
2017-01-10  8:49 ` [PATCH v10 10/20] ref-filter: introduce refname_atom_parser_internal() Karthik Nayak
2017-01-10  8:49 ` [PATCH v10 11/20] ref-filter: introduce refname_atom_parser() Karthik Nayak
2017-01-10  8:49 ` [PATCH v10 12/20] ref-filter: make remote_ref_atom_parser() use refname_atom_parser_internal() Karthik Nayak
2017-01-10  8:49 ` [PATCH v10 13/20] ref-filter: rename the 'strip' option to 'lstrip' Karthik Nayak
2017-01-10  8:49 ` [PATCH v10 14/20] ref-filter: Do not abruptly die when using the 'lstrip=<N>' option Karthik Nayak
2017-01-10  8:49 ` [PATCH v10 15/20] ref-filter: modify the 'lstrip=<N>' option to work with negative '<N>' Karthik Nayak
2017-01-10  8:49 ` [PATCH v10 16/20] ref-filter: add an 'rstrip=<N>' option to atoms which deal with refnames Karthik Nayak
2017-01-10  8:49 ` [PATCH v10 17/20] ref-filter: allow porcelain to translate messages in the output Karthik Nayak
2017-01-10  8:49 ` [PATCH v10 18/20] branch, tag: use porcelain output Karthik Nayak
2017-01-10  8:49 ` [PATCH v10 19/20] branch: use ref-filter printing APIs Karthik Nayak
2017-01-11 23:47   ` Jacob Keller
2017-01-14 10:01     ` Karthik Nayak
2017-01-15  5:51       ` Jacob Keller
2017-01-10  8:49 ` Karthik Nayak [this message]
2017-01-10 20:51 ` [PATCH v10 00/20] port branch.c to use ref-filter's printing options Junio C Hamano
2017-01-14 10:03   ` Karthik Nayak

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170110084953.15890-21-Karthik.188@gmail.com \
    --to=karthik.188@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    --cc=jacob.keller@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).