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

[PATCH 3/4] Move the file read logic to read_patch_file() in builtin-apply.c

From
Mike Hommey <mh@glandium.org>
Date
Dec 9, 2007, 17:04 UTC
Message-ID
<1197219900-19334-3-git-send-email-mh@glandium.org>
In-Reply-To
<1197219900-19334-2-git-send-email-mh@glandium.org>

This will allow to extend the read logic further. We also return a better error message than usage() when the given filename can't be opened, and avoid whitespace options not being set when reading from stdin with the "-" argument as a side effect.

Signed-off-by: Mike Hommey <mh@glandium.org>
---
 builtin-apply.c |   32 +++++++++++++++++---------------
 1 files changed, 17 insertions(+), 15 deletions(-)
diff --git a/builtin-apply.c b/builtin-apply.c
index f2e9a33..8c8162a 100644
--- a/builtin-apply.c
+++ b/builtin-apply.c
@@ -182,11 +182,23 @@ static void say_patch_name(FILE *output, const char *pre,
 #define CHUNKSIZE (8192)
 #define SLOP (16)
 
-static void read_patch_file(struct strbuf *sb, int fd)
+static void read_patch_file(struct strbuf *sb, const char *filename)
 {
+	int fd;
+
+	if (!strcmp(filename, "-")) {
+		fd = 0;
+	} else {
+		fd = open(filename, O_RDONLY);
+		if (fd < 0)
+			die("git-apply: could not open %s: %s", filename,
+			    strerror(errno));
+	}
+
 	if (strbuf_read(sb, fd, 0) < 0)
 		die("git-apply: read returned %s", strerror(errno));
 
+	close(fd);
 	/*
 	 * Make sure that we have some slop in the buffer
 	 * so that we can do speculative "memcmp" etc, and
@@ -2705,7 +2717,7 @@ static void prefix_patches(struct patch *p)
 	}
 }
 
-static int apply_patch(int fd, const char *filename, int inaccurate_eof)
+static int apply_patch(const char *filename, int inaccurate_eof)
 {
 	size_t offset;
 	struct strbuf buf;
@@ -2714,7 +2726,7 @@ static int apply_patch(int fd, const char *filename, int inaccurate_eof)
 
 	strbuf_init(&buf, 0);
 	patch_input_file = filename;
-	read_patch_file(&buf, fd);
+	read_patch_file(&buf, filename);
 	offset = 0;
 	while (offset < buf.len) {
 		struct patch *patch;
@@ -2807,13 +2819,7 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)
 	for (i = 1; i < argc; i++) {
 		const char *arg = argv[i];
 		char *end;
-		int fd;
 
-		if (!strcmp(arg, "-")) {
-			errs |= apply_patch(0, "<stdin>", inaccurate_eof);
-			read_stdin = 0;
-			continue;
-		}
 		if (!prefixcmp(arg, "--exclude=")) {
 			struct excludes *x = xmalloc(sizeof(*x));
 			x->path = arg + 10;
@@ -2916,17 +2922,13 @@ int cmd_apply(int argc, const char **argv, const char *unused_prefix)
 		if (0 < prefix_length)
 			arg = prefix_filename(prefix, prefix_length, arg);
 
-		fd = open(arg, O_RDONLY);
-		if (fd < 0)
-			usage(apply_usage);
 		read_stdin = 0;
 		set_default_whitespace_mode(whitespace_option);
-		errs |= apply_patch(fd, arg, inaccurate_eof);
-		close(fd);
+		errs |= apply_patch(arg, inaccurate_eof);
 	}
 	set_default_whitespace_mode(whitespace_option);
 	if (read_stdin)
-		errs |= apply_patch(0, "<stdin>", inaccurate_eof);
+		errs |= apply_patch("-", inaccurate_eof);
 	if (whitespace_error) {
 		if (squelch_whitespace_errors &&
 		    squelch_whitespace_errors < whitespace_error) {
-- 
1.5.3.7
Previous: Mike HommeyNext: Mike Hommey
Message 3 of 23 in “Cleanup variables in http.[ch]”
  1. 1/4 Cleanup variables in http.[ch]Mike Hommey, Dec 9, 2007
  2. 2/4 Use strbuf in http codeMike Hommey, Dec 9, 2007
  3. 3/4 Move the file read logic to read_patch_file() in builtin-apply.cMike Hommey, Dec 9, 2007
  4. 4/4 Add support for URLs to git-applyMike Hommey, Dec 9, 2007
  5. 4/4 Add support for URLs to git-applyMike Hommey, Dec 10, 2007
  6. Junio C HamanoDec 9, 2007
  7. Junio C HamanoDec 9, 2007
  8. Mike HommeyDec 9, 2007
  9. Junio C HamanoDec 9, 2007
  10. 2/4 Use strbuf in http codeMike Hommey, Dec 9, 2007
  11. Junio C HamanoDec 11, 2007
  12. Mike HommeyDec 11, 2007
  13. 2/4 Use strbuf in http codeMike Hommey, Dec 11, 2007
  14. git-send-email.perl: Really add angle brackets to In-Reply-To if necessaryMike Hommey, Dec 9, 2007
  15. Mike HommeyDec 9, 2007
  16. Junio C HamanoDec 9, 2007
  17. Mike HommeyDec 9, 2007
  18. David KastrupDec 9, 2007
  19. Randal L. SchwartzDec 9, 2007
  20. David KastrupDec 9, 2007
  21. Randal L. SchwartzDec 9, 2007
  22. git-send-email.perl: Really add angle brackets to In-Reply-To if necessaryMike Hommey, Dec 9, 2007
  23. Junio C HamanoDec 9, 2007

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.