Skip to content

Commit bab8118

Browse files
glandiumgitster
authored andcommitted
Reuse previous annotation when overwriting a tag
When forcing to overwrite an annotated tag, there are good chances one wants to keep the old annotation, or modify it, not start from scratch. This is obviously only triggered for annotated tagging (-a or -s). Signed-off-by: Mike Hommey <mh@glandium.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent fe61935 commit bab8118

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

builtin-tag.c

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,37 @@ static int git_tag_config(const char *var, const char *value)
247247
return git_default_config(var, value);
248248
}
249249

250+
static void write_tag_body(int fd, const unsigned char *sha1)
251+
{
252+
unsigned long size;
253+
enum object_type type;
254+
char *buf, *sp, *eob;
255+
size_t len;
256+
257+
buf = read_sha1_file(sha1, &type, &size);
258+
if (!buf)
259+
return;
260+
/* skip header */
261+
sp = strstr(buf, "\n\n");
262+
263+
if (!sp || !size || type != OBJ_TAG) {
264+
free(buf);
265+
return;
266+
}
267+
sp += 2; /* skip the 2 LFs */
268+
eob = strstr(sp, "\n" PGP_SIGNATURE "\n");
269+
if (eob)
270+
len = eob - sp;
271+
else
272+
len = buf + size - sp;
273+
write_or_die(fd, sp, len);
274+
275+
free(buf);
276+
}
277+
250278
static void create_tag(const unsigned char *object, const char *tag,
251279
struct strbuf *buf, int message, int sign,
252-
unsigned char *result)
280+
unsigned char *prev, unsigned char *result)
253281
{
254282
enum object_type type;
255283
char header_buf[1024];
@@ -282,7 +310,11 @@ static void create_tag(const unsigned char *object, const char *tag,
282310
if (fd < 0)
283311
die("could not create file '%s': %s",
284312
path, strerror(errno));
285-
write_or_die(fd, tag_template, strlen(tag_template));
313+
314+
if (!is_null_sha1(prev))
315+
write_tag_body(fd, prev);
316+
else
317+
write_or_die(fd, tag_template, strlen(tag_template));
286318
close(fd);
287319

288320
launch_editor(path, buf);
@@ -419,7 +451,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
419451
die("tag '%s' already exists", tag);
420452

421453
if (annotate)
422-
create_tag(object, tag, &buf, message, sign, object);
454+
create_tag(object, tag, &buf, message, sign, prev, object);
423455

424456
lock = lock_any_ref_for_update(ref, prev, 0);
425457
if (!lock)

0 commit comments

Comments
 (0)