Skip to content
This repository was archived by the owner on Nov 9, 2017. It is now read-only.

Commit 25a05a8

Browse files
chriscoolgitster
authored andcommitted
replace: check mergetags when using --graft
When using --graft, with a mergetag in the original commit, we should check that the commit pointed to by the mergetag is still a parent of then new commit we create, otherwise the mergetag could be misleading. If the commit pointed to by the mergetag is no more a parent of the new commit, we could remove the mergetag, but in this case there is a good chance that the title or other elements of the commit might also be misleading. So let's just error out and suggest to use --edit instead on the commit. Signed-off-by: Christian Couder <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 60e2f5a commit 25a05a8

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

builtin/replace.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "refs.h"
1414
#include "parse-options.h"
1515
#include "run-command.h"
16+
#include "tag.h"
1617

1718
static const char * const git_replace_usage[] = {
1819
N_("git replace [-f] <object> <replacement>"),
@@ -325,6 +326,50 @@ static void replace_parents(struct strbuf *buf, int argc, const char **argv)
325326
strbuf_release(&new_parents);
326327
}
327328

329+
struct check_mergetag_data {
330+
int argc;
331+
const char **argv;
332+
};
333+
334+
static void check_one_mergetag(struct commit *commit,
335+
struct commit_extra_header *extra,
336+
void *data)
337+
{
338+
struct check_mergetag_data *mergetag_data = (struct check_mergetag_data *)data;
339+
const char *ref = mergetag_data->argv[0];
340+
unsigned char tag_sha1[20];
341+
struct tag *tag;
342+
int i;
343+
344+
hash_sha1_file(extra->value, extra->len, typename(OBJ_TAG), tag_sha1);
345+
tag = lookup_tag(tag_sha1);
346+
if (!tag)
347+
die(_("bad mergetag in commit '%s'"), ref);
348+
if (parse_tag_buffer(tag, extra->value, extra->len))
349+
die(_("malformed mergetag in commit '%s'"), ref);
350+
351+
/* iterate over new parents */
352+
for (i = 1; i < mergetag_data->argc; i++) {
353+
unsigned char sha1[20];
354+
if (get_sha1(mergetag_data->argv[i], sha1) < 0)
355+
die(_("Not a valid object name: '%s'"), mergetag_data->argv[i]);
356+
if (!hashcmp(tag->tagged->sha1, sha1))
357+
return; /* found */
358+
}
359+
360+
die(_("original commit '%s' contains mergetag '%s' that is discarded; "
361+
"use --edit instead of --graft"), ref, sha1_to_hex(tag_sha1));
362+
}
363+
364+
static void check_mergetags(struct commit *commit, int argc, const char **argv)
365+
{
366+
struct check_mergetag_data mergetag_data;
367+
368+
mergetag_data.argc = argc;
369+
mergetag_data.argv = argv;
370+
for_each_mergetag(check_one_mergetag, commit, &mergetag_data);
371+
}
372+
328373
static int create_graft(int argc, const char **argv, int force)
329374
{
330375
unsigned char old[20], new[20];
@@ -349,6 +394,8 @@ static int create_graft(int argc, const char **argv, int force)
349394
warning(_("the signature will be removed in the replacement commit!"));
350395
}
351396

397+
check_mergetags(commit, argc, argv);
398+
352399
if (write_sha1_file(buf.buf, buf.len, commit_type, new))
353400
die(_("could not write replacement commit for: '%s'"), old_ref);
354401

0 commit comments

Comments
 (0)