summaryrefslogtreecommitdiff
path: root/sequencer.c
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2018-11-13 13:37:23 (GMT)
committerJunio C Hamano <gitster@pobox.com>2018-11-13 13:37:23 (GMT)
commit6b2a52431b238fc3c25ea931def6e5b1cf9e78fb (patch)
treebda721edc23a6a3cd7bfb960aecc5dabfafeb7f2 /sequencer.c
parentfd4bb3806bcccbd8afa5dd45052ea37a882708ba (diff)
parent4d010a757c40d6e6e478354991bd052ef30cb853 (diff)
downloadgit-6b2a52431b238fc3c25ea931def6e5b1cf9e78fb.zip
git-6b2a52431b238fc3c25ea931def6e5b1cf9e78fb.tar.gz
git-6b2a52431b238fc3c25ea931def6e5b1cf9e78fb.tar.bz2
Merge branch 'pw/am-rebase-read-author-script'
Unify code to read the author-script used in "git am" and the commands that use the sequencer machinery, e.g. "git rebase -i". * pw/am-rebase-read-author-script: sequencer: use read_author_script() add read_author_script() to libgit am: rename read_author_script() am: improve author-script error reporting am: don't die in read_author_script()
Diffstat (limited to 'sequencer.c')
-rw-r--r--sequencer.c192
1 files changed, 121 insertions, 71 deletions
diff --git a/sequencer.c b/sequencer.c
index 405d5ef..1869435 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -669,55 +669,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;
}
@@ -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;
+ 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;
}