Merge branch 'jk/unused-parameter-fixes'
[gitweb.git] / sequencer.c
index 9e1ab3a2a7e3f65cc444cded7f3330bb27be2db8..0d87b0739be5c5fdaf686063729aca02559638b8 100644 (file)
@@ -669,55 +669,131 @@ static int write_author_script(const char *message)
        return res;
 }
 
+/**
+ * Take a series of KEY='VALUE' lines where VALUE part is
+ * sq-quoted, and append <KEY, VALUE> at the end of the string list
+ */
+static int parse_key_value_squoted(char *buf, struct string_list *list)
+{
+       while (*buf) {
+               struct string_list_item *item;
+               char *np;
+               char *cp = strchr(buf, '=');
+               if (!cp) {
+                       np = strchrnul(buf, '\n');
+                       return error(_("no key present in '%.*s'"),
+                                    (int) (np - buf), buf);
+               }
+               np = strchrnul(cp, '\n');
+               *cp++ = '\0';
+               item = string_list_append(list, buf);
+
+               buf = np + (*np == '\n');
+               *np = '\0';
+               cp = sq_dequote(cp);
+               if (!cp)
+                       return error(_("unable to dequote value of '%s'"),
+                                    item->string);
+               item->util = xstrdup(cp);
+       }
+       return 0;
+}
 
-/*
- * write_author_script() used to fail to terminate the last line with a "'" and
- * also escaped "'" incorrectly as "'\\\\''" rather than "'\\''". We check for
- * the terminating "'" on the last line to see how "'" has been escaped in case
- * git was upgraded while rebase was stopped.
+/**
+ * Reads and parses the state directory's "author-script" file, and sets name,
+ * email and date accordingly.
+ * Returns 0 on success, -1 if the file could not be parsed.
+ *
+ * The author script is of the format:
+ *
+ *     GIT_AUTHOR_NAME='$author_name'
+ *     GIT_AUTHOR_EMAIL='$author_email'
+ *     GIT_AUTHOR_DATE='$author_date'
+ *
+ * where $author_name, $author_email and $author_date are quoted. We are strict
+ * with our parsing, as the file was meant to be eval'd in the old
+ * git-am.sh/git-rebase--interactive.sh scripts, and thus if the file differs
+ * from what this function expects, it is better to bail out than to do
+ * something that the user does not expect.
  */
-static int quoting_is_broken(const char *s, size_t n)
+int read_author_script(const char *path, char **name, char **email, char **date,
+                      int allow_missing)
 {
-       /* Skip any empty lines in case the file was hand edited */
-       while (n > 0 && s[--n] == '\n')
-               ; /* empty */
-       if (n > 0 && s[n] != '\'')
-               return 1;
+       struct strbuf buf = STRBUF_INIT;
+       struct string_list kv = STRING_LIST_INIT_DUP;
+       int retval = -1; /* assume failure */
+       int i, name_i = -2, email_i = -2, date_i = -2, err = 0;
 
-       return 0;
+       if (strbuf_read_file(&buf, path, 256) <= 0) {
+               strbuf_release(&buf);
+               if (errno == ENOENT && allow_missing)
+                       return 0;
+               else
+                       return error_errno(_("could not open '%s' for reading"),
+                                          path);
+       }
+
+       if (parse_key_value_squoted(buf.buf, &kv))
+               goto finish;
+
+       for (i = 0; i < kv.nr; i++) {
+               if (!strcmp(kv.items[i].string, "GIT_AUTHOR_NAME")) {
+                       if (name_i != -2)
+                               name_i = error(_("'GIT_AUTHOR_NAME' already given"));
+                       else
+                               name_i = i;
+               } else if (!strcmp(kv.items[i].string, "GIT_AUTHOR_EMAIL")) {
+                       if (email_i != -2)
+                               email_i = error(_("'GIT_AUTHOR_EMAIL' already given"));
+                       else
+                               email_i = i;
+               } else if (!strcmp(kv.items[i].string, "GIT_AUTHOR_DATE")) {
+                       if (date_i != -2)
+                               date_i = error(_("'GIT_AUTHOR_DATE' already given"));
+                       else
+                               date_i = i;
+               } else {
+                       err = error(_("unknown variable '%s'"),
+                                   kv.items[i].string);
+               }
+       }
+       if (name_i == -2)
+               error(_("missing 'GIT_AUTHOR_NAME'"));
+       if (email_i == -2)
+               error(_("missing 'GIT_AUTHOR_EMAIL'"));
+       if (date_i == -2)
+               error(_("missing 'GIT_AUTHOR_DATE'"));
+       if (date_i < 0 || email_i < 0 || date_i < 0 || err)
+               goto finish;
+       *name = kv.items[name_i].util;
+       *email = kv.items[email_i].util;
+       *date = kv.items[date_i].util;
+       retval = 0;
+finish:
+       string_list_clear(&kv, !!retval);
+       strbuf_release(&buf);
+       return retval;
 }
 
 /*
- * Read a list of environment variable assignments (such as the author-script
- * file) into an environment block. Returns -1 on error, 0 otherwise.
+ * Read a GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL AND GIT_AUTHOR_DATE from a
+ * file with shell quoting into struct argv_array. Returns -1 on
+ * error, 0 otherwise.
  */
 static int read_env_script(struct argv_array *env)
 {
-       struct strbuf script = STRBUF_INIT;
-       int i, count = 0, sq_bug;
-       const char *p2;
-       char *p;
+       char *name, *email, *date;
 
-       if (strbuf_read_file(&script, rebase_path_author_script(), 256) <= 0)
+       if (read_author_script(rebase_path_author_script(),
+                              &name, &email, &date, 0))
                return -1;
-       /* write_author_script() used to quote incorrectly */
-       sq_bug = quoting_is_broken(script.buf, script.len);
-       for (p = script.buf; *p; p++)
-               if (sq_bug && skip_prefix(p, "'\\\\''", &p2))
-                       strbuf_splice(&script, p - script.buf, p2 - p, "'", 1);
-               else if (skip_prefix(p, "'\\''", &p2))
-                       strbuf_splice(&script, p - script.buf, p2 - p, "'", 1);
-               else if (*p == '\'')
-                       strbuf_splice(&script, p-- - script.buf, 1, "", 0);
-               else if (*p == '\n') {
-                       *p = '\0';
-                       count++;
-               }
 
-       for (i = 0, p = script.buf; i < count; i++) {
-               argv_array_push(env, p);
-               p += strlen(p) + 1;
-       }
+       argv_array_pushf(env, "GIT_AUTHOR_NAME=%s", name);
+       argv_array_pushf(env, "GIT_AUTHOR_EMAIL=%s", email);
+       argv_array_pushf(env, "GIT_AUTHOR_DATE=%s", date);
+       free(name);
+       free(email);
+       free(date);
 
        return 0;
 }
@@ -737,54 +813,28 @@ static char *get_author(const char *message)
 /* Read author-script and return an ident line (author <email> timestamp) */
 static const char *read_author_ident(struct strbuf *buf)
 {
-       const char *keys[] = {
-               "GIT_AUTHOR_NAME=", "GIT_AUTHOR_EMAIL=", "GIT_AUTHOR_DATE="
-       };
        struct strbuf out = STRBUF_INIT;
-       char *in, *eol;
-       const char *val[3];
-       int i = 0;
-
-       if (strbuf_read_file(buf, rebase_path_author_script(), 256) <= 0)
-               return NULL;
-
-       /* dequote values and construct ident line in-place */
-       for (in = buf->buf; i < 3 && in - buf->buf < buf->len; i++) {
-               if (!skip_prefix(in, keys[i], (const char **)&in)) {
-                       warning(_("could not parse '%s' (looking for '%s')"),
-                               rebase_path_author_script(), keys[i]);
-                       return NULL;
-               }
-
-               eol = strchrnul(in, '\n');
-               *eol = '\0';
-               if (!sq_dequote(in)) {
-                       warning(_("bad quoting on %s value in '%s'"),
-                               keys[i], rebase_path_author_script());
-                       return NULL;
-               }
-               val[i] = in;
-               in = eol + 1;
-       }
+       char *name, *email, *date;
 
-       if (i < 3) {
-               warning(_("could not parse '%s' (looking for '%s')"),
-                       rebase_path_author_script(), keys[i]);
+       if (read_author_script(rebase_path_author_script(),
+                              &name, &email, &date, 0))
                return NULL;
-       }
 
        /* validate date since fmt_ident() will die() on bad value */
-       if (parse_date(val[2], &out)){
+       if (parse_date(date, &out)){
                warning(_("invalid date format '%s' in '%s'"),
-                       val[2], rebase_path_author_script());
+                       date, rebase_path_author_script());
                strbuf_release(&out);
                return NULL;
        }
 
        strbuf_reset(&out);
-       strbuf_addstr(&out, fmt_ident(val[0], val[1], val[2], 0));
+       strbuf_addstr(&out, fmt_ident(name, email, date, 0));
        strbuf_swap(buf, &out);
        strbuf_release(&out);
+       free(name);
+       free(email);
+       free(date);
        return buf->buf;
 }
 
@@ -1919,7 +1969,7 @@ static int read_and_refresh_cache(struct replay_opts *opts)
 {
        struct lock_file index_lock = LOCK_INIT;
        int index_fd = hold_locked_index(&index_lock, 0);
-       if (read_index_preload(&the_index, NULL, 0) < 0) {
+       if (read_index(&the_index) < 0) {
                rollback_lock_file(&index_lock);
                return error(_("git %s: failed to read the index"),
                        _(action_name(opts)));
@@ -2890,7 +2940,7 @@ static int do_reset(const char *name, int len, struct replay_opts *opts)
        struct tree_desc desc;
        struct tree *tree;
        struct unpack_trees_options unpack_tree_opts;
-       int ret = 0, i;
+       int ret = 0;
 
        if (hold_locked_index(&lock, LOCK_REPORT_ON_ERROR) < 0)
                return -1;
@@ -2910,10 +2960,13 @@ static int do_reset(const char *name, int len, struct replay_opts *opts)
                }
                oidcpy(&oid, &opts->squash_onto);
        } else {
+               int i;
+
                /* Determine the length of the label */
                for (i = 0; i < len; i++)
                        if (isspace(name[i]))
-                               len = i;
+                               break;
+               len = i;
 
                strbuf_addf(&ref_name, "refs/rewritten/%.*s", len, name);
                if (get_oid(ref_name.buf, &oid) &&
@@ -4846,7 +4899,7 @@ int complete_action(struct replay_opts *opts, unsigned flags,
 
        if (checkout_onto(opts, onto_name, oid_to_hex(&oid), orig_head))
                return -1;
-;
+
        if (require_clean_work_tree("rebase", "", 1, 1))
                return -1;