1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
6 #include "environment.h"
11 #include "object-name.h"
12 #include "parse-options.h"
15 static const char * const git_update_ref_usage
[] = {
16 N_("git update-ref [<options>] -d <refname> [<old-oid>]"),
17 N_("git update-ref [<options>] <refname> <new-oid> [<old-oid>]"),
18 N_("git update-ref [<options>] --stdin [-z] [--batch-updates]"),
22 static char line_termination
= '\n';
23 static unsigned int update_flags
;
24 static unsigned int default_flags
;
25 static unsigned create_reflog_flag
;
26 static const char *msg
;
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.
34 static const char *parse_arg(const char *next
, struct strbuf
*arg
)
37 const char *orig
= next
;
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
);
44 while (*next
&& !isspace(*next
))
45 strbuf_addch(arg
, *next
++);
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
59 static char *parse_refname(const char **next
)
61 struct strbuf ref
= STRBUF_INIT
;
63 if (line_termination
) {
64 /* Without -z, use the next argument */
65 *next
= parse_arg(*next
, &ref
);
67 /* With -z, use everything up to the next NUL */
68 strbuf_addstr(&ref
, *next
);
77 if (check_refname_format(ref
.buf
, REFNAME_ALLOW_ONELEVEL
))
78 die("invalid ref format: %s", ref
.buf
);
80 return strbuf_detach(&ref
, NULL
);
84 * Wrapper around parse_refname which skips the next delimiter.
86 static char *parse_next_refname(const char **next
)
88 if (line_termination
) {
89 /* Without -z, consume SP and use next argument */
90 if (!**next
|| **next
== line_termination
)
93 die("expected SP but got: %s", *next
);
95 /* With -z, read the next NUL-terminated line */
99 /* Skip the delimiter */
102 return parse_refname(next
);
106 * Wrapper around parse_arg which skips the next delimiter.
108 static char *parse_next_arg(const char **next
)
110 struct strbuf arg
= STRBUF_INIT
;
112 if (line_termination
) {
113 /* Without -z, consume SP and use next argument */
114 if (!**next
|| **next
== line_termination
)
117 die("expected SP but got: %s", *next
);
119 /* With -z, read the next NUL-terminated line */
123 /* Skip the delimiter */
126 if (line_termination
) {
127 /* Without -z, use the next argument */
128 *next
= parse_arg(*next
, &arg
);
130 /* With -z, use everything up to the next NUL */
131 strbuf_addstr(&arg
, *next
);
136 return strbuf_detach(&arg
, NULL
);
138 strbuf_release(&arg
);
143 * The value being parsed is <old-oid> (as opposed to <new-oid>; the
144 * difference affects which error messages are generated):
146 #define PARSE_SHA1_OLD 0x01
149 * For backwards compatibility, accept an empty string for update's
150 * <new-oid> in binary mode to be equivalent to specifying zeros.
152 #define PARSE_SHA1_ALLOW_EMPTY 0x02
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
158 * return 0. If there is no argument at all (not even the empty
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.
163 static int parse_next_oid(const char **next
, const char *end
,
164 struct object_id
*oid
,
165 const char *command
, const char *refname
,
168 struct strbuf arg
= STRBUF_INIT
;
174 if (line_termination
) {
175 /* Without -z, consume SP and use next argument */
176 if (!**next
|| **next
== line_termination
)
179 die("%s %s: expected SP but got: %s",
180 command
, refname
, *next
);
182 *next
= parse_arg(*next
, &arg
);
184 if (repo_get_oid_with_flags(the_repository
, arg
.buf
, oid
,
185 GET_OID_SKIP_AMBIGUITY_CHECK
))
188 /* Without -z, an empty value means all zeros: */
189 oidclr(oid
, the_repository
->hash_algo
);
192 /* With -z, read the next NUL-terminated line */
194 die("%s %s: expected NUL but got: %s",
195 command
, refname
, *next
);
199 strbuf_addstr(&arg
, *next
);
203 if (repo_get_oid_with_flags(the_repository
, arg
.buf
, oid
,
204 GET_OID_SKIP_AMBIGUITY_CHECK
))
206 } else if (flags
& PARSE_SHA1_ALLOW_EMPTY
) {
207 /* With -z, treat an empty value as all zeros: */
208 warning("%s %s: missing <new-oid>, treating as zero",
210 oidclr(oid
, the_repository
->hash_algo
);
213 * With -z, an empty non-required value means
220 strbuf_release(&arg
);
225 die(flags
& PARSE_SHA1_OLD
?
226 "%s %s: invalid <old-oid>: %s" :
227 "%s %s: invalid <new-oid>: %s",
228 command
, refname
, arg
.buf
);
231 die(flags
& PARSE_SHA1_OLD
?
232 "%s %s: unexpected end of input when reading <old-oid>" :
233 "%s %s: unexpected end of input when reading <new-oid>",
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.
248 static void parse_cmd_update(struct ref_transaction
*transaction
,
249 const char *next
, const char *end
)
251 struct strbuf err
= STRBUF_INIT
;
253 struct object_id new_oid
, old_oid
;
256 refname
= parse_refname(&next
);
258 die("update: missing <ref>");
260 if (parse_next_oid(&next
, end
, &new_oid
, "update", refname
,
261 PARSE_SHA1_ALLOW_EMPTY
))
262 die("update %s: missing <new-oid>", refname
);
264 have_old
= !parse_next_oid(&next
, end
, &old_oid
, "update", refname
,
267 if (*next
!= line_termination
)
268 die("update %s: extra input: %s", refname
, next
);
270 if (ref_transaction_update(transaction
, refname
,
271 &new_oid
, have_old
? &old_oid
: NULL
,
273 update_flags
| create_reflog_flag
,
277 update_flags
= default_flags
;
279 strbuf_release(&err
);
282 static void parse_cmd_symref_update(struct ref_transaction
*transaction
,
283 const char *next
, const char *end UNUSED
)
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;
291 refname
= parse_refname(&next
);
293 die("symref-update: missing <ref>");
295 new_target
= parse_next_refname(&next
);
297 die("symref-update %s: missing <new-target>", refname
);
299 old_arg
= parse_next_arg(&next
);
301 old_target
= parse_next_arg(&next
);
303 die("symref-update %s: expected old value", refname
);
305 if (!strcmp(old_arg
, "oid")) {
306 if (repo_get_oid_with_flags(the_repository
, old_target
, &old_oid
,
307 GET_OID_SKIP_AMBIGUITY_CHECK
))
308 die("symref-update %s: invalid oid: %s", refname
, old_target
);
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
);
315 die("symref-update %s: invalid arg '%s' for old value", refname
, old_arg
);
319 if (*next
!= line_termination
)
320 die("symref-update %s: extra input: %s", refname
, next
);
322 if (ref_transaction_update(transaction
, refname
, NULL
,
323 have_old_oid
? &old_oid
: NULL
,
325 have_old_oid
? NULL
: old_target
,
326 update_flags
| create_reflog_flag
,
330 update_flags
= default_flags
;
335 strbuf_release(&err
);
338 static void parse_cmd_create(struct ref_transaction
*transaction
,
339 const char *next
, const char *end
)
341 struct strbuf err
= STRBUF_INIT
;
343 struct object_id new_oid
;
345 refname
= parse_refname(&next
);
347 die("create: missing <ref>");
349 if (parse_next_oid(&next
, end
, &new_oid
, "create", refname
, 0))
350 die("create %s: missing <new-oid>", refname
);
352 if (is_null_oid(&new_oid
))
353 die("create %s: zero <new-oid>", refname
);
355 if (*next
!= line_termination
)
356 die("create %s: extra input: %s", refname
, next
);
358 if (ref_transaction_create(transaction
, refname
, &new_oid
, NULL
,
359 update_flags
| create_reflog_flag
,
363 update_flags
= default_flags
;
365 strbuf_release(&err
);
369 static void parse_cmd_symref_create(struct ref_transaction
*transaction
,
370 const char *next
, const char *end UNUSED
)
372 struct strbuf err
= STRBUF_INIT
;
373 char *refname
, *new_target
;
375 refname
= parse_refname(&next
);
377 die("symref-create: missing <ref>");
379 new_target
= parse_next_refname(&next
);
381 die("symref-create %s: missing <new-target>", refname
);
383 if (*next
!= line_termination
)
384 die("symref-create %s: extra input: %s", refname
, next
);
386 if (ref_transaction_create(transaction
, refname
, NULL
, new_target
,
387 update_flags
| create_reflog_flag
,
391 update_flags
= default_flags
;
394 strbuf_release(&err
);
397 static void parse_cmd_delete(struct ref_transaction
*transaction
,
398 const char *next
, const char *end
)
400 struct strbuf err
= STRBUF_INIT
;
402 struct object_id old_oid
;
405 refname
= parse_refname(&next
);
407 die("delete: missing <ref>");
409 if (parse_next_oid(&next
, end
, &old_oid
, "delete", refname
,
413 if (is_null_oid(&old_oid
))
414 die("delete %s: zero <old-oid>", refname
);
418 if (*next
!= line_termination
)
419 die("delete %s: extra input: %s", refname
, next
);
421 if (ref_transaction_delete(transaction
, refname
,
422 have_old
? &old_oid
: NULL
,
423 NULL
, update_flags
, msg
, &err
))
426 update_flags
= default_flags
;
428 strbuf_release(&err
);
432 static void parse_cmd_symref_delete(struct ref_transaction
*transaction
,
433 const char *next
, const char *end UNUSED
)
435 struct strbuf err
= STRBUF_INIT
;
436 char *refname
, *old_target
;
438 if (!(update_flags
& REF_NO_DEREF
))
439 die("symref-delete: cannot operate with deref mode");
441 refname
= parse_refname(&next
);
443 die("symref-delete: missing <ref>");
445 old_target
= parse_next_refname(&next
);
447 if (*next
!= line_termination
)
448 die("symref-delete %s: extra input: %s", refname
, next
);
450 if (ref_transaction_delete(transaction
, refname
, NULL
,
451 old_target
, update_flags
, msg
, &err
))
454 update_flags
= default_flags
;
457 strbuf_release(&err
);
461 static void parse_cmd_verify(struct ref_transaction
*transaction
,
462 const char *next
, const char *end
)
464 struct strbuf err
= STRBUF_INIT
;
466 struct object_id old_oid
;
468 refname
= parse_refname(&next
);
470 die("verify: missing <ref>");
472 if (parse_next_oid(&next
, end
, &old_oid
, "verify", refname
,
474 oidclr(&old_oid
, the_repository
->hash_algo
);
476 if (*next
!= line_termination
)
477 die("verify %s: extra input: %s", refname
, next
);
479 if (ref_transaction_verify(transaction
, refname
, &old_oid
,
480 NULL
, update_flags
, &err
))
483 update_flags
= default_flags
;
485 strbuf_release(&err
);
488 static void parse_cmd_symref_verify(struct ref_transaction
*transaction
,
489 const char *next
, const char *end UNUSED
)
491 struct strbuf err
= STRBUF_INIT
;
492 struct object_id old_oid
;
493 char *refname
, *old_target
;
495 if (!(update_flags
& REF_NO_DEREF
))
496 die("symref-verify: cannot operate with deref mode");
498 refname
= parse_refname(&next
);
500 die("symref-verify: missing <ref>");
503 * old_ref is optional, if not provided, we need to ensure that the
506 old_target
= parse_next_refname(&next
);
508 oidcpy(&old_oid
, null_oid(the_hash_algo
));
510 if (*next
!= line_termination
)
511 die("symref-verify %s: extra input: %s", refname
, next
);
513 if (ref_transaction_verify(transaction
, refname
,
514 old_target
? NULL
: &old_oid
,
515 old_target
, update_flags
, &err
))
518 update_flags
= default_flags
;
521 strbuf_release(&err
);
524 static void report_ok(const char *command
)
526 fprintf(stdout
, "%s: ok\n", command
);
530 static void parse_cmd_option(struct ref_transaction
*transaction UNUSED
,
531 const char *next
, const char *end UNUSED
)
534 if (skip_prefix(next
, "no-deref", &rest
) && *rest
== line_termination
)
535 update_flags
|= REF_NO_DEREF
;
537 die("option unknown: %s", next
);
540 static void parse_cmd_start(struct ref_transaction
*transaction UNUSED
,
541 const char *next
, const char *end UNUSED
)
543 if (*next
!= line_termination
)
544 die("start: extra input: %s", next
);
548 static void parse_cmd_prepare(struct ref_transaction
*transaction
,
549 const char *next
, const char *end UNUSED
)
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
);
556 report_ok("prepare");
559 static void parse_cmd_abort(struct ref_transaction
*transaction
,
560 const char *next
, const char *end UNUSED
)
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
);
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
)
578 struct strbuf sb
= STRBUF_INIT
;
579 const char *reason
= ref_transaction_error_msg(err
);
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
,
586 fwrite(sb
.buf
, sb
.len
, 1, stdout
);
590 static void parse_cmd_commit(struct ref_transaction
*transaction
,
591 const char *next
, const char *end UNUSED
)
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
);
599 ref_transaction_for_each_rejected_update(transaction
,
600 print_rejected_refs
, NULL
);
603 ref_transaction_free(transaction
);
606 enum update_refs_state
{
607 /* Non-transactional state open for updates. */
609 /* A transaction has been started. */
611 /* References are locked and ready for commit */
612 UPDATE_REFS_PREPARED
,
613 /* Transaction has been committed or closed. */
617 static const struct parse_cmd
{
619 void (*fn
)(struct ref_transaction
*, const char *, const char *);
621 enum update_refs_state state
;
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
},
627 { "symref-update", parse_cmd_symref_update
, 4, UPDATE_REFS_OPEN
},
628 { "symref-create", parse_cmd_symref_create
, 2, UPDATE_REFS_OPEN
},
629 { "symref-delete", parse_cmd_symref_delete
, 2, UPDATE_REFS_OPEN
},
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
},
638 static void update_refs_stdin(unsigned int flags
)
640 struct strbuf input
= STRBUF_INIT
, err
= STRBUF_INIT
;
641 enum update_refs_state state
= UPDATE_REFS_OPEN
;
642 struct ref_transaction
*transaction
;
645 transaction
= ref_store_transaction_begin(get_main_ref_store(the_repository
),
650 /* Read each line dispatch its command */
651 while (!strbuf_getwholeline(&input
, stdin
, line_termination
)) {
652 const struct parse_cmd
*cmd
= NULL
;
654 if (*input
.buf
== line_termination
)
655 die("empty command in input");
656 else if (isspace(*input
.buf
))
657 die("whitespace before command: %s", input
.buf
);
659 for (i
= 0; i
< ARRAY_SIZE(command
); i
++) {
660 const char *prefix
= command
[i
].prefix
;
663 if (!starts_with(input
.buf
, prefix
))
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.
671 c
= command
[i
].args
? ' ' : line_termination
;
672 if (input
.buf
[strlen(prefix
)] != c
)
679 die("unknown command: %s", input
.buf
);
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.
686 for (j
= 1; line_termination
== '\0' && j
< cmd
->args
; j
++)
687 if (strbuf_appendwholeline(&input
, stdin
, line_termination
))
691 case UPDATE_REFS_OPEN
:
692 case UPDATE_REFS_STARTED
:
693 if (state
== UPDATE_REFS_STARTED
&& cmd
->state
== UPDATE_REFS_STARTED
)
694 die("cannot restart ongoing transaction");
695 /* Do not downgrade a transaction to a non-transaction. */
696 if (cmd
->state
>= state
)
699 case UPDATE_REFS_PREPARED
:
700 if (cmd
->state
!= UPDATE_REFS_CLOSED
)
701 die("prepared transactions can only be closed");
704 case UPDATE_REFS_CLOSED
:
705 if (cmd
->state
!= UPDATE_REFS_STARTED
)
706 die("transaction is closed");
709 * Open a new transaction if we're currently closed and
713 transaction
= ref_store_transaction_begin(get_main_ref_store(the_repository
),
721 cmd
->fn(transaction
, input
.buf
+ strlen(cmd
->prefix
) + !!cmd
->args
,
722 input
.buf
+ input
.len
);
726 case UPDATE_REFS_OPEN
:
727 /* Commit by default if no transaction was requested. */
728 if (ref_transaction_commit(transaction
, &err
))
730 ref_transaction_for_each_rejected_update(transaction
,
731 print_rejected_refs
, NULL
);
732 ref_transaction_free(transaction
);
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
))
740 case UPDATE_REFS_CLOSED
:
741 /* Otherwise no need to do anything, the transaction was closed already. */
745 strbuf_release(&err
);
746 strbuf_release(&input
);
749 int cmd_update_ref(int argc
,
752 struct repository
*repo UNUSED
)
754 const char *refname
, *oldval
;
755 struct object_id oid
, oldoid
;
756 int delete = 0, no_deref
= 0, read_stdin
= 0, end_null
= 0;
757 int create_reflog
= 0;
758 unsigned int flags
= 0;
760 struct option options
[] = {
761 OPT_STRING( 'm', NULL
, &msg
, N_("reason"), N_("reason of the update")),
762 OPT_BOOL('d', NULL
, &delete, N_("delete the reference")),
763 OPT_BOOL( 0 , "no-deref", &no_deref
,
764 N_("update <refname> not the one it points to")),
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")),
767 OPT_BOOL( 0 , "create-reflog", &create_reflog
, N_("create a reflog")),
768 OPT_BIT('0', "batch-updates", &flags
, N_("batch reference updates"),
769 REF_TRANSACTION_ALLOW_FAILURE
),
773 repo_config(the_repository
, git_default_config
, NULL
);
774 argc
= parse_options(argc
, argv
, prefix
, options
, git_update_ref_usage
,
777 die("Refusing to perform update with empty message.");
779 create_reflog_flag
= create_reflog
? REF_FORCE_CREATE_REFLOG
: 0;
782 default_flags
= REF_NO_DEREF
;
783 update_flags
= default_flags
;
787 if (delete || argc
> 0)
788 usage_with_options(git_update_ref_usage
, options
);
790 line_termination
= '\0';
791 update_refs_stdin(flags
);
793 } else if (flags
& REF_TRANSACTION_ALLOW_FAILURE
) {
794 die("--batch-updates can only be used with --stdin");
798 usage_with_options(git_update_ref_usage
, options
);
801 if (argc
< 1 || argc
> 2)
802 usage_with_options(git_update_ref_usage
, options
);
807 if (argc
< 2 || argc
> 3)
808 usage_with_options(git_update_ref_usage
, options
);
812 if (repo_get_oid_with_flags(the_repository
, value
, &oid
,
813 GET_OID_SKIP_AMBIGUITY_CHECK
))
814 die("%s: not a valid SHA1", value
);
820 * The empty string implies that the reference
821 * must not already exist:
823 oidclr(&oldoid
, the_repository
->hash_algo
);
824 else if (repo_get_oid_with_flags(the_repository
, oldval
, &oldoid
,
825 GET_OID_SKIP_AMBIGUITY_CHECK
))
826 die("%s: not a valid old SHA1", oldval
);
831 * For purposes of backwards compatibility, we treat
832 * NULL_SHA1 as "don't care" here:
834 return refs_delete_ref(get_main_ref_store(the_repository
),
836 (oldval
&& !is_null_oid(&oldoid
)) ? &oldoid
: NULL
,
839 return refs_update_ref(get_main_ref_store(the_repository
),
841 oldval
? &oldoid
: NULL
,
842 default_flags
| create_reflog_flag
,
843 UPDATE_REFS_DIE_ON_ERR
);