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