]>
Commit | Line | Data |
---|---|---|
66bf85a4 LT |
1 | #include "cache.h" |
2 | #include "refs.h" | |
854b4629 | 3 | #include "builtin.h" |
89942be6 | 4 | #include "parse-options.h" |
c750ba95 BK |
5 | #include "quote.h" |
6 | #include "argv-array.h" | |
66bf85a4 | 7 | |
89942be6 | 8 | static const char * const git_update_ref_usage[] = { |
82269505 NTND |
9 | N_("git update-ref [options] -d <refname> [<oldval>]"), |
10 | N_("git update-ref [options] <refname> <newval> [<oldval>]"), | |
c750ba95 | 11 | N_("git update-ref [options] --stdin [-z]"), |
89942be6 PH |
12 | NULL |
13 | }; | |
152da3df | 14 | |
c750ba95 BK |
15 | static int updates_alloc; |
16 | static int updates_count; | |
595deb8d | 17 | static struct ref_update **updates; |
c750ba95 BK |
18 | |
19 | static char line_termination = '\n'; | |
20 | static int update_flags; | |
21 | ||
22 | static struct ref_update *update_alloc(void) | |
23 | { | |
24 | struct ref_update *update; | |
25 | ||
26 | /* Allocate and zero-init a struct ref_update */ | |
27 | update = xcalloc(1, sizeof(*update)); | |
28 | ALLOC_GROW(updates, updates_count + 1, updates_alloc); | |
29 | updates[updates_count++] = update; | |
30 | ||
31 | /* Store and reset accumulated options */ | |
32 | update->flags = update_flags; | |
33 | update_flags = 0; | |
34 | ||
35 | return update; | |
36 | } | |
37 | ||
697a4151 MH |
38 | /* |
39 | * Parse one whitespace- or NUL-terminated, possibly C-quoted argument | |
40 | * and append the result to arg. Return a pointer to the terminator. | |
41 | * Die if there is an error in how the argument is C-quoted. This | |
42 | * function is only used if not -z. | |
43 | */ | |
c750ba95 BK |
44 | static const char *parse_arg(const char *next, struct strbuf *arg) |
45 | { | |
697a4151 MH |
46 | if (*next == '"') { |
47 | const char *orig = next; | |
48 | ||
49 | if (unquote_c_style(arg, next, &next)) | |
50 | die("badly quoted argument: %s", orig); | |
51 | if (*next && !isspace(*next)) | |
52 | die("unexpected character after quoted argument: %s", orig); | |
53 | } else { | |
c750ba95 BK |
54 | while (*next && !isspace(*next)) |
55 | strbuf_addch(arg, *next++); | |
697a4151 | 56 | } |
c750ba95 | 57 | |
c750ba95 BK |
58 | return next; |
59 | } | |
60 | ||
e23d8435 | 61 | /* |
ed410e61 MH |
62 | * Parse the reference name immediately after "command SP". If not |
63 | * -z, then handle C-quoting. Return a pointer to a newly allocated | |
64 | * string containing the name of the reference, or NULL if there was | |
65 | * an error. Update *next to point at the character that terminates | |
66 | * the argument. Die if C-quoting is malformed or the reference name | |
67 | * is invalid. | |
e23d8435 | 68 | */ |
ed410e61 | 69 | static char *parse_refname(struct strbuf *input, const char **next) |
c750ba95 | 70 | { |
ed410e61 MH |
71 | struct strbuf ref = STRBUF_INIT; |
72 | ||
c750ba95 BK |
73 | if (line_termination) { |
74 | /* Without -z, use the next argument */ | |
ed410e61 | 75 | *next = parse_arg(*next, &ref); |
c750ba95 | 76 | } else { |
e23d8435 | 77 | /* With -z, use everything up to the next NUL */ |
ed410e61 MH |
78 | strbuf_addstr(&ref, *next); |
79 | *next += ref.len; | |
80 | } | |
81 | ||
82 | if (!ref.len) { | |
83 | strbuf_release(&ref); | |
84 | return NULL; | |
c750ba95 | 85 | } |
ed410e61 MH |
86 | |
87 | if (check_refname_format(ref.buf, REFNAME_ALLOW_ONELEVEL)) | |
88 | die("invalid ref format: %s", ref.buf); | |
89 | ||
90 | return strbuf_detach(&ref, NULL); | |
c750ba95 BK |
91 | } |
92 | ||
e23d8435 | 93 | /* |
3afcc463 MH |
94 | * The value being parsed is <oldvalue> (as opposed to <newvalue>; the |
95 | * difference affects which error messages are generated): | |
96 | */ | |
97 | #define PARSE_SHA1_OLD 0x01 | |
98 | ||
99 | /* | |
100 | * For backwards compatibility, accept an empty string for update's | |
101 | * <newvalue> in binary mode to be equivalent to specifying zeros. | |
102 | */ | |
103 | #define PARSE_SHA1_ALLOW_EMPTY 0x02 | |
104 | ||
105 | /* | |
106 | * Parse an argument separator followed by the next argument, if any. | |
107 | * If there is an argument, convert it to a SHA-1, write it to sha1, | |
108 | * set *next to point at the character terminating the argument, and | |
e23d8435 | 109 | * return 0. If there is no argument at all (not even the empty |
3afcc463 MH |
110 | * string), return 1 and leave *next unchanged. If the value is |
111 | * provided but cannot be converted to a SHA-1, die. flags can | |
112 | * include PARSE_SHA1_OLD and/or PARSE_SHA1_ALLOW_EMPTY. | |
e23d8435 | 113 | */ |
3afcc463 MH |
114 | static int parse_next_sha1(struct strbuf *input, const char **next, |
115 | unsigned char *sha1, | |
116 | const char *command, const char *refname, | |
117 | int flags) | |
c750ba95 | 118 | { |
3afcc463 MH |
119 | struct strbuf arg = STRBUF_INIT; |
120 | int ret = 0; | |
121 | ||
122 | if (*next == input->buf + input->len) | |
123 | goto eof; | |
124 | ||
c750ba95 BK |
125 | if (line_termination) { |
126 | /* Without -z, consume SP and use next argument */ | |
e23d8435 | 127 | if (!**next || **next == line_termination) |
3afcc463 | 128 | return 1; |
e23d8435 | 129 | if (**next != ' ') |
3afcc463 MH |
130 | die("%s %s: expected SP but got: %s", |
131 | command, refname, *next); | |
e23d8435 | 132 | (*next)++; |
3afcc463 MH |
133 | *next = parse_arg(*next, &arg); |
134 | if (arg.len) { | |
135 | if (get_sha1(arg.buf, sha1)) | |
136 | goto invalid; | |
137 | } else { | |
138 | /* Without -z, an empty value means all zeros: */ | |
139 | hashclr(sha1); | |
140 | } | |
c750ba95 BK |
141 | } else { |
142 | /* With -z, read the next NUL-terminated line */ | |
e23d8435 | 143 | if (**next) |
3afcc463 MH |
144 | die("%s %s: expected NUL but got: %s", |
145 | command, refname, *next); | |
e23d8435 MH |
146 | (*next)++; |
147 | if (*next == input->buf + input->len) | |
3afcc463 MH |
148 | goto eof; |
149 | strbuf_addstr(&arg, *next); | |
150 | *next += arg.len; | |
151 | ||
152 | if (arg.len) { | |
153 | if (get_sha1(arg.buf, sha1)) | |
154 | goto invalid; | |
155 | } else if (flags & PARSE_SHA1_ALLOW_EMPTY) { | |
156 | /* With -z, treat an empty value as all zeros: */ | |
157 | hashclr(sha1); | |
158 | } else { | |
159 | /* | |
160 | * With -z, an empty non-required value means | |
161 | * unspecified: | |
162 | */ | |
163 | ret = 1; | |
164 | } | |
c750ba95 | 165 | } |
3afcc463 MH |
166 | |
167 | strbuf_release(&arg); | |
168 | ||
169 | return ret; | |
170 | ||
171 | invalid: | |
172 | die(flags & PARSE_SHA1_OLD ? | |
173 | "%s %s: invalid <oldvalue>: %s" : | |
174 | "%s %s: invalid <newvalue>: %s", | |
175 | command, refname, arg.buf); | |
176 | ||
177 | eof: | |
178 | die(flags & PARSE_SHA1_OLD ? | |
179 | "%s %s missing <oldvalue>" : | |
180 | "%s %s missing <newvalue>", | |
181 | command, refname); | |
c750ba95 BK |
182 | } |
183 | ||
e23d8435 MH |
184 | |
185 | /* | |
186 | * The following five parse_cmd_*() functions parse the corresponding | |
187 | * command. In each case, next points at the character following the | |
188 | * command name and the following space. They each return a pointer | |
189 | * to the character terminating the command, and die with an | |
190 | * explanatory message if there are any parsing problems. All of | |
191 | * these functions handle either text or binary format input, | |
192 | * depending on how line_termination is set. | |
193 | */ | |
194 | ||
195 | static const char *parse_cmd_update(struct strbuf *input, const char *next) | |
c750ba95 | 196 | { |
c750ba95 BK |
197 | struct ref_update *update; |
198 | ||
199 | update = update_alloc(); | |
200 | ||
ed410e61 MH |
201 | update->ref_name = parse_refname(input, &next); |
202 | if (!update->ref_name) | |
c750ba95 BK |
203 | die("update line missing <ref>"); |
204 | ||
3afcc463 MH |
205 | if (parse_next_sha1(input, &next, update->new_sha1, |
206 | "update", update->ref_name, | |
207 | PARSE_SHA1_ALLOW_EMPTY)) | |
ed410e61 | 208 | die("update %s missing <newvalue>", update->ref_name); |
c750ba95 | 209 | |
3afcc463 MH |
210 | update->have_old = !parse_next_sha1(input, &next, update->old_sha1, |
211 | "update", update->ref_name, | |
212 | PARSE_SHA1_OLD); | |
213 | ||
214 | if (*next != line_termination) | |
215 | die("update %s has extra input: %s", update->ref_name, next); | |
c750ba95 | 216 | |
e23d8435 | 217 | return next; |
c750ba95 BK |
218 | } |
219 | ||
e23d8435 | 220 | static const char *parse_cmd_create(struct strbuf *input, const char *next) |
c750ba95 | 221 | { |
c750ba95 BK |
222 | struct ref_update *update; |
223 | ||
224 | update = update_alloc(); | |
225 | ||
ed410e61 MH |
226 | update->ref_name = parse_refname(input, &next); |
227 | if (!update->ref_name) | |
c750ba95 BK |
228 | die("create line missing <ref>"); |
229 | ||
3afcc463 MH |
230 | if (parse_next_sha1(input, &next, update->new_sha1, |
231 | "create", update->ref_name, 0)) | |
ed410e61 | 232 | die("create %s missing <newvalue>", update->ref_name); |
e23d8435 | 233 | |
c750ba95 | 234 | if (is_null_sha1(update->new_sha1)) |
9255f059 | 235 | die("create %s given zero <newvalue>", update->ref_name); |
c750ba95 | 236 | |
e23d8435 | 237 | if (*next != line_termination) |
ed410e61 | 238 | die("create %s has extra input: %s", update->ref_name, next); |
e23d8435 MH |
239 | |
240 | return next; | |
c750ba95 BK |
241 | } |
242 | ||
e23d8435 | 243 | static const char *parse_cmd_delete(struct strbuf *input, const char *next) |
c750ba95 | 244 | { |
c750ba95 BK |
245 | struct ref_update *update; |
246 | ||
247 | update = update_alloc(); | |
248 | ||
ed410e61 MH |
249 | update->ref_name = parse_refname(input, &next); |
250 | if (!update->ref_name) | |
c750ba95 BK |
251 | die("delete line missing <ref>"); |
252 | ||
3afcc463 MH |
253 | if (parse_next_sha1(input, &next, update->old_sha1, |
254 | "delete", update->ref_name, PARSE_SHA1_OLD)) { | |
255 | update->have_old = 0; | |
256 | } else { | |
257 | if (is_null_sha1(update->old_sha1)) | |
9255f059 | 258 | die("delete %s given zero <oldvalue>", update->ref_name); |
3afcc463 MH |
259 | update->have_old = 1; |
260 | } | |
c750ba95 | 261 | |
e23d8435 | 262 | if (*next != line_termination) |
ed410e61 | 263 | die("delete %s has extra input: %s", update->ref_name, next); |
e23d8435 MH |
264 | |
265 | return next; | |
c750ba95 BK |
266 | } |
267 | ||
e23d8435 | 268 | static const char *parse_cmd_verify(struct strbuf *input, const char *next) |
c750ba95 | 269 | { |
c750ba95 BK |
270 | struct ref_update *update; |
271 | ||
272 | update = update_alloc(); | |
273 | ||
ed410e61 MH |
274 | update->ref_name = parse_refname(input, &next); |
275 | if (!update->ref_name) | |
c750ba95 BK |
276 | die("verify line missing <ref>"); |
277 | ||
3afcc463 MH |
278 | if (parse_next_sha1(input, &next, update->old_sha1, |
279 | "verify", update->ref_name, PARSE_SHA1_OLD)) { | |
280 | update->have_old = 0; | |
281 | } else { | |
2f577360 | 282 | hashcpy(update->new_sha1, update->old_sha1); |
3afcc463 MH |
283 | update->have_old = 1; |
284 | } | |
c750ba95 | 285 | |
e23d8435 | 286 | if (*next != line_termination) |
ed410e61 | 287 | die("verify %s has extra input: %s", update->ref_name, next); |
e23d8435 MH |
288 | |
289 | return next; | |
c750ba95 BK |
290 | } |
291 | ||
e23d8435 | 292 | static const char *parse_cmd_option(struct strbuf *input, const char *next) |
c750ba95 | 293 | { |
e23d8435 | 294 | if (!strncmp(next, "no-deref", 8) && next[8] == line_termination) |
c750ba95 BK |
295 | update_flags |= REF_NODEREF; |
296 | else | |
297 | die("option unknown: %s", next); | |
e23d8435 | 298 | return next + 8; |
c750ba95 BK |
299 | } |
300 | ||
301 | static void update_refs_stdin(void) | |
302 | { | |
e23d8435 MH |
303 | struct strbuf input = STRBUF_INIT; |
304 | const char *next; | |
c750ba95 | 305 | |
e23d8435 MH |
306 | if (strbuf_read(&input, 0, 1000) < 0) |
307 | die_errno("could not read from stdin"); | |
308 | next = input.buf; | |
c750ba95 | 309 | /* Read each line dispatch its command */ |
e23d8435 MH |
310 | while (next < input.buf + input.len) { |
311 | if (*next == line_termination) | |
c750ba95 | 312 | die("empty command in input"); |
e23d8435 MH |
313 | else if (isspace(*next)) |
314 | die("whitespace before command: %s", next); | |
315 | else if (starts_with(next, "update ")) | |
316 | next = parse_cmd_update(&input, next + 7); | |
317 | else if (starts_with(next, "create ")) | |
318 | next = parse_cmd_create(&input, next + 7); | |
319 | else if (starts_with(next, "delete ")) | |
320 | next = parse_cmd_delete(&input, next + 7); | |
321 | else if (starts_with(next, "verify ")) | |
322 | next = parse_cmd_verify(&input, next + 7); | |
323 | else if (starts_with(next, "option ")) | |
324 | next = parse_cmd_option(&input, next + 7); | |
c750ba95 | 325 | else |
e23d8435 MH |
326 | die("unknown command: %s", next); |
327 | ||
328 | next++; | |
329 | } | |
c750ba95 | 330 | |
e23d8435 | 331 | strbuf_release(&input); |
c750ba95 BK |
332 | } |
333 | ||
a633fca0 | 334 | int cmd_update_ref(int argc, const char **argv, const char *prefix) |
66bf85a4 | 335 | { |
f877fd82 | 336 | const char *refname, *oldval, *msg = NULL; |
5b16b090 | 337 | unsigned char sha1[20], oldsha1[20]; |
c750ba95 | 338 | int delete = 0, no_deref = 0, read_stdin = 0, end_null = 0, flags = 0; |
89942be6 | 339 | struct option options[] = { |
82269505 | 340 | OPT_STRING( 'm', NULL, &msg, N_("reason"), N_("reason of the update")), |
d5d09d47 SB |
341 | OPT_BOOL('d', NULL, &delete, N_("delete the reference")), |
342 | OPT_BOOL( 0 , "no-deref", &no_deref, | |
82269505 | 343 | N_("update <refname> not the one it points to")), |
9a86b899 JH |
344 | OPT_BOOL('z', NULL, &end_null, N_("stdin has NUL-terminated arguments")), |
345 | OPT_BOOL( 0 , "stdin", &read_stdin, N_("read updates from stdin")), | |
89942be6 PH |
346 | OPT_END(), |
347 | }; | |
66bf85a4 | 348 | |
ef90d6d4 | 349 | git_config(git_default_config, NULL); |
37782920 SB |
350 | argc = parse_options(argc, argv, prefix, options, git_update_ref_usage, |
351 | 0); | |
89942be6 PH |
352 | if (msg && !*msg) |
353 | die("Refusing to perform update with empty message."); | |
5b16b090 | 354 | |
c750ba95 BK |
355 | if (read_stdin) { |
356 | if (delete || no_deref || argc > 0) | |
357 | usage_with_options(git_update_ref_usage, options); | |
358 | if (end_null) | |
359 | line_termination = '\0'; | |
360 | update_refs_stdin(); | |
f4124112 MH |
361 | return update_refs(msg, updates, updates_count, |
362 | UPDATE_REFS_DIE_ON_ERR); | |
c750ba95 BK |
363 | } |
364 | ||
365 | if (end_null) | |
366 | usage_with_options(git_update_ref_usage, options); | |
367 | ||
ac5409e4 | 368 | if (delete) { |
3fe8dce6 | 369 | if (argc < 1 || argc > 2) |
973a70ea KW |
370 | usage_with_options(git_update_ref_usage, options); |
371 | refname = argv[0]; | |
372 | oldval = argv[1]; | |
373 | } else { | |
374 | const char *value; | |
375 | if (argc < 2 || argc > 3) | |
89942be6 | 376 | usage_with_options(git_update_ref_usage, options); |
973a70ea KW |
377 | refname = argv[0]; |
378 | value = argv[1]; | |
379 | oldval = argv[2]; | |
380 | if (get_sha1(value, sha1)) | |
381 | die("%s: not a valid SHA1", value); | |
ac5409e4 JH |
382 | } |
383 | ||
973a70ea | 384 | hashclr(oldsha1); /* all-zero hash in case oldval is the empty string */ |
ac5409e4 | 385 | if (oldval && *oldval && get_sha1(oldval, oldsha1)) |
66bf85a4 LT |
386 | die("%s: not a valid old SHA1", oldval); |
387 | ||
569740bd MV |
388 | if (no_deref) |
389 | flags = REF_NODEREF; | |
973a70ea | 390 | if (delete) |
569740bd | 391 | return delete_ref(refname, oldval ? oldsha1 : NULL, flags); |
973a70ea KW |
392 | else |
393 | return update_ref(msg, refname, sha1, oldval ? oldsha1 : NULL, | |
f4124112 | 394 | flags, UPDATE_REFS_DIE_ON_ERR); |
66bf85a4 | 395 | } |