]>
Commit | Line | Data |
---|---|---|
03eae9af | 1 | #define USE_THE_REPOSITORY_VARIABLE |
41f43b82 PS |
2 | #define DISABLE_SIGN_COMPARE_WARNINGS |
3 | ||
bc5c5ec0 | 4 | #include "builtin.h" |
b2141fc1 | 5 | #include "config.h" |
08b77586 | 6 | #include "environment.h" |
f394e093 | 7 | #include "gettext.h" |
d1cbe1e6 | 8 | #include "hash.h" |
221e8fcb | 9 | #include "hex.h" |
66bf85a4 | 10 | #include "refs.h" |
dabab1d6 | 11 | #include "object-name.h" |
89942be6 | 12 | #include "parse-options.h" |
c750ba95 | 13 | #include "quote.h" |
66bf85a4 | 14 | |
89942be6 | 15 | static const char * const git_update_ref_usage[] = { |
67e943c3 KN |
16 | N_("git update-ref [<options>] -d <refname> [<old-oid>]"), |
17 | N_("git update-ref [<options>] <refname> <new-oid> [<old-oid>]"), | |
221e8fcb | 18 | N_("git update-ref [<options>] --stdin [-z] [--batch-updates]"), |
89942be6 PH |
19 | NULL |
20 | }; | |
152da3df | 21 | |
c750ba95 | 22 | static char line_termination = '\n'; |
e4c34855 | 23 | static unsigned int update_flags; |
d345e9fb | 24 | static unsigned int default_flags; |
144c76fa | 25 | static unsigned create_reflog_flag; |
db7516ab | 26 | static const char *msg; |
c750ba95 | 27 | |
697a4151 MH |
28 | /* |
29 | * Parse one whitespace- or NUL-terminated, possibly C-quoted argument | |
30 | * and append the result to arg. Return a pointer to the terminator. | |
31 | * Die if there is an error in how the argument is C-quoted. This | |
32 | * function is only used if not -z. | |
33 | */ | |
c750ba95 BK |
34 | static const char *parse_arg(const char *next, struct strbuf *arg) |
35 | { | |
697a4151 MH |
36 | if (*next == '"') { |
37 | const char *orig = next; | |
38 | ||
39 | if (unquote_c_style(arg, next, &next)) | |
40 | die("badly quoted argument: %s", orig); | |
41 | if (*next && !isspace(*next)) | |
42 | die("unexpected character after quoted argument: %s", orig); | |
43 | } else { | |
c750ba95 BK |
44 | while (*next && !isspace(*next)) |
45 | strbuf_addch(arg, *next++); | |
697a4151 | 46 | } |
c750ba95 | 47 | |
c750ba95 BK |
48 | return next; |
49 | } | |
50 | ||
e23d8435 | 51 | /* |
ed410e61 MH |
52 | * Parse the reference name immediately after "command SP". If not |
53 | * -z, then handle C-quoting. Return a pointer to a newly allocated | |
54 | * string containing the name of the reference, or NULL if there was | |
55 | * an error. Update *next to point at the character that terminates | |
56 | * the argument. Die if C-quoting is malformed or the reference name | |
57 | * is invalid. | |
e23d8435 | 58 | */ |
5ae6c5a7 | 59 | static char *parse_refname(const char **next) |
c750ba95 | 60 | { |
ed410e61 MH |
61 | struct strbuf ref = STRBUF_INIT; |
62 | ||
c750ba95 BK |
63 | if (line_termination) { |
64 | /* Without -z, use the next argument */ | |
ed410e61 | 65 | *next = parse_arg(*next, &ref); |
c750ba95 | 66 | } else { |
e23d8435 | 67 | /* With -z, use everything up to the next NUL */ |
ed410e61 MH |
68 | strbuf_addstr(&ref, *next); |
69 | *next += ref.len; | |
70 | } | |
71 | ||
72 | if (!ref.len) { | |
73 | strbuf_release(&ref); | |
74 | return NULL; | |
c750ba95 | 75 | } |
ed410e61 MH |
76 | |
77 | if (check_refname_format(ref.buf, REFNAME_ALLOW_ONELEVEL)) | |
78 | die("invalid ref format: %s", ref.buf); | |
79 | ||
80 | return strbuf_detach(&ref, NULL); | |
c750ba95 BK |
81 | } |
82 | ||
1451ac73 KN |
83 | /* |
84 | * Wrapper around parse_refname which skips the next delimiter. | |
85 | */ | |
86 | static char *parse_next_refname(const char **next) | |
87 | { | |
88 | if (line_termination) { | |
89 | /* Without -z, consume SP and use next argument */ | |
90 | if (!**next || **next == line_termination) | |
91 | return NULL; | |
92 | if (**next != ' ') | |
93 | die("expected SP but got: %s", *next); | |
94 | } else { | |
95 | /* With -z, read the next NUL-terminated line */ | |
96 | if (**next) | |
97 | return NULL; | |
98 | } | |
99 | /* Skip the delimiter */ | |
100 | (*next)++; | |
101 | ||
102 | return parse_refname(next); | |
103 | } | |
104 | ||
7dd4051b KN |
105 | /* |
106 | * Wrapper around parse_arg which skips the next delimiter. | |
107 | */ | |
108 | static char *parse_next_arg(const char **next) | |
109 | { | |
110 | struct strbuf arg = STRBUF_INIT; | |
111 | ||
112 | if (line_termination) { | |
113 | /* Without -z, consume SP and use next argument */ | |
114 | if (!**next || **next == line_termination) | |
115 | return NULL; | |
116 | if (**next != ' ') | |
117 | die("expected SP but got: %s", *next); | |
118 | } else { | |
119 | /* With -z, read the next NUL-terminated line */ | |
120 | if (**next) | |
121 | return NULL; | |
122 | } | |
123 | /* Skip the delimiter */ | |
124 | (*next)++; | |
125 | ||
126 | if (line_termination) { | |
127 | /* Without -z, use the next argument */ | |
128 | *next = parse_arg(*next, &arg); | |
129 | } else { | |
130 | /* With -z, use everything up to the next NUL */ | |
131 | strbuf_addstr(&arg, *next); | |
132 | *next += arg.len; | |
133 | } | |
134 | ||
135 | if (arg.len) | |
136 | return strbuf_detach(&arg, NULL); | |
137 | ||
138 | strbuf_release(&arg); | |
139 | return NULL; | |
140 | } | |
1451ac73 | 141 | |
e23d8435 | 142 | /* |
67e943c3 | 143 | * The value being parsed is <old-oid> (as opposed to <new-oid>; the |
3afcc463 MH |
144 | * difference affects which error messages are generated): |
145 | */ | |
146 | #define PARSE_SHA1_OLD 0x01 | |
147 | ||
148 | /* | |
149 | * For backwards compatibility, accept an empty string for update's | |
67e943c3 | 150 | * <new-oid> in binary mode to be equivalent to specifying zeros. |
3afcc463 MH |
151 | */ |
152 | #define PARSE_SHA1_ALLOW_EMPTY 0x02 | |
153 | ||
154 | /* | |
155 | * Parse an argument separator followed by the next argument, if any. | |
156 | * If there is an argument, convert it to a SHA-1, write it to sha1, | |
157 | * set *next to point at the character terminating the argument, and | |
e23d8435 | 158 | * return 0. If there is no argument at all (not even the empty |
3afcc463 MH |
159 | * string), return 1 and leave *next unchanged. If the value is |
160 | * provided but cannot be converted to a SHA-1, die. flags can | |
161 | * include PARSE_SHA1_OLD and/or PARSE_SHA1_ALLOW_EMPTY. | |
e23d8435 | 162 | */ |
804dba54 | 163 | static int parse_next_oid(const char **next, const char *end, |
a0bb5535 | 164 | struct object_id *oid, |
165 | const char *command, const char *refname, | |
166 | int flags) | |
c750ba95 | 167 | { |
3afcc463 MH |
168 | struct strbuf arg = STRBUF_INIT; |
169 | int ret = 0; | |
170 | ||
804dba54 | 171 | if (*next == end) |
3afcc463 MH |
172 | goto eof; |
173 | ||
c750ba95 BK |
174 | if (line_termination) { |
175 | /* Without -z, consume SP and use next argument */ | |
e23d8435 | 176 | if (!**next || **next == line_termination) |
3afcc463 | 177 | return 1; |
e23d8435 | 178 | if (**next != ' ') |
3afcc463 MH |
179 | die("%s %s: expected SP but got: %s", |
180 | command, refname, *next); | |
e23d8435 | 181 | (*next)++; |
3afcc463 MH |
182 | *next = parse_arg(*next, &arg); |
183 | if (arg.len) { | |
3c20bf0c PS |
184 | if (repo_get_oid_with_flags(the_repository, arg.buf, oid, |
185 | GET_OID_SKIP_AMBIGUITY_CHECK)) | |
3afcc463 MH |
186 | goto invalid; |
187 | } else { | |
188 | /* Without -z, an empty value means all zeros: */ | |
9da95bda | 189 | oidclr(oid, the_repository->hash_algo); |
3afcc463 | 190 | } |
c750ba95 BK |
191 | } else { |
192 | /* With -z, read the next NUL-terminated line */ | |
e23d8435 | 193 | if (**next) |
3afcc463 MH |
194 | die("%s %s: expected NUL but got: %s", |
195 | command, refname, *next); | |
e23d8435 | 196 | (*next)++; |
804dba54 | 197 | if (*next == end) |
3afcc463 MH |
198 | goto eof; |
199 | strbuf_addstr(&arg, *next); | |
200 | *next += arg.len; | |
201 | ||
202 | if (arg.len) { | |
3c20bf0c PS |
203 | if (repo_get_oid_with_flags(the_repository, arg.buf, oid, |
204 | GET_OID_SKIP_AMBIGUITY_CHECK)) | |
3afcc463 MH |
205 | goto invalid; |
206 | } else if (flags & PARSE_SHA1_ALLOW_EMPTY) { | |
207 | /* With -z, treat an empty value as all zeros: */ | |
67e943c3 | 208 | warning("%s %s: missing <new-oid>, treating as zero", |
1fbd5049 | 209 | command, refname); |
9da95bda | 210 | oidclr(oid, the_repository->hash_algo); |
3afcc463 MH |
211 | } else { |
212 | /* | |
213 | * With -z, an empty non-required value means | |
214 | * unspecified: | |
215 | */ | |
216 | ret = 1; | |
217 | } | |
c750ba95 | 218 | } |
3afcc463 MH |
219 | |
220 | strbuf_release(&arg); | |
221 | ||
222 | return ret; | |
223 | ||
224 | invalid: | |
225 | die(flags & PARSE_SHA1_OLD ? | |
67e943c3 KN |
226 | "%s %s: invalid <old-oid>: %s" : |
227 | "%s %s: invalid <new-oid>: %s", | |
3afcc463 MH |
228 | command, refname, arg.buf); |
229 | ||
230 | eof: | |
231 | die(flags & PARSE_SHA1_OLD ? | |
67e943c3 KN |
232 | "%s %s: unexpected end of input when reading <old-oid>" : |
233 | "%s %s: unexpected end of input when reading <new-oid>", | |
3afcc463 | 234 | command, refname); |
c750ba95 BK |
235 | } |
236 | ||
e23d8435 MH |
237 | |
238 | /* | |
239 | * The following five parse_cmd_*() functions parse the corresponding | |
240 | * command. In each case, next points at the character following the | |
241 | * command name and the following space. They each return a pointer | |
242 | * to the character terminating the command, and die with an | |
243 | * explanatory message if there are any parsing problems. All of | |
244 | * these functions handle either text or binary format input, | |
245 | * depending on how line_termination is set. | |
246 | */ | |
247 | ||
94fd491a PS |
248 | static void parse_cmd_update(struct ref_transaction *transaction, |
249 | const char *next, const char *end) | |
c750ba95 | 250 | { |
ab5ac957 | 251 | struct strbuf err = STRBUF_INIT; |
aebfc133 | 252 | char *refname; |
a0bb5535 | 253 | struct object_id new_oid, old_oid; |
aebfc133 | 254 | int have_old; |
c750ba95 | 255 | |
5ae6c5a7 | 256 | refname = parse_refname(&next); |
aebfc133 | 257 | if (!refname) |
f11b09fb | 258 | die("update: missing <ref>"); |
c750ba95 | 259 | |
804dba54 | 260 | if (parse_next_oid(&next, end, &new_oid, "update", refname, |
a0bb5535 | 261 | PARSE_SHA1_ALLOW_EMPTY)) |
67e943c3 | 262 | die("update %s: missing <new-oid>", refname); |
c750ba95 | 263 | |
804dba54 | 264 | have_old = !parse_next_oid(&next, end, &old_oid, "update", refname, |
a0bb5535 | 265 | PARSE_SHA1_OLD); |
3afcc463 MH |
266 | |
267 | if (*next != line_termination) | |
aebfc133 MH |
268 | die("update %s: extra input: %s", refname, next); |
269 | ||
1d147bdf | 270 | if (ref_transaction_update(transaction, refname, |
89f3bbdd | 271 | &new_oid, have_old ? &old_oid : NULL, |
1bc4cc3f | 272 | NULL, NULL, |
144c76fa DT |
273 | update_flags | create_reflog_flag, |
274 | msg, &err)) | |
8e34800e | 275 | die("%s", err.buf); |
aebfc133 | 276 | |
d345e9fb | 277 | update_flags = default_flags; |
aebfc133 | 278 | free(refname); |
ab5ac957 | 279 | strbuf_release(&err); |
c750ba95 BK |
280 | } |
281 | ||
7dd4051b | 282 | static void parse_cmd_symref_update(struct ref_transaction *transaction, |
9dc1e748 | 283 | const char *next, const char *end UNUSED) |
7dd4051b KN |
284 | { |
285 | char *refname, *new_target, *old_arg; | |
286 | char *old_target = NULL; | |
287 | struct strbuf err = STRBUF_INIT; | |
288 | struct object_id old_oid; | |
289 | int have_old_oid = 0; | |
290 | ||
291 | refname = parse_refname(&next); | |
292 | if (!refname) | |
293 | die("symref-update: missing <ref>"); | |
294 | ||
295 | new_target = parse_next_refname(&next); | |
296 | if (!new_target) | |
297 | die("symref-update %s: missing <new-target>", refname); | |
298 | ||
299 | old_arg = parse_next_arg(&next); | |
300 | if (old_arg) { | |
301 | old_target = parse_next_arg(&next); | |
302 | if (!old_target) | |
303 | die("symref-update %s: expected old value", refname); | |
304 | ||
305 | if (!strcmp(old_arg, "oid")) { | |
3c20bf0c PS |
306 | if (repo_get_oid_with_flags(the_repository, old_target, &old_oid, |
307 | GET_OID_SKIP_AMBIGUITY_CHECK)) | |
7dd4051b KN |
308 | die("symref-update %s: invalid oid: %s", refname, old_target); |
309 | ||
310 | have_old_oid = 1; | |
311 | } else if (!strcmp(old_arg, "ref")) { | |
312 | if (check_refname_format(old_target, REFNAME_ALLOW_ONELEVEL)) | |
313 | die("symref-update %s: invalid ref: %s", refname, old_target); | |
314 | } else { | |
315 | die("symref-update %s: invalid arg '%s' for old value", refname, old_arg); | |
316 | } | |
317 | } | |
318 | ||
319 | if (*next != line_termination) | |
320 | die("symref-update %s: extra input: %s", refname, next); | |
321 | ||
322 | if (ref_transaction_update(transaction, refname, NULL, | |
323 | have_old_oid ? &old_oid : NULL, | |
324 | new_target, | |
325 | have_old_oid ? NULL : old_target, | |
326 | update_flags | create_reflog_flag, | |
327 | msg, &err)) | |
328 | die("%s", err.buf); | |
329 | ||
330 | update_flags = default_flags; | |
331 | free(refname); | |
332 | free(old_arg); | |
333 | free(old_target); | |
334 | free(new_target); | |
335 | strbuf_release(&err); | |
336 | } | |
337 | ||
94fd491a PS |
338 | static void parse_cmd_create(struct ref_transaction *transaction, |
339 | const char *next, const char *end) | |
c750ba95 | 340 | { |
ab5ac957 | 341 | struct strbuf err = STRBUF_INIT; |
aebfc133 | 342 | char *refname; |
a0bb5535 | 343 | struct object_id new_oid; |
c750ba95 | 344 | |
5ae6c5a7 | 345 | refname = parse_refname(&next); |
aebfc133 | 346 | if (!refname) |
f11b09fb | 347 | die("create: missing <ref>"); |
c750ba95 | 348 | |
804dba54 | 349 | if (parse_next_oid(&next, end, &new_oid, "create", refname, 0)) |
67e943c3 | 350 | die("create %s: missing <new-oid>", refname); |
e23d8435 | 351 | |
a0bb5535 | 352 | if (is_null_oid(&new_oid)) |
67e943c3 | 353 | die("create %s: zero <new-oid>", refname); |
c750ba95 | 354 | |
e23d8435 | 355 | if (*next != line_termination) |
aebfc133 MH |
356 | die("create %s: extra input: %s", refname, next); |
357 | ||
ed327272 | 358 | if (ref_transaction_create(transaction, refname, &new_oid, NULL, |
144c76fa DT |
359 | update_flags | create_reflog_flag, |
360 | msg, &err)) | |
b416af5b | 361 | die("%s", err.buf); |
aebfc133 | 362 | |
d345e9fb | 363 | update_flags = default_flags; |
aebfc133 | 364 | free(refname); |
ab5ac957 | 365 | strbuf_release(&err); |
c750ba95 BK |
366 | } |
367 | ||
ed327272 KN |
368 | |
369 | static void parse_cmd_symref_create(struct ref_transaction *transaction, | |
9dc1e748 | 370 | const char *next, const char *end UNUSED) |
ed327272 KN |
371 | { |
372 | struct strbuf err = STRBUF_INIT; | |
373 | char *refname, *new_target; | |
374 | ||
375 | refname = parse_refname(&next); | |
376 | if (!refname) | |
377 | die("symref-create: missing <ref>"); | |
378 | ||
379 | new_target = parse_next_refname(&next); | |
380 | if (!new_target) | |
381 | die("symref-create %s: missing <new-target>", refname); | |
382 | ||
383 | if (*next != line_termination) | |
384 | die("symref-create %s: extra input: %s", refname, next); | |
385 | ||
386 | if (ref_transaction_create(transaction, refname, NULL, new_target, | |
144c76fa DT |
387 | update_flags | create_reflog_flag, |
388 | msg, &err)) | |
b416af5b | 389 | die("%s", err.buf); |
aebfc133 | 390 | |
d345e9fb | 391 | update_flags = default_flags; |
aebfc133 | 392 | free(refname); |
ed327272 | 393 | free(new_target); |
ab5ac957 | 394 | strbuf_release(&err); |
c750ba95 BK |
395 | } |
396 | ||
94fd491a PS |
397 | static void parse_cmd_delete(struct ref_transaction *transaction, |
398 | const char *next, const char *end) | |
c750ba95 | 399 | { |
ab5ac957 | 400 | struct strbuf err = STRBUF_INIT; |
aebfc133 | 401 | char *refname; |
a0bb5535 | 402 | struct object_id old_oid; |
aebfc133 | 403 | int have_old; |
c750ba95 | 404 | |
5ae6c5a7 | 405 | refname = parse_refname(&next); |
aebfc133 | 406 | if (!refname) |
f11b09fb | 407 | die("delete: missing <ref>"); |
c750ba95 | 408 | |
804dba54 | 409 | if (parse_next_oid(&next, end, &old_oid, "delete", refname, |
a0bb5535 | 410 | PARSE_SHA1_OLD)) { |
aebfc133 | 411 | have_old = 0; |
3afcc463 | 412 | } else { |
a0bb5535 | 413 | if (is_null_oid(&old_oid)) |
67e943c3 | 414 | die("delete %s: zero <old-oid>", refname); |
aebfc133 | 415 | have_old = 1; |
3afcc463 | 416 | } |
c750ba95 | 417 | |
e23d8435 | 418 | if (*next != line_termination) |
aebfc133 MH |
419 | die("delete %s: extra input: %s", refname, next); |
420 | ||
fb5a6bb6 | 421 | if (ref_transaction_delete(transaction, refname, |
89f3bbdd | 422 | have_old ? &old_oid : NULL, |
23437209 | 423 | NULL, update_flags, msg, &err)) |
8c8bdc0d | 424 | die("%s", err.buf); |
aebfc133 | 425 | |
d345e9fb | 426 | update_flags = default_flags; |
aebfc133 | 427 | free(refname); |
ab5ac957 | 428 | strbuf_release(&err); |
c750ba95 BK |
429 | } |
430 | ||
23437209 KN |
431 | |
432 | static void parse_cmd_symref_delete(struct ref_transaction *transaction, | |
9dc1e748 | 433 | const char *next, const char *end UNUSED) |
23437209 KN |
434 | { |
435 | struct strbuf err = STRBUF_INIT; | |
436 | char *refname, *old_target; | |
437 | ||
438 | if (!(update_flags & REF_NO_DEREF)) | |
439 | die("symref-delete: cannot operate with deref mode"); | |
440 | ||
441 | refname = parse_refname(&next); | |
442 | if (!refname) | |
443 | die("symref-delete: missing <ref>"); | |
444 | ||
445 | old_target = parse_next_refname(&next); | |
446 | ||
447 | if (*next != line_termination) | |
448 | die("symref-delete %s: extra input: %s", refname, next); | |
449 | ||
450 | if (ref_transaction_delete(transaction, refname, NULL, | |
451 | old_target, update_flags, msg, &err)) | |
452 | die("%s", err.buf); | |
453 | ||
454 | update_flags = default_flags; | |
455 | free(refname); | |
456 | free(old_target); | |
457 | strbuf_release(&err); | |
458 | } | |
459 | ||
460 | ||
94fd491a PS |
461 | static void parse_cmd_verify(struct ref_transaction *transaction, |
462 | const char *next, const char *end) | |
c750ba95 | 463 | { |
ab5ac957 | 464 | struct strbuf err = STRBUF_INIT; |
aebfc133 | 465 | char *refname; |
a0bb5535 | 466 | struct object_id old_oid; |
c750ba95 | 467 | |
5ae6c5a7 | 468 | refname = parse_refname(&next); |
aebfc133 | 469 | if (!refname) |
f11b09fb | 470 | die("verify: missing <ref>"); |
c750ba95 | 471 | |
804dba54 | 472 | if (parse_next_oid(&next, end, &old_oid, "verify", refname, |
a0bb5535 | 473 | PARSE_SHA1_OLD)) |
9da95bda | 474 | oidclr(&old_oid, the_repository->hash_algo); |
0e729c7e | 475 | |
e23d8435 | 476 | if (*next != line_termination) |
aebfc133 MH |
477 | die("verify %s: extra input: %s", refname, next); |
478 | ||
89f3bbdd | 479 | if (ref_transaction_verify(transaction, refname, &old_oid, |
1451ac73 KN |
480 | NULL, update_flags, &err)) |
481 | die("%s", err.buf); | |
482 | ||
483 | update_flags = default_flags; | |
484 | free(refname); | |
485 | strbuf_release(&err); | |
486 | } | |
487 | ||
488 | static void parse_cmd_symref_verify(struct ref_transaction *transaction, | |
9dc1e748 | 489 | const char *next, const char *end UNUSED) |
1451ac73 KN |
490 | { |
491 | struct strbuf err = STRBUF_INIT; | |
492 | struct object_id old_oid; | |
493 | char *refname, *old_target; | |
494 | ||
495 | if (!(update_flags & REF_NO_DEREF)) | |
496 | die("symref-verify: cannot operate with deref mode"); | |
497 | ||
498 | refname = parse_refname(&next); | |
499 | if (!refname) | |
500 | die("symref-verify: missing <ref>"); | |
501 | ||
502 | /* | |
503 | * old_ref is optional, if not provided, we need to ensure that the | |
504 | * ref doesn't exist. | |
505 | */ | |
506 | old_target = parse_next_refname(&next); | |
507 | if (!old_target) | |
7d70b29c | 508 | oidcpy(&old_oid, null_oid(the_hash_algo)); |
1451ac73 KN |
509 | |
510 | if (*next != line_termination) | |
511 | die("symref-verify %s: extra input: %s", refname, next); | |
512 | ||
513 | if (ref_transaction_verify(transaction, refname, | |
514 | old_target ? NULL : &old_oid, | |
515 | old_target, update_flags, &err)) | |
8e34800e | 516 | die("%s", err.buf); |
aebfc133 | 517 | |
d345e9fb | 518 | update_flags = default_flags; |
aebfc133 | 519 | free(refname); |
1451ac73 | 520 | free(old_target); |
ab5ac957 | 521 | strbuf_release(&err); |
c750ba95 BK |
522 | } |
523 | ||
efa3d64c PS |
524 | static void report_ok(const char *command) |
525 | { | |
526 | fprintf(stdout, "%s: ok\n", command); | |
527 | fflush(stdout); | |
528 | } | |
529 | ||
44ad0829 JK |
530 | static void parse_cmd_option(struct ref_transaction *transaction UNUSED, |
531 | const char *next, const char *end UNUSED) | |
c750ba95 | 532 | { |
aee9be2e SG |
533 | const char *rest; |
534 | if (skip_prefix(next, "no-deref", &rest) && *rest == line_termination) | |
91774afc | 535 | update_flags |= REF_NO_DEREF; |
c750ba95 BK |
536 | else |
537 | die("option unknown: %s", next); | |
538 | } | |
539 | ||
44ad0829 JK |
540 | static void parse_cmd_start(struct ref_transaction *transaction UNUSED, |
541 | const char *next, const char *end UNUSED) | |
e48cf33b PS |
542 | { |
543 | if (*next != line_termination) | |
544 | die("start: extra input: %s", next); | |
efa3d64c | 545 | report_ok("start"); |
e48cf33b PS |
546 | } |
547 | ||
548 | static void parse_cmd_prepare(struct ref_transaction *transaction, | |
44ad0829 | 549 | const char *next, const char *end UNUSED) |
e48cf33b PS |
550 | { |
551 | struct strbuf error = STRBUF_INIT; | |
552 | if (*next != line_termination) | |
553 | die("prepare: extra input: %s", next); | |
554 | if (ref_transaction_prepare(transaction, &error)) | |
555 | die("prepare: %s", error.buf); | |
efa3d64c | 556 | report_ok("prepare"); |
e48cf33b PS |
557 | } |
558 | ||
559 | static void parse_cmd_abort(struct ref_transaction *transaction, | |
44ad0829 | 560 | const char *next, const char *end UNUSED) |
e48cf33b PS |
561 | { |
562 | struct strbuf error = STRBUF_INIT; | |
563 | if (*next != line_termination) | |
564 | die("abort: extra input: %s", next); | |
565 | if (ref_transaction_abort(transaction, &error)) | |
566 | die("abort: %s", error.buf); | |
efa3d64c | 567 | report_ok("abort"); |
e48cf33b PS |
568 | } |
569 | ||
221e8fcb KN |
570 | static void print_rejected_refs(const char *refname, |
571 | const struct object_id *old_oid, | |
572 | const struct object_id *new_oid, | |
573 | const char *old_target, | |
574 | const char *new_target, | |
575 | enum ref_transaction_error err, | |
576 | void *cb_data UNUSED) | |
577 | { | |
578 | struct strbuf sb = STRBUF_INIT; | |
b3de3832 | 579 | const char *reason = ref_transaction_error_msg(err); |
221e8fcb KN |
580 | |
581 | strbuf_addf(&sb, "rejected %s %s %s %s\n", refname, | |
582 | new_oid ? oid_to_hex(new_oid) : new_target, | |
583 | old_oid ? oid_to_hex(old_oid) : old_target, | |
584 | reason); | |
585 | ||
586 | fwrite(sb.buf, sb.len, 1, stdout); | |
587 | strbuf_release(&sb); | |
588 | } | |
589 | ||
e48cf33b | 590 | static void parse_cmd_commit(struct ref_transaction *transaction, |
44ad0829 | 591 | const char *next, const char *end UNUSED) |
e48cf33b PS |
592 | { |
593 | struct strbuf error = STRBUF_INIT; | |
594 | if (*next != line_termination) | |
595 | die("commit: extra input: %s", next); | |
596 | if (ref_transaction_commit(transaction, &error)) | |
597 | die("commit: %s", error.buf); | |
221e8fcb KN |
598 | |
599 | ref_transaction_for_each_rejected_update(transaction, | |
600 | print_rejected_refs, NULL); | |
601 | ||
efa3d64c | 602 | report_ok("commit"); |
e48cf33b PS |
603 | ref_transaction_free(transaction); |
604 | } | |
605 | ||
606 | enum update_refs_state { | |
607 | /* Non-transactional state open for updates. */ | |
608 | UPDATE_REFS_OPEN, | |
609 | /* A transaction has been started. */ | |
610 | UPDATE_REFS_STARTED, | |
611 | /* References are locked and ready for commit */ | |
612 | UPDATE_REFS_PREPARED, | |
613 | /* Transaction has been committed or closed. */ | |
614 | UPDATE_REFS_CLOSED, | |
615 | }; | |
616 | ||
a65b8ac2 PS |
617 | static const struct parse_cmd { |
618 | const char *prefix; | |
94fd491a PS |
619 | void (*fn)(struct ref_transaction *, const char *, const char *); |
620 | unsigned args; | |
e48cf33b | 621 | enum update_refs_state state; |
a65b8ac2 | 622 | } command[] = { |
1451ac73 KN |
623 | { "update", parse_cmd_update, 3, UPDATE_REFS_OPEN }, |
624 | { "create", parse_cmd_create, 2, UPDATE_REFS_OPEN }, | |
625 | { "delete", parse_cmd_delete, 2, UPDATE_REFS_OPEN }, | |
626 | { "verify", parse_cmd_verify, 2, UPDATE_REFS_OPEN }, | |
7dd4051b | 627 | { "symref-update", parse_cmd_symref_update, 4, UPDATE_REFS_OPEN }, |
ed327272 | 628 | { "symref-create", parse_cmd_symref_create, 2, UPDATE_REFS_OPEN }, |
23437209 | 629 | { "symref-delete", parse_cmd_symref_delete, 2, UPDATE_REFS_OPEN }, |
1451ac73 KN |
630 | { "symref-verify", parse_cmd_symref_verify, 2, UPDATE_REFS_OPEN }, |
631 | { "option", parse_cmd_option, 1, UPDATE_REFS_OPEN }, | |
632 | { "start", parse_cmd_start, 0, UPDATE_REFS_STARTED }, | |
633 | { "prepare", parse_cmd_prepare, 0, UPDATE_REFS_PREPARED }, | |
634 | { "abort", parse_cmd_abort, 0, UPDATE_REFS_CLOSED }, | |
635 | { "commit", parse_cmd_commit, 0, UPDATE_REFS_CLOSED }, | |
a65b8ac2 PS |
636 | }; |
637 | ||
221e8fcb | 638 | static void update_refs_stdin(unsigned int flags) |
c750ba95 | 639 | { |
de0e0d65 | 640 | struct strbuf input = STRBUF_INIT, err = STRBUF_INIT; |
e48cf33b | 641 | enum update_refs_state state = UPDATE_REFS_OPEN; |
de0e0d65 | 642 | struct ref_transaction *transaction; |
94fd491a | 643 | int i, j; |
de0e0d65 | 644 | |
2e5c4758 | 645 | transaction = ref_store_transaction_begin(get_main_ref_store(the_repository), |
221e8fcb | 646 | flags, &err); |
de0e0d65 PS |
647 | if (!transaction) |
648 | die("%s", err.buf); | |
649 | ||
c750ba95 | 650 | /* Read each line dispatch its command */ |
94fd491a | 651 | while (!strbuf_getwholeline(&input, stdin, line_termination)) { |
a65b8ac2 PS |
652 | const struct parse_cmd *cmd = NULL; |
653 | ||
94fd491a | 654 | if (*input.buf == line_termination) |
c750ba95 | 655 | die("empty command in input"); |
94fd491a PS |
656 | else if (isspace(*input.buf)) |
657 | die("whitespace before command: %s", input.buf); | |
a65b8ac2 PS |
658 | |
659 | for (i = 0; i < ARRAY_SIZE(command); i++) { | |
660 | const char *prefix = command[i].prefix; | |
94fd491a | 661 | char c; |
a65b8ac2 | 662 | |
94fd491a PS |
663 | if (!starts_with(input.buf, prefix)) |
664 | continue; | |
665 | ||
666 | /* | |
667 | * If the command has arguments, verify that it's | |
668 | * followed by a space. Otherwise, it shall be followed | |
669 | * by a line terminator. | |
670 | */ | |
671 | c = command[i].args ? ' ' : line_termination; | |
672 | if (input.buf[strlen(prefix)] != c) | |
a65b8ac2 PS |
673 | continue; |
674 | ||
675 | cmd = &command[i]; | |
676 | break; | |
677 | } | |
678 | if (!cmd) | |
94fd491a PS |
679 | die("unknown command: %s", input.buf); |
680 | ||
681 | /* | |
682 | * Read additional arguments if NUL-terminated. Do not raise an | |
683 | * error in case there is an early EOF to let the command | |
684 | * handle missing arguments with a proper error message. | |
685 | */ | |
686 | for (j = 1; line_termination == '\0' && j < cmd->args; j++) | |
687 | if (strbuf_appendwholeline(&input, stdin, line_termination)) | |
688 | break; | |
689 | ||
e48cf33b PS |
690 | switch (state) { |
691 | case UPDATE_REFS_OPEN: | |
692 | case UPDATE_REFS_STARTED: | |
8c4417f1 PS |
693 | if (state == UPDATE_REFS_STARTED && cmd->state == UPDATE_REFS_STARTED) |
694 | die("cannot restart ongoing transaction"); | |
e48cf33b PS |
695 | /* Do not downgrade a transaction to a non-transaction. */ |
696 | if (cmd->state >= state) | |
697 | state = cmd->state; | |
698 | break; | |
699 | case UPDATE_REFS_PREPARED: | |
700 | if (cmd->state != UPDATE_REFS_CLOSED) | |
701 | die("prepared transactions can only be closed"); | |
702 | state = cmd->state; | |
703 | break; | |
704 | case UPDATE_REFS_CLOSED: | |
262a4d28 PS |
705 | if (cmd->state != UPDATE_REFS_STARTED) |
706 | die("transaction is closed"); | |
707 | ||
708 | /* | |
709 | * Open a new transaction if we're currently closed and | |
710 | * get a "start". | |
711 | */ | |
712 | state = cmd->state; | |
2e5c4758 | 713 | transaction = ref_store_transaction_begin(get_main_ref_store(the_repository), |
221e8fcb | 714 | flags, &err); |
262a4d28 PS |
715 | if (!transaction) |
716 | die("%s", err.buf); | |
717 | ||
e48cf33b PS |
718 | break; |
719 | } | |
720 | ||
94fd491a PS |
721 | cmd->fn(transaction, input.buf + strlen(cmd->prefix) + !!cmd->args, |
722 | input.buf + input.len); | |
e23d8435 | 723 | } |
c750ba95 | 724 | |
e48cf33b PS |
725 | switch (state) { |
726 | case UPDATE_REFS_OPEN: | |
727 | /* Commit by default if no transaction was requested. */ | |
728 | if (ref_transaction_commit(transaction, &err)) | |
729 | die("%s", err.buf); | |
221e8fcb KN |
730 | ref_transaction_for_each_rejected_update(transaction, |
731 | print_rejected_refs, NULL); | |
e48cf33b PS |
732 | ref_transaction_free(transaction); |
733 | break; | |
734 | case UPDATE_REFS_STARTED: | |
735 | case UPDATE_REFS_PREPARED: | |
736 | /* If using a transaction, we want to abort it. */ | |
737 | if (ref_transaction_abort(transaction, &err)) | |
738 | die("%s", err.buf); | |
739 | break; | |
740 | case UPDATE_REFS_CLOSED: | |
741 | /* Otherwise no need to do anything, the transaction was closed already. */ | |
742 | break; | |
743 | } | |
de0e0d65 | 744 | |
de0e0d65 | 745 | strbuf_release(&err); |
e23d8435 | 746 | strbuf_release(&input); |
c750ba95 BK |
747 | } |
748 | ||
9b1cb507 JC |
749 | int cmd_update_ref(int argc, |
750 | const char **argv, | |
751 | const char *prefix, | |
752 | struct repository *repo UNUSED) | |
66bf85a4 | 753 | { |
db7516ab | 754 | const char *refname, *oldval; |
a0bb5535 | 755 | struct object_id oid, oldoid; |
fec14ec3 | 756 | int delete = 0, no_deref = 0, read_stdin = 0, end_null = 0; |
144c76fa | 757 | int create_reflog = 0; |
221e8fcb KN |
758 | unsigned int flags = 0; |
759 | ||
89942be6 | 760 | struct option options[] = { |
82269505 | 761 | OPT_STRING( 'm', NULL, &msg, N_("reason"), N_("reason of the update")), |
d5d09d47 SB |
762 | OPT_BOOL('d', NULL, &delete, N_("delete the reference")), |
763 | OPT_BOOL( 0 , "no-deref", &no_deref, | |
82269505 | 764 | N_("update <refname> not the one it points to")), |
9a86b899 JH |
765 | OPT_BOOL('z', NULL, &end_null, N_("stdin has NUL-terminated arguments")), |
766 | OPT_BOOL( 0 , "stdin", &read_stdin, N_("read updates from stdin")), | |
98c32bd8 | 767 | OPT_BOOL( 0 , "create-reflog", &create_reflog, N_("create a reflog")), |
221e8fcb KN |
768 | OPT_BIT('0', "batch-updates", &flags, N_("batch reference updates"), |
769 | REF_TRANSACTION_ALLOW_FAILURE), | |
89942be6 PH |
770 | OPT_END(), |
771 | }; | |
66bf85a4 | 772 | |
9ce196e8 | 773 | repo_config(the_repository, git_default_config, NULL); |
37782920 SB |
774 | argc = parse_options(argc, argv, prefix, options, git_update_ref_usage, |
775 | 0); | |
89942be6 PH |
776 | if (msg && !*msg) |
777 | die("Refusing to perform update with empty message."); | |
5b16b090 | 778 | |
144c76fa DT |
779 | create_reflog_flag = create_reflog ? REF_FORCE_CREATE_REFLOG : 0; |
780 | ||
d345e9fb EN |
781 | if (no_deref) { |
782 | default_flags = REF_NO_DEREF; | |
783 | update_flags = default_flags; | |
784 | } | |
785 | ||
c750ba95 | 786 | if (read_stdin) { |
d345e9fb | 787 | if (delete || argc > 0) |
c750ba95 BK |
788 | usage_with_options(git_update_ref_usage, options); |
789 | if (end_null) | |
790 | line_termination = '\0'; | |
221e8fcb | 791 | update_refs_stdin(flags); |
8bcd3748 | 792 | return 0; |
221e8fcb KN |
793 | } else if (flags & REF_TRANSACTION_ALLOW_FAILURE) { |
794 | die("--batch-updates can only be used with --stdin"); | |
c750ba95 BK |
795 | } |
796 | ||
797 | if (end_null) | |
798 | usage_with_options(git_update_ref_usage, options); | |
799 | ||
ac5409e4 | 800 | if (delete) { |
3fe8dce6 | 801 | if (argc < 1 || argc > 2) |
973a70ea KW |
802 | usage_with_options(git_update_ref_usage, options); |
803 | refname = argv[0]; | |
804 | oldval = argv[1]; | |
805 | } else { | |
806 | const char *value; | |
807 | if (argc < 2 || argc > 3) | |
89942be6 | 808 | usage_with_options(git_update_ref_usage, options); |
973a70ea KW |
809 | refname = argv[0]; |
810 | value = argv[1]; | |
811 | oldval = argv[2]; | |
3c20bf0c PS |
812 | if (repo_get_oid_with_flags(the_repository, value, &oid, |
813 | GET_OID_SKIP_AMBIGUITY_CHECK)) | |
973a70ea | 814 | die("%s: not a valid SHA1", value); |
ac5409e4 JH |
815 | } |
816 | ||
e2991c80 MH |
817 | if (oldval) { |
818 | if (!*oldval) | |
819 | /* | |
820 | * The empty string implies that the reference | |
821 | * must not already exist: | |
822 | */ | |
9da95bda | 823 | oidclr(&oldoid, the_repository->hash_algo); |
3c20bf0c PS |
824 | else if (repo_get_oid_with_flags(the_repository, oldval, &oldoid, |
825 | GET_OID_SKIP_AMBIGUITY_CHECK)) | |
e2991c80 MH |
826 | die("%s: not a valid old SHA1", oldval); |
827 | } | |
66bf85a4 | 828 | |
973a70ea | 829 | if (delete) |
1c03c4d3 MH |
830 | /* |
831 | * For purposes of backwards compatibility, we treat | |
832 | * NULL_SHA1 as "don't care" here: | |
833 | */ | |
2e5c4758 PS |
834 | return refs_delete_ref(get_main_ref_store(the_repository), |
835 | msg, refname, | |
836 | (oldval && !is_null_oid(&oldoid)) ? &oldoid : NULL, | |
837 | default_flags); | |
973a70ea | 838 | else |
2e5c4758 PS |
839 | return refs_update_ref(get_main_ref_store(the_repository), |
840 | msg, refname, &oid, | |
841 | oldval ? &oldoid : NULL, | |
842 | default_flags | create_reflog_flag, | |
843 | UPDATE_REFS_DIE_ON_ERR); | |
66bf85a4 | 844 | } |