[PATCH 7/8] Make it possible to call cmd_apply multiple times

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



* xmalloc a new struct lock_file each invocation.
* Don't die() if the patch doesn't apply.
* Initialize the global variables each invocation.
* Roll back the lock_file.

Signed-off-by: Lukas Sandström <lukass@xxxxxxxxxxxxxxxx>
---
 builtin-apply.c |   94 +++++++++++++++++++++++++++++++++++--------------------
 1 files changed, 60 insertions(+), 34 deletions(-)

diff --git a/builtin-apply.c b/builtin-apply.c
index e113c74..a76c553 100644
--- a/builtin-apply.c
+++ b/builtin-apply.c
@@ -22,24 +22,13 @@ #include "builtin.h"
 //  --index updates the cache as well.
 //  --cached updates only the cache without ever touching the working tree.
 //
-static const char *prefix;
-static int prefix_length = -1;
-static int newfd = -1;
-
-static int p_value = 1;
-static int allow_binary_replacement = 0;
-static int check_index = 0;
-static int write_index = 0;
-static int cached = 0;
-static int diffstat = 0;
-static int numstat = 0;
-static int summary = 0;
-static int check = 0;
-static int apply = 1;
-static int no_add = 0;
-static int show_index_info = 0;
-static int line_termination = '\n';
+static const char *prefix, *patch_input_file;
+static int prefix_length, newfd, p_value, allow_binary_replacement, check_index,
+	write_index, cached, diffstat, numstat, summary, check, apply, no_add,
+	show_index_info, line_termination, whitespace_error, squelch_whitespace_errors,
+	applied_after_stripping;
 static unsigned long p_context = -1;
+
 static const char apply_usage[] =
 "git-apply [--stat] [--numstat] [--summary] [--check] [--index] [--cached] [--apply] [--no-add] [--index-info] [--allow-binary-replacement] [-z] [-pNUM] [-CNUM] [--whitespace=<nowarn|warn|error|error-all|strip>] <patch>...";
 
@@ -49,10 +38,31 @@ static enum whitespace_eol {
 	error_on_whitespace,
 	strip_whitespace,
 } new_whitespace = warn_on_whitespace;
-static int whitespace_error = 0;
-static int squelch_whitespace_errors = 5;
-static int applied_after_stripping = 0;
-static const char *patch_input_file = NULL;
+
+static void setup_state()
+{
+	prefix = NULL;
+	prefix_length = -1;
+	newfd = -1;
+	p_value = 1;
+	allow_binary_replacement = 0;
+	check_index = 0;
+	write_index = 0;
+	cached = 0;
+	diffstat = 0;
+	numstat = 0;
+	summary = 0;
+	check = 0;
+	apply = 1;
+	no_add = 0;
+	show_index_info = 0;
+	line_termination = '\n';
+	p_context = -1;
+	whitespace_error = 0;
+	squelch_whitespace_errors = 5;
+	applied_after_stripping = 0;
+	patch_input_file = NULL;
+}
 
 static void parse_whitespace_option(const char *option)
 {
@@ -2072,7 +2082,7 @@ static void write_out_results(struct pat
 	}
 }
 
-static struct lock_file lock_file;
+static struct lock_file *lock_file;
 
 static struct excludes {
 	struct excludes *next;
@@ -2106,7 +2116,7 @@ static int apply_patch(int fd, const cha
 
 	patch_input_file = filename;
 	if (!buffer)
-		return -1;
+		return -2;
 	offset = 0;
 	while (size > 0) {
 		struct patch *patch;
@@ -2134,7 +2144,7 @@ static int apply_patch(int fd, const cha
 
 	write_index = check_index && apply;
 	if (write_index && newfd < 0) {
-		newfd = hold_lock_file_for_update(&lock_file,
+		newfd = hold_lock_file_for_update(lock_file,
 						  get_index_file());
 		if (newfd < 0)
 			die("unable to create new index file");
@@ -2145,7 +2155,7 @@ static int apply_patch(int fd, const cha
 	}
 
 	if ((check || apply) && check_patch_list(list) < 0)
-		exit(1);
+		return -1;
 
 	if (apply)
 		write_out_results(list, skipped_patch);
@@ -2175,20 +2185,27 @@ static int git_apply_config(const char *
 	return git_default_config(var, value);
 }
 
-
 int cmd_apply(int argc, const char **argv, char **envp)
 {
-	int i;
+	int i, ret = 0;
 	int read_stdin = 1;
 	const char *whitespace_option = NULL;
 
+	setup_state();
+
+	/* This memory can't be free()'d since it's needed atexit() */
+	lock_file = xmalloc(sizeof(struct lock_file));
+
 	for (i = 1; i < argc; i++) {
 		const char *arg = argv[i];
 		char *end;
-		int fd;
+		int fd, apply_status;
 
 		if (!strcmp(arg, "-")) {
-			apply_patch(0, "<stdin>");
+			if (apply_patch(0, "<stdin>")) {
+				ret = 1;
+				goto err;
+			}
 			read_stdin = 0;
 			continue;
 		}
@@ -2281,12 +2298,18 @@ int cmd_apply(int argc, const char **arg
 			usage(apply_usage);
 		read_stdin = 0;
 		set_default_whitespace_mode(whitespace_option);
-		apply_patch(fd, arg);
+		apply_status = apply_patch(fd, arg);
 		close(fd);
+		if (apply_status) {
+			ret = 1;
+			goto err;
+		}
 	}
 	set_default_whitespace_mode(whitespace_option);
-	if (read_stdin)
-		apply_patch(0, "<stdin>");
+	if (read_stdin && apply_patch(0, "<stdin>")) {
+		ret = 1;
+		goto err;
+	}
 	if (whitespace_error) {
 		if (squelch_whitespace_errors &&
 		    squelch_whitespace_errors < whitespace_error) {
@@ -2316,9 +2339,12 @@ int cmd_apply(int argc, const char **arg
 
 	if (write_index) {
 		if (write_cache(newfd, active_cache, active_nr) ||
-		    commit_lock_file(&lock_file))
+		    commit_lock_file(lock_file))
 			die("Unable to write new index file");
 	}
 
-	return 0;
+err:
+	rollback_lock_file(lock_file);
+
+	return ret;
 }
-- 
1.4.0


-
: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]