Skip to content

Commit 3927bbe

Browse files
peffgitster
authored andcommitted
tag: delete TAG_EDITMSG only on successful tag
The user may put some effort into writing an annotated tag message. When the tagging process later fails (which can happen fairly easily, since it may be dependent on gpg being correctly configured and used), there is no record left on disk of the tag message. Instead, let's keep the TAG_EDITMSG file around until we are sure the tag has been created successfully. If we die because of an error, the user can recover their text from that file. Leaving the file in place causes no conflicts; it will be silently overwritten by the next annotated tag creation. This matches the behavior of COMMIT_EDITMSG, which stays around in case of error. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bcc6a83 commit 3927bbe

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

builtin-tag.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -253,13 +253,23 @@ static void write_tag_body(int fd, const unsigned char *sha1)
253253
free(buf);
254254
}
255255

256+
static int build_tag_object(struct strbuf *buf, int sign, unsigned char *result)
257+
{
258+
if (sign && do_sign(buf) < 0)
259+
return error("unable to sign the tag");
260+
if (write_sha1_file(buf->buf, buf->len, tag_type, result) < 0)
261+
return error("unable to write tag file");
262+
return 0;
263+
}
264+
256265
static void create_tag(const unsigned char *object, const char *tag,
257266
struct strbuf *buf, int message, int sign,
258267
unsigned char *prev, unsigned char *result)
259268
{
260269
enum object_type type;
261270
char header_buf[1024];
262271
int header_len;
272+
char *path = NULL;
263273

264274
type = sha1_object_info(object, NULL);
265275
if (type <= OBJ_NONE)
@@ -279,7 +289,6 @@ static void create_tag(const unsigned char *object, const char *tag,
279289
die("tag header too big.");
280290

281291
if (!message) {
282-
char *path;
283292
int fd;
284293

285294
/* write the template message before editing: */
@@ -300,9 +309,6 @@ static void create_tag(const unsigned char *object, const char *tag,
300309
"Please supply the message using either -m or -F option.\n");
301310
exit(1);
302311
}
303-
304-
unlink(path);
305-
free(path);
306312
}
307313

308314
stripspace(buf, 1);
@@ -312,10 +318,16 @@ static void create_tag(const unsigned char *object, const char *tag,
312318

313319
strbuf_insert(buf, 0, header_buf, header_len);
314320

315-
if (sign && do_sign(buf) < 0)
316-
die("unable to sign the tag");
317-
if (write_sha1_file(buf->buf, buf->len, tag_type, result) < 0)
318-
die("unable to write tag file");
321+
if (build_tag_object(buf, sign, result) < 0) {
322+
if (path)
323+
fprintf(stderr, "The tag message has been left in %s\n",
324+
path);
325+
exit(128);
326+
}
327+
if (path) {
328+
unlink(path);
329+
free(path);
330+
}
319331
}
320332

321333
struct msg_arg {

0 commit comments

Comments
 (0)