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