Skip to content

Commit 2f19a52

Browse files
committed
Merge branch 'jk/reset-reflog-message-fix' into maint
* jk/reset-reflog-message-fix: reset: give better reflog messages
2 parents 5d4fcd9 + d04520e commit 2f19a52

File tree

2 files changed

+20
-37
lines changed

2 files changed

+20
-37
lines changed

builtin/reset.c

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -33,25 +33,6 @@ static const char *reset_type_names[] = {
3333
N_("mixed"), N_("soft"), N_("hard"), N_("merge"), N_("keep"), NULL
3434
};
3535

36-
static char *args_to_str(const char **argv)
37-
{
38-
char *buf = NULL;
39-
unsigned long len, space = 0, nr = 0;
40-
41-
for (; *argv; argv++) {
42-
len = strlen(*argv);
43-
ALLOC_GROW(buf, nr + 1 + len, space);
44-
if (nr)
45-
buf[nr++] = ' ';
46-
memcpy(buf + nr, *argv, len);
47-
nr += len;
48-
}
49-
ALLOC_GROW(buf, nr + 1, space);
50-
buf[nr] = '\0';
51-
52-
return buf;
53-
}
54-
5536
static inline int is_merge(void)
5637
{
5738
return !access(git_path("MERGE_HEAD"), F_OK);
@@ -215,14 +196,18 @@ static int read_from_tree(const char *prefix, const char **argv,
215196
return update_index_refresh(index_fd, lock, refresh_flags);
216197
}
217198

218-
static void prepend_reflog_action(const char *action, char *buf, size_t size)
199+
static void set_reflog_message(struct strbuf *sb, const char *action,
200+
const char *rev)
219201
{
220-
const char *sep = ": ";
221202
const char *rla = getenv("GIT_REFLOG_ACTION");
222-
if (!rla)
223-
rla = sep = "";
224-
if (snprintf(buf, size, "%s%s%s", rla, sep, action) >= size)
225-
warning(_("Reflog action message too long: %.*s..."), 50, buf);
203+
204+
strbuf_reset(sb);
205+
if (rla)
206+
strbuf_addf(sb, "%s: %s", rla, action);
207+
else if (rev)
208+
strbuf_addf(sb, "reset: moving to %s", rev);
209+
else
210+
strbuf_addf(sb, "reset: %s", action);
226211
}
227212

228213
static void die_if_unmerged_cache(int reset_type)
@@ -241,7 +226,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
241226
unsigned char sha1[20], *orig = NULL, sha1_orig[20],
242227
*old_orig = NULL, sha1_old_orig[20];
243228
struct commit *commit;
244-
char *reflog_action, msg[1024];
229+
struct strbuf msg = STRBUF_INIT;
245230
const struct option options[] = {
246231
OPT__QUIET(&quiet, "be quiet, only report errors"),
247232
OPT_SET_INT(0, "mixed", &reset_type,
@@ -261,8 +246,6 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
261246

262247
argc = parse_options(argc, argv, prefix, options, git_reset_usage,
263248
PARSE_OPT_KEEP_DASHDASH);
264-
reflog_action = args_to_str(argv);
265-
setenv("GIT_REFLOG_ACTION", reflog_action, 0);
266249

267250
/*
268251
* Possible arguments are:
@@ -357,13 +340,13 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
357340
old_orig = sha1_old_orig;
358341
if (!get_sha1("HEAD", sha1_orig)) {
359342
orig = sha1_orig;
360-
prepend_reflog_action("updating ORIG_HEAD", msg, sizeof(msg));
361-
update_ref(msg, "ORIG_HEAD", orig, old_orig, 0, MSG_ON_ERR);
343+
set_reflog_message(&msg, "updating ORIG_HEAD", NULL);
344+
update_ref(msg.buf, "ORIG_HEAD", orig, old_orig, 0, MSG_ON_ERR);
362345
}
363346
else if (old_orig)
364347
delete_ref("ORIG_HEAD", old_orig, 0);
365-
prepend_reflog_action("updating HEAD", msg, sizeof(msg));
366-
update_ref_status = update_ref(msg, "HEAD", sha1, orig, 0, MSG_ON_ERR);
348+
set_reflog_message(&msg, "updating HEAD", rev);
349+
update_ref_status = update_ref(msg.buf, "HEAD", sha1, orig, 0, MSG_ON_ERR);
367350

368351
switch (reset_type) {
369352
case HARD:
@@ -380,7 +363,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
380363

381364
remove_branch_state();
382365

383-
free(reflog_action);
366+
strbuf_release(&msg);
384367

385368
return update_ref_status;
386369
}

t/t1412-reflog-loop.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ test_expect_success 'setup reflog with alternating commits' '
2121

2222
test_expect_success 'reflog shows all entries' '
2323
cat >expect <<-\EOF
24-
topic@{0} two: updating HEAD
25-
topic@{1} one: updating HEAD
26-
topic@{2} two: updating HEAD
27-
topic@{3} one: updating HEAD
24+
topic@{0} reset: moving to two
25+
topic@{1} reset: moving to one
26+
topic@{2} reset: moving to two
27+
topic@{3} reset: moving to one
2828
topic@{4} branch: Created from HEAD
2929
EOF
3030
git log -g --format="%gd %gs" topic >actual &&

0 commit comments

Comments
 (0)