]> git.ipfire.org Git - thirdparty/git.git/commitdiff
log: add %S option (like --source) to log --format
authorIssac Trotts <issactrotts@google.com>
Fri, 11 Jan 2019 06:30:46 +0000 (22:30 -0800)
committerJunio C Hamano <gitster@pobox.com>
Fri, 11 Jan 2019 18:28:11 +0000 (10:28 -0800)
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>
Documentation/pretty-formats.txt
builtin/log.c
log-tree.c
pretty.c
pretty.h
t/t4205-log-pretty-formats.sh
t/t6006-rev-list-format.sh

index 417b638cd803e6cf3b106f3bd7333ba4f72d800a..de6953108cbc5324b9f6e9dacf23482aa698f45a 100644 (file)
@@ -134,6 +134,8 @@ The placeholders are:
 - '%cI': committer date, strict ISO 8601 format
 - '%d': ref names, like the --decorate option of linkgit:git-log[1]
 - '%D': ref names without the " (", ")" wrapping.
+- '%S': ref name given on the command line by which the commit was reached
+  (like `git log --source`), only works with `git log`
 - '%e': encoding
 - '%s': subject
 - '%f': sanitized subject line, suitable for a filename
index e8e51068bd903c01e12207637d59d80ea940bf2d..9deff32b89a2c5ec9290f5bba355b0ad8b4a69d6 100644 (file)
@@ -203,7 +203,7 @@ static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,
            rev->diffopt.filter || rev->diffopt.flags.follow_renames)
                rev->always_show_header = 0;
 
-       if (source) {
+       if (source || w.source) {
                init_revision_sources(&revision_sources);
                rev->sources = &revision_sources;
        }
index 10680c139eeb530bc0e70e0c1713767b6ca5be21..3cb14256ec5b7a2712e23b8feb12b1a91328bc34 100644 (file)
@@ -700,6 +700,7 @@ void show_log(struct rev_info *opt)
        ctx.color = opt->diffopt.use_color;
        ctx.expand_tabs_in_log = opt->expand_tabs_in_log;
        ctx.output_encoding = get_log_output_encoding();
+       ctx.rev = opt;
        if (opt->from_ident.mail_begin && opt->from_ident.name_begin)
                ctx.from_ident = &opt->from_ident;
        if (opt->graph)
index b83a3ecd2331af7f2e7e03bd76336cde3c2a6dfc..1b453fbb59a9414f947ad4f2253a54e6e9ad68f0 100644 (file)
--- a/pretty.c
+++ b/pretty.c
@@ -1084,6 +1084,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
        struct commit_list *p;
        const char *arg;
        int ch;
+       char **slot;
 
        /* these are independent of the commit */
        switch (placeholder[0]) {
@@ -1194,6 +1195,14 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */
                load_ref_decorations(NULL, DECORATE_SHORT_REFS);
                format_decorations_extended(sb, commit, c->auto_color, "", ", ", "");
                return 1;
+       case 'S':               /* tag/branch like --source */
+               if (!(c->pretty_ctx->rev && c->pretty_ctx->rev->sources))
+                       return 0;
+               slot = revision_sources_at(c->pretty_ctx->rev->sources, commit);
+               if (!(slot && *slot))
+                       return 0;
+               strbuf_addstr(sb, *slot);
+               return 1;
        case 'g':               /* reflog info */
                switch(placeholder[1]) {
                case 'd':       /* reflog selector */
@@ -1498,6 +1507,9 @@ static size_t userformat_want_item(struct strbuf *sb, const char *placeholder,
        case 'N':
                w->notes = 1;
                break;
+       case 'S':
+               w->source = 1;
+               break;
        }
        return 0;
 }
index 7359d318a92c167e0f36eabf73fdd1142f0e9031..87ca5dfcbad73fca39e5a8f346bb2cc36996eb80 100644 (file)
--- a/pretty.h
+++ b/pretty.h
@@ -60,6 +60,7 @@ static inline int cmit_fmt_is_mail(enum cmit_fmt fmt)
 
 struct userformat_want {
        unsigned notes:1;
+       unsigned source:1;
 };
 
 /* Set the flag "w->notes" if there is placeholder %N in "fmt". */
index 978a8a66ff05055ad1967b5cc25f36880230a5e4..7df8c3d4ec04ef86e12277e5debfeb8f8f825521 100755 (executable)
@@ -621,4 +621,54 @@ test_expect_success 'trailer parsing not fooled by --- line' '
        test_cmp expect actual
 '
 
+test_expect_success 'set up %S tests' '
+       git checkout --orphan source-a &&
+       test_commit one &&
+       test_commit two &&
+       git checkout -b source-b HEAD^ &&
+       test_commit three
+'
+
+test_expect_success 'log --format=%S paints branch names' '
+       cat >expect <<-\EOF &&
+       source-b
+       source-a
+       source-b
+       EOF
+       git log --format=%S source-a source-b >actual &&
+       test_cmp expect actual
+'
+
+test_expect_success 'log --format=%S paints tag names' '
+       git tag -m tagged source-tag &&
+       cat >expect <<-\EOF &&
+       source-tag
+       source-a
+       source-tag
+       EOF
+       git log --format=%S source-tag source-a >actual &&
+       test_cmp expect actual
+'
+
+test_expect_success 'log --format=%S paints symmetric ranges' '
+       cat >expect <<-\EOF &&
+       source-b
+       source-a
+       EOF
+       git log --format=%S source-a...source-b >actual &&
+       test_cmp expect actual
+'
+
+test_expect_success '%S in git log --format works with other placeholders (part 1)' '
+       git log --format="source-b %h" source-b >expect &&
+       git log --format="%S %h" source-b >actual &&
+       test_cmp expect actual
+'
+
+test_expect_success '%S in git log --format works with other placeholders (part 2)' '
+       git log --format="%h source-b" source-b >expect &&
+       git log --format="%h %S" source-b >actual &&
+       test_cmp expect actual
+'
+
 test_done
index ec42c2f7797c38f39bb3420402d975a94217d1f4..da113d975b160f3c34a4acda3d82a26d65f5268e 100755 (executable)
@@ -185,6 +185,10 @@ test_expect_success 'basic colors' '
        test_cmp expect actual
 '
 
+test_expect_success '%S is not a placeholder for rev-list yet' '
+       git rev-list --format="%S" -1 master | grep "%S"
+'
+
 test_expect_success 'advanced colors' '
        cat >expect <<-EOF &&
        commit $head2