blob: affcdfb4169899c2affee51593f0453d5d9b9add [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"
Stefan Beller60ce76d2018-04-11 17:21:10 -070017#include "object-store.h"
18#include "repository.h"
Christian Couder25a05a82014-07-19 17:01:14 +020019#include "tag.h"
Christian Couder54b0c1e2009-02-02 06:12:44 +010020
21static const char * const git_replace_usage[] = {
Nguyễn Thái Ngọc Duy2477beb2012-08-20 19:32:36 +070022 N_("git replace [-f] <object> <replacement>"),
Christian Couderab77c302014-05-17 14:16:38 +020023 N_("git replace [-f] --edit <object>"),
Christian Couder4228e8b2014-07-19 17:01:08 +020024 N_("git replace [-f] --graft <commit> [<parent>...]"),
Johannes Schindelinfb404292018-04-29 00:44:35 +020025 N_("git replace [-f] --convert-graft-file"),
Nguyễn Thái Ngọc Duy2477beb2012-08-20 19:32:36 +070026 N_("git replace -d <object>..."),
Christian Couder44f9f852013-12-11 08:46:10 +010027 N_("git replace [--format=<format>] [-l [<pattern>]]"),
Christian Couder54b0c1e2009-02-02 06:12:44 +010028 NULL
29};
30
Christian Couder663a8562013-12-28 12:00:05 +010031enum replace_format {
Jeff King3cc9d872014-06-24 05:43:07 -040032 REPLACE_FORMAT_SHORT,
33 REPLACE_FORMAT_MEDIUM,
34 REPLACE_FORMAT_LONG
Christian Couder663a8562013-12-28 12:00:05 +010035};
Christian Couder44f9f852013-12-11 08:46:10 +010036
37struct show_data {
38 const char *pattern;
Christian Couder663a8562013-12-28 12:00:05 +010039 enum replace_format format;
Christian Couder44f9f852013-12-11 08:46:10 +010040};
41
Stefan Beller212e0f72018-08-20 18:24:19 +000042static int show_reference(struct repository *r, const char *refname,
43 const struct object_id *oid,
Christian Couder54b0c1e2009-02-02 06:12:44 +010044 int flag, void *cb_data)
45{
Christian Couder44f9f852013-12-11 08:46:10 +010046 struct show_data *data = cb_data;
Christian Couder54b0c1e2009-02-02 06:12:44 +010047
Ævar Arnfjörð Bjarmason55d34262017-06-22 21:38:08 +000048 if (!wildmatch(data->pattern, refname, 0)) {
Christian Couder663a8562013-12-28 12:00:05 +010049 if (data->format == REPLACE_FORMAT_SHORT)
Christian Couder44f9f852013-12-11 08:46:10 +010050 printf("%s\n", refname);
Christian Couder663a8562013-12-28 12:00:05 +010051 else if (data->format == REPLACE_FORMAT_MEDIUM)
Michael Haggertyd70d7a82015-05-25 18:38:44 +000052 printf("%s -> %s\n", refname, oid_to_hex(oid));
Christian Couder663a8562013-12-28 12:00:05 +010053 else { /* data->format == REPLACE_FORMAT_LONG */
Michael Haggertyd70d7a82015-05-25 18:38:44 +000054 struct object_id object;
Christian Couder44f9f852013-12-11 08:46:10 +010055 enum object_type obj_type, repl_type;
56
brian m. carlsone82caf32017-07-13 23:49:28 +000057 if (get_oid(refname, &object))
Nguyễn Thái Ngọc Duy225c62e2018-07-21 09:49:25 +020058 return error(_("failed to resolve '%s' as a valid ref"), refname);
Christian Couder44f9f852013-12-11 08:46:10 +010059
Stefan Beller212e0f72018-08-20 18:24:19 +000060 obj_type = oid_object_info(r, &object, NULL);
61 repl_type = oid_object_info(r, oid, NULL);
Christian Couder44f9f852013-12-11 08:46:10 +010062
Brandon Williamsdebca9d2018-02-14 10:59:24 -080063 printf("%s (%s) -> %s (%s)\n", refname, type_name(obj_type),
64 oid_to_hex(oid), type_name(repl_type));
Christian Couder44f9f852013-12-11 08:46:10 +010065 }
66 }
Christian Couder54b0c1e2009-02-02 06:12:44 +010067
68 return 0;
69}
70
Christian Couder44f9f852013-12-11 08:46:10 +010071static int list_replace_refs(const char *pattern, const char *format)
Christian Couder54b0c1e2009-02-02 06:12:44 +010072{
Christian Couder44f9f852013-12-11 08:46:10 +010073 struct show_data data;
74
Christian Couder54b0c1e2009-02-02 06:12:44 +010075 if (pattern == NULL)
76 pattern = "*";
Christian Couder44f9f852013-12-11 08:46:10 +010077 data.pattern = pattern;
Christian Couder54b0c1e2009-02-02 06:12:44 +010078
Christian Couder44f9f852013-12-11 08:46:10 +010079 if (format == NULL || *format == '\0' || !strcmp(format, "short"))
Christian Couder663a8562013-12-28 12:00:05 +010080 data.format = REPLACE_FORMAT_SHORT;
Christian Couder44f9f852013-12-11 08:46:10 +010081 else if (!strcmp(format, "medium"))
Christian Couder663a8562013-12-28 12:00:05 +010082 data.format = REPLACE_FORMAT_MEDIUM;
83 else if (!strcmp(format, "long"))
84 data.format = REPLACE_FORMAT_LONG;
Christian Couder44f9f852013-12-11 08:46:10 +010085 else
Nguyễn Thái Ngọc Duy225c62e2018-07-21 09:49:25 +020086 return error(_("invalid replace format '%s'\n"
87 "valid formats are 'short', 'medium' and 'long'"),
Johannes Schindeline24e8712018-04-29 00:44:26 +020088 format);
Christian Couder44f9f852013-12-11 08:46:10 +010089
Stefan Beller60ce76d2018-04-11 17:21:10 -070090 for_each_replace_ref(the_repository, show_reference, (void *)&data);
Christian Couder54b0c1e2009-02-02 06:12:44 +010091
92 return 0;
93}
94
95typedef int (*each_replace_name_fn)(const char *name, const char *ref,
brian m. carlsoncea43322017-02-21 23:47:30 +000096 const struct object_id *oid);
Christian Couder54b0c1e2009-02-02 06:12:44 +010097
98static int for_each_replace_name(const char **argv, each_replace_name_fn fn)
99{
Michael J Gruber9dfc3682012-11-13 11:34:11 +0100100 const char **p, *full_hex;
Jeff King7f897b62017-03-28 15:46:30 -0400101 struct strbuf ref = STRBUF_INIT;
102 size_t base_len;
Christian Couder54b0c1e2009-02-02 06:12:44 +0100103 int had_error = 0;
brian m. carlsoncea43322017-02-21 23:47:30 +0000104 struct object_id oid;
Christian Couder54b0c1e2009-02-02 06:12:44 +0100105
Jeff King7f897b62017-03-28 15:46:30 -0400106 strbuf_addstr(&ref, git_replace_ref_base);
107 base_len = ref.len;
108
Christian Couder54b0c1e2009-02-02 06:12:44 +0100109 for (p = argv; *p; p++) {
brian m. carlsoncea43322017-02-21 23:47:30 +0000110 if (get_oid(*p, &oid)) {
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +0200111 error("failed to resolve '%s' as a valid ref", *p);
Christian Couder54b0c1e2009-02-02 06:12:44 +0100112 had_error = 1;
113 continue;
114 }
Jeff King7f897b62017-03-28 15:46:30 -0400115
116 strbuf_setlen(&ref, base_len);
117 strbuf_addstr(&ref, oid_to_hex(&oid));
118 full_hex = ref.buf + base_len;
119
brian m. carlson34c290a2017-10-15 22:06:56 +0000120 if (read_ref(ref.buf, &oid)) {
Nguyễn Thái Ngọc Duy225c62e2018-07-21 09:49:25 +0200121 error(_("replace ref '%s' not found"), full_hex);
Christian Couder54b0c1e2009-02-02 06:12:44 +0100122 had_error = 1;
123 continue;
124 }
Jeff King7f897b62017-03-28 15:46:30 -0400125 if (fn(full_hex, ref.buf, &oid))
Christian Couder54b0c1e2009-02-02 06:12:44 +0100126 had_error = 1;
127 }
Junio C Hamano372b0502017-04-17 21:56:54 -0700128 strbuf_release(&ref);
Christian Couder54b0c1e2009-02-02 06:12:44 +0100129 return had_error;
130}
131
132static int delete_replace_ref(const char *name, const char *ref,
brian m. carlsoncea43322017-02-21 23:47:30 +0000133 const struct object_id *oid)
Christian Couder54b0c1e2009-02-02 06:12:44 +0100134{
brian m. carlson2616a5e2017-10-15 22:06:50 +0000135 if (delete_ref(NULL, ref, oid, 0))
Christian Couder54b0c1e2009-02-02 06:12:44 +0100136 return 1;
Nguyễn Thái Ngọc Duy225c62e2018-07-21 09:49:25 +0200137 printf_ln(_("Deleted replace ref '%s'"), name);
Christian Couder54b0c1e2009-02-02 06:12:44 +0100138 return 0;
139}
140
Johannes Schindeline24e8712018-04-29 00:44:26 +0200141static int check_ref_valid(struct object_id *object,
brian m. carlsoncea43322017-02-21 23:47:30 +0000142 struct object_id *prev,
Jeff King7f897b62017-03-28 15:46:30 -0400143 struct strbuf *ref,
Christian Couderb6e38842014-05-17 14:16:35 +0200144 int force)
145{
Jeff King7f897b62017-03-28 15:46:30 -0400146 strbuf_reset(ref);
147 strbuf_addf(ref, "%s%s", git_replace_ref_base, oid_to_hex(object));
148 if (check_refname_format(ref->buf, 0))
Nguyễn Thái Ngọc Duy225c62e2018-07-21 09:49:25 +0200149 return error(_("'%s' is not a valid ref name"), ref->buf);
Christian Couderb6e38842014-05-17 14:16:35 +0200150
brian m. carlson34c290a2017-10-15 22:06:56 +0000151 if (read_ref(ref->buf, prev))
brian m. carlsoncea43322017-02-21 23:47:30 +0000152 oidclr(prev);
Christian Couderb6e38842014-05-17 14:16:35 +0200153 else if (!force)
Nguyễn Thái Ngọc Duy225c62e2018-07-21 09:49:25 +0200154 return error(_("replace ref '%s' already exists"), ref->buf);
Johannes Schindeline24e8712018-04-29 00:44:26 +0200155 return 0;
Christian Couderb6e38842014-05-17 14:16:35 +0200156}
157
brian m. carlsoncea43322017-02-21 23:47:30 +0000158static int replace_object_oid(const char *object_ref,
159 struct object_id *object,
Jeff King479bd752014-04-26 22:00:56 +0200160 const char *replace_ref,
brian m. carlsoncea43322017-02-21 23:47:30 +0000161 struct object_id *repl,
Jeff King479bd752014-04-26 22:00:56 +0200162 int force)
Christian Couderbebdd272009-02-02 06:12:53 +0100163{
brian m. carlsoncea43322017-02-21 23:47:30 +0000164 struct object_id prev;
Christian Couder277336a2013-09-06 07:10:53 +0200165 enum object_type obj_type, repl_type;
Jeff King7f897b62017-03-28 15:46:30 -0400166 struct strbuf ref = STRBUF_INIT;
Ronnie Sahlberg867c2fa2014-04-16 15:32:29 -0700167 struct ref_transaction *transaction;
168 struct strbuf err = STRBUF_INIT;
Johannes Schindeline24e8712018-04-29 00:44:26 +0200169 int res = 0;
Christian Couderbebdd272009-02-02 06:12:53 +0100170
Stefan Beller0df8e962018-04-25 11:20:59 -0700171 obj_type = oid_object_info(the_repository, object, NULL);
172 repl_type = oid_object_info(the_repository, repl, NULL);
Christian Couder277336a2013-09-06 07:10:53 +0200173 if (!force && obj_type != repl_type)
Nguyễn Thái Ngọc Duy225c62e2018-07-21 09:49:25 +0200174 return error(_("Objects must be of the same type.\n"
175 "'%s' points to a replaced object of type '%s'\n"
176 "while '%s' points to a replacement object of "
177 "type '%s'."),
Johannes Schindeline24e8712018-04-29 00:44:26 +0200178 object_ref, type_name(obj_type),
179 replace_ref, type_name(repl_type));
Christian Couder277336a2013-09-06 07:10:53 +0200180
Johannes Schindeline24e8712018-04-29 00:44:26 +0200181 if (check_ref_valid(object, &prev, &ref, force)) {
182 strbuf_release(&ref);
183 return -1;
184 }
Christian Couderbebdd272009-02-02 06:12:53 +0100185
Ronnie Sahlberg867c2fa2014-04-16 15:32:29 -0700186 transaction = ref_transaction_begin(&err);
187 if (!transaction ||
brian m. carlson89f3bbd2017-10-15 22:06:53 +0000188 ref_transaction_update(transaction, ref.buf, repl, &prev,
Michael Haggerty1d147bd2015-02-17 18:00:15 +0100189 0, NULL, &err) ||
Ronnie Sahlbergdb7516a2014-04-30 12:22:42 -0700190 ref_transaction_commit(transaction, &err))
Johannes Schindeline24e8712018-04-29 00:44:26 +0200191 res = error("%s", err.buf);
Christian Couderbebdd272009-02-02 06:12:53 +0100192
Ronnie Sahlberg867c2fa2014-04-16 15:32:29 -0700193 ref_transaction_free(transaction);
Jeff King7f897b62017-03-28 15:46:30 -0400194 strbuf_release(&ref);
Johannes Schindeline24e8712018-04-29 00:44:26 +0200195 return res;
Christian Couderbebdd272009-02-02 06:12:53 +0100196}
197
Jeff King479bd752014-04-26 22:00:56 +0200198static int replace_object(const char *object_ref, const char *replace_ref, int force)
199{
brian m. carlsoncea43322017-02-21 23:47:30 +0000200 struct object_id object, repl;
Jeff King479bd752014-04-26 22:00:56 +0200201
brian m. carlsoncea43322017-02-21 23:47:30 +0000202 if (get_oid(object_ref, &object))
Nguyễn Thái Ngọc Duy225c62e2018-07-21 09:49:25 +0200203 return error(_("failed to resolve '%s' as a valid ref"),
Johannes Schindeline24e8712018-04-29 00:44:26 +0200204 object_ref);
brian m. carlsoncea43322017-02-21 23:47:30 +0000205 if (get_oid(replace_ref, &repl))
Nguyễn Thái Ngọc Duy225c62e2018-07-21 09:49:25 +0200206 return error(_("failed to resolve '%s' as a valid ref"),
Johannes Schindeline24e8712018-04-29 00:44:26 +0200207 replace_ref);
Jeff King479bd752014-04-26 22:00:56 +0200208
brian m. carlsoncea43322017-02-21 23:47:30 +0000209 return replace_object_oid(object_ref, &object, replace_ref, &repl, force);
Jeff King479bd752014-04-26 22:00:56 +0200210}
211
Jeff Kingb892bb42014-04-26 22:00:57 +0200212/*
Jeff King2deda622014-06-24 05:46:31 -0400213 * Write the contents of the object named by "sha1" to the file "filename".
214 * If "raw" is true, then the object's raw contents are printed according to
215 * "type". Otherwise, we pretty-print the contents for human editing.
Jeff Kingb892bb42014-04-26 22:00:57 +0200216 */
Johannes Schindeline24e8712018-04-29 00:44:26 +0200217static int export_object(const struct object_id *oid, enum object_type type,
Jeff King2deda622014-06-24 05:46:31 -0400218 int raw, const char *filename)
Jeff Kingb892bb42014-04-26 22:00:57 +0200219{
René Scharfed3180272014-08-19 21:09:35 +0200220 struct child_process cmd = CHILD_PROCESS_INIT;
Jeff Kingb892bb42014-04-26 22:00:57 +0200221 int fd;
222
223 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
224 if (fd < 0)
Nguyễn Thái Ngọc Duy225c62e2018-07-21 09:49:25 +0200225 return error_errno(_("unable to open %s for writing"), filename);
Jeff Kingb892bb42014-04-26 22:00:57 +0200226
Jeff King36857e02014-06-24 05:46:05 -0400227 argv_array_push(&cmd.args, "--no-replace-objects");
228 argv_array_push(&cmd.args, "cat-file");
Jeff King2deda622014-06-24 05:46:31 -0400229 if (raw)
Brandon Williamsdebca9d2018-02-14 10:59:24 -0800230 argv_array_push(&cmd.args, type_name(type));
Jeff King2deda622014-06-24 05:46:31 -0400231 else
232 argv_array_push(&cmd.args, "-p");
brian m. carlsoncea43322017-02-21 23:47:30 +0000233 argv_array_push(&cmd.args, oid_to_hex(oid));
Jeff Kingb892bb42014-04-26 22:00:57 +0200234 cmd.git_cmd = 1;
235 cmd.out = fd;
236
237 if (run_command(&cmd))
Nguyễn Thái Ngọc Duy225c62e2018-07-21 09:49:25 +0200238 return error(_("cat-file reported failure"));
Johannes Schindeline24e8712018-04-29 00:44:26 +0200239 return 0;
Jeff Kingb892bb42014-04-26 22:00:57 +0200240}
241
242/*
243 * Read a previously-exported (and possibly edited) object back from "filename",
244 * interpreting it as "type", and writing the result to the object database.
245 * The sha1 of the written object is returned via sha1.
246 */
Johannes Schindeline24e8712018-04-29 00:44:26 +0200247static int import_object(struct object_id *oid, enum object_type type,
Jeff King2deda622014-06-24 05:46:31 -0400248 int raw, const char *filename)
Jeff Kingb892bb42014-04-26 22:00:57 +0200249{
250 int fd;
251
252 fd = open(filename, O_RDONLY);
253 if (fd < 0)
Nguyễn Thái Ngọc Duy225c62e2018-07-21 09:49:25 +0200254 return error_errno(_("unable to open %s for reading"), filename);
Jeff Kingb892bb42014-04-26 22:00:57 +0200255
Jeff King2deda622014-06-24 05:46:31 -0400256 if (!raw && type == OBJ_TREE) {
Jeff Kingb892bb42014-04-26 22:00:57 +0200257 const char *argv[] = { "mktree", NULL };
René Scharfed3180272014-08-19 21:09:35 +0200258 struct child_process cmd = CHILD_PROCESS_INIT;
Jeff Kingb892bb42014-04-26 22:00:57 +0200259 struct strbuf result = STRBUF_INIT;
260
261 cmd.argv = argv;
262 cmd.git_cmd = 1;
263 cmd.in = fd;
264 cmd.out = -1;
265
Johannes Schindeline24e8712018-04-29 00:44:26 +0200266 if (start_command(&cmd)) {
267 close(fd);
Nguyễn Thái Ngọc Duy225c62e2018-07-21 09:49:25 +0200268 return error(_("unable to spawn mktree"));
Johannes Schindeline24e8712018-04-29 00:44:26 +0200269 }
Jeff Kingb892bb42014-04-26 22:00:57 +0200270
Johannes Schindeline24e8712018-04-29 00:44:26 +0200271 if (strbuf_read(&result, cmd.out, 41) < 0) {
Nguyễn Thái Ngọc Duy225c62e2018-07-21 09:49:25 +0200272 error_errno(_("unable to read from mktree"));
Johannes Schindeline24e8712018-04-29 00:44:26 +0200273 close(fd);
274 close(cmd.out);
275 return -1;
276 }
Jeff Kingb892bb42014-04-26 22:00:57 +0200277 close(cmd.out);
278
Johannes Schindeline24e8712018-04-29 00:44:26 +0200279 if (finish_command(&cmd)) {
280 strbuf_release(&result);
Nguyễn Thái Ngọc Duy225c62e2018-07-21 09:49:25 +0200281 return error(_("mktree reported failure"));
Johannes Schindeline24e8712018-04-29 00:44:26 +0200282 }
283 if (get_oid_hex(result.buf, oid) < 0) {
284 strbuf_release(&result);
Nguyễn Thái Ngọc Duy225c62e2018-07-21 09:49:25 +0200285 return error(_("mktree did not return an object name"));
Johannes Schindeline24e8712018-04-29 00:44:26 +0200286 }
Jeff Kingb892bb42014-04-26 22:00:57 +0200287
288 strbuf_release(&result);
289 } else {
290 struct stat st;
291 int flags = HASH_FORMAT_CHECK | HASH_WRITE_OBJECT;
292
Johannes Schindeline24e8712018-04-29 00:44:26 +0200293 if (fstat(fd, &st) < 0) {
Nguyễn Thái Ngọc Duy225c62e2018-07-21 09:49:25 +0200294 error_errno(_("unable to fstat %s"), filename);
Johannes Schindeline24e8712018-04-29 00:44:26 +0200295 close(fd);
296 return -1;
297 }
Nguyễn Thái Ngọc Duy58bf2a42018-09-21 17:57:31 +0200298 if (index_fd(&the_index, oid, fd, &st, type, NULL, flags) < 0)
Nguyễn Thái Ngọc Duy225c62e2018-07-21 09:49:25 +0200299 return error(_("unable to write object to database"));
Jeff Kingb892bb42014-04-26 22:00:57 +0200300 /* index_fd close()s fd for us */
301 }
302
303 /*
304 * No need to close(fd) here; both run-command and index-fd
305 * will have done it for us.
306 */
Johannes Schindeline24e8712018-04-29 00:44:26 +0200307 return 0;
Jeff Kingb892bb42014-04-26 22:00:57 +0200308}
309
Jeff King2deda622014-06-24 05:46:31 -0400310static int edit_and_replace(const char *object_ref, int force, int raw)
Jeff Kingb892bb42014-04-26 22:00:57 +0200311{
Johannes Schindeline24e8712018-04-29 00:44:26 +0200312 char *tmpfile;
Jeff Kingb892bb42014-04-26 22:00:57 +0200313 enum object_type type;
Brandon Williamsefdfe112018-02-14 10:59:59 -0800314 struct object_id old_oid, new_oid, prev;
Jeff King7f897b62017-03-28 15:46:30 -0400315 struct strbuf ref = STRBUF_INIT;
Jeff Kingb892bb42014-04-26 22:00:57 +0200316
Brandon Williamsefdfe112018-02-14 10:59:59 -0800317 if (get_oid(object_ref, &old_oid) < 0)
Nguyễn Thái Ngọc Duy225c62e2018-07-21 09:49:25 +0200318 return error(_("not a valid object name: '%s'"), object_ref);
Jeff Kingb892bb42014-04-26 22:00:57 +0200319
Stefan Beller0df8e962018-04-25 11:20:59 -0700320 type = oid_object_info(the_repository, &old_oid, NULL);
Jeff Kingb892bb42014-04-26 22:00:57 +0200321 if (type < 0)
Nguyễn Thái Ngọc Duy225c62e2018-07-21 09:49:25 +0200322 return error(_("unable to get object type for %s"),
Johannes Schindeline24e8712018-04-29 00:44:26 +0200323 oid_to_hex(&old_oid));
Jeff Kingb892bb42014-04-26 22:00:57 +0200324
Johannes Schindeline24e8712018-04-29 00:44:26 +0200325 if (check_ref_valid(&old_oid, &prev, &ref, force)) {
326 strbuf_release(&ref);
327 return -1;
328 }
Jeff King7f897b62017-03-28 15:46:30 -0400329 strbuf_release(&ref);
Christian Couder24790832014-05-17 14:16:36 +0200330
Johannes Schindeline24e8712018-04-29 00:44:26 +0200331 tmpfile = git_pathdup("REPLACE_EDITOBJ");
332 if (export_object(&old_oid, type, raw, tmpfile)) {
333 free(tmpfile);
334 return -1;
335 }
336 if (launch_editor(tmpfile, NULL, NULL) < 0) {
337 free(tmpfile);
Nguyễn Thái Ngọc Duy225c62e2018-07-21 09:49:25 +0200338 return error(_("editing object file failed"));
Johannes Schindeline24e8712018-04-29 00:44:26 +0200339 }
340 if (import_object(&new_oid, type, raw, tmpfile)) {
341 free(tmpfile);
342 return -1;
343 }
Jeff Kingb892bb42014-04-26 22:00:57 +0200344 free(tmpfile);
345
Jeff King4a7e27e2018-08-28 17:22:40 -0400346 if (oideq(&old_oid, &new_oid))
Nguyễn Thái Ngọc Duy225c62e2018-07-21 09:49:25 +0200347 return error(_("new object is the same as the old one: '%s'"), oid_to_hex(&old_oid));
Christian Couderf22166b2014-05-17 14:16:34 +0200348
Brandon Williamsefdfe112018-02-14 10:59:59 -0800349 return replace_object_oid(object_ref, &old_oid, "replacement", &new_oid, force);
Jeff Kingb892bb42014-04-26 22:00:57 +0200350}
351
Johannes Schindeline24e8712018-04-29 00:44:26 +0200352static int replace_parents(struct strbuf *buf, int argc, const char **argv)
Christian Couder4228e8b2014-07-19 17:01:08 +0200353{
354 struct strbuf new_parents = STRBUF_INIT;
355 const char *parent_start, *parent_end;
356 int i;
357
358 /* find existing parents */
359 parent_start = buf->buf;
brian m. carlsoncea43322017-02-21 23:47:30 +0000360 parent_start += GIT_SHA1_HEXSZ + 6; /* "tree " + "hex sha1" + "\n" */
Christian Couder4228e8b2014-07-19 17:01:08 +0200361 parent_end = parent_start;
362
363 while (starts_with(parent_end, "parent "))
364 parent_end += 48; /* "parent " + "hex sha1" + "\n" */
365
366 /* prepare new parents */
367 for (i = 0; i < argc; i++) {
brian m. carlsoncea43322017-02-21 23:47:30 +0000368 struct object_id oid;
Johannes Schindeline24e8712018-04-29 00:44:26 +0200369 if (get_oid(argv[i], &oid) < 0) {
370 strbuf_release(&new_parents);
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +0200371 return error(_("not a valid object name: '%s'"),
Johannes Schindeline24e8712018-04-29 00:44:26 +0200372 argv[i]);
373 }
Stefan Beller2122f672018-06-28 18:21:58 -0700374 if (!lookup_commit_reference(the_repository, &oid)) {
Johannes Schindeline24e8712018-04-29 00:44:26 +0200375 strbuf_release(&new_parents);
376 return error(_("could not parse %s"), argv[i]);
377 }
brian m. carlsoncea43322017-02-21 23:47:30 +0000378 strbuf_addf(&new_parents, "parent %s\n", oid_to_hex(&oid));
Christian Couder4228e8b2014-07-19 17:01:08 +0200379 }
380
381 /* replace existing parents with new ones */
382 strbuf_splice(buf, parent_start - buf->buf, parent_end - parent_start,
383 new_parents.buf, new_parents.len);
384
385 strbuf_release(&new_parents);
Johannes Schindeline24e8712018-04-29 00:44:26 +0200386 return 0;
Christian Couder4228e8b2014-07-19 17:01:08 +0200387}
388
Christian Couder25a05a82014-07-19 17:01:14 +0200389struct check_mergetag_data {
390 int argc;
391 const char **argv;
392};
393
Johannes Schindelinfef461e2018-04-25 11:54:04 +0200394static int check_one_mergetag(struct commit *commit,
Christian Couder25a05a82014-07-19 17:01:14 +0200395 struct commit_extra_header *extra,
396 void *data)
397{
398 struct check_mergetag_data *mergetag_data = (struct check_mergetag_data *)data;
399 const char *ref = mergetag_data->argv[0];
brian m. carlsoncea43322017-02-21 23:47:30 +0000400 struct object_id tag_oid;
Christian Couder25a05a82014-07-19 17:01:14 +0200401 struct tag *tag;
402 int i;
403
Junio C Hamano169c9c02018-03-06 14:54:07 -0800404 hash_object_file(extra->value, extra->len, type_name(OBJ_TAG), &tag_oid);
Stefan Bellerce71efb2018-06-28 18:22:03 -0700405 tag = lookup_tag(the_repository, &tag_oid);
Christian Couder25a05a82014-07-19 17:01:14 +0200406 if (!tag)
Johannes Schindeline24e8712018-04-29 00:44:26 +0200407 return error(_("bad mergetag in commit '%s'"), ref);
Stefan Beller0e740fe2018-06-28 18:22:04 -0700408 if (parse_tag_buffer(the_repository, tag, extra->value, extra->len))
Johannes Schindeline24e8712018-04-29 00:44:26 +0200409 return error(_("malformed mergetag in commit '%s'"), ref);
Christian Couder25a05a82014-07-19 17:01:14 +0200410
411 /* iterate over new parents */
412 for (i = 1; i < mergetag_data->argc; i++) {
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000413 struct object_id oid;
brian m. carlsone82caf32017-07-13 23:49:28 +0000414 if (get_oid(mergetag_data->argv[i], &oid) < 0)
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +0200415 return error(_("not a valid object name: '%s'"),
Johannes Schindeline24e8712018-04-29 00:44:26 +0200416 mergetag_data->argv[i]);
Jeff King4a7e27e2018-08-28 17:22:40 -0400417 if (oideq(&tag->tagged->oid, &oid))
Johannes Schindelinfef461e2018-04-25 11:54:04 +0200418 return 0; /* found */
Christian Couder25a05a82014-07-19 17:01:14 +0200419 }
420
Johannes Schindeline24e8712018-04-29 00:44:26 +0200421 return error(_("original commit '%s' contains mergetag '%s' that is "
422 "discarded; use --edit instead of --graft"), ref,
423 oid_to_hex(&tag_oid));
Christian Couder25a05a82014-07-19 17:01:14 +0200424}
425
Johannes Schindelinfef461e2018-04-25 11:54:04 +0200426static int check_mergetags(struct commit *commit, int argc, const char **argv)
Christian Couder25a05a82014-07-19 17:01:14 +0200427{
428 struct check_mergetag_data mergetag_data;
429
430 mergetag_data.argc = argc;
431 mergetag_data.argv = argv;
Johannes Schindelinfef461e2018-04-25 11:54:04 +0200432 return for_each_mergetag(check_one_mergetag, commit, &mergetag_data);
Christian Couder25a05a82014-07-19 17:01:14 +0200433}
434
Johannes Schindelin041c98e2018-04-29 00:44:32 +0200435static int create_graft(int argc, const char **argv, int force, int gentle)
Christian Couder4228e8b2014-07-19 17:01:08 +0200436{
Brandon Williamsefdfe112018-02-14 10:59:59 -0800437 struct object_id old_oid, new_oid;
Christian Couder4228e8b2014-07-19 17:01:08 +0200438 const char *old_ref = argv[0];
439 struct commit *commit;
440 struct strbuf buf = STRBUF_INIT;
441 const char *buffer;
442 unsigned long size;
443
Brandon Williamsefdfe112018-02-14 10:59:59 -0800444 if (get_oid(old_ref, &old_oid) < 0)
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +0200445 return error(_("not a valid object name: '%s'"), old_ref);
Stefan Beller2122f672018-06-28 18:21:58 -0700446 commit = lookup_commit_reference(the_repository, &old_oid);
Johannes Schindeline24e8712018-04-29 00:44:26 +0200447 if (!commit)
448 return error(_("could not parse %s"), old_ref);
Christian Couder4228e8b2014-07-19 17:01:08 +0200449
450 buffer = get_commit_buffer(commit, &size);
451 strbuf_add(&buf, buffer, size);
452 unuse_commit_buffer(commit, buffer);
453
Johannes Schindeline24e8712018-04-29 00:44:26 +0200454 if (replace_parents(&buf, argc - 1, &argv[1]) < 0) {
455 strbuf_release(&buf);
456 return -1;
457 }
Christian Couder4228e8b2014-07-19 17:01:08 +0200458
Christian Couder0b05ab62014-07-19 17:01:12 +0200459 if (remove_signature(&buf)) {
Nguyễn Thái Ngọc Duy1a07e592018-07-21 09:49:19 +0200460 warning(_("the original commit '%s' has a gpg signature"), old_ref);
Christian Couder0b05ab62014-07-19 17:01:12 +0200461 warning(_("the signature will be removed in the replacement commit!"));
462 }
463
Johannes Schindeline24e8712018-04-29 00:44:26 +0200464 if (check_mergetags(commit, argc, argv)) {
465 strbuf_release(&buf);
466 return -1;
467 }
Christian Couder25a05a82014-07-19 17:01:14 +0200468
Johannes Schindeline24e8712018-04-29 00:44:26 +0200469 if (write_object_file(buf.buf, buf.len, commit_type, &new_oid)) {
470 strbuf_release(&buf);
471 return error(_("could not write replacement commit for: '%s'"),
472 old_ref);
473 }
Christian Couder4228e8b2014-07-19 17:01:08 +0200474
475 strbuf_release(&buf);
476
Jeff King4a7e27e2018-08-28 17:22:40 -0400477 if (oideq(&old_oid, &new_oid)) {
Johannes Schindelin041c98e2018-04-29 00:44:32 +0200478 if (gentle) {
Nguyễn Thái Ngọc Duy225c62e2018-07-21 09:49:25 +0200479 warning(_("graft for '%s' unnecessary"), oid_to_hex(&old_oid));
Johannes Schindelin041c98e2018-04-29 00:44:32 +0200480 return 0;
481 }
Nguyễn Thái Ngọc Duy225c62e2018-07-21 09:49:25 +0200482 return error(_("new commit is the same as the old one: '%s'"), oid_to_hex(&old_oid));
Johannes Schindelin041c98e2018-04-29 00:44:32 +0200483 }
Christian Couder4228e8b2014-07-19 17:01:08 +0200484
Brandon Williamsefdfe112018-02-14 10:59:59 -0800485 return replace_object_oid(old_ref, &old_oid, "replacement", &new_oid, force);
Christian Couder4228e8b2014-07-19 17:01:08 +0200486}
487
Johannes Schindelinfb404292018-04-29 00:44:35 +0200488static int convert_graft_file(int force)
489{
Junio C Hamanob16b60f2018-06-29 10:24:33 -0700490 const char *graft_file = get_graft_file(the_repository);
Johannes Schindelinfb404292018-04-29 00:44:35 +0200491 FILE *fp = fopen_or_warn(graft_file, "r");
492 struct strbuf buf = STRBUF_INIT, err = STRBUF_INIT;
493 struct argv_array args = ARGV_ARRAY_INIT;
494
495 if (!fp)
496 return -1;
497
Ævar Arnfjörð Bjarmason8821e902018-11-27 21:12:55 +0100498 advice_graft_file_deprecated = 0;
Johannes Schindelinfb404292018-04-29 00:44:35 +0200499 while (strbuf_getline(&buf, fp) != EOF) {
500 if (*buf.buf == '#')
501 continue;
502
503 argv_array_split(&args, buf.buf);
504 if (args.argc && create_graft(args.argc, args.argv, force, 1))
505 strbuf_addf(&err, "\n\t%s", buf.buf);
506 argv_array_clear(&args);
507 }
508 fclose(fp);
509
510 strbuf_release(&buf);
511
512 if (!err.len)
513 return unlink_or_warn(graft_file);
514
515 warning(_("could not convert the following graft(s):\n%s"), err.buf);
516 strbuf_release(&err);
517
518 return -1;
519}
520
Christian Couder54b0c1e2009-02-02 06:12:44 +0100521int cmd_replace(int argc, const char **argv, const char *prefix)
522{
Jeff King70c7bd62014-04-26 22:00:55 +0200523 int force = 0;
Jeff King2deda622014-06-24 05:46:31 -0400524 int raw = 0;
Christian Couder44f9f852013-12-11 08:46:10 +0100525 const char *format = NULL;
Jeff King70c7bd62014-04-26 22:00:55 +0200526 enum {
527 MODE_UNSPECIFIED = 0,
528 MODE_LIST,
529 MODE_DELETE,
Jeff Kingb892bb42014-04-26 22:00:57 +0200530 MODE_EDIT,
Christian Couder4228e8b2014-07-19 17:01:08 +0200531 MODE_GRAFT,
Johannes Schindelinfb404292018-04-29 00:44:35 +0200532 MODE_CONVERT_GRAFT_FILE,
Jeff King70c7bd62014-04-26 22:00:55 +0200533 MODE_REPLACE
534 } cmdmode = MODE_UNSPECIFIED;
Christian Couder54b0c1e2009-02-02 06:12:44 +0100535 struct option options[] = {
Jeff King70c7bd62014-04-26 22:00:55 +0200536 OPT_CMDMODE('l', "list", &cmdmode, N_("list replace refs"), MODE_LIST),
537 OPT_CMDMODE('d', "delete", &cmdmode, N_("delete replace refs"), MODE_DELETE),
Jeff Kingb892bb42014-04-26 22:00:57 +0200538 OPT_CMDMODE('e', "edit", &cmdmode, N_("edit existing object"), MODE_EDIT),
Christian Couder4228e8b2014-07-19 17:01:08 +0200539 OPT_CMDMODE('g', "graft", &cmdmode, N_("change a commit's parents"), MODE_GRAFT),
Johannes Schindelinfb404292018-04-29 00:44:35 +0200540 OPT_CMDMODE(0, "convert-graft-file", &cmdmode, N_("convert existing graft file"), MODE_CONVERT_GRAFT_FILE),
Nguyễn Thái Ngọc Duy1b354752018-02-09 18:02:13 +0700541 OPT_BOOL_F('f', "force", &force, N_("replace the ref if it exists"),
542 PARSE_OPT_NOCOMPLETE),
Jeff King2deda622014-06-24 05:46:31 -0400543 OPT_BOOL(0, "raw", &raw, N_("do not pretty-print contents for --edit")),
Christian Couder44f9f852013-12-11 08:46:10 +0100544 OPT_STRING(0, "format", &format, N_("format"), N_("use this format")),
Christian Couder54b0c1e2009-02-02 06:12:44 +0100545 OPT_END()
546 };
547
Jeff King6ebd1ca2018-07-18 16:45:20 -0400548 read_replace_refs = 0;
Johannes Schindelin36b14372016-04-20 08:38:03 +0200549 git_config(git_default_config, NULL);
Christian Couder769a4fa2013-12-11 08:46:12 +0100550
Christian Couder451bb212009-02-02 06:12:58 +0100551 argc = parse_options(argc, argv, prefix, options, git_replace_usage, 0);
Christian Couder54b0c1e2009-02-02 06:12:44 +0100552
Jeff King70c7bd62014-04-26 22:00:55 +0200553 if (!cmdmode)
554 cmdmode = argc ? MODE_REPLACE : MODE_LIST;
Jeff King3f495f62014-04-26 22:00:54 +0200555
Jeff King70c7bd62014-04-26 22:00:55 +0200556 if (format && cmdmode != MODE_LIST)
Nguyễn Thái Ngọc Duy225c62e2018-07-21 09:49:25 +0200557 usage_msg_opt(_("--format cannot be used when not listing"),
Christian Couder44f9f852013-12-11 08:46:10 +0100558 git_replace_usage, options);
559
Christian Couder4228e8b2014-07-19 17:01:08 +0200560 if (force &&
561 cmdmode != MODE_REPLACE &&
562 cmdmode != MODE_EDIT &&
Johannes Schindelinfb404292018-04-29 00:44:35 +0200563 cmdmode != MODE_GRAFT &&
564 cmdmode != MODE_CONVERT_GRAFT_FILE)
Nguyễn Thái Ngọc Duy225c62e2018-07-21 09:49:25 +0200565 usage_msg_opt(_("-f only makes sense when writing a replacement"),
Christian Couder86af2ca2009-02-02 06:13:06 +0100566 git_replace_usage, options);
Christian Couderbebdd272009-02-02 06:12:53 +0100567
Jeff King2deda622014-06-24 05:46:31 -0400568 if (raw && cmdmode != MODE_EDIT)
Nguyễn Thái Ngọc Duy225c62e2018-07-21 09:49:25 +0200569 usage_msg_opt(_("--raw only makes sense with --edit"),
Jeff King2deda622014-06-24 05:46:31 -0400570 git_replace_usage, options);
571
Jeff King70c7bd62014-04-26 22:00:55 +0200572 switch (cmdmode) {
573 case MODE_DELETE:
Christian Couder54b0c1e2009-02-02 06:12:44 +0100574 if (argc < 1)
Nguyễn Thái Ngọc Duy225c62e2018-07-21 09:49:25 +0200575 usage_msg_opt(_("-d needs at least one argument"),
Christian Couder86af2ca2009-02-02 06:13:06 +0100576 git_replace_usage, options);
Christian Couder54b0c1e2009-02-02 06:12:44 +0100577 return for_each_replace_name(argv, delete_replace_ref);
Christian Couder54b0c1e2009-02-02 06:12:44 +0100578
Jeff King70c7bd62014-04-26 22:00:55 +0200579 case MODE_REPLACE:
Christian Couderbebdd272009-02-02 06:12:53 +0100580 if (argc != 2)
Nguyễn Thái Ngọc Duy225c62e2018-07-21 09:49:25 +0200581 usage_msg_opt(_("bad number of arguments"),
Christian Couder86af2ca2009-02-02 06:13:06 +0100582 git_replace_usage, options);
Christian Couderbebdd272009-02-02 06:12:53 +0100583 return replace_object(argv[0], argv[1], force);
Jeff King70c7bd62014-04-26 22:00:55 +0200584
Jeff Kingb892bb42014-04-26 22:00:57 +0200585 case MODE_EDIT:
586 if (argc != 1)
Nguyễn Thái Ngọc Duy225c62e2018-07-21 09:49:25 +0200587 usage_msg_opt(_("-e needs exactly one argument"),
Jeff Kingb892bb42014-04-26 22:00:57 +0200588 git_replace_usage, options);
Jeff King2deda622014-06-24 05:46:31 -0400589 return edit_and_replace(argv[0], force, raw);
Jeff Kingb892bb42014-04-26 22:00:57 +0200590
Christian Couder4228e8b2014-07-19 17:01:08 +0200591 case MODE_GRAFT:
592 if (argc < 1)
Nguyễn Thái Ngọc Duy225c62e2018-07-21 09:49:25 +0200593 usage_msg_opt(_("-g needs at least one argument"),
Christian Couder4228e8b2014-07-19 17:01:08 +0200594 git_replace_usage, options);
Johannes Schindelin041c98e2018-04-29 00:44:32 +0200595 return create_graft(argc, argv, force, 0);
Christian Couder4228e8b2014-07-19 17:01:08 +0200596
Johannes Schindelinfb404292018-04-29 00:44:35 +0200597 case MODE_CONVERT_GRAFT_FILE:
598 if (argc != 0)
Nguyễn Thái Ngọc Duy225c62e2018-07-21 09:49:25 +0200599 usage_msg_opt(_("--convert-graft-file takes no argument"),
Johannes Schindelinfb404292018-04-29 00:44:35 +0200600 git_replace_usage, options);
601 return !!convert_graft_file(force);
Christian Couder54b0c1e2009-02-02 06:12:44 +0100602
603 case MODE_LIST:
604 if (argc > 1)
Nguyễn Thái Ngọc Duy225c62e2018-07-21 09:49:25 +0200605 usage_msg_opt(_("only one pattern can be given with -l"),
Christian Couder54b0c1e2009-02-02 06:12:44 +0100606 git_replace_usage, options);
607 return list_replace_refs(argv[0], format);
608
609 default:
Johannes Schindelind398f2e2018-04-25 11:54:06 +0200610 BUG("invalid cmdmode %d", (int)cmdmode);
Christian Couder54b0c1e2009-02-02 06:12:44 +0100611 }
612}