1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
10 #include "object-name.h"
11 #include "parse-options.h"
14 static 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]"),
21 static char line_termination
= '\n';
22 static unsigned int update_flags
;
23 static unsigned int default_flags
;
24 static unsigned create_reflog_flag
;
25 static const char *msg
;
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.
33 static const char *parse_arg(const char *next
, struct strbuf
*arg
)
36 const char *orig
= next
;
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
);
43 while (*next
&& !isspace(*next
))
44 strbuf_addch(arg
, *next
++);
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
58 static char *parse_refname(const char **next
)
60 struct strbuf ref
= STRBUF_INIT
;
62 if (line_termination
) {
63 /* Without -z, use the next argument */
64 *next
= parse_arg(*next
, &ref
);
66 /* With -z, use everything up to the next NUL */
67 strbuf_addstr(&ref
, *next
);
76 if (check_refname_format(ref
.buf
, REFNAME_ALLOW_ONELEVEL
))
77 die("invalid ref format: %s", ref
.buf
);
79 return strbuf_detach(&ref
, NULL
);
83 * Wrapper around parse_refname which skips the next delimiter.
85 static char *parse_next_refname(const char **next
)
87 if (line_termination
) {
88 /* Without -z, consume SP and use next argument */
89 if (!**next
|| **next
== line_termination
)
92 die("expected SP but got: %s", *next
);
94 /* With -z, read the next NUL-terminated line */
98 /* Skip the delimiter */
101 return parse_refname(next
);
105 * Wrapper around parse_arg which skips the next delimiter.
107 static char *parse_next_arg(const char **next
)
109 struct strbuf arg
= STRBUF_INIT
;
111 if (line_termination
) {
112 /* Without -z, consume SP and use next argument */
113 if (!**next
|| **next
== line_termination
)
116 die("expected SP but got: %s", *next
);
118 /* With -z, read the next NUL-terminated line */
122 /* Skip the delimiter */
125 if (line_termination
) {
126 /* Without -z, use the next argument */
127 *next
= parse_arg(*next
, &arg
);
129 /* With -z, use everything up to the next NUL */
130 strbuf_addstr(&arg
, *next
);
135 return strbuf_detach(&arg
, NULL
);
137 strbuf_release(&arg
);
142 * The value being parsed is <old-oid> (as opposed to <new-oid>; the
143 * difference affects which error messages are generated):
145 #define PARSE_SHA1_OLD 0x01
148 * For backwards compatibility, accept an empty string for update's
149 * <new-oid> in binary mode to be equivalent to specifying zeros.
151 #define PARSE_SHA1_ALLOW_EMPTY 0x02
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.
162 static int parse_next_oid(const char **next
, const char *end
,
163 struct object_id
*oid
,
164 const char *command
, const char *refname
,
167 struct strbuf arg
= STRBUF_INIT
;
173 if (line_termination
) {
174 /* Without -z, consume SP and use next argument */
175 if (!**next
|| **next
== line_termination
)
178 die("%s %s: expected SP but got: %s",
179 command
, refname
, *next
);
181 *next
= parse_arg(*next
, &arg
);
183 if (repo_get_oid_with_flags(the_repository
, arg
.buf
, oid
,
184 GET_OID_SKIP_AMBIGUITY_CHECK
))
187 /* Without -z, an empty value means all zeros: */
188 oidclr(oid
, the_repository
->hash_algo
);
191 /* With -z, read the next NUL-terminated line */
193 die("%s %s: expected NUL but got: %s",
194 command
, refname
, *next
);
198 strbuf_addstr(&arg
, *next
);
202 if (repo_get_oid_with_flags(the_repository
, arg
.buf
, oid
,
203 GET_OID_SKIP_AMBIGUITY_CHECK
))
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",
209 oidclr(oid
, the_repository
->hash_algo
);
212 * With -z, an empty non-required value means
219 strbuf_release(&arg
);
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
);
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>",
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.
247 static void parse_cmd_update(struct ref_transaction
*transaction
,
248 const char *next
, const char *end
)
250 struct strbuf err
= STRBUF_INIT
;
252 struct object_id new_oid
, old_oid
;
255 refname
= parse_refname(&next
);
257 die("update: missing <ref>");
259 if (parse_next_oid(&next
, end
, &new_oid
, "update", refname
,
260 PARSE_SHA1_ALLOW_EMPTY
))
261 die("update %s: missing <new-oid>", refname
);
263 have_old
= !parse_next_oid(&next
, end
, &old_oid
, "update", refname
,
266 if (*next
!= line_termination
)
267 die("update %s: extra input: %s", refname
, next
);
269 if (ref_transaction_update(transaction
, refname
,
270 &new_oid
, have_old
? &old_oid
: NULL
,
272 update_flags
| create_reflog_flag
,
276 update_flags
= default_flags
;
278 strbuf_release(&err
);
281 static void parse_cmd_symref_update(struct ref_transaction
*transaction
,
282 const char *next
, const char *end UNUSED
)
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;
290 refname
= parse_refname(&next
);
292 die("symref-update: missing <ref>");
294 new_target
= parse_next_refname(&next
);
296 die("symref-update %s: missing <new-target>", refname
);
298 old_arg
= parse_next_arg(&next
);
300 old_target
= parse_next_arg(&next
);
302 die("symref-update %s: expected old value", refname
);
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
);
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
);
314 die("symref-update %s: invalid arg '%s' for old value", refname
, old_arg
);
318 if (*next
!= line_termination
)
319 die("symref-update %s: extra input: %s", refname
, next
);
321 if (ref_transaction_update(transaction
, refname
, NULL
,
322 have_old_oid
? &old_oid
: NULL
,
324 have_old_oid
? NULL
: old_target
,
325 update_flags
| create_reflog_flag
,
329 update_flags
= default_flags
;
334 strbuf_release(&err
);
337 static void parse_cmd_create(struct ref_transaction
*transaction
,
338 const char *next
, const char *end
)
340 struct strbuf err
= STRBUF_INIT
;
342 struct object_id new_oid
;
344 refname
= parse_refname(&next
);
346 die("create: missing <ref>");
348 if (parse_next_oid(&next
, end
, &new_oid
, "create", refname
, 0))
349 die("create %s: missing <new-oid>", refname
);
351 if (is_null_oid(&new_oid
))
352 die("create %s: zero <new-oid>", refname
);
354 if (*next
!= line_termination
)
355 die("create %s: extra input: %s", refname
, next
);
357 if (ref_transaction_create(transaction
, refname
, &new_oid
, NULL
,
358 update_flags
| create_reflog_flag
,
362 update_flags
= default_flags
;
364 strbuf_release(&err
);
368 static void parse_cmd_symref_create(struct ref_transaction
*transaction
,
369 const char *next
, const char *end UNUSED
)
371 struct strbuf err
= STRBUF_INIT
;
372 char *refname
, *new_target
;
374 refname
= parse_refname(&next
);
376 die("symref-create: missing <ref>");
378 new_target
= parse_next_refname(&next
);
380 die("symref-create %s: missing <new-target>", refname
);
382 if (*next
!= line_termination
)
383 die("symref-create %s: extra input: %s", refname
, next
);
385 if (ref_transaction_create(transaction
, refname
, NULL
, new_target
,
386 update_flags
| create_reflog_flag
,
390 update_flags
= default_flags
;
393 strbuf_release(&err
);
396 static void parse_cmd_delete(struct ref_transaction
*transaction
,
397 const char *next
, const char *end
)
399 struct strbuf err
= STRBUF_INIT
;
401 struct object_id old_oid
;
404 refname
= parse_refname(&next
);
406 die("delete: missing <ref>");
408 if (parse_next_oid(&next
, end
, &old_oid
, "delete", refname
,
412 if (is_null_oid(&old_oid
))
413 die("delete %s: zero <old-oid>", refname
);
417 if (*next
!= line_termination
)
418 die("delete %s: extra input: %s", refname
, next
);
420 if (ref_transaction_delete(transaction
, refname
,
421 have_old
? &old_oid
: NULL
,
422 NULL
, update_flags
, msg
, &err
))
425 update_flags
= default_flags
;
427 strbuf_release(&err
);
431 static void parse_cmd_symref_delete(struct ref_transaction
*transaction
,
432 const char *next
, const char *end UNUSED
)
434 struct strbuf err
= STRBUF_INIT
;
435 char *refname
, *old_target
;
437 if (!(update_flags
& REF_NO_DEREF
))
438 die("symref-delete: cannot operate with deref mode");
440 refname
= parse_refname(&next
);
442 die("symref-delete: missing <ref>");
444 old_target
= parse_next_refname(&next
);
446 if (*next
!= line_termination
)
447 die("symref-delete %s: extra input: %s", refname
, next
);
449 if (ref_transaction_delete(transaction
, refname
, NULL
,
450 old_target
, update_flags
, msg
, &err
))
453 update_flags
= default_flags
;
456 strbuf_release(&err
);
460 static void parse_cmd_verify(struct ref_transaction
*transaction
,
461 const char *next
, const char *end
)
463 struct strbuf err
= STRBUF_INIT
;
465 struct object_id old_oid
;
467 refname
= parse_refname(&next
);
469 die("verify: missing <ref>");
471 if (parse_next_oid(&next
, end
, &old_oid
, "verify", refname
,
473 oidclr(&old_oid
, the_repository
->hash_algo
);
475 if (*next
!= line_termination
)
476 die("verify %s: extra input: %s", refname
, next
);
478 if (ref_transaction_verify(transaction
, refname
, &old_oid
,
479 NULL
, update_flags
, &err
))
482 update_flags
= default_flags
;
484 strbuf_release(&err
);
487 static void parse_cmd_symref_verify(struct ref_transaction
*transaction
,
488 const char *next
, const char *end UNUSED
)
490 struct strbuf err
= STRBUF_INIT
;
491 struct object_id old_oid
;
492 char *refname
, *old_target
;
494 if (!(update_flags
& REF_NO_DEREF
))
495 die("symref-verify: cannot operate with deref mode");
497 refname
= parse_refname(&next
);
499 die("symref-verify: missing <ref>");
502 * old_ref is optional, if not provided, we need to ensure that the
505 old_target
= parse_next_refname(&next
);
507 oidcpy(&old_oid
, null_oid(the_hash_algo
));
509 if (*next
!= line_termination
)
510 die("symref-verify %s: extra input: %s", refname
, next
);
512 if (ref_transaction_verify(transaction
, refname
,
513 old_target
? NULL
: &old_oid
,
514 old_target
, update_flags
, &err
))
517 update_flags
= default_flags
;
520 strbuf_release(&err
);
523 static void report_ok(const char *command
)
525 fprintf(stdout
, "%s: ok\n", command
);
529 static void parse_cmd_option(struct ref_transaction
*transaction UNUSED
,
530 const char *next
, const char *end UNUSED
)
533 if (skip_prefix(next
, "no-deref", &rest
) && *rest
== line_termination
)
534 update_flags
|= REF_NO_DEREF
;
536 die("option unknown: %s", next
);
539 static void parse_cmd_start(struct ref_transaction
*transaction UNUSED
,
540 const char *next
, const char *end UNUSED
)
542 if (*next
!= line_termination
)
543 die("start: extra input: %s", next
);
547 static void parse_cmd_prepare(struct ref_transaction
*transaction
,
548 const char *next
, const char *end UNUSED
)
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");
558 static void parse_cmd_abort(struct ref_transaction
*transaction
,
559 const char *next
, const char *end UNUSED
)
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
);
569 static 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
)
577 struct strbuf sb
= STRBUF_INIT
;
578 const char *reason
= "";
581 case REF_TRANSACTION_ERROR_NAME_CONFLICT
:
582 reason
= "refname conflict";
584 case REF_TRANSACTION_ERROR_CREATE_EXISTS
:
585 reason
= "reference already exists";
587 case REF_TRANSACTION_ERROR_NONEXISTENT_REF
:
588 reason
= "reference does not exist";
590 case REF_TRANSACTION_ERROR_INCORRECT_OLD_VALUE
:
591 reason
= "incorrect old value provided";
593 case REF_TRANSACTION_ERROR_INVALID_NEW_VALUE
:
594 reason
= "invalid new value provided";
596 case REF_TRANSACTION_ERROR_EXPECTED_SYMREF
:
597 reason
= "expected symref but found regular ref";
600 reason
= "unkown failure";
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
,
608 fwrite(sb
.buf
, sb
.len
, 1, stdout
);
612 static void parse_cmd_commit(struct ref_transaction
*transaction
,
613 const char *next
, const char *end UNUSED
)
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
);
621 ref_transaction_for_each_rejected_update(transaction
,
622 print_rejected_refs
, NULL
);
625 ref_transaction_free(transaction
);
628 enum update_refs_state
{
629 /* Non-transactional state open for updates. */
631 /* A transaction has been started. */
633 /* References are locked and ready for commit */
634 UPDATE_REFS_PREPARED
,
635 /* Transaction has been committed or closed. */
639 static const struct parse_cmd
{
641 void (*fn
)(struct ref_transaction
*, const char *, const char *);
643 enum update_refs_state state
;
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
},
660 static void update_refs_stdin(unsigned int flags
)
662 struct strbuf input
= STRBUF_INIT
, err
= STRBUF_INIT
;
663 enum update_refs_state state
= UPDATE_REFS_OPEN
;
664 struct ref_transaction
*transaction
;
667 transaction
= ref_store_transaction_begin(get_main_ref_store(the_repository
),
672 /* Read each line dispatch its command */
673 while (!strbuf_getwholeline(&input
, stdin
, line_termination
)) {
674 const struct parse_cmd
*cmd
= NULL
;
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
);
681 for (i
= 0; i
< ARRAY_SIZE(command
); i
++) {
682 const char *prefix
= command
[i
].prefix
;
685 if (!starts_with(input
.buf
, prefix
))
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.
693 c
= command
[i
].args
? ' ' : line_termination
;
694 if (input
.buf
[strlen(prefix
)] != c
)
701 die("unknown command: %s", input
.buf
);
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.
708 for (j
= 1; line_termination
== '\0' && j
< cmd
->args
; j
++)
709 if (strbuf_appendwholeline(&input
, stdin
, line_termination
))
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
)
721 case UPDATE_REFS_PREPARED
:
722 if (cmd
->state
!= UPDATE_REFS_CLOSED
)
723 die("prepared transactions can only be closed");
726 case UPDATE_REFS_CLOSED
:
727 if (cmd
->state
!= UPDATE_REFS_STARTED
)
728 die("transaction is closed");
731 * Open a new transaction if we're currently closed and
735 transaction
= ref_store_transaction_begin(get_main_ref_store(the_repository
),
743 cmd
->fn(transaction
, input
.buf
+ strlen(cmd
->prefix
) + !!cmd
->args
,
744 input
.buf
+ input
.len
);
748 case UPDATE_REFS_OPEN
:
749 /* Commit by default if no transaction was requested. */
750 if (ref_transaction_commit(transaction
, &err
))
752 ref_transaction_for_each_rejected_update(transaction
,
753 print_rejected_refs
, NULL
);
754 ref_transaction_free(transaction
);
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
))
762 case UPDATE_REFS_CLOSED
:
763 /* Otherwise no need to do anything, the transaction was closed already. */
767 strbuf_release(&err
);
768 strbuf_release(&input
);
771 int cmd_update_ref(int argc
,
774 struct repository
*repo UNUSED
)
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;
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
),
795 git_config(git_default_config
, NULL
);
796 argc
= parse_options(argc
, argv
, prefix
, options
, git_update_ref_usage
,
799 die("Refusing to perform update with empty message.");
801 create_reflog_flag
= create_reflog
? REF_FORCE_CREATE_REFLOG
: 0;
804 default_flags
= REF_NO_DEREF
;
805 update_flags
= default_flags
;
809 if (delete || argc
> 0)
810 usage_with_options(git_update_ref_usage
, options
);
812 line_termination
= '\0';
813 update_refs_stdin(flags
);
815 } else if (flags
& REF_TRANSACTION_ALLOW_FAILURE
) {
816 die("--batch-updates can only be used with --stdin");
820 usage_with_options(git_update_ref_usage
, options
);
823 if (argc
< 1 || argc
> 2)
824 usage_with_options(git_update_ref_usage
, options
);
829 if (argc
< 2 || argc
> 3)
830 usage_with_options(git_update_ref_usage
, options
);
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
);
842 * The empty string implies that the reference
843 * must not already exist:
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
);
853 * For purposes of backwards compatibility, we treat
854 * NULL_SHA1 as "don't care" here:
856 return refs_delete_ref(get_main_ref_store(the_repository
),
858 (oldval
&& !is_null_oid(&oldoid
)) ? &oldoid
: NULL
,
861 return refs_update_ref(get_main_ref_store(the_repository
),
863 oldval
? &oldoid
: NULL
,
864 default_flags
| create_reflog_flag
,
865 UPDATE_REFS_DIE_ON_ERR
);