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