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