reset --hard: make use of the pretty machinery
reset --hard currently uses its own logic for printing the first line of the commit message in its output. Instead of just using the first line, use the pretty machinery to create the output.
In addition to the easier to follow code, this makes the output more consistent with other commands that print the title of the commit, such as 'git commit --oneline' or 'git checkout', which both use 'pp_commit_easy()' with the CMIT_FMT_ONELINE modifier.
It is a slight change of the output if the second line of the commit message is not a blank line, i.e. if the commit message is
foo
bar
previously we would print "HEAD is now at 000000 foo", while after this change we print "HEAD is now at 000000 foo bar", same as 'git log --oneline' shows "000000 foo bar".
So this does make the output more consistent with other commands, and 'reset' is a porcelain command, so nobody should be parsing the output in scripts.
The current behaviour dates back to 0e5a7faa3a ("Make "git reset" a builtin.", 2007-09-11), so I assume (without digging into the old codebase too much) that the logic was implemented because there was no convenience function such as 'pp_commit_easy' that would do this already.
---
Sending this as RFC/PATCH, as I'm not 100% sure this change in behaviour is acceptable, and that I'm not missing some other edge case, but I noticed this while trying to find out how this message gets constructed, and just using the pretty machinery seems much simpler, and more consisent
builtin/reset.c | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-)
builtin/reset.c +10 -18
@@ -106,24 +106,16 @@ static int reset_index(const struct object_id *oid, int reset_type, int quiet)static void print_new_head_line (struct commit *commit) { -const char *hex, *body; -const char *msg; - - hex =find_unique_abbrev (commit->object.oid.hash,DEFAULT_ABBREV ); -printf (_ ("HEAD is now at %s" ), hex); - msg =logmsg_reencode (commit,NULL ,get_log_output_encoding ()); - body =strstr (msg,"\n\n" ); -if (body) { -const char *eol; -size_t len; - body =skip_blank_lines (body +2 ); - eol =strchr (body,'\n' ); - len = eol ? eol - body :strlen (body); -printf (" %.*s\n" , (int ) len, body); - } -else -printf ("\n" ); -unuse_commit_buffer (commit, msg); +struct strbuf buf =STRBUF_INIT ; + +printf (_ ("HEAD is now at %s" ), +find_unique_abbrev (commit->object.oid.hash,DEFAULT_ABBREV )); + +pp_commit_easy (CMIT_FMT_ONELINE , commit, &buf); +if (buf.len >0 ) +printf (" %s" , buf.buf); +putchar ('\n' ); +strbuf_release (&buf); } static void update_index_from_diff(struct diff_queue_struct *q,
Signature · 1 line
2.16.1.101.gde0f0111ea
Quoted 6 lines · Thomas Gummerer
It is a slight change of the output if the second line of the commit message is not a blank line, i.e. if the commit message is
foo
bar
Quoted 7 lines
previously we would print "HEAD is now at 000000 foo", while after this change we print "HEAD is now at 000000 foo bar", same as 'git log --oneline' shows "000000 foo bar".
So this does make the output more consistent with other commands, and 'reset' is a porcelain command, so nobody should be parsing the output in scripts.
Yeah, I'd say it's definitely fine to change the (already translated!) stderr output of git-reset. I agree that it's an improvement to be consistent with other commands here.
Quoted 5 lines
The current behaviour dates back to
0e5a7faa3a("Make "git reset" a builtin.", 2007-09-11), so I assume (without digging into the old codebase too much) that the logic was implemented because there was no convenience function such as 'pp_commit_easy' that would do this already.
Yes, there used to be quite a lot of ad hoc parsing of commit objects, but these days we have safer and more readable alternatives. Even without the visible behavior change, I think it would be worth doing this patch just as a code cleanup.
builtin/reset.c static int reset_index(const struct object_id *oid, int reset_type, int quiet) · line 118
Quoted 10 lines
builtin/reset.c +10
-0+struct strbuf buf =STRBUF_INIT ; + +printf (_ ("HEAD is now at %s" ), +find_unique_abbrev (commit->object.oid.hash,DEFAULT_ABBREV )); + +pp_commit_easy (CMIT_FMT_ONELINE , commit, &buf); +if (buf.len >0 ) +printf (" %s" , buf.buf); +putchar ('\n' ); +strbuf_release (&buf);
I was disappointed you still had to call find_unique_abbrev(). It seems like we ought to be able to do this in a single formatting call. But CMIT_FMT_ONELINE is just about the subject, and doesn't include the hash at all. There's no equivalent without turning to the user-format code.
You can do:
struct pretty_print_context ctx = {0, DEFAULT_ABBREV};
format_commit_message(commit, _("HEAD is now at %H %s"), &buf, &ctx);
puts(buf.buf);but I was annoyed at having to say "DEFAULT_ABBREV". It seems like that ought to be the default.
Anyway, I'm fine with your patch as-is. I just needed a little formatting code-golf in my day.
-Peff
Quoted 4 lines · Thomas Gummerer
In addition to the easier to follow code, this makes the output more consistent with other commands that print the title of the commit, such as 'git commit --oneline' or 'git checkout', which both use '
pp_commit_easy()' with theCMIT_FMT_ONELINEmodifier.
Yes, this is absolutely the right thing to do.
builtin/reset.c static int reset_index(const struct object_id *oid, int reset_type, int quiet) · line 121
Quoted 9 lines
builtin/reset.c +6
-0+ +pp_commit_easy (CMIT_FMT_ONELINE , commit, &buf); +if (buf.len >0 ) +printf (" %s" , buf.buf); +putchar ('\n' ); +strbuf_release (&buf); } static void update_index_from_diff(struct diff_queue_struct *q,
Thanks, will queue.