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 -d <object>..."),
20 N_("git replace [--format=<format>] [-l [<pattern>]]"),
26 REPLACE_FORMAT_MEDIUM
,
32 enum replace_format format
;
35 static int show_reference(const char *refname
, const unsigned char *sha1
,
36 int flag
, void *cb_data
)
38 struct show_data
*data
= cb_data
;
40 if (!wildmatch(data
->pattern
, refname
, 0, NULL
)) {
41 if (data
->format
== REPLACE_FORMAT_SHORT
)
42 printf("%s\n", refname
);
43 else if (data
->format
== REPLACE_FORMAT_MEDIUM
)
44 printf("%s -> %s\n", refname
, sha1_to_hex(sha1
));
45 else { /* data->format == REPLACE_FORMAT_LONG */
46 unsigned char object
[20];
47 enum object_type obj_type
, repl_type
;
49 if (get_sha1(refname
, object
))
50 return error("Failed to resolve '%s' as a valid ref.", refname
);
52 obj_type
= sha1_object_info(object
, NULL
);
53 repl_type
= sha1_object_info(sha1
, NULL
);
55 printf("%s (%s) -> %s (%s)\n", refname
, typename(obj_type
),
56 sha1_to_hex(sha1
), typename(repl_type
));
63 static int list_replace_refs(const char *pattern
, const char *format
)
65 struct show_data data
;
69 data
.pattern
= pattern
;
71 if (format
== NULL
|| *format
== '\0' || !strcmp(format
, "short"))
72 data
.format
= REPLACE_FORMAT_SHORT
;
73 else if (!strcmp(format
, "medium"))
74 data
.format
= REPLACE_FORMAT_MEDIUM
;
75 else if (!strcmp(format
, "long"))
76 data
.format
= REPLACE_FORMAT_LONG
;
78 die("invalid replace format '%s'\n"
79 "valid formats are 'short', 'medium' and 'long'\n",
82 for_each_replace_ref(show_reference
, (void *) &data
);
87 typedef int (*each_replace_name_fn
)(const char *name
, const char *ref
,
88 const unsigned char *sha1
);
90 static int for_each_replace_name(const char **argv
, each_replace_name_fn fn
)
92 const char **p
, *full_hex
;
95 unsigned char sha1
[20];
97 for (p
= argv
; *p
; p
++) {
98 if (get_sha1(*p
, sha1
)) {
99 error("Failed to resolve '%s' as a valid ref.", *p
);
103 full_hex
= sha1_to_hex(sha1
);
104 snprintf(ref
, sizeof(ref
), "refs/replace/%s", full_hex
);
105 /* read_ref() may reuse the buffer */
106 full_hex
= ref
+ strlen("refs/replace/");
107 if (read_ref(ref
, sha1
)) {
108 error("replace ref '%s' not found.", full_hex
);
112 if (fn(full_hex
, ref
, sha1
))
118 static int delete_replace_ref(const char *name
, const char *ref
,
119 const unsigned char *sha1
)
121 if (delete_ref(ref
, sha1
, 0))
123 printf("Deleted replace ref '%s'\n", name
);
127 static int replace_object_sha1(const char *object_ref
,
128 unsigned char object
[20],
129 const char *replace_ref
,
130 unsigned char repl
[20],
133 unsigned char prev
[20];
134 enum object_type obj_type
, repl_type
;
136 struct ref_lock
*lock
;
138 if (snprintf(ref
, sizeof(ref
),
140 sha1_to_hex(object
)) > sizeof(ref
) - 1)
141 die("replace ref name too long: %.*s...", 50, ref
);
142 if (check_refname_format(ref
, 0))
143 die("'%s' is not a valid ref name.", ref
);
145 obj_type
= sha1_object_info(object
, NULL
);
146 repl_type
= sha1_object_info(repl
, NULL
);
147 if (!force
&& obj_type
!= repl_type
)
148 die("Objects must be of the same type.\n"
149 "'%s' points to a replaced object of type '%s'\n"
150 "while '%s' points to a replacement object of type '%s'.",
151 object_ref
, typename(obj_type
),
152 replace_ref
, typename(repl_type
));
154 if (read_ref(ref
, prev
))
157 die("replace ref '%s' already exists", ref
);
159 lock
= lock_any_ref_for_update(ref
, prev
, 0, NULL
);
161 die("%s: cannot lock the ref", ref
);
162 if (write_ref_sha1(lock
, repl
, NULL
) < 0)
163 die("%s: cannot update the ref", ref
);
168 static int replace_object(const char *object_ref
, const char *replace_ref
, int force
)
170 unsigned char object
[20], repl
[20];
172 if (get_sha1(object_ref
, object
))
173 die("Failed to resolve '%s' as a valid ref.", object_ref
);
174 if (get_sha1(replace_ref
, repl
))
175 die("Failed to resolve '%s' as a valid ref.", replace_ref
);
177 return replace_object_sha1(object_ref
, object
, replace_ref
, repl
, force
);
181 * Write the contents of the object named by "sha1" to the file "filename",
182 * pretty-printed for human editing based on its type.
184 static void export_object(const unsigned char *sha1
, const char *filename
)
186 const char *argv
[] = { "--no-replace-objects", "cat-file", "-p", NULL
, NULL
};
187 struct child_process cmd
= { argv
};
190 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
192 die_errno("unable to open %s for writing", filename
);
194 argv
[3] = sha1_to_hex(sha1
);
198 if (run_command(&cmd
))
199 die("cat-file reported failure");
205 * Read a previously-exported (and possibly edited) object back from "filename",
206 * interpreting it as "type", and writing the result to the object database.
207 * The sha1 of the written object is returned via sha1.
209 static void import_object(unsigned char *sha1
, enum object_type type
,
210 const char *filename
)
214 fd
= open(filename
, O_RDONLY
);
216 die_errno("unable to open %s for reading", filename
);
218 if (type
== OBJ_TREE
) {
219 const char *argv
[] = { "mktree", NULL
};
220 struct child_process cmd
= { argv
};
221 struct strbuf result
= STRBUF_INIT
;
228 if (start_command(&cmd
))
229 die("unable to spawn mktree");
231 if (strbuf_read(&result
, cmd
.out
, 41) < 0)
232 die_errno("unable to read from mktree");
235 if (finish_command(&cmd
))
236 die("mktree reported failure");
237 if (get_sha1_hex(result
.buf
, sha1
) < 0)
238 die("mktree did not return an object name");
240 strbuf_release(&result
);
243 int flags
= HASH_FORMAT_CHECK
| HASH_WRITE_OBJECT
;
245 if (fstat(fd
, &st
) < 0)
246 die_errno("unable to fstat %s", filename
);
247 if (index_fd(sha1
, fd
, &st
, type
, NULL
, flags
) < 0)
248 die("unable to write object to database");
249 /* index_fd close()s fd for us */
253 * No need to close(fd) here; both run-command and index-fd
254 * will have done it for us.
258 static int edit_and_replace(const char *object_ref
, int force
)
260 char *tmpfile
= git_pathdup("REPLACE_EDITOBJ");
261 enum object_type type
;
262 unsigned char old
[20], new[20];
264 if (get_sha1(object_ref
, old
) < 0)
265 die("Not a valid object name: '%s'", object_ref
);
267 type
= sha1_object_info(old
, NULL
);
269 die("unable to get object type for %s", sha1_to_hex(old
));
271 export_object(old
, tmpfile
);
272 if (launch_editor(tmpfile
, NULL
, NULL
) < 0)
273 die("editing object file failed");
274 import_object(new, type
, tmpfile
);
278 if (!hashcmp(old
, new))
279 return error("new object is the same as the old one: '%s'", sha1_to_hex(old
));
281 return replace_object_sha1(object_ref
, old
, "replacement", new, force
);
284 int cmd_replace(int argc
, const char **argv
, const char *prefix
)
287 const char *format
= NULL
;
289 MODE_UNSPECIFIED
= 0,
294 } cmdmode
= MODE_UNSPECIFIED
;
295 struct option options
[] = {
296 OPT_CMDMODE('l', "list", &cmdmode
, N_("list replace refs"), MODE_LIST
),
297 OPT_CMDMODE('d', "delete", &cmdmode
, N_("delete replace refs"), MODE_DELETE
),
298 OPT_CMDMODE('e', "edit", &cmdmode
, N_("edit existing object"), MODE_EDIT
),
299 OPT_BOOL('f', "force", &force
, N_("replace the ref if it exists")),
300 OPT_STRING(0, "format", &format
, N_("format"), N_("use this format")),
304 check_replace_refs
= 0;
306 argc
= parse_options(argc
, argv
, prefix
, options
, git_replace_usage
, 0);
309 cmdmode
= argc
? MODE_REPLACE
: MODE_LIST
;
311 if (format
&& cmdmode
!= MODE_LIST
)
312 usage_msg_opt("--format cannot be used when not listing",
313 git_replace_usage
, options
);
315 if (force
&& cmdmode
!= MODE_REPLACE
&& cmdmode
!= MODE_EDIT
)
316 usage_msg_opt("-f only makes sense when writing a replacement",
317 git_replace_usage
, options
);
322 usage_msg_opt("-d needs at least one argument",
323 git_replace_usage
, options
);
324 return for_each_replace_name(argv
, delete_replace_ref
);
328 usage_msg_opt("bad number of arguments",
329 git_replace_usage
, options
);
330 return replace_object(argv
[0], argv
[1], force
);
334 usage_msg_opt("-e needs exactly one argument",
335 git_replace_usage
, options
);
336 return edit_and_replace(argv
[0], force
);
340 usage_msg_opt("only one pattern can be given with -l",
341 git_replace_usage
, options
);
342 return list_replace_refs(argv
[0], format
);
345 die("BUG: invalid cmdmode %d", (int)cmdmode
);