From: Phillip Wood <phillip.wood@talktalk.net>
To: Git Mailing List <git@vger.kernel.org>,
Junio C Hamano <gitster@pobox.com>
Cc: Eric Sunshine <sunshine@sunshineco.com>,
Johannes Schindelin <Johannes.Schindelin@gmx.de>,
Phillip Wood <phillip.wood@dunelm.org.uk>
Subject: [PATCH v3 5/5] sequencer: use read_author_script()
Date: Tue, 30 Oct 2018 10:39:16 +0000 [thread overview]
Message-ID: <20181030103916.6241-6-phillip.wood@talktalk.net> (raw)
In-Reply-To: <20181030103916.6241-1-phillip.wood@talktalk.net>
From: Phillip Wood <phillip.wood@dunelm.org.uk>
Use the new function added in the last commit to read the author
script, updating read_env_script() and read_author_ident(). We now
have a single code path that reads the author script for am and all
flavors of rebase. This changes the behavior of read_env_script() as
previously it would set any environment variables that were in the
author-script file. Now it is an error if the file contains other
variables or any of GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL and
GIT_AUTHOR_DATE are missing. This is what am and the non interactive
version of rebase have been doing for several years so hopefully it
will not cause a problem for interactive rebase users. The advantage
is that we are reusing existing code from am which uses sq_dequote()
to properly dequote variables. This fixes potential problems with user
edited scripts as read_env_script() which did not track quotes
properly.
This commit also removes the fallback code for checking for a broken
author script after git is upgraded when a rebase is stopped. Now that
the parsing uses sq_dequote() it will reliably return an error if the
quoting is broken and the user will have to abort the rebase and
restart. This isn't ideal but it's a corner case and the detection of
the broken quoting could be confused by user edited author scripts.
Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
---
Notes:
changes since v1
- use argv_array_pushf() as suggested by Eric
- fixed strbuf handling as suggested by Eric
- fix comments and commit message to reflect changed behavior of
read_env_script()
sequencer.c | 97 ++++++++++++-----------------------------------------
1 file changed, 21 insertions(+), 76 deletions(-)
diff --git a/sequencer.c b/sequencer.c
index 3530dbeb6c..987542f67c 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -767,53 +767,24 @@ int read_author_script(const char *path, char **name, char **email, char **date,
}
/*
- * 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.
- */
-static int quoting_is_broken(const char *s, size_t n)
-{
- /* 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;
-
- return 0;
-}
-
-/*
- * 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;
}
@@ -833,54 +804,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;
}
--
2.19.1
next prev parent reply other threads:[~2018-10-30 10:39 UTC|newest]
Thread overview: 41+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-09-12 10:10 [PATCH 0/3] am/rebase: share read_author_script() Phillip Wood
2018-09-12 10:10 ` [PATCH 1/3] am: rename read_author_script() Phillip Wood
2018-09-12 10:10 ` [PATCH 2/3] add read_author_script() to libgit Phillip Wood
2018-09-13 23:49 ` Eric Sunshine
2018-10-10 10:14 ` Phillip Wood
2018-10-10 10:22 ` Eric Sunshine
2018-09-12 10:10 ` [PATCH 3/3] sequencer: use read_author_script() Phillip Wood
2018-09-12 12:14 ` Eric Sunshine
2018-09-14 0:31 ` Eric Sunshine
2018-10-10 10:35 ` Phillip Wood
2018-10-18 10:00 ` [PATCH v2 0/5] am/rebase: share read_author_script() Phillip Wood
2018-10-18 10:00 ` [PATCH v2 1/5] am: don't die in read_author_script() Phillip Wood
2018-10-25 8:44 ` Junio C Hamano
2018-10-18 10:00 ` [PATCH v2 2/5] am: improve author-script error reporting Phillip Wood
2018-10-25 8:55 ` Junio C Hamano
2018-10-18 10:00 ` [PATCH v2 3/5] am: rename read_author_script() Phillip Wood
2018-10-18 10:00 ` [PATCH v2 4/5] add read_author_script() to libgit Phillip Wood
2018-10-18 10:00 ` [PATCH v2 5/5] sequencer: use read_author_script() Phillip Wood
2018-10-25 8:59 ` [PATCH v2 0/5] am/rebase: share read_author_script() Junio C Hamano
2018-10-26 9:00 ` Phillip Wood
2018-10-26 13:32 ` Junio C Hamano
2018-10-30 10:39 ` [PATCH v3 " Phillip Wood
2018-10-30 10:39 ` [PATCH v3 1/5] am: don't die in read_author_script() Phillip Wood
2018-10-30 10:39 ` [PATCH v3 2/5] am: improve author-script error reporting Phillip Wood
2018-10-30 10:39 ` [PATCH v3 3/5] am: rename read_author_script() Phillip Wood
2018-10-30 10:39 ` [PATCH v3 4/5] add read_author_script() to libgit Phillip Wood
2018-10-30 10:39 ` Phillip Wood [this message]
2018-10-31 2:50 ` [PATCH v3 0/5] am/rebase: share read_author_script() Junio C Hamano
2018-10-31 10:14 ` Phillip Wood
2018-10-31 10:15 ` [PATCH v4 " Phillip Wood
2018-10-31 10:15 ` [PATCH v4 1/5] am: don't die in read_author_script() Phillip Wood
2018-10-31 10:15 ` [PATCH v4 2/5] am: improve author-script error reporting Phillip Wood
2018-11-04 4:12 ` Eric Sunshine
2018-11-05 1:53 ` Junio C Hamano
2018-10-31 10:15 ` [PATCH v4 3/5] am: rename read_author_script() Phillip Wood
2018-10-31 10:15 ` [PATCH v4 4/5] add read_author_script() to libgit Phillip Wood
2018-10-31 10:15 ` [PATCH v4 5/5] sequencer: use read_author_script() Phillip Wood
2018-11-01 3:01 ` [PATCH v4 0/5] am/rebase: share read_author_script() Junio C Hamano
2018-11-01 3:12 ` [PATCH v4fixed 2/5] am: improve author-script error reporting Junio C Hamano
2018-11-01 3:12 ` [PATCH v4fixed 4/5] add read_author_script() to libgit Junio C Hamano
2018-11-01 16:27 ` [PATCH v4 0/5] am/rebase: share read_author_script() Phillip Wood
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: http://vger.kernel.org/majordomo-info.html
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20181030103916.6241-6-phillip.wood@talktalk.net \
--to=phillip.wood@talktalk.net \
--cc=Johannes.Schindelin@gmx.de \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=phillip.wood@dunelm.org.uk \
--cc=sunshine@sunshineco.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://80x24.org/mirrors/git.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).