Skip to content

Commit 1cf823f

Browse files
tgummerergitster
authored andcommitted
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 0e5a7fa ("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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 42e6fde commit 1cf823f

File tree

1 file changed

+10
-18
lines changed

1 file changed

+10
-18
lines changed

builtin/reset.c

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -93,24 +93,16 @@ static int reset_index(const struct object_id *oid, int reset_type, int quiet)
9393

9494
static void print_new_head_line(struct commit *commit)
9595
{
96-
const char *hex, *body;
97-
const char *msg;
98-
99-
hex = find_unique_abbrev(commit->object.oid.hash, DEFAULT_ABBREV);
100-
printf(_("HEAD is now at %s"), hex);
101-
msg = logmsg_reencode(commit, NULL, get_log_output_encoding());
102-
body = strstr(msg, "\n\n");
103-
if (body) {
104-
const char *eol;
105-
size_t len;
106-
body = skip_blank_lines(body + 2);
107-
eol = strchr(body, '\n');
108-
len = eol ? eol - body : strlen(body);
109-
printf(" %.*s\n", (int) len, body);
110-
}
111-
else
112-
printf("\n");
113-
unuse_commit_buffer(commit, msg);
96+
struct strbuf buf = STRBUF_INIT;
97+
98+
printf(_("HEAD is now at %s"),
99+
find_unique_abbrev(commit->object.oid.hash, DEFAULT_ABBREV));
100+
101+
pp_commit_easy(CMIT_FMT_ONELINE, commit, &buf);
102+
if (buf.len > 0)
103+
printf(" %s", buf.buf);
104+
putchar('\n');
105+
strbuf_release(&buf);
114106
}
115107

116108
static void update_index_from_diff(struct diff_queue_struct *q,

0 commit comments

Comments
 (0)