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