]>
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" | |
12 | #include "builtin.h" | |
13 | #include "refs.h" | |
14 | #include "parse-options.h" | |
b892bb45 | 15 | #include "run-command.h" |
25a05a8c | 16 | #include "tag.h" |
54b0c1e0 CC |
17 | |
18 | static const char * const git_replace_usage[] = { | |
2477bebb | 19 | N_("git replace [-f] <object> <replacement>"), |
ab77c309 | 20 | N_("git replace [-f] --edit <object>"), |
4228e8bc | 21 | N_("git replace [-f] --graft <commit> [<parent>...]"), |
2477bebb | 22 | N_("git replace -d <object>..."), |
44f9f850 | 23 | N_("git replace [--format=<format>] [-l [<pattern>]]"), |
54b0c1e0 CC |
24 | NULL |
25 | }; | |
26 | ||
663a8566 | 27 | enum replace_format { |
3cc9d877 JK |
28 | REPLACE_FORMAT_SHORT, |
29 | REPLACE_FORMAT_MEDIUM, | |
30 | REPLACE_FORMAT_LONG | |
663a8566 | 31 | }; |
44f9f850 CC |
32 | |
33 | struct show_data { | |
34 | const char *pattern; | |
663a8566 | 35 | enum replace_format format; |
44f9f850 CC |
36 | }; |
37 | ||
54b0c1e0 CC |
38 | static int show_reference(const char *refname, const unsigned char *sha1, |
39 | int flag, void *cb_data) | |
40 | { | |
44f9f850 CC |
41 | struct show_data *data = cb_data; |
42 | ||
eb07894f | 43 | if (!wildmatch(data->pattern, refname, 0, NULL)) { |
663a8566 | 44 | if (data->format == REPLACE_FORMAT_SHORT) |
44f9f850 | 45 | printf("%s\n", refname); |
663a8566 | 46 | else if (data->format == REPLACE_FORMAT_MEDIUM) |
44f9f850 | 47 | printf("%s -> %s\n", refname, sha1_to_hex(sha1)); |
663a8566 | 48 | else { /* data->format == REPLACE_FORMAT_LONG */ |
44f9f850 CC |
49 | unsigned char object[20]; |
50 | enum object_type obj_type, repl_type; | |
54b0c1e0 | 51 | |
44f9f850 CC |
52 | if (get_sha1(refname, object)) |
53 | return error("Failed to resolve '%s' as a valid ref.", refname); | |
54 | ||
55 | obj_type = sha1_object_info(object, NULL); | |
56 | repl_type = sha1_object_info(sha1, NULL); | |
57 | ||
58 | printf("%s (%s) -> %s (%s)\n", refname, typename(obj_type), | |
59 | sha1_to_hex(sha1), typename(repl_type)); | |
60 | } | |
61 | } | |
54b0c1e0 CC |
62 | |
63 | return 0; | |
64 | } | |
65 | ||
44f9f850 | 66 | static int list_replace_refs(const char *pattern, const char *format) |
54b0c1e0 | 67 | { |
44f9f850 CC |
68 | struct show_data data; |
69 | ||
54b0c1e0 CC |
70 | if (pattern == NULL) |
71 | pattern = "*"; | |
44f9f850 CC |
72 | data.pattern = pattern; |
73 | ||
74 | if (format == NULL || *format == '\0' || !strcmp(format, "short")) | |
663a8566 | 75 | data.format = REPLACE_FORMAT_SHORT; |
44f9f850 | 76 | else if (!strcmp(format, "medium")) |
663a8566 CC |
77 | data.format = REPLACE_FORMAT_MEDIUM; |
78 | else if (!strcmp(format, "long")) | |
79 | data.format = REPLACE_FORMAT_LONG; | |
44f9f850 CC |
80 | else |
81 | die("invalid replace format '%s'\n" | |
663a8566 | 82 | "valid formats are 'short', 'medium' and 'long'\n", |
44f9f850 | 83 | format); |
54b0c1e0 | 84 | |
44f9f850 | 85 | for_each_replace_ref(show_reference, (void *) &data); |
54b0c1e0 CC |
86 | |
87 | return 0; | |
88 | } | |
89 | ||
90 | typedef int (*each_replace_name_fn)(const char *name, const char *ref, | |
91 | const unsigned char *sha1); | |
92 | ||
93 | static int for_each_replace_name(const char **argv, each_replace_name_fn fn) | |
94 | { | |
9dfc3684 | 95 | const char **p, *full_hex; |
54b0c1e0 CC |
96 | char ref[PATH_MAX]; |
97 | int had_error = 0; | |
98 | unsigned char sha1[20]; | |
99 | ||
100 | for (p = argv; *p; p++) { | |
9dfc3684 MG |
101 | if (get_sha1(*p, sha1)) { |
102 | error("Failed to resolve '%s' as a valid ref.", *p); | |
54b0c1e0 CC |
103 | had_error = 1; |
104 | continue; | |
105 | } | |
9dfc3684 MG |
106 | full_hex = sha1_to_hex(sha1); |
107 | snprintf(ref, sizeof(ref), "refs/replace/%s", full_hex); | |
108 | /* read_ref() may reuse the buffer */ | |
109 | full_hex = ref + strlen("refs/replace/"); | |
c6893323 | 110 | if (read_ref(ref, sha1)) { |
9dfc3684 | 111 | error("replace ref '%s' not found.", full_hex); |
54b0c1e0 CC |
112 | had_error = 1; |
113 | continue; | |
114 | } | |
9dfc3684 | 115 | if (fn(full_hex, ref, sha1)) |
54b0c1e0 CC |
116 | had_error = 1; |
117 | } | |
118 | return had_error; | |
119 | } | |
120 | ||
121 | static int delete_replace_ref(const char *name, const char *ref, | |
122 | const unsigned char *sha1) | |
123 | { | |
124 | if (delete_ref(ref, sha1, 0)) | |
125 | return 1; | |
126 | printf("Deleted replace ref '%s'\n", name); | |
127 | return 0; | |
128 | } | |
129 | ||
b6e38840 CC |
130 | static void check_ref_valid(unsigned char object[20], |
131 | unsigned char prev[20], | |
132 | char *ref, | |
133 | int ref_size, | |
134 | int force) | |
135 | { | |
136 | if (snprintf(ref, ref_size, | |
137 | "refs/replace/%s", | |
138 | sha1_to_hex(object)) > ref_size - 1) | |
139 | die("replace ref name too long: %.*s...", 50, ref); | |
140 | if (check_refname_format(ref, 0)) | |
141 | die("'%s' is not a valid ref name.", ref); | |
142 | ||
143 | if (read_ref(ref, prev)) | |
144 | hashclr(prev); | |
145 | else if (!force) | |
146 | die("replace ref '%s' already exists", ref); | |
147 | } | |
148 | ||
479bd757 JK |
149 | static int replace_object_sha1(const char *object_ref, |
150 | unsigned char object[20], | |
151 | const char *replace_ref, | |
152 | unsigned char repl[20], | |
153 | int force) | |
bebdd271 | 154 | { |
479bd757 | 155 | unsigned char prev[20]; |
277336a5 | 156 | enum object_type obj_type, repl_type; |
bebdd271 | 157 | char ref[PATH_MAX]; |
867c2fac RS |
158 | struct ref_transaction *transaction; |
159 | struct strbuf err = STRBUF_INIT; | |
bebdd271 | 160 | |
277336a5 CC |
161 | obj_type = sha1_object_info(object, NULL); |
162 | repl_type = sha1_object_info(repl, NULL); | |
163 | if (!force && obj_type != repl_type) | |
164 | die("Objects must be of the same type.\n" | |
165 | "'%s' points to a replaced object of type '%s'\n" | |
166 | "while '%s' points to a replacement object of type '%s'.", | |
167 | object_ref, typename(obj_type), | |
168 | replace_ref, typename(repl_type)); | |
169 | ||
b6e38840 | 170 | check_ref_valid(object, prev, ref, sizeof(ref), force); |
bebdd271 | 171 | |
867c2fac RS |
172 | transaction = ref_transaction_begin(&err); |
173 | if (!transaction || | |
174 | ref_transaction_update(transaction, ref, repl, prev, 0, 1, &err) || | |
175 | ref_transaction_commit(transaction, NULL, &err)) | |
176 | die("%s", err.buf); | |
bebdd271 | 177 | |
867c2fac | 178 | ref_transaction_free(transaction); |
bebdd271 CC |
179 | return 0; |
180 | } | |
181 | ||
479bd757 JK |
182 | static int replace_object(const char *object_ref, const char *replace_ref, int force) |
183 | { | |
184 | unsigned char object[20], repl[20]; | |
185 | ||
186 | if (get_sha1(object_ref, object)) | |
187 | die("Failed to resolve '%s' as a valid ref.", object_ref); | |
188 | if (get_sha1(replace_ref, repl)) | |
189 | die("Failed to resolve '%s' as a valid ref.", replace_ref); | |
190 | ||
191 | return replace_object_sha1(object_ref, object, replace_ref, repl, force); | |
192 | } | |
193 | ||
b892bb45 | 194 | /* |
2deda629 JK |
195 | * Write the contents of the object named by "sha1" to the file "filename". |
196 | * If "raw" is true, then the object's raw contents are printed according to | |
197 | * "type". Otherwise, we pretty-print the contents for human editing. | |
b892bb45 | 198 | */ |
2deda629 JK |
199 | static void export_object(const unsigned char *sha1, enum object_type type, |
200 | int raw, const char *filename) | |
b892bb45 | 201 | { |
d3180279 | 202 | struct child_process cmd = CHILD_PROCESS_INIT; |
b892bb45 JK |
203 | int fd; |
204 | ||
205 | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666); | |
206 | if (fd < 0) | |
207 | die_errno("unable to open %s for writing", filename); | |
208 | ||
36857e00 JK |
209 | argv_array_push(&cmd.args, "--no-replace-objects"); |
210 | argv_array_push(&cmd.args, "cat-file"); | |
2deda629 JK |
211 | if (raw) |
212 | argv_array_push(&cmd.args, typename(type)); | |
213 | else | |
214 | argv_array_push(&cmd.args, "-p"); | |
36857e00 | 215 | argv_array_push(&cmd.args, sha1_to_hex(sha1)); |
b892bb45 JK |
216 | cmd.git_cmd = 1; |
217 | cmd.out = fd; | |
218 | ||
219 | if (run_command(&cmd)) | |
220 | die("cat-file reported failure"); | |
b892bb45 JK |
221 | } |
222 | ||
223 | /* | |
224 | * Read a previously-exported (and possibly edited) object back from "filename", | |
225 | * interpreting it as "type", and writing the result to the object database. | |
226 | * The sha1 of the written object is returned via sha1. | |
227 | */ | |
228 | static void import_object(unsigned char *sha1, enum object_type type, | |
2deda629 | 229 | int raw, const char *filename) |
b892bb45 JK |
230 | { |
231 | int fd; | |
232 | ||
233 | fd = open(filename, O_RDONLY); | |
234 | if (fd < 0) | |
235 | die_errno("unable to open %s for reading", filename); | |
236 | ||
2deda629 | 237 | if (!raw && type == OBJ_TREE) { |
b892bb45 | 238 | const char *argv[] = { "mktree", NULL }; |
d3180279 | 239 | struct child_process cmd = CHILD_PROCESS_INIT; |
b892bb45 JK |
240 | struct strbuf result = STRBUF_INIT; |
241 | ||
242 | cmd.argv = argv; | |
243 | cmd.git_cmd = 1; | |
244 | cmd.in = fd; | |
245 | cmd.out = -1; | |
246 | ||
247 | if (start_command(&cmd)) | |
248 | die("unable to spawn mktree"); | |
249 | ||
250 | if (strbuf_read(&result, cmd.out, 41) < 0) | |
251 | die_errno("unable to read from mktree"); | |
252 | close(cmd.out); | |
253 | ||
254 | if (finish_command(&cmd)) | |
255 | die("mktree reported failure"); | |
256 | if (get_sha1_hex(result.buf, sha1) < 0) | |
257 | die("mktree did not return an object name"); | |
258 | ||
259 | strbuf_release(&result); | |
260 | } else { | |
261 | struct stat st; | |
262 | int flags = HASH_FORMAT_CHECK | HASH_WRITE_OBJECT; | |
263 | ||
264 | if (fstat(fd, &st) < 0) | |
265 | die_errno("unable to fstat %s", filename); | |
266 | if (index_fd(sha1, fd, &st, type, NULL, flags) < 0) | |
267 | die("unable to write object to database"); | |
268 | /* index_fd close()s fd for us */ | |
269 | } | |
270 | ||
271 | /* | |
272 | * No need to close(fd) here; both run-command and index-fd | |
273 | * will have done it for us. | |
274 | */ | |
275 | } | |
276 | ||
2deda629 | 277 | static int edit_and_replace(const char *object_ref, int force, int raw) |
b892bb45 JK |
278 | { |
279 | char *tmpfile = git_pathdup("REPLACE_EDITOBJ"); | |
280 | enum object_type type; | |
24790835 CC |
281 | unsigned char old[20], new[20], prev[20]; |
282 | char ref[PATH_MAX]; | |
b892bb45 JK |
283 | |
284 | if (get_sha1(object_ref, old) < 0) | |
285 | die("Not a valid object name: '%s'", object_ref); | |
286 | ||
287 | type = sha1_object_info(old, NULL); | |
288 | if (type < 0) | |
289 | die("unable to get object type for %s", sha1_to_hex(old)); | |
290 | ||
24790835 CC |
291 | check_ref_valid(old, prev, ref, sizeof(ref), force); |
292 | ||
2deda629 | 293 | export_object(old, type, raw, tmpfile); |
b892bb45 JK |
294 | if (launch_editor(tmpfile, NULL, NULL) < 0) |
295 | die("editing object file failed"); | |
2deda629 | 296 | import_object(new, type, raw, tmpfile); |
b892bb45 JK |
297 | |
298 | free(tmpfile); | |
299 | ||
f22166b5 CC |
300 | if (!hashcmp(old, new)) |
301 | return error("new object is the same as the old one: '%s'", sha1_to_hex(old)); | |
302 | ||
b892bb45 JK |
303 | return replace_object_sha1(object_ref, old, "replacement", new, force); |
304 | } | |
305 | ||
4228e8bc CC |
306 | static void replace_parents(struct strbuf *buf, int argc, const char **argv) |
307 | { | |
308 | struct strbuf new_parents = STRBUF_INIT; | |
309 | const char *parent_start, *parent_end; | |
310 | int i; | |
311 | ||
312 | /* find existing parents */ | |
313 | parent_start = buf->buf; | |
314 | parent_start += 46; /* "tree " + "hex sha1" + "\n" */ | |
315 | parent_end = parent_start; | |
316 | ||
317 | while (starts_with(parent_end, "parent ")) | |
318 | parent_end += 48; /* "parent " + "hex sha1" + "\n" */ | |
319 | ||
320 | /* prepare new parents */ | |
321 | for (i = 0; i < argc; i++) { | |
322 | unsigned char sha1[20]; | |
323 | if (get_sha1(argv[i], sha1) < 0) | |
324 | die(_("Not a valid object name: '%s'"), argv[i]); | |
325 | lookup_commit_or_die(sha1, argv[i]); | |
326 | strbuf_addf(&new_parents, "parent %s\n", sha1_to_hex(sha1)); | |
327 | } | |
328 | ||
329 | /* replace existing parents with new ones */ | |
330 | strbuf_splice(buf, parent_start - buf->buf, parent_end - parent_start, | |
331 | new_parents.buf, new_parents.len); | |
332 | ||
333 | strbuf_release(&new_parents); | |
334 | } | |
335 | ||
25a05a8c CC |
336 | struct check_mergetag_data { |
337 | int argc; | |
338 | const char **argv; | |
339 | }; | |
340 | ||
341 | static void check_one_mergetag(struct commit *commit, | |
342 | struct commit_extra_header *extra, | |
343 | void *data) | |
344 | { | |
345 | struct check_mergetag_data *mergetag_data = (struct check_mergetag_data *)data; | |
346 | const char *ref = mergetag_data->argv[0]; | |
347 | unsigned char tag_sha1[20]; | |
348 | struct tag *tag; | |
349 | int i; | |
350 | ||
351 | hash_sha1_file(extra->value, extra->len, typename(OBJ_TAG), tag_sha1); | |
352 | tag = lookup_tag(tag_sha1); | |
353 | if (!tag) | |
354 | die(_("bad mergetag in commit '%s'"), ref); | |
355 | if (parse_tag_buffer(tag, extra->value, extra->len)) | |
356 | die(_("malformed mergetag in commit '%s'"), ref); | |
357 | ||
358 | /* iterate over new parents */ | |
359 | for (i = 1; i < mergetag_data->argc; i++) { | |
360 | unsigned char sha1[20]; | |
361 | if (get_sha1(mergetag_data->argv[i], sha1) < 0) | |
362 | die(_("Not a valid object name: '%s'"), mergetag_data->argv[i]); | |
363 | if (!hashcmp(tag->tagged->sha1, sha1)) | |
364 | return; /* found */ | |
365 | } | |
366 | ||
367 | die(_("original commit '%s' contains mergetag '%s' that is discarded; " | |
368 | "use --edit instead of --graft"), ref, sha1_to_hex(tag_sha1)); | |
369 | } | |
370 | ||
371 | static void check_mergetags(struct commit *commit, int argc, const char **argv) | |
372 | { | |
373 | struct check_mergetag_data mergetag_data; | |
374 | ||
375 | mergetag_data.argc = argc; | |
376 | mergetag_data.argv = argv; | |
377 | for_each_mergetag(check_one_mergetag, commit, &mergetag_data); | |
378 | } | |
379 | ||
4228e8bc CC |
380 | static int create_graft(int argc, const char **argv, int force) |
381 | { | |
382 | unsigned char old[20], new[20]; | |
383 | const char *old_ref = argv[0]; | |
384 | struct commit *commit; | |
385 | struct strbuf buf = STRBUF_INIT; | |
386 | const char *buffer; | |
387 | unsigned long size; | |
388 | ||
389 | if (get_sha1(old_ref, old) < 0) | |
390 | die(_("Not a valid object name: '%s'"), old_ref); | |
391 | commit = lookup_commit_or_die(old, old_ref); | |
392 | ||
393 | buffer = get_commit_buffer(commit, &size); | |
394 | strbuf_add(&buf, buffer, size); | |
395 | unuse_commit_buffer(commit, buffer); | |
396 | ||
397 | replace_parents(&buf, argc - 1, &argv[1]); | |
398 | ||
0b05ab6f CC |
399 | if (remove_signature(&buf)) { |
400 | warning(_("the original commit '%s' has a gpg signature."), old_ref); | |
401 | warning(_("the signature will be removed in the replacement commit!")); | |
402 | } | |
403 | ||
25a05a8c CC |
404 | check_mergetags(commit, argc, argv); |
405 | ||
4228e8bc CC |
406 | if (write_sha1_file(buf.buf, buf.len, commit_type, new)) |
407 | die(_("could not write replacement commit for: '%s'"), old_ref); | |
408 | ||
409 | strbuf_release(&buf); | |
410 | ||
411 | if (!hashcmp(old, new)) | |
412 | return error("new commit is the same as the old one: '%s'", sha1_to_hex(old)); | |
413 | ||
414 | return replace_object_sha1(old_ref, old, "replacement", new, force); | |
415 | } | |
416 | ||
54b0c1e0 CC |
417 | int cmd_replace(int argc, const char **argv, const char *prefix) |
418 | { | |
70c7bd6d | 419 | int force = 0; |
2deda629 | 420 | int raw = 0; |
44f9f850 | 421 | const char *format = NULL; |
70c7bd6d JK |
422 | enum { |
423 | MODE_UNSPECIFIED = 0, | |
424 | MODE_LIST, | |
425 | MODE_DELETE, | |
b892bb45 | 426 | MODE_EDIT, |
4228e8bc | 427 | MODE_GRAFT, |
70c7bd6d JK |
428 | MODE_REPLACE |
429 | } cmdmode = MODE_UNSPECIFIED; | |
54b0c1e0 | 430 | struct option options[] = { |
70c7bd6d JK |
431 | OPT_CMDMODE('l', "list", &cmdmode, N_("list replace refs"), MODE_LIST), |
432 | OPT_CMDMODE('d', "delete", &cmdmode, N_("delete replace refs"), MODE_DELETE), | |
b892bb45 | 433 | OPT_CMDMODE('e', "edit", &cmdmode, N_("edit existing object"), MODE_EDIT), |
4228e8bc | 434 | OPT_CMDMODE('g', "graft", &cmdmode, N_("change a commit's parents"), MODE_GRAFT), |
80f165a5 | 435 | OPT_BOOL('f', "force", &force, N_("replace the ref if it exists")), |
2deda629 | 436 | OPT_BOOL(0, "raw", &raw, N_("do not pretty-print contents for --edit")), |
44f9f850 | 437 | OPT_STRING(0, "format", &format, N_("format"), N_("use this format")), |
54b0c1e0 CC |
438 | OPT_END() |
439 | }; | |
440 | ||
afc711b8 | 441 | check_replace_refs = 0; |
769a4fa4 | 442 | |
451bb210 | 443 | argc = parse_options(argc, argv, prefix, options, git_replace_usage, 0); |
54b0c1e0 | 444 | |
70c7bd6d JK |
445 | if (!cmdmode) |
446 | cmdmode = argc ? MODE_REPLACE : MODE_LIST; | |
3f495f67 | 447 | |
70c7bd6d | 448 | if (format && cmdmode != MODE_LIST) |
3f495f67 | 449 | usage_msg_opt("--format cannot be used when not listing", |
44f9f850 CC |
450 | git_replace_usage, options); |
451 | ||
4228e8bc CC |
452 | if (force && |
453 | cmdmode != MODE_REPLACE && | |
454 | cmdmode != MODE_EDIT && | |
455 | cmdmode != MODE_GRAFT) | |
70c7bd6d | 456 | usage_msg_opt("-f only makes sense when writing a replacement", |
86af2caa | 457 | git_replace_usage, options); |
bebdd271 | 458 | |
2deda629 JK |
459 | if (raw && cmdmode != MODE_EDIT) |
460 | usage_msg_opt("--raw only makes sense with --edit", | |
461 | git_replace_usage, options); | |
462 | ||
70c7bd6d JK |
463 | switch (cmdmode) { |
464 | case MODE_DELETE: | |
54b0c1e0 | 465 | if (argc < 1) |
86af2caa CC |
466 | usage_msg_opt("-d needs at least one argument", |
467 | git_replace_usage, options); | |
54b0c1e0 | 468 | return for_each_replace_name(argv, delete_replace_ref); |
54b0c1e0 | 469 | |
70c7bd6d | 470 | case MODE_REPLACE: |
bebdd271 | 471 | if (argc != 2) |
86af2caa CC |
472 | usage_msg_opt("bad number of arguments", |
473 | git_replace_usage, options); | |
bebdd271 | 474 | return replace_object(argv[0], argv[1], force); |
bebdd271 | 475 | |
b892bb45 JK |
476 | case MODE_EDIT: |
477 | if (argc != 1) | |
478 | usage_msg_opt("-e needs exactly one argument", | |
479 | git_replace_usage, options); | |
2deda629 | 480 | return edit_and_replace(argv[0], force, raw); |
b892bb45 | 481 | |
4228e8bc CC |
482 | case MODE_GRAFT: |
483 | if (argc < 1) | |
484 | usage_msg_opt("-g needs at least one argument", | |
485 | git_replace_usage, options); | |
486 | return create_graft(argc, argv, force); | |
487 | ||
70c7bd6d JK |
488 | case MODE_LIST: |
489 | if (argc > 1) | |
490 | usage_msg_opt("only one pattern can be given with -l", | |
491 | git_replace_usage, options); | |
492 | return list_replace_refs(argv[0], format); | |
54b0c1e0 | 493 | |
70c7bd6d JK |
494 | default: |
495 | die("BUG: invalid cmdmode %d", (int)cmdmode); | |
496 | } | |
54b0c1e0 | 497 | } |