replace: add --edit option
[git/git.git] / builtin / replace.c
CommitLineData
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"
54b0c1e0
CC
16
17static const char * const git_replace_usage[] = {
2477bebb
NTND
18 N_("git replace [-f] <object> <replacement>"),
19 N_("git replace -d <object>..."),
44f9f850 20 N_("git replace [--format=<format>] [-l [<pattern>]]"),
54b0c1e0
CC
21 NULL
22};
23
663a8566
CC
24enum replace_format {
25 REPLACE_FORMAT_SHORT,
26 REPLACE_FORMAT_MEDIUM,
27 REPLACE_FORMAT_LONG
28};
44f9f850
CC
29
30struct show_data {
31 const char *pattern;
663a8566 32 enum replace_format format;
44f9f850
CC
33};
34
54b0c1e0
CC
35static int show_reference(const char *refname, const unsigned char *sha1,
36 int flag, void *cb_data)
37{
44f9f850
CC
38 struct show_data *data = cb_data;
39
eb07894f 40 if (!wildmatch(data->pattern, refname, 0, NULL)) {
663a8566 41 if (data->format == REPLACE_FORMAT_SHORT)
44f9f850 42 printf("%s\n", refname);
663a8566 43 else if (data->format == REPLACE_FORMAT_MEDIUM)
44f9f850 44 printf("%s -> %s\n", refname, sha1_to_hex(sha1));
663a8566 45 else { /* data->format == REPLACE_FORMAT_LONG */
44f9f850
CC
46 unsigned char object[20];
47 enum object_type obj_type, repl_type;
54b0c1e0 48
44f9f850
CC
49 if (get_sha1(refname, object))
50 return error("Failed to resolve '%s' as a valid ref.", refname);
51
52 obj_type = sha1_object_info(object, NULL);
53 repl_type = sha1_object_info(sha1, NULL);
54
55 printf("%s (%s) -> %s (%s)\n", refname, typename(obj_type),
56 sha1_to_hex(sha1), typename(repl_type));
57 }
58 }
54b0c1e0
CC
59
60 return 0;
61}
62
44f9f850 63static int list_replace_refs(const char *pattern, const char *format)
54b0c1e0 64{
44f9f850
CC
65 struct show_data data;
66
54b0c1e0
CC
67 if (pattern == NULL)
68 pattern = "*";
44f9f850
CC
69 data.pattern = pattern;
70
71 if (format == NULL || *format == '\0' || !strcmp(format, "short"))
663a8566 72 data.format = REPLACE_FORMAT_SHORT;
44f9f850 73 else if (!strcmp(format, "medium"))
663a8566
CC
74 data.format = REPLACE_FORMAT_MEDIUM;
75 else if (!strcmp(format, "long"))
76 data.format = REPLACE_FORMAT_LONG;
44f9f850
CC
77 else
78 die("invalid replace format '%s'\n"
663a8566 79 "valid formats are 'short', 'medium' and 'long'\n",
44f9f850 80 format);
54b0c1e0 81
44f9f850 82 for_each_replace_ref(show_reference, (void *) &data);
54b0c1e0
CC
83
84 return 0;
85}
86
87typedef int (*each_replace_name_fn)(const char *name, const char *ref,
88 const unsigned char *sha1);
89
90static int for_each_replace_name(const char **argv, each_replace_name_fn fn)
91{
9dfc3684 92 const char **p, *full_hex;
54b0c1e0
CC
93 char ref[PATH_MAX];
94 int had_error = 0;
95 unsigned char sha1[20];
96
97 for (p = argv; *p; p++) {
9dfc3684
MG
98 if (get_sha1(*p, sha1)) {
99 error("Failed to resolve '%s' as a valid ref.", *p);
54b0c1e0
CC
100 had_error = 1;
101 continue;
102 }
9dfc3684
MG
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/");
c6893323 107 if (read_ref(ref, sha1)) {
9dfc3684 108 error("replace ref '%s' not found.", full_hex);
54b0c1e0
CC
109 had_error = 1;
110 continue;
111 }
9dfc3684 112 if (fn(full_hex, ref, sha1))
54b0c1e0
CC
113 had_error = 1;
114 }
115 return had_error;
116}
117
118static int delete_replace_ref(const char *name, const char *ref,
119 const unsigned char *sha1)
120{
121 if (delete_ref(ref, sha1, 0))
122 return 1;
123 printf("Deleted replace ref '%s'\n", name);
124 return 0;
125}
126
479bd757
JK
127static int replace_object_sha1(const char *object_ref,
128 unsigned char object[20],
129 const char *replace_ref,
130 unsigned char repl[20],
131 int force)
bebdd271 132{
479bd757 133 unsigned char prev[20];
277336a5 134 enum object_type obj_type, repl_type;
bebdd271
CC
135 char ref[PATH_MAX];
136 struct ref_lock *lock;
137
bebdd271
CC
138 if (snprintf(ref, sizeof(ref),
139 "refs/replace/%s",
140 sha1_to_hex(object)) > sizeof(ref) - 1)
141 die("replace ref name too long: %.*s...", 50, ref);
8d9c5010 142 if (check_refname_format(ref, 0))
bebdd271
CC
143 die("'%s' is not a valid ref name.", ref);
144
277336a5
CC
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));
153
c6893323 154 if (read_ref(ref, prev))
bebdd271
CC
155 hashclr(prev);
156 else if (!force)
157 die("replace ref '%s' already exists", ref);
158
9bbb0fa1 159 lock = lock_any_ref_for_update(ref, prev, 0, NULL);
bebdd271
CC
160 if (!lock)
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);
164
165 return 0;
166}
167
479bd757
JK
168static int replace_object(const char *object_ref, const char *replace_ref, int force)
169{
170 unsigned char object[20], repl[20];
171
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);
176
177 return replace_object_sha1(object_ref, object, replace_ref, repl, force);
178}
179
b892bb45
JK
180/*
181 * Write the contents of the object named by "sha1" to the file "filename",
182 * pretty-printed for human editing based on its type.
183 */
184static void export_object(const unsigned char *sha1, const char *filename)
185{
186 const char *argv[] = { "--no-replace-objects", "cat-file", "-p", NULL, NULL };
187 struct child_process cmd = { argv };
188 int fd;
189
190 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
191 if (fd < 0)
192 die_errno("unable to open %s for writing", filename);
193
194 argv[3] = sha1_to_hex(sha1);
195 cmd.git_cmd = 1;
196 cmd.out = fd;
197
198 if (run_command(&cmd))
199 die("cat-file reported failure");
200
201 close(fd);
202}
203
204/*
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.
208 */
209static void import_object(unsigned char *sha1, enum object_type type,
210 const char *filename)
211{
212 int fd;
213
214 fd = open(filename, O_RDONLY);
215 if (fd < 0)
216 die_errno("unable to open %s for reading", filename);
217
218 if (type == OBJ_TREE) {
219 const char *argv[] = { "mktree", NULL };
220 struct child_process cmd = { argv };
221 struct strbuf result = STRBUF_INIT;
222
223 cmd.argv = argv;
224 cmd.git_cmd = 1;
225 cmd.in = fd;
226 cmd.out = -1;
227
228 if (start_command(&cmd))
229 die("unable to spawn mktree");
230
231 if (strbuf_read(&result, cmd.out, 41) < 0)
232 die_errno("unable to read from mktree");
233 close(cmd.out);
234
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");
239
240 strbuf_release(&result);
241 } else {
242 struct stat st;
243 int flags = HASH_FORMAT_CHECK | HASH_WRITE_OBJECT;
244
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 */
250 }
251
252 /*
253 * No need to close(fd) here; both run-command and index-fd
254 * will have done it for us.
255 */
256}
257
258static int edit_and_replace(const char *object_ref, int force)
259{
260 char *tmpfile = git_pathdup("REPLACE_EDITOBJ");
261 enum object_type type;
262 unsigned char old[20], new[20];
263
264 if (get_sha1(object_ref, old) < 0)
265 die("Not a valid object name: '%s'", object_ref);
266
267 type = sha1_object_info(old, NULL);
268 if (type < 0)
269 die("unable to get object type for %s", sha1_to_hex(old));
270
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);
275
276 free(tmpfile);
277
278 return replace_object_sha1(object_ref, old, "replacement", new, force);
279}
280
54b0c1e0
CC
281int cmd_replace(int argc, const char **argv, const char *prefix)
282{
70c7bd6d 283 int force = 0;
44f9f850 284 const char *format = NULL;
70c7bd6d
JK
285 enum {
286 MODE_UNSPECIFIED = 0,
287 MODE_LIST,
288 MODE_DELETE,
b892bb45 289 MODE_EDIT,
70c7bd6d
JK
290 MODE_REPLACE
291 } cmdmode = MODE_UNSPECIFIED;
54b0c1e0 292 struct option options[] = {
70c7bd6d
JK
293 OPT_CMDMODE('l', "list", &cmdmode, N_("list replace refs"), MODE_LIST),
294 OPT_CMDMODE('d', "delete", &cmdmode, N_("delete replace refs"), MODE_DELETE),
b892bb45 295 OPT_CMDMODE('e', "edit", &cmdmode, N_("edit existing object"), MODE_EDIT),
80f165a5 296 OPT_BOOL('f', "force", &force, N_("replace the ref if it exists")),
44f9f850 297 OPT_STRING(0, "format", &format, N_("format"), N_("use this format")),
54b0c1e0
CC
298 OPT_END()
299 };
300
afc711b8 301 check_replace_refs = 0;
769a4fa4 302
451bb210 303 argc = parse_options(argc, argv, prefix, options, git_replace_usage, 0);
54b0c1e0 304
70c7bd6d
JK
305 if (!cmdmode)
306 cmdmode = argc ? MODE_REPLACE : MODE_LIST;
3f495f67 307
70c7bd6d 308 if (format && cmdmode != MODE_LIST)
3f495f67 309 usage_msg_opt("--format cannot be used when not listing",
44f9f850
CC
310 git_replace_usage, options);
311
b892bb45 312 if (force && cmdmode != MODE_REPLACE && cmdmode != MODE_EDIT)
70c7bd6d 313 usage_msg_opt("-f only makes sense when writing a replacement",
86af2caa 314 git_replace_usage, options);
bebdd271 315
70c7bd6d
JK
316 switch (cmdmode) {
317 case MODE_DELETE:
54b0c1e0 318 if (argc < 1)
86af2caa
CC
319 usage_msg_opt("-d needs at least one argument",
320 git_replace_usage, options);
54b0c1e0 321 return for_each_replace_name(argv, delete_replace_ref);
54b0c1e0 322
70c7bd6d 323 case MODE_REPLACE:
bebdd271 324 if (argc != 2)
86af2caa
CC
325 usage_msg_opt("bad number of arguments",
326 git_replace_usage, options);
bebdd271 327 return replace_object(argv[0], argv[1], force);
bebdd271 328
b892bb45
JK
329 case MODE_EDIT:
330 if (argc != 1)
331 usage_msg_opt("-e needs exactly one argument",
332 git_replace_usage, options);
333 return edit_and_replace(argv[0], force);
334
70c7bd6d
JK
335 case MODE_LIST:
336 if (argc > 1)
337 usage_msg_opt("only one pattern can be given with -l",
338 git_replace_usage, options);
339 return list_replace_refs(argv[0], format);
54b0c1e0 340
70c7bd6d
JK
341 default:
342 die("BUG: invalid cmdmode %d", (int)cmdmode);
343 }
54b0c1e0 344}