0dc2061c7ea8aaef2032212dfc71346294a5cb65
   1#include "cache.h"
   2#include "refs.h"
   3#include "builtin.h"
   4#include "parse-options.h"
   5#include "quote.h"
   6#include "argv-array.h"
   7
   8static const char * const git_update_ref_usage[] = {
   9        N_("git update-ref [options] -d <refname> [<oldval>]"),
  10        N_("git update-ref [options]    <refname> <newval> [<oldval>]"),
  11        N_("git update-ref [options] --stdin [-z]"),
  12        NULL
  13};
  14
  15static int updates_alloc;
  16static int updates_count;
  17static struct ref_update **updates;
  18
  19static char line_termination = '\n';
  20static int update_flags;
  21
  22static struct ref_update *update_alloc(void)
  23{
  24        struct ref_update *update;
  25
  26        /* Allocate and zero-init a struct ref_update */
  27        update = xcalloc(1, sizeof(*update));
  28        ALLOC_GROW(updates, updates_count + 1, updates_alloc);
  29        updates[updates_count++] = update;
  30
  31        /* Store and reset accumulated options */
  32        update->flags = update_flags;
  33        update_flags = 0;
  34
  35        return update;
  36}
  37
  38static void update_store_new_sha1(struct ref_update *update,
  39                                  const char *newvalue)
  40{
  41        if (*newvalue && get_sha1(newvalue, update->new_sha1))
  42                die("invalid new value for ref %s: %s",
  43                    update->ref_name, newvalue);
  44}
  45
  46static void update_store_old_sha1(struct ref_update *update,
  47                                  const char *oldvalue)
  48{
  49        if (*oldvalue && get_sha1(oldvalue, update->old_sha1))
  50                die("invalid old value for ref %s: %s",
  51                    update->ref_name, oldvalue);
  52
  53        /* We have an old value if non-empty, or if empty without -z */
  54        update->have_old = *oldvalue || line_termination;
  55}
  56
  57/*
  58 * Parse one whitespace- or NUL-terminated, possibly C-quoted argument
  59 * and append the result to arg.  Return a pointer to the terminator.
  60 * Die if there is an error in how the argument is C-quoted.  This
  61 * function is only used if not -z.
  62 */
  63static const char *parse_arg(const char *next, struct strbuf *arg)
  64{
  65        if (*next == '"') {
  66                const char *orig = next;
  67
  68                if (unquote_c_style(arg, next, &next))
  69                        die("badly quoted argument: %s", orig);
  70                if (*next && !isspace(*next))
  71                        die("unexpected character after quoted argument: %s", orig);
  72        } else {
  73                while (*next && !isspace(*next))
  74                        strbuf_addch(arg, *next++);
  75        }
  76
  77        return next;
  78}
  79
  80/*
  81 * Parse the reference name immediately after "command SP".  If not
  82 * -z, then handle C-quoting.  Return a pointer to a newly allocated
  83 * string containing the name of the reference, or NULL if there was
  84 * an error.  Update *next to point at the character that terminates
  85 * the argument.  Die if C-quoting is malformed or the reference name
  86 * is invalid.
  87 */
  88static char *parse_refname(struct strbuf *input, const char **next)
  89{
  90        struct strbuf ref = STRBUF_INIT;
  91
  92        if (line_termination) {
  93                /* Without -z, use the next argument */
  94                *next = parse_arg(*next, &ref);
  95        } else {
  96                /* With -z, use everything up to the next NUL */
  97                strbuf_addstr(&ref, *next);
  98                *next += ref.len;
  99        }
 100
 101        if (!ref.len) {
 102                strbuf_release(&ref);
 103                return NULL;
 104        }
 105
 106        if (check_refname_format(ref.buf, REFNAME_ALLOW_ONELEVEL))
 107                die("invalid ref format: %s", ref.buf);
 108
 109        return strbuf_detach(&ref, NULL);
 110}
 111
 112/*
 113 * Parse a SP/NUL separator followed by the next SP- or NUL-terminated
 114 * argument, if any.  If there is an argument, write it to arg, set
 115 * *next to point at the character terminating the argument, and
 116 * return 0.  If there is no argument at all (not even the empty
 117 * string), return a non-zero result and leave *next unchanged.
 118 */
 119static int parse_next_arg(struct strbuf *input, const char **next,
 120                          struct strbuf *arg)
 121{
 122        strbuf_reset(arg);
 123        if (line_termination) {
 124                /* Without -z, consume SP and use next argument */
 125                if (!**next || **next == line_termination)
 126                        return -1;
 127                if (**next != ' ')
 128                        die("expected SP but got: %s", *next);
 129                (*next)++;
 130                *next = parse_arg(*next, arg);
 131        } else {
 132                /* With -z, read the next NUL-terminated line */
 133                if (**next)
 134                        die("expected NUL but got: %s", *next);
 135                (*next)++;
 136                if (*next == input->buf + input->len)
 137                        return -1;
 138                strbuf_addstr(arg, *next);
 139                *next += arg->len;
 140        }
 141        return 0;
 142}
 143
 144
 145/*
 146 * The following five parse_cmd_*() functions parse the corresponding
 147 * command.  In each case, next points at the character following the
 148 * command name and the following space.  They each return a pointer
 149 * to the character terminating the command, and die with an
 150 * explanatory message if there are any parsing problems.  All of
 151 * these functions handle either text or binary format input,
 152 * depending on how line_termination is set.
 153 */
 154
 155static const char *parse_cmd_update(struct strbuf *input, const char *next)
 156{
 157        struct strbuf newvalue = STRBUF_INIT;
 158        struct strbuf oldvalue = STRBUF_INIT;
 159        struct ref_update *update;
 160
 161        update = update_alloc();
 162
 163        update->ref_name = parse_refname(input, &next);
 164        if (!update->ref_name)
 165                die("update line missing <ref>");
 166
 167        if (!parse_next_arg(input, &next, &newvalue))
 168                update_store_new_sha1(update, newvalue.buf);
 169        else
 170                die("update %s missing <newvalue>", update->ref_name);
 171
 172        if (!parse_next_arg(input, &next, &oldvalue)) {
 173                update_store_old_sha1(update, oldvalue.buf);
 174                if (*next != line_termination)
 175                        die("update %s has extra input: %s", update->ref_name, next);
 176        } else if (!line_termination)
 177                die("update %s missing [<oldvalue>] NUL", update->ref_name);
 178
 179        return next;
 180}
 181
 182static const char *parse_cmd_create(struct strbuf *input, const char *next)
 183{
 184        struct strbuf newvalue = STRBUF_INIT;
 185        struct ref_update *update;
 186
 187        update = update_alloc();
 188
 189        update->ref_name = parse_refname(input, &next);
 190        if (!update->ref_name)
 191                die("create line missing <ref>");
 192
 193        if (!parse_next_arg(input, &next, &newvalue))
 194                update_store_new_sha1(update, newvalue.buf);
 195        else
 196                die("create %s missing <newvalue>", update->ref_name);
 197
 198        if (is_null_sha1(update->new_sha1))
 199                die("create %s given zero new value", update->ref_name);
 200
 201        if (*next != line_termination)
 202                die("create %s has extra input: %s", update->ref_name, next);
 203
 204        return next;
 205}
 206
 207static const char *parse_cmd_delete(struct strbuf *input, const char *next)
 208{
 209        struct strbuf oldvalue = STRBUF_INIT;
 210        struct ref_update *update;
 211
 212        update = update_alloc();
 213
 214        update->ref_name = parse_refname(input, &next);
 215        if (!update->ref_name)
 216                die("delete line missing <ref>");
 217
 218        if (!parse_next_arg(input, &next, &oldvalue)) {
 219                update_store_old_sha1(update, oldvalue.buf);
 220                if (update->have_old && is_null_sha1(update->old_sha1))
 221                        die("delete %s given zero old value", update->ref_name);
 222        } else if (!line_termination)
 223                die("delete %s missing [<oldvalue>] NUL", update->ref_name);
 224
 225        if (*next != line_termination)
 226                die("delete %s has extra input: %s", update->ref_name, next);
 227
 228        return next;
 229}
 230
 231static const char *parse_cmd_verify(struct strbuf *input, const char *next)
 232{
 233        struct strbuf value = STRBUF_INIT;
 234        struct ref_update *update;
 235
 236        update = update_alloc();
 237
 238        update->ref_name = parse_refname(input, &next);
 239        if (!update->ref_name)
 240                die("verify line missing <ref>");
 241
 242        if (!parse_next_arg(input, &next, &value)) {
 243                update_store_old_sha1(update, value.buf);
 244                hashcpy(update->new_sha1, update->old_sha1);
 245        } else if (!line_termination)
 246                die("verify %s missing [<oldvalue>] NUL", update->ref_name);
 247
 248        if (*next != line_termination)
 249                die("verify %s has extra input: %s", update->ref_name, next);
 250
 251        return next;
 252}
 253
 254static const char *parse_cmd_option(struct strbuf *input, const char *next)
 255{
 256        if (!strncmp(next, "no-deref", 8) && next[8] == line_termination)
 257                update_flags |= REF_NODEREF;
 258        else
 259                die("option unknown: %s", next);
 260        return next + 8;
 261}
 262
 263static void update_refs_stdin(void)
 264{
 265        struct strbuf input = STRBUF_INIT;
 266        const char *next;
 267
 268        if (strbuf_read(&input, 0, 1000) < 0)
 269                die_errno("could not read from stdin");
 270        next = input.buf;
 271        /* Read each line dispatch its command */
 272        while (next < input.buf + input.len) {
 273                if (*next == line_termination)
 274                        die("empty command in input");
 275                else if (isspace(*next))
 276                        die("whitespace before command: %s", next);
 277                else if (starts_with(next, "update "))
 278                        next = parse_cmd_update(&input, next + 7);
 279                else if (starts_with(next, "create "))
 280                        next = parse_cmd_create(&input, next + 7);
 281                else if (starts_with(next, "delete "))
 282                        next = parse_cmd_delete(&input, next + 7);
 283                else if (starts_with(next, "verify "))
 284                        next = parse_cmd_verify(&input, next + 7);
 285                else if (starts_with(next, "option "))
 286                        next = parse_cmd_option(&input, next + 7);
 287                else
 288                        die("unknown command: %s", next);
 289
 290                next++;
 291        }
 292
 293        strbuf_release(&input);
 294}
 295
 296int cmd_update_ref(int argc, const char **argv, const char *prefix)
 297{
 298        const char *refname, *oldval, *msg = NULL;
 299        unsigned char sha1[20], oldsha1[20];
 300        int delete = 0, no_deref = 0, read_stdin = 0, end_null = 0, flags = 0;
 301        struct option options[] = {
 302                OPT_STRING( 'm', NULL, &msg, N_("reason"), N_("reason of the update")),
 303                OPT_BOOL('d', NULL, &delete, N_("delete the reference")),
 304                OPT_BOOL( 0 , "no-deref", &no_deref,
 305                                        N_("update <refname> not the one it points to")),
 306                OPT_BOOL('z', NULL, &end_null, N_("stdin has NUL-terminated arguments")),
 307                OPT_BOOL( 0 , "stdin", &read_stdin, N_("read updates from stdin")),
 308                OPT_END(),
 309        };
 310
 311        git_config(git_default_config, NULL);
 312        argc = parse_options(argc, argv, prefix, options, git_update_ref_usage,
 313                             0);
 314        if (msg && !*msg)
 315                die("Refusing to perform update with empty message.");
 316
 317        if (read_stdin) {
 318                if (delete || no_deref || argc > 0)
 319                        usage_with_options(git_update_ref_usage, options);
 320                if (end_null)
 321                        line_termination = '\0';
 322                update_refs_stdin();
 323                return update_refs(msg, updates, updates_count,
 324                                   UPDATE_REFS_DIE_ON_ERR);
 325        }
 326
 327        if (end_null)
 328                usage_with_options(git_update_ref_usage, options);
 329
 330        if (delete) {
 331                if (argc < 1 || argc > 2)
 332                        usage_with_options(git_update_ref_usage, options);
 333                refname = argv[0];
 334                oldval = argv[1];
 335        } else {
 336                const char *value;
 337                if (argc < 2 || argc > 3)
 338                        usage_with_options(git_update_ref_usage, options);
 339                refname = argv[0];
 340                value = argv[1];
 341                oldval = argv[2];
 342                if (get_sha1(value, sha1))
 343                        die("%s: not a valid SHA1", value);
 344        }
 345
 346        hashclr(oldsha1); /* all-zero hash in case oldval is the empty string */
 347        if (oldval && *oldval && get_sha1(oldval, oldsha1))
 348                die("%s: not a valid old SHA1", oldval);
 349
 350        if (no_deref)
 351                flags = REF_NODEREF;
 352        if (delete)
 353                return delete_ref(refname, oldval ? oldsha1 : NULL, flags);
 354        else
 355                return update_ref(msg, refname, sha1, oldval ? oldsha1 : NULL,
 356                                  flags, UPDATE_REFS_DIE_ON_ERR);
 357}