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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxx>
---
 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



[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]