Skip to content

Commit fb75e31

Browse files
committed
Merge branch 'cw/tag-reflog-message'
"git tag" did not leave useful message when adding a new entry to reflog; this was left unnoticed for a long time because refs/tags/* doesn't keep reflog by default. * cw/tag-reflog-message: tag: generate useful reflog message
2 parents b9c2919 + df8512e commit fb75e31

File tree

2 files changed

+70
-2
lines changed

2 files changed

+70
-2
lines changed

builtin/tag.c

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,54 @@ static void create_tag(const unsigned char *object, const char *tag,
302302
}
303303
}
304304

305+
static void create_reflog_msg(const unsigned char *sha1, struct strbuf *sb)
306+
{
307+
enum object_type type;
308+
struct commit *c;
309+
char *buf;
310+
unsigned long size;
311+
int subject_len = 0;
312+
const char *subject_start;
313+
314+
char *rla = getenv("GIT_REFLOG_ACTION");
315+
if (rla) {
316+
strbuf_addstr(sb, rla);
317+
} else {
318+
strbuf_addstr(sb, _("tag: tagging "));
319+
strbuf_add_unique_abbrev(sb, sha1, DEFAULT_ABBREV);
320+
}
321+
322+
strbuf_addstr(sb, " (");
323+
type = sha1_object_info(sha1, NULL);
324+
switch (type) {
325+
default:
326+
strbuf_addstr(sb, _("object of unknown type"));
327+
break;
328+
case OBJ_COMMIT:
329+
if ((buf = read_sha1_file(sha1, &type, &size)) != NULL) {
330+
subject_len = find_commit_subject(buf, &subject_start);
331+
strbuf_insert(sb, sb->len, subject_start, subject_len);
332+
} else {
333+
strbuf_addstr(sb, _("commit object"));
334+
}
335+
free(buf);
336+
337+
if ((c = lookup_commit_reference(sha1)) != NULL)
338+
strbuf_addf(sb, ", %s", show_date(c->date, 0, DATE_MODE(SHORT)));
339+
break;
340+
case OBJ_TREE:
341+
strbuf_addstr(sb, _("tree object"));
342+
break;
343+
case OBJ_BLOB:
344+
strbuf_addstr(sb, _("blob object"));
345+
break;
346+
case OBJ_TAG:
347+
strbuf_addstr(sb, _("other tag object"));
348+
break;
349+
}
350+
strbuf_addch(sb, ')');
351+
}
352+
305353
struct msg_arg {
306354
int given;
307355
struct strbuf buf;
@@ -335,6 +383,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
335383
{
336384
struct strbuf buf = STRBUF_INIT;
337385
struct strbuf ref = STRBUF_INIT;
386+
struct strbuf reflog_msg = STRBUF_INIT;
338387
unsigned char object[20], prev[20];
339388
const char *object_ref, *tag;
340389
struct create_tag_options opt;
@@ -496,6 +545,8 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
496545
else
497546
die(_("Invalid cleanup mode %s"), cleanup_arg);
498547

548+
create_reflog_msg(object, &reflog_msg);
549+
499550
if (create_tag_object) {
500551
if (force_sign_annotate && !annotate)
501552
opt.sign = 1;
@@ -506,7 +557,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
506557
if (!transaction ||
507558
ref_transaction_update(transaction, ref.buf, object, prev,
508559
create_reflog ? REF_FORCE_CREATE_REFLOG : 0,
509-
NULL, &err) ||
560+
reflog_msg.buf, &err) ||
510561
ref_transaction_commit(transaction, &err))
511562
die("%s", err.buf);
512563
ref_transaction_free(transaction);
@@ -516,5 +567,6 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
516567
strbuf_release(&err);
517568
strbuf_release(&buf);
518569
strbuf_release(&ref);
570+
strbuf_release(&reflog_msg);
519571
return 0;
520572
}

t/t7004-tag.sh

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,25 @@ test_expect_success 'creating a tag using default HEAD should succeed' '
8181
'
8282

8383
test_expect_success 'creating a tag with --create-reflog should create reflog' '
84+
git log -1 \
85+
--format="format:tag: tagging %h (%s, %cd)%n" \
86+
--date=format:%Y-%m-%d >expected &&
8487
test_when_finished "git tag -d tag_with_reflog" &&
8588
git tag --create-reflog tag_with_reflog &&
86-
git reflog exists refs/tags/tag_with_reflog
89+
git reflog exists refs/tags/tag_with_reflog &&
90+
sed -e "s/^.* //" .git/logs/refs/tags/tag_with_reflog >actual &&
91+
test_cmp expected actual
92+
'
93+
94+
test_expect_success 'annotated tag with --create-reflog has correct message' '
95+
git log -1 \
96+
--format="format:tag: tagging %h (%s, %cd)%n" \
97+
--date=format:%Y-%m-%d >expected &&
98+
test_when_finished "git tag -d tag_with_reflog" &&
99+
git tag -m "annotated tag" --create-reflog tag_with_reflog &&
100+
git reflog exists refs/tags/tag_with_reflog &&
101+
sed -e "s/^.* //" .git/logs/refs/tags/tag_with_reflog >actual &&
102+
test_cmp expected actual
87103
'
88104

89105
test_expect_success '--create-reflog does not create reflog on failure' '

0 commit comments

Comments
 (0)