Skip to content

Commit 08c12ec

Browse files
LemmingAvalanchegitster
authored andcommitted
tag: keep the message file in case ref transaction fails
The ref transaction can fail after the user has written their tag message. In particular, if there exists a tag `foo/bar` and `git tag -a foo` is said then the command will only fail once it tries to write `refs/tags/foo`, which is after the file has been unlinked. Hold on to the message file for a little longer so that it is not unlinked before the fatal error occurs. Signed-off-by: Kristoffer Haugsbakk <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 669c11d commit 08c12ec

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

builtin/tag.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,10 @@ static const char message_advice_nested_tag[] =
271271
static void create_tag(const struct object_id *object, const char *object_ref,
272272
const char *tag,
273273
struct strbuf *buf, struct create_tag_options *opt,
274-
struct object_id *prev, struct object_id *result)
274+
struct object_id *prev, struct object_id *result, char *path)
275275
{
276276
enum object_type type;
277277
struct strbuf header = STRBUF_INIT;
278-
char *path = NULL;
279278

280279
type = oid_object_info(the_repository, object, NULL);
281280
if (type <= OBJ_NONE)
@@ -299,7 +298,6 @@ static void create_tag(const struct object_id *object, const char *object_ref,
299298
int fd;
300299

301300
/* write the template message before editing: */
302-
path = git_pathdup("TAG_EDITMSG");
303301
fd = xopen(path, O_CREAT | O_TRUNC | O_WRONLY, 0600);
304302

305303
if (opt->message_given) {
@@ -341,10 +339,6 @@ static void create_tag(const struct object_id *object, const char *object_ref,
341339
path);
342340
exit(128);
343341
}
344-
if (path) {
345-
unlink_or_warn(path);
346-
free(path);
347-
}
348342
}
349343

350344
static void create_reflog_msg(const struct object_id *oid, struct strbuf *sb)
@@ -495,6 +489,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
495489
};
496490
int ret = 0;
497491
const char *only_in_list = NULL;
492+
char *path = NULL;
498493

499494
setup_ref_filter_porcelain_msg();
500495

@@ -629,16 +624,27 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
629624
if (create_tag_object) {
630625
if (force_sign_annotate && !annotate)
631626
opt.sign = 1;
632-
create_tag(&object, object_ref, tag, &buf, &opt, &prev, &object);
627+
path = git_pathdup("TAG_EDITMSG");
628+
create_tag(&object, object_ref, tag, &buf, &opt, &prev, &object,
629+
path);
633630
}
634631

635632
transaction = ref_transaction_begin(&err);
636633
if (!transaction ||
637634
ref_transaction_update(transaction, ref.buf, &object, &prev,
638635
create_reflog ? REF_FORCE_CREATE_REFLOG : 0,
639636
reflog_msg.buf, &err) ||
640-
ref_transaction_commit(transaction, &err))
637+
ref_transaction_commit(transaction, &err)) {
638+
if (path)
639+
fprintf(stderr,
640+
_("The tag message has been left in %s\n"),
641+
path);
641642
die("%s", err.buf);
643+
}
644+
if (path) {
645+
unlink_or_warn(path);
646+
free(path);
647+
}
642648
ref_transaction_free(transaction);
643649
if (force && !is_null_oid(&prev) && !oideq(&prev, &object))
644650
printf(_("Updated tag '%s' (was %s)\n"), tag,

t/t7004-tag.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,4 +2197,14 @@ test_expect_success 'If tag is created then tag message file is unlinked' '
21972197
test_path_is_missing .git/TAG_EDITMSG
21982198
'
21992199

2200+
test_expect_success 'If tag cannot be created then tag message file is not unlinked' '
2201+
test_when_finished "git tag -d foo/bar && rm .git/TAG_EDITMSG" &&
2202+
write_script fakeeditor <<-\EOF &&
2203+
echo Message >.git/TAG_EDITMSG
2204+
EOF
2205+
git tag foo/bar &&
2206+
test_must_fail env GIT_EDITOR=./fakeeditor git tag -a foo &&
2207+
test_path_exists .git/TAG_EDITMSG
2208+
'
2209+
22002210
test_done

0 commit comments

Comments
 (0)