14
#include "environment.h"
18
#include "parse-options.h"
20
#include "run-command.h"
21
#include "object-file.h"
22
#include "object-name.h"
23
#include "object-store-ll.h"
24
#include "replace-object.h"
25
#include "repository.h"
29
static const char * const git_replace_usage[] = {
30
N_("git replace [-f] <object> <replacement>"),
31
N_("git replace [-f] --edit <object>"),
32
N_("git replace [-f] --graft <commit> [<parent>...]"),
33
"git replace [-f] --convert-graft-file",
34
N_("git replace -d <object>..."),
35
N_("git replace [--format=<format>] [-l [<pattern>]]"),
41
REPLACE_FORMAT_MEDIUM,
46
struct repository *repo;
48
enum replace_format format;
51
static int show_reference(const char *refname,
52
const char *referent UNUSED,
53
const struct object_id *oid,
54
int flag UNUSED, void *cb_data)
56
struct show_data *data = cb_data;
58
if (!wildmatch(data->pattern, refname, 0)) {
59
if (data->format == REPLACE_FORMAT_SHORT)
60
printf("%s\n", refname);
61
else if (data->format == REPLACE_FORMAT_MEDIUM)
62
printf("%s -> %s\n", refname, oid_to_hex(oid));
64
struct object_id object;
65
enum object_type obj_type, repl_type;
67
if (repo_get_oid(data->repo, refname, &object))
68
return error(_("failed to resolve '%s' as a valid ref"), refname);
70
obj_type = oid_object_info(data->repo, &object, NULL);
71
repl_type = oid_object_info(data->repo, oid, NULL);
73
printf("%s (%s) -> %s (%s)\n", refname, type_name(obj_type),
74
oid_to_hex(oid), type_name(repl_type));
81
static int list_replace_refs(const char *pattern, const char *format)
83
struct show_data data;
85
data.repo = the_repository;
88
data.pattern = pattern;
90
if (format == NULL || *format == '\0' || !strcmp(format, "short"))
91
data.format = REPLACE_FORMAT_SHORT;
92
else if (!strcmp(format, "medium"))
93
data.format = REPLACE_FORMAT_MEDIUM;
94
else if (!strcmp(format, "long"))
95
data.format = REPLACE_FORMAT_LONG;
101
return error(_("invalid replace format '%s'\n"
102
"valid formats are 'short', 'medium' and 'long'"),
105
refs_for_each_replace_ref(get_main_ref_store(the_repository),
106
show_reference, (void *)&data);
111
typedef int (*each_replace_name_fn)(const char *name, const char *ref,
112
const struct object_id *oid);
114
static int for_each_replace_name(const char **argv, each_replace_name_fn fn)
116
const char **p, *full_hex;
117
struct strbuf ref = STRBUF_INIT;
120
struct object_id oid;
121
const char *git_replace_ref_base = ref_namespace[NAMESPACE_REPLACE].ref;
123
strbuf_addstr(&ref, git_replace_ref_base);
126
for (p = argv; *p; p++) {
127
if (repo_get_oid(the_repository, *p, &oid)) {
128
error("failed to resolve '%s' as a valid ref", *p);
133
strbuf_setlen(&ref, base_len);
134
strbuf_addstr(&ref, oid_to_hex(&oid));
135
full_hex = ref.buf + base_len;
137
if (refs_read_ref(get_main_ref_store(the_repository), ref.buf, &oid)) {
138
error(_("replace ref '%s' not found"), full_hex);
142
if (fn(full_hex, ref.buf, &oid))
145
strbuf_release(&ref);
149
static int delete_replace_ref(const char *name, const char *ref,
150
const struct object_id *oid)
152
if (refs_delete_ref(get_main_ref_store(the_repository), NULL, ref, oid, 0))
154
printf_ln(_("Deleted replace ref '%s'"), name);
158
static int check_ref_valid(struct object_id *object,
159
struct object_id *prev,
163
const char *git_replace_ref_base = ref_namespace[NAMESPACE_REPLACE].ref;
166
strbuf_addf(ref, "%s%s", git_replace_ref_base, oid_to_hex(object));
167
if (check_refname_format(ref->buf, 0))
168
return error(_("'%s' is not a valid ref name"), ref->buf);
170
if (refs_read_ref(get_main_ref_store(the_repository), ref->buf, prev))
171
oidclr(prev, the_repository->hash_algo);
173
return error(_("replace ref '%s' already exists"), ref->buf);
177
static int replace_object_oid(const char *object_ref,
178
struct object_id *object,
179
const char *replace_ref,
180
struct object_id *repl,
183
struct object_id prev;
184
enum object_type obj_type, repl_type;
185
struct strbuf ref = STRBUF_INIT;
186
struct ref_transaction *transaction;
187
struct strbuf err = STRBUF_INIT;
190
obj_type = oid_object_info(the_repository, object, NULL);
191
repl_type = oid_object_info(the_repository, repl, NULL);
192
if (!force && obj_type != repl_type)
193
return error(_("Objects must be of the same type.\n"
194
"'%s' points to a replaced object of type '%s'\n"
195
"while '%s' points to a replacement object of "
197
object_ref, type_name(obj_type),
198
replace_ref, type_name(repl_type));
200
if (check_ref_valid(object, &prev, &ref, force)) {
201
strbuf_release(&ref);
205
transaction = ref_store_transaction_begin(get_main_ref_store(the_repository),
208
ref_transaction_update(transaction, ref.buf, repl, &prev,
209
NULL, NULL, 0, NULL, &err) ||
210
ref_transaction_commit(transaction, &err))
211
res = error("%s", err.buf);
213
ref_transaction_free(transaction);
214
strbuf_release(&ref);
218
static int replace_object(const char *object_ref, const char *replace_ref, int force)
220
struct object_id object, repl;
222
if (repo_get_oid(the_repository, object_ref, &object))
223
return error(_("failed to resolve '%s' as a valid ref"),
225
if (repo_get_oid(the_repository, replace_ref, &repl))
226
return error(_("failed to resolve '%s' as a valid ref"),
229
return replace_object_oid(object_ref, &object, replace_ref, &repl, force);
237
static int export_object(const struct object_id *oid, enum object_type type,
238
int raw, const char *filename)
240
struct child_process cmd = CHILD_PROCESS_INIT;
243
fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
245
return error_errno(_("unable to open %s for writing"), filename);
247
strvec_push(&cmd.args, "--no-replace-objects");
248
strvec_push(&cmd.args, "cat-file");
250
strvec_push(&cmd.args, type_name(type));
252
strvec_push(&cmd.args, "-p");
253
strvec_push(&cmd.args, oid_to_hex(oid));
257
if (run_command(&cmd))
258
return error(_("cat-file reported failure"));
267
static int import_object(struct object_id *oid, enum object_type type,
268
int raw, const char *filename)
272
fd = open(filename, O_RDONLY);
274
return error_errno(_("unable to open %s for reading"), filename);
276
if (!raw && type == OBJ_TREE) {
277
struct child_process cmd = CHILD_PROCESS_INIT;
278
struct strbuf result = STRBUF_INIT;
280
strvec_push(&cmd.args, "mktree");
285
if (start_command(&cmd)) {
287
return error(_("unable to spawn mktree"));
290
if (strbuf_read(&result, cmd.out, the_hash_algo->hexsz + 1) < 0) {
291
error_errno(_("unable to read from mktree"));
298
if (finish_command(&cmd)) {
299
strbuf_release(&result);
300
return error(_("mktree reported failure"));
302
if (get_oid_hex(result.buf, oid) < 0) {
303
strbuf_release(&result);
304
return error(_("mktree did not return an object name"));
307
strbuf_release(&result);
310
int flags = HASH_FORMAT_CHECK | HASH_WRITE_OBJECT;
312
if (fstat(fd, &st) < 0) {
313
error_errno(_("unable to fstat %s"), filename);
317
if (index_fd(the_repository->index, oid, fd, &st, type, NULL, flags) < 0)
318
return error(_("unable to write object to database"));
329
static int edit_and_replace(const char *object_ref, int force, int raw)
332
enum object_type type;
333
struct object_id old_oid, new_oid, prev;
334
struct strbuf ref = STRBUF_INIT;
336
if (repo_get_oid(the_repository, object_ref, &old_oid) < 0)
337
return error(_("not a valid object name: '%s'"), object_ref);
339
type = oid_object_info(the_repository, &old_oid, NULL);
341
return error(_("unable to get object type for %s"),
342
oid_to_hex(&old_oid));
344
if (check_ref_valid(&old_oid, &prev, &ref, force)) {
345
strbuf_release(&ref);
348
strbuf_release(&ref);
350
tmpfile = git_pathdup("REPLACE_EDITOBJ");
351
if (export_object(&old_oid, type, raw, tmpfile)) {
355
if (launch_editor(tmpfile, NULL, NULL) < 0) {
357
return error(_("editing object file failed"));
359
if (import_object(&new_oid, type, raw, tmpfile)) {
365
if (oideq(&old_oid, &new_oid))
366
return error(_("new object is the same as the old one: '%s'"), oid_to_hex(&old_oid));
368
return replace_object_oid(object_ref, &old_oid, "replacement", &new_oid, force);
371
static int replace_parents(struct strbuf *buf, int argc, const char **argv)
373
struct strbuf new_parents = STRBUF_INIT;
374
const char *parent_start, *parent_end;
376
const unsigned hexsz = the_hash_algo->hexsz;
379
parent_start = buf->buf;
380
parent_start += hexsz + 6;
381
parent_end = parent_start;
383
while (starts_with(parent_end, "parent "))
384
parent_end += hexsz + 8;
387
for (i = 0; i < argc; i++) {
388
struct object_id oid;
389
struct commit *commit;
391
if (repo_get_oid(the_repository, argv[i], &oid) < 0) {
392
strbuf_release(&new_parents);
393
return error(_("not a valid object name: '%s'"),
396
commit = lookup_commit_reference(the_repository, &oid);
398
strbuf_release(&new_parents);
399
return error(_("could not parse %s as a commit"), argv[i]);
401
strbuf_addf(&new_parents, "parent %s\n", oid_to_hex(&commit->object.oid));
405
strbuf_splice(buf, parent_start - buf->buf, parent_end - parent_start,
406
new_parents.buf, new_parents.len);
408
strbuf_release(&new_parents);
412
struct check_mergetag_data {
417
static int check_one_mergetag(struct commit *commit UNUSED,
418
struct commit_extra_header *extra,
421
struct check_mergetag_data *mergetag_data = (struct check_mergetag_data *)data;
422
const char *ref = mergetag_data->argv[0];
423
struct object_id tag_oid;
427
hash_object_file(the_hash_algo, extra->value, extra->len,
429
tag = lookup_tag(the_repository, &tag_oid);
431
return error(_("bad mergetag in commit '%s'"), ref);
432
if (parse_tag_buffer(the_repository, tag, extra->value, extra->len))
433
return error(_("malformed mergetag in commit '%s'"), ref);
436
for (i = 1; i < mergetag_data->argc; i++) {
437
struct object_id oid;
438
if (repo_get_oid(the_repository, mergetag_data->argv[i], &oid) < 0)
439
return error(_("not a valid object name: '%s'"),
440
mergetag_data->argv[i]);
441
if (oideq(get_tagged_oid(tag), &oid))
445
return error(_("original commit '%s' contains mergetag '%s' that is "
446
"discarded; use --edit instead of --graft"), ref,
447
oid_to_hex(&tag_oid));
450
static int check_mergetags(struct commit *commit, int argc, const char **argv)
452
struct check_mergetag_data mergetag_data;
454
mergetag_data.argc = argc;
455
mergetag_data.argv = argv;
456
return for_each_mergetag(check_one_mergetag, commit, &mergetag_data);
459
static int create_graft(int argc, const char **argv, int force, int gentle)
461
struct object_id old_oid, new_oid;
462
const char *old_ref = argv[0];
463
struct commit *commit;
464
struct strbuf buf = STRBUF_INIT;
468
if (repo_get_oid(the_repository, old_ref, &old_oid) < 0)
469
return error(_("not a valid object name: '%s'"), old_ref);
470
commit = lookup_commit_reference(the_repository, &old_oid);
472
return error(_("could not parse %s"), old_ref);
474
buffer = repo_get_commit_buffer(the_repository, commit, &size);
475
strbuf_add(&buf, buffer, size);
476
repo_unuse_commit_buffer(the_repository, commit, buffer);
478
if (replace_parents(&buf, argc - 1, &argv[1]) < 0) {
479
strbuf_release(&buf);
483
if (remove_signature(&buf)) {
484
warning(_("the original commit '%s' has a gpg signature"), old_ref);
485
warning(_("the signature will be removed in the replacement commit!"));
488
if (check_mergetags(commit, argc, argv)) {
489
strbuf_release(&buf);
493
if (write_object_file(buf.buf, buf.len, OBJ_COMMIT, &new_oid)) {
494
strbuf_release(&buf);
495
return error(_("could not write replacement commit for: '%s'"),
499
strbuf_release(&buf);
501
if (oideq(&commit->object.oid, &new_oid)) {
503
warning(_("graft for '%s' unnecessary"),
504
oid_to_hex(&commit->object.oid));
507
return error(_("new commit is the same as the old one: '%s'"),
508
oid_to_hex(&commit->object.oid));
511
return replace_object_oid(old_ref, &commit->object.oid,
512
"replacement", &new_oid, force);
515
static int convert_graft_file(int force)
517
const char *graft_file = get_graft_file(the_repository);
518
FILE *fp = fopen_or_warn(graft_file, "r");
519
struct strbuf buf = STRBUF_INIT, err = STRBUF_INIT;
520
struct strvec args = STRVEC_INIT;
525
no_graft_file_deprecated_advice = 1;
526
while (strbuf_getline(&buf, fp) != EOF) {
530
strvec_split(&args, buf.buf);
531
if (args.nr && create_graft(args.nr, args.v, force, 1))
532
strbuf_addf(&err, "\n\t%s", buf.buf);
537
strbuf_release(&buf);
540
return unlink_or_warn(graft_file);
542
warning(_("could not convert the following graft(s):\n%s"), err.buf);
543
strbuf_release(&err);
548
int cmd_replace(int argc, const char **argv, const char *prefix)
552
const char *format = NULL;
554
MODE_UNSPECIFIED = 0,
559
MODE_CONVERT_GRAFT_FILE,
561
} cmdmode = MODE_UNSPECIFIED;
562
struct option options[] = {
563
OPT_CMDMODE('l', "list", &cmdmode, N_("list replace refs"), MODE_LIST),
564
OPT_CMDMODE('d', "delete", &cmdmode, N_("delete replace refs"), MODE_DELETE),
565
OPT_CMDMODE('e', "edit", &cmdmode, N_("edit existing object"), MODE_EDIT),
566
OPT_CMDMODE('g', "graft", &cmdmode, N_("change a commit's parents"), MODE_GRAFT),
567
OPT_CMDMODE(0, "convert-graft-file", &cmdmode, N_("convert existing graft file"), MODE_CONVERT_GRAFT_FILE),
568
OPT_BOOL_F('f', "force", &force, N_("replace the ref if it exists"),
569
PARSE_OPT_NOCOMPLETE),
570
OPT_BOOL(0, "raw", &raw, N_("do not pretty-print contents for --edit")),
571
OPT_STRING(0, "format", &format, N_("format"), N_("use this format")),
575
disable_replace_refs();
576
git_config(git_default_config, NULL);
578
argc = parse_options(argc, argv, prefix, options, git_replace_usage, 0);
581
cmdmode = argc ? MODE_REPLACE : MODE_LIST;
583
if (format && cmdmode != MODE_LIST)
584
usage_msg_opt(_("--format cannot be used when not listing"),
585
git_replace_usage, options);
588
cmdmode != MODE_REPLACE &&
589
cmdmode != MODE_EDIT &&
590
cmdmode != MODE_GRAFT &&
591
cmdmode != MODE_CONVERT_GRAFT_FILE)
592
usage_msg_opt(_("-f only makes sense when writing a replacement"),
593
git_replace_usage, options);
595
if (raw && cmdmode != MODE_EDIT)
596
usage_msg_opt(_("--raw only makes sense with --edit"),
597
git_replace_usage, options);
602
usage_msg_opt(_("-d needs at least one argument"),
603
git_replace_usage, options);
604
return for_each_replace_name(argv, delete_replace_ref);
608
usage_msg_opt(_("bad number of arguments"),
609
git_replace_usage, options);
610
return replace_object(argv[0], argv[1], force);
614
usage_msg_opt(_("-e needs exactly one argument"),
615
git_replace_usage, options);
616
return edit_and_replace(argv[0], force, raw);
620
usage_msg_opt(_("-g needs at least one argument"),
621
git_replace_usage, options);
622
return create_graft(argc, argv, force, 0);
624
case MODE_CONVERT_GRAFT_FILE:
626
usage_msg_opt(_("--convert-graft-file takes no argument"),
627
git_replace_usage, options);
628
return !!convert_graft_file(force);
632
usage_msg_opt(_("only one pattern can be given with -l"),
633
git_replace_usage, options);
634
return list_replace_refs(argv[0], format);
637
BUG("invalid cmdmode %d", (int)cmdmode);