replace: factor object resolution out of replace_object
[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"
15
16static const char * const git_replace_usage[] = {
2477bebb
NTND
17 N_("git replace [-f] <object> <replacement>"),
18 N_("git replace -d <object>..."),
44f9f850 19 N_("git replace [--format=<format>] [-l [<pattern>]]"),
54b0c1e0
CC
20 NULL
21};
22
663a8566
CC
23enum replace_format {
24 REPLACE_FORMAT_SHORT,
25 REPLACE_FORMAT_MEDIUM,
26 REPLACE_FORMAT_LONG
27};
44f9f850
CC
28
29struct show_data {
30 const char *pattern;
663a8566 31 enum replace_format format;
44f9f850
CC
32};
33
54b0c1e0
CC
34static int show_reference(const char *refname, const unsigned char *sha1,
35 int flag, void *cb_data)
36{
44f9f850
CC
37 struct show_data *data = cb_data;
38
eb07894f 39 if (!wildmatch(data->pattern, refname, 0, NULL)) {
663a8566 40 if (data->format == REPLACE_FORMAT_SHORT)
44f9f850 41 printf("%s\n", refname);
663a8566 42 else if (data->format == REPLACE_FORMAT_MEDIUM)
44f9f850 43 printf("%s -> %s\n", refname, sha1_to_hex(sha1));
663a8566 44 else { /* data->format == REPLACE_FORMAT_LONG */
44f9f850
CC
45 unsigned char object[20];
46 enum object_type obj_type, repl_type;
54b0c1e0 47
44f9f850
CC
48 if (get_sha1(refname, object))
49 return error("Failed to resolve '%s' as a valid ref.", refname);
50
51 obj_type = sha1_object_info(object, NULL);
52 repl_type = sha1_object_info(sha1, NULL);
53
54 printf("%s (%s) -> %s (%s)\n", refname, typename(obj_type),
55 sha1_to_hex(sha1), typename(repl_type));
56 }
57 }
54b0c1e0
CC
58
59 return 0;
60}
61
44f9f850 62static int list_replace_refs(const char *pattern, const char *format)
54b0c1e0 63{
44f9f850
CC
64 struct show_data data;
65
54b0c1e0
CC
66 if (pattern == NULL)
67 pattern = "*";
44f9f850
CC
68 data.pattern = pattern;
69
70 if (format == NULL || *format == '\0' || !strcmp(format, "short"))
663a8566 71 data.format = REPLACE_FORMAT_SHORT;
44f9f850 72 else if (!strcmp(format, "medium"))
663a8566
CC
73 data.format = REPLACE_FORMAT_MEDIUM;
74 else if (!strcmp(format, "long"))
75 data.format = REPLACE_FORMAT_LONG;
44f9f850
CC
76 else
77 die("invalid replace format '%s'\n"
663a8566 78 "valid formats are 'short', 'medium' and 'long'\n",
44f9f850 79 format);
54b0c1e0 80
44f9f850 81 for_each_replace_ref(show_reference, (void *) &data);
54b0c1e0
CC
82
83 return 0;
84}
85
86typedef int (*each_replace_name_fn)(const char *name, const char *ref,
87 const unsigned char *sha1);
88
89static int for_each_replace_name(const char **argv, each_replace_name_fn fn)
90{
9dfc3684 91 const char **p, *full_hex;
54b0c1e0
CC
92 char ref[PATH_MAX];
93 int had_error = 0;
94 unsigned char sha1[20];
95
96 for (p = argv; *p; p++) {
9dfc3684
MG
97 if (get_sha1(*p, sha1)) {
98 error("Failed to resolve '%s' as a valid ref.", *p);
54b0c1e0
CC
99 had_error = 1;
100 continue;
101 }
9dfc3684
MG
102 full_hex = sha1_to_hex(sha1);
103 snprintf(ref, sizeof(ref), "refs/replace/%s", full_hex);
104 /* read_ref() may reuse the buffer */
105 full_hex = ref + strlen("refs/replace/");
c6893323 106 if (read_ref(ref, sha1)) {
9dfc3684 107 error("replace ref '%s' not found.", full_hex);
54b0c1e0
CC
108 had_error = 1;
109 continue;
110 }
9dfc3684 111 if (fn(full_hex, ref, sha1))
54b0c1e0
CC
112 had_error = 1;
113 }
114 return had_error;
115}
116
117static int delete_replace_ref(const char *name, const char *ref,
118 const unsigned char *sha1)
119{
120 if (delete_ref(ref, sha1, 0))
121 return 1;
122 printf("Deleted replace ref '%s'\n", name);
123 return 0;
124}
125
479bd757
JK
126static int replace_object_sha1(const char *object_ref,
127 unsigned char object[20],
128 const char *replace_ref,
129 unsigned char repl[20],
130 int force)
bebdd271 131{
479bd757 132 unsigned char prev[20];
277336a5 133 enum object_type obj_type, repl_type;
bebdd271
CC
134 char ref[PATH_MAX];
135 struct ref_lock *lock;
136
bebdd271
CC
137 if (snprintf(ref, sizeof(ref),
138 "refs/replace/%s",
139 sha1_to_hex(object)) > sizeof(ref) - 1)
140 die("replace ref name too long: %.*s...", 50, ref);
8d9c5010 141 if (check_refname_format(ref, 0))
bebdd271
CC
142 die("'%s' is not a valid ref name.", ref);
143
277336a5
CC
144 obj_type = sha1_object_info(object, NULL);
145 repl_type = sha1_object_info(repl, NULL);
146 if (!force && obj_type != repl_type)
147 die("Objects must be of the same type.\n"
148 "'%s' points to a replaced object of type '%s'\n"
149 "while '%s' points to a replacement object of type '%s'.",
150 object_ref, typename(obj_type),
151 replace_ref, typename(repl_type));
152
c6893323 153 if (read_ref(ref, prev))
bebdd271
CC
154 hashclr(prev);
155 else if (!force)
156 die("replace ref '%s' already exists", ref);
157
9bbb0fa1 158 lock = lock_any_ref_for_update(ref, prev, 0, NULL);
bebdd271
CC
159 if (!lock)
160 die("%s: cannot lock the ref", ref);
161 if (write_ref_sha1(lock, repl, NULL) < 0)
162 die("%s: cannot update the ref", ref);
163
164 return 0;
165}
166
479bd757
JK
167static int replace_object(const char *object_ref, const char *replace_ref, int force)
168{
169 unsigned char object[20], repl[20];
170
171 if (get_sha1(object_ref, object))
172 die("Failed to resolve '%s' as a valid ref.", object_ref);
173 if (get_sha1(replace_ref, repl))
174 die("Failed to resolve '%s' as a valid ref.", replace_ref);
175
176 return replace_object_sha1(object_ref, object, replace_ref, repl, force);
177}
178
54b0c1e0
CC
179int cmd_replace(int argc, const char **argv, const char *prefix)
180{
70c7bd6d 181 int force = 0;
44f9f850 182 const char *format = NULL;
70c7bd6d
JK
183 enum {
184 MODE_UNSPECIFIED = 0,
185 MODE_LIST,
186 MODE_DELETE,
187 MODE_REPLACE
188 } cmdmode = MODE_UNSPECIFIED;
54b0c1e0 189 struct option options[] = {
70c7bd6d
JK
190 OPT_CMDMODE('l', "list", &cmdmode, N_("list replace refs"), MODE_LIST),
191 OPT_CMDMODE('d', "delete", &cmdmode, N_("delete replace refs"), MODE_DELETE),
80f165a5 192 OPT_BOOL('f', "force", &force, N_("replace the ref if it exists")),
44f9f850 193 OPT_STRING(0, "format", &format, N_("format"), N_("use this format")),
54b0c1e0
CC
194 OPT_END()
195 };
196
afc711b8 197 check_replace_refs = 0;
769a4fa4 198
451bb210 199 argc = parse_options(argc, argv, prefix, options, git_replace_usage, 0);
54b0c1e0 200
70c7bd6d
JK
201 if (!cmdmode)
202 cmdmode = argc ? MODE_REPLACE : MODE_LIST;
3f495f67 203
70c7bd6d 204 if (format && cmdmode != MODE_LIST)
3f495f67 205 usage_msg_opt("--format cannot be used when not listing",
44f9f850
CC
206 git_replace_usage, options);
207
70c7bd6d
JK
208 if (force && cmdmode != MODE_REPLACE)
209 usage_msg_opt("-f only makes sense when writing a replacement",
86af2caa 210 git_replace_usage, options);
bebdd271 211
70c7bd6d
JK
212 switch (cmdmode) {
213 case MODE_DELETE:
54b0c1e0 214 if (argc < 1)
86af2caa
CC
215 usage_msg_opt("-d needs at least one argument",
216 git_replace_usage, options);
54b0c1e0 217 return for_each_replace_name(argv, delete_replace_ref);
54b0c1e0 218
70c7bd6d 219 case MODE_REPLACE:
bebdd271 220 if (argc != 2)
86af2caa
CC
221 usage_msg_opt("bad number of arguments",
222 git_replace_usage, options);
bebdd271 223 return replace_object(argv[0], argv[1], force);
bebdd271 224
70c7bd6d
JK
225 case MODE_LIST:
226 if (argc > 1)
227 usage_msg_opt("only one pattern can be given with -l",
228 git_replace_usage, options);
229 return list_replace_refs(argv[0], format);
54b0c1e0 230
70c7bd6d
JK
231 default:
232 die("BUG: invalid cmdmode %d", (int)cmdmode);
233 }
54b0c1e0 234}