{"thread":{"id":"50179","subject":"[PATCH] log: add %S option (like --source) to log --format","startedAt":"2019-01-08T13:21:11Z","lastAt":"2019-01-11T17:47:34Z","messageCount":5,"participants":["issac.trotts@gmail.com","Junio C Hamano","Issac Trotts"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"366327","messageId":"20190108132048.57142-1-issactrotts@google.com","threadId":"50179","inReplyTo":null,"subject":"[PATCH] log: add %S option (like --source) to log --format","fromName":"","fromEmail":"issac.trotts@gmail.com","sentAt":"2019-01-08T13:20:48Z","receivedAt":"2019-01-08T13:21:11Z","isPatch":true,"sender":{"key":"issac.trotts@gmail.com","avatar":"https://gravatar.com/avatar/44dd86aa2ea47c79d9b2a0a99b90e0ff18601855d2f05c6b84d6c432c5906721?d=mp&s=160"},"body":"From: Issac Trotts <issac.trotts@gmail.com>\n\nMake it possible to write for example\n\n        git log --format=\"%H,%S\"\n\nwhere the %S at the end is a new placeholder that prints out the ref\n(tag/branch) for each commit.\n\nUsing %d might seem like an alternative but it only shows the ref for the last\ncommit in the branch.\n\nSigned-off-by: Issac Trotts <issactrotts@google.com>\n\n---\n\nThis change is based on a question from Stack Overflow:\nhttps://stackoverflow.com/questions/12712775/git-get-source-information-in-format\n---\n Documentation/pretty-formats.txt |  2 ++\n builtin/log.c                    |  2 +-\n log-tree.c                       |  1 +\n pretty.c                         | 14 +++++++++\n pretty.h                         |  1 +\n t/t4205-log-pretty-formats.sh    | 50 ++++++++++++++++++++++++++++++++\n t/t6006-rev-list-format.sh       |  4 +++\n 7 files changed, 73 insertions(+), 1 deletion(-)\n\ndiff --git a/Documentation/pretty-formats.txt b/Documentation/pretty-formats.txt\nindex 417b638cd..de6953108 100644\n--- a/Documentation/pretty-formats.txt\n+++ b/Documentation/pretty-formats.txt\n@@ -134,6 +134,8 @@ The placeholders are:\n - '%cI': committer date, strict ISO 8601 format\n - '%d': ref names, like the --decorate option of linkgit:git-log[1]\n - '%D': ref names without the \" (\", \")\" wrapping.\n+- '%S': ref name given on the command line by which the commit was reached\n+  (like `git log --source`), only works with `git log`\n - '%e': encoding\n - '%s': subject\n - '%f': sanitized subject line, suitable for a filename\ndiff --git a/builtin/log.c b/builtin/log.c\nindex e8e51068b..9deff32b8 100644\n--- a/builtin/log.c\n+++ b/builtin/log.c\n@@ -203,7 +203,7 @@ static void cmd_log_init_finish(int argc, const char **argv, const char *prefix,\n \t    rev->diffopt.filter || rev->diffopt.flags.follow_renames)\n \t\trev->always_show_header = 0;\n \n-\tif (source) {\n+\tif (source || w.source) {\n \t\tinit_revision_sources(&revision_sources);\n \t\trev->sources = &revision_sources;\n \t}\ndiff --git a/log-tree.c b/log-tree.c\nindex 10680c139..3cb14256e 100644\n--- a/log-tree.c\n+++ b/log-tree.c\n@@ -700,6 +700,7 @@ void show_log(struct rev_info *opt)\n \tctx.color = opt->diffopt.use_color;\n \tctx.expand_tabs_in_log = opt->expand_tabs_in_log;\n \tctx.output_encoding = get_log_output_encoding();\n+\tctx.rev = opt;\n \tif (opt->from_ident.mail_begin && opt->from_ident.name_begin)\n \t\tctx.from_ident = &opt->from_ident;\n \tif (opt->graph)\ndiff --git a/pretty.c b/pretty.c\nindex b83a3ecd2..06075d625 100644\n--- a/pretty.c\n+++ b/pretty.c\n@@ -1084,6 +1084,7 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */\n \tstruct commit_list *p;\n \tconst char *arg;\n \tint ch;\n+\tchar **slot;\n \n \t/* these are independent of the commit */\n \tswitch (placeholder[0]) {\n@@ -1194,6 +1195,16 @@ static size_t format_commit_one(struct strbuf *sb, /* in UTF-8 */\n \t\tload_ref_decorations(NULL, DECORATE_SHORT_REFS);\n \t\tformat_decorations_extended(sb, commit, c->auto_color, \"\", \", \", \"\");\n \t\treturn 1;\n+\tcase 'S':\t\t/* tag/branch like --source */\n+\t\tif (c->pretty_ctx->rev == NULL || c->pretty_ctx->rev->sources == NULL) {\n+\t\t\treturn 0;\n+\t\t}\n+\t\tslot = revision_sources_at(c->pretty_ctx->rev->sources, commit);\n+\t\tif (!(slot && *slot)) {\n+\t\t\treturn 0;\n+\t\t}\n+\t\tstrbuf_addstr(sb, *slot);\n+\t\treturn 1;\n \tcase 'g':\t\t/* reflog info */\n \t\tswitch(placeholder[1]) {\n \t\tcase 'd':\t/* reflog selector */\n@@ -1498,6 +1509,9 @@ static size_t userformat_want_item(struct strbuf *sb, const char *placeholder,\n \tcase 'N':\n \t\tw->notes = 1;\n \t\tbreak;\n+\tcase 'S':\n+\t\tw->source = 1;\n+\t\tbreak;\n \t}\n \treturn 0;\n }\ndiff --git a/pretty.h b/pretty.h\nindex 7359d318a..87ca5dfcb 100644\n--- a/pretty.h\n+++ b/pretty.h\n@@ -60,6 +60,7 @@ static inline int cmit_fmt_is_mail(enum cmit_fmt fmt)\n \n struct userformat_want {\n \tunsigned notes:1;\n+\tunsigned source:1;\n };\n \n /* Set the flag \"w->notes\" if there is placeholder %N in \"fmt\". */\ndiff --git a/t/t4205-log-pretty-formats.sh b/t/t4205-log-pretty-formats.sh\nindex 978a8a66f..7df8c3d4e 100755\n--- a/t/t4205-log-pretty-formats.sh\n+++ b/t/t4205-log-pretty-formats.sh\n@@ -621,4 +621,54 @@ test_expect_success 'trailer parsing not fooled by --- line' '\n \ttest_cmp expect actual\n '\n \n+test_expect_success 'set up %S tests' '\n+\tgit checkout --orphan source-a &&\n+\ttest_commit one &&\n+\ttest_commit two &&\n+\tgit checkout -b source-b HEAD^ &&\n+\ttest_commit three\n+'\n+\n+test_expect_success 'log --format=%S paints branch names' '\n+\tcat >expect <<-\\EOF &&\n+\tsource-b\n+\tsource-a\n+\tsource-b\n+\tEOF\n+\tgit log --format=%S source-a source-b >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'log --format=%S paints tag names' '\n+\tgit tag -m tagged source-tag &&\n+\tcat >expect <<-\\EOF &&\n+\tsource-tag\n+\tsource-a\n+\tsource-tag\n+\tEOF\n+\tgit log --format=%S source-tag source-a >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success 'log --format=%S paints symmetric ranges' '\n+\tcat >expect <<-\\EOF &&\n+\tsource-b\n+\tsource-a\n+\tEOF\n+\tgit log --format=%S source-a...source-b >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success '%S in git log --format works with other placeholders (part 1)' '\n+\tgit log --format=\"source-b %h\" source-b >expect &&\n+\tgit log --format=\"%S %h\" source-b >actual &&\n+\ttest_cmp expect actual\n+'\n+\n+test_expect_success '%S in git log --format works with other placeholders (part 2)' '\n+\tgit log --format=\"%h source-b\" source-b >expect &&\n+\tgit log --format=\"%h %S\" source-b >actual &&\n+\ttest_cmp expect actual\n+'\n+\n test_done\ndiff --git a/t/t6006-rev-list-format.sh b/t/t6006-rev-list-format.sh\nindex ec42c2f77..da113d975 100755\n--- a/t/t6006-rev-list-format.sh\n+++ b/t/t6006-rev-list-format.sh\n@@ -185,6 +185,10 @@ test_expect_success 'basic colors' '\n \ttest_cmp expect actual\n '\n \n+test_expect_success '%S is not a placeholder for rev-list yet' '\n+\tgit rev-list --format=\"%S\" -1 master | grep \"%S\"\n+'\n+\n test_expect_success 'advanced colors' '\n \tcat >expect <<-EOF &&\n \tcommit $head2\n-- \n2.19.1\n\n"},{"id":"366381","messageId":"xmqqmuoa4wp7.fsf@gitster-ct.c.googlers.com","threadId":"50179","inReplyTo":"20190108132048.57142-1-issactrotts@google.com","subject":"Re: [PATCH] log: add %S option (like --source) to log --format","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2019-01-08T22:21:40Z","receivedAt":"2019-01-08T22:21:45Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"issac.trotts@gmail.com writes:\n\n> From: Issac Trotts <issac.trotts@gmail.com>\n\nHeh, I'll edit this line to match S-o-b: below.\n\n>\n> Make it possible to write for example\n>\n>         git log --format=\"%H,%S\"\n>\n> where the %S at the end is a new placeholder that prints out the ref\n> (tag/branch) for each commit.\n>\n> Using %d might seem like an alternative but it only shows the ref for the last\n> commit in the branch.\n>\n> Signed-off-by: Issac Trotts <issactrotts@google.com>\n>\n> ---\n\n\n\n> diff --git a/log-tree.c b/log-tree.c\n> index 10680c139..3cb14256e 100644\n> --- a/log-tree.c\n> +++ b/log-tree.c\n> @@ -700,6 +700,7 @@ void show_log(struct rev_info *opt)\n>  \tctx.color = opt->diffopt.use_color;\n>  \tctx.expand_tabs_in_log = opt->expand_tabs_in_log;\n>  \tctx.output_encoding = get_log_output_encoding();\n> +\tctx.rev = opt;\n\nThere are quite a few existing codepaths that change their behaviour\nbased on NULL-ness of ctx.rev field.  How would we make sure that\nthis change have no unintended consequence, I wonder?\n\n> +\tcase 'S':\t\t/* tag/branch like --source */\n> +\t\tif (c->pretty_ctx->rev == NULL || c->pretty_ctx->rev->sources == NULL) {\n> +\t\t\treturn 0;\n> +\t\t}\n> +\t\tslot = revision_sources_at(c->pretty_ctx->rev->sources, commit);\n> +\t\tif (!(slot && *slot)) {\n> +\t\t\treturn 0;\n> +\t\t}\n> +\t\tstrbuf_addstr(sb, *slot);\n> +\t\treturn 1;\n\nLet's update the style of this hunk here like so:\n\n\tif (!c->pretty_ctx->rev || !c->pretty_ctx->rev->sources)\n\t\treturn 0;\n\tslot = ...;\n\tif (!(slot && *slot))\n\t\treturn 0;\n\tstrbuf_addstr(...);\n\treturn 1;\n\nI wonder if it is even better to apply de-Morgan to one of the above\ntwo, i.e.\n\n\tif (!(c->pretty_ctx->rev && c->pretty_ctx->rev->sources))\n\t\treturn 0;\n\n\nAnyway, thanks.  Will queue.\n\n"},{"id":"366553","messageId":"CANdyxMzDevigyMTCixjV7bfa-UZ1vknwxt0X=RGc6LZz7F4UXQ@mail.gmail.com","threadId":"50179","inReplyTo":"xmqqmuoa4wp7.fsf@gitster-ct.c.googlers.com","subject":"Re: [PATCH] log: add %S option (like --source) to log --format","fromName":"Issac Trotts","fromEmail":"issac.trotts@gmail.com","sentAt":"2019-01-11T06:28:41Z","receivedAt":"2019-01-11T06:28:56Z","isPatch":true,"sender":{"key":"issac.trotts@gmail.com","avatar":"https://gravatar.com/avatar/44dd86aa2ea47c79d9b2a0a99b90e0ff18601855d2f05c6b84d6c432c5906721?d=mp&s=160"},"body":"On Tue, Jan 8, 2019 at 2:21 PM Junio C Hamano <gitster@pobox.com> wrote:\n>\n> issac.trotts@gmail.com writes:\n>\n> > From: Issac Trotts <issac.trotts@gmail.com>\n>\n> Heh, I'll edit this line to match S-o-b: below.\n\nThanks. Otherwise I have to set up git send-email again on my work laptop.\n\n> > Make it possible to write for example\n> >\n> >         git log --format=\"%H,%S\"\n> >\n> > where the %S at the end is a new placeholder that prints out the ref\n> > (tag/branch) for each commit.\n> >\n> > Using %d might seem like an alternative but it only shows the ref for the last\n> > commit in the branch.\n> >\n> > Signed-off-by: Issac Trotts <issactrotts@google.com>\n> >\n> > ---\n>\n>\n>\n> > diff --git a/log-tree.c b/log-tree.c\n> > index 10680c139..3cb14256e 100644\n> > --- a/log-tree.c\n> > +++ b/log-tree.c\n> > @@ -700,6 +700,7 @@ void show_log(struct rev_info *opt)\n> >       ctx.color = opt->diffopt.use_color;\n> >       ctx.expand_tabs_in_log = opt->expand_tabs_in_log;\n> >       ctx.output_encoding = get_log_output_encoding();\n> > +     ctx.rev = opt;\n>\n> There are quite a few existing codepaths that change their behaviour\n> based on NULL-ness of ctx.rev field.  How would we make sure that\n> this change have no unintended consequence, I wonder?\n>\n> > +     case 'S':               /* tag/branch like --source */\n> > +             if (c->pretty_ctx->rev == NULL || c->pretty_ctx->rev->sources == NULL) {\n> > +                     return 0;\n> > +             }\n> > +             slot = revision_sources_at(c->pretty_ctx->rev->sources, commit);\n> > +             if (!(slot && *slot)) {\n> > +                     return 0;\n> > +             }\n> > +             strbuf_addstr(sb, *slot);\n> > +             return 1;\n>\n> Let's update the style of this hunk here like so:\n>\n>         if (!c->pretty_ctx->rev || !c->pretty_ctx->rev->sources)\n>                 return 0;\n>         slot = ...;\n>         if (!(slot && *slot))\n>                 return 0;\n>         strbuf_addstr(...);\n>         return 1;\n\nDone.\n\n>\n> I wonder if it is even better to apply de-Morgan to one of the above\n> two, i.e.\n>\n>         if (!(c->pretty_ctx->rev && c->pretty_ctx->rev->sources))\n>                 return 0;\n>\n\nDone.\n\n>\n> Anyway, thanks.  Will queue.\n>\n\nSounds good. Btw, did you queue it yet? I didn't see it at the mirror:\nhttps://github.com/git/git/commits/master.\n\nI'll send out another patch with your suggestions, in case you haven't\nalready queued it.\n\nIssac\n"},{"id":"366565","messageId":"xmqqh8efyu28.fsf@gitster-ct.c.googlers.com","threadId":"50179","inReplyTo":"CANdyxMzDevigyMTCixjV7bfa-UZ1vknwxt0X=RGc6LZz7F4UXQ@mail.gmail.com","subject":"Re: [PATCH] log: add %S option (like --source) to log --format","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2019-01-11T17:37:19Z","receivedAt":"2019-01-11T17:37:24Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Issac Trotts <issac.trotts@gmail.com> writes:\n\n> On Tue, Jan 8, 2019 at 2:21 PM Junio C Hamano <gitster@pobox.com> wrote:\n>>\n>> issac.trotts@gmail.com writes:\n>>\n>> > From: Issac Trotts <issac.trotts@gmail.com>\n>>\n>> Heh, I'll edit this line to match S-o-b: below.\n>\n> Thanks. Otherwise I have to set up git send-email again on my work laptop.\n\nThis in-body From: line is either (1) something you would write\nyourself in your MUA, in which case you can just write your Google\naddress, or (2) the author identity a tool picks up and insert at\nthe body of the message for you, in which case you would make sure\nthat the commit is made under your Google identity (e.g. \"commit\n--amend --author=...\").  Either case, I am not sure where the need\nfor send-email comes from.\n\n"},{"id":"366566","messageId":"xmqqd0p3ytlb.fsf@gitster-ct.c.googlers.com","threadId":"50179","inReplyTo":"CANdyxMzDevigyMTCixjV7bfa-UZ1vknwxt0X=RGc6LZz7F4UXQ@mail.gmail.com","subject":"Re: [PATCH] log: add %S option (like --source) to log --format","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2019-01-11T17:47:28Z","receivedAt":"2019-01-11T17:47:34Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Issac Trotts <issac.trotts@gmail.com> writes:\n\n> Sounds good. Btw, did you queue it yet? I didn't see it at the mirror:\n> https://github.com/git/git/commits/master.\n\nNo patch goes to 'master' directly.  \n\nOnce we see that a patch is in reviewable shape during the mailing\nlist discussion, we wait for a few more days and unless there are\nmore issues to be addressed discovered during that period, it hits\nthe 'next' branch, and spends a week or so before graduating.\n\nBefore all of that happens, i.e. when a patch has not proven itself\n'next'-worthy, I may pick it up to make trial merges in order to see\nhow it interacts with other proposed updates, which happens in the\n'pu' (proposed updates) branch.  As there is only limited amount of\ntime in a day, this obviously cannot happen to all patches sent to\nthe list, but I try to cover as much as possible.\n\nYou'd find it as 5ca3af27 (\"log: add %S option (like --source) to\nlog --format\", 2019-01-09) on 'pu'.\n\n\n"}]}