blob: ab17668f4330a5e688d423a60c138768b9955e1b [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"
12#include "builtin.h"
13#include "refs.h"
14#include "parse-options.h"
Jeff Kingb892bb42014-04-26 22:00:57 +020015#include "run-command.h"
Christian Couder25a05a82014-07-19 17:01:14 +020016#include "tag.h"
Christian Couder54b0c1e2009-02-02 06:12:44 +010017
18static const char * const git_replace_usage[] = {
Nguyễn Thái Ngọc Duy2477beb2012-08-20 19:32:36 +070019 N_("git replace [-f] <object> <replacement>"),
Christian Couderab77c302014-05-17 14:16:38 +020020 N_("git replace [-f] --edit <object>"),
Christian Couder4228e8b2014-07-19 17:01:08 +020021 N_("git replace [-f] --graft <commit> [<parent>...]"),
Nguyễn Thái Ngọc Duy2477beb2012-08-20 19:32:36 +070022 N_("git replace -d <object>..."),
Christian Couder44f9f852013-12-11 08:46:10 +010023 N_("git replace [--format=<format>] [-l [<pattern>]]"),
Christian Couder54b0c1e2009-02-02 06:12:44 +010024 NULL
25};
26
Christian Couder663a8562013-12-28 12:00:05 +010027enum replace_format {
Jeff King3cc9d872014-06-24 05:43:07 -040028 REPLACE_FORMAT_SHORT,
29 REPLACE_FORMAT_MEDIUM,
30 REPLACE_FORMAT_LONG
Christian Couder663a8562013-12-28 12:00:05 +010031};
Christian Couder44f9f852013-12-11 08:46:10 +010032
33struct show_data {
34 const char *pattern;
Christian Couder663a8562013-12-28 12:00:05 +010035 enum replace_format format;
Christian Couder44f9f852013-12-11 08:46:10 +010036};
37
Michael Haggertyd70d7a82015-05-25 18:38:44 +000038static int show_reference(const char *refname, const struct object_id *oid,
Christian Couder54b0c1e2009-02-02 06:12:44 +010039 int flag, void *cb_data)
40{
Christian Couder44f9f852013-12-11 08:46:10 +010041 struct show_data *data = cb_data;
Christian Couder54b0c1e2009-02-02 06:12:44 +010042
Nguyễn Thái Ngọc Duyeb078942014-02-15 09:01:46 +070043 if (!wildmatch(data->pattern, refname, 0, NULL)) {
Christian Couder663a8562013-12-28 12:00:05 +010044 if (data->format == REPLACE_FORMAT_SHORT)
Christian Couder44f9f852013-12-11 08:46:10 +010045 printf("%s\n", refname);
Christian Couder663a8562013-12-28 12:00:05 +010046 else if (data->format == REPLACE_FORMAT_MEDIUM)
Michael Haggertyd70d7a82015-05-25 18:38:44 +000047 printf("%s -> %s\n", refname, oid_to_hex(oid));
Christian Couder663a8562013-12-28 12:00:05 +010048 else { /* data->format == REPLACE_FORMAT_LONG */
Michael Haggertyd70d7a82015-05-25 18:38:44 +000049 struct object_id object;
Christian Couder44f9f852013-12-11 08:46:10 +010050 enum object_type obj_type, repl_type;
51
Michael Haggertyd70d7a82015-05-25 18:38:44 +000052 if (get_sha1(refname, object.hash))
Christian Couder44f9f852013-12-11 08:46:10 +010053 return error("Failed to resolve '%s' as a valid ref.", refname);
54
Michael Haggertyd70d7a82015-05-25 18:38:44 +000055 obj_type = sha1_object_info(object.hash, NULL);
56 repl_type = sha1_object_info(oid->hash, NULL);
Christian Couder44f9f852013-12-11 08:46:10 +010057
58 printf("%s (%s) -> %s (%s)\n", refname, typename(obj_type),
Michael Haggertyd70d7a82015-05-25 18:38:44 +000059 oid_to_hex(oid), typename(repl_type));
Christian Couder44f9f852013-12-11 08:46:10 +010060 }
61 }
Christian Couder54b0c1e2009-02-02 06:12:44 +010062
63 return 0;
64}
65
Christian Couder44f9f852013-12-11 08:46:10 +010066static int list_replace_refs(const char *pattern, const char *format)
Christian Couder54b0c1e2009-02-02 06:12:44 +010067{
Christian Couder44f9f852013-12-11 08:46:10 +010068 struct show_data data;
69
Christian Couder54b0c1e2009-02-02 06:12:44 +010070 if (pattern == NULL)
71 pattern = "*";
Christian Couder44f9f852013-12-11 08:46:10 +010072 data.pattern = pattern;
Christian Couder54b0c1e2009-02-02 06:12:44 +010073
Christian Couder44f9f852013-12-11 08:46:10 +010074 if (format == NULL || *format == '\0' || !strcmp(format, "short"))
Christian Couder663a8562013-12-28 12:00:05 +010075 data.format = REPLACE_FORMAT_SHORT;
Christian Couder44f9f852013-12-11 08:46:10 +010076 else if (!strcmp(format, "medium"))
Christian Couder663a8562013-12-28 12:00:05 +010077 data.format = REPLACE_FORMAT_MEDIUM;
78 else if (!strcmp(format, "long"))
79 data.format = REPLACE_FORMAT_LONG;
Christian Couder44f9f852013-12-11 08:46:10 +010080 else
81 die("invalid replace format '%s'\n"
Christian Couder663a8562013-12-28 12:00:05 +010082 "valid formats are 'short', 'medium' and 'long'\n",
Christian Couder44f9f852013-12-11 08:46:10 +010083 format);
84
Michael Haggertyd70d7a82015-05-25 18:38:44 +000085 for_each_replace_ref(show_reference, (void *)&data);
Christian Couder54b0c1e2009-02-02 06:12:44 +010086
87 return 0;
88}
89
90typedef int (*each_replace_name_fn)(const char *name, const char *ref,
brian m. carlsoncea43322017-02-21 23:47:30 +000091 const struct object_id *oid);
Christian Couder54b0c1e2009-02-02 06:12:44 +010092
93static int for_each_replace_name(const char **argv, each_replace_name_fn fn)
94{
Michael J Gruber9dfc3682012-11-13 11:34:11 +010095 const char **p, *full_hex;
Jeff King7f897b62017-03-28 15:46:30 -040096 struct strbuf ref = STRBUF_INIT;
97 size_t base_len;
Christian Couder54b0c1e2009-02-02 06:12:44 +010098 int had_error = 0;
brian m. carlsoncea43322017-02-21 23:47:30 +000099 struct object_id oid;
Christian Couder54b0c1e2009-02-02 06:12:44 +0100100
Jeff King7f897b62017-03-28 15:46:30 -0400101 strbuf_addstr(&ref, git_replace_ref_base);
102 base_len = ref.len;
103
Christian Couder54b0c1e2009-02-02 06:12:44 +0100104 for (p = argv; *p; p++) {
brian m. carlsoncea43322017-02-21 23:47:30 +0000105 if (get_oid(*p, &oid)) {
Michael J Gruber9dfc3682012-11-13 11:34:11 +0100106 error("Failed to resolve '%s' as a valid ref.", *p);
Christian Couder54b0c1e2009-02-02 06:12:44 +0100107 had_error = 1;
108 continue;
109 }
Jeff King7f897b62017-03-28 15:46:30 -0400110
111 strbuf_setlen(&ref, base_len);
112 strbuf_addstr(&ref, oid_to_hex(&oid));
113 full_hex = ref.buf + base_len;
114
115 if (read_ref(ref.buf, oid.hash)) {
Michael J Gruber9dfc3682012-11-13 11:34:11 +0100116 error("replace ref '%s' not found.", full_hex);
Christian Couder54b0c1e2009-02-02 06:12:44 +0100117 had_error = 1;
118 continue;
119 }
Jeff King7f897b62017-03-28 15:46:30 -0400120 if (fn(full_hex, ref.buf, &oid))
Christian Couder54b0c1e2009-02-02 06:12:44 +0100121 had_error = 1;
122 }
Junio C Hamano372b0502017-04-17 21:56:54 -0700123 strbuf_release(&ref);
Christian Couder54b0c1e2009-02-02 06:12:44 +0100124 return had_error;
125}
126
127static int delete_replace_ref(const char *name, const char *ref,
brian m. carlsoncea43322017-02-21 23:47:30 +0000128 const struct object_id *oid)
Christian Couder54b0c1e2009-02-02 06:12:44 +0100129{
Junio C Hamanoe1fae932017-03-17 13:50:24 -0700130 if (delete_ref(NULL, ref, oid->hash, 0))
Christian Couder54b0c1e2009-02-02 06:12:44 +0100131 return 1;
132 printf("Deleted replace ref '%s'\n", name);
133 return 0;
134}
135
brian m. carlsoncea43322017-02-21 23:47:30 +0000136static void check_ref_valid(struct object_id *object,
137 struct object_id *prev,
Jeff King7f897b62017-03-28 15:46:30 -0400138 struct strbuf *ref,
Christian Couderb6e38842014-05-17 14:16:35 +0200139 int force)
140{
Jeff King7f897b62017-03-28 15:46:30 -0400141 strbuf_reset(ref);
142 strbuf_addf(ref, "%s%s", git_replace_ref_base, oid_to_hex(object));
143 if (check_refname_format(ref->buf, 0))
144 die("'%s' is not a valid ref name.", ref->buf);
Christian Couderb6e38842014-05-17 14:16:35 +0200145
Jeff King7f897b62017-03-28 15:46:30 -0400146 if (read_ref(ref->buf, prev->hash))
brian m. carlsoncea43322017-02-21 23:47:30 +0000147 oidclr(prev);
Christian Couderb6e38842014-05-17 14:16:35 +0200148 else if (!force)
Jeff King7f897b62017-03-28 15:46:30 -0400149 die("replace ref '%s' already exists", ref->buf);
Christian Couderb6e38842014-05-17 14:16:35 +0200150}
151
brian m. carlsoncea43322017-02-21 23:47:30 +0000152static int replace_object_oid(const char *object_ref,
153 struct object_id *object,
Jeff King479bd752014-04-26 22:00:56 +0200154 const char *replace_ref,
brian m. carlsoncea43322017-02-21 23:47:30 +0000155 struct object_id *repl,
Jeff King479bd752014-04-26 22:00:56 +0200156 int force)
Christian Couderbebdd272009-02-02 06:12:53 +0100157{
brian m. carlsoncea43322017-02-21 23:47:30 +0000158 struct object_id prev;
Christian Couder277336a2013-09-06 07:10:53 +0200159 enum object_type obj_type, repl_type;
Jeff King7f897b62017-03-28 15:46:30 -0400160 struct strbuf ref = STRBUF_INIT;
Ronnie Sahlberg867c2fa2014-04-16 15:32:29 -0700161 struct ref_transaction *transaction;
162 struct strbuf err = STRBUF_INIT;
Christian Couderbebdd272009-02-02 06:12:53 +0100163
brian m. carlsoncea43322017-02-21 23:47:30 +0000164 obj_type = sha1_object_info(object->hash, NULL);
165 repl_type = sha1_object_info(repl->hash, NULL);
Christian Couder277336a2013-09-06 07:10:53 +0200166 if (!force && obj_type != repl_type)
167 die("Objects must be of the same type.\n"
168 "'%s' points to a replaced object of type '%s'\n"
169 "while '%s' points to a replacement object of type '%s'.",
170 object_ref, typename(obj_type),
171 replace_ref, typename(repl_type));
172
Jeff King7f897b62017-03-28 15:46:30 -0400173 check_ref_valid(object, &prev, &ref, force);
Christian Couderbebdd272009-02-02 06:12:53 +0100174
Ronnie Sahlberg867c2fa2014-04-16 15:32:29 -0700175 transaction = ref_transaction_begin(&err);
176 if (!transaction ||
Jeff King7f897b62017-03-28 15:46:30 -0400177 ref_transaction_update(transaction, ref.buf, repl->hash, prev.hash,
Michael Haggerty1d147bd2015-02-17 18:00:15 +0100178 0, NULL, &err) ||
Ronnie Sahlbergdb7516a2014-04-30 12:22:42 -0700179 ref_transaction_commit(transaction, &err))
Ronnie Sahlberg867c2fa2014-04-16 15:32:29 -0700180 die("%s", err.buf);
Christian Couderbebdd272009-02-02 06:12:53 +0100181
Ronnie Sahlberg867c2fa2014-04-16 15:32:29 -0700182 ref_transaction_free(transaction);
Jeff King7f897b62017-03-28 15:46:30 -0400183 strbuf_release(&ref);
Christian Couderbebdd272009-02-02 06:12:53 +0100184 return 0;
185}
186
Jeff King479bd752014-04-26 22:00:56 +0200187static int replace_object(const char *object_ref, const char *replace_ref, int force)
188{
brian m. carlsoncea43322017-02-21 23:47:30 +0000189 struct object_id object, repl;
Jeff King479bd752014-04-26 22:00:56 +0200190
brian m. carlsoncea43322017-02-21 23:47:30 +0000191 if (get_oid(object_ref, &object))
Jeff King479bd752014-04-26 22:00:56 +0200192 die("Failed to resolve '%s' as a valid ref.", object_ref);
brian m. carlsoncea43322017-02-21 23:47:30 +0000193 if (get_oid(replace_ref, &repl))
Jeff King479bd752014-04-26 22:00:56 +0200194 die("Failed to resolve '%s' as a valid ref.", replace_ref);
195
brian m. carlsoncea43322017-02-21 23:47:30 +0000196 return replace_object_oid(object_ref, &object, replace_ref, &repl, force);
Jeff King479bd752014-04-26 22:00:56 +0200197}
198
Jeff Kingb892bb42014-04-26 22:00:57 +0200199/*
Jeff King2deda622014-06-24 05:46:31 -0400200 * Write the contents of the object named by "sha1" to the file "filename".
201 * If "raw" is true, then the object's raw contents are printed according to
202 * "type". Otherwise, we pretty-print the contents for human editing.
Jeff Kingb892bb42014-04-26 22:00:57 +0200203 */
brian m. carlsoncea43322017-02-21 23:47:30 +0000204static void export_object(const struct object_id *oid, enum object_type type,
Jeff King2deda622014-06-24 05:46:31 -0400205 int raw, const char *filename)
Jeff Kingb892bb42014-04-26 22:00:57 +0200206{
René Scharfed3180272014-08-19 21:09:35 +0200207 struct child_process cmd = CHILD_PROCESS_INIT;
Jeff Kingb892bb42014-04-26 22:00:57 +0200208 int fd;
209
210 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
211 if (fd < 0)
212 die_errno("unable to open %s for writing", filename);
213
Jeff King36857e02014-06-24 05:46:05 -0400214 argv_array_push(&cmd.args, "--no-replace-objects");
215 argv_array_push(&cmd.args, "cat-file");
Jeff King2deda622014-06-24 05:46:31 -0400216 if (raw)
217 argv_array_push(&cmd.args, typename(type));
218 else
219 argv_array_push(&cmd.args, "-p");
brian m. carlsoncea43322017-02-21 23:47:30 +0000220 argv_array_push(&cmd.args, oid_to_hex(oid));
Jeff Kingb892bb42014-04-26 22:00:57 +0200221 cmd.git_cmd = 1;
222 cmd.out = fd;
223
224 if (run_command(&cmd))
225 die("cat-file reported failure");
Jeff Kingb892bb42014-04-26 22:00:57 +0200226}
227
228/*
229 * Read a previously-exported (and possibly edited) object back from "filename",
230 * interpreting it as "type", and writing the result to the object database.
231 * The sha1 of the written object is returned via sha1.
232 */
brian m. carlsoncea43322017-02-21 23:47:30 +0000233static void import_object(struct object_id *oid, enum object_type type,
Jeff King2deda622014-06-24 05:46:31 -0400234 int raw, const char *filename)
Jeff Kingb892bb42014-04-26 22:00:57 +0200235{
236 int fd;
237
238 fd = open(filename, O_RDONLY);
239 if (fd < 0)
240 die_errno("unable to open %s for reading", filename);
241
Jeff King2deda622014-06-24 05:46:31 -0400242 if (!raw && type == OBJ_TREE) {
Jeff Kingb892bb42014-04-26 22:00:57 +0200243 const char *argv[] = { "mktree", NULL };
René Scharfed3180272014-08-19 21:09:35 +0200244 struct child_process cmd = CHILD_PROCESS_INIT;
Jeff Kingb892bb42014-04-26 22:00:57 +0200245 struct strbuf result = STRBUF_INIT;
246
247 cmd.argv = argv;
248 cmd.git_cmd = 1;
249 cmd.in = fd;
250 cmd.out = -1;
251
252 if (start_command(&cmd))
253 die("unable to spawn mktree");
254
255 if (strbuf_read(&result, cmd.out, 41) < 0)
256 die_errno("unable to read from mktree");
257 close(cmd.out);
258
259 if (finish_command(&cmd))
260 die("mktree reported failure");
brian m. carlsoncea43322017-02-21 23:47:30 +0000261 if (get_oid_hex(result.buf, oid) < 0)
Jeff Kingb892bb42014-04-26 22:00:57 +0200262 die("mktree did not return an object name");
263
264 strbuf_release(&result);
265 } else {
266 struct stat st;
267 int flags = HASH_FORMAT_CHECK | HASH_WRITE_OBJECT;
268
269 if (fstat(fd, &st) < 0)
270 die_errno("unable to fstat %s", filename);
brian m. carlsoncea43322017-02-21 23:47:30 +0000271 if (index_fd(oid->hash, fd, &st, type, NULL, flags) < 0)
Jeff Kingb892bb42014-04-26 22:00:57 +0200272 die("unable to write object to database");
273 /* index_fd close()s fd for us */
274 }
275
276 /*
277 * No need to close(fd) here; both run-command and index-fd
278 * will have done it for us.
279 */
280}
281
Jeff King2deda622014-06-24 05:46:31 -0400282static int edit_and_replace(const char *object_ref, int force, int raw)
Jeff Kingb892bb42014-04-26 22:00:57 +0200283{
284 char *tmpfile = git_pathdup("REPLACE_EDITOBJ");
285 enum object_type type;
brian m. carlsoncea43322017-02-21 23:47:30 +0000286 struct object_id old, new, prev;
Jeff King7f897b62017-03-28 15:46:30 -0400287 struct strbuf ref = STRBUF_INIT;
Jeff Kingb892bb42014-04-26 22:00:57 +0200288
brian m. carlsoncea43322017-02-21 23:47:30 +0000289 if (get_oid(object_ref, &old) < 0)
Jeff Kingb892bb42014-04-26 22:00:57 +0200290 die("Not a valid object name: '%s'", object_ref);
291
brian m. carlsoncea43322017-02-21 23:47:30 +0000292 type = sha1_object_info(old.hash, NULL);
Jeff Kingb892bb42014-04-26 22:00:57 +0200293 if (type < 0)
brian m. carlsoncea43322017-02-21 23:47:30 +0000294 die("unable to get object type for %s", oid_to_hex(&old));
Jeff Kingb892bb42014-04-26 22:00:57 +0200295
Jeff King7f897b62017-03-28 15:46:30 -0400296 check_ref_valid(&old, &prev, &ref, force);
297 strbuf_release(&ref);
Christian Couder24790832014-05-17 14:16:36 +0200298
brian m. carlsoncea43322017-02-21 23:47:30 +0000299 export_object(&old, type, raw, tmpfile);
Jeff Kingb892bb42014-04-26 22:00:57 +0200300 if (launch_editor(tmpfile, NULL, NULL) < 0)
301 die("editing object file failed");
brian m. carlsoncea43322017-02-21 23:47:30 +0000302 import_object(&new, type, raw, tmpfile);
Jeff Kingb892bb42014-04-26 22:00:57 +0200303
304 free(tmpfile);
305
brian m. carlsoncea43322017-02-21 23:47:30 +0000306 if (!oidcmp(&old, &new))
307 return error("new object is the same as the old one: '%s'", oid_to_hex(&old));
Christian Couderf22166b2014-05-17 14:16:34 +0200308
brian m. carlsoncea43322017-02-21 23:47:30 +0000309 return replace_object_oid(object_ref, &old, "replacement", &new, force);
Jeff Kingb892bb42014-04-26 22:00:57 +0200310}
311
Christian Couder4228e8b2014-07-19 17:01:08 +0200312static void replace_parents(struct strbuf *buf, int argc, const char **argv)
313{
314 struct strbuf new_parents = STRBUF_INIT;
315 const char *parent_start, *parent_end;
316 int i;
317
318 /* find existing parents */
319 parent_start = buf->buf;
brian m. carlsoncea43322017-02-21 23:47:30 +0000320 parent_start += GIT_SHA1_HEXSZ + 6; /* "tree " + "hex sha1" + "\n" */
Christian Couder4228e8b2014-07-19 17:01:08 +0200321 parent_end = parent_start;
322
323 while (starts_with(parent_end, "parent "))
324 parent_end += 48; /* "parent " + "hex sha1" + "\n" */
325
326 /* prepare new parents */
327 for (i = 0; i < argc; i++) {
brian m. carlsoncea43322017-02-21 23:47:30 +0000328 struct object_id oid;
329 if (get_oid(argv[i], &oid) < 0)
Christian Couder4228e8b2014-07-19 17:01:08 +0200330 die(_("Not a valid object name: '%s'"), argv[i]);
brian m. carlsoncea43322017-02-21 23:47:30 +0000331 lookup_commit_or_die(oid.hash, argv[i]);
332 strbuf_addf(&new_parents, "parent %s\n", oid_to_hex(&oid));
Christian Couder4228e8b2014-07-19 17:01:08 +0200333 }
334
335 /* replace existing parents with new ones */
336 strbuf_splice(buf, parent_start - buf->buf, parent_end - parent_start,
337 new_parents.buf, new_parents.len);
338
339 strbuf_release(&new_parents);
340}
341
Christian Couder25a05a82014-07-19 17:01:14 +0200342struct check_mergetag_data {
343 int argc;
344 const char **argv;
345};
346
347static void check_one_mergetag(struct commit *commit,
348 struct commit_extra_header *extra,
349 void *data)
350{
351 struct check_mergetag_data *mergetag_data = (struct check_mergetag_data *)data;
352 const char *ref = mergetag_data->argv[0];
brian m. carlsoncea43322017-02-21 23:47:30 +0000353 struct object_id tag_oid;
Christian Couder25a05a82014-07-19 17:01:14 +0200354 struct tag *tag;
355 int i;
356
brian m. carlsoncea43322017-02-21 23:47:30 +0000357 hash_sha1_file(extra->value, extra->len, typename(OBJ_TAG), tag_oid.hash);
358 tag = lookup_tag(tag_oid.hash);
Christian Couder25a05a82014-07-19 17:01:14 +0200359 if (!tag)
360 die(_("bad mergetag in commit '%s'"), ref);
361 if (parse_tag_buffer(tag, extra->value, extra->len))
362 die(_("malformed mergetag in commit '%s'"), ref);
363
364 /* iterate over new parents */
365 for (i = 1; i < mergetag_data->argc; i++) {
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000366 struct object_id oid;
367 if (get_sha1(mergetag_data->argv[i], oid.hash) < 0)
Christian Couder25a05a82014-07-19 17:01:14 +0200368 die(_("Not a valid object name: '%s'"), mergetag_data->argv[i]);
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000369 if (!oidcmp(&tag->tagged->oid, &oid))
Christian Couder25a05a82014-07-19 17:01:14 +0200370 return; /* found */
371 }
372
373 die(_("original commit '%s' contains mergetag '%s' that is discarded; "
brian m. carlsoncea43322017-02-21 23:47:30 +0000374 "use --edit instead of --graft"), ref, oid_to_hex(&tag_oid));
Christian Couder25a05a82014-07-19 17:01:14 +0200375}
376
377static void check_mergetags(struct commit *commit, int argc, const char **argv)
378{
379 struct check_mergetag_data mergetag_data;
380
381 mergetag_data.argc = argc;
382 mergetag_data.argv = argv;
383 for_each_mergetag(check_one_mergetag, commit, &mergetag_data);
384}
385
Christian Couder4228e8b2014-07-19 17:01:08 +0200386static int create_graft(int argc, const char **argv, int force)
387{
brian m. carlsoncea43322017-02-21 23:47:30 +0000388 struct object_id old, new;
Christian Couder4228e8b2014-07-19 17:01:08 +0200389 const char *old_ref = argv[0];
390 struct commit *commit;
391 struct strbuf buf = STRBUF_INIT;
392 const char *buffer;
393 unsigned long size;
394
brian m. carlsoncea43322017-02-21 23:47:30 +0000395 if (get_oid(old_ref, &old) < 0)
Christian Couder4228e8b2014-07-19 17:01:08 +0200396 die(_("Not a valid object name: '%s'"), old_ref);
brian m. carlsoncea43322017-02-21 23:47:30 +0000397 commit = lookup_commit_or_die(old.hash, old_ref);
Christian Couder4228e8b2014-07-19 17:01:08 +0200398
399 buffer = get_commit_buffer(commit, &size);
400 strbuf_add(&buf, buffer, size);
401 unuse_commit_buffer(commit, buffer);
402
403 replace_parents(&buf, argc - 1, &argv[1]);
404
Christian Couder0b05ab62014-07-19 17:01:12 +0200405 if (remove_signature(&buf)) {
406 warning(_("the original commit '%s' has a gpg signature."), old_ref);
407 warning(_("the signature will be removed in the replacement commit!"));
408 }
409
Christian Couder25a05a82014-07-19 17:01:14 +0200410 check_mergetags(commit, argc, argv);
411
brian m. carlsoncea43322017-02-21 23:47:30 +0000412 if (write_sha1_file(buf.buf, buf.len, commit_type, new.hash))
Christian Couder4228e8b2014-07-19 17:01:08 +0200413 die(_("could not write replacement commit for: '%s'"), old_ref);
414
415 strbuf_release(&buf);
416
brian m. carlsoncea43322017-02-21 23:47:30 +0000417 if (!oidcmp(&old, &new))
418 return error("new commit is the same as the old one: '%s'", oid_to_hex(&old));
Christian Couder4228e8b2014-07-19 17:01:08 +0200419
brian m. carlsoncea43322017-02-21 23:47:30 +0000420 return replace_object_oid(old_ref, &old, "replacement", &new, force);
Christian Couder4228e8b2014-07-19 17:01:08 +0200421}
422
Christian Couder54b0c1e2009-02-02 06:12:44 +0100423int cmd_replace(int argc, const char **argv, const char *prefix)
424{
Jeff King70c7bd62014-04-26 22:00:55 +0200425 int force = 0;
Jeff King2deda622014-06-24 05:46:31 -0400426 int raw = 0;
Christian Couder44f9f852013-12-11 08:46:10 +0100427 const char *format = NULL;
Jeff King70c7bd62014-04-26 22:00:55 +0200428 enum {
429 MODE_UNSPECIFIED = 0,
430 MODE_LIST,
431 MODE_DELETE,
Jeff Kingb892bb42014-04-26 22:00:57 +0200432 MODE_EDIT,
Christian Couder4228e8b2014-07-19 17:01:08 +0200433 MODE_GRAFT,
Jeff King70c7bd62014-04-26 22:00:55 +0200434 MODE_REPLACE
435 } cmdmode = MODE_UNSPECIFIED;
Christian Couder54b0c1e2009-02-02 06:12:44 +0100436 struct option options[] = {
Jeff King70c7bd62014-04-26 22:00:55 +0200437 OPT_CMDMODE('l', "list", &cmdmode, N_("list replace refs"), MODE_LIST),
438 OPT_CMDMODE('d', "delete", &cmdmode, N_("delete replace refs"), MODE_DELETE),
Jeff Kingb892bb42014-04-26 22:00:57 +0200439 OPT_CMDMODE('e', "edit", &cmdmode, N_("edit existing object"), MODE_EDIT),
Christian Couder4228e8b2014-07-19 17:01:08 +0200440 OPT_CMDMODE('g', "graft", &cmdmode, N_("change a commit's parents"), MODE_GRAFT),
Jonathan Nieder80f165a2013-09-24 23:35:24 -0700441 OPT_BOOL('f', "force", &force, N_("replace the ref if it exists")),
Jeff King2deda622014-06-24 05:46:31 -0400442 OPT_BOOL(0, "raw", &raw, N_("do not pretty-print contents for --edit")),
Christian Couder44f9f852013-12-11 08:46:10 +0100443 OPT_STRING(0, "format", &format, N_("format"), N_("use this format")),
Christian Couder54b0c1e2009-02-02 06:12:44 +0100444 OPT_END()
445 };
446
Michael Haggertyafc711b2014-02-18 12:24:55 +0100447 check_replace_refs = 0;
Johannes Schindelin36b14372016-04-20 08:38:03 +0200448 git_config(git_default_config, NULL);
Christian Couder769a4fa2013-12-11 08:46:12 +0100449
Christian Couder451bb212009-02-02 06:12:58 +0100450 argc = parse_options(argc, argv, prefix, options, git_replace_usage, 0);
Christian Couder54b0c1e2009-02-02 06:12:44 +0100451
Jeff King70c7bd62014-04-26 22:00:55 +0200452 if (!cmdmode)
453 cmdmode = argc ? MODE_REPLACE : MODE_LIST;
Jeff King3f495f62014-04-26 22:00:54 +0200454
Jeff King70c7bd62014-04-26 22:00:55 +0200455 if (format && cmdmode != MODE_LIST)
Jeff King3f495f62014-04-26 22:00:54 +0200456 usage_msg_opt("--format cannot be used when not listing",
Christian Couder44f9f852013-12-11 08:46:10 +0100457 git_replace_usage, options);
458
Christian Couder4228e8b2014-07-19 17:01:08 +0200459 if (force &&
460 cmdmode != MODE_REPLACE &&
461 cmdmode != MODE_EDIT &&
462 cmdmode != MODE_GRAFT)
Jeff King70c7bd62014-04-26 22:00:55 +0200463 usage_msg_opt("-f only makes sense when writing a replacement",
Christian Couder86af2ca2009-02-02 06:13:06 +0100464 git_replace_usage, options);
Christian Couderbebdd272009-02-02 06:12:53 +0100465
Jeff King2deda622014-06-24 05:46:31 -0400466 if (raw && cmdmode != MODE_EDIT)
467 usage_msg_opt("--raw only makes sense with --edit",
468 git_replace_usage, options);
469
Jeff King70c7bd62014-04-26 22:00:55 +0200470 switch (cmdmode) {
471 case MODE_DELETE:
Christian Couder54b0c1e2009-02-02 06:12:44 +0100472 if (argc < 1)
Christian Couder86af2ca2009-02-02 06:13:06 +0100473 usage_msg_opt("-d needs at least one argument",
474 git_replace_usage, options);
Christian Couder54b0c1e2009-02-02 06:12:44 +0100475 return for_each_replace_name(argv, delete_replace_ref);
Christian Couder54b0c1e2009-02-02 06:12:44 +0100476
Jeff King70c7bd62014-04-26 22:00:55 +0200477 case MODE_REPLACE:
Christian Couderbebdd272009-02-02 06:12:53 +0100478 if (argc != 2)
Christian Couder86af2ca2009-02-02 06:13:06 +0100479 usage_msg_opt("bad number of arguments",
480 git_replace_usage, options);
Christian Couderbebdd272009-02-02 06:12:53 +0100481 return replace_object(argv[0], argv[1], force);
Jeff King70c7bd62014-04-26 22:00:55 +0200482
Jeff Kingb892bb42014-04-26 22:00:57 +0200483 case MODE_EDIT:
484 if (argc != 1)
485 usage_msg_opt("-e needs exactly one argument",
486 git_replace_usage, options);
Jeff King2deda622014-06-24 05:46:31 -0400487 return edit_and_replace(argv[0], force, raw);
Jeff Kingb892bb42014-04-26 22:00:57 +0200488
Christian Couder4228e8b2014-07-19 17:01:08 +0200489 case MODE_GRAFT:
490 if (argc < 1)
491 usage_msg_opt("-g needs at least one argument",
492 git_replace_usage, options);
493 return create_graft(argc, argv, force);
494
Jeff King70c7bd62014-04-26 22:00:55 +0200495 case MODE_LIST:
496 if (argc > 1)
497 usage_msg_opt("only one pattern can be given with -l",
498 git_replace_usage, options);
499 return list_replace_refs(argv[0], format);
500
501 default:
502 die("BUG: invalid cmdmode %d", (int)cmdmode);
Christian Couderbebdd272009-02-02 06:12:53 +0100503 }
Christian Couder54b0c1e2009-02-02 06:12:44 +0100504}