Thread (120 messages) 120 messages, 4 authors, 2016-06-16
STALE3642d
Revisions (2)
  1. v1 [diff vs current]
  2. v2 current

[PATCH v2 63/94] builtin/apply: make apply_all_patches() return -1 on error

From: Christian Couder <hidden>
Date: 2016-06-16 02:19:19
Subsystem: the rest · Maintainer: Linus Torvalds

To finish libifying the apply functionality, apply_all_patches() should not
die() or exit() in case of error, but return -1.

While doing that we must take care that file descriptors are properly closed
and, if needed, reset a sensible value.

Helped-by: Nguyễn Thái Ngọc Duy [off-list ref]
Helped-by: Johannes Schindelin [off-list ref]
Helped-by: Eric Sunshine [off-list ref]
Signed-off-by: Christian Couder <redacted>
---
 builtin/apply.c | 25 +++++++++++++------------
 1 file changed, 13 insertions(+), 12 deletions(-)
diff --git a/builtin/apply.c b/builtin/apply.c
index ae16f99..dd212c9 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4557,7 +4557,7 @@ static int apply_all_patches(struct apply_state *state,
 		if (!strcmp(arg, "-")) {
 			res = apply_patch(state, 0, "<stdin>", options);
 			if (res < 0)
-				exit(1);
+				return -1;
 			errs |= res;
 			read_stdin = 0;
 			continue;
@@ -4568,20 +4568,20 @@ static int apply_all_patches(struct apply_state *state,
 
 		fd = open(arg, O_RDONLY);
 		if (fd < 0)
-			die_errno(_("can't open patch '%s'"), arg);
+			return error(_("can't open patch '%s': %s"), arg, strerror(errno));
 		read_stdin = 0;
 		set_default_whitespace_mode(state);
 		res = apply_patch(state, fd, arg, options);
+		close(fd);
 		if (res < 0)
-			exit(1);
+			return -1;
 		errs |= res;
-		close(fd);
 	}
 	set_default_whitespace_mode(state);
 	if (read_stdin) {
 		res = apply_patch(state, 0, "<stdin>", options);
 		if (res < 0)
-			exit(1);
+			return -1;
 		errs |= res;
 	}
 
@@ -4596,10 +4596,10 @@ static int apply_all_patches(struct apply_state *state,
 				squelched);
 		}
 		if (state->ws_error_action == die_on_ws_error)
-			die(Q_("%d line adds whitespace errors.",
-			       "%d lines add whitespace errors.",
-			       state->whitespace_error),
-			    state->whitespace_error);
+			return error(Q_("%d line adds whitespace errors.",
+					"%d lines add whitespace errors.",
+					state->whitespace_error),
+				     state->whitespace_error);
 		if (state->applied_after_fixing_ws && state->apply)
 			warning("%d line%s applied after"
 				" fixing whitespace errors.",
@@ -4613,9 +4613,10 @@ static int apply_all_patches(struct apply_state *state,
 	}
 
 	if (state->update_index) {
-		if (write_locked_index(&the_index, state->lock_file, COMMIT_LOCK))
-			die(_("Unable to write new index file"));
+		res = write_locked_index(&the_index, state->lock_file, COMMIT_LOCK);
 		state->newfd = -1;
+		if (res)
+			return error(_("Unable to write new index file"));
 	}
 
 	return !!errs;
@@ -4705,5 +4706,5 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
 	if (check_apply_state(&state, force_apply))
 		exit(1);
 
-	return apply_all_patches(&state, argc, argv, options);
+	return !!apply_all_patches(&state, argc, argv, options);
 }
-- 
2.8.2.490.g3dabe57
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help