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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Dec 6, 2008 at 14:40 UTC
3927bbe9a4d4033551dea39ac1abb9cdc45d822c
1 file changed
+20
-8
builtin-tag.c
+20
-8
@@ -253,6 +253,15 @@ static void write_tag_body(int fd, const unsigned char *sha1)
253
free(buf);
254
}
255
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
+
265
static void create_tag(const unsigned char *object, const char *tag,
266
struct strbuf *buf, int message, int sign,
267
unsigned char *prev, unsigned char *result)
@@ -260,6 +269,7 @@ static void create_tag(const unsigned char *object, const char *tag,
269
enum object_type type;
270
char header_buf[1024];
271
int header_len;
272
+ char *path = NULL;
273
274
type = sha1_object_info(object, NULL);
275
if (type <= OBJ_NONE)
@@ -279,7 +289,6 @@ static void create_tag(const unsigned char *object, const char *tag,
289
die("tag header too big.");
290
291
if (!message) {
282
- char *path;
292
int fd;
293
294
/* write the template message before editing: */
@@ -300,9 +309,6 @@ static void create_tag(const unsigned char *object, const char *tag,
309
"Please supply the message using either -m or -F option.\n");
310
exit(1);
311
}
303
-
304
- unlink(path);
305
- free(path);
312
}
313
314
stripspace(buf, 1);
@@ -312,10 +318,16 @@ static void create_tag(const unsigned char *object, const char *tag,
318
319
strbuf_insert(buf, 0, header_buf, header_len);
320
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
+ }
331
}
332
333
struct msg_arg {