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