]> git.ipfire.org Git - thirdparty/git.git/blame_incremental - builtin/update-ref.c
The sixth batch
[thirdparty/git.git] / builtin / update-ref.c
... / ...
CommitLineData
1#define USE_THE_REPOSITORY_VARIABLE
2#define DISABLE_SIGN_COMPARE_WARNINGS
3
4#include "builtin.h"
5#include "config.h"
6#include "gettext.h"
7#include "hash.h"
8#include "hex.h"
9#include "refs.h"
10#include "object-name.h"
11#include "parse-options.h"
12#include "quote.h"
13
14static const char * const git_update_ref_usage[] = {
15 N_("git update-ref [<options>] -d <refname> [<old-oid>]"),
16 N_("git update-ref [<options>] <refname> <new-oid> [<old-oid>]"),
17 N_("git update-ref [<options>] --stdin [-z] [--batch-updates]"),
18 NULL
19};
20
21static char line_termination = '\n';
22static unsigned int update_flags;
23static unsigned int default_flags;
24static unsigned create_reflog_flag;
25static const char *msg;
26
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 */
33static const char *parse_arg(const char *next, struct strbuf *arg)
34{
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 {
43 while (*next && !isspace(*next))
44 strbuf_addch(arg, *next++);
45 }
46
47 return next;
48}
49
50/*
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.
57 */
58static char *parse_refname(const char **next)
59{
60 struct strbuf ref = STRBUF_INIT;
61
62 if (line_termination) {
63 /* Without -z, use the next argument */
64 *next = parse_arg(*next, &ref);
65 } else {
66 /* With -z, use everything up to the next NUL */
67 strbuf_addstr(&ref, *next);
68 *next += ref.len;
69 }
70
71 if (!ref.len) {
72 strbuf_release(&ref);
73 return NULL;
74 }
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);
80}
81
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
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}
140
141/*
142 * The value being parsed is <old-oid> (as opposed to <new-oid>; the
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
149 * <new-oid> in binary mode to be equivalent to specifying zeros.
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
157 * return 0. If there is no argument at all (not even the empty
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.
161 */
162static int parse_next_oid(const char **next, const char *end,
163 struct object_id *oid,
164 const char *command, const char *refname,
165 int flags)
166{
167 struct strbuf arg = STRBUF_INIT;
168 int ret = 0;
169
170 if (*next == end)
171 goto eof;
172
173 if (line_termination) {
174 /* Without -z, consume SP and use next argument */
175 if (!**next || **next == line_termination)
176 return 1;
177 if (**next != ' ')
178 die("%s %s: expected SP but got: %s",
179 command, refname, *next);
180 (*next)++;
181 *next = parse_arg(*next, &arg);
182 if (arg.len) {
183 if (repo_get_oid_with_flags(the_repository, arg.buf, oid,
184 GET_OID_SKIP_AMBIGUITY_CHECK))
185 goto invalid;
186 } else {
187 /* Without -z, an empty value means all zeros: */
188 oidclr(oid, the_repository->hash_algo);
189 }
190 } else {
191 /* With -z, read the next NUL-terminated line */
192 if (**next)
193 die("%s %s: expected NUL but got: %s",
194 command, refname, *next);
195 (*next)++;
196 if (*next == end)
197 goto eof;
198 strbuf_addstr(&arg, *next);
199 *next += arg.len;
200
201 if (arg.len) {
202 if (repo_get_oid_with_flags(the_repository, arg.buf, oid,
203 GET_OID_SKIP_AMBIGUITY_CHECK))
204 goto invalid;
205 } else if (flags & PARSE_SHA1_ALLOW_EMPTY) {
206 /* With -z, treat an empty value as all zeros: */
207 warning("%s %s: missing <new-oid>, treating as zero",
208 command, refname);
209 oidclr(oid, the_repository->hash_algo);
210 } else {
211 /*
212 * With -z, an empty non-required value means
213 * unspecified:
214 */
215 ret = 1;
216 }
217 }
218
219 strbuf_release(&arg);
220
221 return ret;
222
223 invalid:
224 die(flags & PARSE_SHA1_OLD ?
225 "%s %s: invalid <old-oid>: %s" :
226 "%s %s: invalid <new-oid>: %s",
227 command, refname, arg.buf);
228
229 eof:
230 die(flags & PARSE_SHA1_OLD ?
231 "%s %s: unexpected end of input when reading <old-oid>" :
232 "%s %s: unexpected end of input when reading <new-oid>",
233 command, refname);
234}
235
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
247static void parse_cmd_update(struct ref_transaction *transaction,
248 const char *next, const char *end)
249{
250 struct strbuf err = STRBUF_INIT;
251 char *refname;
252 struct object_id new_oid, old_oid;
253 int have_old;
254
255 refname = parse_refname(&next);
256 if (!refname)
257 die("update: missing <ref>");
258
259 if (parse_next_oid(&next, end, &new_oid, "update", refname,
260 PARSE_SHA1_ALLOW_EMPTY))
261 die("update %s: missing <new-oid>", refname);
262
263 have_old = !parse_next_oid(&next, end, &old_oid, "update", refname,
264 PARSE_SHA1_OLD);
265
266 if (*next != line_termination)
267 die("update %s: extra input: %s", refname, next);
268
269 if (ref_transaction_update(transaction, refname,
270 &new_oid, have_old ? &old_oid : NULL,
271 NULL, NULL,
272 update_flags | create_reflog_flag,
273 msg, &err))
274 die("%s", err.buf);
275
276 update_flags = default_flags;
277 free(refname);
278 strbuf_release(&err);
279}
280
281static void parse_cmd_symref_update(struct ref_transaction *transaction,
282 const char *next, const char *end UNUSED)
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")) {
305 if (repo_get_oid_with_flags(the_repository, old_target, &old_oid,
306 GET_OID_SKIP_AMBIGUITY_CHECK))
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
337static void parse_cmd_create(struct ref_transaction *transaction,
338 const char *next, const char *end)
339{
340 struct strbuf err = STRBUF_INIT;
341 char *refname;
342 struct object_id new_oid;
343
344 refname = parse_refname(&next);
345 if (!refname)
346 die("create: missing <ref>");
347
348 if (parse_next_oid(&next, end, &new_oid, "create", refname, 0))
349 die("create %s: missing <new-oid>", refname);
350
351 if (is_null_oid(&new_oid))
352 die("create %s: zero <new-oid>", refname);
353
354 if (*next != line_termination)
355 die("create %s: extra input: %s", refname, next);
356
357 if (ref_transaction_create(transaction, refname, &new_oid, NULL,
358 update_flags | create_reflog_flag,
359 msg, &err))
360 die("%s", err.buf);
361
362 update_flags = default_flags;
363 free(refname);
364 strbuf_release(&err);
365}
366
367
368static void parse_cmd_symref_create(struct ref_transaction *transaction,
369 const char *next, const char *end UNUSED)
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,
386 update_flags | create_reflog_flag,
387 msg, &err))
388 die("%s", err.buf);
389
390 update_flags = default_flags;
391 free(refname);
392 free(new_target);
393 strbuf_release(&err);
394}
395
396static void parse_cmd_delete(struct ref_transaction *transaction,
397 const char *next, const char *end)
398{
399 struct strbuf err = STRBUF_INIT;
400 char *refname;
401 struct object_id old_oid;
402 int have_old;
403
404 refname = parse_refname(&next);
405 if (!refname)
406 die("delete: missing <ref>");
407
408 if (parse_next_oid(&next, end, &old_oid, "delete", refname,
409 PARSE_SHA1_OLD)) {
410 have_old = 0;
411 } else {
412 if (is_null_oid(&old_oid))
413 die("delete %s: zero <old-oid>", refname);
414 have_old = 1;
415 }
416
417 if (*next != line_termination)
418 die("delete %s: extra input: %s", refname, next);
419
420 if (ref_transaction_delete(transaction, refname,
421 have_old ? &old_oid : NULL,
422 NULL, update_flags, msg, &err))
423 die("%s", err.buf);
424
425 update_flags = default_flags;
426 free(refname);
427 strbuf_release(&err);
428}
429
430
431static void parse_cmd_symref_delete(struct ref_transaction *transaction,
432 const char *next, const char *end UNUSED)
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
460static void parse_cmd_verify(struct ref_transaction *transaction,
461 const char *next, const char *end)
462{
463 struct strbuf err = STRBUF_INIT;
464 char *refname;
465 struct object_id old_oid;
466
467 refname = parse_refname(&next);
468 if (!refname)
469 die("verify: missing <ref>");
470
471 if (parse_next_oid(&next, end, &old_oid, "verify", refname,
472 PARSE_SHA1_OLD))
473 oidclr(&old_oid, the_repository->hash_algo);
474
475 if (*next != line_termination)
476 die("verify %s: extra input: %s", refname, next);
477
478 if (ref_transaction_verify(transaction, refname, &old_oid,
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,
488 const char *next, const char *end UNUSED)
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)
507 oidcpy(&old_oid, null_oid(the_hash_algo));
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))
515 die("%s", err.buf);
516
517 update_flags = default_flags;
518 free(refname);
519 free(old_target);
520 strbuf_release(&err);
521}
522
523static void report_ok(const char *command)
524{
525 fprintf(stdout, "%s: ok\n", command);
526 fflush(stdout);
527}
528
529static void parse_cmd_option(struct ref_transaction *transaction UNUSED,
530 const char *next, const char *end UNUSED)
531{
532 const char *rest;
533 if (skip_prefix(next, "no-deref", &rest) && *rest == line_termination)
534 update_flags |= REF_NO_DEREF;
535 else
536 die("option unknown: %s", next);
537}
538
539static void parse_cmd_start(struct ref_transaction *transaction UNUSED,
540 const char *next, const char *end UNUSED)
541{
542 if (*next != line_termination)
543 die("start: extra input: %s", next);
544 report_ok("start");
545}
546
547static void parse_cmd_prepare(struct ref_transaction *transaction,
548 const char *next, const char *end UNUSED)
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);
555 report_ok("prepare");
556}
557
558static void parse_cmd_abort(struct ref_transaction *transaction,
559 const char *next, const char *end UNUSED)
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);
566 report_ok("abort");
567}
568
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
612static void parse_cmd_commit(struct ref_transaction *transaction,
613 const char *next, const char *end UNUSED)
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);
620
621 ref_transaction_for_each_rejected_update(transaction,
622 print_rejected_refs, NULL);
623
624 report_ok("commit");
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
639static const struct parse_cmd {
640 const char *prefix;
641 void (*fn)(struct ref_transaction *, const char *, const char *);
642 unsigned args;
643 enum update_refs_state state;
644} command[] = {
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 },
649 { "symref-update", parse_cmd_symref_update, 4, UPDATE_REFS_OPEN },
650 { "symref-create", parse_cmd_symref_create, 2, UPDATE_REFS_OPEN },
651 { "symref-delete", parse_cmd_symref_delete, 2, UPDATE_REFS_OPEN },
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 },
658};
659
660static void update_refs_stdin(unsigned int flags)
661{
662 struct strbuf input = STRBUF_INIT, err = STRBUF_INIT;
663 enum update_refs_state state = UPDATE_REFS_OPEN;
664 struct ref_transaction *transaction;
665 int i, j;
666
667 transaction = ref_store_transaction_begin(get_main_ref_store(the_repository),
668 flags, &err);
669 if (!transaction)
670 die("%s", err.buf);
671
672 /* Read each line dispatch its command */
673 while (!strbuf_getwholeline(&input, stdin, line_termination)) {
674 const struct parse_cmd *cmd = NULL;
675
676 if (*input.buf == line_termination)
677 die("empty command in input");
678 else if (isspace(*input.buf))
679 die("whitespace before command: %s", input.buf);
680
681 for (i = 0; i < ARRAY_SIZE(command); i++) {
682 const char *prefix = command[i].prefix;
683 char c;
684
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)
695 continue;
696
697 cmd = &command[i];
698 break;
699 }
700 if (!cmd)
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
712 switch (state) {
713 case UPDATE_REFS_OPEN:
714 case UPDATE_REFS_STARTED:
715 if (state == UPDATE_REFS_STARTED && cmd->state == UPDATE_REFS_STARTED)
716 die("cannot restart ongoing transaction");
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:
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;
735 transaction = ref_store_transaction_begin(get_main_ref_store(the_repository),
736 flags, &err);
737 if (!transaction)
738 die("%s", err.buf);
739
740 break;
741 }
742
743 cmd->fn(transaction, input.buf + strlen(cmd->prefix) + !!cmd->args,
744 input.buf + input.len);
745 }
746
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);
752 ref_transaction_for_each_rejected_update(transaction,
753 print_rejected_refs, NULL);
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 }
766
767 strbuf_release(&err);
768 strbuf_release(&input);
769}
770
771int cmd_update_ref(int argc,
772 const char **argv,
773 const char *prefix,
774 struct repository *repo UNUSED)
775{
776 const char *refname, *oldval;
777 struct object_id oid, oldoid;
778 int delete = 0, no_deref = 0, read_stdin = 0, end_null = 0;
779 int create_reflog = 0;
780 unsigned int flags = 0;
781
782 struct option options[] = {
783 OPT_STRING( 'm', NULL, &msg, N_("reason"), N_("reason of the update")),
784 OPT_BOOL('d', NULL, &delete, N_("delete the reference")),
785 OPT_BOOL( 0 , "no-deref", &no_deref,
786 N_("update <refname> not the one it points to")),
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")),
789 OPT_BOOL( 0 , "create-reflog", &create_reflog, N_("create a reflog")),
790 OPT_BIT('0', "batch-updates", &flags, N_("batch reference updates"),
791 REF_TRANSACTION_ALLOW_FAILURE),
792 OPT_END(),
793 };
794
795 git_config(git_default_config, NULL);
796 argc = parse_options(argc, argv, prefix, options, git_update_ref_usage,
797 0);
798 if (msg && !*msg)
799 die("Refusing to perform update with empty message.");
800
801 create_reflog_flag = create_reflog ? REF_FORCE_CREATE_REFLOG : 0;
802
803 if (no_deref) {
804 default_flags = REF_NO_DEREF;
805 update_flags = default_flags;
806 }
807
808 if (read_stdin) {
809 if (delete || argc > 0)
810 usage_with_options(git_update_ref_usage, options);
811 if (end_null)
812 line_termination = '\0';
813 update_refs_stdin(flags);
814 return 0;
815 } else if (flags & REF_TRANSACTION_ALLOW_FAILURE) {
816 die("--batch-updates can only be used with --stdin");
817 }
818
819 if (end_null)
820 usage_with_options(git_update_ref_usage, options);
821
822 if (delete) {
823 if (argc < 1 || argc > 2)
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)
830 usage_with_options(git_update_ref_usage, options);
831 refname = argv[0];
832 value = argv[1];
833 oldval = argv[2];
834 if (repo_get_oid_with_flags(the_repository, value, &oid,
835 GET_OID_SKIP_AMBIGUITY_CHECK))
836 die("%s: not a valid SHA1", value);
837 }
838
839 if (oldval) {
840 if (!*oldval)
841 /*
842 * The empty string implies that the reference
843 * must not already exist:
844 */
845 oidclr(&oldoid, the_repository->hash_algo);
846 else if (repo_get_oid_with_flags(the_repository, oldval, &oldoid,
847 GET_OID_SKIP_AMBIGUITY_CHECK))
848 die("%s: not a valid old SHA1", oldval);
849 }
850
851 if (delete)
852 /*
853 * For purposes of backwards compatibility, we treat
854 * NULL_SHA1 as "don't care" here:
855 */
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);
860 else
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);
866}