git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [RFC/PATCH] reset --hard: make use of the pretty machinery
@ 2018-02-01 20:57 Thomas Gummerer
  2018-02-02  7:15 ` Jeff King
  2018-02-02 20:16 ` Junio C Hamano
  0 siblings, 2 replies; 3+ messages in thread
From: Thomas Gummerer @ 2018-02-01 20:57 UTC (permalink / raw)
  To: git; +Cc: Thomas Gummerer

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.

Signed-off-by: Thomas Gummerer <t.gummerer@gmail.com>
---

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(-)

diff --git a/builtin/reset.c b/builtin/reset.c
index e15f595799..5da0f75de9 100644
--- a/builtin/reset.c
+++ b/builtin/reset.c
@@ -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,
-- 
2.16.1.101.gde0f0111ea


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [RFC/PATCH] reset --hard: make use of the pretty machinery
  2018-02-01 20:57 [RFC/PATCH] reset --hard: make use of the pretty machinery Thomas Gummerer
@ 2018-02-02  7:15 ` Jeff King
  2018-02-02 20:16 ` Junio C Hamano
  1 sibling, 0 replies; 3+ messages in thread
From: Jeff King @ 2018-02-02  7:15 UTC (permalink / raw)
  To: Thomas Gummerer; +Cc: git

On Thu, Feb 01, 2018 at 08:57:21PM +0000, Thomas Gummerer wrote:

> 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.

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.

> 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.

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [RFC/PATCH] reset --hard: make use of the pretty machinery
  2018-02-01 20:57 [RFC/PATCH] reset --hard: make use of the pretty machinery Thomas Gummerer
  2018-02-02  7:15 ` Jeff King
@ 2018-02-02 20:16 ` Junio C Hamano
  1 sibling, 0 replies; 3+ messages in thread
From: Junio C Hamano @ 2018-02-02 20:16 UTC (permalink / raw)
  To: Thomas Gummerer; +Cc: git

Thomas Gummerer <t.gummerer@gmail.com> writes:

> 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.

Yes, this is absolutely the right thing to do.

> +
> +	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.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2018-02-02 20:17 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-02-01 20:57 [RFC/PATCH] reset --hard: make use of the pretty machinery Thomas Gummerer
2018-02-02  7:15 ` Jeff King
2018-02-02 20:16 ` Junio C Hamano

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).