c61120faf7680d5d10abd78fd0d055e90cd6ad8e
1#include "cache.h"
2#include "refs.h"
3#include "builtin.h"
4#include "parse-options.h"
5#include "quote.h"
6#include "argv-array.h"
7
8static const char * const git_update_ref_usage[] = {
9 N_("git update-ref [options] -d <refname> [<oldval>]"),
10 N_("git update-ref [options] <refname> <newval> [<oldval>]"),
11 N_("git update-ref [options] --stdin [-z]"),
12 NULL
13};
14
15static int updates_alloc;
16static int updates_count;
17static struct ref_update **updates;
18
19static char line_termination = '\n';
20static int update_flags;
21
22static 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
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 */
44static const char *parse_arg(const char *next, struct strbuf *arg)
45{
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 {
54 while (*next && !isspace(*next))
55 strbuf_addch(arg, *next++);
56 }
57
58 return next;
59}
60
61/*
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.
68 */
69static char *parse_refname(struct strbuf *input, const char **next)
70{
71 struct strbuf ref = STRBUF_INIT;
72
73 if (line_termination) {
74 /* Without -z, use the next argument */
75 *next = parse_arg(*next, &ref);
76 } else {
77 /* With -z, use everything up to the next NUL */
78 strbuf_addstr(&ref, *next);
79 *next += ref.len;
80 }
81
82 if (!ref.len) {
83 strbuf_release(&ref);
84 return NULL;
85 }
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);
91}
92
93/*
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
109 * return 0. If there is no argument at all (not even the empty
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.
113 */
114static int parse_next_sha1(struct strbuf *input, const char **next,
115 unsigned char *sha1,
116 const char *command, const char *refname,
117 int flags)
118{
119 struct strbuf arg = STRBUF_INIT;
120 int ret = 0;
121
122 if (*next == input->buf + input->len)
123 goto eof;
124
125 if (line_termination) {
126 /* Without -z, consume SP and use next argument */
127 if (!**next || **next == line_termination)
128 return 1;
129 if (**next != ' ')
130 die("%s %s: expected SP but got: %s",
131 command, refname, *next);
132 (*next)++;
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 }
141 } else {
142 /* With -z, read the next NUL-terminated line */
143 if (**next)
144 die("%s %s: expected NUL but got: %s",
145 command, refname, *next);
146 (*next)++;
147 if (*next == input->buf + input->len)
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 }
165 }
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);
182}
183
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
195static const char *parse_cmd_update(struct strbuf *input, const char *next)
196{
197 struct ref_update *update;
198
199 update = update_alloc();
200
201 update->ref_name = parse_refname(input, &next);
202 if (!update->ref_name)
203 die("update line missing <ref>");
204
205 if (parse_next_sha1(input, &next, update->new_sha1,
206 "update", update->ref_name,
207 PARSE_SHA1_ALLOW_EMPTY))
208 die("update %s missing <newvalue>", update->ref_name);
209
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);
216
217 return next;
218}
219
220static const char *parse_cmd_create(struct strbuf *input, const char *next)
221{
222 struct ref_update *update;
223
224 update = update_alloc();
225
226 update->ref_name = parse_refname(input, &next);
227 if (!update->ref_name)
228 die("create line missing <ref>");
229
230 if (parse_next_sha1(input, &next, update->new_sha1,
231 "create", update->ref_name, 0))
232 die("create %s missing <newvalue>", update->ref_name);
233
234 if (is_null_sha1(update->new_sha1))
235 die("create %s given zero <newvalue>", update->ref_name);
236
237 if (*next != line_termination)
238 die("create %s has extra input: %s", update->ref_name, next);
239
240 return next;
241}
242
243static const char *parse_cmd_delete(struct strbuf *input, const char *next)
244{
245 struct ref_update *update;
246
247 update = update_alloc();
248
249 update->ref_name = parse_refname(input, &next);
250 if (!update->ref_name)
251 die("delete line missing <ref>");
252
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))
258 die("delete %s given zero <oldvalue>", update->ref_name);
259 update->have_old = 1;
260 }
261
262 if (*next != line_termination)
263 die("delete %s has extra input: %s", update->ref_name, next);
264
265 return next;
266}
267
268static const char *parse_cmd_verify(struct strbuf *input, const char *next)
269{
270 struct ref_update *update;
271
272 update = update_alloc();
273
274 update->ref_name = parse_refname(input, &next);
275 if (!update->ref_name)
276 die("verify line missing <ref>");
277
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 {
282 hashcpy(update->new_sha1, update->old_sha1);
283 update->have_old = 1;
284 }
285
286 if (*next != line_termination)
287 die("verify %s has extra input: %s", update->ref_name, next);
288
289 return next;
290}
291
292static const char *parse_cmd_option(struct strbuf *input, const char *next)
293{
294 if (!strncmp(next, "no-deref", 8) && next[8] == line_termination)
295 update_flags |= REF_NODEREF;
296 else
297 die("option unknown: %s", next);
298 return next + 8;
299}
300
301static void update_refs_stdin(void)
302{
303 struct strbuf input = STRBUF_INIT;
304 const char *next;
305
306 if (strbuf_read(&input, 0, 1000) < 0)
307 die_errno("could not read from stdin");
308 next = input.buf;
309 /* Read each line dispatch its command */
310 while (next < input.buf + input.len) {
311 if (*next == line_termination)
312 die("empty command in input");
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);
325 else
326 die("unknown command: %s", next);
327
328 next++;
329 }
330
331 strbuf_release(&input);
332}
333
334int cmd_update_ref(int argc, const char **argv, const char *prefix)
335{
336 const char *refname, *oldval, *msg = NULL;
337 unsigned char sha1[20], oldsha1[20];
338 int delete = 0, no_deref = 0, read_stdin = 0, end_null = 0, flags = 0;
339 struct option options[] = {
340 OPT_STRING( 'm', NULL, &msg, N_("reason"), N_("reason of the update")),
341 OPT_BOOL('d', NULL, &delete, N_("delete the reference")),
342 OPT_BOOL( 0 , "no-deref", &no_deref,
343 N_("update <refname> not the one it points to")),
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")),
346 OPT_END(),
347 };
348
349 git_config(git_default_config, NULL);
350 argc = parse_options(argc, argv, prefix, options, git_update_ref_usage,
351 0);
352 if (msg && !*msg)
353 die("Refusing to perform update with empty message.");
354
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();
361 return update_refs(msg, updates, updates_count,
362 UPDATE_REFS_DIE_ON_ERR);
363 }
364
365 if (end_null)
366 usage_with_options(git_update_ref_usage, options);
367
368 if (delete) {
369 if (argc < 1 || argc > 2)
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)
376 usage_with_options(git_update_ref_usage, options);
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);
382 }
383
384 hashclr(oldsha1); /* all-zero hash in case oldval is the empty string */
385 if (oldval && *oldval && get_sha1(oldval, oldsha1))
386 die("%s: not a valid old SHA1", oldval);
387
388 if (no_deref)
389 flags = REF_NODEREF;
390 if (delete)
391 return delete_ref(refname, oldval ? oldsha1 : NULL, flags);
392 else
393 return update_ref(msg, refname, sha1, oldval ? oldsha1 : NULL,
394 flags, UPDATE_REFS_DIE_ON_ERR);
395}