log: add %S option (like --source) to log --format
PATCH 1/1 log: add %S option (like --source) to log --format
From: Issac Trotts <[email protected]>
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.
---
This change is based on a question from Stack Overflow:
https://stackoverflow.com/questions/12712775/git-get-source-information-in-format
--- Documentation/pretty-formats.txt | 2 ++ builtin/log.c | 2 +- log-tree.c | 1 + pretty.c | 14 +++++++++ pretty.h | 1 + t/t4205-log-pretty-formats.sh | 50 ++++++++++++++++++++++++++++++++ t/t6006-rev-list-format.sh | 4 +++ 7 files changed, 73 insertions(+), 1 deletion(-)
Documentation/pretty-formats.txt +2 -0
@@ -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
builtin/log.c +1 -1
@@ -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; }
log-tree.c +1 -0
@@ -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)
pretty.c +14 -0
@@ -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,16 @@ 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 ==NULL || c->pretty_ctx->rev->sources ==NULL ) { +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 +1509,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 ; }
pretty.h +1 -0
@@ -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". */
t/t4205-log-pretty-formats.sh +50 -0
54 lines not on this page · raw message
t/t6006-rev-list-format.sh +4 -0
@@ -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
Signature · 1 line
2.19.1
Quoted 1 line · [email protected] writes:
From: Issac Trotts <[email protected]>
Heh, I'll edit this line to match S-o-b: below.
Quoted 22 lines
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 <[email protected]>---
log-tree.c +0
-0
log-tree.c void show_log(struct rev_info *opt) · line 703
Quoted 4 lines
log-tree.c +1
-0@@ -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;
There are quite a few existing codepaths that change their behaviour based on NULL-ness of ctx.rev field. How would we make sure that this change have no unintended consequence, I wonder?
log-tree.c void show_log(struct rev_info *opt)
Quoted 10 lines
log-tree.c +10
-0+case 'S' :/* tag/branch like --source */ +if (c->pretty_ctx->rev ==NULL || c->pretty_ctx->rev->sources ==NULL ) { +return 0 ; + } + slot =revision_sources_at (c->pretty_ctx->rev->sources, commit); +if (!(slot && *slot)) { +return 0 ; + } +strbuf_addstr (sb, *slot); +return 1 ;
Let's update the style of this hunk here like so:
if (!c->pretty_ctx->rev || !c->pretty_ctx->rev->sources) return 0; slot = ...; if (!(slot && *slot)) return 0; strbuf_addstr(...); return 1;
I wonder if it is even better to apply de-Morgan to one of the above two, i.e.
if (!(c->pretty_ctx->rev && c->pretty_ctx->rev->sources)) return 0;
Anyway, thanks. Will queue.
Quoted 6 lines · Junio C Hamano
Quoted 1 line · [email protected] writes:
From: Issac Trotts <[email protected]>
Heh, I'll edit this line to match S-o-b: below.
Thanks. Otherwise I have to set up git send-email again on my work laptop.
Quoted 43 lines
Quoted 21 lines
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 <[email protected]>---
log-tree.c +0
-0log-tree.c void show_log(struct rev_info *opt) · line 703
Quoted 4 lines
log-tree.c +1
-0@@ -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;There are quite a few existing codepaths that change their behaviour based on NULL-ness of ctx.rev field. How would we make sure that this change have no unintended consequence, I wonder?
log-tree.c void show_log(struct rev_info *opt)
Quoted 10 lines
log-tree.c +10
-0+case 'S' :/* tag/branch like --source */ +if (c->pretty_ctx->rev ==NULL || c->pretty_ctx->rev->sources ==NULL ) { +return 0 ; + } + slot =revision_sources_at (c->pretty_ctx->rev->sources, commit); +if (!(slot && *slot)) { +return 0 ; + } +strbuf_addstr (sb, *slot); +return 1 ;Let's update the style of this hunk here like so:
Quoted 7 lines
if (!c->pretty_ctx->rev || !c->pretty_ctx->rev->sources)
return 0;
slot = ...;
if (!(slot && *slot))
return 0;
strbuf_addstr(...);
return 1;Done.
Quoted 7 lines
I wonder if it is even better to apply de-Morgan to one of the above two, i.e.
if (!(c->pretty_ctx->rev && c->pretty_ctx->rev->sources)) return 0;
Done.
Quoted 3 lines
Anyway, thanks. Will queue.
Sounds good. Btw, did you queue it yet? I didn't see it at the mirror:
https://github.com/git/git/commits/master.
I'll send out another patch with your suggestions, in case you haven't already queued it.
Issac
Quoted 9 lines · Issac Trotts
Quoted 6 lines · Junio C Hamano
Quoted 1 line · [email protected] writes:
From: Issac Trotts <[email protected]>
Heh, I'll edit this line to match S-o-b: below.
Thanks. Otherwise I have to set up git send-email again on my work laptop.
This in-body From: line is either (1) something you would write yourself in your MUA, in which case you can just write your Google address, or (2) the author identity a tool picks up and insert at the body of the message for you, in which case you would make sure that the commit is made under your Google identity (e.g. "commit --amend --author=..."). Either case, I am not sure where the need for send-email comes from.
Quoted 2 lines · Issac Trotts
Sounds good. Btw, did you queue it yet? I didn't see it at the mirror:
https://github.com/git/git/commits/master.
No patch goes to 'master' directly.
Once we see that a patch is in reviewable shape during the mailing list discussion, we wait for a few more days and unless there are more issues to be addressed discovered during that period, it hits the 'next' branch, and spends a week or so before graduating.
Before all of that happens, i.e. when a patch has not proven itself 'next'-worthy, I may pick it up to make trial merges in order to see how it interacts with other proposed updates, which happens in the 'pu' (proposed updates) branch. As there is only limited amount of time in a day, this obviously cannot happen to all patches sent to the list, but I try to cover as much as possible.
You'd find it as 5ca3af27 ("log: add %S option (like --source) to log --format", 2019-01-09) on 'pu'.