worktree: rename is_worktree_locked to worktree_lock_reason
[gitweb.git] / builtin / update-ref.c
index 6b90c5deadb62600aa08166f500bde24cb800fbf..2d8f7f05785dca5a083ee6d003d16b4d008dc4d2 100644 (file)
@@ -14,7 +14,8 @@ static const char * const git_update_ref_usage[] = {
 };
 
 static char line_termination = '\n';
-static int update_flags;
+static unsigned int update_flags;
+static unsigned int default_flags;
 static unsigned create_reflog_flag;
 static const char *msg;
 
@@ -200,12 +201,12 @@ static const char *parse_cmd_update(struct ref_transaction *transaction,
                die("update %s: extra input: %s", refname, next);
 
        if (ref_transaction_update(transaction, refname,
-                                  new_oid.hash, have_old ? old_oid.hash : NULL,
+                                  &new_oid, have_old ? &old_oid : NULL,
                                   update_flags | create_reflog_flag,
                                   msg, &err))
                die("%s", err.buf);
 
-       update_flags = 0;
+       update_flags = default_flags;
        free(refname);
        strbuf_release(&err);
 
@@ -232,12 +233,12 @@ static const char *parse_cmd_create(struct ref_transaction *transaction,
        if (*next != line_termination)
                die("create %s: extra input: %s", refname, next);
 
-       if (ref_transaction_create(transaction, refname, new_oid.hash,
+       if (ref_transaction_create(transaction, refname, &new_oid,
                                   update_flags | create_reflog_flag,
                                   msg, &err))
                die("%s", err.buf);
 
-       update_flags = 0;
+       update_flags = default_flags;
        free(refname);
        strbuf_release(&err);
 
@@ -269,11 +270,11 @@ static const char *parse_cmd_delete(struct ref_transaction *transaction,
                die("delete %s: extra input: %s", refname, next);
 
        if (ref_transaction_delete(transaction, refname,
-                                  have_old ? old_oid.hash : NULL,
+                                  have_old ? &old_oid : NULL,
                                   update_flags, msg, &err))
                die("%s", err.buf);
 
-       update_flags = 0;
+       update_flags = default_flags;
        free(refname);
        strbuf_release(&err);
 
@@ -298,11 +299,11 @@ static const char *parse_cmd_verify(struct ref_transaction *transaction,
        if (*next != line_termination)
                die("verify %s: extra input: %s", refname, next);
 
-       if (ref_transaction_verify(transaction, refname, old_oid.hash,
+       if (ref_transaction_verify(transaction, refname, &old_oid,
                                   update_flags, &err))
                die("%s", err.buf);
 
-       update_flags = 0;
+       update_flags = default_flags;
        free(refname);
        strbuf_release(&err);
 
@@ -311,11 +312,12 @@ static const char *parse_cmd_verify(struct ref_transaction *transaction,
 
 static const char *parse_cmd_option(struct strbuf *input, const char *next)
 {
-       if (!strncmp(next, "no-deref", 8) && next[8] == line_termination)
-               update_flags |= REF_NODEREF;
+       const char *rest;
+       if (skip_prefix(next, "no-deref", &rest) && *rest == line_termination)
+               update_flags |= REF_NO_DEREF;
        else
                die("option unknown: %s", next);
-       return next + 8;
+       return rest;
 }
 
 static void update_refs_stdin(struct ref_transaction *transaction)
@@ -332,16 +334,16 @@ static void update_refs_stdin(struct ref_transaction *transaction)
                        die("empty command in input");
                else if (isspace(*next))
                        die("whitespace before command: %s", next);
-               else if (starts_with(next, "update "))
-                       next = parse_cmd_update(transaction, &input, next + 7);
-               else if (starts_with(next, "create "))
-                       next = parse_cmd_create(transaction, &input, next + 7);
-               else if (starts_with(next, "delete "))
-                       next = parse_cmd_delete(transaction, &input, next + 7);
-               else if (starts_with(next, "verify "))
-                       next = parse_cmd_verify(transaction, &input, next + 7);
-               else if (starts_with(next, "option "))
-                       next = parse_cmd_option(&input, next + 7);
+               else if (skip_prefix(next, "update ", &next))
+                       next = parse_cmd_update(transaction, &input, next);
+               else if (skip_prefix(next, "create ", &next))
+                       next = parse_cmd_create(transaction, &input, next);
+               else if (skip_prefix(next, "delete ", &next))
+                       next = parse_cmd_delete(transaction, &input, next);
+               else if (skip_prefix(next, "verify ", &next))
+                       next = parse_cmd_verify(transaction, &input, next);
+               else if (skip_prefix(next, "option ", &next))
+                       next = parse_cmd_option(&input, next);
                else
                        die("unknown command: %s", next);
 
@@ -356,7 +358,6 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
        const char *refname, *oldval;
        struct object_id oid, oldoid;
        int delete = 0, no_deref = 0, read_stdin = 0, end_null = 0;
-       unsigned int flags = 0;
        int create_reflog = 0;
        struct option options[] = {
                OPT_STRING( 'm', NULL, &msg, N_("reason"), N_("reason of the update")),
@@ -377,6 +378,11 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
 
        create_reflog_flag = create_reflog ? REF_FORCE_CREATE_REFLOG : 0;
 
+       if (no_deref) {
+               default_flags = REF_NO_DEREF;
+               update_flags = default_flags;
+       }
+
        if (read_stdin) {
                struct strbuf err = STRBUF_INIT;
                struct ref_transaction *transaction;
@@ -384,7 +390,7 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
                transaction = ref_transaction_begin(&err);
                if (!transaction)
                        die("%s", err.buf);
-               if (delete || no_deref || argc > 0)
+               if (delete || argc > 0)
                        usage_with_options(git_update_ref_usage, options);
                if (end_null)
                        line_termination = '\0';
@@ -426,18 +432,16 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
                        die("%s: not a valid old SHA1", oldval);
        }
 
-       if (no_deref)
-               flags = REF_NODEREF;
        if (delete)
                /*
                 * For purposes of backwards compatibility, we treat
                 * NULL_SHA1 as "don't care" here:
                 */
                return delete_ref(msg, refname,
-                                 (oldval && !is_null_oid(&oldoid)) ? oldoid.hash : NULL,
-                                 flags);
+                                 (oldval && !is_null_oid(&oldoid)) ? &oldoid : NULL,
+                                 default_flags);
        else
-               return update_ref(msg, refname, oid.hash, oldval ? oldoid.hash : NULL,
-                                 flags | create_reflog_flag,
+               return update_ref(msg, refname, &oid, oldval ? &oldoid : NULL,
+                                 default_flags | create_reflog_flag,
                                  UPDATE_REFS_DIE_ON_ERR);
 }