Skip to content

Commit 6d2a88c

Browse files
committed
Merge branch 'kh/keep-tag-editmsg-upon-failure'
"git tag" learned to leave the "$GIT_DIR/TAG_EDITMSG" file when the command failed, so that the user can salvage what they typed. * kh/keep-tag-editmsg-upon-failure: tag: keep the message file in case ref transaction fails t/t7004-tag: add regression test for successful tag creation doc: tag: document `TAG_EDITMSG`
2 parents fe86abd + 08c12ec commit 6d2a88c

File tree

3 files changed

+44
-9
lines changed

3 files changed

+44
-9
lines changed

Documentation/git-tag.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,16 @@ $ GIT_COMMITTER_DATE="2006-10-02 10:31" git tag -s v1.0.1
381381

382382
include::date-formats.txt[]
383383

384+
FILES
385+
-----
386+
387+
`$GIT_DIR/TAG_EDITMSG`::
388+
This file contains the message of an in-progress annotated
389+
tag. If `git tag` exits due to an error before creating an
390+
annotated tag then the tag message that has been provided by the
391+
user in an editor session will be available in this file, but
392+
may be overwritten by the next invocation of `git tag`.
393+
384394
NOTES
385395
-----
386396

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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2188,4 +2188,23 @@ test_expect_success 'Does --[no-]contains stop at commits? Yes!' '
21882188
test_cmp expected actual
21892189
'
21902190

2191+
test_expect_success 'If tag is created then tag message file is unlinked' '
2192+
test_when_finished "git tag -d foo" &&
2193+
write_script fakeeditor <<-\EOF &&
2194+
echo Message >.git/TAG_EDITMSG
2195+
EOF
2196+
GIT_EDITOR=./fakeeditor git tag -a foo &&
2197+
test_path_is_missing .git/TAG_EDITMSG
2198+
'
2199+
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+
21912210
test_done

0 commit comments

Comments
 (0)