Skip to content

Commit a562a11

Browse files
committed
Merge branch 'it/log-format-source'
Custom userformat "log --format" learned %S atom that stands for the tip the traversal reached the commit from, i.e. --source. * it/log-format-source: log: add %S option (like --source) to log --format
2 parents 7fa92ba + ad6f028 commit a562a11

File tree

7 files changed

+71
-1
lines changed

7 files changed

+71
-1
lines changed

Documentation/pretty-formats.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ The placeholders are:
134134
- '%cI': committer date, strict ISO 8601 format
135135
- '%d': ref names, like the --decorate option of linkgit:git-log[1]
136136
- '%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`
137139
- '%e': encoding
138140
- '%s': subject
139141
- '%f': sanitized subject line, suitable for a filename

builtin/log.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,
203203
rev->diffopt.filter || rev->diffopt.flags.follow_renames)
204204
rev->always_show_header = 0;
205205

206-
if (source) {
206+
if (source || w.source) {
207207
init_revision_sources(&revision_sources);
208208
rev->sources = &revision_sources;
209209
}

log-tree.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,7 @@ void show_log(struct rev_info *opt)
700700
ctx.color = opt->diffopt.use_color;
701701
ctx.expand_tabs_in_log = opt->expand_tabs_in_log;
702702
ctx.output_encoding = get_log_output_encoding();
703+
ctx.rev = opt;
703704
if (opt->from_ident.mail_begin && opt->from_ident.name_begin)
704705
ctx.from_ident = &opt->from_ident;
705706
if (opt->graph)

pretty.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
10841084
struct commit_list *p;
10851085
const char *arg;
10861086
int ch;
1087+
char **slot;
10871088

10881089
/* these are independent of the commit */
10891090
switch (placeholder[0]) {
@@ -1194,6 +1195,14 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
11941195
load_ref_decorations(NULL, DECORATE_SHORT_REFS);
11951196
format_decorations_extended(sb, commit, c->auto_color, "", ", ", "");
11961197
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;
11971206
case 'g': /* reflog info */
11981207
switch(placeholder[1]) {
11991208
case 'd': /* reflog selector */
@@ -1498,6 +1507,9 @@ static size_t userformat_want_item(struct strbuf *sb, const char *placeholder,
14981507
case 'N':
14991508
w->notes = 1;
15001509
break;
1510+
case 'S':
1511+
w->source = 1;
1512+
break;
15011513
}
15021514
return 0;
15031515
}

pretty.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ static inline int cmit_fmt_is_mail(enum cmit_fmt fmt)
6060

6161
struct userformat_want {
6262
unsigned notes:1;
63+
unsigned source:1;
6364
};
6465

6566
/* Set the flag "w->notes" if there is placeholder %N in "fmt". */

t/t4205-log-pretty-formats.sh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,4 +621,54 @@ test_expect_success 'trailer parsing not fooled by --- line' '
621621
test_cmp expect actual
622622
'
623623

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+
624674
test_done

t/t6006-rev-list-format.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ test_expect_success 'basic colors' '
185185
test_cmp expect actual
186186
'
187187

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+
188192
test_expect_success 'advanced colors' '
189193
cat >expect <<-EOF &&
190194
commit $head2

0 commit comments

Comments
 (0)