]>
Commit | Line | Data |
---|---|---|
54b0c1e0 CC |
1 | /* |
2 | * Builtin "git replace" | |
3 | * | |
4 | * Copyright (c) 2008 Christian Couder <chriscool@tuxfamily.org> | |
5 | * | |
09b7e220 | 6 | * Based on builtin/tag.c by Kristian Høgsberg <krh@redhat.com> |
54b0c1e0 CC |
7 | * 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" | |
b2141fc1 | 12 | #include "config.h" |
54b0c1e0 | 13 | #include "builtin.h" |
32a8f510 | 14 | #include "environment.h" |
f394e093 | 15 | #include "gettext.h" |
41771fa4 | 16 | #include "hex.h" |
54b0c1e0 CC |
17 | #include "refs.h" |
18 | #include "parse-options.h" | |
b892bb45 | 19 | #include "run-command.h" |
dabab1d6 | 20 | #include "object-name.h" |
60ce76d3 | 21 | #include "object-store.h" |
cbeab747 | 22 | #include "replace-object.h" |
60ce76d3 | 23 | #include "repository.h" |
25a05a8c | 24 | #include "tag.h" |
54b0c1e0 CC |
25 | |
26 | static const char * const git_replace_usage[] = { | |
2477bebb | 27 | N_("git replace [-f] <object> <replacement>"), |
ab77c309 | 28 | N_("git replace [-f] --edit <object>"), |
4228e8bc | 29 | N_("git replace [-f] --graft <commit> [<parent>...]"), |
959d670d | 30 | "git replace [-f] --convert-graft-file", |
2477bebb | 31 | N_("git replace -d <object>..."), |
44f9f850 | 32 | N_("git replace [--format=<format>] [-l [<pattern>]]"), |
54b0c1e0 CC |
33 | NULL |
34 | }; | |
35 | ||
663a8566 | 36 | enum replace_format { |
3cc9d877 JK |
37 | REPLACE_FORMAT_SHORT, |
38 | REPLACE_FORMAT_MEDIUM, | |
39 | REPLACE_FORMAT_LONG | |
663a8566 | 40 | }; |
44f9f850 CC |
41 | |
42 | struct show_data { | |
43 | const char *pattern; | |
663a8566 | 44 | enum replace_format format; |
44f9f850 CC |
45 | }; |
46 | ||
212e0f7e SB |
47 | static int show_reference(struct repository *r, const char *refname, |
48 | const struct object_id *oid, | |
54b0c1e0 CC |
49 | int flag, void *cb_data) |
50 | { | |
44f9f850 CC |
51 | struct show_data *data = cb_data; |
52 | ||
55d34269 | 53 | if (!wildmatch(data->pattern, refname, 0)) { |
663a8566 | 54 | if (data->format == REPLACE_FORMAT_SHORT) |
44f9f850 | 55 | printf("%s\n", refname); |
663a8566 | 56 | else if (data->format == REPLACE_FORMAT_MEDIUM) |
d70d7a8a | 57 | printf("%s -> %s\n", refname, oid_to_hex(oid)); |
663a8566 | 58 | else { /* data->format == REPLACE_FORMAT_LONG */ |
d70d7a8a | 59 | struct object_id object; |
44f9f850 | 60 | enum object_type obj_type, repl_type; |
54b0c1e0 | 61 | |
4a93b899 | 62 | if (repo_get_oid(r, refname, &object)) |
225c62e0 | 63 | return error(_("failed to resolve '%s' as a valid ref"), refname); |
44f9f850 | 64 | |
212e0f7e SB |
65 | obj_type = oid_object_info(r, &object, NULL); |
66 | repl_type = oid_object_info(r, oid, NULL); | |
44f9f850 | 67 | |
debca9d2 BW |
68 | printf("%s (%s) -> %s (%s)\n", refname, type_name(obj_type), |
69 | oid_to_hex(oid), type_name(repl_type)); | |
44f9f850 CC |
70 | } |
71 | } | |
54b0c1e0 CC |
72 | |
73 | return 0; | |
74 | } | |
75 | ||
44f9f850 | 76 | static int list_replace_refs(const char *pattern, const char *format) |
54b0c1e0 | 77 | { |
44f9f850 CC |
78 | struct show_data data; |
79 | ||
afe8a907 | 80 | if (!pattern) |
54b0c1e0 | 81 | pattern = "*"; |
44f9f850 CC |
82 | data.pattern = pattern; |
83 | ||
84 | if (format == NULL || *format == '\0' || !strcmp(format, "short")) | |
663a8566 | 85 | data.format = REPLACE_FORMAT_SHORT; |
44f9f850 | 86 | else if (!strcmp(format, "medium")) |
663a8566 CC |
87 | data.format = REPLACE_FORMAT_MEDIUM; |
88 | else if (!strcmp(format, "long")) | |
89 | data.format = REPLACE_FORMAT_LONG; | |
5a59a230 NTND |
90 | /* |
91 | * Please update _git_replace() in git-completion.bash when | |
92 | * you add new format | |
93 | */ | |
44f9f850 | 94 | else |
225c62e0 NTND |
95 | return error(_("invalid replace format '%s'\n" |
96 | "valid formats are 'short', 'medium' and 'long'"), | |
e24e8719 | 97 | format); |
54b0c1e0 | 98 | |
60ce76d3 | 99 | for_each_replace_ref(the_repository, show_reference, (void *)&data); |
54b0c1e0 CC |
100 | |
101 | return 0; | |
102 | } | |
103 | ||
104 | typedef int (*each_replace_name_fn)(const char *name, const char *ref, | |
cea4332e | 105 | const struct object_id *oid); |
54b0c1e0 CC |
106 | |
107 | static int for_each_replace_name(const char **argv, each_replace_name_fn fn) | |
108 | { | |
9dfc3684 | 109 | const char **p, *full_hex; |
7f897b6f JK |
110 | struct strbuf ref = STRBUF_INIT; |
111 | size_t base_len; | |
54b0c1e0 | 112 | int had_error = 0; |
cea4332e | 113 | struct object_id oid; |
97e61e0f | 114 | const char *git_replace_ref_base = ref_namespace[NAMESPACE_REPLACE].ref; |
54b0c1e0 | 115 | |
7f897b6f JK |
116 | strbuf_addstr(&ref, git_replace_ref_base); |
117 | base_len = ref.len; | |
118 | ||
54b0c1e0 | 119 | for (p = argv; *p; p++) { |
d850b7a5 | 120 | if (repo_get_oid(the_repository, *p, &oid)) { |
1a07e59c | 121 | error("failed to resolve '%s' as a valid ref", *p); |
54b0c1e0 CC |
122 | had_error = 1; |
123 | continue; | |
124 | } | |
7f897b6f JK |
125 | |
126 | strbuf_setlen(&ref, base_len); | |
127 | strbuf_addstr(&ref, oid_to_hex(&oid)); | |
128 | full_hex = ref.buf + base_len; | |
129 | ||
34c290a6 | 130 | if (read_ref(ref.buf, &oid)) { |
225c62e0 | 131 | error(_("replace ref '%s' not found"), full_hex); |
54b0c1e0 CC |
132 | had_error = 1; |
133 | continue; | |
134 | } | |
7f897b6f | 135 | if (fn(full_hex, ref.buf, &oid)) |
54b0c1e0 CC |
136 | had_error = 1; |
137 | } | |
372b050b | 138 | strbuf_release(&ref); |
54b0c1e0 CC |
139 | return had_error; |
140 | } | |
141 | ||
142 | static int delete_replace_ref(const char *name, const char *ref, | |
cea4332e | 143 | const struct object_id *oid) |
54b0c1e0 | 144 | { |
2616a5e5 | 145 | if (delete_ref(NULL, ref, oid, 0)) |
54b0c1e0 | 146 | return 1; |
225c62e0 | 147 | printf_ln(_("Deleted replace ref '%s'"), name); |
54b0c1e0 CC |
148 | return 0; |
149 | } | |
150 | ||
e24e8719 | 151 | static int check_ref_valid(struct object_id *object, |
cea4332e | 152 | struct object_id *prev, |
7f897b6f | 153 | struct strbuf *ref, |
b6e38840 CC |
154 | int force) |
155 | { | |
97e61e0f DS |
156 | const char *git_replace_ref_base = ref_namespace[NAMESPACE_REPLACE].ref; |
157 | ||
7f897b6f JK |
158 | strbuf_reset(ref); |
159 | strbuf_addf(ref, "%s%s", git_replace_ref_base, oid_to_hex(object)); | |
160 | if (check_refname_format(ref->buf, 0)) | |
225c62e0 | 161 | return error(_("'%s' is not a valid ref name"), ref->buf); |
7f897b6f | 162 | |
34c290a6 | 163 | if (read_ref(ref->buf, prev)) |
cea4332e | 164 | oidclr(prev); |
b6e38840 | 165 | else if (!force) |
225c62e0 | 166 | return error(_("replace ref '%s' already exists"), ref->buf); |
e24e8719 | 167 | return 0; |
b6e38840 CC |
168 | } |
169 | ||
cea4332e | 170 | static int replace_object_oid(const char *object_ref, |
171 | struct object_id *object, | |
479bd757 | 172 | const char *replace_ref, |
cea4332e | 173 | struct object_id *repl, |
479bd757 | 174 | int force) |
bebdd271 | 175 | { |
cea4332e | 176 | struct object_id prev; |
277336a5 | 177 | enum object_type obj_type, repl_type; |
7f897b6f | 178 | struct strbuf ref = STRBUF_INIT; |
867c2fac RS |
179 | struct ref_transaction *transaction; |
180 | struct strbuf err = STRBUF_INIT; | |
e24e8719 | 181 | int res = 0; |
bebdd271 | 182 | |
0df8e965 SB |
183 | obj_type = oid_object_info(the_repository, object, NULL); |
184 | repl_type = oid_object_info(the_repository, repl, NULL); | |
277336a5 | 185 | if (!force && obj_type != repl_type) |
225c62e0 NTND |
186 | return error(_("Objects must be of the same type.\n" |
187 | "'%s' points to a replaced object of type '%s'\n" | |
188 | "while '%s' points to a replacement object of " | |
189 | "type '%s'."), | |
e24e8719 JS |
190 | object_ref, type_name(obj_type), |
191 | replace_ref, type_name(repl_type)); | |
192 | ||
193 | if (check_ref_valid(object, &prev, &ref, force)) { | |
194 | strbuf_release(&ref); | |
195 | return -1; | |
196 | } | |
bebdd271 | 197 | |
867c2fac RS |
198 | transaction = ref_transaction_begin(&err); |
199 | if (!transaction || | |
89f3bbdd | 200 | ref_transaction_update(transaction, ref.buf, repl, &prev, |
1d147bdf | 201 | 0, NULL, &err) || |
db7516ab | 202 | ref_transaction_commit(transaction, &err)) |
e24e8719 | 203 | res = error("%s", err.buf); |
bebdd271 | 204 | |
867c2fac | 205 | ref_transaction_free(transaction); |
7f897b6f | 206 | strbuf_release(&ref); |
e24e8719 | 207 | return res; |
bebdd271 CC |
208 | } |
209 | ||
479bd757 JK |
210 | static int replace_object(const char *object_ref, const char *replace_ref, int force) |
211 | { | |
cea4332e | 212 | struct object_id object, repl; |
479bd757 | 213 | |
d850b7a5 | 214 | if (repo_get_oid(the_repository, object_ref, &object)) |
225c62e0 | 215 | return error(_("failed to resolve '%s' as a valid ref"), |
e24e8719 | 216 | object_ref); |
d850b7a5 | 217 | if (repo_get_oid(the_repository, replace_ref, &repl)) |
225c62e0 | 218 | return error(_("failed to resolve '%s' as a valid ref"), |
e24e8719 | 219 | replace_ref); |
479bd757 | 220 | |
cea4332e | 221 | return replace_object_oid(object_ref, &object, replace_ref, &repl, force); |
479bd757 JK |
222 | } |
223 | ||
b892bb45 | 224 | /* |
2deda629 JK |
225 | * Write the contents of the object named by "sha1" to the file "filename". |
226 | * If "raw" is true, then the object's raw contents are printed according to | |
227 | * "type". Otherwise, we pretty-print the contents for human editing. | |
b892bb45 | 228 | */ |
e24e8719 | 229 | static int export_object(const struct object_id *oid, enum object_type type, |
2deda629 | 230 | int raw, const char *filename) |
b892bb45 | 231 | { |
d3180279 | 232 | struct child_process cmd = CHILD_PROCESS_INIT; |
b892bb45 JK |
233 | int fd; |
234 | ||
235 | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666); | |
236 | if (fd < 0) | |
225c62e0 | 237 | return error_errno(_("unable to open %s for writing"), filename); |
b892bb45 | 238 | |
22f9b7f3 JK |
239 | strvec_push(&cmd.args, "--no-replace-objects"); |
240 | strvec_push(&cmd.args, "cat-file"); | |
2deda629 | 241 | if (raw) |
22f9b7f3 | 242 | strvec_push(&cmd.args, type_name(type)); |
2deda629 | 243 | else |
22f9b7f3 JK |
244 | strvec_push(&cmd.args, "-p"); |
245 | strvec_push(&cmd.args, oid_to_hex(oid)); | |
b892bb45 JK |
246 | cmd.git_cmd = 1; |
247 | cmd.out = fd; | |
248 | ||
249 | if (run_command(&cmd)) | |
225c62e0 | 250 | return error(_("cat-file reported failure")); |
e24e8719 | 251 | return 0; |
b892bb45 JK |
252 | } |
253 | ||
254 | /* | |
255 | * Read a previously-exported (and possibly edited) object back from "filename", | |
256 | * interpreting it as "type", and writing the result to the object database. | |
257 | * The sha1 of the written object is returned via sha1. | |
258 | */ | |
e24e8719 | 259 | static int import_object(struct object_id *oid, enum object_type type, |
2deda629 | 260 | int raw, const char *filename) |
b892bb45 JK |
261 | { |
262 | int fd; | |
263 | ||
264 | fd = open(filename, O_RDONLY); | |
265 | if (fd < 0) | |
225c62e0 | 266 | return error_errno(_("unable to open %s for reading"), filename); |
b892bb45 | 267 | |
2deda629 | 268 | if (!raw && type == OBJ_TREE) { |
d3180279 | 269 | struct child_process cmd = CHILD_PROCESS_INIT; |
b892bb45 JK |
270 | struct strbuf result = STRBUF_INIT; |
271 | ||
2b709893 | 272 | strvec_push(&cmd.args, "mktree"); |
b892bb45 JK |
273 | cmd.git_cmd = 1; |
274 | cmd.in = fd; | |
275 | cmd.out = -1; | |
276 | ||
e24e8719 JS |
277 | if (start_command(&cmd)) { |
278 | close(fd); | |
225c62e0 | 279 | return error(_("unable to spawn mktree")); |
e24e8719 | 280 | } |
b892bb45 | 281 | |
28ba1830 | 282 | if (strbuf_read(&result, cmd.out, the_hash_algo->hexsz + 1) < 0) { |
225c62e0 | 283 | error_errno(_("unable to read from mktree")); |
e24e8719 JS |
284 | close(fd); |
285 | close(cmd.out); | |
286 | return -1; | |
287 | } | |
b892bb45 JK |
288 | close(cmd.out); |
289 | ||
e24e8719 JS |
290 | if (finish_command(&cmd)) { |
291 | strbuf_release(&result); | |
225c62e0 | 292 | return error(_("mktree reported failure")); |
e24e8719 JS |
293 | } |
294 | if (get_oid_hex(result.buf, oid) < 0) { | |
295 | strbuf_release(&result); | |
225c62e0 | 296 | return error(_("mktree did not return an object name")); |
e24e8719 | 297 | } |
b892bb45 JK |
298 | |
299 | strbuf_release(&result); | |
300 | } else { | |
301 | struct stat st; | |
302 | int flags = HASH_FORMAT_CHECK | HASH_WRITE_OBJECT; | |
303 | ||
e24e8719 | 304 | if (fstat(fd, &st) < 0) { |
225c62e0 | 305 | error_errno(_("unable to fstat %s"), filename); |
e24e8719 JS |
306 | close(fd); |
307 | return -1; | |
308 | } | |
f8adbec9 | 309 | if (index_fd(the_repository->index, oid, fd, &st, type, NULL, flags) < 0) |
225c62e0 | 310 | return error(_("unable to write object to database")); |
b892bb45 JK |
311 | /* index_fd close()s fd for us */ |
312 | } | |
313 | ||
314 | /* | |
315 | * No need to close(fd) here; both run-command and index-fd | |
316 | * will have done it for us. | |
317 | */ | |
e24e8719 | 318 | return 0; |
b892bb45 JK |
319 | } |
320 | ||
2deda629 | 321 | static int edit_and_replace(const char *object_ref, int force, int raw) |
b892bb45 | 322 | { |
e24e8719 | 323 | char *tmpfile; |
b892bb45 | 324 | enum object_type type; |
efdfe11f | 325 | struct object_id old_oid, new_oid, prev; |
7f897b6f | 326 | struct strbuf ref = STRBUF_INIT; |
b892bb45 | 327 | |
d850b7a5 | 328 | if (repo_get_oid(the_repository, object_ref, &old_oid) < 0) |
225c62e0 | 329 | return error(_("not a valid object name: '%s'"), object_ref); |
b892bb45 | 330 | |
0df8e965 | 331 | type = oid_object_info(the_repository, &old_oid, NULL); |
b892bb45 | 332 | if (type < 0) |
225c62e0 | 333 | return error(_("unable to get object type for %s"), |
e24e8719 | 334 | oid_to_hex(&old_oid)); |
b892bb45 | 335 | |
e24e8719 JS |
336 | if (check_ref_valid(&old_oid, &prev, &ref, force)) { |
337 | strbuf_release(&ref); | |
338 | return -1; | |
339 | } | |
7f897b6f | 340 | strbuf_release(&ref); |
24790835 | 341 | |
e24e8719 JS |
342 | tmpfile = git_pathdup("REPLACE_EDITOBJ"); |
343 | if (export_object(&old_oid, type, raw, tmpfile)) { | |
344 | free(tmpfile); | |
345 | return -1; | |
346 | } | |
347 | if (launch_editor(tmpfile, NULL, NULL) < 0) { | |
348 | free(tmpfile); | |
225c62e0 | 349 | return error(_("editing object file failed")); |
e24e8719 JS |
350 | } |
351 | if (import_object(&new_oid, type, raw, tmpfile)) { | |
352 | free(tmpfile); | |
353 | return -1; | |
354 | } | |
b892bb45 JK |
355 | free(tmpfile); |
356 | ||
4a7e27e9 | 357 | if (oideq(&old_oid, &new_oid)) |
225c62e0 | 358 | return error(_("new object is the same as the old one: '%s'"), oid_to_hex(&old_oid)); |
f22166b5 | 359 | |
efdfe11f | 360 | return replace_object_oid(object_ref, &old_oid, "replacement", &new_oid, force); |
b892bb45 JK |
361 | } |
362 | ||
e24e8719 | 363 | static int replace_parents(struct strbuf *buf, int argc, const char **argv) |
4228e8bc CC |
364 | { |
365 | struct strbuf new_parents = STRBUF_INIT; | |
366 | const char *parent_start, *parent_end; | |
367 | int i; | |
28ba1830 | 368 | const unsigned hexsz = the_hash_algo->hexsz; |
4228e8bc CC |
369 | |
370 | /* find existing parents */ | |
371 | parent_start = buf->buf; | |
28ba1830 | 372 | parent_start += hexsz + 6; /* "tree " + "hex sha1" + "\n" */ |
4228e8bc CC |
373 | parent_end = parent_start; |
374 | ||
375 | while (starts_with(parent_end, "parent ")) | |
28ba1830 | 376 | parent_end += hexsz + 8; /* "parent " + "hex sha1" + "\n" */ |
4228e8bc CC |
377 | |
378 | /* prepare new parents */ | |
379 | for (i = 0; i < argc; i++) { | |
cea4332e | 380 | struct object_id oid; |
f8e44a81 CC |
381 | struct commit *commit; |
382 | ||
d850b7a5 | 383 | if (repo_get_oid(the_repository, argv[i], &oid) < 0) { |
e24e8719 | 384 | strbuf_release(&new_parents); |
1a07e59c | 385 | return error(_("not a valid object name: '%s'"), |
e24e8719 JS |
386 | argv[i]); |
387 | } | |
f8e44a81 CC |
388 | commit = lookup_commit_reference(the_repository, &oid); |
389 | if (!commit) { | |
e24e8719 | 390 | strbuf_release(&new_parents); |
f8e44a81 | 391 | return error(_("could not parse %s as a commit"), argv[i]); |
e24e8719 | 392 | } |
f8e44a81 | 393 | strbuf_addf(&new_parents, "parent %s\n", oid_to_hex(&commit->object.oid)); |
4228e8bc CC |
394 | } |
395 | ||
396 | /* replace existing parents with new ones */ | |
397 | strbuf_splice(buf, parent_start - buf->buf, parent_end - parent_start, | |
398 | new_parents.buf, new_parents.len); | |
399 | ||
400 | strbuf_release(&new_parents); | |
e24e8719 | 401 | return 0; |
4228e8bc CC |
402 | } |
403 | ||
25a05a8c CC |
404 | struct check_mergetag_data { |
405 | int argc; | |
406 | const char **argv; | |
407 | }; | |
408 | ||
fef461ea | 409 | static int check_one_mergetag(struct commit *commit, |
25a05a8c CC |
410 | struct commit_extra_header *extra, |
411 | void *data) | |
412 | { | |
413 | struct check_mergetag_data *mergetag_data = (struct check_mergetag_data *)data; | |
414 | const char *ref = mergetag_data->argv[0]; | |
cea4332e | 415 | struct object_id tag_oid; |
25a05a8c CC |
416 | struct tag *tag; |
417 | int i; | |
418 | ||
2dcde20e | 419 | hash_object_file(the_hash_algo, extra->value, extra->len, |
44439c1c | 420 | OBJ_TAG, &tag_oid); |
ce71efb7 | 421 | tag = lookup_tag(the_repository, &tag_oid); |
25a05a8c | 422 | if (!tag) |
e24e8719 | 423 | return error(_("bad mergetag in commit '%s'"), ref); |
0e740fed | 424 | if (parse_tag_buffer(the_repository, tag, extra->value, extra->len)) |
e24e8719 | 425 | return error(_("malformed mergetag in commit '%s'"), ref); |
25a05a8c CC |
426 | |
427 | /* iterate over new parents */ | |
428 | for (i = 1; i < mergetag_data->argc; i++) { | |
f2fd0760 | 429 | struct object_id oid; |
d850b7a5 | 430 | if (repo_get_oid(the_repository, mergetag_data->argv[i], &oid) < 0) |
1a07e59c | 431 | return error(_("not a valid object name: '%s'"), |
e24e8719 | 432 | mergetag_data->argv[i]); |
c77722b3 | 433 | if (oideq(get_tagged_oid(tag), &oid)) |
fef461ea | 434 | return 0; /* found */ |
25a05a8c CC |
435 | } |
436 | ||
e24e8719 JS |
437 | return error(_("original commit '%s' contains mergetag '%s' that is " |
438 | "discarded; use --edit instead of --graft"), ref, | |
439 | oid_to_hex(&tag_oid)); | |
25a05a8c CC |
440 | } |
441 | ||
fef461ea | 442 | static int check_mergetags(struct commit *commit, int argc, const char **argv) |
25a05a8c CC |
443 | { |
444 | struct check_mergetag_data mergetag_data; | |
445 | ||
446 | mergetag_data.argc = argc; | |
447 | mergetag_data.argv = argv; | |
fef461ea | 448 | return for_each_mergetag(check_one_mergetag, commit, &mergetag_data); |
25a05a8c CC |
449 | } |
450 | ||
041c98e2 | 451 | static int create_graft(int argc, const char **argv, int force, int gentle) |
4228e8bc | 452 | { |
efdfe11f | 453 | struct object_id old_oid, new_oid; |
4228e8bc CC |
454 | const char *old_ref = argv[0]; |
455 | struct commit *commit; | |
456 | struct strbuf buf = STRBUF_INIT; | |
457 | const char *buffer; | |
458 | unsigned long size; | |
459 | ||
d850b7a5 | 460 | if (repo_get_oid(the_repository, old_ref, &old_oid) < 0) |
1a07e59c | 461 | return error(_("not a valid object name: '%s'"), old_ref); |
2122f675 | 462 | commit = lookup_commit_reference(the_repository, &old_oid); |
e24e8719 JS |
463 | if (!commit) |
464 | return error(_("could not parse %s"), old_ref); | |
4228e8bc | 465 | |
ecb5091f | 466 | buffer = repo_get_commit_buffer(the_repository, commit, &size); |
4228e8bc | 467 | strbuf_add(&buf, buffer, size); |
ecb5091f | 468 | repo_unuse_commit_buffer(the_repository, commit, buffer); |
4228e8bc | 469 | |
e24e8719 JS |
470 | if (replace_parents(&buf, argc - 1, &argv[1]) < 0) { |
471 | strbuf_release(&buf); | |
472 | return -1; | |
473 | } | |
4228e8bc | 474 | |
0b05ab6f | 475 | if (remove_signature(&buf)) { |
1a07e59c | 476 | warning(_("the original commit '%s' has a gpg signature"), old_ref); |
0b05ab6f CC |
477 | warning(_("the signature will be removed in the replacement commit!")); |
478 | } | |
479 | ||
e24e8719 JS |
480 | if (check_mergetags(commit, argc, argv)) { |
481 | strbuf_release(&buf); | |
482 | return -1; | |
483 | } | |
25a05a8c | 484 | |
c80d226a | 485 | if (write_object_file(buf.buf, buf.len, OBJ_COMMIT, &new_oid)) { |
e24e8719 JS |
486 | strbuf_release(&buf); |
487 | return error(_("could not write replacement commit for: '%s'"), | |
488 | old_ref); | |
489 | } | |
4228e8bc CC |
490 | |
491 | strbuf_release(&buf); | |
492 | ||
ee521ec4 | 493 | if (oideq(&commit->object.oid, &new_oid)) { |
041c98e2 | 494 | if (gentle) { |
ee521ec4 CC |
495 | warning(_("graft for '%s' unnecessary"), |
496 | oid_to_hex(&commit->object.oid)); | |
041c98e2 JS |
497 | return 0; |
498 | } | |
ee521ec4 CC |
499 | return error(_("new commit is the same as the old one: '%s'"), |
500 | oid_to_hex(&commit->object.oid)); | |
041c98e2 | 501 | } |
4228e8bc | 502 | |
ee521ec4 CC |
503 | return replace_object_oid(old_ref, &commit->object.oid, |
504 | "replacement", &new_oid, force); | |
4228e8bc CC |
505 | } |
506 | ||
fb404291 JS |
507 | static int convert_graft_file(int force) |
508 | { | |
b16b60f7 | 509 | const char *graft_file = get_graft_file(the_repository); |
fb404291 JS |
510 | FILE *fp = fopen_or_warn(graft_file, "r"); |
511 | struct strbuf buf = STRBUF_INIT, err = STRBUF_INIT; | |
22f9b7f3 | 512 | struct strvec args = STRVEC_INIT; |
fb404291 JS |
513 | |
514 | if (!fp) | |
515 | return -1; | |
516 | ||
ab628588 | 517 | no_graft_file_deprecated_advice = 1; |
fb404291 JS |
518 | while (strbuf_getline(&buf, fp) != EOF) { |
519 | if (*buf.buf == '#') | |
520 | continue; | |
521 | ||
22f9b7f3 | 522 | strvec_split(&args, buf.buf); |
d70a9eb6 | 523 | if (args.nr && create_graft(args.nr, args.v, force, 1)) |
fb404291 | 524 | strbuf_addf(&err, "\n\t%s", buf.buf); |
22f9b7f3 | 525 | strvec_clear(&args); |
fb404291 JS |
526 | } |
527 | fclose(fp); | |
528 | ||
529 | strbuf_release(&buf); | |
530 | ||
531 | if (!err.len) | |
532 | return unlink_or_warn(graft_file); | |
533 | ||
534 | warning(_("could not convert the following graft(s):\n%s"), err.buf); | |
535 | strbuf_release(&err); | |
536 | ||
537 | return -1; | |
538 | } | |
539 | ||
54b0c1e0 CC |
540 | int cmd_replace(int argc, const char **argv, const char *prefix) |
541 | { | |
70c7bd6d | 542 | int force = 0; |
2deda629 | 543 | int raw = 0; |
44f9f850 | 544 | const char *format = NULL; |
70c7bd6d JK |
545 | enum { |
546 | MODE_UNSPECIFIED = 0, | |
547 | MODE_LIST, | |
548 | MODE_DELETE, | |
b892bb45 | 549 | MODE_EDIT, |
4228e8bc | 550 | MODE_GRAFT, |
fb404291 | 551 | MODE_CONVERT_GRAFT_FILE, |
70c7bd6d JK |
552 | MODE_REPLACE |
553 | } cmdmode = MODE_UNSPECIFIED; | |
54b0c1e0 | 554 | struct option options[] = { |
70c7bd6d JK |
555 | OPT_CMDMODE('l', "list", &cmdmode, N_("list replace refs"), MODE_LIST), |
556 | OPT_CMDMODE('d', "delete", &cmdmode, N_("delete replace refs"), MODE_DELETE), | |
b892bb45 | 557 | OPT_CMDMODE('e', "edit", &cmdmode, N_("edit existing object"), MODE_EDIT), |
4228e8bc | 558 | OPT_CMDMODE('g', "graft", &cmdmode, N_("change a commit's parents"), MODE_GRAFT), |
fb404291 | 559 | OPT_CMDMODE(0, "convert-graft-file", &cmdmode, N_("convert existing graft file"), MODE_CONVERT_GRAFT_FILE), |
1b354755 NTND |
560 | OPT_BOOL_F('f', "force", &force, N_("replace the ref if it exists"), |
561 | PARSE_OPT_NOCOMPLETE), | |
2deda629 | 562 | OPT_BOOL(0, "raw", &raw, N_("do not pretty-print contents for --edit")), |
44f9f850 | 563 | OPT_STRING(0, "format", &format, N_("format"), N_("use this format")), |
54b0c1e0 CC |
564 | OPT_END() |
565 | }; | |
566 | ||
6ebd1caf | 567 | read_replace_refs = 0; |
36b14370 | 568 | git_config(git_default_config, NULL); |
769a4fa4 | 569 | |
451bb210 | 570 | argc = parse_options(argc, argv, prefix, options, git_replace_usage, 0); |
54b0c1e0 | 571 | |
70c7bd6d JK |
572 | if (!cmdmode) |
573 | cmdmode = argc ? MODE_REPLACE : MODE_LIST; | |
3f495f67 | 574 | |
70c7bd6d | 575 | if (format && cmdmode != MODE_LIST) |
225c62e0 | 576 | usage_msg_opt(_("--format cannot be used when not listing"), |
44f9f850 CC |
577 | git_replace_usage, options); |
578 | ||
4228e8bc CC |
579 | if (force && |
580 | cmdmode != MODE_REPLACE && | |
581 | cmdmode != MODE_EDIT && | |
fb404291 JS |
582 | cmdmode != MODE_GRAFT && |
583 | cmdmode != MODE_CONVERT_GRAFT_FILE) | |
225c62e0 | 584 | usage_msg_opt(_("-f only makes sense when writing a replacement"), |
86af2caa | 585 | git_replace_usage, options); |
bebdd271 | 586 | |
2deda629 | 587 | if (raw && cmdmode != MODE_EDIT) |
225c62e0 | 588 | usage_msg_opt(_("--raw only makes sense with --edit"), |
2deda629 JK |
589 | git_replace_usage, options); |
590 | ||
70c7bd6d JK |
591 | switch (cmdmode) { |
592 | case MODE_DELETE: | |
54b0c1e0 | 593 | if (argc < 1) |
225c62e0 | 594 | usage_msg_opt(_("-d needs at least one argument"), |
86af2caa | 595 | git_replace_usage, options); |
54b0c1e0 | 596 | return for_each_replace_name(argv, delete_replace_ref); |
54b0c1e0 | 597 | |
70c7bd6d | 598 | case MODE_REPLACE: |
bebdd271 | 599 | if (argc != 2) |
225c62e0 | 600 | usage_msg_opt(_("bad number of arguments"), |
86af2caa | 601 | git_replace_usage, options); |
bebdd271 | 602 | return replace_object(argv[0], argv[1], force); |
bebdd271 | 603 | |
b892bb45 JK |
604 | case MODE_EDIT: |
605 | if (argc != 1) | |
225c62e0 | 606 | usage_msg_opt(_("-e needs exactly one argument"), |
b892bb45 | 607 | git_replace_usage, options); |
2deda629 | 608 | return edit_and_replace(argv[0], force, raw); |
b892bb45 | 609 | |
4228e8bc CC |
610 | case MODE_GRAFT: |
611 | if (argc < 1) | |
225c62e0 | 612 | usage_msg_opt(_("-g needs at least one argument"), |
4228e8bc | 613 | git_replace_usage, options); |
041c98e2 | 614 | return create_graft(argc, argv, force, 0); |
4228e8bc | 615 | |
fb404291 JS |
616 | case MODE_CONVERT_GRAFT_FILE: |
617 | if (argc != 0) | |
225c62e0 | 618 | usage_msg_opt(_("--convert-graft-file takes no argument"), |
fb404291 JS |
619 | git_replace_usage, options); |
620 | return !!convert_graft_file(force); | |
4228e8bc | 621 | |
70c7bd6d JK |
622 | case MODE_LIST: |
623 | if (argc > 1) | |
225c62e0 | 624 | usage_msg_opt(_("only one pattern can be given with -l"), |
70c7bd6d JK |
625 | git_replace_usage, options); |
626 | return list_replace_refs(argv[0], format); | |
54b0c1e0 | 627 | |
70c7bd6d | 628 | default: |
d398f2ea | 629 | BUG("invalid cmdmode %d", (int)cmdmode); |
70c7bd6d | 630 | } |
54b0c1e0 | 631 | } |