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_ref_name(struct ref_update *update,
- const char *ref_name)
-{
- if (check_refname_format(ref_name, REFNAME_ALLOW_ONELEVEL))
- die("invalid ref format: %s", ref_name);
- update->ref_name = xstrdup(ref_name);
-}
-
-static void update_store_new_sha1(struct ref_update *update,
- const char *newvalue)
-{
- if (*newvalue && get_sha1(newvalue, update->new_sha1))
- die("invalid new value for ref %s: %s",
- update->ref_name, newvalue);
-}
-
-static void update_store_old_sha1(struct ref_update *update,
- const char *oldvalue)
-{
- if (*oldvalue && get_sha1(oldvalue, update->old_sha1))
- die("invalid old value for ref %s: %s",
- 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.
}
/*
- * Parse the argument immediately after "command SP". If not -z, then
- * handle C-quoting. Write the argument to arg. Set *next to point
- * at the character that terminates the argument. Die if C-quoting is
- * malformed.
+ * Parse the reference name immediately after "command SP". If not
+ * -z, then handle C-quoting. Return a pointer to a newly allocated
+ * string containing the name of the reference, or NULL if there was
+ * an error. Update *next to point at the character that terminates
+ * the argument. Die if C-quoting is malformed or the reference name
+ * is invalid.
*/
-static void parse_first_arg(struct strbuf *input, const char **next,
- struct strbuf *arg)
+static char *parse_refname(struct strbuf *input, const char **next)
{
- strbuf_reset(arg);
+ struct strbuf ref = STRBUF_INIT;
+
if (line_termination) {
/* Without -z, use the next argument */
- *next = parse_arg(*next, arg);
+ *next = parse_arg(*next, &ref);
} else {
/* With -z, use everything up to the next NUL */
- strbuf_addstr(arg, *next);
- *next += arg->len;
+ strbuf_addstr(&ref, *next);
+ *next += ref.len;
+ }
+
+ if (!ref.len) {
+ strbuf_release(&ref);
+ return NULL;
}
+
+ if (check_refname_format(ref.buf, REFNAME_ALLOW_ONELEVEL))
+ die("invalid ref format: %s", ref.buf);
+
+ return strbuf_detach(&ref, NULL);
}
/*
- * 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);
}
static const char *parse_cmd_update(struct strbuf *input, const char *next)
{
- struct strbuf ref = STRBUF_INIT;
- 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>");
- parse_first_arg(input, &next, &ref);
- if (ref.buf[0])
- update_store_ref_name(update, ref.buf);
- else
- 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, newvalue.buf);
- else
- die("update %s missing <newvalue>", ref.buf);
+ have_old = !parse_next_sha1(input, &next, old_sha1, "update", refname,
+ PARSE_SHA1_OLD);
- if (!parse_next_arg(input, &next, &oldvalue)) {
- update_store_old_sha1(update, oldvalue.buf);
- if (*next != line_termination)
- die("update %s has extra input: %s", ref.buf, next);
- } else if (!line_termination)
- die("update %s missing [<oldvalue>] NUL", ref.buf);
+ 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);
+
+ update_flags = 0;
+ free(refname);
return next;
}
static const char *parse_cmd_create(struct strbuf *input, const char *next)
{
- struct strbuf ref = STRBUF_INIT;
- struct strbuf newvalue = STRBUF_INIT;
- struct ref_update *update;
-
- update = update_alloc();
+ char *refname;
+ unsigned char new_sha1[20];
- parse_first_arg(input, &next, &ref);
- if (ref.buf[0])
- update_store_ref_name(update, ref.buf);
- else
- die("create line missing <ref>");
+ refname = parse_refname(input, &next);
+ if (!refname)
+ die("create: missing <ref>");
- if (!parse_next_arg(input, &next, &newvalue))
- update_store_new_sha1(update, newvalue.buf);
- else
- die("create %s missing <newvalue>", ref.buf);
+ if (parse_next_sha1(input, &next, new_sha1, "create", refname, 0))
+ die("create %s: missing <newvalue>", refname);
- if (is_null_sha1(update->new_sha1))
- die("create %s given zero new value", ref.buf);
+ if (is_null_sha1(new_sha1))
+ die("create %s: zero <newvalue>", refname);
if (*next != line_termination)
- die("create %s has extra input: %s", ref.buf, 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 ref = STRBUF_INIT;
- struct strbuf oldvalue = STRBUF_INIT;
- struct ref_update *update;
+ char *refname;
+ unsigned char old_sha1[20];
+ int have_old;
- update = update_alloc();
+ refname = parse_refname(input, &next);
+ if (!refname)
+ die("delete: missing <ref>");
- parse_first_arg(input, &next, &ref);
- if (ref.buf[0])
- update_store_ref_name(update, ref.buf);
- else
- die("delete line missing <ref>");
-
- if (!parse_next_arg(input, &next, &oldvalue)) {
- update_store_old_sha1(update, oldvalue.buf);
- if (update->have_old && is_null_sha1(update->old_sha1))
- die("delete %s given zero old value", ref.buf);
- } else if (!line_termination)
- die("delete %s missing [<oldvalue>] NUL", ref.buf);
+ 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", ref.buf, 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 ref = STRBUF_INIT;
- 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;
+ }
- parse_first_arg(input, &next, &ref);
- if (ref.buf[0])
- update_store_ref_name(update, ref.buf);
- else
- 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(update, value.buf);
- update_store_new_sha1(update, value.buf);
- } else if (!line_termination)
- die("verify %s missing [<oldvalue>] NUL", ref.buf);
+ ref_transaction_update(transaction, refname, new_sha1, old_sha1,
+ update_flags, have_old);
- if (*next != line_termination)
- die("verify %s has extra input: %s", ref.buf, next);
+ update_flags = 0;
+ free(refname);
return next;
}
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)