Skip to contentSkip to conversation

pretty: add %r format specifier for showing refs

PATCH Superseded Andy Koppe 3 participants 5 messages Jul 12, 2023 Documentation

lore
Share & tools
lore

Andy Koppe

author
#0 lore raw

PATCH 1/1 pretty: add %r format specifier for showing refs

This lists refs similarly to the %D decoration format, but separates the refs with spaces only and omits "tag:" annotations. It's intended primarily for color output, where tags are already distinguished by color.

Refactor format_decorations() to take an enum decoration_format argument that determines the prefix, separator and suffix as well as the tag annotation.

For %d and %D, wrap the "tag:" annotation and the actual tag in separate color controls, because otherwise the tag ends up uncolored when %w width formatting breaks a line between the annotation and tag.

Amend t4207-log-decoration-colors.sh to reflect the added color controls, and t4202-log.sh to test the %r format.

---
 Documentation/pretty-formats.txt |  3 +-
 log-tree.c                       | 52 +++++++++++++++++++++-----------
 log-tree.h                       | 15 ++++-----
 pretty.c                         |  7 +++--
 t/t4202-log.sh                   |  7 ++++-
 t/t4207-log-decoration-colors.sh | 22 +++++++-------
 6 files changed, 67 insertions(+), 39 deletions(-)
Documentation/pretty-formats.txt +2 -1
index 3b71334459..d2ae898c79 100644
@@ -221,7 +221,8 @@ The placeholders are:
 '%ch':: committer date, human style (like the `--date=human` option of
 	linkgit:git-rev-list[1])
 '%d':: ref names, like the --decorate option of linkgit:git-log[1]
-'%D':: ref names without the " (", ")" wrapping.
+'%D':: ref names without the " (", ")" wrapping
+'%r':: ref names only, separated by spaces
 '%(describe[:options])':: human-readable name, like
 			  linkgit:git-describe[1]; empty string for
 			  undescribable commits.  The `describe` string
log-tree.c +35 -17

94 lines not on this page · raw message

log-tree.h +8 -7
index e7e4641cf8..5aa8908c65 100644
@@ -13,17 +13,18 @@ struct decoration_filter {
 	struct string_list *exclude_ref_config_pattern;
 };
 
+enum decoration_format {
+  DECO_FMT_BARE = 0,
+  DECO_FMT_UNWRAPPED,
+  DECO_FMT_WRAPPED,
+};
+
 int parse_decorate_color_config(const char *var, const char *slot_name, const char *value);
 int log_tree_diff_flush(struct rev_info *);
 int log_tree_commit(struct rev_info *, struct commit *);
 void show_log(struct rev_info *opt);
-void format_decorations_extended(struct strbuf *sb, const struct commit *commit,
-			     int use_color,
-			     const char *prefix,
-			     const char *separator,
-			     const char *suffix);
-#define format_decorations(strbuf, commit, color) \
-			     format_decorations_extended((strbuf), (commit), (color), " (", ", ", ")")
+void format_decorations(struct strbuf *sb, const struct commit *commit,
+			int use_color, enum decoration_format format);
 void show_decorations(struct rev_info *opt, struct commit *commit);
 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
 			     const char **extra_headers_p,
pretty.c +5 -2
index 0bb938021b..88b041df85 100644
@@ -1526,10 +1526,13 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
 		strbuf_addstr(sb, get_revision_mark(NULL, commit));
 		return 1;
 	case 'd':
-		format_decorations(sb, commit, c->auto_color);
+		format_decorations(sb, commit, c->auto_color, DECO_FMT_WRAPPED);
 		return 1;
 	case 'D':
-		format_decorations_extended(sb, commit, c->auto_color, "", ", ", "");
+		format_decorations(sb, commit, c->auto_color, DECO_FMT_UNWRAPPED);
+		return 1;
+	case 'r':
+		format_decorations(sb, commit, c->auto_color, DECO_FMT_BARE);
 		return 1;
 	case 'S':		/* tag/branch like --source */
 		if (!(c->pretty_ctx->rev && c->pretty_ctx->rev->sources))
t/t4202-log.sh +6 -1
index ae73aef922..9e0871df24 100755
@@ -2325,7 +2325,12 @@ test_expect_success 'log --decorate includes all levels of tag annotated tags' '
 	HEAD -> branch, tag: lightweight, tag: double-1, tag: double-0, tag: annotated
 	EOF
 	git log -1 --format="%D" >actual &&
-	test_cmp expect actual
+	test_cmp expect actual &&
+	cat >expect2 <<-\EOF &&
+	HEAD -> branch lightweight double-1 double-0 annotated
+	EOF
+	git log -1 --format="%r" >actual2 &&
+	test_cmp expect2 actual2
 '
 
 test_expect_success 'log --decorate does not include things outside filter' '
t/t4207-log-decoration-colors.sh +11 -11
index ded33a82e2..7effc0813f 100755
@@ -55,13 +55,13 @@ test_expect_success 'commit decorations colored correctly' '
 	cat >expect <<-EOF &&
 	${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_HEAD}HEAD -> \
 ${c_reset}${c_branch}main${c_reset}${c_commit}, \
-${c_reset}${c_tag}tag: v1.0${c_reset}${c_commit}, \
-${c_reset}${c_tag}tag: B${c_reset}${c_commit})${c_reset} B
-${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_tag}tag: A1${c_reset}${c_commit}, \
+${c_reset}${c_tag}tag: ${c_reset}${c_tag}v1.0${c_reset}${c_commit}, \
+${c_reset}${c_tag}tag: ${c_reset}${c_tag}B${c_reset}${c_commit})${c_reset} B
+${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_tag}tag: ${c_reset}${c_tag}A1${c_reset}${c_commit}, \
 ${c_reset}${c_remoteBranch}other/main${c_reset}${c_commit})${c_reset} A1
 	${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_stash}refs/stash${c_reset}${c_commit})${c_reset} \
 On main: Changes to A.t
-	${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_tag}tag: A${c_reset}${c_commit})${c_reset} A
+	${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_tag}tag: ${c_reset}${c_tag}A${c_reset}${c_commit})${c_reset} A
 	EOF
 
 	git log --first-parent --no-abbrev --decorate --oneline --color=always --all >actual &&
@@ -78,10 +78,10 @@ test_expect_success 'test coloring with replace-objects' '
 	cat >expect <<-EOF &&
 	${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_HEAD}HEAD -> \
 ${c_reset}${c_branch}main${c_reset}${c_commit}, \
-${c_reset}${c_tag}tag: D${c_reset}${c_commit})${c_reset} D
-	${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_tag}tag: C${c_reset}${c_commit}, \
+${c_reset}${c_tag}tag: ${c_reset}${c_tag}D${c_reset}${c_commit})${c_reset} D
+	${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_tag}tag: ${c_reset}${c_tag}C${c_reset}${c_commit}, \
 ${c_reset}${c_grafted}replaced${c_reset}${c_commit})${c_reset} B
-	${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_tag}tag: A${c_reset}${c_commit})${c_reset} A
+	${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_tag}tag: ${c_reset}${c_tag}A${c_reset}${c_commit})${c_reset} A
 EOF
 
 	git log --first-parent --no-abbrev --decorate --oneline --color=always HEAD >actual &&
@@ -102,11 +102,11 @@ test_expect_success 'test coloring with grafted commit' '
 	cat >expect <<-EOF &&
 	${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_HEAD}HEAD -> \
 ${c_reset}${c_branch}main${c_reset}${c_commit}, \
-${c_reset}${c_tag}tag: D${c_reset}${c_commit}, \
+${c_reset}${c_tag}tag: ${c_reset}${c_tag}D${c_reset}${c_commit}, \
 ${c_reset}${c_grafted}replaced${c_reset}${c_commit})${c_reset} D
-	${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_tag}tag: v1.0${c_reset}${c_commit}, \
-${c_reset}${c_tag}tag: B${c_reset}${c_commit})${c_reset} B
-	${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_tag}tag: A${c_reset}${c_commit})${c_reset} A
+	${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_tag}tag: ${c_reset}${c_tag}v1.0${c_reset}${c_commit}, \
+${c_reset}${c_tag}tag: ${c_reset}${c_tag}B${c_reset}${c_commit})${c_reset} B
+	${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_tag}tag: ${c_reset}${c_tag}A${c_reset}${c_commit})${c_reset} A
 	EOF
 
 	git log --first-parent --no-abbrev --decorate --oneline --color=always HEAD >actual &&
Signature · 1 line
2.41.0
#1 lore raw

Not a proper review... just running my eye quickly over the patch...

Quoted 9 lines · Andy Koppe

This lists refs similarly to the %D decoration format, but separates the refs with spaces only and omits "tag:" annotations. It's intended primarily for color output, where tags are already distinguished by color.

Refactor format_decorations() to take an enum decoration_format argument that determines the prefix, separator and suffix as well as the tag annotation.

Quoted 7 lines

For %d and %D, wrap the "tag:" annotation and the actual tag in separate color controls, because otherwise the tag ends up uncolored when %w width formatting breaks a line between the annotation and tag.

Amend t4207-log-decoration-colors.sh to reflect the added color controls, and t4202-log.sh to test the %r format.
---

Missing sign-off.

log-tree.h struct decoration_filter { · line 17

Quoted 7 lines
log-tree.h +5 -0
@@ -13,17 +13,18 @@ struct decoration_filter {
+enum decoration_format {
+  DECO_FMT_BARE = 0,
+  DECO_FMT_UNWRAPPED,
+  DECO_FMT_WRAPPED,
+};

Indent with TAB, not spaces.

Is this enum name a bit too generic for a public header? A quick scan of other enums in the project shows that they usually incorporate the "subsystem" into their names somehow (often as a prefix); for instance, "enum apply_ws_ignore", "enum bisect_error".

#2 lore raw
Quoted 18 lines · Eric Sunshine

Not a proper review... just running my eye quickly over the patch...
...

Quoted 3 lines

Amend t4207-log-decoration-colors.sh to reflect the added color controls, and t4202-log.sh to test the %r format.
---

Missing sign-off.

log-tree.h struct decoration_filter { · line 17

Quoted 7 lines
log-tree.h +5 -0
@@ -13,17 +13,18 @@ struct decoration_filter {
+enum decoration_format {
+  DECO_FMT_BARE = 0,
+  DECO_FMT_UNWRAPPED,
+  DECO_FMT_WRAPPED,
+};

Indent with TAB, not spaces.

Quoted 4 lines

Is this enum name a bit too generic for a public header? A quick scan of other enums in the project shows that they usually incorporate the "subsystem" into their names somehow (often as a prefix); for instance, "enum apply_ws_ignore", "enum bisect_error".

Everything you said makes sense.

But more importantly, I doubt the wisdom of adding any more %<single letter> placeholders to the vocabulary. Even though I personally do not see any need for variants other than just the plain "%d" to show the "decorate" information (if you want anything else, just post-process the output), if we really want to, the way we should extend the format placeholders is to add %(decorate:<options>) that is extensible enough that it can produce the identical output as existing "%d" and "%D" placeholders do, and add new ones as a new option to %(decorate).

Thanks.

author
#3 lore raw
Quoted 7 lines
Quoted 5 lines · Eric Sunshine

Not a proper review... just running my eye quickly over the patch...
...
Missing sign-off.

Indent with TAB, not spaces.

Thanks for the check, and apologies for those avoidable mistakes. Must remember to run checkpatch …

I'll send a corrected patch, if only for completeness.

Quoted 5 lines
Quoted 5 lines
Quoted 1 line

+enum decoration_format {

Is this enum name a bit too generic for a public header? A quick scan of other enums in the project shows that they usually incorporate the "subsystem" into their names somehow (often as a prefix); for instance, "enum apply_ws_ignore", "enum bisect_error".

I took existing decoration-related types as precedent, in particular enum decoration_type and structs decoration_entry and decoration_filter, whereby the latter is in the same header.

Quoted 5 lines · Junio C Hamano

But more importantly, I doubt the wisdom of adding any more %<single letter> placeholders to the vocabulary. Even though I personally do not see any need for variants other than just the plain "%d" to show the "decorate" information (if you want anything else, just post-process the output)

The proposed %r placeholder basically is the minimised version of %d, which could save space in one-line logs and generally reduce visual noise in custom log formats. Post-processing is rather more difficult and error-prone than a built-in feature.

Quoted 5 lines

if we really want to, the way we should
extend the format placeholders is to add %(decorate:<options>) that is extensible enough that it can produce the identical output as existing "%d" and "%D" placeholders do, and add new ones as a new option to %(decorate).

I'd be happy to look into that.

What have you got in mind for the <options>?

Something like:
%(decorate) for %d
%(decorate:unwrapped) for %D
%(decorate:bare) instead of the proposed %r

Or something with separate options for each element, similar to the separator option of %(trailers)?

%r might look as follows, with a space for the separator and empty strings for the other elements:

%(decorate:prefix=,separator= ,suffix=,tag=)

(Each option would default to its %d value if not specified.)

Thanks,
Andy

author
#4 lore raw

PATCH v2 1/1 pretty: add %r format specifier for showing refs

In reply to Eric Sunshine

This lists refs similarly to the %D decoration format, but separates the refs with spaces only and omits "tag:" annotations. It's intended primarily for color output, where tags are already distinguished by color.

Refactor format_decorations() to take an enum decoration_format argument that determines the prefix, separator and suffix as well as the tag annotation.

For %d and %D, wrap the "tag:" annotation and the actual tag in separate color controls, because otherwise the tag ends up uncolored when %w width formatting breaks a line between the annotation and tag.

Amend t4207-log-decoration-colors.sh to reflect the added color controls, and t4202-log.sh to test the %r format.

---
CI: https://github.com/ak2/git/actions/runs/5535093717
All passed apart from unrelated errors for MacOS.

 Documentation/pretty-formats.txt |  3 +-
 log-tree.c                       | 53 ++++++++++++++++++++++----------
 log-tree.h                       | 15 ++++-----
 pretty.c                         |  7 +++--
 t/t4202-log.sh                   |  7 ++++-
 t/t4207-log-decoration-colors.sh | 32 +++++++++++--------
 6 files changed, 76 insertions(+), 41 deletions(-)
Documentation/pretty-formats.txt +2 -1
index 3b71334459..d2ae898c79 100644
@@ -221,7 +221,8 @@ The placeholders are:
 '%ch':: committer date, human style (like the `--date=human` option of
 	linkgit:git-rev-list[1])
 '%d':: ref names, like the --decorate option of linkgit:git-log[1]
-'%D':: ref names without the " (", ")" wrapping.
+'%D':: ref names without the " (", ")" wrapping
+'%r':: ref names only, separated by spaces
 '%(describe[:options])':: human-readable name, like
 			  linkgit:git-describe[1]; empty string for
 			  undescribable commits.  The `describe` string
log-tree.c +36 -17

95 lines not on this page · raw message

log-tree.h +8 -7
index e7e4641cf8..345d1e80dc 100644
@@ -13,17 +13,18 @@ struct decoration_filter {
 	struct string_list *exclude_ref_config_pattern;
 };
 
+enum decoration_format {
+	DECO_FMT_BARE = 0,
+	DECO_FMT_UNWRAPPED,
+	DECO_FMT_WRAPPED,
+};
+
 int parse_decorate_color_config(const char *var, const char *slot_name, const char *value);
 int log_tree_diff_flush(struct rev_info *);
 int log_tree_commit(struct rev_info *, struct commit *);
 void show_log(struct rev_info *opt);
-void format_decorations_extended(struct strbuf *sb, const struct commit *commit,
-			     int use_color,
-			     const char *prefix,
-			     const char *separator,
-			     const char *suffix);
-#define format_decorations(strbuf, commit, color) \
-			     format_decorations_extended((strbuf), (commit), (color), " (", ", ", ")")
+void format_decorations(struct strbuf *sb, const struct commit *commit,
+			int use_color, enum decoration_format format);
 void show_decorations(struct rev_info *opt, struct commit *commit);
 void log_write_email_headers(struct rev_info *opt, struct commit *commit,
 			     const char **extra_headers_p,
pretty.c +5 -2
index 0bb938021b..88b041df85 100644
@@ -1526,10 +1526,13 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
 		strbuf_addstr(sb, get_revision_mark(NULL, commit));
 		return 1;
 	case 'd':
-		format_decorations(sb, commit, c->auto_color);
+		format_decorations(sb, commit, c->auto_color, DECO_FMT_WRAPPED);
 		return 1;
 	case 'D':
-		format_decorations_extended(sb, commit, c->auto_color, "", ", ", "");
+		format_decorations(sb, commit, c->auto_color, DECO_FMT_UNWRAPPED);
+		return 1;
+	case 'r':
+		format_decorations(sb, commit, c->auto_color, DECO_FMT_BARE);
 		return 1;
 	case 'S':		/* tag/branch like --source */
 		if (!(c->pretty_ctx->rev && c->pretty_ctx->rev->sources))
t/t4202-log.sh +6 -1
index ae73aef922..9e0871df24 100755
@@ -2325,7 +2325,12 @@ test_expect_success 'log --decorate includes all levels of tag annotated tags' '
 	HEAD -> branch, tag: lightweight, tag: double-1, tag: double-0, tag: annotated
 	EOF
 	git log -1 --format="%D" >actual &&
-	test_cmp expect actual
+	test_cmp expect actual &&
+	cat >expect2 <<-\EOF &&
+	HEAD -> branch lightweight double-1 double-0 annotated
+	EOF
+	git log -1 --format="%r" >actual2 &&
+	test_cmp expect2 actual2
 '
 
 test_expect_success 'log --decorate does not include things outside filter' '
t/t4207-log-decoration-colors.sh +19 -13
index ded33a82e2..3a4eedc494 100755
@@ -55,13 +55,15 @@ test_expect_success 'commit decorations colored correctly' '
 	cat >expect <<-EOF &&
 	${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_HEAD}HEAD -> \
 ${c_reset}${c_branch}main${c_reset}${c_commit}, \
-${c_reset}${c_tag}tag: v1.0${c_reset}${c_commit}, \
-${c_reset}${c_tag}tag: B${c_reset}${c_commit})${c_reset} B
-${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_tag}tag: A1${c_reset}${c_commit}, \
+${c_reset}${c_tag}tag: ${c_reset}${c_tag}v1.0${c_reset}${c_commit}, \
+${c_reset}${c_tag}tag: ${c_reset}${c_tag}B${c_reset}${c_commit})${c_reset} B
+${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}\
+${c_tag}tag: ${c_reset}${c_tag}A1${c_reset}${c_commit}, \
 ${c_reset}${c_remoteBranch}other/main${c_reset}${c_commit})${c_reset} A1
-	${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_stash}refs/stash${c_reset}${c_commit})${c_reset} \
-On main: Changes to A.t
-	${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_tag}tag: A${c_reset}${c_commit})${c_reset} A
+	${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}\
+${c_stash}refs/stash${c_reset}${c_commit})${c_reset} On main: Changes to A.t
+	${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}\
+${c_tag}tag: ${c_reset}${c_tag}A${c_reset}${c_commit})${c_reset} A
 	EOF
 
 	git log --first-parent --no-abbrev --decorate --oneline --color=always --all >actual &&
@@ -78,10 +80,12 @@ test_expect_success 'test coloring with replace-objects' '
 	cat >expect <<-EOF &&
 	${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_HEAD}HEAD -> \
 ${c_reset}${c_branch}main${c_reset}${c_commit}, \
-${c_reset}${c_tag}tag: D${c_reset}${c_commit})${c_reset} D
-	${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_tag}tag: C${c_reset}${c_commit}, \
+${c_reset}${c_tag}tag: ${c_reset}${c_tag}D${c_reset}${c_commit})${c_reset} D
+	${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}\
+${c_tag}tag: ${c_reset}${c_tag}C${c_reset}${c_commit}, \
 ${c_reset}${c_grafted}replaced${c_reset}${c_commit})${c_reset} B
-	${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_tag}tag: A${c_reset}${c_commit})${c_reset} A
+	${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}\
+${c_tag}tag: ${c_reset}${c_tag}A${c_reset}${c_commit})${c_reset} A
 EOF
 
 	git log --first-parent --no-abbrev --decorate --oneline --color=always HEAD >actual &&
@@ -102,11 +106,13 @@ test_expect_success 'test coloring with grafted commit' '
 	cat >expect <<-EOF &&
 	${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_HEAD}HEAD -> \
 ${c_reset}${c_branch}main${c_reset}${c_commit}, \
-${c_reset}${c_tag}tag: D${c_reset}${c_commit}, \
+${c_reset}${c_tag}tag: ${c_reset}${c_tag}D${c_reset}${c_commit}, \
 ${c_reset}${c_grafted}replaced${c_reset}${c_commit})${c_reset} D
-	${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_tag}tag: v1.0${c_reset}${c_commit}, \
-${c_reset}${c_tag}tag: B${c_reset}${c_commit})${c_reset} B
-	${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}${c_tag}tag: A${c_reset}${c_commit})${c_reset} A
+	${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}\
+${c_tag}tag: ${c_reset}${c_tag}v1.0${c_reset}${c_commit}, \
+${c_reset}${c_tag}tag: ${c_reset}${c_tag}B${c_reset}${c_commit})${c_reset} B
+	${c_commit}COMMIT_ID${c_reset}${c_commit} (${c_reset}\
+${c_tag}tag: ${c_reset}${c_tag}A${c_reset}${c_commit})${c_reset} A
 	EOF
 
 	git log --first-parent --no-abbrev --decorate --oneline --color=always HEAD >actual &&
Signature · 1 line
2.41.0