Skip to content

Commit ef4de83

Browse files
committed
Merge branch 'mh/retag'
* mh/retag: Add tests for git tag Reuse previous annotation when overwriting a tag
2 parents 5e389c4 + 4d8b1dc commit ef4de83

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

builtin-tag.c

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

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

287319
launch_editor(path, buf);
@@ -418,7 +450,7 @@ int cmd_tag(int argc, const char **argv, const char *prefix)
418450
die("tag '%s' already exists", tag);
419451

420452
if (annotate)
421-
create_tag(object, tag, &buf, message, sign, object);
453+
create_tag(object, tag, &buf, message, sign, prev, object);
422454

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

t/t7004-tag.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,4 +1004,20 @@ test_expect_failure \
10041004
'verify signed tag fails when public key is not present' \
10051005
'git-tag -v signed-tag'
10061006

1007+
test_expect_success \
1008+
'message in editor has initial comment' '
1009+
GIT_EDITOR=cat git tag -a initial-comment > actual || true &&
1010+
test $(sed -n "/^\(#\|\$\)/p" actual | wc -l) -gt 0
1011+
'
1012+
1013+
get_tag_header reuse $commit commit $time >expect
1014+
echo "An annotation to be reused" >> expect
1015+
test_expect_success \
1016+
'overwriting an annoted tag should use its previous body' '
1017+
git tag -a -m "An annotation to be reused" reuse &&
1018+
GIT_EDITOR=true git tag -f -a reuse &&
1019+
get_tag_msg reuse >actual &&
1020+
git diff expect actual
1021+
'
1022+
10071023
test_done

0 commit comments

Comments
 (0)