threads / patch / 40104

v11, 13 partstag.c: implement '--format' option

Subject: [PATCH v11 12/13] tag.c: implement '--format' option

## tl;dr

4 messages between Aug 15, 2015 and Aug 17, 2015. Diffs are folded; open one to read it.

replies: 3people: 3as markdown or json

Karthik Nayak· Aug 15, 2015, 18:00 UTC · lore
From: Karthik Nayak <karthik.188@gmail.com>

Implement the '--format' option provided by 'ref-filter'. This lets the user list tags 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-tag.txt | 15 ++++++++++++++-
 builtin/tag.c             | 11 +++++++----
 t/t7004-tag.sh            | 16 ++++++++++++++++
 3 files changed, 37 insertions(+), 5 deletions(-)
Show changes to 3 files +37 −5

Documentation/git-tag.txt, builtin/tag.c, t/t7004-tag.sh

diff --git a/Documentation/git-tag.txt b/Documentation/git-tag.txt
index 3ac4a96..75703c5 100644
--- a/Documentation/git-tag.txt
+++ b/Documentation/git-tag.txt
@@ -13,7 +13,8 @@ SYNOPSIS
 	<tagname> [<commit> | <object>]
 'git tag' -d <tagname>...
 'git tag' [-n[<num>]] -l [--contains <commit>] [--points-at <object>]
-	[--column[=<options>] | --no-column] [--create-reflog] [--sort=<key>] [<pattern>...]
+	[--column[=<options>] | --no-column] [--create-reflog] [--sort=<key>]
+	[--format=<format>] [<pattern>...]
 'git tag' -v <tagname>...
 
 DESCRIPTION
@@ -158,6 +159,18 @@ This option is only applicable when listing tags without annotation lines.
 	The object that the new tag will refer to, usually a commit.
 	Defaults to HEAD.
 
+<format>::
+	A string that interpolates `%(fieldname)` from the object
+	pointed at by a ref being shown.  If `fieldname` is prefixed
+	with an asterisk (`*`) and the ref points at a tag object, the
+	value for the field in the object tag refers is used.  When
+	unspecified, defaults to `%(refname:short)`.  It also
+	interpolates `%%` to `%`, and `%xx` where `xx` are hex digits
+	interpolates to character with hex code `xx`; for example
+	`%00` interpolates to `\0` (NUL), `%09` to `\t` (TAB) and
+	`%0a` to `\n` (LF).  The fields are same as those in `git
+	for-each-ref`.
+
 
 CONFIGURATION
 -------------
diff --git a/builtin/tag.c b/builtin/tag.c
index 501fc52..69997a4 100644
--- a/builtin/tag.c
+++ b/builtin/tag.c
@@ -30,10 +30,9 @@ static const char * const git_tag_usage[] = {
 
 static unsigned int colopts;
 
-static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting)
+static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting, const char *format)
 {
 	struct ref_array array;
-	char *format;
 	int i;
 
 	memset(&array, 0, sizeof(array));
@@ -43,7 +42,7 @@ static int list_tags(struct ref_filter *filter, struct ref_sorting *sorting)
 
 	if (filter->lines)
 		format = "%(align:16,left)%(refname:short)%(end)";
-	else
+	else if (!format)
 		format = "%(refname:short)";
 
 	verify_ref_format(format);
@@ -327,6 +326,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 	struct strbuf err = STRBUF_INIT;
 	struct ref_filter filter;
 	static struct ref_sorting *sorting = NULL, **sorting_tail = &sorting;
+	const char *format = NULL;
 	struct option options[] = {
 		OPT_CMDMODE('l', "list", &cmdmode, N_("list tag names"), 'l'),
 		{ OPTION_INTEGER, 'n', NULL, &filter.lines, N_("n"),
@@ -359,6 +359,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 			OPTION_CALLBACK, 0, "points-at", &filter.points_at, N_("object"),
 			N_("print only tags of the object"), 0, parse_opt_object_name
 		},
+		OPT_STRING(  0 , "format", &format, N_("format"), N_("format to use for the output")),
 		OPT_END()
 	};
 
@@ -398,8 +399,10 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
 			copts.padding = 2;
 			run_column_filter(colopts, &copts);
 		}
+		if (format && (filter.lines != -1))
+			die(_("--format and -n are incompatible"));
 		filter.name_patterns = argv;
-		ret = list_tags(&filter, sorting);
+		ret = list_tags(&filter, sorting, format);
 		if (column_active(colopts))
 			stop_column_filter();
 		return ret;
diff --git a/t/t7004-tag.sh b/t/t7004-tag.sh
index 1f066aa..1809011 100755
--- a/t/t7004-tag.sh
+++ b/t/t7004-tag.sh
@@ -1519,4 +1519,20 @@ EOF"
 	test_cmp expect actual
 '
 
+test_expect_success '--format cannot be used with -n' '
+	test_must_fail git tag -l -n4 --format="%(refname)"
+'
+
+test_expect_success '--format should list tags as per format given' '
+	cat >expect <<-\EOF &&
+	refname : refs/tags/foo1.10
+	refname : refs/tags/foo1.3
+	refname : refs/tags/foo1.6
+	refname : refs/tags/foo1.6-rc1
+	refname : refs/tags/foo1.6-rc2
+	EOF
+	git tag -l --format="refname : %(refname)" "foo*" >actual &&
+	test_cmp expect actual
+'
+
 test_done
-- 
2.5.0
Junio C Hamano· Aug 17, 2015, 19:04 UTC · re: Karthik Nayak · lore

Re: [PATCH v11 12/13] tag.c: implement '--format' option

Karthik Nayak <karthik.188@gmail.com> writes:
Show 7 quoted lines
> From: Karthik Nayak <karthik.188@gmail.com>
>
> Implement the '--format' option provided by 'ref-filter'.
> This lets the user list tags as per desired format similar
> to the implementation in 'git for-each-ref'.
>
> Add tests and documentation for the same.

Hmm, do we want "--format" added to "tag -l" and "branch -l" in the first place? Scriptors should be using "for-each-ref" plumbing in the first place, and the point of unifying these three is to share filtering features among them, which would make "for-each-ref" able to express what the other two can do. I'd hesitate to add too much flexibility to "branch -l" and "tag -l" Porcelains to entice people to script around them.

Karthik Nayak· Aug 17, 2015, 19:14 UTC · re: Junio C Hamano · lore

Re: [PATCH v11 12/13] tag.c: implement '--format' option

On Tue, Aug 18, 2015 at 12:34 AM, Junio C Hamano <gitster@pobox.com> wrote:
Show 18 quoted lines
> Karthik Nayak <karthik.188@gmail.com> writes:
>
>> From: Karthik Nayak <karthik.188@gmail.com>
>>
>> Implement the '--format' option provided by 'ref-filter'.
>> This lets the user list tags as per desired format similar
>> to the implementation in 'git for-each-ref'.
>>
>> Add tests and documentation for the same.
>
> Hmm, do we want "--format" added to "tag -l" and "branch -l" in the
> first place?  Scriptors should be using "for-each-ref" plumbing in
> the first place, and the point of unifying these three is to share
> filtering features among them, which would make "for-each-ref" able
> to express what the other two can do.  I'd hesitate to add too much
> flexibility to "branch -l" and "tag -l" Porcelains to entice people
> to script around them.
>

I'll leave that decision to you, but I see it as a good feature, when perhaps I just want to list tags with authors. Agreed `for-each-ref` can handle this too but I don't see why `tag -l` shouldn't.

-- 
Regards,
Karthik Nayak
Jacob Keller· Aug 17, 2015, 21:38 UTC · re: Karthik Nayak · lore

Re: [PATCH v11 12/13] tag.c: implement '--format' option

On Mon, Aug 17, 2015 at 12:14 PM, Karthik Nayak <karthik.188@gmail.com> wrote:
Show 27 quoted lines
> On Tue, Aug 18, 2015 at 12:34 AM, Junio C Hamano <gitster@pobox.com> wrote:
>> Karthik Nayak <karthik.188@gmail.com> writes:
>>
>>> From: Karthik Nayak <karthik.188@gmail.com>
>>>
>>> Implement the '--format' option provided by 'ref-filter'.
>>> This lets the user list tags as per desired format similar
>>> to the implementation in 'git for-each-ref'.
>>>
>>> Add tests and documentation for the same.
>>
>> Hmm, do we want "--format" added to "tag -l" and "branch -l" in the
>> first place?  Scriptors should be using "for-each-ref" plumbing in
>> the first place, and the point of unifying these three is to share
>> filtering features among them, which would make "for-each-ref" able
>> to express what the other two can do.  I'd hesitate to add too much
>> flexibility to "branch -l" and "tag -l" Porcelains to entice people
>> to script around them.
>>
>
> I'll leave that decision to you, but I see it as a good feature, when perhaps
> I just want to list tags with authors. Agreed `for-each-ref` can handle this too
> but I don't see why `tag -l` shouldn't.
>
> --
> Regards,
> Karthik Nayak

I agree with Karthik,it doesn't really hurt to add it to tag, and will allow users who aren't familiar with for-each-ref to be able to get the --format for some use cases. I think it would increase visibility and use of the format option if it's available on tag and branch.

Regards, Jake

← back to recent threads