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 -d <object>..."),
21 N_("git replace [--format=<format>] [-l [<pattern>]]"),
27 REPLACE_FORMAT_MEDIUM
,
33 enum replace_format format
;
36 static int show_reference(const char *refname
, const unsigned char *sha1
,
37 int flag
, void *cb_data
)
39 struct show_data
*data
= cb_data
;
41 if (!wildmatch(data
->pattern
, refname
, 0, NULL
)) {
42 if (data
->format
== REPLACE_FORMAT_SHORT
)
43 printf("%s\n", refname
);
44 else if (data
->format
== REPLACE_FORMAT_MEDIUM
)
45 printf("%s -> %s\n", refname
, sha1_to_hex(sha1
));
46 else { /* data->format == REPLACE_FORMAT_LONG */
47 unsigned char object
[20];
48 enum object_type obj_type
, repl_type
;
50 if (get_sha1(refname
, object
))
51 return error("Failed to resolve '%s' as a valid ref.", refname
);
53 obj_type
= sha1_object_info(object
, NULL
);
54 repl_type
= sha1_object_info(sha1
, NULL
);
56 printf("%s (%s) -> %s (%s)\n", refname
, typename(obj_type
),
57 sha1_to_hex(sha1
), typename(repl_type
));
64 static int list_replace_refs(const char *pattern
, const char *format
)
66 struct show_data data
;
70 data
.pattern
= pattern
;
72 if (format
== NULL
|| *format
== '\0' || !strcmp(format
, "short"))
73 data
.format
= REPLACE_FORMAT_SHORT
;
74 else if (!strcmp(format
, "medium"))
75 data
.format
= REPLACE_FORMAT_MEDIUM
;
76 else if (!strcmp(format
, "long"))
77 data
.format
= REPLACE_FORMAT_LONG
;
79 die("invalid replace format '%s'\n"
80 "valid formats are 'short', 'medium' and 'long'\n",
83 for_each_replace_ref(show_reference
, (void *) &data
);
88 typedef int (*each_replace_name_fn
)(const char *name
, const char *ref
,
89 const unsigned char *sha1
);
91 static int for_each_replace_name(const char **argv
, each_replace_name_fn fn
)
93 const char **p
, *full_hex
;
96 unsigned char sha1
[20];
98 for (p
= argv
; *p
; p
++) {
99 if (get_sha1(*p
, sha1
)) {
100 error("Failed to resolve '%s' as a valid ref.", *p
);
104 full_hex
= sha1_to_hex(sha1
);
105 snprintf(ref
, sizeof(ref
), "refs/replace/%s", full_hex
);
106 /* read_ref() may reuse the buffer */
107 full_hex
= ref
+ strlen("refs/replace/");
108 if (read_ref(ref
, sha1
)) {
109 error("replace ref '%s' not found.", full_hex
);
113 if (fn(full_hex
, ref
, sha1
))
119 static int delete_replace_ref(const char *name
, const char *ref
,
120 const unsigned char *sha1
)
122 if (delete_ref(ref
, sha1
, 0))
124 printf("Deleted replace ref '%s'\n", name
);
128 static void check_ref_valid(unsigned char object
[20],
129 unsigned char prev
[20],
134 if (snprintf(ref
, ref_size
,
136 sha1_to_hex(object
)) > ref_size
- 1)
137 die("replace ref name too long: %.*s...", 50, ref
);
138 if (check_refname_format(ref
, 0))
139 die("'%s' is not a valid ref name.", ref
);
141 if (read_ref(ref
, prev
))
144 die("replace ref '%s' already exists", ref
);
147 static int replace_object_sha1(const char *object_ref
,
148 unsigned char object
[20],
149 const char *replace_ref
,
150 unsigned char repl
[20],
153 unsigned char prev
[20];
154 enum object_type obj_type
, repl_type
;
156 struct ref_lock
*lock
;
158 obj_type
= sha1_object_info(object
, NULL
);
159 repl_type
= sha1_object_info(repl
, NULL
);
160 if (!force
&& obj_type
!= repl_type
)
161 die("Objects must be of the same type.\n"
162 "'%s' points to a replaced object of type '%s'\n"
163 "while '%s' points to a replacement object of type '%s'.",
164 object_ref
, typename(obj_type
),
165 replace_ref
, typename(repl_type
));
167 check_ref_valid(object
, prev
, ref
, sizeof(ref
), force
);
169 lock
= lock_any_ref_for_update(ref
, prev
, 0, NULL
);
171 die("%s: cannot lock the ref", ref
);
172 if (write_ref_sha1(lock
, repl
, NULL
) < 0)
173 die("%s: cannot update the ref", ref
);
178 static int replace_object(const char *object_ref
, const char *replace_ref
, int force
)
180 unsigned char object
[20], repl
[20];
182 if (get_sha1(object_ref
, object
))
183 die("Failed to resolve '%s' as a valid ref.", object_ref
);
184 if (get_sha1(replace_ref
, repl
))
185 die("Failed to resolve '%s' as a valid ref.", replace_ref
);
187 return replace_object_sha1(object_ref
, object
, replace_ref
, repl
, force
);
191 * Write the contents of the object named by "sha1" to the file "filename".
192 * If "raw" is true, then the object's raw contents are printed according to
193 * "type". Otherwise, we pretty-print the contents for human editing.
195 static void export_object(const unsigned char *sha1
, enum object_type type
,
196 int raw
, const char *filename
)
198 struct child_process cmd
= { NULL
};
201 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
203 die_errno("unable to open %s for writing", filename
);
205 argv_array_push(&cmd
.args
, "--no-replace-objects");
206 argv_array_push(&cmd
.args
, "cat-file");
208 argv_array_push(&cmd
.args
, typename(type
));
210 argv_array_push(&cmd
.args
, "-p");
211 argv_array_push(&cmd
.args
, sha1_to_hex(sha1
));
215 if (run_command(&cmd
))
216 die("cat-file reported failure");
220 * Read a previously-exported (and possibly edited) object back from "filename",
221 * interpreting it as "type", and writing the result to the object database.
222 * The sha1 of the written object is returned via sha1.
224 static void import_object(unsigned char *sha1
, enum object_type type
,
225 int raw
, const char *filename
)
229 fd
= open(filename
, O_RDONLY
);
231 die_errno("unable to open %s for reading", filename
);
233 if (!raw
&& type
== OBJ_TREE
) {
234 const char *argv
[] = { "mktree", NULL
};
235 struct child_process cmd
= { argv
};
236 struct strbuf result
= STRBUF_INIT
;
243 if (start_command(&cmd
))
244 die("unable to spawn mktree");
246 if (strbuf_read(&result
, cmd
.out
, 41) < 0)
247 die_errno("unable to read from mktree");
250 if (finish_command(&cmd
))
251 die("mktree reported failure");
252 if (get_sha1_hex(result
.buf
, sha1
) < 0)
253 die("mktree did not return an object name");
255 strbuf_release(&result
);
258 int flags
= HASH_FORMAT_CHECK
| HASH_WRITE_OBJECT
;
260 if (fstat(fd
, &st
) < 0)
261 die_errno("unable to fstat %s", filename
);
262 if (index_fd(sha1
, fd
, &st
, type
, NULL
, flags
) < 0)
263 die("unable to write object to database");
264 /* index_fd close()s fd for us */
268 * No need to close(fd) here; both run-command and index-fd
269 * will have done it for us.
273 static int edit_and_replace(const char *object_ref
, int force
, int raw
)
275 char *tmpfile
= git_pathdup("REPLACE_EDITOBJ");
276 enum object_type type
;
277 unsigned char old
[20], new[20], prev
[20];
280 if (get_sha1(object_ref
, old
) < 0)
281 die("Not a valid object name: '%s'", object_ref
);
283 type
= sha1_object_info(old
, NULL
);
285 die("unable to get object type for %s", sha1_to_hex(old
));
287 check_ref_valid(old
, prev
, ref
, sizeof(ref
), force
);
289 export_object(old
, type
, raw
, tmpfile
);
290 if (launch_editor(tmpfile
, NULL
, NULL
) < 0)
291 die("editing object file failed");
292 import_object(new, type
, raw
, tmpfile
);
296 if (!hashcmp(old
, new))
297 return error("new object is the same as the old one: '%s'", sha1_to_hex(old
));
299 return replace_object_sha1(object_ref
, old
, "replacement", new, force
);
302 int cmd_replace(int argc
, const char **argv
, const char *prefix
)
306 const char *format
= NULL
;
308 MODE_UNSPECIFIED
= 0,
313 } cmdmode
= MODE_UNSPECIFIED
;
314 struct option options
[] = {
315 OPT_CMDMODE('l', "list", &cmdmode
, N_("list replace refs"), MODE_LIST
),
316 OPT_CMDMODE('d', "delete", &cmdmode
, N_("delete replace refs"), MODE_DELETE
),
317 OPT_CMDMODE('e', "edit", &cmdmode
, N_("edit existing object"), MODE_EDIT
),
318 OPT_BOOL('f', "force", &force
, N_("replace the ref if it exists")),
319 OPT_BOOL(0, "raw", &raw
, N_("do not pretty-print contents for --edit")),
320 OPT_STRING(0, "format", &format
, N_("format"), N_("use this format")),
324 check_replace_refs
= 0;
326 argc
= parse_options(argc
, argv
, prefix
, options
, git_replace_usage
, 0);
329 cmdmode
= argc
? MODE_REPLACE
: MODE_LIST
;
331 if (format
&& cmdmode
!= MODE_LIST
)
332 usage_msg_opt("--format cannot be used when not listing",
333 git_replace_usage
, options
);
335 if (force
&& cmdmode
!= MODE_REPLACE
&& cmdmode
!= MODE_EDIT
)
336 usage_msg_opt("-f only makes sense when writing a replacement",
337 git_replace_usage
, options
);
339 if (raw
&& cmdmode
!= MODE_EDIT
)
340 usage_msg_opt("--raw only makes sense with --edit",
341 git_replace_usage
, options
);
346 usage_msg_opt("-d needs at least one argument",
347 git_replace_usage
, options
);
348 return for_each_replace_name(argv
, delete_replace_ref
);
352 usage_msg_opt("bad number of arguments",
353 git_replace_usage
, options
);
354 return replace_object(argv
[0], argv
[1], force
);
358 usage_msg_opt("-e needs exactly one argument",
359 git_replace_usage
, options
);
360 return edit_and_replace(argv
[0], force
, raw
);
364 usage_msg_opt("only one pattern can be given with -l",
365 git_replace_usage
, options
);
366 return list_replace_refs(argv
[0], format
);
369 die("BUG: invalid cmdmode %d", (int)cmdmode
);