From: "Lukas Sandström" <lukass@etek.chalmers.se>
To: Junio C Hamano <junkio@cox.net>, Git Mailing List <git@vger.kernel.org>
Cc: "Lukas Sandström" <lukass@etek.chalmers.se>
Subject: [PATCH 7/8] Make it possible to call cmd_apply multiple times
Date: Tue, 13 Jun 2006 22:22:03 +0200 [thread overview]
Message-ID: <448F1E6B.6050507@etek.chalmers.se> (raw)
In-Reply-To: <448EF791.7070504@etek.chalmers.se>
* 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@etek.chalmers.se>
---
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
next prev parent reply other threads:[~2006-06-13 20:22 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <448EF791.7070504@etek.chalmers.se>
2006-06-13 20:21 ` [PATCH 1/8] Make git-write-tree a builtin Lukas Sandström
2006-06-13 20:21 ` [PATCH 2/8] Make git-mailsplit " Lukas Sandström
2006-06-13 20:21 ` [PATCH 3/8] Make git-mailinfo " Lukas Sandström
2006-06-13 20:21 ` [PATCH 4/8] Make git-stripspace " Lukas Sandström
2006-06-13 20:21 ` [PATCH 5/8] Make git-update-index " Lukas Sandström
2006-06-13 20:22 ` [PATCH 6/8] Make git-update-ref " Lukas Sandström
2006-06-14 2:22 ` Shawn Pearce
2006-06-13 20:22 ` Lukas Sandström [this message]
2006-06-13 20:22 ` [PATCH/RFC 8/8] Make git-am " Lukas Sandström
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=448F1E6B.6050507@etek.chalmers.se \
--to=lukass@etek.chalmers.se \
--cc=git@vger.kernel.org \
--cc=junkio@cox.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).