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

[PATCH 5/5] log: add --commit-header option

From
Jeff King <peff@peff.net>
Date
Sep 29, 2016, 08:38 UTC
Message-ID
<20160929083851.kx6itvrh4n2rttrx@sigill.intra.peff.net>
In-Reply-To
<20160929083315.vwb3aurwbyjwlkjn@sigill.intra.peff.net>

This lets you stick a header right before a commit, but suppresses headers that are duplicates. This means you can do something like:

  git log --graph --author-date-order --commit-header='== %as =='
to get a marker in the graph whenever the day changes.

This probably needs some refactoring around the setup of the pretty-print context.

Signed-off-by: Jeff King <peff@peff.net>
---
 log-tree.c | 31 +++++++++++++++++++++++++++++++
 revision.c |  4 ++++
 revision.h |  3 +++
 3 files changed, 38 insertions(+)
diff --git a/log-tree.c b/log-tree.c
index 8c24157..76cef51 100644
--- a/log-tree.c
+++ b/log-tree.c
@@ -536,6 +536,28 @@ static void show_mergetag(struct rev_info *opt, struct commit *commit)
 	for_each_mergetag(show_one_mergetag, commit, opt);
 }
 
+static void show_commit_header(struct rev_info *opt,
+			       struct pretty_print_context *pp,
+			       struct commit *commit)
+{
+	struct strbuf out = STRBUF_INIT;
+
+	format_commit_message(commit, opt->commit_header, &out, pp);
+	strbuf_complete_line(&out);
+
+	if (!strbuf_cmp(&out, &opt->last_commit_header)) {
+		strbuf_release(&out);
+		return;
+	}
+
+	graph_show_precommit(opt->graph);
+	graph_show_padding(opt->graph);
+	fwrite(out.buf, 1, out.len, opt->diffopt.file);
+
+	strbuf_swap(&out, &opt->last_commit_header);
+	strbuf_release(&out);
+}
+
 void show_log(struct rev_info *opt)
 {
 	struct strbuf msgbuf = STRBUF_INIT;
@@ -591,6 +613,15 @@ void show_log(struct rev_info *opt)
 	}
 	opt->shown_one = 1;
 
+	if (opt->commit_header) {
+		/*
+		 * XXX probably the initialization of the pretty ctx from "opt"
+		 * below should happen sooner so we can use it.
+		 */
+		ctx.color = opt->diffopt.use_color;
+		show_commit_header(opt, &ctx, commit);
+	}
+
 	/*
 	 * If the history graph was requested,
 	 * print the graph, up to this commit's line
diff --git a/revision.c b/revision.c
index 969b3d1..229ff86 100644
--- a/revision.c
+++ b/revision.c
@@ -1361,6 +1361,8 @@ void init_revisions(struct rev_info *revs, const char *prefix)
 	}
 
 	revs->notes_opt.use_default_notes = -1;
+
+	strbuf_init(&revs->last_commit_header, 0);
 }
 
 static void add_pending_commit_list(struct rev_info *revs,
@@ -1844,6 +1846,8 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
 		revs->verbose_header = 1;
 		revs->pretty_given = 1;
 		get_commit_format(arg+9, revs);
+	} else if (skip_prefix(arg, "--commit-header=", &arg)) {
+		revs->commit_header = arg;
 	} else if (!strcmp(arg, "--expand-tabs")) {
 		revs->expand_tabs_in_log = 8;
 	} else if (!strcmp(arg, "--no-expand-tabs")) {
diff --git a/revision.h b/revision.h
index 9fac1a6..39ec092 100644
--- a/revision.h
+++ b/revision.h
@@ -170,6 +170,9 @@ struct rev_info {
 	int		show_log_size;
 	struct string_list *mailmap;
 
+	const char *commit_header;
+	struct strbuf last_commit_header;
+
 	/* Filter by commit log message */
 	struct grep_opt	grep_filter;
 	/* Negate the match of grep_filter */
-- 
2.10.0.566.g5365f87
Previous: Jeff KingNext: Junio C Hamano
Message 13 of 20 in “git log --oneline alternative with dates, times and initials”
  1. git log --oneline alternative with dates, times and initialsKyle J. McKay, Sep 29, 2016
  2. Jeff KingSep 29, 2016
  3. 1/5 pretty: allow formatting DATE_SHORTJeff King, Sep 29, 2016
  4. Jacob KellerSep 30, 2016
  5. SZEDER GáborSep 30, 2016
  6. Jacob KellerSep 30, 2016
  7. Jeff KingOct 1, 2016
  8. 3/5 graph: fix extra spaces in graph_padding_lineJeff King, Sep 29, 2016
  9. 4/5 graph: helper functions for printing commit headerJeff King, Sep 29, 2016
  10. 2/5 pretty: allow formatting names as initialsJeff King, Sep 29, 2016
  11. Junio C HamanoSep 29, 2016
  12. Jeff KingSep 29, 2016
  13. 5/5 log: add --commit-header optionJeff King, Sep 29, 2016
  14. Junio C HamanoSep 29, 2016
  15. Jeff KingSep 29, 2016
  16. Kyle J. McKaySep 29, 2016
  17. Jeff KingSep 29, 2016
  18. Junio C HamanoSep 29, 2016
  19. Jeff KingSep 29, 2016
  20. Junio C HamanoSep 29, 2016

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.