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