Skip to content

Commit 46ed407

Browse files
committed
log: add --commit-header option
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 <[email protected]>
1 parent 7ef9de3 commit 46ed407

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

log-tree.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,28 @@ static int show_mergetag(struct rev_info *opt, struct commit *commit)
681681
return for_each_mergetag(show_one_mergetag, commit, opt);
682682
}
683683

684+
static void show_commit_header(struct rev_info *opt,
685+
struct pretty_print_context *pp,
686+
struct commit *commit)
687+
{
688+
struct strbuf out = STRBUF_INIT;
689+
690+
repo_format_commit_message(the_repository, commit, opt->commit_header, &out, pp);
691+
strbuf_complete_line(&out);
692+
693+
if (!strbuf_cmp(&out, &opt->last_commit_header)) {
694+
strbuf_release(&out);
695+
return;
696+
}
697+
698+
graph_show_precommit(opt->graph);
699+
graph_show_padding(opt->graph);
700+
fwrite(out.buf, 1, out.len, opt->diffopt.file);
701+
702+
strbuf_swap(&out, &opt->last_commit_header);
703+
strbuf_release(&out);
704+
}
705+
684706
static void next_commentary_block(struct rev_info *opt, struct strbuf *sb)
685707
{
686708
const char *x = opt->shown_dashes ? "\n" : "---\n";
@@ -793,6 +815,15 @@ void show_log(struct rev_info *opt)
793815
}
794816
opt->shown_one = 1;
795817

818+
if (opt->commit_header) {
819+
/*
820+
* XXX probably the initialization of the pretty ctx from "opt"
821+
* below should happen sooner so we can use it.
822+
*/
823+
ctx.color = opt->diffopt.use_color;
824+
show_commit_header(opt, &ctx, commit);
825+
}
826+
796827
/*
797828
* If the history graph was requested,
798829
* print the graph, up to this commit's line

revision.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1980,6 +1980,7 @@ void repo_init_revisions(struct repository *r,
19801980
list_objects_filter_init(&revs->filter);
19811981
init_ref_exclusions(&revs->ref_excludes);
19821982
oidset_init(&revs->missing_commits, 0);
1983+
strbuf_init(&revs->last_commit_header, 0);
19831984
}
19841985

19851986
static void add_pending_commit_list(struct rev_info *revs,
@@ -2573,6 +2574,8 @@ static int handle_revision_opt(struct rev_info *revs, int argc, const char **arg
25732574
revs->verbose_header = 1;
25742575
revs->pretty_given = 1;
25752576
get_commit_format(optarg, revs);
2577+
} else if (skip_prefix(arg, "--commit-header=", &arg)) {
2578+
revs->commit_header = arg;
25762579
} else if (!strcmp(arg, "--expand-tabs")) {
25772580
revs->expand_tabs_in_log = 8;
25782581
} else if (!strcmp(arg, "--no-expand-tabs")) {

revision.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,9 @@ struct rev_info {
296296
int show_log_size;
297297
struct string_list *mailmap;
298298

299+
const char *commit_header;
300+
struct strbuf last_commit_header;
301+
299302
/* Filter by commit log message */
300303
struct grep_opt grep_filter;
301304

0 commit comments

Comments
 (0)