Skip to content

Commit

Permalink
log: add --commit-header option
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
peff committed Nov 22, 2024
1 parent 2507ab6 commit 60e68ba
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
31 changes: 31 additions & 0 deletions log-tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,28 @@ static int show_mergetag(struct rev_info *opt, struct commit *commit)
return 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;

repo_format_commit_message(the_repository, 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);
}

static void next_commentary_block(struct rev_info *opt, struct strbuf *sb)
{
const char *x = opt->shown_dashes ? "\n" : "---\n";
Expand Down Expand Up @@ -792,6 +814,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
Expand Down
3 changes: 3 additions & 0 deletions revision.c
Original file line number Diff line number Diff line change
Expand Up @@ -1984,6 +1984,7 @@ void repo_init_revisions(struct repository *r,
list_objects_filter_init(&revs->filter);
init_ref_exclusions(&revs->ref_excludes);
oidset_init(&revs->missing_commits, 0);
strbuf_init(&revs->last_commit_header, 0);
}

static void add_pending_commit_list(struct rev_info *revs,
Expand Down Expand Up @@ -2563,6 +2564,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(optarg, 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")) {
Expand Down
3 changes: 3 additions & 0 deletions revision.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,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;

Expand Down

0 comments on commit 60e68ba

Please sign in to comment.