blob: 303ca134dcc00131986a79c31f68dc6dcd70be0d [file] [log] [blame]
Christian Couder54b0c1e2009-02-02 06:12:44 +01001/*
2 * Builtin "git replace"
3 *
4 * Copyright (c) 2008 Christian Couder <chriscool@tuxfamily.org>
5 *
Phil Hord09b7e222013-06-18 13:44:58 -04006 * Based on builtin/tag.c by Kristian Høgsberg <krh@redhat.com>
Christian Couder54b0c1e2009-02-02 06:12:44 +01007 * and Carlos Rica <jasampler@gmail.com> that was itself based on
8 * git-tag.sh and mktag.c by Linus Torvalds.
9 */
10
11#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -070012#include "config.h"
Christian Couder54b0c1e2009-02-02 06:12:44 +010013#include "builtin.h"
14#include "refs.h"
15#include "parse-options.h"
Jeff Kingb892bb42014-04-26 22:00:57 +020016#include "run-command.h"
Christian Couder25a05a82014-07-19 17:01:14 +020017#include "tag.h"
Christian Couder54b0c1e2009-02-02 06:12:44 +010018
19static const char * const git_replace_usage[] = {
Nguyễn Thái Ngọc Duy2477beb2012-08-20 19:32:36 +070020 N_("git replace [-f] <object> <replacement>"),
Christian Couderab77c302014-05-17 14:16:38 +020021 N_("git replace [-f] --edit <object>"),
Christian Couder4228e8b2014-07-19 17:01:08 +020022 N_("git replace [-f] --graft <commit> [<parent>...]"),
Nguyễn Thái Ngọc Duy2477beb2012-08-20 19:32:36 +070023 N_("git replace -d <object>..."),
Christian Couder44f9f852013-12-11 08:46:10 +010024 N_("git replace [--format=<format>] [-l [<pattern>]]"),
Christian Couder54b0c1e2009-02-02 06:12:44 +010025 NULL
26};
27
Christian Couder663a8562013-12-28 12:00:05 +010028enum replace_format {
Jeff King3cc9d872014-06-24 05:43:07 -040029 REPLACE_FORMAT_SHORT,
30 REPLACE_FORMAT_MEDIUM,
31 REPLACE_FORMAT_LONG
Christian Couder663a8562013-12-28 12:00:05 +010032};
Christian Couder44f9f852013-12-11 08:46:10 +010033
34struct show_data {
35 const char *pattern;
Christian Couder663a8562013-12-28 12:00:05 +010036 enum replace_format format;
Christian Couder44f9f852013-12-11 08:46:10 +010037};
38
Michael Haggertyd70d7a82015-05-25 18:38:44 +000039static int show_reference(const char *refname, const struct object_id *oid,
Christian Couder54b0c1e2009-02-02 06:12:44 +010040 int flag, void *cb_data)
41{
Christian Couder44f9f852013-12-11 08:46:10 +010042 struct show_data *data = cb_data;
Christian Couder54b0c1e2009-02-02 06:12:44 +010043
Ævar Arnfjörð Bjarmason55d34262017-06-22 21:38:08 +000044 if (!wildmatch(data->pattern, refname, 0)) {
Christian Couder663a8562013-12-28 12:00:05 +010045 if (data->format == REPLACE_FORMAT_SHORT)
Christian Couder44f9f852013-12-11 08:46:10 +010046 printf("%s\n", refname);
Christian Couder663a8562013-12-28 12:00:05 +010047 else if (data->format == REPLACE_FORMAT_MEDIUM)
Michael Haggertyd70d7a82015-05-25 18:38:44 +000048 printf("%s -> %s\n", refname, oid_to_hex(oid));
Christian Couder663a8562013-12-28 12:00:05 +010049 else { /* data->format == REPLACE_FORMAT_LONG */
Michael Haggertyd70d7a82015-05-25 18:38:44 +000050 struct object_id object;
Christian Couder44f9f852013-12-11 08:46:10 +010051 enum object_type obj_type, repl_type;
52
brian m. carlsone82caf32017-07-13 23:49:28 +000053 if (get_oid(refname, &object))
Christian Couder44f9f852013-12-11 08:46:10 +010054 return error("Failed to resolve '%s' as a valid ref.", refname);
55
Michael Haggertyd70d7a82015-05-25 18:38:44 +000056 obj_type = sha1_object_info(object.hash, NULL);
57 repl_type = sha1_object_info(oid->hash, NULL);
Christian Couder44f9f852013-12-11 08:46:10 +010058
Brandon Williamsdebca9d2018-02-14 10:59:24 -080059 printf("%s (%s) -> %s (%s)\n", refname, type_name(obj_type),
60 oid_to_hex(oid), type_name(repl_type));
Christian Couder44f9f852013-12-11 08:46:10 +010061 }
62 }
Christian Couder54b0c1e2009-02-02 06:12:44 +010063
64 return 0;
65}
66
Christian Couder44f9f852013-12-11 08:46:10 +010067static int list_replace_refs(const char *pattern, const char *format)
Christian Couder54b0c1e2009-02-02 06:12:44 +010068{
Christian Couder44f9f852013-12-11 08:46:10 +010069 struct show_data data;
70
Christian Couder54b0c1e2009-02-02 06:12:44 +010071 if (pattern == NULL)
72 pattern = "*";
Christian Couder44f9f852013-12-11 08:46:10 +010073 data.pattern = pattern;
Christian Couder54b0c1e2009-02-02 06:12:44 +010074
Christian Couder44f9f852013-12-11 08:46:10 +010075 if (format == NULL || *format == '\0' || !strcmp(format, "short"))
Christian Couder663a8562013-12-28 12:00:05 +010076 data.format = REPLACE_FORMAT_SHORT;
Christian Couder44f9f852013-12-11 08:46:10 +010077 else if (!strcmp(format, "medium"))
Christian Couder663a8562013-12-28 12:00:05 +010078 data.format = REPLACE_FORMAT_MEDIUM;
79 else if (!strcmp(format, "long"))
80 data.format = REPLACE_FORMAT_LONG;
Christian Couder44f9f852013-12-11 08:46:10 +010081 else
82 die("invalid replace format '%s'\n"
Christian Couder663a8562013-12-28 12:00:05 +010083 "valid formats are 'short', 'medium' and 'long'\n",
Christian Couder44f9f852013-12-11 08:46:10 +010084 format);
85
Michael Haggertyd70d7a82015-05-25 18:38:44 +000086 for_each_replace_ref(show_reference, (void *)&data);
Christian Couder54b0c1e2009-02-02 06:12:44 +010087
88 return 0;
89}
90
91typedef int (*each_replace_name_fn)(const char *name, const char *ref,
brian m. carlsoncea43322017-02-21 23:47:30 +000092 const struct object_id *oid);
Christian Couder54b0c1e2009-02-02 06:12:44 +010093
94static int for_each_replace_name(const char **argv, each_replace_name_fn fn)
95{
Michael J Gruber9dfc3682012-11-13 11:34:11 +010096 const char **p, *full_hex;
Jeff King7f897b62017-03-28 15:46:30 -040097 struct strbuf ref = STRBUF_INIT;
98 size_t base_len;
Christian Couder54b0c1e2009-02-02 06:12:44 +010099 int had_error = 0;
brian m. carlsoncea43322017-02-21 23:47:30 +0000100 struct object_id oid;
Christian Couder54b0c1e2009-02-02 06:12:44 +0100101
Jeff King7f897b62017-03-28 15:46:30 -0400102 strbuf_addstr(&ref, git_replace_ref_base);
103 base_len = ref.len;
104
Christian Couder54b0c1e2009-02-02 06:12:44 +0100105 for (p = argv; *p; p++) {
brian m. carlsoncea43322017-02-21 23:47:30 +0000106 if (get_oid(*p, &oid)) {
Michael J Gruber9dfc3682012-11-13 11:34:11 +0100107 error("Failed to resolve '%s' as a valid ref.", *p);
Christian Couder54b0c1e2009-02-02 06:12:44 +0100108 had_error = 1;
109 continue;
110 }
Jeff King7f897b62017-03-28 15:46:30 -0400111
112 strbuf_setlen(&ref, base_len);
113 strbuf_addstr(&ref, oid_to_hex(&oid));
114 full_hex = ref.buf + base_len;
115
brian m. carlson34c290a2017-10-15 22:06:56 +0000116 if (read_ref(ref.buf, &oid)) {
Michael J Gruber9dfc3682012-11-13 11:34:11 +0100117 error("replace ref '%s' not found.", full_hex);
Christian Couder54b0c1e2009-02-02 06:12:44 +0100118 had_error = 1;
119 continue;
120 }
Jeff King7f897b62017-03-28 15:46:30 -0400121 if (fn(full_hex, ref.buf, &oid))
Christian Couder54b0c1e2009-02-02 06:12:44 +0100122 had_error = 1;
123 }
Junio C Hamano372b0502017-04-17 21:56:54 -0700124 strbuf_release(&ref);
Christian Couder54b0c1e2009-02-02 06:12:44 +0100125 return had_error;
126}
127
128static int delete_replace_ref(const char *name, const char *ref,
brian m. carlsoncea43322017-02-21 23:47:30 +0000129 const struct object_id *oid)
Christian Couder54b0c1e2009-02-02 06:12:44 +0100130{
brian m. carlson2616a5e2017-10-15 22:06:50 +0000131 if (delete_ref(NULL, ref, oid, 0))
Christian Couder54b0c1e2009-02-02 06:12:44 +0100132 return 1;
133 printf("Deleted replace ref '%s'\n", name);
134 return 0;
135}
136
brian m. carlsoncea43322017-02-21 23:47:30 +0000137static void check_ref_valid(struct object_id *object,
138 struct object_id *prev,
Jeff King7f897b62017-03-28 15:46:30 -0400139 struct strbuf *ref,
Christian Couderb6e38842014-05-17 14:16:35 +0200140 int force)
141{
Jeff King7f897b62017-03-28 15:46:30 -0400142 strbuf_reset(ref);
143 strbuf_addf(ref, "%s%s", git_replace_ref_base, oid_to_hex(object));
144 if (check_refname_format(ref->buf, 0))
145 die("'%s' is not a valid ref name.", ref->buf);
Christian Couderb6e38842014-05-17 14:16:35 +0200146
brian m. carlson34c290a2017-10-15 22:06:56 +0000147 if (read_ref(ref->buf, prev))
brian m. carlsoncea43322017-02-21 23:47:30 +0000148 oidclr(prev);
Christian Couderb6e38842014-05-17 14:16:35 +0200149 else if (!force)
Jeff King7f897b62017-03-28 15:46:30 -0400150 die("replace ref '%s' already exists", ref->buf);
Christian Couderb6e38842014-05-17 14:16:35 +0200151}
152
brian m. carlsoncea43322017-02-21 23:47:30 +0000153static int replace_object_oid(const char *object_ref,
154 struct object_id *object,
Jeff King479bd752014-04-26 22:00:56 +0200155 const char *replace_ref,
brian m. carlsoncea43322017-02-21 23:47:30 +0000156 struct object_id *repl,
Jeff King479bd752014-04-26 22:00:56 +0200157 int force)
Christian Couderbebdd272009-02-02 06:12:53 +0100158{
brian m. carlsoncea43322017-02-21 23:47:30 +0000159 struct object_id prev;
Christian Couder277336a2013-09-06 07:10:53 +0200160 enum object_type obj_type, repl_type;
Jeff King7f897b62017-03-28 15:46:30 -0400161 struct strbuf ref = STRBUF_INIT;
Ronnie Sahlberg867c2fa2014-04-16 15:32:29 -0700162 struct ref_transaction *transaction;
163 struct strbuf err = STRBUF_INIT;
Christian Couderbebdd272009-02-02 06:12:53 +0100164
brian m. carlsoncea43322017-02-21 23:47:30 +0000165 obj_type = sha1_object_info(object->hash, NULL);
166 repl_type = sha1_object_info(repl->hash, NULL);
Christian Couder277336a2013-09-06 07:10:53 +0200167 if (!force && obj_type != repl_type)
168 die("Objects must be of the same type.\n"
169 "'%s' points to a replaced object of type '%s'\n"
170 "while '%s' points to a replacement object of type '%s'.",
Brandon Williamsdebca9d2018-02-14 10:59:24 -0800171 object_ref, type_name(obj_type),
172 replace_ref, type_name(repl_type));
Christian Couder277336a2013-09-06 07:10:53 +0200173
Jeff King7f897b62017-03-28 15:46:30 -0400174 check_ref_valid(object, &prev, &ref, force);
Christian Couderbebdd272009-02-02 06:12:53 +0100175
Ronnie Sahlberg867c2fa2014-04-16 15:32:29 -0700176 transaction = ref_transaction_begin(&err);
177 if (!transaction ||
brian m. carlson89f3bbd2017-10-15 22:06:53 +0000178 ref_transaction_update(transaction, ref.buf, repl, &prev,
Michael Haggerty1d147bd2015-02-17 18:00:15 +0100179 0, NULL, &err) ||
Ronnie Sahlbergdb7516a2014-04-30 12:22:42 -0700180 ref_transaction_commit(transaction, &err))
Ronnie Sahlberg867c2fa2014-04-16 15:32:29 -0700181 die("%s", err.buf);
Christian Couderbebdd272009-02-02 06:12:53 +0100182
Ronnie Sahlberg867c2fa2014-04-16 15:32:29 -0700183 ref_transaction_free(transaction);
Jeff King7f897b62017-03-28 15:46:30 -0400184 strbuf_release(&ref);
Christian Couderbebdd272009-02-02 06:12:53 +0100185 return 0;
186}
187
Jeff King479bd752014-04-26 22:00:56 +0200188static int replace_object(const char *object_ref, const char *replace_ref, int force)
189{
brian m. carlsoncea43322017-02-21 23:47:30 +0000190 struct object_id object, repl;
Jeff King479bd752014-04-26 22:00:56 +0200191
brian m. carlsoncea43322017-02-21 23:47:30 +0000192 if (get_oid(object_ref, &object))
Jeff King479bd752014-04-26 22:00:56 +0200193 die("Failed to resolve '%s' as a valid ref.", object_ref);
brian m. carlsoncea43322017-02-21 23:47:30 +0000194 if (get_oid(replace_ref, &repl))
Jeff King479bd752014-04-26 22:00:56 +0200195 die("Failed to resolve '%s' as a valid ref.", replace_ref);
196
brian m. carlsoncea43322017-02-21 23:47:30 +0000197 return replace_object_oid(object_ref, &object, replace_ref, &repl, force);
Jeff King479bd752014-04-26 22:00:56 +0200198}
199
Jeff Kingb892bb42014-04-26 22:00:57 +0200200/*
Jeff King2deda622014-06-24 05:46:31 -0400201 * Write the contents of the object named by "sha1" to the file "filename".
202 * If "raw" is true, then the object's raw contents are printed according to
203 * "type". Otherwise, we pretty-print the contents for human editing.
Jeff Kingb892bb42014-04-26 22:00:57 +0200204 */
brian m. carlsoncea43322017-02-21 23:47:30 +0000205static void export_object(const struct object_id *oid, enum object_type type,
Jeff King2deda622014-06-24 05:46:31 -0400206 int raw, const char *filename)
Jeff Kingb892bb42014-04-26 22:00:57 +0200207{
René Scharfed3180272014-08-19 21:09:35 +0200208 struct child_process cmd = CHILD_PROCESS_INIT;
Jeff Kingb892bb42014-04-26 22:00:57 +0200209 int fd;
210
211 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
212 if (fd < 0)
213 die_errno("unable to open %s for writing", filename);
214
Jeff King36857e02014-06-24 05:46:05 -0400215 argv_array_push(&cmd.args, "--no-replace-objects");
216 argv_array_push(&cmd.args, "cat-file");
Jeff King2deda622014-06-24 05:46:31 -0400217 if (raw)
Brandon Williamsdebca9d2018-02-14 10:59:24 -0800218 argv_array_push(&cmd.args, type_name(type));
Jeff King2deda622014-06-24 05:46:31 -0400219 else
220 argv_array_push(&cmd.args, "-p");
brian m. carlsoncea43322017-02-21 23:47:30 +0000221 argv_array_push(&cmd.args, oid_to_hex(oid));
Jeff Kingb892bb42014-04-26 22:00:57 +0200222 cmd.git_cmd = 1;
223 cmd.out = fd;
224
225 if (run_command(&cmd))
226 die("cat-file reported failure");
Jeff Kingb892bb42014-04-26 22:00:57 +0200227}
228
229/*
230 * Read a previously-exported (and possibly edited) object back from "filename",
231 * interpreting it as "type", and writing the result to the object database.
232 * The sha1 of the written object is returned via sha1.
233 */
brian m. carlsoncea43322017-02-21 23:47:30 +0000234static void import_object(struct object_id *oid, enum object_type type,
Jeff King2deda622014-06-24 05:46:31 -0400235 int raw, const char *filename)
Jeff Kingb892bb42014-04-26 22:00:57 +0200236{
237 int fd;
238
239 fd = open(filename, O_RDONLY);
240 if (fd < 0)
241 die_errno("unable to open %s for reading", filename);
242
Jeff King2deda622014-06-24 05:46:31 -0400243 if (!raw && type == OBJ_TREE) {
Jeff Kingb892bb42014-04-26 22:00:57 +0200244 const char *argv[] = { "mktree", NULL };
René Scharfed3180272014-08-19 21:09:35 +0200245 struct child_process cmd = CHILD_PROCESS_INIT;
Jeff Kingb892bb42014-04-26 22:00:57 +0200246 struct strbuf result = STRBUF_INIT;
247
248 cmd.argv = argv;
249 cmd.git_cmd = 1;
250 cmd.in = fd;
251 cmd.out = -1;
252
253 if (start_command(&cmd))
254 die("unable to spawn mktree");
255
256 if (strbuf_read(&result, cmd.out, 41) < 0)
257 die_errno("unable to read from mktree");
258 close(cmd.out);
259
260 if (finish_command(&cmd))
261 die("mktree reported failure");
brian m. carlsoncea43322017-02-21 23:47:30 +0000262 if (get_oid_hex(result.buf, oid) < 0)
Jeff Kingb892bb42014-04-26 22:00:57 +0200263 die("mktree did not return an object name");
264
265 strbuf_release(&result);
266 } else {
267 struct stat st;
268 int flags = HASH_FORMAT_CHECK | HASH_WRITE_OBJECT;
269
270 if (fstat(fd, &st) < 0)
271 die_errno("unable to fstat %s", filename);
Patryk Obarae3506552017-08-20 22:09:29 +0200272 if (index_fd(oid, fd, &st, type, NULL, flags) < 0)
Jeff Kingb892bb42014-04-26 22:00:57 +0200273 die("unable to write object to database");
274 /* index_fd close()s fd for us */
275 }
276
277 /*
278 * No need to close(fd) here; both run-command and index-fd
279 * will have done it for us.
280 */
281}
282
Jeff King2deda622014-06-24 05:46:31 -0400283static int edit_and_replace(const char *object_ref, int force, int raw)
Jeff Kingb892bb42014-04-26 22:00:57 +0200284{
285 char *tmpfile = git_pathdup("REPLACE_EDITOBJ");
286 enum object_type type;
Brandon Williamsefdfe112018-02-14 10:59:59 -0800287 struct object_id old_oid, new_oid, prev;
Jeff King7f897b62017-03-28 15:46:30 -0400288 struct strbuf ref = STRBUF_INIT;
Jeff Kingb892bb42014-04-26 22:00:57 +0200289
Brandon Williamsefdfe112018-02-14 10:59:59 -0800290 if (get_oid(object_ref, &old_oid) < 0)
Jeff Kingb892bb42014-04-26 22:00:57 +0200291 die("Not a valid object name: '%s'", object_ref);
292
Brandon Williamsefdfe112018-02-14 10:59:59 -0800293 type = sha1_object_info(old_oid.hash, NULL);
Jeff Kingb892bb42014-04-26 22:00:57 +0200294 if (type < 0)
Brandon Williamsefdfe112018-02-14 10:59:59 -0800295 die("unable to get object type for %s", oid_to_hex(&old_oid));
Jeff Kingb892bb42014-04-26 22:00:57 +0200296
Brandon Williamsefdfe112018-02-14 10:59:59 -0800297 check_ref_valid(&old_oid, &prev, &ref, force);
Jeff King7f897b62017-03-28 15:46:30 -0400298 strbuf_release(&ref);
Christian Couder24790832014-05-17 14:16:36 +0200299
Brandon Williamsefdfe112018-02-14 10:59:59 -0800300 export_object(&old_oid, type, raw, tmpfile);
Jeff Kingb892bb42014-04-26 22:00:57 +0200301 if (launch_editor(tmpfile, NULL, NULL) < 0)
302 die("editing object file failed");
Brandon Williamsefdfe112018-02-14 10:59:59 -0800303 import_object(&new_oid, type, raw, tmpfile);
Jeff Kingb892bb42014-04-26 22:00:57 +0200304
305 free(tmpfile);
306
Brandon Williamsefdfe112018-02-14 10:59:59 -0800307 if (!oidcmp(&old_oid, &new_oid))
308 return error("new object is the same as the old one: '%s'", oid_to_hex(&old_oid));
Christian Couderf22166b2014-05-17 14:16:34 +0200309
Brandon Williamsefdfe112018-02-14 10:59:59 -0800310 return replace_object_oid(object_ref, &old_oid, "replacement", &new_oid, force);
Jeff Kingb892bb42014-04-26 22:00:57 +0200311}
312
Christian Couder4228e8b2014-07-19 17:01:08 +0200313static void replace_parents(struct strbuf *buf, int argc, const char **argv)
314{
315 struct strbuf new_parents = STRBUF_INIT;
316 const char *parent_start, *parent_end;
317 int i;
318
319 /* find existing parents */
320 parent_start = buf->buf;
brian m. carlsoncea43322017-02-21 23:47:30 +0000321 parent_start += GIT_SHA1_HEXSZ + 6; /* "tree " + "hex sha1" + "\n" */
Christian Couder4228e8b2014-07-19 17:01:08 +0200322 parent_end = parent_start;
323
324 while (starts_with(parent_end, "parent "))
325 parent_end += 48; /* "parent " + "hex sha1" + "\n" */
326
327 /* prepare new parents */
328 for (i = 0; i < argc; i++) {
brian m. carlsoncea43322017-02-21 23:47:30 +0000329 struct object_id oid;
330 if (get_oid(argv[i], &oid) < 0)
Christian Couder4228e8b2014-07-19 17:01:08 +0200331 die(_("Not a valid object name: '%s'"), argv[i]);
brian m. carlsonbc832662017-05-06 22:10:10 +0000332 lookup_commit_or_die(&oid, argv[i]);
brian m. carlsoncea43322017-02-21 23:47:30 +0000333 strbuf_addf(&new_parents, "parent %s\n", oid_to_hex(&oid));
Christian Couder4228e8b2014-07-19 17:01:08 +0200334 }
335
336 /* replace existing parents with new ones */
337 strbuf_splice(buf, parent_start - buf->buf, parent_end - parent_start,
338 new_parents.buf, new_parents.len);
339
340 strbuf_release(&new_parents);
341}
342
Christian Couder25a05a82014-07-19 17:01:14 +0200343struct check_mergetag_data {
344 int argc;
345 const char **argv;
346};
347
348static void check_one_mergetag(struct commit *commit,
349 struct commit_extra_header *extra,
350 void *data)
351{
352 struct check_mergetag_data *mergetag_data = (struct check_mergetag_data *)data;
353 const char *ref = mergetag_data->argv[0];
brian m. carlsoncea43322017-02-21 23:47:30 +0000354 struct object_id tag_oid;
Christian Couder25a05a82014-07-19 17:01:14 +0200355 struct tag *tag;
356 int i;
357
Brandon Williamsdebca9d2018-02-14 10:59:24 -0800358 hash_sha1_file(extra->value, extra->len, type_name(OBJ_TAG), tag_oid.hash);
brian m. carlsond3101b52017-05-06 22:10:19 +0000359 tag = lookup_tag(&tag_oid);
Christian Couder25a05a82014-07-19 17:01:14 +0200360 if (!tag)
361 die(_("bad mergetag in commit '%s'"), ref);
362 if (parse_tag_buffer(tag, extra->value, extra->len))
363 die(_("malformed mergetag in commit '%s'"), ref);
364
365 /* iterate over new parents */
366 for (i = 1; i < mergetag_data->argc; i++) {
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000367 struct object_id oid;
brian m. carlsone82caf32017-07-13 23:49:28 +0000368 if (get_oid(mergetag_data->argv[i], &oid) < 0)
Christian Couder25a05a82014-07-19 17:01:14 +0200369 die(_("Not a valid object name: '%s'"), mergetag_data->argv[i]);
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000370 if (!oidcmp(&tag->tagged->oid, &oid))
Christian Couder25a05a82014-07-19 17:01:14 +0200371 return; /* found */
372 }
373
374 die(_("original commit '%s' contains mergetag '%s' that is discarded; "
brian m. carlsoncea43322017-02-21 23:47:30 +0000375 "use --edit instead of --graft"), ref, oid_to_hex(&tag_oid));
Christian Couder25a05a82014-07-19 17:01:14 +0200376}
377
378static void check_mergetags(struct commit *commit, int argc, const char **argv)
379{
380 struct check_mergetag_data mergetag_data;
381
382 mergetag_data.argc = argc;
383 mergetag_data.argv = argv;
384 for_each_mergetag(check_one_mergetag, commit, &mergetag_data);
385}
386
Christian Couder4228e8b2014-07-19 17:01:08 +0200387static int create_graft(int argc, const char **argv, int force)
388{
Brandon Williamsefdfe112018-02-14 10:59:59 -0800389 struct object_id old_oid, new_oid;
Christian Couder4228e8b2014-07-19 17:01:08 +0200390 const char *old_ref = argv[0];
391 struct commit *commit;
392 struct strbuf buf = STRBUF_INIT;
393 const char *buffer;
394 unsigned long size;
395
Brandon Williamsefdfe112018-02-14 10:59:59 -0800396 if (get_oid(old_ref, &old_oid) < 0)
Christian Couder4228e8b2014-07-19 17:01:08 +0200397 die(_("Not a valid object name: '%s'"), old_ref);
Brandon Williamsefdfe112018-02-14 10:59:59 -0800398 commit = lookup_commit_or_die(&old_oid, old_ref);
Christian Couder4228e8b2014-07-19 17:01:08 +0200399
400 buffer = get_commit_buffer(commit, &size);
401 strbuf_add(&buf, buffer, size);
402 unuse_commit_buffer(commit, buffer);
403
404 replace_parents(&buf, argc - 1, &argv[1]);
405
Christian Couder0b05ab62014-07-19 17:01:12 +0200406 if (remove_signature(&buf)) {
407 warning(_("the original commit '%s' has a gpg signature."), old_ref);
408 warning(_("the signature will be removed in the replacement commit!"));
409 }
410
Christian Couder25a05a82014-07-19 17:01:14 +0200411 check_mergetags(commit, argc, argv);
412
Brandon Williamsefdfe112018-02-14 10:59:59 -0800413 if (write_sha1_file(buf.buf, buf.len, commit_type, new_oid.hash))
Christian Couder4228e8b2014-07-19 17:01:08 +0200414 die(_("could not write replacement commit for: '%s'"), old_ref);
415
416 strbuf_release(&buf);
417
Brandon Williamsefdfe112018-02-14 10:59:59 -0800418 if (!oidcmp(&old_oid, &new_oid))
419 return error("new commit is the same as the old one: '%s'", oid_to_hex(&old_oid));
Christian Couder4228e8b2014-07-19 17:01:08 +0200420
Brandon Williamsefdfe112018-02-14 10:59:59 -0800421 return replace_object_oid(old_ref, &old_oid, "replacement", &new_oid, force);
Christian Couder4228e8b2014-07-19 17:01:08 +0200422}
423
Christian Couder54b0c1e2009-02-02 06:12:44 +0100424int cmd_replace(int argc, const char **argv, const char *prefix)
425{
Jeff King70c7bd62014-04-26 22:00:55 +0200426 int force = 0;
Jeff King2deda622014-06-24 05:46:31 -0400427 int raw = 0;
Christian Couder44f9f852013-12-11 08:46:10 +0100428 const char *format = NULL;
Jeff King70c7bd62014-04-26 22:00:55 +0200429 enum {
430 MODE_UNSPECIFIED = 0,
431 MODE_LIST,
432 MODE_DELETE,
Jeff Kingb892bb42014-04-26 22:00:57 +0200433 MODE_EDIT,
Christian Couder4228e8b2014-07-19 17:01:08 +0200434 MODE_GRAFT,
Jeff King70c7bd62014-04-26 22:00:55 +0200435 MODE_REPLACE
436 } cmdmode = MODE_UNSPECIFIED;
Christian Couder54b0c1e2009-02-02 06:12:44 +0100437 struct option options[] = {
Jeff King70c7bd62014-04-26 22:00:55 +0200438 OPT_CMDMODE('l', "list", &cmdmode, N_("list replace refs"), MODE_LIST),
439 OPT_CMDMODE('d', "delete", &cmdmode, N_("delete replace refs"), MODE_DELETE),
Jeff Kingb892bb42014-04-26 22:00:57 +0200440 OPT_CMDMODE('e', "edit", &cmdmode, N_("edit existing object"), MODE_EDIT),
Christian Couder4228e8b2014-07-19 17:01:08 +0200441 OPT_CMDMODE('g', "graft", &cmdmode, N_("change a commit's parents"), MODE_GRAFT),
Jonathan Nieder80f165a2013-09-24 23:35:24 -0700442 OPT_BOOL('f', "force", &force, N_("replace the ref if it exists")),
Jeff King2deda622014-06-24 05:46:31 -0400443 OPT_BOOL(0, "raw", &raw, N_("do not pretty-print contents for --edit")),
Christian Couder44f9f852013-12-11 08:46:10 +0100444 OPT_STRING(0, "format", &format, N_("format"), N_("use this format")),
Christian Couder54b0c1e2009-02-02 06:12:44 +0100445 OPT_END()
446 };
447
Michael Haggertyafc711b2014-02-18 12:24:55 +0100448 check_replace_refs = 0;
Johannes Schindelin36b14372016-04-20 08:38:03 +0200449 git_config(git_default_config, NULL);
Christian Couder769a4fa2013-12-11 08:46:12 +0100450
Christian Couder451bb212009-02-02 06:12:58 +0100451 argc = parse_options(argc, argv, prefix, options, git_replace_usage, 0);
Christian Couder54b0c1e2009-02-02 06:12:44 +0100452
Jeff King70c7bd62014-04-26 22:00:55 +0200453 if (!cmdmode)
454 cmdmode = argc ? MODE_REPLACE : MODE_LIST;
Jeff King3f495f62014-04-26 22:00:54 +0200455
Jeff King70c7bd62014-04-26 22:00:55 +0200456 if (format && cmdmode != MODE_LIST)
Jeff King3f495f62014-04-26 22:00:54 +0200457 usage_msg_opt("--format cannot be used when not listing",
Christian Couder44f9f852013-12-11 08:46:10 +0100458 git_replace_usage, options);
459
Christian Couder4228e8b2014-07-19 17:01:08 +0200460 if (force &&
461 cmdmode != MODE_REPLACE &&
462 cmdmode != MODE_EDIT &&
463 cmdmode != MODE_GRAFT)
Jeff King70c7bd62014-04-26 22:00:55 +0200464 usage_msg_opt("-f only makes sense when writing a replacement",
Christian Couder86af2ca2009-02-02 06:13:06 +0100465 git_replace_usage, options);
Christian Couderbebdd272009-02-02 06:12:53 +0100466
Jeff King2deda622014-06-24 05:46:31 -0400467 if (raw && cmdmode != MODE_EDIT)
468 usage_msg_opt("--raw only makes sense with --edit",
469 git_replace_usage, options);
470
Jeff King70c7bd62014-04-26 22:00:55 +0200471 switch (cmdmode) {
472 case MODE_DELETE:
Christian Couder54b0c1e2009-02-02 06:12:44 +0100473 if (argc < 1)
Christian Couder86af2ca2009-02-02 06:13:06 +0100474 usage_msg_opt("-d needs at least one argument",
475 git_replace_usage, options);
Christian Couder54b0c1e2009-02-02 06:12:44 +0100476 return for_each_replace_name(argv, delete_replace_ref);
Christian Couder54b0c1e2009-02-02 06:12:44 +0100477
Jeff King70c7bd62014-04-26 22:00:55 +0200478 case MODE_REPLACE:
Christian Couderbebdd272009-02-02 06:12:53 +0100479 if (argc != 2)
Christian Couder86af2ca2009-02-02 06:13:06 +0100480 usage_msg_opt("bad number of arguments",
481 git_replace_usage, options);
Christian Couderbebdd272009-02-02 06:12:53 +0100482 return replace_object(argv[0], argv[1], force);
Jeff King70c7bd62014-04-26 22:00:55 +0200483
Jeff Kingb892bb42014-04-26 22:00:57 +0200484 case MODE_EDIT:
485 if (argc != 1)
486 usage_msg_opt("-e needs exactly one argument",
487 git_replace_usage, options);
Jeff King2deda622014-06-24 05:46:31 -0400488 return edit_and_replace(argv[0], force, raw);
Jeff Kingb892bb42014-04-26 22:00:57 +0200489
Christian Couder4228e8b2014-07-19 17:01:08 +0200490 case MODE_GRAFT:
491 if (argc < 1)
492 usage_msg_opt("-g needs at least one argument",
493 git_replace_usage, options);
494 return create_graft(argc, argv, force);
495
Jeff King70c7bd62014-04-26 22:00:55 +0200496 case MODE_LIST:
497 if (argc > 1)
498 usage_msg_opt("only one pattern can be given with -l",
499 git_replace_usage, options);
500 return list_replace_refs(argv[0], format);
501
502 default:
503 die("BUG: invalid cmdmode %d", (int)cmdmode);
Christian Couderbebdd272009-02-02 06:12:53 +0100504 }
Christian Couder54b0c1e2009-02-02 06:12:44 +0100505}