git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 09/10] pretty: implement 'summary' format

From
Denton Liu <liu.denton@gmail.com>
Date
Nov 8, 2019, 20:08 UTC
Message-ID
<e74eab6d21f655698ef8b6e1286b44ea070a7af7.1573241590.git.liu.denton@gmail.com>
In-Reply-To
<cover.1573241590.git.liu.denton@gmail.com>

The standard format for referencing other commits within some projects (such as git.git) is the summary format. This is described in Documentation/SubmittingPatches as

	If you want to reference a previous commit in the history of a stable
	branch, use the format "abbreviated hash (subject, date)",
	with the subject enclosed in a pair of double-quotes, like this:
	....
		Commit f86a374 ("pack-bitmap.c: fix a memleak", 2015-03-30)
		noticed that ...
	....

Since this format is so commonly used, standardize it as a pretty format.

This format is implemented as a separate flow that skips most of pretty_print_commit() and instead calls format_commit_summary(). The reason why this is done is because the other pretty formats expect output to be generated in a specific order. Specifically, the header, including the date, is always printed before the commit message, including the subject. Reversing the order would be possible but would involve butchering pretty_print_commit() so it is implemented as a separate flow.

Signed-off-by: Denton Liu <liu.denton@gmail.com>
---
 Documentation/pretty-formats.txt   |  9 ++++
 Documentation/pretty-options.txt   |  2 +-
 Documentation/rev-list-options.txt |  2 +-
 builtin/log.c                      | 30 +++++++++--
 log-tree.c                         | 11 ++--
 pretty.c                           | 31 ++++++++++-
 pretty.h                           |  1 +
 t/t4205-log-pretty-formats.sh      | 83 +++++++++++++++++++++++++-----
 8 files changed, 144 insertions(+), 25 deletions(-)
diff --git a/Documentation/pretty-formats.txt b/Documentation/pretty-formats.txt
index 71eef684d0..f9c1296d7c 100644
--- a/Documentation/pretty-formats.txt
+++ b/Documentation/pretty-formats.txt
@@ -63,6 +63,15 @@ This is designed to be as compact as possible.
 
 	       <full commit message>
 
+* 'summary'
+
+	  <abbrev hash> ("<title line>", <short author date>)
++
+This format is useful for referring to other commits when writing a new
+commit message. Although by default, '<abbrev hash>' is used, this can
+be overridden with '--no-abbrev'. In addition, '<short author date>' can
+be overridden by with '--date='.
+
 * 'email'
 
 	  From <hash> <date>
diff --git a/Documentation/pretty-options.txt b/Documentation/pretty-options.txt
index e44fc8f738..0a5b206193 100644
--- a/Documentation/pretty-options.txt
+++ b/Documentation/pretty-options.txt
@@ -3,7 +3,7 @@
 
 	Pretty-print the contents of the commit logs in a given format,
 	where '<format>' can be one of 'oneline', 'short', 'medium',
-	'full', 'fuller', 'email', 'raw', 'format:<string>'
+	'full', 'fuller', 'summary', 'email', 'raw', 'format:<string>'
 	and 'tformat:<string>'.  When '<format>' is none of the above,
 	and has '%placeholder' in it, it acts as if
 	'--pretty=tformat:<format>' were given.
diff --git a/Documentation/rev-list-options.txt b/Documentation/rev-list-options.txt
index 90ff9e2bea..76df494ed6 100644
--- a/Documentation/rev-list-options.txt
+++ b/Documentation/rev-list-options.txt
@@ -289,7 +289,7 @@ depending on a few rules:
 4. Otherwise, show the index format.
 --
 +
-Under `--pretty=oneline`, the commit message is
+Under `--pretty=oneline` and `--pretty=summary`, the commit message is
 prefixed with this information on the same line.
 This option cannot be combined with `--reverse`.
 See also linkgit:git-reflog[1].
diff --git a/builtin/log.c b/builtin/log.c
index e4df16be79..ad96a746a3 100644
--- a/builtin/log.c
+++ b/builtin/log.c
@@ -223,15 +223,35 @@ static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,
 		read_mailmap(rev->mailmap, NULL);
 	}
 
-	if (rev->pretty_given && rev->commit_format == CMIT_FMT_RAW) {
+	if (rev->pretty_given) {
+		switch (rev->commit_format) {
+
 		/*
 		 * "log --pretty=raw" is special; ignore UI oriented
 		 * configuration variables such as decoration.
 		 */
-		if (!decoration_given)
-			decoration_style = 0;
-		if (!rev->abbrev_commit_explicit)
-			rev->abbrev_commit = 0;
+		case CMIT_FMT_RAW:
+			if (!decoration_given)
+				decoration_style = 0;
+			if (!rev->abbrev_commit_explicit)
+				rev->abbrev_commit = 0;
+			break;
+
+		/*
+		 * "log --pretty=summary" is special; ignore UI oriented
+		 * configuration variables such as decoration but keep
+		 * abbreviations.
+		 */
+		case CMIT_FMT_SUMMARY:
+			if (!decoration_given)
+				decoration_style = 0;
+			if (!rev->abbrev_commit_explicit)
+				rev->abbrev_commit = 1;
+			break;
+
+		default:
+			break;
+		}
 	}
 
 	if (decoration_style) {
diff --git a/log-tree.c b/log-tree.c
index 4a7d668af6..433adaf0f1 100644
--- a/log-tree.c
+++ b/log-tree.c
@@ -627,7 +627,8 @@ void show_log(struct rev_info *opt)
 		ctx.print_email_subject = 1;
 	} else if (opt->commit_format != CMIT_FMT_USERFORMAT) {
 		fputs(diff_get_color_opt(&opt->diffopt, DIFF_COMMIT), opt->diffopt.file);
-		if (opt->commit_format != CMIT_FMT_ONELINE)
+		if (opt->commit_format != CMIT_FMT_ONELINE &&
+		    opt->commit_format != CMIT_FMT_SUMMARY)
 			fputs("commit ", opt->diffopt.file);
 
 		if (!opt->graph)
@@ -644,7 +645,8 @@ void show_log(struct rev_info *opt)
 			       find_unique_abbrev(&parent->object.oid, abbrev_commit));
 		fputs(diff_get_color_opt(&opt->diffopt, DIFF_RESET), opt->diffopt.file);
 		show_decorations(opt, commit);
-		if (opt->commit_format == CMIT_FMT_ONELINE) {
+		if (opt->commit_format == CMIT_FMT_ONELINE ||
+		    opt->commit_format == CMIT_FMT_SUMMARY) {
 			putc(' ', opt->diffopt.file);
 		} else {
 			putc('\n', opt->diffopt.file);
@@ -658,12 +660,15 @@ void show_log(struct rev_info *opt)
 			 * graph info here.
 			 */
 			show_reflog_message(opt->reflog_info,
-					    opt->commit_format == CMIT_FMT_ONELINE,
+					    (opt->commit_format == CMIT_FMT_ONELINE ||
+					     opt->commit_format == CMIT_FMT_SUMMARY),
 					    &opt->date_mode,
 					    opt->date_mode_explicit);
 			if (opt->commit_format == CMIT_FMT_ONELINE) {
 				putc('\n', opt->diffopt.file);
 				return;
+			} else if (opt->commit_format == CMIT_FMT_SUMMARY) {
+				putc(' ', opt->diffopt.file);
 			}
 		}
 	}
diff --git a/pretty.c b/pretty.c
index a6e5fc115a..8efb2486a5 100644
--- a/pretty.c
+++ b/pretty.c
@@ -97,7 +97,8 @@ static void setup_commit_formats(void)
 		{ "mboxrd",	CMIT_FMT_MBOXRD,	0,	0 },
 		{ "fuller",	CMIT_FMT_FULLER,	0,	8 },
 		{ "full",	CMIT_FMT_FULL,		0,	8 },
-		{ "oneline",	CMIT_FMT_ONELINE,	1,	0 }
+		{ "oneline",	CMIT_FMT_ONELINE,	1,	0 },
+		{ "summary",	CMIT_FMT_SUMMARY,	1,	0 },
 		/*
 		 * Please update $__git_log_pretty_formats in
 		 * git-completion.bash when you add new formats.
@@ -1672,6 +1673,26 @@ void repo_format_commit_message(struct repository *r,
 				   do_repo_format_commit_message, (void *)format);
 }
 
+static void do_repo_format_commit_summary(struct strbuf *sb,
+					  struct format_commit_context *context,
+					  void *additional_context)
+{
+	struct ident_split ident;
+
+	parse_commit_header(context);
+	parse_commit_message(context);
+
+	strbuf_addstr(sb, "(\"");
+	format_subject(sb, context->message + context->subject_off, " ");
+	if (!split_ident_line(&ident,
+			      context->message + context->author.off,
+			      context->author.len)) {
+		strbuf_addstr(sb, "\", ");
+		strbuf_addstr(sb, show_ident_date(&ident, &context->pretty_ctx->date_mode));
+	}
+	strbuf_addstr(sb, ")");
+}
+
 static void pp_header(struct pretty_print_context *pp,
 		      const char *encoding,
 		      const struct commit *commit,
@@ -1923,6 +1944,14 @@ void pretty_print_commit(struct pretty_print_context *pp,
 		format_commit_message(commit, user_format, sb, pp);
 		return;
 	}
+	if (pp->fmt == CMIT_FMT_SUMMARY) {
+		if (!pp->date_mode_explicit)
+			pp->date_mode = *DATE_MODE(SHORT);
+
+		repo_format_commit_generic(the_repository, commit, sb, pp,
+					   do_repo_format_commit_summary, NULL);
+		return;
+	}
 
 	encoding = get_log_output_encoding();
 	msg = reencoded = logmsg_reencode(commit, NULL, encoding);
diff --git a/pretty.h b/pretty.h
index 4ad1fc31ff..6d5b18a4f1 100644
--- a/pretty.h
+++ b/pretty.h
@@ -16,6 +16,7 @@ enum cmit_fmt {
 	CMIT_FMT_FULL,
 	CMIT_FMT_FULLER,
 	CMIT_FMT_ONELINE,
+	CMIT_FMT_SUMMARY,
 	CMIT_FMT_EMAIL,
 	CMIT_FMT_MBOXRD,
 	CMIT_FMT_USERFORMAT,
diff --git a/t/t4205-log-pretty-formats.sh b/t/t4205-log-pretty-formats.sh
index d35650cae7..fcfd66faf9 100755
--- a/t/t4205-log-pretty-formats.sh
+++ b/t/t4205-log-pretty-formats.sh
@@ -154,20 +154,23 @@ do
 	'
 done
 
-test_expect_success 'NUL termination with --reflog --pretty=oneline' '
-	>expect &&
-	revs="$(git rev-list --reflog)" &&
-	for r in $revs
-	do
-		# trim trailing newline
-		output="$(git show -s --pretty=oneline "$r")" || return 1
-		printf "%s" "$output" >>expect
-		emit_nul >>expect
-	done &&
-	git log -z --pretty=oneline --reflog >actual &&
-	# no trailing NUL
-	test_cmp expect actual
-'
+for p in oneline summary
+do
+	test_expect_success "NUL termination with --reflog --pretty=$p" '
+		>expect &&
+		revs="$(git rev-list --reflog)" &&
+		for r in $revs
+		do
+			# trim trailing newline
+			output="$(git show -s --pretty='$p' "$r")" || return 1
+			printf "%s" "$output" >>expect
+			emit_nul >>expect
+		done &&
+		git log -z --pretty='$p' --reflog >actual &&
+		# no trailing NUL
+		test_cmp expect actual
+	'
+done
 
 test_expect_success 'setup more commits' '
 	test_commit "message one" one one message-one &&
@@ -823,4 +826,56 @@ test_expect_success '%S in git log --format works with other placeholders (part
 	test_cmp expect actual
 '
 
+test_expect_success 'log --pretty=summary' '
+	git log --date=short --pretty="tformat:%h (\"%s\", %ad)" >expect &&
+	git log --pretty=summary >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'log --pretty=summary with log.date is overridden by short date' '
+	git log --date=short --pretty="tformat:%h (\"%s\", %ad)" >expect &&
+	test_config log.date rfc &&
+	git log --pretty=summary >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'log --pretty=summary with explicit date overrides short date' '
+	git log --date=rfc --pretty="tformat:%h (\"%s\", %ad)" >expect &&
+	git log --date=rfc --pretty=summary >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'log --pretty=summary with log.abbrevCommit is overidden' '
+	git log --date=short --pretty="tformat:%h (\"%s\", %ad)" >expect &&
+	test_config log.abbrevCommit false &&
+	git log --pretty=summary >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'log --pretty=summary with explicit --no-abbrev overrides abbreviated' '
+	git log --date=short --pretty="tformat:%H (\"%s\", %ad)" >expect &&
+	git log --no-abbrev --pretty=summary >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'log --pretty=summary with log.decorate is overridden' '
+	git log --date=short --pretty="tformat:%h (\"%s\", %ad)" >expect &&
+	test_config log.decorate short &&
+	git log --pretty=summary >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'log --pretty=summary with explicit decorate overrides no decoration' '
+	git log --date=short --pretty="tformat:%h%d (\"%s\", %ad)" >expect &&
+	git log --decorate=short --pretty=summary >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success 'log --pretty=summary with --walk-reflogs' '
+	test_config log.date short &&
+	git log --walk-reflogs --pretty="tformat:%h %gd: %gs (\"%s\", %ad)" >expect &&
+	git log --walk-reflogs --pretty=summary >actual &&
+	test_cmp expect actual
+'
+
 test_done
-- 
2.24.0.298.g3e88fbd976
Previous: Denton LiuNext: Eric Sunshine
Message 28 of 81 in “learn the "summary" pretty format”
  1. 0/8 learn the "summary" pretty formatDenton Liu, Nov 4, 2019
  2. 1/8 pretty-formats.txt: use generic terms for hashDenton Liu, Nov 4, 2019
  3. 2/8 revision: make get_revision_mark() return const pointerDenton Liu, Nov 4, 2019
  4. 3/8 revision: change abbrev_commit_given to abbrev_commit_explicitDenton Liu, Nov 4, 2019
  5. 4/8 pretty.c: inline initalize format_contextDenton Liu, Nov 4, 2019
  6. 5/8 pretty.c: extract functionality to repo_format_commit_generic()Denton Liu, Nov 4, 2019
  7. 6/8 reflog-walk.c: don't print last newline with onelineDenton Liu, Nov 4, 2019
  8. Junio C HamanoNov 6, 2019
  9. Denton LiuNov 8, 2019
  10. 7/8 pretty: implement 'summary' formatDenton Liu, Nov 4, 2019
  11. Eric SunshineNov 4, 2019
  12. Denton LiuNov 4, 2019
  13. Eric SunshineNov 4, 2019
  14. Denton LiuNov 4, 2019
  15. 8/8 SubmittingPatches: use `--pretty=summary`Denton Liu, Nov 4, 2019
  16. 00/10 learn the "summary" pretty formatDenton Liu, Nov 8, 2019
  17. 01/10 SubmittingPatches: use generic terms for hashDenton Liu, Nov 8, 2019
  18. 02/10 pretty-formats.txt: use generic terms for hashDenton Liu, Nov 8, 2019
  19. 03/10 revision: make get_revision_mark() return const pointerDenton Liu, Nov 8, 2019
  20. 04/10 revision: change abbrev_commit_given to abbrev_commit_explicitDenton Liu, Nov 8, 2019
  21. 06/10 pretty.c: extract functionality to repo_format_commit_generic()Denton Liu, Nov 8, 2019
  22. 05/10 pretty.c: inline initalize format_contextDenton Liu, Nov 8, 2019
  23. 07/10 t4205: cover `git log --reflog -z` blindspotDenton Liu, Nov 8, 2019
  24. Eric SunshineNov 8, 2019
  25. Denton LiuNov 8, 2019
  26. Eric SunshineNov 8, 2019
  27. 08/10 reflog-walk.c: move where the newline is addedDenton Liu, Nov 8, 2019
  28. 09/10 pretty: implement 'summary' formatDenton Liu, Nov 8, 2019
  29. Eric SunshineNov 8, 2019
  30. René ScharfeNov 9, 2019
  31. Junio C HamanoNov 10, 2019
  32. Denton LiuNov 11, 2019
  33. SZEDER GáborNov 14, 2019
  34. Junio C HamanoNov 14, 2019
  35. René ScharfeNov 9, 2019
  36. SZEDER GáborNov 14, 2019
  37. 10/10 SubmittingPatches: use `--pretty=summary`Denton Liu, Nov 8, 2019
  38. 00/10 learn the "reference" pretty formatDenton Liu, Nov 14, 2019
  39. 01/10 SubmittingPatches: use generic terms for hashDenton Liu, Nov 14, 2019
  40. 02/10 pretty-formats.txt: use generic terms for hashDenton Liu, Nov 14, 2019
  41. 03/10 SubmittingPatches: remove dq from commit referenceDenton Liu, Nov 14, 2019
  42. 04/10 completion: complete `tformat:` pretty formatDenton Liu, Nov 14, 2019
  43. 05/10 revision: make get_revision_mark() return const pointerDenton Liu, Nov 14, 2019
  44. 06/10 pretty.c: inline initalize format_contextDenton Liu, Nov 14, 2019
  45. 07/10 t4205: cover `git log --reflog -z` blindspotDenton Liu, Nov 14, 2019
  46. 08/10 pretty: provide short date formatDenton Liu, Nov 14, 2019
  47. 10/10 SubmittingPatches: use `--pretty=reference`Denton Liu, Nov 14, 2019
  48. 09/10 pretty: implement 'reference' formatDenton Liu, Nov 14, 2019
  49. Todd ZullingerNov 15, 2019
  50. Junio C HamanoNov 15, 2019
  51. SZEDER GáborNov 15, 2019
  52. Junio C HamanoNov 16, 2019
  53. Junio C HamanoNov 18, 2019
  54. Junio C HamanoNov 18, 2019
  55. Eric SunshineNov 15, 2019
  56. 00/11 learn the "reference" pretty formatDenton Liu, Nov 19, 2019
  57. 01/11 SubmittingPatches: use generic terms for hashDenton Liu, Nov 19, 2019
  58. 02/11 pretty-formats.txt: use generic terms for hashDenton Liu, Nov 19, 2019
  59. 03/11 SubmittingPatches: remove dq from commit referenceDenton Liu, Nov 19, 2019
  60. 04/11 completion: complete `tformat:` pretty formatDenton Liu, Nov 19, 2019
  61. 05/11 revision: make get_revision_mark() return const pointerDenton Liu, Nov 19, 2019
  62. 06/11 pretty.c: inline initalize format_contextDenton Liu, Nov 19, 2019
  63. 07/11 t4205: cover `git log --reflog -z` blindspotDenton Liu, Nov 19, 2019
  64. 08/11 pretty: provide short date formatDenton Liu, Nov 19, 2019
  65. 09/11 pretty: implement 'reference' formatDenton Liu, Nov 19, 2019
  66. 10/11 SubmittingPatches: use `--pretty=reference`Denton Liu, Nov 19, 2019
  67. 11/11 squash! pretty: implement 'reference' formatDenton Liu, Nov 19, 2019
  68. Junio C HamanoNov 19, 2019
  69. 00/11 learn the "reference" pretty formatDenton Liu, Nov 20, 2019
  70. 01/11 SubmittingPatches: use generic terms for hashDenton Liu, Nov 20, 2019
  71. 02/11 pretty-formats.txt: use generic terms for hashDenton Liu, Nov 20, 2019
  72. 03/11 SubmittingPatches: remove dq from commit referenceDenton Liu, Nov 20, 2019
  73. 04/11 completion: complete `tformat:` pretty formatDenton Liu, Nov 20, 2019
  74. 05/11 revision: make get_revision_mark() return const pointerDenton Liu, Nov 20, 2019
  75. 06/11 pretty.c: inline initalize format_contextDenton Liu, Nov 20, 2019
  76. 07/11 t4205: cover `git log --reflog -z` blindspotDenton Liu, Nov 20, 2019
  77. 08/11 pretty: provide short date formatDenton Liu, Nov 20, 2019
  78. 09/11 pretty: add struct cmt_fmt_map::default_date_mode_typeDenton Liu, Nov 20, 2019
  79. Junio C HamanoNov 20, 2019
  80. 10/11 pretty: implement 'reference' formatDenton Liu, Nov 20, 2019
  81. 11/11 SubmittingPatches: use `--pretty=reference`Denton Liu, Nov 20, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.