git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 3/3] sequencer: use read_author_script()

From
PWPhillip Wood <phillip.wood@talktalk.net>
Date
Sep 12, 2018, 10:10 UTC
Message-ID
<20180912101029.28052-4-phillip.wood@talktalk.net>
In-Reply-To
<20180912101029.28052-1-phillip.wood@talktalk.net>
From: Phillip Wood <phillip.wood@dunelm.org.uk>

Use the new function to read the author script, updating read_env_script() and read_author_ident(). This means we now have a single code path that reads the author script and uses sq_dequote() to dequote it. 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>
---
 sequencer.c | 108 +++++++++++++++-------------------------------------
 1 file changed, 30 insertions(+), 78 deletions(-)
diff --git a/sequencer.c b/sequencer.c
index 5d0ff8f1c1..630741cfe0 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -723,54 +723,35 @@ int read_author_script(const char *path, char **name, char **email, char **date,
 	return retval;
 }
 
-/*
- * 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.
  */
 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;
-	}
+	strbuf_addstr(&script, "GIT_AUTHOR_NAME=");
+	strbuf_addstr(&script, name);
+	argv_array_push(env, script.buf);
+	strbuf_reset(&script);
+	strbuf_addstr(&script, "GIT_AUTHOR_EMAIL=");
+	strbuf_addstr(&script, email);
+	argv_array_push(env, script.buf);
+	strbuf_reset(&script);
+	strbuf_addstr(&script, "GIT_AUTHOR_DATE=");
+	strbuf_addstr(&script, date);
+	argv_array_push(env, script.buf);
+	strbuf_release(&script);
+
+	free(name);
+	free(email);
+	free(date);
 
 	return 0;
 }
@@ -790,54 +771,25 @@ 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, buf)){
 		warning(_("invalid date format '%s' in '%s'"),
-			val[2], rebase_path_author_script());
-		strbuf_release(&out);
+			date, rebase_path_author_script());
+		strbuf_release(buf);
 		return NULL;
 	}
 
-	strbuf_reset(&out);
-	strbuf_addstr(&out, fmt_ident(val[0], val[1], val[2], 0));
-	strbuf_swap(buf, &out);
-	strbuf_release(&out);
+	strbuf_reset(buf);
+	strbuf_addstr(buf, fmt_ident(name, email, date, 0));
+	free(name);
+	free(email);
+	free(date);
 	return buf->buf;
 }
 
-- 
2.18.0
Previous: Eric SunshineNext: Eric Sunshine
Message 7 of 41 in “am/rebase: share read_author_script()”
  1. 0/3 am/rebase: share read_author_script()Phillip Wood, Sep 12, 2018
  2. 1/3 am: rename read_author_script()Phillip Wood, Sep 12, 2018
  3. 2/3 add read_author_script() to libgitPhillip Wood, Sep 12, 2018
  4. Eric SunshineSep 13, 2018
  5. Phillip WoodOct 10, 2018
  6. Eric SunshineOct 10, 2018
  7. 3/3 sequencer: use read_author_script()Phillip Wood, Sep 12, 2018
  8. Eric SunshineSep 12, 2018
  9. Eric SunshineSep 14, 2018
  10. Phillip WoodOct 10, 2018
  11. 0/5 am/rebase: share read_author_script()Phillip Wood, Oct 18, 2018
  12. 1/5 am: don't die in read_author_script()Phillip Wood, Oct 18, 2018
  13. Junio C HamanoOct 25, 2018
  14. 2/5 am: improve author-script error reportingPhillip Wood, Oct 18, 2018
  15. Junio C HamanoOct 25, 2018
  16. 3/5 am: rename read_author_script()Phillip Wood, Oct 18, 2018
  17. 5/5 sequencer: use read_author_script()Phillip Wood, Oct 18, 2018
  18. 4/5 add read_author_script() to libgitPhillip Wood, Oct 18, 2018
  19. Junio C HamanoOct 25, 2018
  20. Phillip WoodOct 26, 2018
  21. Junio C HamanoOct 26, 2018
  22. 0/5 am/rebase: share read_author_script()Phillip Wood, Oct 30, 2018
  23. 2/5 am: improve author-script error reportingPhillip Wood, Oct 30, 2018
  24. 1/5 am: don't die in read_author_script()Phillip Wood, Oct 30, 2018
  25. 4/5 add read_author_script() to libgitPhillip Wood, Oct 30, 2018
  26. 5/5 sequencer: use read_author_script()Phillip Wood, Oct 30, 2018
  27. 3/5 am: rename read_author_script()Phillip Wood, Oct 30, 2018
  28. Junio C HamanoOct 31, 2018
  29. Phillip WoodOct 31, 2018
  30. 0/5 am/rebase: share read_author_script()Phillip Wood, Oct 31, 2018
  31. 2/5 am: improve author-script error reportingPhillip Wood, Oct 31, 2018
  32. Eric SunshineNov 4, 2018
  33. Junio C HamanoNov 5, 2018
  34. 3/5 am: rename read_author_script()Phillip Wood, Oct 31, 2018
  35. 5/5 sequencer: use read_author_script()Phillip Wood, Oct 31, 2018
  36. 4/5 add read_author_script() to libgitPhillip Wood, Oct 31, 2018
  37. 1/5 am: don't die in read_author_script()Phillip Wood, Oct 31, 2018
  38. Junio C HamanoNov 1, 2018
  39. 2/5 am: improve author-script error reportingJunio C Hamano, Nov 1, 2018
  40. 4/5 add read_author_script() to libgitJunio C Hamano, Nov 1, 2018
  41. Phillip WoodNov 1, 2018

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.