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

Re: [PATCH] Builtin-commit: show on which branch a commit was added

From
Jeff King <peff@peff.net>
Date
Oct 1, 2008, 03:14 UTC
Message-ID
<20081001031400.GA24513@coredump.intra.peff.net>
In-Reply-To
<48E1F87D.2010906@op5.se>
On Tue, Sep 30, 2008 at 11:59:25AM +0200, Andreas Ericsson wrote:
> I agree. Obvious solution is to do
>
> subj_len = term_width - (strlen(cruft) + strlen(branch_name))

I think the difficulty is that the printing is sometimes done by our printf and sometimes by log_tree_commit, and there isn't a convenient way to hook into log_tree_commit to postprocess the formatted output.

> where strlen(cruft) is just 8 less if we drop 'commit ' from the
> cases. See the patch I just sent though. I sort of like that one.

I like it much better than what is on next (and I thought your commit message summed up the issue nicely), but...

> Another way would be to write
> <branch>: Created <hash>: "subject line..."

I think I like this even better. My only concern is that many programs say "program: some error", so you could potentially have a confusing branch name. But I personally have never used a branch name that would cause such confusion.

Show 6 quoted lines
> As <hash> will very, very rarely match anything the user would put
> in his/her commit message themselves. Quoting the subject is probably
> a nice touch, and it can make sense to put it last as it's the least
> interesting of the things we print. Ah well. I'll just await commentary
> on the patch I've already sent before I go ahead and do something like
> that.

Here is a patch for that format on top of next (the patch between this and what is in master is even more simple, since we are mostly removing Pieter's helper function).

---
diff --git a/builtin-commit.c b/builtin-commit.c
index 917f638..9954a81 100644
--- a/builtin-commit.c
+++ b/builtin-commit.c
@@ -876,41 +876,17 @@ int cmd_status(int argc, const char **argv, const char *prefix)
 	rollback_index_files();
 
 	return commitable ? 0 : 1;
 }
 
-static char *get_commit_format_string(void)
-{
-	unsigned char sha[20];
-	const char *head = resolve_ref("HEAD", sha, 0, NULL);
-	struct strbuf buf = STRBUF_INIT;
-
-	strbuf_addstr(&buf, "format:%h");
-
-	/* Are we on a detached HEAD? */
-	if (!strcmp("HEAD", head))
-		strbuf_addstr(&buf, " on detached HEAD");
-	else if (!prefixcmp(head, "refs/heads/")) {
-		const char *cp;
-		strbuf_addstr(&buf, " on ");
-		for (cp = head + 11; *cp; cp++) {
-			if (*cp == '%')
-				strbuf_addstr(&buf, "%x25");
-			else
-				strbuf_addch(&buf, *cp);
-		}
-	}
-	strbuf_addstr(&buf, ": %s");
-
-	return strbuf_detach(&buf, NULL);
-}
-
 static void print_summary(const char *prefix, const unsigned char *sha1)
 {
 	struct rev_info rev;
 	struct commit *commit;
-	char *format = get_commit_format_string();
+	static const char *format = "format:%h: \"%s\"";
+	unsigned char junk_sha1[20];
+	const char *head = resolve_ref("HEAD", junk_sha1, 0, NULL);
 
 	commit = lookup_commit(sha1);
 	if (!commit)
 		die("couldn't look up newly created commit");
 	if (!commit || parse_commit(commit))
@@ -931,19 +907,20 @@ static void print_summary(const char *prefix, const unsigned char *sha1)
 	rev.diffopt.detect_rename = 1;
 	rev.diffopt.rename_limit = 100;
 	rev.diffopt.break_opt = 0;
 	diff_setup_done(&rev.diffopt);
 
-	printf("Created %scommit ", initial_commit ? "initial " : "");
+	printf("%s%s: created ",
+		!prefixcmp(head, "refs/heads/") ? head + 11 : head,
+		initial_commit ? " (initial)" : "");
 
 	if (!log_tree_commit(&rev, commit)) {
 		struct strbuf buf = STRBUF_INIT;
 		format_commit_message(commit, format + 7, &buf, DATE_NORMAL);
 		printf("%s\n", buf.buf);
 		strbuf_release(&buf);
 	}
-	free(format);
 }
 
 static int git_commit_config(const char *k, const char *v, void *cb)
 {
 	if (!strcmp(k, "commit.template"))
Previous: Andreas EricssonNext: Andreas Ericsson
Message 49 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.