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()
This commit is contained in:
60
builtin/am.c
60
builtin/am.c
@ -260,32 +260,6 @@ static int read_state_file(struct strbuf *sb, const struct am_state *state,
|
|||||||
die_errno(_("could not read '%s'"), am_path(state, file));
|
die_errno(_("could not read '%s'"), am_path(state, file));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 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)
|
|
||||||
return -1;
|
|
||||||
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 -1;
|
|
||||||
item->util = xstrdup(cp);
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads and parses the state directory's "author-script" file, and sets
|
* Reads and parses the state directory's "author-script" file, and sets
|
||||||
* state->author_name, state->author_email and state->author_date accordingly.
|
* state->author_name, state->author_email and state->author_date accordingly.
|
||||||
@ -302,42 +276,16 @@ static int parse_key_value_squoted(char *buf, struct string_list *list)
|
|||||||
* script, and thus if the file differs from what this function expects, it is
|
* script, 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.
|
* better to bail out than to do something that the user does not expect.
|
||||||
*/
|
*/
|
||||||
static int read_author_script(struct am_state *state)
|
static int read_am_author_script(struct am_state *state)
|
||||||
{
|
{
|
||||||
const char *filename = am_path(state, "author-script");
|
const char *filename = am_path(state, "author-script");
|
||||||
struct strbuf buf = STRBUF_INIT;
|
|
||||||
struct string_list kv = STRING_LIST_INIT_DUP;
|
|
||||||
int retval = -1; /* assume failure */
|
|
||||||
int fd;
|
|
||||||
|
|
||||||
assert(!state->author_name);
|
assert(!state->author_name);
|
||||||
assert(!state->author_email);
|
assert(!state->author_email);
|
||||||
assert(!state->author_date);
|
assert(!state->author_date);
|
||||||
|
|
||||||
fd = open(filename, O_RDONLY);
|
return read_author_script(filename, &state->author_name,
|
||||||
if (fd < 0) {
|
&state->author_email, &state->author_date, 1);
|
||||||
if (errno == ENOENT)
|
|
||||||
return 0;
|
|
||||||
die_errno(_("could not open '%s' for reading"), filename);
|
|
||||||
}
|
|
||||||
strbuf_read(&buf, fd, 0);
|
|
||||||
close(fd);
|
|
||||||
if (parse_key_value_squoted(buf.buf, &kv))
|
|
||||||
goto finish;
|
|
||||||
|
|
||||||
if (kv.nr != 3 ||
|
|
||||||
strcmp(kv.items[0].string, "GIT_AUTHOR_NAME") ||
|
|
||||||
strcmp(kv.items[1].string, "GIT_AUTHOR_EMAIL") ||
|
|
||||||
strcmp(kv.items[2].string, "GIT_AUTHOR_DATE"))
|
|
||||||
goto finish;
|
|
||||||
state->author_name = kv.items[0].util;
|
|
||||||
state->author_email = kv.items[1].util;
|
|
||||||
state->author_date = kv.items[2].util;
|
|
||||||
retval = 0;
|
|
||||||
finish:
|
|
||||||
string_list_clear(&kv, !!retval);
|
|
||||||
strbuf_release(&buf);
|
|
||||||
return retval;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -411,7 +359,7 @@ static void am_load(struct am_state *state)
|
|||||||
BUG("state file 'last' does not exist");
|
BUG("state file 'last' does not exist");
|
||||||
state->last = strtol(sb.buf, NULL, 10);
|
state->last = strtol(sb.buf, NULL, 10);
|
||||||
|
|
||||||
if (read_author_script(state) < 0)
|
if (read_am_author_script(state) < 0)
|
||||||
die(_("could not parse author script"));
|
die(_("could not parse author script"));
|
||||||
|
|
||||||
read_commit_msg(state);
|
read_commit_msg(state);
|
||||||
|
192
sequencer.c
192
sequencer.c
@ -669,55 +669,131 @@ missing_author:
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
/*
|
* Take a series of KEY='VALUE' lines where VALUE part is
|
||||||
* write_author_script() used to fail to terminate the last line with a "'" and
|
* sq-quoted, and append <KEY, VALUE> at the end of the string list
|
||||||
* 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.
|
|
||||||
*/
|
*/
|
||||||
static int quoting_is_broken(const char *s, size_t n)
|
static int parse_key_value_squoted(char *buf, struct string_list *list)
|
||||||
{
|
{
|
||||||
/* Skip any empty lines in case the file was hand edited */
|
while (*buf) {
|
||||||
while (n > 0 && s[--n] == '\n')
|
struct string_list_item *item;
|
||||||
; /* empty */
|
char *np;
|
||||||
if (n > 0 && s[n] != '\'')
|
char *cp = strchr(buf, '=');
|
||||||
return 1;
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
int read_author_script(const char *path, char **name, char **email, char **date,
|
||||||
|
int allow_missing)
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
|
||||||
|
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
|
* Read a GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL AND GIT_AUTHOR_DATE from a
|
||||||
* file) into an environment block. Returns -1 on error, 0 otherwise.
|
* file with shell quoting into struct argv_array. Returns -1 on
|
||||||
|
* error, 0 otherwise.
|
||||||
*/
|
*/
|
||||||
static int read_env_script(struct argv_array *env)
|
static int read_env_script(struct argv_array *env)
|
||||||
{
|
{
|
||||||
struct strbuf script = STRBUF_INIT;
|
char *name, *email, *date;
|
||||||
int i, count = 0, sq_bug;
|
|
||||||
const char *p2;
|
|
||||||
char *p;
|
|
||||||
|
|
||||||
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;
|
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_pushf(env, "GIT_AUTHOR_NAME=%s", name);
|
||||||
argv_array_push(env, p);
|
argv_array_pushf(env, "GIT_AUTHOR_EMAIL=%s", email);
|
||||||
p += strlen(p) + 1;
|
argv_array_pushf(env, "GIT_AUTHOR_DATE=%s", date);
|
||||||
}
|
free(name);
|
||||||
|
free(email);
|
||||||
|
free(date);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -737,54 +813,28 @@ static char *get_author(const char *message)
|
|||||||
/* Read author-script and return an ident line (author <email> timestamp) */
|
/* Read author-script and return an ident line (author <email> timestamp) */
|
||||||
static const char *read_author_ident(struct strbuf *buf)
|
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;
|
struct strbuf out = STRBUF_INIT;
|
||||||
char *in, *eol;
|
char *name, *email, *date;
|
||||||
const char *val[3];
|
|
||||||
int i = 0;
|
|
||||||
|
|
||||||
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;
|
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 */
|
/* 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'"),
|
warning(_("invalid date format '%s' in '%s'"),
|
||||||
val[2], rebase_path_author_script());
|
date, rebase_path_author_script());
|
||||||
strbuf_release(&out);
|
strbuf_release(&out);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
strbuf_reset(&out);
|
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_swap(buf, &out);
|
||||||
strbuf_release(&out);
|
strbuf_release(&out);
|
||||||
|
free(name);
|
||||||
|
free(email);
|
||||||
|
free(date);
|
||||||
return buf->buf;
|
return buf->buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,6 +130,9 @@ int prepare_branch_to_be_rebased(struct replay_opts *opts, const char *commit);
|
|||||||
#define SUMMARY_SHOW_AUTHOR_DATE (1 << 1)
|
#define SUMMARY_SHOW_AUTHOR_DATE (1 << 1)
|
||||||
void print_commit_summary(const char *prefix, const struct object_id *oid,
|
void print_commit_summary(const char *prefix, const struct object_id *oid,
|
||||||
unsigned int flags);
|
unsigned int flags);
|
||||||
|
|
||||||
|
int read_author_script(const char *path, char **name, char **email, char **date,
|
||||||
|
int allow_missing);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void parse_strategy_opts(struct replay_opts *opts, char *raw_opts);
|
void parse_strategy_opts(struct replay_opts *opts, char *raw_opts);
|
||||||
|
Reference in New Issue
Block a user