]> git.ipfire.org Git - thirdparty/git.git/blobdiff - sequencer.c
Merge branch 'nd/the-index' into md/list-objects-filter-by-depth
[thirdparty/git.git] / sequencer.c
index d726f77e11bf39e44037f65bf7bb561df4fd76bf..b68bca0bef927f0ebb2d8762f658946be7107fcb 100644 (file)
@@ -676,55 +676,131 @@ missing_author:
        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;
 }
@@ -744,54 +820,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;
+       char *name, *email, *date;
 
-       if (strbuf_read_file(buf, rebase_path_author_script(), 256) <= 0)
+       if (read_author_script(rebase_path_author_script(),
+                              &name, &email, &date, 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;
-       }
-
-       if (i < 3) {
-               warning(_("could not parse '%s' (looking for '%s')"),
-                       rebase_path_author_script(), keys[i]);
-               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;
 }
 
@@ -1942,7 +1992,7 @@ static int read_and_refresh_cache(struct repository *r,
 {
        struct lock_file index_lock = LOCK_INIT;
        int index_fd = hold_locked_index(&index_lock, 0);
-       if (read_index_preload(r->index, NULL, 0) < 0) {
+       if (read_index(r->index) < 0) {
                rollback_lock_file(&index_lock);
                return error(_("git %s: failed to read the index"),
                        _(action_name(opts)));
@@ -2925,7 +2975,7 @@ static int do_reset(struct repository *r,
        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;
@@ -2945,10 +2995,13 @@ static int do_reset(struct repository *r,
                }
                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) &&
@@ -3229,10 +3282,6 @@ static int do_merge(struct repository *r,
        }
 
        merge_commit = to_merge->item;
-       write_message(oid_to_hex(&merge_commit->object.oid), GIT_SHA1_HEXSZ,
-                     git_path_merge_head(r), 0);
-       write_message("no-ff", 5, git_path_merge_mode(r), 0);
-
        bases = get_merge_bases(head_commit, merge_commit);
        if (bases && oideq(&merge_commit->object.oid,
                           &bases->item->object.oid)) {
@@ -3241,6 +3290,10 @@ static int do_merge(struct repository *r,
                goto leave_merge;
        }
 
+       write_message(oid_to_hex(&merge_commit->object.oid), GIT_SHA1_HEXSZ,
+                     git_path_merge_head(r), 0);
+       write_message("no-ff", 5, git_path_merge_mode(r), 0);
+
        for (j = bases; j; j = j->next)
                commit_list_insert(j->item, &reversed);
        free_commit_list(bases);
@@ -3499,6 +3552,7 @@ static int pick_commits(struct repository *r,
                        unlink(rebase_path_author_script());
                        unlink(rebase_path_stopped_sha());
                        unlink(rebase_path_amend());
+                       unlink(git_path_merge_head(the_repository));
                        delete_ref(NULL, "REBASE_HEAD", NULL, REF_NO_DEREF);
 
                        if (item->command == TODO_BREAK)
@@ -3871,6 +3925,7 @@ static int commit_staged_changes(struct repository *r,
                           opts, flags))
                return error(_("could not commit staged changes."));
        unlink(rebase_path_amend());
+       unlink(git_path_merge_head(the_repository));
        if (final_fixup) {
                unlink(rebase_path_fixup_msg());
                unlink(rebase_path_squash_msg());
@@ -4893,7 +4948,7 @@ int complete_action(struct repository *r, struct replay_opts *opts, unsigned fla
 
        if (checkout_onto(opts, onto_name, oid_to_hex(&oid), orig_head))
                return -1;
-;
+
        if (require_clean_work_tree(r, "rebase", "", 1, 1))
                return -1;