update-ref --stdin: make error messages more consistent
[git/debian.git] / builtin / update-ref.c
blobe4c0854e9fc58b6fa6b96e3216d417de3245c02e
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"
8 static 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
15 static int updates_alloc;
16 static int updates_count;
17 static struct ref_update **updates;
19 static char line_termination = '\n';
20 static int update_flags;
22 static struct ref_update *update_alloc(void)
24 struct ref_update *update;
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;
31 /* Store and reset accumulated options */
32 update->flags = update_flags;
33 update_flags = 0;
35 return update;
38 static void update_store_new_sha1(const char *command,
39 struct ref_update *update,
40 const char *newvalue)
42 if (*newvalue && get_sha1(newvalue, update->new_sha1))
43 die("%s %s: invalid <newvalue>: %s",
44 command, update->ref_name, newvalue);
47 static void update_store_old_sha1(const char *command,
48 struct ref_update *update,
49 const char *oldvalue)
51 if (*oldvalue && get_sha1(oldvalue, update->old_sha1))
52 die("%s %s: invalid <oldvalue>: %s",
53 command, update->ref_name, oldvalue);
55 /* We have an old value if non-empty, or if empty without -z */
56 update->have_old = *oldvalue || line_termination;
60 * Parse one whitespace- or NUL-terminated, possibly C-quoted argument
61 * and append the result to arg. Return a pointer to the terminator.
62 * Die if there is an error in how the argument is C-quoted. This
63 * function is only used if not -z.
65 static const char *parse_arg(const char *next, struct strbuf *arg)
67 if (*next == '"') {
68 const char *orig = next;
70 if (unquote_c_style(arg, next, &next))
71 die("badly quoted argument: %s", orig);
72 if (*next && !isspace(*next))
73 die("unexpected character after quoted argument: %s", orig);
74 } else {
75 while (*next && !isspace(*next))
76 strbuf_addch(arg, *next++);
79 return next;
83 * Parse the reference name immediately after "command SP". If not
84 * -z, then handle C-quoting. Return a pointer to a newly allocated
85 * string containing the name of the reference, or NULL if there was
86 * an error. Update *next to point at the character that terminates
87 * the argument. Die if C-quoting is malformed or the reference name
88 * is invalid.
90 static char *parse_refname(struct strbuf *input, const char **next)
92 struct strbuf ref = STRBUF_INIT;
94 if (line_termination) {
95 /* Without -z, use the next argument */
96 *next = parse_arg(*next, &ref);
97 } else {
98 /* With -z, use everything up to the next NUL */
99 strbuf_addstr(&ref, *next);
100 *next += ref.len;
103 if (!ref.len) {
104 strbuf_release(&ref);
105 return NULL;
108 if (check_refname_format(ref.buf, REFNAME_ALLOW_ONELEVEL))
109 die("invalid ref format: %s", ref.buf);
111 return strbuf_detach(&ref, NULL);
115 * Parse a SP/NUL separator followed by the next SP- or NUL-terminated
116 * argument, if any. If there is an argument, write it to arg, set
117 * *next to point at the character terminating the argument, and
118 * return 0. If there is no argument at all (not even the empty
119 * string), return a non-zero result and leave *next unchanged.
121 static int parse_next_arg(struct strbuf *input, const char **next,
122 struct strbuf *arg)
124 strbuf_reset(arg);
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("expected SP but got: %s", *next);
131 (*next)++;
132 *next = parse_arg(*next, arg);
133 } else {
134 /* With -z, read the next NUL-terminated line */
135 if (**next)
136 die("expected NUL but got: %s", *next);
137 (*next)++;
138 if (*next == input->buf + input->len)
139 return -1;
140 strbuf_addstr(arg, *next);
141 *next += arg->len;
143 return 0;
148 * The following five parse_cmd_*() functions parse the corresponding
149 * command. In each case, next points at the character following the
150 * command name and the following space. They each return a pointer
151 * to the character terminating the command, and die with an
152 * explanatory message if there are any parsing problems. All of
153 * these functions handle either text or binary format input,
154 * depending on how line_termination is set.
157 static const char *parse_cmd_update(struct strbuf *input, const char *next)
159 struct strbuf newvalue = STRBUF_INIT;
160 struct strbuf oldvalue = STRBUF_INIT;
161 struct ref_update *update;
163 update = update_alloc();
165 update->ref_name = parse_refname(input, &next);
166 if (!update->ref_name)
167 die("update line missing <ref>");
169 if (!parse_next_arg(input, &next, &newvalue))
170 update_store_new_sha1("update", update, newvalue.buf);
171 else
172 die("update %s missing <newvalue>", update->ref_name);
174 if (!parse_next_arg(input, &next, &oldvalue)) {
175 update_store_old_sha1("update", update, oldvalue.buf);
176 if (*next != line_termination)
177 die("update %s has extra input: %s", update->ref_name, next);
178 } else if (!line_termination)
179 die("update %s missing [<oldvalue>] NUL", update->ref_name);
181 return next;
184 static const char *parse_cmd_create(struct strbuf *input, const char *next)
186 struct strbuf newvalue = STRBUF_INIT;
187 struct ref_update *update;
189 update = update_alloc();
191 update->ref_name = parse_refname(input, &next);
192 if (!update->ref_name)
193 die("create line missing <ref>");
195 if (!parse_next_arg(input, &next, &newvalue))
196 update_store_new_sha1("create", update, newvalue.buf);
197 else
198 die("create %s missing <newvalue>", update->ref_name);
200 if (is_null_sha1(update->new_sha1))
201 die("create %s given zero <newvalue>", update->ref_name);
203 if (*next != line_termination)
204 die("create %s has extra input: %s", update->ref_name, next);
206 return next;
209 static const char *parse_cmd_delete(struct strbuf *input, const char *next)
211 struct strbuf oldvalue = STRBUF_INIT;
212 struct ref_update *update;
214 update = update_alloc();
216 update->ref_name = parse_refname(input, &next);
217 if (!update->ref_name)
218 die("delete line missing <ref>");
220 if (!parse_next_arg(input, &next, &oldvalue)) {
221 update_store_old_sha1("delete", update, oldvalue.buf);
222 if (update->have_old && is_null_sha1(update->old_sha1))
223 die("delete %s given zero <oldvalue>", update->ref_name);
224 } else if (!line_termination)
225 die("delete %s missing [<oldvalue>] NUL", update->ref_name);
227 if (*next != line_termination)
228 die("delete %s has extra input: %s", update->ref_name, next);
230 return next;
233 static const char *parse_cmd_verify(struct strbuf *input, const char *next)
235 struct strbuf value = STRBUF_INIT;
236 struct ref_update *update;
238 update = update_alloc();
240 update->ref_name = parse_refname(input, &next);
241 if (!update->ref_name)
242 die("verify line missing <ref>");
244 if (!parse_next_arg(input, &next, &value)) {
245 update_store_old_sha1("verify", update, value.buf);
246 hashcpy(update->new_sha1, update->old_sha1);
247 } else if (!line_termination)
248 die("verify %s missing [<oldvalue>] NUL", update->ref_name);
250 if (*next != line_termination)
251 die("verify %s has extra input: %s", update->ref_name, next);
253 return next;
256 static const char *parse_cmd_option(struct strbuf *input, const char *next)
258 if (!strncmp(next, "no-deref", 8) && next[8] == line_termination)
259 update_flags |= REF_NODEREF;
260 else
261 die("option unknown: %s", next);
262 return next + 8;
265 static void update_refs_stdin(void)
267 struct strbuf input = STRBUF_INIT;
268 const char *next;
270 if (strbuf_read(&input, 0, 1000) < 0)
271 die_errno("could not read from stdin");
272 next = input.buf;
273 /* Read each line dispatch its command */
274 while (next < input.buf + input.len) {
275 if (*next == line_termination)
276 die("empty command in input");
277 else if (isspace(*next))
278 die("whitespace before command: %s", next);
279 else if (starts_with(next, "update "))
280 next = parse_cmd_update(&input, next + 7);
281 else if (starts_with(next, "create "))
282 next = parse_cmd_create(&input, next + 7);
283 else if (starts_with(next, "delete "))
284 next = parse_cmd_delete(&input, next + 7);
285 else if (starts_with(next, "verify "))
286 next = parse_cmd_verify(&input, next + 7);
287 else if (starts_with(next, "option "))
288 next = parse_cmd_option(&input, next + 7);
289 else
290 die("unknown command: %s", next);
292 next++;
295 strbuf_release(&input);
298 int cmd_update_ref(int argc, const char **argv, const char *prefix)
300 const char *refname, *oldval, *msg = NULL;
301 unsigned char sha1[20], oldsha1[20];
302 int delete = 0, no_deref = 0, read_stdin = 0, end_null = 0, flags = 0;
303 struct option options[] = {
304 OPT_STRING( 'm', NULL, &msg, N_("reason"), N_("reason of the update")),
305 OPT_BOOL('d', NULL, &delete, N_("delete the reference")),
306 OPT_BOOL( 0 , "no-deref", &no_deref,
307 N_("update <refname> not the one it points to")),
308 OPT_BOOL('z', NULL, &end_null, N_("stdin has NUL-terminated arguments")),
309 OPT_BOOL( 0 , "stdin", &read_stdin, N_("read updates from stdin")),
310 OPT_END(),
313 git_config(git_default_config, NULL);
314 argc = parse_options(argc, argv, prefix, options, git_update_ref_usage,
316 if (msg && !*msg)
317 die("Refusing to perform update with empty message.");
319 if (read_stdin) {
320 if (delete || no_deref || argc > 0)
321 usage_with_options(git_update_ref_usage, options);
322 if (end_null)
323 line_termination = '\0';
324 update_refs_stdin();
325 return update_refs(msg, updates, updates_count,
326 UPDATE_REFS_DIE_ON_ERR);
329 if (end_null)
330 usage_with_options(git_update_ref_usage, options);
332 if (delete) {
333 if (argc < 1 || argc > 2)
334 usage_with_options(git_update_ref_usage, options);
335 refname = argv[0];
336 oldval = argv[1];
337 } else {
338 const char *value;
339 if (argc < 2 || argc > 3)
340 usage_with_options(git_update_ref_usage, options);
341 refname = argv[0];
342 value = argv[1];
343 oldval = argv[2];
344 if (get_sha1(value, sha1))
345 die("%s: not a valid SHA1", value);
348 hashclr(oldsha1); /* all-zero hash in case oldval is the empty string */
349 if (oldval && *oldval && get_sha1(oldval, oldsha1))
350 die("%s: not a valid old SHA1", oldval);
352 if (no_deref)
353 flags = REF_NODEREF;
354 if (delete)
355 return delete_ref(refname, oldval ? oldsha1 : NULL, flags);
356 else
357 return update_ref(msg, refname, sha1, oldval ? oldsha1 : NULL,
358 flags, UPDATE_REFS_DIE_ON_ERR);