Merge branch 'sk/mingw-main'
[gitweb.git] / builtin / update-ref.c
index a9eb5fe24d3efdb8ac2b70b55ad3de19b4182582..405267f6e2776b3b2bf5c91c1102e090f5ac68f0 100644 (file)
@@ -12,50 +12,11 @@ static const char * const git_update_ref_usage[] = {
        NULL
 };
 
-static int updates_alloc;
-static int updates_count;
-static struct ref_update **updates;
+static struct ref_transaction *transaction;
 
 static char line_termination = '\n';
 static int update_flags;
 
-static struct ref_update *update_alloc(void)
-{
-       struct ref_update *update;
-
-       /* Allocate and zero-init a struct ref_update */
-       update = xcalloc(1, sizeof(*update));
-       ALLOC_GROW(updates, updates_count + 1, updates_alloc);
-       updates[updates_count++] = update;
-
-       /* Store and reset accumulated options */
-       update->flags = update_flags;
-       update_flags = 0;
-
-       return update;
-}
-
-static void update_store_new_sha1(const char *command,
-                                 struct ref_update *update,
-                                 const char *newvalue)
-{
-       if (*newvalue && get_sha1(newvalue, update->new_sha1))
-               die("%s %s: invalid <newvalue>: %s",
-                   command, update->ref_name, newvalue);
-}
-
-static void update_store_old_sha1(const char *command,
-                                 struct ref_update *update,
-                                 const char *oldvalue)
-{
-       if (*oldvalue && get_sha1(oldvalue, update->old_sha1))
-               die("%s %s: invalid <oldvalue>: %s",
-                   command, update->ref_name, oldvalue);
-
-       /* We have an old value if non-empty, or if empty without -z */
-       update->have_old = *oldvalue || line_termination;
-}
-
 /*
  * Parse one whitespace- or NUL-terminated, possibly C-quoted argument
  * and append the result to arg.  Return a pointer to the terminator.
@@ -112,35 +73,96 @@ static char *parse_refname(struct strbuf *input, const char **next)
 }
 
 /*
- * Parse a SP/NUL separator followed by the next SP- or NUL-terminated
- * argument, if any.  If there is an argument, write it to arg, set
- * *next to point at the character terminating the argument, and
+ * The value being parsed is <oldvalue> (as opposed to <newvalue>; the
+ * difference affects which error messages are generated):
+ */
+#define PARSE_SHA1_OLD 0x01
+
+/*
+ * For backwards compatibility, accept an empty string for update's
+ * <newvalue> in binary mode to be equivalent to specifying zeros.
+ */
+#define PARSE_SHA1_ALLOW_EMPTY 0x02
+
+/*
+ * Parse an argument separator followed by the next argument, if any.
+ * If there is an argument, convert it to a SHA-1, write it to sha1,
+ * set *next to point at the character terminating the argument, and
  * return 0.  If there is no argument at all (not even the empty
- * string), return a non-zero result and leave *next unchanged.
+ * string), return 1 and leave *next unchanged.  If the value is
+ * provided but cannot be converted to a SHA-1, die.  flags can
+ * include PARSE_SHA1_OLD and/or PARSE_SHA1_ALLOW_EMPTY.
  */
-static int parse_next_arg(struct strbuf *input, const char **next,
-                         struct strbuf *arg)
+static int parse_next_sha1(struct strbuf *input, const char **next,
+                          unsigned char *sha1,
+                          const char *command, const char *refname,
+                          int flags)
 {
-       strbuf_reset(arg);
+       struct strbuf arg = STRBUF_INIT;
+       int ret = 0;
+
+       if (*next == input->buf + input->len)
+               goto eof;
+
        if (line_termination) {
                /* Without -z, consume SP and use next argument */
                if (!**next || **next == line_termination)
-                       return -1;
+                       return 1;
                if (**next != ' ')
-                       die("expected SP but got: %s", *next);
+                       die("%s %s: expected SP but got: %s",
+                           command, refname, *next);
                (*next)++;
-               *next = parse_arg(*next, arg);
+               *next = parse_arg(*next, &arg);
+               if (arg.len) {
+                       if (get_sha1(arg.buf, sha1))
+                               goto invalid;
+               } else {
+                       /* Without -z, an empty value means all zeros: */
+                       hashclr(sha1);
+               }
        } else {
                /* With -z, read the next NUL-terminated line */
                if (**next)
-                       die("expected NUL but got: %s", *next);
+                       die("%s %s: expected NUL but got: %s",
+                           command, refname, *next);
                (*next)++;
                if (*next == input->buf + input->len)
-                       return -1;
-               strbuf_addstr(arg, *next);
-               *next += arg->len;
+                       goto eof;
+               strbuf_addstr(&arg, *next);
+               *next += arg.len;
+
+               if (arg.len) {
+                       if (get_sha1(arg.buf, sha1))
+                               goto invalid;
+               } else if (flags & PARSE_SHA1_ALLOW_EMPTY) {
+                       /* With -z, treat an empty value as all zeros: */
+                       warning("%s %s: missing <newvalue>, treating as zero",
+                               command, refname);
+                       hashclr(sha1);
+               } else {
+                       /*
+                        * With -z, an empty non-required value means
+                        * unspecified:
+                        */
+                       ret = 1;
+               }
        }
-       return 0;
+
+       strbuf_release(&arg);
+
+       return ret;
+
+ invalid:
+       die(flags & PARSE_SHA1_OLD ?
+           "%s %s: invalid <oldvalue>: %s" :
+           "%s %s: invalid <newvalue>: %s",
+           command, refname, arg.buf);
+
+ eof:
+       die(flags & PARSE_SHA1_OLD ?
+           "%s %s: unexpected end of input when reading <oldvalue>" :
+           "%s %s: unexpected end of input when reading <newvalue>",
+           command, refname);
 }
 
 
@@ -156,99 +178,119 @@ static int parse_next_arg(struct strbuf *input, const char **next,
 
 static const char *parse_cmd_update(struct strbuf *input, const char *next)
 {
-       struct strbuf newvalue = STRBUF_INIT;
-       struct strbuf oldvalue = STRBUF_INIT;
-       struct ref_update *update;
+       char *refname;
+       unsigned char new_sha1[20];
+       unsigned char old_sha1[20];
+       int have_old;
 
-       update = update_alloc();
+       refname = parse_refname(input, &next);
+       if (!refname)
+               die("update: missing <ref>");
 
-       update->ref_name = parse_refname(input, &next);
-       if (!update->ref_name)
-               die("update line missing <ref>");
+       if (parse_next_sha1(input, &next, new_sha1, "update", refname,
+                           PARSE_SHA1_ALLOW_EMPTY))
+               die("update %s: missing <newvalue>", refname);
 
-       if (!parse_next_arg(input, &next, &newvalue))
-               update_store_new_sha1("update", update, newvalue.buf);
-       else
-               die("update %s missing <newvalue>", update->ref_name);
+       have_old = !parse_next_sha1(input, &next, old_sha1, "update", refname,
+                                   PARSE_SHA1_OLD);
+
+       if (*next != line_termination)
+               die("update %s: extra input: %s", refname, next);
+
+       ref_transaction_update(transaction, refname, new_sha1, old_sha1,
+                              update_flags, have_old);
 
-       if (!parse_next_arg(input, &next, &oldvalue)) {
-               update_store_old_sha1("update", update, oldvalue.buf);
-               if (*next != line_termination)
-                       die("update %s has extra input: %s", update->ref_name, next);
-       } else if (!line_termination)
-               die("update %s missing <oldvalue>", update->ref_name);
+       update_flags = 0;
+       free(refname);
 
        return next;
 }
 
 static const char *parse_cmd_create(struct strbuf *input, const char *next)
 {
-       struct strbuf newvalue = STRBUF_INIT;
-       struct ref_update *update;
+       char *refname;
+       unsigned char new_sha1[20];
 
-       update = update_alloc();
+       refname = parse_refname(input, &next);
+       if (!refname)
+               die("create: missing <ref>");
 
-       update->ref_name = parse_refname(input, &next);
-       if (!update->ref_name)
-               die("create line missing <ref>");
+       if (parse_next_sha1(input, &next, new_sha1, "create", refname, 0))
+               die("create %s: missing <newvalue>", refname);
 
-       if (!parse_next_arg(input, &next, &newvalue))
-               update_store_new_sha1("create", update, newvalue.buf);
-       else
-               die("create %s missing <newvalue>", update->ref_name);
-
-       if (is_null_sha1(update->new_sha1))
-               die("create %s given zero <newvalue>", update->ref_name);
+       if (is_null_sha1(new_sha1))
+               die("create %s: zero <newvalue>", refname);
 
        if (*next != line_termination)
-               die("create %s has extra input: %s", update->ref_name, next);
+               die("create %s: extra input: %s", refname, next);
+
+       ref_transaction_create(transaction, refname, new_sha1, update_flags);
+
+       update_flags = 0;
+       free(refname);
 
        return next;
 }
 
 static const char *parse_cmd_delete(struct strbuf *input, const char *next)
 {
-       struct strbuf oldvalue = STRBUF_INIT;
-       struct ref_update *update;
-
-       update = update_alloc();
+       char *refname;
+       unsigned char old_sha1[20];
+       int have_old;
 
-       update->ref_name = parse_refname(input, &next);
-       if (!update->ref_name)
-               die("delete line missing <ref>");
+       refname = parse_refname(input, &next);
+       if (!refname)
+               die("delete: missing <ref>");
 
-       if (!parse_next_arg(input, &next, &oldvalue)) {
-               update_store_old_sha1("delete", update, oldvalue.buf);
-               if (update->have_old && is_null_sha1(update->old_sha1))
-                       die("delete %s given zero <oldvalue>", update->ref_name);
-       } else if (!line_termination)
-               die("delete %s missing <oldvalue>", update->ref_name);
+       if (parse_next_sha1(input, &next, old_sha1, "delete", refname,
+                           PARSE_SHA1_OLD)) {
+               have_old = 0;
+       } else {
+               if (is_null_sha1(old_sha1))
+                       die("delete %s: zero <oldvalue>", refname);
+               have_old = 1;
+       }
 
        if (*next != line_termination)
-               die("delete %s has extra input: %s", update->ref_name, next);
+               die("delete %s: extra input: %s", refname, next);
+
+       ref_transaction_delete(transaction, refname, old_sha1,
+                              update_flags, have_old);
+
+       update_flags = 0;
+       free(refname);
 
        return next;
 }
 
 static const char *parse_cmd_verify(struct strbuf *input, const char *next)
 {
-       struct strbuf value = STRBUF_INIT;
-       struct ref_update *update;
-
-       update = update_alloc();
+       char *refname;
+       unsigned char new_sha1[20];
+       unsigned char old_sha1[20];
+       int have_old;
+
+       refname = parse_refname(input, &next);
+       if (!refname)
+               die("verify: missing <ref>");
+
+       if (parse_next_sha1(input, &next, old_sha1, "verify", refname,
+                           PARSE_SHA1_OLD)) {
+               hashclr(new_sha1);
+               have_old = 0;
+       } else {
+               hashcpy(new_sha1, old_sha1);
+               have_old = 1;
+       }
 
-       update->ref_name = parse_refname(input, &next);
-       if (!update->ref_name)
-               die("verify line missing <ref>");
+       if (*next != line_termination)
+               die("verify %s: extra input: %s", refname, next);
 
-       if (!parse_next_arg(input, &next, &value)) {
-               update_store_old_sha1("verify", update, value.buf);
-               hashcpy(update->new_sha1, update->old_sha1);
-       } else if (!line_termination)
-               die("verify %s missing <oldvalue>", update->ref_name);
+       ref_transaction_update(transaction, refname, new_sha1, old_sha1,
+                              update_flags, have_old);
 
-       if (*next != line_termination)
-               die("verify %s has extra input: %s", update->ref_name, next);
+       update_flags = 0;
+       free(refname);
 
        return next;
 }
@@ -317,13 +359,17 @@ int cmd_update_ref(int argc, const char **argv, const char *prefix)
                die("Refusing to perform update with empty message.");
 
        if (read_stdin) {
+               int ret;
+               transaction = ref_transaction_begin();
+
                if (delete || no_deref || argc > 0)
                        usage_with_options(git_update_ref_usage, options);
                if (end_null)
                        line_termination = '\0';
                update_refs_stdin();
-               return update_refs(msg, updates, updates_count,
-                                  UPDATE_REFS_DIE_ON_ERR);
+               ret = ref_transaction_commit(transaction, msg,
+                                            UPDATE_REFS_DIE_ON_ERR);
+               return ret;
        }
 
        if (end_null)