log: add %S option (like --source) to log --format

Make it possible to write for example git log --format="%H,%S" where the %S at the end is a new placeholder that prints out the ref (tag/branch) for each commit. Using %d might seem like an alternative but it only shows the ref for the last commit in the branch. Signed-off-by: Issac Trotts <issactrotts@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Issac Trotts committed Jan 10, 2019 at 22:30 UTC ad6f028f067673cadadbc2219fcb0bb864300a6c
7 files changed +71 -1
Documentation/pretty-formats.txt
+2
@@ -134,6 +134,8 @@ The placeholders are:
134 - '%cI': committer date, strict ISO 8601 format
135 - '%d': ref names, like the --decorate option of linkgit:git-log[1]
136 - '%D': ref names without the " (", ")" wrapping.
137 +- '%S': ref name given on the command line by which the commit was reached
138 + (like `git log --source`), only works with `git log`
139 - '%e': encoding
140 - '%s': subject
141 - '%f': sanitized subject line, suitable for a filename
builtin/log.c
+1 -1
@@ -203,7 +203,7 @@ static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,
203 rev->diffopt.filter || rev->diffopt.flags.follow_renames)
204 rev->always_show_header = 0;
205
206 - if (source) {
206 + if (source || w.source) {
207 init_revision_sources(&revision_sources);
208 rev->sources = &revision_sources;
209 }
log-tree.c
+1
@@ -700,6 +700,7 @@ void show_log(struct rev_info *opt)
700 ctx.color = opt->diffopt.use_color;
701 ctx.expand_tabs_in_log = opt->expand_tabs_in_log;
702 ctx.output_encoding = get_log_output_encoding();
703 + ctx.rev = opt;
704 if (opt->from_ident.mail_begin && opt->from_ident.name_begin)
705 ctx.from_ident = &opt->from_ident;
706 if (opt->graph)
pretty.c
+12
@@ -1084,6 +1084,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
1084 struct commit_list *p;
1085 const char *arg;
1086 int ch;
1087 + char **slot;
1088
1089 /* these are independent of the commit */
1090 switch (placeholder[0]) {
@@ -1194,6 +1195,14 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
1195 load_ref_decorations(NULL, DECORATE_SHORT_REFS);
1196 format_decorations_extended(sb, commit, c->auto_color, "", ", ", "");
1197 return 1;
1198 + case 'S': /* tag/branch like --source */
1199 + if (!(c->pretty_ctx->rev && c->pretty_ctx->rev->sources))
1200 + return 0;
1201 + slot = revision_sources_at(c->pretty_ctx->rev->sources, commit);
1202 + if (!(slot && *slot))
1203 + return 0;
1204 + strbuf_addstr(sb, *slot);
1205 + return 1;
1206 case 'g': /* reflog info */
1207 switch(placeholder[1]) {
1208 case 'd': /* reflog selector */
@@ -1498,6 +1507,9 @@ static size_t userformat_want_item(struct strbuf *sb, const char *placeholder,
1507 case 'N':
1508 w->notes = 1;
1509 break;
1510 + case 'S':
1511 + w->source = 1;
1512 + break;
1513 }
1514 return 0;
1515 }
pretty.h
+1
@@ -60,6 +60,7 @@ static inline int cmit_fmt_is_mail(enum cmit_fmt fmt)
60
61 struct userformat_want {
62 unsigned notes:1;
63 + unsigned source:1;
64 };
65
66 /* Set the flag "w->notes" if there is placeholder %N in "fmt". */
t/t4205-log-pretty-formats.sh
+50
@@ -621,4 +621,54 @@ test_expect_success 'trailer parsing not fooled by --- line' '
621 test_cmp expect actual
622 '
623
624 +test_expect_success 'set up %S tests' '
625 + git checkout --orphan source-a &&
626 + test_commit one &&
627 + test_commit two &&
628 + git checkout -b source-b HEAD^ &&
629 + test_commit three
630 +'
631 +
632 +test_expect_success 'log --format=%S paints branch names' '
633 + cat >expect <<-\EOF &&
634 + source-b
635 + source-a
636 + source-b
637 + EOF
638 + git log --format=%S source-a source-b >actual &&
639 + test_cmp expect actual
640 +'
641 +
642 +test_expect_success 'log --format=%S paints tag names' '
643 + git tag -m tagged source-tag &&
644 + cat >expect <<-\EOF &&
645 + source-tag
646 + source-a
647 + source-tag
648 + EOF
649 + git log --format=%S source-tag source-a >actual &&
650 + test_cmp expect actual
651 +'
652 +
653 +test_expect_success 'log --format=%S paints symmetric ranges' '
654 + cat >expect <<-\EOF &&
655 + source-b
656 + source-a
657 + EOF
658 + git log --format=%S source-a...source-b >actual &&
659 + test_cmp expect actual
660 +'
661 +
662 +test_expect_success '%S in git log --format works with other placeholders (part 1)' '
663 + git log --format="source-b %h" source-b >expect &&
664 + git log --format="%S %h" source-b >actual &&
665 + test_cmp expect actual
666 +'
667 +
668 +test_expect_success '%S in git log --format works with other placeholders (part 2)' '
669 + git log --format="%h source-b" source-b >expect &&
670 + git log --format="%h %S" source-b >actual &&
671 + test_cmp expect actual
672 +'
673 +
674 test_done
t/t6006-rev-list-format.sh
+4
@@ -185,6 +185,10 @@ test_expect_success 'basic colors' '
185 test_cmp expect actual
186 '
187
188 +test_expect_success '%S is not a placeholder for rev-list yet' '
189 + git rev-list --format="%S" -1 master | grep "%S"
190 +'
191 +
192 test_expect_success 'advanced colors' '
193 cat >expect <<-EOF &&
194 commit $head2