]>
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 | ||
1746ef4e MH |
38 | static void update_store_new_sha1(const char *command, |
39 | struct ref_update *update, | |
c750ba95 BK |
40 | const char *newvalue) |
41 | { | |
42 | if (*newvalue && get_sha1(newvalue, update->new_sha1)) | |
9255f059 | 43 | die("%s %s: invalid <newvalue>: %s", |
1746ef4e | 44 | command, update->ref_name, newvalue); |
c750ba95 BK |
45 | } |
46 | ||
1746ef4e MH |
47 | static void update_store_old_sha1(const char *command, |
48 | struct ref_update *update, | |
c750ba95 BK |
49 | const char *oldvalue) |
50 | { | |
51 | if (*oldvalue && get_sha1(oldvalue, update->old_sha1)) | |
9255f059 | 52 | die("%s %s: invalid <oldvalue>: %s", |
1746ef4e | 53 | command, update->ref_name, oldvalue); |
c750ba95 BK |
54 | |
55 | /* We have an old value if non-empty, or if empty without -z */ | |
56 | update->have_old = *oldvalue || line_termination; | |
57 | } | |
58 | ||
697a4151 MH |
59 | /* |
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. | |
64 | */ | |
c750ba95 BK |
65 | static const char *parse_arg(const char *next, struct strbuf *arg) |
66 | { | |
697a4151 MH |
67 | if (*next == '"') { |
68 | const char *orig = next; | |
69 | ||
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 { | |
c750ba95 BK |
75 | while (*next && !isspace(*next)) |
76 | strbuf_addch(arg, *next++); | |
697a4151 | 77 | } |
c750ba95 | 78 | |
c750ba95 BK |
79 | return next; |
80 | } | |
81 | ||
e23d8435 | 82 | /* |
ed410e61 MH |
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. | |
e23d8435 | 89 | */ |
ed410e61 | 90 | static char *parse_refname(struct strbuf *input, const char **next) |
c750ba95 | 91 | { |
ed410e61 MH |
92 | struct strbuf ref = STRBUF_INIT; |
93 | ||
c750ba95 BK |
94 | if (line_termination) { |
95 | /* Without -z, use the next argument */ | |
ed410e61 | 96 | *next = parse_arg(*next, &ref); |
c750ba95 | 97 | } else { |
e23d8435 | 98 | /* With -z, use everything up to the next NUL */ |
ed410e61 MH |
99 | strbuf_addstr(&ref, *next); |
100 | *next += ref.len; | |
101 | } | |
102 | ||
103 | if (!ref.len) { | |
104 | strbuf_release(&ref); | |
105 | return NULL; | |
c750ba95 | 106 | } |
ed410e61 MH |
107 | |
108 | if (check_refname_format(ref.buf, REFNAME_ALLOW_ONELEVEL)) | |
109 | die("invalid ref format: %s", ref.buf); | |
110 | ||
111 | return strbuf_detach(&ref, NULL); | |
c750ba95 BK |
112 | } |
113 | ||
e23d8435 MH |
114 | /* |
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. | |
120 | */ | |
121 | static int parse_next_arg(struct strbuf *input, const char **next, | |
122 | struct strbuf *arg) | |
c750ba95 | 123 | { |
c750ba95 BK |
124 | strbuf_reset(arg); |
125 | if (line_termination) { | |
126 | /* Without -z, consume SP and use next argument */ | |
e23d8435 MH |
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); | |
c750ba95 BK |
133 | } else { |
134 | /* With -z, read the next NUL-terminated line */ | |
e23d8435 MH |
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; | |
c750ba95 | 142 | } |
e23d8435 | 143 | return 0; |
c750ba95 BK |
144 | } |
145 | ||
e23d8435 MH |
146 | |
147 | /* | |
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. | |
155 | */ | |
156 | ||
157 | static const char *parse_cmd_update(struct strbuf *input, const char *next) | |
c750ba95 | 158 | { |
c750ba95 BK |
159 | struct strbuf newvalue = STRBUF_INIT; |
160 | struct strbuf oldvalue = STRBUF_INIT; | |
161 | struct ref_update *update; | |
162 | ||
163 | update = update_alloc(); | |
164 | ||
ed410e61 MH |
165 | update->ref_name = parse_refname(input, &next); |
166 | if (!update->ref_name) | |
c750ba95 BK |
167 | die("update line missing <ref>"); |
168 | ||
e23d8435 | 169 | if (!parse_next_arg(input, &next, &newvalue)) |
1746ef4e | 170 | update_store_new_sha1("update", update, newvalue.buf); |
c750ba95 | 171 | else |
ed410e61 | 172 | die("update %s missing <newvalue>", update->ref_name); |
c750ba95 | 173 | |
e23d8435 | 174 | if (!parse_next_arg(input, &next, &oldvalue)) { |
1746ef4e | 175 | update_store_old_sha1("update", update, oldvalue.buf); |
e23d8435 | 176 | if (*next != line_termination) |
ed410e61 | 177 | die("update %s has extra input: %s", update->ref_name, next); |
e23d8435 | 178 | } else if (!line_termination) |
ac117755 | 179 | die("update %s missing <oldvalue>", update->ref_name); |
c750ba95 | 180 | |
e23d8435 | 181 | return next; |
c750ba95 BK |
182 | } |
183 | ||
e23d8435 | 184 | static const char *parse_cmd_create(struct strbuf *input, const char *next) |
c750ba95 | 185 | { |
c750ba95 BK |
186 | struct strbuf newvalue = STRBUF_INIT; |
187 | struct ref_update *update; | |
188 | ||
189 | update = update_alloc(); | |
190 | ||
ed410e61 MH |
191 | update->ref_name = parse_refname(input, &next); |
192 | if (!update->ref_name) | |
c750ba95 BK |
193 | die("create line missing <ref>"); |
194 | ||
e23d8435 | 195 | if (!parse_next_arg(input, &next, &newvalue)) |
1746ef4e | 196 | update_store_new_sha1("create", update, newvalue.buf); |
c750ba95 | 197 | else |
ed410e61 | 198 | die("create %s missing <newvalue>", update->ref_name); |
e23d8435 | 199 | |
c750ba95 | 200 | if (is_null_sha1(update->new_sha1)) |
9255f059 | 201 | die("create %s given zero <newvalue>", update->ref_name); |
c750ba95 | 202 | |
e23d8435 | 203 | if (*next != line_termination) |
ed410e61 | 204 | die("create %s has extra input: %s", update->ref_name, next); |
e23d8435 MH |
205 | |
206 | return next; | |
c750ba95 BK |
207 | } |
208 | ||
e23d8435 | 209 | static const char *parse_cmd_delete(struct strbuf *input, const char *next) |
c750ba95 | 210 | { |
c750ba95 BK |
211 | struct strbuf oldvalue = STRBUF_INIT; |
212 | struct ref_update *update; | |
213 | ||
214 | update = update_alloc(); | |
215 | ||
ed410e61 MH |
216 | update->ref_name = parse_refname(input, &next); |
217 | if (!update->ref_name) | |
c750ba95 BK |
218 | die("delete line missing <ref>"); |
219 | ||
e23d8435 | 220 | if (!parse_next_arg(input, &next, &oldvalue)) { |
1746ef4e | 221 | update_store_old_sha1("delete", update, oldvalue.buf); |
e23d8435 | 222 | if (update->have_old && is_null_sha1(update->old_sha1)) |
9255f059 | 223 | die("delete %s given zero <oldvalue>", update->ref_name); |
e23d8435 | 224 | } else if (!line_termination) |
ac117755 | 225 | die("delete %s missing <oldvalue>", update->ref_name); |
c750ba95 | 226 | |
e23d8435 | 227 | if (*next != line_termination) |
ed410e61 | 228 | die("delete %s has extra input: %s", update->ref_name, next); |
e23d8435 MH |
229 | |
230 | return next; | |
c750ba95 BK |
231 | } |
232 | ||
e23d8435 | 233 | static const char *parse_cmd_verify(struct strbuf *input, const char *next) |
c750ba95 | 234 | { |
c750ba95 BK |
235 | struct strbuf value = STRBUF_INIT; |
236 | struct ref_update *update; | |
237 | ||
238 | update = update_alloc(); | |
239 | ||
ed410e61 MH |
240 | update->ref_name = parse_refname(input, &next); |
241 | if (!update->ref_name) | |
c750ba95 BK |
242 | die("verify line missing <ref>"); |
243 | ||
e23d8435 | 244 | if (!parse_next_arg(input, &next, &value)) { |
1746ef4e | 245 | update_store_old_sha1("verify", update, value.buf); |
2f577360 | 246 | hashcpy(update->new_sha1, update->old_sha1); |
e23d8435 | 247 | } else if (!line_termination) |
ac117755 | 248 | die("verify %s missing <oldvalue>", update->ref_name); |
c750ba95 | 249 | |
e23d8435 | 250 | if (*next != line_termination) |
ed410e61 | 251 | die("verify %s has extra input: %s", update->ref_name, next); |
e23d8435 MH |
252 | |
253 | return next; | |
c750ba95 BK |
254 | } |
255 | ||
e23d8435 | 256 | static const char *parse_cmd_option(struct strbuf *input, const char *next) |
c750ba95 | 257 | { |
e23d8435 | 258 | if (!strncmp(next, "no-deref", 8) && next[8] == line_termination) |
c750ba95 BK |
259 | update_flags |= REF_NODEREF; |
260 | else | |
261 | die("option unknown: %s", next); | |
e23d8435 | 262 | return next + 8; |
c750ba95 BK |
263 | } |
264 | ||
265 | static void update_refs_stdin(void) | |
266 | { | |
e23d8435 MH |
267 | struct strbuf input = STRBUF_INIT; |
268 | const char *next; | |
c750ba95 | 269 | |
e23d8435 MH |
270 | if (strbuf_read(&input, 0, 1000) < 0) |
271 | die_errno("could not read from stdin"); | |
272 | next = input.buf; | |
c750ba95 | 273 | /* Read each line dispatch its command */ |
e23d8435 MH |
274 | while (next < input.buf + input.len) { |
275 | if (*next == line_termination) | |
c750ba95 | 276 | die("empty command in input"); |
e23d8435 MH |
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); | |
c750ba95 | 289 | else |
e23d8435 MH |
290 | die("unknown command: %s", next); |
291 | ||
292 | next++; | |
293 | } | |
c750ba95 | 294 | |
e23d8435 | 295 | strbuf_release(&input); |
c750ba95 BK |
296 | } |
297 | ||
a633fca0 | 298 | int cmd_update_ref(int argc, const char **argv, const char *prefix) |
66bf85a4 | 299 | { |
f877fd82 | 300 | const char *refname, *oldval, *msg = NULL; |
5b16b090 | 301 | unsigned char sha1[20], oldsha1[20]; |
c750ba95 | 302 | int delete = 0, no_deref = 0, read_stdin = 0, end_null = 0, flags = 0; |
89942be6 | 303 | struct option options[] = { |
82269505 | 304 | OPT_STRING( 'm', NULL, &msg, N_("reason"), N_("reason of the update")), |
d5d09d47 SB |
305 | OPT_BOOL('d', NULL, &delete, N_("delete the reference")), |
306 | OPT_BOOL( 0 , "no-deref", &no_deref, | |
82269505 | 307 | N_("update <refname> not the one it points to")), |
9a86b899 JH |
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")), | |
89942be6 PH |
310 | OPT_END(), |
311 | }; | |
66bf85a4 | 312 | |
ef90d6d4 | 313 | git_config(git_default_config, NULL); |
37782920 SB |
314 | argc = parse_options(argc, argv, prefix, options, git_update_ref_usage, |
315 | 0); | |
89942be6 PH |
316 | if (msg && !*msg) |
317 | die("Refusing to perform update with empty message."); | |
5b16b090 | 318 | |
c750ba95 BK |
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(); | |
f4124112 MH |
325 | return update_refs(msg, updates, updates_count, |
326 | UPDATE_REFS_DIE_ON_ERR); | |
c750ba95 BK |
327 | } |
328 | ||
329 | if (end_null) | |
330 | usage_with_options(git_update_ref_usage, options); | |
331 | ||
ac5409e4 | 332 | if (delete) { |
3fe8dce6 | 333 | if (argc < 1 || argc > 2) |
973a70ea KW |
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) | |
89942be6 | 340 | usage_with_options(git_update_ref_usage, options); |
973a70ea KW |
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); | |
ac5409e4 JH |
346 | } |
347 | ||
973a70ea | 348 | hashclr(oldsha1); /* all-zero hash in case oldval is the empty string */ |
ac5409e4 | 349 | if (oldval && *oldval && get_sha1(oldval, oldsha1)) |
66bf85a4 LT |
350 | die("%s: not a valid old SHA1", oldval); |
351 | ||
569740bd MV |
352 | if (no_deref) |
353 | flags = REF_NODEREF; | |
973a70ea | 354 | if (delete) |
569740bd | 355 | return delete_ref(refname, oldval ? oldsha1 : NULL, flags); |
973a70ea KW |
356 | else |
357 | return update_ref(msg, refname, sha1, oldval ? oldsha1 : NULL, | |
f4124112 | 358 | flags, UPDATE_REFS_DIE_ON_ERR); |
66bf85a4 | 359 | } |