2 * Builtin "git replace"
4 * Copyright (c) 2008 Christian Couder <chriscool@tuxfamily.org>
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.
14 #include "parse-options.h"
15 #include "run-command.h"
17 static const char * const git_replace_usage
[] = {
18 N_("git replace [-f] <object> <replacement>"),
19 N_("git replace [-f] --edit <object>"),
20 N_("git replace [-f] --graft <commit> [<parent>...]"),
21 N_("git replace -d <object>..."),
22 N_("git replace [--format=<format>] [-l [<pattern>]]"),
28 REPLACE_FORMAT_MEDIUM
,
34 enum replace_format format
;
37 static int show_reference(const char *refname
, const unsigned char *sha1
,
38 int flag
, void *cb_data
)
40 struct show_data
*data
= cb_data
;
42 if (!wildmatch(data
->pattern
, refname
, 0, NULL
)) {
43 if (data
->format
== REPLACE_FORMAT_SHORT
)
44 printf("%s\n", refname
);
45 else if (data
->format
== REPLACE_FORMAT_MEDIUM
)
46 printf("%s -> %s\n", refname
, sha1_to_hex(sha1
));
47 else { /* data->format == REPLACE_FORMAT_LONG */
48 unsigned char object
[20];
49 enum object_type obj_type
, repl_type
;
51 if (get_sha1(refname
, object
))
52 return error("Failed to resolve '%s' as a valid ref.", refname
);
54 obj_type
= sha1_object_info(object
, NULL
);
55 repl_type
= sha1_object_info(sha1
, NULL
);
57 printf("%s (%s) -> %s (%s)\n", refname
, typename(obj_type
),
58 sha1_to_hex(sha1
), typename(repl_type
));
65 static int list_replace_refs(const char *pattern
, const char *format
)
67 struct show_data data
;
71 data
.pattern
= pattern
;
73 if (format
== NULL
|| *format
== '\0' || !strcmp(format
, "short"))
74 data
.format
= REPLACE_FORMAT_SHORT
;
75 else if (!strcmp(format
, "medium"))
76 data
.format
= REPLACE_FORMAT_MEDIUM
;
77 else if (!strcmp(format
, "long"))
78 data
.format
= REPLACE_FORMAT_LONG
;
80 die("invalid replace format '%s'\n"
81 "valid formats are 'short', 'medium' and 'long'\n",
84 for_each_replace_ref(show_reference
, (void *) &data
);
89 typedef int (*each_replace_name_fn
)(const char *name
, const char *ref
,
90 const unsigned char *sha1
);
92 static int for_each_replace_name(const char **argv
, each_replace_name_fn fn
)
94 const char **p
, *full_hex
;
97 unsigned char sha1
[20];
99 for (p
= argv
; *p
; p
++) {
100 if (get_sha1(*p
, sha1
)) {
101 error("Failed to resolve '%s' as a valid ref.", *p
);
105 full_hex
= sha1_to_hex(sha1
);
106 snprintf(ref
, sizeof(ref
), "refs/replace/%s", full_hex
);
107 /* read_ref() may reuse the buffer */
108 full_hex
= ref
+ strlen("refs/replace/");
109 if (read_ref(ref
, sha1
)) {
110 error("replace ref '%s' not found.", full_hex
);
114 if (fn(full_hex
, ref
, sha1
))
120 static int delete_replace_ref(const char *name
, const char *ref
,
121 const unsigned char *sha1
)
123 if (delete_ref(ref
, sha1
, 0))
125 printf("Deleted replace ref '%s'\n", name
);
129 static void check_ref_valid(unsigned char object
[20],
130 unsigned char prev
[20],
135 if (snprintf(ref
, ref_size
,
137 sha1_to_hex(object
)) > ref_size
- 1)
138 die("replace ref name too long: %.*s...", 50, ref
);
139 if (check_refname_format(ref
, 0))
140 die("'%s' is not a valid ref name.", ref
);
142 if (read_ref(ref
, prev
))
145 die("replace ref '%s' already exists", ref
);
148 static int replace_object_sha1(const char *object_ref
,
149 unsigned char object
[20],
150 const char *replace_ref
,
151 unsigned char repl
[20],
154 unsigned char prev
[20];
155 enum object_type obj_type
, repl_type
;
157 struct ref_lock
*lock
;
159 obj_type
= sha1_object_info(object
, NULL
);
160 repl_type
= sha1_object_info(repl
, NULL
);
161 if (!force
&& obj_type
!= repl_type
)
162 die("Objects must be of the same type.\n"
163 "'%s' points to a replaced object of type '%s'\n"
164 "while '%s' points to a replacement object of type '%s'.",
165 object_ref
, typename(obj_type
),
166 replace_ref
, typename(repl_type
));
168 check_ref_valid(object
, prev
, ref
, sizeof(ref
), force
);
170 lock
= lock_any_ref_for_update(ref
, prev
, 0, NULL
);
172 die("%s: cannot lock the ref", ref
);
173 if (write_ref_sha1(lock
, repl
, NULL
) < 0)
174 die("%s: cannot update the ref", ref
);
179 static int replace_object(const char *object_ref
, const char *replace_ref
, int force
)
181 unsigned char object
[20], repl
[20];
183 if (get_sha1(object_ref
, object
))
184 die("Failed to resolve '%s' as a valid ref.", object_ref
);
185 if (get_sha1(replace_ref
, repl
))
186 die("Failed to resolve '%s' as a valid ref.", replace_ref
);
188 return replace_object_sha1(object_ref
, object
, replace_ref
, repl
, force
);
192 * Write the contents of the object named by "sha1" to the file "filename",
193 * pretty-printed for human editing based on its type.
195 static void export_object(const unsigned char *sha1
, const char *filename
)
197 const char *argv
[] = { "--no-replace-objects", "cat-file", "-p", NULL
, NULL
};
198 struct child_process cmd
= { argv
};
201 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
203 die_errno("unable to open %s for writing", filename
);
205 argv
[3] = sha1_to_hex(sha1
);
209 if (run_command(&cmd
))
210 die("cat-file reported failure");
216 * Read a previously-exported (and possibly edited) object back from "filename",
217 * interpreting it as "type", and writing the result to the object database.
218 * The sha1 of the written object is returned via sha1.
220 static void import_object(unsigned char *sha1
, enum object_type type
,
221 const char *filename
)
225 fd
= open(filename
, O_RDONLY
);
227 die_errno("unable to open %s for reading", filename
);
229 if (type
== OBJ_TREE
) {
230 const char *argv
[] = { "mktree", NULL
};
231 struct child_process cmd
= { argv
};
232 struct strbuf result
= STRBUF_INIT
;
239 if (start_command(&cmd
))
240 die("unable to spawn mktree");
242 if (strbuf_read(&result
, cmd
.out
, 41) < 0)
243 die_errno("unable to read from mktree");
246 if (finish_command(&cmd
))
247 die("mktree reported failure");
248 if (get_sha1_hex(result
.buf
, sha1
) < 0)
249 die("mktree did not return an object name");
251 strbuf_release(&result
);
254 int flags
= HASH_FORMAT_CHECK
| HASH_WRITE_OBJECT
;
256 if (fstat(fd
, &st
) < 0)
257 die_errno("unable to fstat %s", filename
);
258 if (index_fd(sha1
, fd
, &st
, type
, NULL
, flags
) < 0)
259 die("unable to write object to database");
260 /* index_fd close()s fd for us */
264 * No need to close(fd) here; both run-command and index-fd
265 * will have done it for us.
269 static int edit_and_replace(const char *object_ref
, int force
)
271 char *tmpfile
= git_pathdup("REPLACE_EDITOBJ");
272 enum object_type type
;
273 unsigned char old
[20], new[20], prev
[20];
276 if (get_sha1(object_ref
, old
) < 0)
277 die("Not a valid object name: '%s'", object_ref
);
279 type
= sha1_object_info(old
, NULL
);
281 die("unable to get object type for %s", sha1_to_hex(old
));
283 check_ref_valid(old
, prev
, ref
, sizeof(ref
), force
);
285 export_object(old
, tmpfile
);
286 if (launch_editor(tmpfile
, NULL
, NULL
) < 0)
287 die("editing object file failed");
288 import_object(new, type
, tmpfile
);
292 if (!hashcmp(old
, new))
293 return error("new object is the same as the old one: '%s'", sha1_to_hex(old
));
295 return replace_object_sha1(object_ref
, old
, "replacement", new, force
);
298 static void replace_parents(struct strbuf
*buf
, int argc
, const char **argv
)
300 struct strbuf new_parents
= STRBUF_INIT
;
301 const char *parent_start
, *parent_end
;
304 /* find existing parents */
305 parent_start
= buf
->buf
;
306 parent_start
+= 46; /* "tree " + "hex sha1" + "\n" */
307 parent_end
= parent_start
;
309 while (starts_with(parent_end
, "parent "))
310 parent_end
+= 48; /* "parent " + "hex sha1" + "\n" */
312 /* prepare new parents */
313 for (i
= 0; i
< argc
; i
++) {
314 unsigned char sha1
[20];
315 if (get_sha1(argv
[i
], sha1
) < 0)
316 die(_("Not a valid object name: '%s'"), argv
[i
]);
317 lookup_commit_or_die(sha1
, argv
[i
]);
318 strbuf_addf(&new_parents
, "parent %s\n", sha1_to_hex(sha1
));
321 /* replace existing parents with new ones */
322 strbuf_splice(buf
, parent_start
- buf
->buf
, parent_end
- parent_start
,
323 new_parents
.buf
, new_parents
.len
);
325 strbuf_release(&new_parents
);
328 static int create_graft(int argc
, const char **argv
, int force
)
330 unsigned char old
[20], new[20];
331 const char *old_ref
= argv
[0];
332 struct commit
*commit
;
333 struct strbuf buf
= STRBUF_INIT
;
337 if (get_sha1(old_ref
, old
) < 0)
338 die(_("Not a valid object name: '%s'"), old_ref
);
339 commit
= lookup_commit_or_die(old
, old_ref
);
341 buffer
= get_commit_buffer(commit
, &size
);
342 strbuf_add(&buf
, buffer
, size
);
343 unuse_commit_buffer(commit
, buffer
);
345 replace_parents(&buf
, argc
- 1, &argv
[1]);
347 if (write_sha1_file(buf
.buf
, buf
.len
, commit_type
, new))
348 die(_("could not write replacement commit for: '%s'"), old_ref
);
350 strbuf_release(&buf
);
352 if (!hashcmp(old
, new))
353 return error("new commit is the same as the old one: '%s'", sha1_to_hex(old
));
355 return replace_object_sha1(old_ref
, old
, "replacement", new, force
);
358 int cmd_replace(int argc
, const char **argv
, const char *prefix
)
361 const char *format
= NULL
;
363 MODE_UNSPECIFIED
= 0,
369 } cmdmode
= MODE_UNSPECIFIED
;
370 struct option options
[] = {
371 OPT_CMDMODE('l', "list", &cmdmode
, N_("list replace refs"), MODE_LIST
),
372 OPT_CMDMODE('d', "delete", &cmdmode
, N_("delete replace refs"), MODE_DELETE
),
373 OPT_CMDMODE('e', "edit", &cmdmode
, N_("edit existing object"), MODE_EDIT
),
374 OPT_CMDMODE('g', "graft", &cmdmode
, N_("change a commit's parents"), MODE_GRAFT
),
375 OPT_BOOL('f', "force", &force
, N_("replace the ref if it exists")),
376 OPT_STRING(0, "format", &format
, N_("format"), N_("use this format")),
380 check_replace_refs
= 0;
382 argc
= parse_options(argc
, argv
, prefix
, options
, git_replace_usage
, 0);
385 cmdmode
= argc
? MODE_REPLACE
: MODE_LIST
;
387 if (format
&& cmdmode
!= MODE_LIST
)
388 usage_msg_opt("--format cannot be used when not listing",
389 git_replace_usage
, options
);
392 cmdmode
!= MODE_REPLACE
&&
393 cmdmode
!= MODE_EDIT
&&
394 cmdmode
!= MODE_GRAFT
)
395 usage_msg_opt("-f only makes sense when writing a replacement",
396 git_replace_usage
, options
);
401 usage_msg_opt("-d needs at least one argument",
402 git_replace_usage
, options
);
403 return for_each_replace_name(argv
, delete_replace_ref
);
407 usage_msg_opt("bad number of arguments",
408 git_replace_usage
, options
);
409 return replace_object(argv
[0], argv
[1], force
);
413 usage_msg_opt("-e needs exactly one argument",
414 git_replace_usage
, options
);
415 return edit_and_replace(argv
[0], force
);
419 usage_msg_opt("-g needs at least one argument",
420 git_replace_usage
, options
);
421 return create_graft(argc
, argv
, force
);
425 usage_msg_opt("only one pattern can be given with -l",
426 git_replace_usage
, options
);
427 return list_replace_refs(argv
[0], format
);
430 die("BUG: invalid cmdmode %d", (int)cmdmode
);