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 <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Christian Couder committed Jul 19, 2014 at 17:01 UTC 25a05a8cae263ca55e75d31336d09c8d9bc90be3
1 file changed +47
builtin/replace.c
+47
@@ -13,6 +13,7 @@
13 #include "refs.h"
14 #include "parse-options.h"
15 #include "run-command.h"
16 +#include "tag.h"
17
18 static const char * const git_replace_usage[] = {
19 N_("git replace [-f] <object> <replacement>"),
@@ -325,6 +326,50 @@ static void replace_parents(struct strbuf *buf, int argc, const char **argv)
326 strbuf_release(&new_parents);
327 }
328
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 +
373 static int create_graft(int argc, const char **argv, int force)
374 {
375 unsigned char old[20], new[20];
@@ -349,6 +394,8 @@ static int create_graft(int argc, const char **argv, int force)
394 warning(_("the signature will be removed in the replacement commit!"));
395 }
396
397 + check_mergetags(commit, argc, argv);
398 +
399 if (write_sha1_file(buf.buf, buf.len, commit_type, new))
400 die(_("could not write replacement commit for: '%s'"), old_ref);
401