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

[PATCH v2 2/3] revision: small readability improvement for reading from stdin

From
Patrick Steinhardt <ps@pks.im>
Date
Jun 15, 2023, 14:39 UTC
Message-ID
<5c1a9a0d08b25ce94ba1498cd88051abd062369c.1686839572.git.ps@pks.im>
In-Reply-To
<cover.1686839572.git.ps@pks.im>

The code that reads lines from standard input manually compares whether the read line matches "--", which is a bit awkward to read. Furthermore, we're about to extend the code to also support reading pseudo-options via standard input, requiring more elaborate handling of lines with a leading dash.

Refactor the code by hoisting out the check for "--" outside of the block that checks for a leading dash.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 revision.c | 17 +++++++++--------
 1 file changed, 9 insertions(+), 8 deletions(-)
diff --git a/revision.c b/revision.c
index cc22ccd76e..3a39f41bb8 100644
--- a/revision.c
+++ b/revision.c
@@ -2795,16 +2795,17 @@ static void read_revisions_from_stdin(struct rev_info *revs,
 
 	strbuf_init(&sb, 1000);
 	while (strbuf_getline(&sb, stdin) != EOF) {
-		int len = sb.len;
-		if (!len)
+		if (!sb.len)
+			break;
+
+		if (!strcmp(sb.buf, "--")) {
+			seen_dashdash = 1;
 			break;
-		if (sb.buf[0] == '-') {
-			if (len == 2 && sb.buf[1] == '-') {
-				seen_dashdash = 1;
-				break;
-			}
-			die("options not supported in --stdin mode");
 		}
+
+		if (sb.buf[0] == '-')
+			die("options not supported in --stdin mode");
+
 		if (handle_revision_arg(sb.buf, revs, 0,
 					REVARG_CANNOT_BE_FILENAME))
 			die("bad revision '%s'", sb.buf);
-- 
2.41.0
Previous: Patrick Steinhardt
Message 12 of 12 in “revision: handle pseudo-opts in `--stdin` mode”
  1. 0/3 revision: handle pseudo-opts in `--stdin` modePatrick Steinhardt, Jun 14, 2023
  2. 1/3 revision: reorder `read_revisions_from_stdin()`Patrick Steinhardt, Jun 14, 2023
  3. Junio C HamanoJun 14, 2023
  4. 2/3 revision: small readability improvement for reading from stdinPatrick Steinhardt, Jun 14, 2023
  5. Junio C HamanoJun 14, 2023
  6. 3/3 revision: handle pseudo-opts in `--stdin` modePatrick Steinhardt, Jun 14, 2023
  7. Junio C HamanoJun 14, 2023
  8. Patrick SteinhardtJun 15, 2023
  9. 0/3 revision: handle pseudo-opts in `--stdin` modePatrick Steinhardt, Jun 15, 2023
  10. 1/3 revision: reorder `read_revisions_from_stdin()`Patrick Steinhardt, Jun 15, 2023
  11. 3/3 revision: handle pseudo-opts in `--stdin` modePatrick Steinhardt, Jun 15, 2023
  12. 2/3 revision: small readability improvement for reading from stdinPatrick Steinhardt, Jun 15, 2023

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.