6 #include "object-name.h"
7 #include "parse-options.h"
9 #include "repository.h"
11 static const char * const git_update_ref_usage
[] = {
12 N_("git update-ref [<options>] -d <refname> [<old-oid>]"),
13 N_("git update-ref [<options>] <refname> <new-oid> [<old-oid>]"),
14 N_("git update-ref [<options>] --stdin [-z]"),
18 static char line_termination
= '\n';
19 static unsigned int update_flags
;
20 static unsigned int default_flags
;
21 static unsigned create_reflog_flag
;
22 static const char *msg
;
25 * Parse one whitespace- or NUL-terminated, possibly C-quoted argument
26 * and append the result to arg. Return a pointer to the terminator.
27 * Die if there is an error in how the argument is C-quoted. This
28 * function is only used if not -z.
30 static const char *parse_arg(const char *next
, struct strbuf
*arg
)
33 const char *orig
= next
;
35 if (unquote_c_style(arg
, next
, &next
))
36 die("badly quoted argument: %s", orig
);
37 if (*next
&& !isspace(*next
))
38 die("unexpected character after quoted argument: %s", orig
);
40 while (*next
&& !isspace(*next
))
41 strbuf_addch(arg
, *next
++);
48 * Parse the reference name immediately after "command SP". If not
49 * -z, then handle C-quoting. Return a pointer to a newly allocated
50 * string containing the name of the reference, or NULL if there was
51 * an error. Update *next to point at the character that terminates
52 * the argument. Die if C-quoting is malformed or the reference name
55 static char *parse_refname(const char **next
)
57 struct strbuf ref
= STRBUF_INIT
;
59 if (line_termination
) {
60 /* Without -z, use the next argument */
61 *next
= parse_arg(*next
, &ref
);
63 /* With -z, use everything up to the next NUL */
64 strbuf_addstr(&ref
, *next
);
73 if (check_refname_format(ref
.buf
, REFNAME_ALLOW_ONELEVEL
))
74 die("invalid ref format: %s", ref
.buf
);
76 return strbuf_detach(&ref
, NULL
);
80 * Wrapper around parse_refname which skips the next delimiter.
82 static char *parse_next_refname(const char **next
)
84 if (line_termination
) {
85 /* Without -z, consume SP and use next argument */
86 if (!**next
|| **next
== line_termination
)
89 die("expected SP but got: %s", *next
);
91 /* With -z, read the next NUL-terminated line */
95 /* Skip the delimiter */
98 return parse_refname(next
);
102 * Wrapper around parse_arg which skips the next delimiter.
104 static char *parse_next_arg(const char **next
)
106 struct strbuf arg
= STRBUF_INIT
;
108 if (line_termination
) {
109 /* Without -z, consume SP and use next argument */
110 if (!**next
|| **next
== line_termination
)
113 die("expected SP but got: %s", *next
);
115 /* With -z, read the next NUL-terminated line */
119 /* Skip the delimiter */
122 if (line_termination
) {
123 /* Without -z, use the next argument */
124 *next
= parse_arg(*next
, &arg
);
126 /* With -z, use everything up to the next NUL */
127 strbuf_addstr(&arg
, *next
);
132 return strbuf_detach(&arg
, NULL
);
134 strbuf_release(&arg
);
139 * The value being parsed is <old-oid> (as opposed to <new-oid>; the
140 * difference affects which error messages are generated):
142 #define PARSE_SHA1_OLD 0x01
145 * For backwards compatibility, accept an empty string for update's
146 * <new-oid> in binary mode to be equivalent to specifying zeros.
148 #define PARSE_SHA1_ALLOW_EMPTY 0x02
151 * Parse an argument separator followed by the next argument, if any.
152 * If there is an argument, convert it to a SHA-1, write it to sha1,
153 * set *next to point at the character terminating the argument, and
154 * return 0. If there is no argument at all (not even the empty
155 * string), return 1 and leave *next unchanged. If the value is
156 * provided but cannot be converted to a SHA-1, die. flags can
157 * include PARSE_SHA1_OLD and/or PARSE_SHA1_ALLOW_EMPTY.
159 static int parse_next_oid(const char **next
, const char *end
,
160 struct object_id
*oid
,
161 const char *command
, const char *refname
,
164 struct strbuf arg
= STRBUF_INIT
;
170 if (line_termination
) {
171 /* Without -z, consume SP and use next argument */
172 if (!**next
|| **next
== line_termination
)
175 die("%s %s: expected SP but got: %s",
176 command
, refname
, *next
);
178 *next
= parse_arg(*next
, &arg
);
180 if (repo_get_oid(the_repository
, arg
.buf
, oid
))
183 /* Without -z, an empty value means all zeros: */
187 /* With -z, read the next NUL-terminated line */
189 die("%s %s: expected NUL but got: %s",
190 command
, refname
, *next
);
194 strbuf_addstr(&arg
, *next
);
198 if (repo_get_oid(the_repository
, arg
.buf
, oid
))
200 } else if (flags
& PARSE_SHA1_ALLOW_EMPTY
) {
201 /* With -z, treat an empty value as all zeros: */
202 warning("%s %s: missing <new-oid>, treating as zero",
207 * With -z, an empty non-required value means
214 strbuf_release(&arg
);
219 die(flags
& PARSE_SHA1_OLD
?
220 "%s %s: invalid <old-oid>: %s" :
221 "%s %s: invalid <new-oid>: %s",
222 command
, refname
, arg
.buf
);
225 die(flags
& PARSE_SHA1_OLD
?
226 "%s %s: unexpected end of input when reading <old-oid>" :
227 "%s %s: unexpected end of input when reading <new-oid>",
233 * The following five parse_cmd_*() functions parse the corresponding
234 * command. In each case, next points at the character following the
235 * command name and the following space. They each return a pointer
236 * to the character terminating the command, and die with an
237 * explanatory message if there are any parsing problems. All of
238 * these functions handle either text or binary format input,
239 * depending on how line_termination is set.
242 static void parse_cmd_update(struct ref_transaction
*transaction
,
243 const char *next
, const char *end
)
245 struct strbuf err
= STRBUF_INIT
;
247 struct object_id new_oid
, old_oid
;
250 refname
= parse_refname(&next
);
252 die("update: missing <ref>");
254 if (parse_next_oid(&next
, end
, &new_oid
, "update", refname
,
255 PARSE_SHA1_ALLOW_EMPTY
))
256 die("update %s: missing <new-oid>", refname
);
258 have_old
= !parse_next_oid(&next
, end
, &old_oid
, "update", refname
,
261 if (*next
!= line_termination
)
262 die("update %s: extra input: %s", refname
, next
);
264 if (ref_transaction_update(transaction
, refname
,
265 &new_oid
, have_old
? &old_oid
: NULL
,
267 update_flags
| create_reflog_flag
,
271 update_flags
= default_flags
;
273 strbuf_release(&err
);
276 static void parse_cmd_symref_update(struct ref_transaction
*transaction
,
277 const char *next
, const char *end
)
279 char *refname
, *new_target
, *old_arg
;
280 char *old_target
= NULL
;
281 struct strbuf err
= STRBUF_INIT
;
282 struct object_id old_oid
;
283 int have_old_oid
= 0;
285 refname
= parse_refname(&next
);
287 die("symref-update: missing <ref>");
289 new_target
= parse_next_refname(&next
);
291 die("symref-update %s: missing <new-target>", refname
);
293 old_arg
= parse_next_arg(&next
);
295 old_target
= parse_next_arg(&next
);
297 die("symref-update %s: expected old value", refname
);
299 if (!strcmp(old_arg
, "oid")) {
300 if (repo_get_oid(the_repository
, old_target
, &old_oid
))
301 die("symref-update %s: invalid oid: %s", refname
, old_target
);
304 } else if (!strcmp(old_arg
, "ref")) {
305 if (check_refname_format(old_target
, REFNAME_ALLOW_ONELEVEL
))
306 die("symref-update %s: invalid ref: %s", refname
, old_target
);
308 die("symref-update %s: invalid arg '%s' for old value", refname
, old_arg
);
312 if (*next
!= line_termination
)
313 die("symref-update %s: extra input: %s", refname
, next
);
315 if (ref_transaction_update(transaction
, refname
, NULL
,
316 have_old_oid
? &old_oid
: NULL
,
318 have_old_oid
? NULL
: old_target
,
319 update_flags
| create_reflog_flag
,
323 update_flags
= default_flags
;
328 strbuf_release(&err
);
331 static void parse_cmd_create(struct ref_transaction
*transaction
,
332 const char *next
, const char *end
)
334 struct strbuf err
= STRBUF_INIT
;
336 struct object_id new_oid
;
338 refname
= parse_refname(&next
);
340 die("create: missing <ref>");
342 if (parse_next_oid(&next
, end
, &new_oid
, "create", refname
, 0))
343 die("create %s: missing <new-oid>", refname
);
345 if (is_null_oid(&new_oid
))
346 die("create %s: zero <new-oid>", refname
);
348 if (*next
!= line_termination
)
349 die("create %s: extra input: %s", refname
, next
);
351 if (ref_transaction_create(transaction
, refname
, &new_oid
, NULL
,
352 update_flags
| create_reflog_flag
,
356 update_flags
= default_flags
;
358 strbuf_release(&err
);
362 static void parse_cmd_symref_create(struct ref_transaction
*transaction
,
363 const char *next
, const char *end
)
365 struct strbuf err
= STRBUF_INIT
;
366 char *refname
, *new_target
;
368 refname
= parse_refname(&next
);
370 die("symref-create: missing <ref>");
372 new_target
= parse_next_refname(&next
);
374 die("symref-create %s: missing <new-target>", refname
);
376 if (*next
!= line_termination
)
377 die("symref-create %s: extra input: %s", refname
, next
);
379 if (ref_transaction_create(transaction
, refname
, NULL
, new_target
,
380 update_flags
| create_reflog_flag
,
384 update_flags
= default_flags
;
387 strbuf_release(&err
);
390 static void parse_cmd_delete(struct ref_transaction
*transaction
,
391 const char *next
, const char *end
)
393 struct strbuf err
= STRBUF_INIT
;
395 struct object_id old_oid
;
398 refname
= parse_refname(&next
);
400 die("delete: missing <ref>");
402 if (parse_next_oid(&next
, end
, &old_oid
, "delete", refname
,
406 if (is_null_oid(&old_oid
))
407 die("delete %s: zero <old-oid>", refname
);
411 if (*next
!= line_termination
)
412 die("delete %s: extra input: %s", refname
, next
);
414 if (ref_transaction_delete(transaction
, refname
,
415 have_old
? &old_oid
: NULL
,
416 NULL
, update_flags
, msg
, &err
))
419 update_flags
= default_flags
;
421 strbuf_release(&err
);
425 static void parse_cmd_symref_delete(struct ref_transaction
*transaction
,
426 const char *next
, const char *end
)
428 struct strbuf err
= STRBUF_INIT
;
429 char *refname
, *old_target
;
431 if (!(update_flags
& REF_NO_DEREF
))
432 die("symref-delete: cannot operate with deref mode");
434 refname
= parse_refname(&next
);
436 die("symref-delete: missing <ref>");
438 old_target
= parse_next_refname(&next
);
440 if (*next
!= line_termination
)
441 die("symref-delete %s: extra input: %s", refname
, next
);
443 if (ref_transaction_delete(transaction
, refname
, NULL
,
444 old_target
, update_flags
, msg
, &err
))
447 update_flags
= default_flags
;
450 strbuf_release(&err
);
454 static void parse_cmd_verify(struct ref_transaction
*transaction
,
455 const char *next
, const char *end
)
457 struct strbuf err
= STRBUF_INIT
;
459 struct object_id old_oid
;
461 refname
= parse_refname(&next
);
463 die("verify: missing <ref>");
465 if (parse_next_oid(&next
, end
, &old_oid
, "verify", refname
,
469 if (*next
!= line_termination
)
470 die("verify %s: extra input: %s", refname
, next
);
472 if (ref_transaction_verify(transaction
, refname
, &old_oid
,
473 NULL
, update_flags
, &err
))
476 update_flags
= default_flags
;
478 strbuf_release(&err
);
481 static void parse_cmd_symref_verify(struct ref_transaction
*transaction
,
482 const char *next
, const char *end
)
484 struct strbuf err
= STRBUF_INIT
;
485 struct object_id old_oid
;
486 char *refname
, *old_target
;
488 if (!(update_flags
& REF_NO_DEREF
))
489 die("symref-verify: cannot operate with deref mode");
491 refname
= parse_refname(&next
);
493 die("symref-verify: missing <ref>");
496 * old_ref is optional, if not provided, we need to ensure that the
499 old_target
= parse_next_refname(&next
);
501 oidcpy(&old_oid
, null_oid());
503 if (*next
!= line_termination
)
504 die("symref-verify %s: extra input: %s", refname
, next
);
506 if (ref_transaction_verify(transaction
, refname
,
507 old_target
? NULL
: &old_oid
,
508 old_target
, update_flags
, &err
))
511 update_flags
= default_flags
;
514 strbuf_release(&err
);
517 static void report_ok(const char *command
)
519 fprintf(stdout
, "%s: ok\n", command
);
523 static void parse_cmd_option(struct ref_transaction
*transaction UNUSED
,
524 const char *next
, const char *end UNUSED
)
527 if (skip_prefix(next
, "no-deref", &rest
) && *rest
== line_termination
)
528 update_flags
|= REF_NO_DEREF
;
530 die("option unknown: %s", next
);
533 static void parse_cmd_start(struct ref_transaction
*transaction UNUSED
,
534 const char *next
, const char *end UNUSED
)
536 if (*next
!= line_termination
)
537 die("start: extra input: %s", next
);
541 static void parse_cmd_prepare(struct ref_transaction
*transaction
,
542 const char *next
, const char *end UNUSED
)
544 struct strbuf error
= STRBUF_INIT
;
545 if (*next
!= line_termination
)
546 die("prepare: extra input: %s", next
);
547 if (ref_transaction_prepare(transaction
, &error
))
548 die("prepare: %s", error
.buf
);
549 report_ok("prepare");
552 static void parse_cmd_abort(struct ref_transaction
*transaction
,
553 const char *next
, const char *end UNUSED
)
555 struct strbuf error
= STRBUF_INIT
;
556 if (*next
!= line_termination
)
557 die("abort: extra input: %s", next
);
558 if (ref_transaction_abort(transaction
, &error
))
559 die("abort: %s", error
.buf
);
563 static void parse_cmd_commit(struct ref_transaction
*transaction
,
564 const char *next
, const char *end UNUSED
)
566 struct strbuf error
= STRBUF_INIT
;
567 if (*next
!= line_termination
)
568 die("commit: extra input: %s", next
);
569 if (ref_transaction_commit(transaction
, &error
))
570 die("commit: %s", error
.buf
);
572 ref_transaction_free(transaction
);
575 enum update_refs_state
{
576 /* Non-transactional state open for updates. */
578 /* A transaction has been started. */
580 /* References are locked and ready for commit */
581 UPDATE_REFS_PREPARED
,
582 /* Transaction has been committed or closed. */
586 static const struct parse_cmd
{
588 void (*fn
)(struct ref_transaction
*, const char *, const char *);
590 enum update_refs_state state
;
592 { "update", parse_cmd_update
, 3, UPDATE_REFS_OPEN
},
593 { "create", parse_cmd_create
, 2, UPDATE_REFS_OPEN
},
594 { "delete", parse_cmd_delete
, 2, UPDATE_REFS_OPEN
},
595 { "verify", parse_cmd_verify
, 2, UPDATE_REFS_OPEN
},
596 { "symref-update", parse_cmd_symref_update
, 4, UPDATE_REFS_OPEN
},
597 { "symref-create", parse_cmd_symref_create
, 2, UPDATE_REFS_OPEN
},
598 { "symref-delete", parse_cmd_symref_delete
, 2, UPDATE_REFS_OPEN
},
599 { "symref-verify", parse_cmd_symref_verify
, 2, UPDATE_REFS_OPEN
},
600 { "option", parse_cmd_option
, 1, UPDATE_REFS_OPEN
},
601 { "start", parse_cmd_start
, 0, UPDATE_REFS_STARTED
},
602 { "prepare", parse_cmd_prepare
, 0, UPDATE_REFS_PREPARED
},
603 { "abort", parse_cmd_abort
, 0, UPDATE_REFS_CLOSED
},
604 { "commit", parse_cmd_commit
, 0, UPDATE_REFS_CLOSED
},
607 static void update_refs_stdin(void)
609 struct strbuf input
= STRBUF_INIT
, err
= STRBUF_INIT
;
610 enum update_refs_state state
= UPDATE_REFS_OPEN
;
611 struct ref_transaction
*transaction
;
614 transaction
= ref_store_transaction_begin(get_main_ref_store(the_repository
),
619 /* Read each line dispatch its command */
620 while (!strbuf_getwholeline(&input
, stdin
, line_termination
)) {
621 const struct parse_cmd
*cmd
= NULL
;
623 if (*input
.buf
== line_termination
)
624 die("empty command in input");
625 else if (isspace(*input
.buf
))
626 die("whitespace before command: %s", input
.buf
);
628 for (i
= 0; i
< ARRAY_SIZE(command
); i
++) {
629 const char *prefix
= command
[i
].prefix
;
632 if (!starts_with(input
.buf
, prefix
))
636 * If the command has arguments, verify that it's
637 * followed by a space. Otherwise, it shall be followed
638 * by a line terminator.
640 c
= command
[i
].args
? ' ' : line_termination
;
641 if (input
.buf
[strlen(prefix
)] != c
)
648 die("unknown command: %s", input
.buf
);
651 * Read additional arguments if NUL-terminated. Do not raise an
652 * error in case there is an early EOF to let the command
653 * handle missing arguments with a proper error message.
655 for (j
= 1; line_termination
== '\0' && j
< cmd
->args
; j
++)
656 if (strbuf_appendwholeline(&input
, stdin
, line_termination
))
660 case UPDATE_REFS_OPEN
:
661 case UPDATE_REFS_STARTED
:
662 if (state
== UPDATE_REFS_STARTED
&& cmd
->state
== UPDATE_REFS_STARTED
)
663 die("cannot restart ongoing transaction");
664 /* Do not downgrade a transaction to a non-transaction. */
665 if (cmd
->state
>= state
)
668 case UPDATE_REFS_PREPARED
:
669 if (cmd
->state
!= UPDATE_REFS_CLOSED
)
670 die("prepared transactions can only be closed");
673 case UPDATE_REFS_CLOSED
:
674 if (cmd
->state
!= UPDATE_REFS_STARTED
)
675 die("transaction is closed");
678 * Open a new transaction if we're currently closed and
682 transaction
= ref_store_transaction_begin(get_main_ref_store(the_repository
),
690 cmd
->fn(transaction
, input
.buf
+ strlen(cmd
->prefix
) + !!cmd
->args
,
691 input
.buf
+ input
.len
);
695 case UPDATE_REFS_OPEN
:
696 /* Commit by default if no transaction was requested. */
697 if (ref_transaction_commit(transaction
, &err
))
699 ref_transaction_free(transaction
);
701 case UPDATE_REFS_STARTED
:
702 case UPDATE_REFS_PREPARED
:
703 /* If using a transaction, we want to abort it. */
704 if (ref_transaction_abort(transaction
, &err
))
707 case UPDATE_REFS_CLOSED
:
708 /* Otherwise no need to do anything, the transaction was closed already. */
712 strbuf_release(&err
);
713 strbuf_release(&input
);
716 int cmd_update_ref(int argc
, const char **argv
, const char *prefix
)
718 const char *refname
, *oldval
;
719 struct object_id oid
, oldoid
;
720 int delete = 0, no_deref
= 0, read_stdin
= 0, end_null
= 0;
721 int create_reflog
= 0;
722 struct option options
[] = {
723 OPT_STRING( 'm', NULL
, &msg
, N_("reason"), N_("reason of the update")),
724 OPT_BOOL('d', NULL
, &delete, N_("delete the reference")),
725 OPT_BOOL( 0 , "no-deref", &no_deref
,
726 N_("update <refname> not the one it points to")),
727 OPT_BOOL('z', NULL
, &end_null
, N_("stdin has NUL-terminated arguments")),
728 OPT_BOOL( 0 , "stdin", &read_stdin
, N_("read updates from stdin")),
729 OPT_BOOL( 0 , "create-reflog", &create_reflog
, N_("create a reflog")),
733 git_config(git_default_config
, NULL
);
734 argc
= parse_options(argc
, argv
, prefix
, options
, git_update_ref_usage
,
737 die("Refusing to perform update with empty message.");
739 create_reflog_flag
= create_reflog
? REF_FORCE_CREATE_REFLOG
: 0;
742 default_flags
= REF_NO_DEREF
;
743 update_flags
= default_flags
;
747 if (delete || argc
> 0)
748 usage_with_options(git_update_ref_usage
, options
);
750 line_termination
= '\0';
756 usage_with_options(git_update_ref_usage
, options
);
759 if (argc
< 1 || argc
> 2)
760 usage_with_options(git_update_ref_usage
, options
);
765 if (argc
< 2 || argc
> 3)
766 usage_with_options(git_update_ref_usage
, options
);
770 if (repo_get_oid(the_repository
, value
, &oid
))
771 die("%s: not a valid SHA1", value
);
777 * The empty string implies that the reference
778 * must not already exist:
781 else if (repo_get_oid(the_repository
, oldval
, &oldoid
))
782 die("%s: not a valid old SHA1", oldval
);
787 * For purposes of backwards compatibility, we treat
788 * NULL_SHA1 as "don't care" here:
790 return refs_delete_ref(get_main_ref_store(the_repository
),
792 (oldval
&& !is_null_oid(&oldoid
)) ? &oldoid
: NULL
,
795 return refs_update_ref(get_main_ref_store(the_repository
),
797 oldval
? &oldoid
: NULL
,
798 default_flags
| create_reflog_flag
,
799 UPDATE_REFS_DIE_ON_ERR
);