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

[PATCH] git commit: Reformat output somewhat

From
Andreas Ericsson <ae@op5.se>
Date
Sep 30, 2008, 09:52 UTC
Message-ID
<48E1F6F2.5080302@op5.se>
In-Reply-To
<20080930061654.GA14584@sigill.intra.peff.net>
Previously, we used to print something along the lines of
	Created commit abc9056 on master: Snib the sprock

but that output was sometimes confusing, as many projects use the "subsystem: message" style of commit subjects (just like this commit message does). When such improvements are done on topic-branches, it's not uncommon to name the topic-branch the same as the subsystem, leading to output like this:

	Created commit abc9056 on i386: i386: Snib the sprock

which doesn't look very nice and can be highly confusing. This patch alters the format so that the noise-word "commit" is dropped except when it makes the output read better and the commit subject is put inside parentheses. We also emphasize the detached case so that users do not overlook it in case the commit subject is long enough to extend to the next line. The end result looks thusly:

	normal case
	Created abc9056 (i386: Snib the sprock) on i386
	detached head
	Created DETACHED commit abc9056 (i386: Snib the sprock)

While we're at it, we rename "initial commit" to "root-commit" to align it with the argument to 'git log', producing this:

	initial commit
	Created root-commit abc9056 (i386: Snib the sprock) on i386

Documentation/gittutorial-2.txt is updated accordingly so that new users recognize what they're looking at.

Signed-off-by: Andreas Ericsson <ae@op5.se>
---

According to the few entries in the discussion about showing the branch we're on, this patch should probably go on top of next fairly soon.

If the code-change isn't accepted, let me know and I'll fix the documentation update to match whatever goes in builtin-commit.c.

Feel free to alter shouty-caps for detached when applying. I have no strong opinion either way, as I never commit on detached head anyway.

Thanks.
 Documentation/gittutorial-2.txt |   13 ++++++++-----
 builtin-commit.c                |   12 +++++-------
 2 files changed, 13 insertions(+), 12 deletions(-)
diff --git a/Documentation/gittutorial-2.txt b/Documentation/gittutorial-2.txt
index 6609046..8484e7a 100644
--- a/Documentation/gittutorial-2.txt
+++ b/Documentation/gittutorial-2.txt
@@ -32,22 +32,25 @@ Initialized empty Git repository in .git/
 $ echo 'hello world' > file.txt
 $ git add .
 $ git commit -a -m "initial commit"
-Created initial commit 54196cc2703dc165cbd373a65a4dcf22d50ae7f7
+Created root-commit 54196cc (initial commit) on master
  create mode 100644 file.txt
 $ echo 'hello world!' >file.txt
 $ git commit -a -m "add emphasis"
-Created commit c4d59f390b9cfd4318117afde11d601c1085f241
+Created c4d59f3 (add emphasis) on master
 ------------------------------------------------
 
-What are the 40 digits of hex that git responded to the commit with?
+What are the 7 digits of hex that git responded to the commit with?
 
 We saw in part one of the tutorial that commits have names like this.
 It turns out that every object in the git history is stored under
-such a 40-digit hex name.  That name is the SHA1 hash of the object's
+a 40-digit hex name.  That name is the SHA1 hash of the object's
 contents; among other things, this ensures that git will never store
 the same data twice (since identical data is given an identical SHA1
 name), and that the contents of a git object will never change (since
-that would change the object's name as well).
+that would change the object's name as well). The 7 char hex strings
+here are simply the abbreviation of such 40 character long strings.
+Abbreviations can be used everywhere where the 40 character strings
+can be used, so long as they are unambiguous.
 
 It is expected that the content of the commit object you created while
 following the example above generates a different SHA1 hash than
diff --git a/builtin-commit.c b/builtin-commit.c
index 161128b..f0765cc 100644
--- a/builtin-commit.c
+++ b/builtin-commit.c
@@ -884,12 +884,11 @@ static char *get_commit_format_string(void)
 	const char *head = resolve_ref("HEAD", sha, 0, NULL);
 	struct strbuf buf = STRBUF_INIT;
 
-	strbuf_addstr(&buf, "format:%h");
+	/* use shouty-caps if we're on detached HEAD */
+	strbuf_addf(&buf, "format:%s", strcmp("HEAD", head) ? "" : "DETACHED commit");
+	strbuf_addstr(&buf, "%h (%s)");
 
-	/* Are we on a detached HEAD? */
-	if (!strcmp("HEAD", head))
-		strbuf_addstr(&buf, " on detached HEAD");
-	else if (!prefixcmp(head, "refs/heads/")) {
+	if (!prefixcmp(head, "refs/heads/")) {
 		const char *cp;
 		strbuf_addstr(&buf, " on ");
 		for (cp = head + 11; *cp; cp++) {
@@ -899,7 +898,6 @@ static char *get_commit_format_string(void)
 				strbuf_addch(&buf, *cp);
 		}
 	}
-	strbuf_addstr(&buf, ": %s");
 
 	return strbuf_detach(&buf, NULL);
 }
@@ -933,7 +931,7 @@ static void print_summary(const char *prefix, const unsigned char *sha1)
 	rev.diffopt.break_opt = 0;
 	diff_setup_done(&rev.diffopt);
 
-	printf("Created %scommit ", initial_commit ? "initial " : "");
+	printf("Created %s", initial_commit ? "root-commit " : "");
 
 	if (!log_tree_commit(&rev, commit)) {
 		struct strbuf buf = STRBUF_INIT;
-- 
1.6.0.2.529.g37dbc.dirty
Previous: Andreas EricssonNext: Wincent Colaiuta
Message 45 of 67 in “[RFC] Detached-HEAD reminder on commit?”
  1. Pieter de BieSep 2, 2008
  2. Robin RosenbergSep 2, 2008
  3. Nicolas PitreSep 2, 2008
  4. Matthieu MoySep 2, 2008
  5. Nicolas PitreSep 2, 2008
  6. Junio C HamanoSep 2, 2008
  7. Stephan BeyerSep 2, 2008
  8. Johan HerlandSep 2, 2008
  9. Jeff KingSep 2, 2008
  10. Jeff KingSep 2, 2008
  11. Johan HerlandSep 3, 2008
  12. Junio C HamanoSep 3, 2008
  13. Johan HerlandSep 3, 2008
  14. Jeff KingSep 3, 2008
  15. Jeff KingSep 3, 2008
  16. Andreas EricssonSep 3, 2008
  17. Daniel BarkalowSep 3, 2008
  18. Jeff KingSep 3, 2008
  19. Junio C HamanoSep 3, 2008
  20. Jeff KingSep 3, 2008
  21. Wincent ColaiutaSep 3, 2008
  22. Jeff KingSep 3, 2008
  23. Nicolas PitreSep 3, 2008
  24. Junio C HamanoSep 2, 2008
  25. Nicolas PitreSep 2, 2008
  26. Avery PennarunSep 4, 2008
  27. Junio C HamanoSep 4, 2008
  28. Junio C HamanoSep 5, 2008
  29. Pieter de BieSep 2, 2008
  30. Jakub NarebskiSep 2, 2008
  31. Junio C HamanoSep 2, 2008
  32. Nicolas PitreSep 2, 2008
  33. Pieter de BieSep 3, 2008
  34. Builtin-commit: show on which branch a commit was addedPieter de Bie, Sep 5, 2008
  35. Junio C HamanoSep 7, 2008
  36. Junio C HamanoSep 7, 2008
  37. 1/2 pretty.c: add %% format specifierPieter de Bie, Sep 7, 2008
  38. 2/2 builtin-commit: show on which branch a commit was addedPieter de Bie, Sep 7, 2008
  39. Jeff KingSep 21, 2008
  40. Pieter de BieSep 29, 2008
  41. Jeff KingSep 29, 2008
  42. Andreas EricssonSep 30, 2008
  43. Jeff KingSep 30, 2008
  44. Andreas EricssonSep 30, 2008
  45. git commit: Reformat output somewhatAndreas Ericsson, Sep 30, 2008
  46. Wincent ColaiutaSep 30, 2008
  47. Jeff KingSep 30, 2008
  48. Andreas EricssonSep 30, 2008
  49. Jeff KingOct 1, 2008
  50. Andreas EricssonOct 1, 2008
  51. Shawn O. PearceOct 1, 2008
  52. Andreas EricssonOct 1, 2008
  53. Jeff KingOct 1, 2008
  54. Shawn O. PearceOct 1, 2008
  55. Jeff KingOct 1, 2008
  56. Shawn O. PearceOct 1, 2008
  57. git commit: Repaint the output format bikeshed (again)Andreas Ericsson, Oct 1, 2008
  58. Jeff KingOct 1, 2008
  59. Jeff KingOct 1, 2008
  60. Andreas EricssonOct 2, 2008
  61. Jeff KingOct 2, 2008
  62. Shawn O. PearceOct 3, 2008
  63. Jeff KingOct 3, 2008
  64. Shawn O. PearceOct 3, 2008
  65. Jeff KingOct 4, 2008
  66. Wincent ColaiutaOct 2, 2008
  67. Jeff KingOct 1, 2008

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.