[PATCH v3 39/49] builtin/apply: move 'applied_after_fixing_ws' into 'struct apply_state'

2016-05-24 Thread Christian Couder
To libify the apply functionality the 'applied_after_fixing_ws' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 10 +-
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 619b8fb..91b6283 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -64,6 +64,7 @@ struct apply_state {
const char *whitespace_option;
int whitespace_error;
int squelch_whitespace_errors;
+   int applied_after_fixing_ws;
 };
 
 static int newfd = -1;
@@ -79,7 +80,6 @@ static enum ws_error_action {
die_on_ws_error,
correct_ws_error
 } ws_error_action = warn_on_ws_error;
-static int applied_after_fixing_ws;
 
 static enum ws_ignore {
ignore_ws_none,
@@ -2862,7 +2862,7 @@ static int apply_one_fragment(struct apply_state *state,
strbuf_add(, patch + 1, plen);
}
else {
-   ws_fix_copy(, patch + 1, plen, 
ws_rule, _after_fixing_ws);
+   ws_fix_copy(, patch + 1, plen, 
ws_rule, >applied_after_fixing_ws);
}
add_line_info(, newlines.buf + start, 
newlines.len - start,
  (first == '+' ? 0 : LINE_COMMON));
@@ -4806,11 +4806,11 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix)
   "%d lines add whitespace errors.",
   state.whitespace_error),
state.whitespace_error);
-   if (applied_after_fixing_ws && state.apply)
+   if (state.applied_after_fixing_ws && state.apply)
warning("%d line%s applied after"
" fixing whitespace errors.",
-   applied_after_fixing_ws,
-   applied_after_fixing_ws == 1 ? "" : "s");
+   state.applied_after_fixing_ws,
+   state.applied_after_fixing_ws == 1 ? "" : "s");
else if (state.whitespace_error)
warning(Q_("%d line adds whitespace errors.",
   "%d lines add whitespace errors.",
-- 
2.8.3.443.gaeee61e

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


[PATCH v3 12/49] builtin/apply: move 'check_index' global into 'struct apply_state'

2016-05-24 Thread Christian Couder
To libify the apply functionality the 'check_index' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 66 -
 1 file changed, 37 insertions(+), 29 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 55a5541..769383c 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -27,6 +27,7 @@ struct apply_state {
 
/* These control what gets looked at and modified */
int check; /* preimage must match working tree, don't actually apply */
+   int check_index; /* preimage must match the indexed version */
 
/* These boolean parameters control how the apply is done */
int unidiff_zero;
@@ -36,14 +37,12 @@ struct apply_state {
  *  --stat does just a diffstat, and doesn't actually apply
  *  --numstat does numeric diffstat, and doesn't actually apply
  *  --index-info shows the old and new index info for paths if available.
- *  --index updates the cache as well.
  *  --cached updates only the cache without ever touching the working tree.
  */
 static int newfd = -1;
 
 static int state_p_value = 1;
 static int p_value_known;
-static int check_index;
 static int update_index;
 static int cached;
 static int diffstat;
@@ -3245,13 +3244,14 @@ static int verify_index_match(const struct cache_entry 
*ce, struct stat *st)
 
 #define SUBMODULE_PATCH_WITHOUT_INDEX 1
 
-static int load_patch_target(struct strbuf *buf,
+static int load_patch_target(struct apply_state *state,
+struct strbuf *buf,
 const struct cache_entry *ce,
 struct stat *st,
 const char *name,
 unsigned expected_mode)
 {
-   if (cached || check_index) {
+   if (cached || state->check_index) {
if (read_file_or_gitlink(ce, buf))
return error(_("read of %s failed"), name);
} else if (name) {
@@ -3277,7 +3277,8 @@ static int load_patch_target(struct strbuf *buf,
  * applying a non-git patch that incrementally updates the tree,
  * we read from the result of a previous diff.
  */
-static int load_preimage(struct image *image,
+static int load_preimage(struct apply_state *state,
+struct image *image,
 struct patch *patch, struct stat *st,
 const struct cache_entry *ce)
 {
@@ -3295,7 +3296,7 @@ static int load_preimage(struct image *image,
/* We have a patched copy in memory; use that. */
strbuf_add(, previous->result, previous->resultsize);
} else {
-   status = load_patch_target(, ce, st,
+   status = load_patch_target(state, , ce, st,
   patch->old_name, patch->old_mode);
if (status < 0)
return status;
@@ -3354,7 +3355,9 @@ static int three_way_merge(struct image *image,
  * the current contents of the new_name.  In no cases other than that
  * this function will be called.
  */
-static int load_current(struct image *image, struct patch *patch)
+static int load_current(struct apply_state *state,
+   struct image *image,
+   struct patch *patch)
 {
struct strbuf buf = STRBUF_INIT;
int status, pos;
@@ -3381,7 +3384,7 @@ static int load_current(struct image *image, struct patch 
*patch)
if (verify_index_match(ce, ))
return error(_("%s: does not match index"), name);
 
-   status = load_patch_target(, ce, , name, mode);
+   status = load_patch_target(state, , ce, , name, mode);
if (status < 0)
return status;
else if (status)
@@ -3431,11 +3434,11 @@ static int try_threeway(struct apply_state *state,
 
/* our_sha1[] is ours */
if (patch->is_new) {
-   if (load_current(_image, patch))
+   if (load_current(state, _image, patch))
return error("cannot read the current contents of '%s'",
 patch->new_name);
} else {
-   if (load_preimage(_image, patch, st, ce))
+   if (load_preimage(state, _image, patch, st, ce))
return error("cannot read the current contents of '%s'",
 patch->old_name);
}
@@ -3470,7 +3473,7 @@ static int apply_data(struct apply_state *state, struct 
patch *patch,
 {
struct image image;
 
-   if (load_preimage(, patch, st, ce) < 0)
+   if (load_preimage(state, , patch, st, ce) < 0)
return -1;
 
if (patch->direct_to_threeway ||
@

[PATCH v3 25/49] builtin/apply: move 'line_termination' global into 'struct apply_state'

2016-05-24 Thread Christian Couder
To libify the apply functionality the 'line_termination' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 14 +-
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 4ef83c1..95cd60a 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -45,6 +45,9 @@ struct apply_state {
int threeway;
int unidiff_zero;
int unsafe_paths;
+
+   /* Other non boolean parameters */
+   int line_termination;
 };
 
 /*
@@ -56,7 +59,6 @@ static int state_p_value = 1;
 static int p_value_known;
 static int apply = 1;
 static const char *fake_ancestor;
-static int line_termination = '\n';
 static unsigned int p_context = UINT_MAX;
 static const char * const apply_usage[] = {
N_("git apply [] [...]"),
@@ -3977,7 +3979,8 @@ static void stat_patch_list(struct patch *patch)
print_stat_summary(stdout, files, adds, dels);
 }
 
-static void numstat_patch_list(struct patch *patch)
+static void numstat_patch_list(struct apply_state *state,
+  struct patch *patch)
 {
for ( ; patch; patch = patch->next) {
const char *name;
@@ -3986,7 +3989,7 @@ static void numstat_patch_list(struct patch *patch)
printf("-\t-\t");
else
printf("%d\t%d\t", patch->lines_added, 
patch->lines_deleted);
-   write_name_quoted(name, stdout, line_termination);
+   write_name_quoted(name, stdout, state->line_termination);
}
 }
 
@@ -4490,7 +4493,7 @@ static int apply_patch(struct apply_state *state,
stat_patch_list(list);
 
if (state->numstat)
-   numstat_patch_list(list);
+   numstat_patch_list(state, list);
 
if (state->summary)
summary_patch_list(list);
@@ -4565,6 +4568,7 @@ static void init_apply_state(struct apply_state *state, 
const char *prefix)
memset(state, 0, sizeof(*state));
state->prefix = prefix;
state->prefix_length = state->prefix ? strlen(state->prefix) : 0;
+   state->line_termination = '\n';
 
git_apply_config();
if (apply_default_whitespace)
@@ -4625,7 +4629,7 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix)
OPT_FILENAME(0, "build-fake-ancestor", _ancestor,
N_("build a temporary index based on embedded index 
information")),
/* Think twice before adding "--nul" synonym to this */
-   OPT_SET_INT('z', NULL, _termination,
+   OPT_SET_INT('z', NULL, _termination,
N_("paths are separated with NUL character"), '\0'),
OPT_INTEGER('C', NULL, _context,
N_("ensure at least  lines of context 
match")),
-- 
2.8.3.443.gaeee61e

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


[PATCH v3 08/49] builtin/apply: introduce 'struct apply_state' to start libifying

2016-05-24 Thread Christian Couder
Currently commands that want to use the apply functionality have to launch
a "git apply" process which can be bad for performance.

Let's start libifying the apply functionality and to do that we first need
to get rid of the global variables in "builtin/apply.c".

This patch introduces "struct apply_state" into which all the previously
global variables will be moved. A new parameter called "state" that is a
pointer to the "apply_state" structure will come at the beginning of the
helper functions that need it and will be passed around the call chain.

To start let's move the "prefix" and "prefix_length" global variables into
"struct apply_state".

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 94 ++---
 1 file changed, 56 insertions(+), 38 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index c911e4e..ae068e7 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -21,6 +21,11 @@
 #include "ll-merge.h"
 #include "rerere.h"
 
+struct apply_state {
+   const char *prefix;
+   int prefix_length;
+};
+
 /*
  *  --check turns on checking that the working tree matches the
  *files that are being modified, but doesn't apply the patch
@@ -30,8 +35,6 @@
  *  --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 unidiff_zero;
@@ -748,7 +751,7 @@ static int count_slashes(const char *cp)
  * Given the string after "--- " or "+++ ", guess the appropriate
  * p_value for the given patch.
  */
-static int guess_p_value(const char *nameline)
+static int guess_p_value(struct apply_state *state, const char *nameline)
 {
char *name, *cp;
int val = -1;
@@ -761,17 +764,17 @@ static int guess_p_value(const char *nameline)
cp = strchr(name, '/');
if (!cp)
val = 0;
-   else if (prefix) {
+   else if (state->prefix) {
/*
 * Does it begin with "a/$our-prefix" and such?  Then this is
 * very likely to apply to our directory.
 */
-   if (!strncmp(name, prefix, prefix_length))
-   val = count_slashes(prefix);
+   if (!strncmp(name, state->prefix, state->prefix_length))
+   val = count_slashes(state->prefix);
else {
cp++;
-   if (!strncmp(cp, prefix, prefix_length))
-   val = count_slashes(prefix) + 1;
+   if (!strncmp(cp, state->prefix, state->prefix_length))
+   val = count_slashes(state->prefix) + 1;
}
}
free(name);
@@ -858,7 +861,10 @@ static int has_epoch_timestamp(const char *nameline)
  * files, we can happily check the index for a match, but for creating a
  * new file we should try to match whatever "patch" does. I have no idea.
  */
-static void parse_traditional_patch(const char *first, const char *second, 
struct patch *patch)
+static void parse_traditional_patch(struct apply_state *state,
+   const char *first,
+   const char *second,
+   struct patch *patch)
 {
char *name;
 
@@ -866,8 +872,8 @@ static void parse_traditional_patch(const char *first, 
const char *second, struc
second += 4;/* skip "+++ " */
if (!p_value_known) {
int p, q;
-   p = guess_p_value(first);
-   q = guess_p_value(second);
+   p = guess_p_value(state, first);
+   q = guess_p_value(state, second);
if (p < 0) p = q;
if (0 <= p && p == q) {
state_p_value = p;
@@ -1429,7 +1435,11 @@ static int parse_fragment_header(const char *line, int 
len, struct fragment *fra
return offset;
 }
 
-static int find_header(const char *line, unsigned long size, int *hdrsize, 
struct patch *patch)
+static int find_header(struct apply_state *state,
+  const char *line,
+  unsigned long size,
+  int *hdrsize,
+  struct patch *patch)
 {
unsigned long offset, len;
 
@@ -1506,7 +1516,7 @@ static int find_header(const char *line, unsigned long 
size, int *hdrsize, struc
continue;
 
/* Ok, we'll consider it a patch */
-   parse_traditional_patch(line, line+len, patch);
+   parse_traditional_patch(state, line, line+len, patch);

[PATCH v3 32/49] builtin/apply: move 'p_value' global into 'struct apply_state'

2016-05-24 Thread Christian Couder
To libify the apply functionality the 'p_value' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 151 +---
 1 file changed, 99 insertions(+), 52 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index ebbc711..843fafd 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -51,6 +51,7 @@ struct apply_state {
const char *fake_ancestor;
const char *patch_input_file;
int line_termination;
+   int p_value;
unsigned int p_context;
 
/* Exclude and include path parameters */
@@ -60,7 +61,6 @@ struct apply_state {
 
 static int newfd = -1;
 
-static int state_p_value = 1;
 static int p_value_known;
 
 static const char * const apply_usage[] = {
@@ -881,24 +881,24 @@ static void parse_traditional_patch(struct apply_state 
*state,
q = guess_p_value(state, second);
if (p < 0) p = q;
if (0 <= p && p == q) {
-   state_p_value = p;
+   state->p_value = p;
p_value_known = 1;
}
}
if (is_dev_null(first)) {
patch->is_new = 1;
patch->is_delete = 0;
-   name = find_name_traditional(second, NULL, state_p_value);
+   name = find_name_traditional(second, NULL, state->p_value);
patch->new_name = name;
} else if (is_dev_null(second)) {
patch->is_new = 0;
patch->is_delete = 1;
-   name = find_name_traditional(first, NULL, state_p_value);
+   name = find_name_traditional(first, NULL, state->p_value);
patch->old_name = name;
} else {
char *first_name;
-   first_name = find_name_traditional(first, NULL, state_p_value);
-   name = find_name_traditional(second, first_name, state_p_value);
+   first_name = find_name_traditional(first, NULL, state->p_value);
+   name = find_name_traditional(second, first_name, 
state->p_value);
free(first_name);
if (has_epoch_timestamp(first)) {
patch->is_new = 1;
@@ -917,7 +917,9 @@ static void parse_traditional_patch(struct apply_state 
*state,
die(_("unable to find filename in patch at line %d"), 
state_linenr);
 }
 
-static int gitdiff_hdrend(const char *line, struct patch *patch)
+static int gitdiff_hdrend(struct apply_state *state,
+ const char *line,
+ struct patch *patch)
 {
return -1;
 }
@@ -934,10 +936,14 @@ static int gitdiff_hdrend(const char *line, struct patch 
*patch)
 #define DIFF_OLD_NAME 0
 #define DIFF_NEW_NAME 1
 
-static void gitdiff_verify_name(const char *line, int isnull, char **name, int 
side)
+static void gitdiff_verify_name(struct apply_state *state,
+   const char *line,
+   int isnull,
+   char **name,
+   int side)
 {
if (!*name && !isnull) {
-   *name = find_name(line, NULL, state_p_value, TERM_TAB);
+   *name = find_name(line, NULL, state->p_value, TERM_TAB);
return;
}
 
@@ -947,7 +953,7 @@ static void gitdiff_verify_name(const char *line, int 
isnull, char **name, int s
if (isnull)
die(_("git apply: bad git-diff - expected /dev/null, 
got %s on line %d"),
*name, state_linenr);
-   another = find_name(line, NULL, state_p_value, TERM_TAB);
+   another = find_name(line, NULL, state->p_value, TERM_TAB);
if (!another || memcmp(another, *name, len + 1))
die((side == DIFF_NEW_NAME) ?
_("git apply: bad git-diff - inconsistent new 
filename on line %d") :
@@ -960,81 +966,105 @@ static void gitdiff_verify_name(const char *line, int 
isnull, char **name, int s
}
 }
 
-static int gitdiff_oldname(const char *line, struct patch *patch)
+static int gitdiff_oldname(struct apply_state *state,
+  const char *line,
+  struct patch *patch)
 {
-   gitdiff_verify_name(line, patch->is_new, >old_name,
+   gitdiff_verify_name(state, line,
+   patch->is_new, >old_name,
DIFF_OLD_NAME);
return 0;
 }
 
-static int gitdiff_newname(const char *line, struct patch *patch)
+static int gitdiff_newname(struct apply_state *state,
+  const char *

[PATCH v3 41/49] builtin/apply: move 'ws_ignore_action' into 'struct apply_state'

2016-05-24 Thread Christian Couder
To libify the apply functionality the 'ws_ignore_action' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 37 -
 1 file changed, 20 insertions(+), 17 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 61d809a..e5bc9cc 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -28,6 +28,12 @@ enum ws_error_action {
correct_ws_error
 };
 
+
+enum ws_ignore {
+   ignore_ws_none,
+   ignore_ws_change
+};
+
 struct apply_state {
const char *prefix;
int prefix_length;
@@ -69,6 +75,7 @@ struct apply_state {
 
/* These control whitespace errors */
enum ws_error_action ws_error_action;
+   enum ws_ignore ws_ignore_action;
const char *whitespace_option;
int whitespace_error;
int squelch_whitespace_errors;
@@ -82,13 +89,6 @@ static const char * const apply_usage[] = {
NULL
 };
 
-
-static enum ws_ignore {
-   ignore_ws_none,
-   ignore_ws_change
-} ws_ignore_action = ignore_ws_none;
-
-
 static void parse_whitespace_option(struct apply_state *state, const char 
*option)
 {
if (!option) {
@@ -119,16 +119,17 @@ static void parse_whitespace_option(struct apply_state 
*state, const char *optio
die(_("unrecognized whitespace option '%s'"), option);
 }
 
-static void parse_ignorewhitespace_option(const char *option)
+static void parse_ignorewhitespace_option(struct apply_state *state,
+ const char *option)
 {
if (!option || !strcmp(option, "no") ||
!strcmp(option, "false") || !strcmp(option, "never") ||
!strcmp(option, "none")) {
-   ws_ignore_action = ignore_ws_none;
+   state->ws_ignore_action = ignore_ws_none;
return;
}
if (!strcmp(option, "change")) {
-   ws_ignore_action = ignore_ws_change;
+   state->ws_ignore_action = ignore_ws_change;
return;
}
die(_("unrecognized whitespace ignore option '%s'"), option);
@@ -2488,7 +2489,7 @@ static int match_fragment(struct apply_state *state,
 * fuzzy matching. We collect all the line length information because
 * we need it to adjust whitespace if we match.
 */
-   if (ws_ignore_action == ignore_ws_change)
+   if (state->ws_ignore_action == ignore_ws_change)
return line_by_line_fuzzy_match(img, preimage, postimage,
try, try_lno, preimage_limit);
 
@@ -4611,12 +4612,13 @@ static int option_parse_p(const struct option *opt,
 }
 
 static int option_parse_space_change(const struct option *opt,
- const char *arg, int unset)
+const char *arg, int unset)
 {
+   struct apply_state *state = opt->value;
if (unset)
-   ws_ignore_action = ignore_ws_none;
+   state->ws_ignore_action = ignore_ws_none;
else
-   ws_ignore_action = ignore_ws_change;
+   state->ws_ignore_action = ignore_ws_change;
return 0;
 }
 
@@ -4650,13 +4652,14 @@ static void init_apply_state(struct apply_state *state, 
const char *prefix)
state->p_context = UINT_MAX;
state->squelch_whitespace_errors = 5;
state->ws_error_action = warn_on_ws_error;
+   state->ws_ignore_action = ignore_ws_none;
strbuf_init(>root, 0);
 
git_apply_config();
if (apply_default_whitespace)
parse_whitespace_option(state, apply_default_whitespace);
if (apply_default_ignorewhitespace)
-   parse_ignorewhitespace_option(apply_default_ignorewhitespace);
+   parse_ignorewhitespace_option(state, 
apply_default_ignorewhitespace);
 }
 
 static void clear_apply_state(struct apply_state *state)
@@ -4717,10 +4720,10 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix)
{ OPTION_CALLBACK, 0, "whitespace", , N_("action"),
N_("detect new or modified lines that have whitespace 
errors"),
0, option_parse_whitespace },
-   { OPTION_CALLBACK, 0, "ignore-space-change", NULL, NULL,
+   { OPTION_CALLBACK, 0, "ignore-space-change", , NULL,
N_("ignore changes in whitespace when finding context"),
PARSE_OPT_NOARG, option_parse_space_change },
-   { OPTION_CALLBACK, 0, "ignore-whitespace", NULL, NULL,
+   { OPTION_CALLBACK, 0, "ignore-whitespace", , NULL,
   

[PATCH v3 45/49] builtin/apply: move 'symlink_changes' global into 'struct apply_state'

2016-05-24 Thread Christian Couder
To libify the apply functionality the 'symlink_changes' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 50 --
 1 file changed, 28 insertions(+), 22 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 47622be..980bb34 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -34,6 +34,20 @@ enum ws_ignore {
ignore_ws_change
 };
 
+/*
+ * We need to keep track of how symlinks in the preimage are
+ * manipulated by the patches.  A patch to add a/b/c where a/b
+ * is a symlink should not be allowed to affect the directory
+ * the symlink points at, but if the same patch removes a/b,
+ * it is perfectly fine, as the patch removes a/b to make room
+ * to create a directory a/b so that a/b/c can be created.
+ *
+ * See also "struct string_list symlink_changes" in "struct
+ * apply_state".
+ */
+#define SYMLINK_GOES_AWAY 01
+#define SYMLINK_IN_RESULT 02
+
 struct apply_state {
const char *prefix;
int prefix_length;
@@ -75,6 +89,7 @@ struct apply_state {
 
/* Various "current state" */
int linenr; /* current line number */
+   struct string_list symlink_changes; /* we have to track symlinks */
 
/*
 * For "diff-stat" like behaviour, we keep track of the biggest change
@@ -3702,52 +3717,42 @@ static int check_to_create(struct apply_state *state,
return 0;
 }
 
-/*
- * We need to keep track of how symlinks in the preimage are
- * manipulated by the patches.  A patch to add a/b/c where a/b
- * is a symlink should not be allowed to affect the directory
- * the symlink points at, but if the same patch removes a/b,
- * it is perfectly fine, as the patch removes a/b to make room
- * to create a directory a/b so that a/b/c can be created.
- */
-static struct string_list symlink_changes;
-#define SYMLINK_GOES_AWAY 01
-#define SYMLINK_IN_RESULT 02
-
-static uintptr_t register_symlink_changes(const char *path, uintptr_t what)
+static uintptr_t register_symlink_changes(struct apply_state *state,
+ const char *path,
+ uintptr_t what)
 {
struct string_list_item *ent;
 
-   ent = string_list_lookup(_changes, path);
+   ent = string_list_lookup(>symlink_changes, path);
if (!ent) {
-   ent = string_list_insert(_changes, path);
+   ent = string_list_insert(>symlink_changes, path);
ent->util = (void *)0;
}
ent->util = (void *)(what | ((uintptr_t)ent->util));
return (uintptr_t)ent->util;
 }
 
-static uintptr_t check_symlink_changes(const char *path)
+static uintptr_t check_symlink_changes(struct apply_state *state, const char 
*path)
 {
struct string_list_item *ent;
 
-   ent = string_list_lookup(_changes, path);
+   ent = string_list_lookup(>symlink_changes, path);
if (!ent)
return 0;
return (uintptr_t)ent->util;
 }
 
-static void prepare_symlink_changes(struct patch *patch)
+static void prepare_symlink_changes(struct apply_state *state, struct patch 
*patch)
 {
for ( ; patch; patch = patch->next) {
if ((patch->old_name && S_ISLNK(patch->old_mode)) &&
(patch->is_rename || patch->is_delete))
/* the symlink at patch->old_name is removed */
-   register_symlink_changes(patch->old_name, 
SYMLINK_GOES_AWAY);
+   register_symlink_changes(state, patch->old_name, 
SYMLINK_GOES_AWAY);
 
if (patch->new_name && S_ISLNK(patch->new_mode))
/* the symlink at patch->new_name is created or remains 
*/
-   register_symlink_changes(patch->new_name, 
SYMLINK_IN_RESULT);
+   register_symlink_changes(state, patch->new_name, 
SYMLINK_IN_RESULT);
}
 }
 
@@ -3761,7 +3766,7 @@ static int path_is_beyond_symlink_1(struct apply_state 
*state, struct strbuf *na
if (!name->len)
break;
name->buf[name->len] = '\0';
-   change = check_symlink_changes(name->buf);
+   change = check_symlink_changes(state, name->buf);
if (change & SYMLINK_IN_RESULT)
return 1;
if (change & SYMLINK_GOES_AWAY)
@@ -3930,7 +3935,7 @@ static int check_patch_list(struct apply_state *state, 
struct patch *patch)
 {
int err = 0;
 
-   prepare_symlink_changes(patch);
+   prepare_symlink_changes(state, patch);
prepare_fn_table(state, patch);
while (patch) {
   

[PATCH v3 43/49] builtin/apply: move 'state_linenr' global into 'struct apply_state'

2016-05-24 Thread Christian Couder
To libify the apply functionality the 'state_linenr' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 71 +
 1 file changed, 36 insertions(+), 35 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 9e7d181..dd56a8e 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -73,6 +73,9 @@ struct apply_state {
struct string_list limit_by_name;
int has_include;
 
+   /* Various "current state" */
+   int linenr; /* current line number */
+
/*
 * For "diff-stat" like behaviour, we keep track of the biggest change
 * we've seen, and the longest filename. That allows us to do simple
@@ -149,13 +152,6 @@ static void set_default_whitespace_mode(struct apply_state 
*state)
state->ws_error_action = (state->apply ? warn_on_ws_error : 
nowarn_ws_error);
 }
 
-/*
- * Various "current state", notably line numbers and what
- * file (and how) we're patching right now.. The "is_"
- * things are flags, where -1 means "don't know yet".
- */
-static int state_linenr = 1;
-
 /*
  * This represents one "hunk" from a patch, starting with
  * "@@ -oldpos,oldlines +newpos,newlines @@" marker.  The
@@ -932,7 +928,7 @@ static void parse_traditional_patch(struct apply_state 
*state,
}
}
if (!name)
-   die(_("unable to find filename in patch at line %d"), 
state_linenr);
+   die(_("unable to find filename in patch at line %d"), 
state->linenr);
 }
 
 static int gitdiff_hdrend(struct apply_state *state,
@@ -970,17 +966,17 @@ static void gitdiff_verify_name(struct apply_state *state,
char *another;
if (isnull)
die(_("git apply: bad git-diff - expected /dev/null, 
got %s on line %d"),
-   *name, state_linenr);
+   *name, state->linenr);
another = find_name(state, line, NULL, state->p_value, 
TERM_TAB);
if (!another || memcmp(another, *name, len + 1))
die((side == DIFF_NEW_NAME) ?
_("git apply: bad git-diff - inconsistent new 
filename on line %d") :
-   _("git apply: bad git-diff - inconsistent old 
filename on line %d"), state_linenr);
+   _("git apply: bad git-diff - inconsistent old 
filename on line %d"), state->linenr);
free(another);
} else {
/* expect "/dev/null" */
if (memcmp("/dev/null", line, 9) || line[9] != '\n')
-   die(_("git apply: bad git-diff - expected /dev/null on 
line %d"), state_linenr);
+   die(_("git apply: bad git-diff - expected /dev/null on 
line %d"), state->linenr);
}
 }
 
@@ -1343,8 +1339,8 @@ static int parse_git_header(struct apply_state *state,
 
line += len;
size -= len;
-   state_linenr++;
-   for (offset = len ; size > 0 ; offset += len, size -= len, line += len, 
state_linenr++) {
+   state->linenr++;
+   for (offset = len ; size > 0 ; offset += len, size -= len, line += len, 
state->linenr++) {
static const struct opentry {
const char *str;
int (*fn)(struct apply_state *, const char *, struct 
patch *);
@@ -1515,7 +1511,7 @@ static int find_header(struct apply_state *state,
patch->is_new = patch->is_delete = -1;
patch->old_mode = patch->new_mode = 0;
patch->old_name = patch->new_name = NULL;
-   for (offset = 0; size > 0; offset += len, size -= len, line += len, 
state_linenr++) {
+   for (offset = 0; size > 0; offset += len, size -= len, line += len, 
state->linenr++) {
unsigned long nextlen;
 
len = linelen(line, size);
@@ -1536,7 +1532,7 @@ static int find_header(struct apply_state *state,
if (parse_fragment_header(line, len, ) < 0)
continue;
die(_("patch fragment without header at line %d: %.*s"),
-   state_linenr, (int)len-1, line);
+   state->linenr, (int)len-1, line);
}
 
if (size < len + 6)
@@ -1557,13 +1553,13 @@ static int find_header(struct apply_state *state,
   "git diff header lacks filename 
information when removing "
 

[PATCH v3 42/49] builtin/apply: move 'max_change' and 'max_len' into 'struct apply_state'

2016-05-24 Thread Christian Couder
To libify the apply functionality the 'max_change' and 'max_len'
variables should not be static and global to the file. Let's move
them into 'struct apply_state'.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 49 +
 1 file changed, 25 insertions(+), 24 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index e5bc9cc..9e7d181 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -73,6 +73,14 @@ struct apply_state {
struct string_list limit_by_name;
int has_include;
 
+   /*
+* For "diff-stat" like behaviour, we keep track of the biggest change
+* we've seen, and the longest filename. That allows us to do simple
+* scaling.
+*/
+   int max_change;
+   int max_len;
+
/* These control whitespace errors */
enum ws_error_action ws_error_action;
enum ws_ignore ws_ignore_action;
@@ -141,13 +149,6 @@ static void set_default_whitespace_mode(struct apply_state 
*state)
state->ws_error_action = (state->apply ? warn_on_ws_error : 
nowarn_ws_error);
 }
 
-/*
- * For "diff-stat" like behaviour, we keep track of the biggest change
- * we've seen, and the longest filename. That allows us to do simple
- * scaling.
- */
-static int max_change, max_len;
-
 /*
  * Various "current state", notably line numbers and what
  * file (and how) we're patching right now.. The "is_"
@@ -2172,7 +2173,7 @@ static const char pluses[] =
 static const char minuses[]=
 "--";
 
-static void show_stats(struct patch *patch)
+static void show_stats(struct apply_state *state, struct patch *patch)
 {
struct strbuf qname = STRBUF_INIT;
char *cp = patch->new_name ? patch->new_name : patch->old_name;
@@ -2183,7 +2184,7 @@ static void show_stats(struct patch *patch)
/*
 * "scale" the filename
 */
-   max = max_len;
+   max = state->max_len;
if (max > 50)
max = 50;
 
@@ -2206,13 +2207,13 @@ static void show_stats(struct patch *patch)
/*
 * scale the add/delete
 */
-   max = max + max_change > 70 ? 70 - max : max_change;
+   max = max + state->max_change > 70 ? 70 - max : state->max_change;
add = patch->lines_added;
del = patch->lines_deleted;
 
-   if (max_change > 0) {
-   int total = ((add + del) * max + max_change / 2) / max_change;
-   add = (add * max + max_change / 2) / max_change;
+   if (state->max_change > 0) {
+   int total = ((add + del) * max + state->max_change / 2) / 
state->max_change;
+   add = (add * max + state->max_change / 2) / state->max_change;
del = total - add;
}
printf("%5d %.*s%.*s\n", patch->lines_added + patch->lines_deleted,
@@ -4038,7 +4039,7 @@ static void build_fake_ancestor(struct patch *list, const 
char *filename)
discard_index();
 }
 
-static void stat_patch_list(struct patch *patch)
+static void stat_patch_list(struct apply_state *state, struct patch *patch)
 {
int files, adds, dels;
 
@@ -4046,7 +4047,7 @@ static void stat_patch_list(struct patch *patch)
files++;
adds += patch->lines_added;
dels += patch->lines_deleted;
-   show_stats(patch);
+   show_stats(state, patch);
}
 
print_stat_summary(stdout, files, adds, dels);
@@ -4144,25 +4145,25 @@ static void summary_patch_list(struct patch *patch)
}
 }
 
-static void patch_stats(struct patch *patch)
+static void patch_stats(struct apply_state *state, struct patch *patch)
 {
int lines = patch->lines_added + patch->lines_deleted;
 
-   if (lines > max_change)
-   max_change = lines;
+   if (lines > state->max_change)
+   state->max_change = lines;
if (patch->old_name) {
int len = quote_c_style(patch->old_name, NULL, NULL, 0);
if (!len)
len = strlen(patch->old_name);
-   if (len > max_len)
-   max_len = len;
+   if (len > state->max_len)
+   state->max_len = len;
}
if (patch->new_name) {
int len = quote_c_style(patch->new_name, NULL, NULL, 0);
if (!len)
len = strlen(patch->new_name);
-   if (len > max_len)
-   max_len = len;
+   if (len > state->max_len)
+   state->max_len = len;
}
 }
 
@@ -4519,7 +4520,7 @@ static int apply_

[PATCH v3 38/49] builtin/apply: move 'squelch_whitespace_errors' into 'struct apply_state'

2016-05-24 Thread Christian Couder
To libify the apply functionality the 'squelch_whitespace_errors' variable 
should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 22 +++---
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 523ed74..619b8fb 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -63,6 +63,7 @@ struct apply_state {
/* These control whitespace errors */
const char *whitespace_option;
int whitespace_error;
+   int squelch_whitespace_errors;
 };
 
 static int newfd = -1;
@@ -78,7 +79,6 @@ static enum ws_error_action {
die_on_ws_error,
correct_ws_error
 } ws_error_action = warn_on_ws_error;
-static int squelch_whitespace_errors = 5;
 static int applied_after_fixing_ws;
 
 static enum ws_ignore {
@@ -87,7 +87,7 @@ static enum ws_ignore {
 } ws_ignore_action = ignore_ws_none;
 
 
-static void parse_whitespace_option(const char *option)
+static void parse_whitespace_option(struct apply_state *state, const char 
*option)
 {
if (!option) {
ws_error_action = warn_on_ws_error;
@@ -107,7 +107,7 @@ static void parse_whitespace_option(const char *option)
}
if (!strcmp(option, "error-all")) {
ws_error_action = die_on_ws_error;
-   squelch_whitespace_errors = 0;
+   state->squelch_whitespace_errors = 0;
return;
}
if (!strcmp(option, "strip") || !strcmp(option, "fix")) {
@@ -1599,8 +1599,8 @@ static void record_ws_error(struct apply_state *state,
return;
 
state->whitespace_error++;
-   if (squelch_whitespace_errors &&
-   squelch_whitespace_errors < state->whitespace_error)
+   if (state->squelch_whitespace_errors &&
+   state->squelch_whitespace_errors < state->whitespace_error)
return;
 
err = whitespace_error_string(result);
@@ -4620,9 +4620,8 @@ static int option_parse_whitespace(const struct option 
*opt,
   const char *arg, int unset)
 {
struct apply_state *state = opt->value;
-
state->whitespace_option = arg;
-   parse_whitespace_option(arg);
+   parse_whitespace_option(state, arg);
return 0;
 }
 
@@ -4645,11 +4644,12 @@ static void init_apply_state(struct apply_state *state, 
const char *prefix)
state->line_termination = '\n';
state->p_value = 1;
state->p_context = UINT_MAX;
+   state->squelch_whitespace_errors = 5;
strbuf_init(>root, 0);
 
git_apply_config();
if (apply_default_whitespace)
-   parse_whitespace_option(apply_default_whitespace);
+   parse_whitespace_option(state, apply_default_whitespace);
if (apply_default_ignorewhitespace)
parse_ignorewhitespace_option(apply_default_ignorewhitespace);
 }
@@ -4792,10 +4792,10 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix)
if (read_stdin)
errs |= apply_patch(, 0, "", options);
if (state.whitespace_error) {
-   if (squelch_whitespace_errors &&
-   squelch_whitespace_errors < state.whitespace_error) {
+   if (state.squelch_whitespace_errors &&
+   state.squelch_whitespace_errors < state.whitespace_error) {
int squelched =
-   state.whitespace_error - 
squelch_whitespace_errors;
+   state.whitespace_error - 
state.squelch_whitespace_errors;
warning(Q_("squelched %d whitespace error",
   "squelched %d whitespace errors",
   squelched),
-- 
2.8.3.443.gaeee61e

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


[PATCH v3 44/49] builtin/apply: move 'fn_table' global into 'struct apply_state'

2016-05-24 Thread Christian Couder
To libify the apply functionality the 'fn_table' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

As fn_table is cleared at the end of apply_patch(), it is not
necessary to clear it in clear_apply_state().

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 47 +--
 1 file changed, 25 insertions(+), 22 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index dd56a8e..47622be 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -84,6 +84,12 @@ struct apply_state {
int max_change;
int max_len;
 
+   /*
+* Records filenames that have been touched, in order to handle
+* the case where more than one patches touch the same file.
+*/
+   struct string_list fn_table;
+
/* These control whitespace errors */
enum ws_error_action ws_error_action;
enum ws_ignore ws_ignore_action;
@@ -271,13 +277,6 @@ struct image {
struct line *line;
 };
 
-/*
- * Records filenames that have been touched, in order to handle
- * the case where more than one patches touch the same file.
- */
-
-static struct string_list fn_table;
-
 static uint32_t hash_line(const char *cp, size_t len)
 {
size_t i;
@@ -3207,14 +3206,14 @@ static int read_file_or_gitlink(const struct 
cache_entry *ce, struct strbuf *buf
return read_blob_object(buf, ce->sha1, ce->ce_mode);
 }
 
-static struct patch *in_fn_table(const char *name)
+static struct patch *in_fn_table(struct apply_state *state, const char *name)
 {
struct string_list_item *item;
 
if (name == NULL)
return NULL;
 
-   item = string_list_lookup(_table, name);
+   item = string_list_lookup(>fn_table, name);
if (item != NULL)
return (struct patch *)item->util;
 
@@ -3246,7 +3245,7 @@ static int was_deleted(struct patch *patch)
return patch == PATH_WAS_DELETED;
 }
 
-static void add_to_fn_table(struct patch *patch)
+static void add_to_fn_table(struct apply_state *state, struct patch *patch)
 {
struct string_list_item *item;
 
@@ -3256,7 +3255,7 @@ static void add_to_fn_table(struct patch *patch)
 * file creations and copies
 */
if (patch->new_name != NULL) {
-   item = string_list_insert(_table, patch->new_name);
+   item = string_list_insert(>fn_table, patch->new_name);
item->util = patch;
}
 
@@ -3265,12 +3264,12 @@ static void add_to_fn_table(struct patch *patch)
 * later chunks shouldn't patch old names
 */
if ((patch->new_name == NULL) || (patch->is_rename)) {
-   item = string_list_insert(_table, patch->old_name);
+   item = string_list_insert(>fn_table, patch->old_name);
item->util = PATH_WAS_DELETED;
}
 }
 
-static void prepare_fn_table(struct patch *patch)
+static void prepare_fn_table(struct apply_state *state, struct patch *patch)
 {
/*
 * store information about incoming file deletion
@@ -3278,7 +3277,7 @@ static void prepare_fn_table(struct patch *patch)
while (patch) {
if ((patch->new_name == NULL) || (patch->is_rename)) {
struct string_list_item *item;
-   item = string_list_insert(_table, patch->old_name);
+   item = string_list_insert(>fn_table, 
patch->old_name);
item->util = PATH_TO_BE_DELETED;
}
patch = patch->next;
@@ -3299,7 +3298,9 @@ static int checkout_target(struct index_state *istate,
return 0;
 }
 
-static struct patch *previous_patch(struct patch *patch, int *gone)
+static struct patch *previous_patch(struct apply_state *state,
+   struct patch *patch,
+   int *gone)
 {
struct patch *previous;
 
@@ -3307,7 +3308,7 @@ static struct patch *previous_patch(struct patch *patch, 
int *gone)
if (patch->is_copy || patch->is_rename)
return NULL; /* "git" patches do not depend on the order */
 
-   previous = in_fn_table(patch->old_name);
+   previous = in_fn_table(state, patch->old_name);
if (!previous)
return NULL;
 
@@ -3376,7 +3377,7 @@ static int load_preimage(struct apply_state *state,
struct patch *previous;
int status;
 
-   previous = previous_patch(patch, );
+   previous = previous_patch(state, patch, );
if (status)
return error(_("path %s has been renamed/deleted"),
 patch->old_name);
@@ -3572,7 +3573,7 @@ static int apply_data(struct apply_state *state, struct 
patch *patch,
}
patch-&g

[PATCH v3 15/49] builtin/apply: move 'apply_verbosely' global into 'struct apply_state'

2016-05-24 Thread Christian Couder
To libify the apply functionality the 'apply_verbosely' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 26 +-
 1 file changed, 13 insertions(+), 13 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 8692e2f..07dc89d 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -32,6 +32,7 @@ struct apply_state {
/* These boolean parameters control how the apply is done */
int apply_in_reverse;
int apply_with_reject;
+   int apply_verbosely;
int unidiff_zero;
 };
 
@@ -51,7 +52,6 @@ static int diffstat;
 static int numstat;
 static int summary;
 static int apply = 1;
-static int apply_verbosely;
 static int allow_overlap;
 static int no_add;
 static int threeway;
@@ -2805,7 +2805,7 @@ static int apply_one_fragment(struct apply_state *state,
/* Ignore it, we already handled it */
break;
default:
-   if (apply_verbosely)
+   if (state->apply_verbosely)
error(_("invalid start of line: '%c'"), first);
applied_pos = -1;
goto out;
@@ -2920,7 +2920,7 @@ static int apply_one_fragment(struct apply_state *state,
apply = 0;
}
 
-   if (apply_verbosely && applied_pos != pos) {
+   if (state->apply_verbosely && applied_pos != pos) {
int offset = applied_pos - pos;
if (state->apply_in_reverse)
offset = 0 - offset;
@@ -2942,7 +2942,7 @@ static int apply_one_fragment(struct apply_state *state,
   leading, trailing, applied_pos+1);
update_image(img, applied_pos, , );
} else {
-   if (apply_verbosely)
+   if (state->apply_verbosely)
error(_("while searching for:\n%.*s"),
  (int)(old - oldlines), oldlines);
}
@@ -3856,7 +3856,7 @@ static int check_patch_list(struct apply_state *state, 
struct patch *patch)
prepare_symlink_changes(patch);
prepare_fn_table(patch);
while (patch) {
-   if (apply_verbosely)
+   if (state->apply_verbosely)
say_patch_name(stderr,
   _("Checking patch %s..."), patch);
err |= check_patch(state, patch);
@@ -4287,7 +4287,7 @@ static void write_out_one_result(struct patch *patch, int 
phase)
create_file(patch);
 }
 
-static int write_out_one_reject(struct patch *patch)
+static int write_out_one_reject(struct apply_state *state, struct patch *patch)
 {
FILE *rej;
char namebuf[PATH_MAX];
@@ -4302,7 +4302,7 @@ static int write_out_one_reject(struct patch *patch)
}
 
if (!cnt) {
-   if (apply_verbosely)
+   if (state->apply_verbosely)
say_patch_name(stderr,
   _("Applied patch %s cleanly."), patch);
return 0;
@@ -4358,7 +4358,7 @@ static int write_out_one_reject(struct patch *patch)
return -1;
 }
 
-static int write_out_results(struct patch *list)
+static int write_out_results(struct apply_state *state, struct patch *list)
 {
int phase;
int errs = 0;
@@ -4373,7 +4373,7 @@ static int write_out_results(struct patch *list)
else {
write_out_one_result(l, phase);
if (phase == 1) {
-   if (write_out_one_reject(l))
+   if (write_out_one_reject(state, l))
errs = 1;
if (l->conflicted_threeway) {
string_list_append(, 
l->new_name);
@@ -4437,7 +4437,7 @@ static int apply_patch(struct apply_state *state,
listp = >next;
}
else {
-   if (apply_verbosely)
+   if (state->apply_verbosely)
say_patch_name(stderr, _("Skipped patch 
'%s'."), patch);
free_patch(patch);
skipped_patch++;
@@ -4465,7 +4465,7 @@ static int apply_patch(struct apply_state *state,
!state->apply_with_reject)
exit(1);
 
-   if (apply && write_out_results(list)) {
+   if (apply && write_out_results(state, list)) {
if (

[PATCH v3 07/49] builtin/apply: move 'read_stdin' global into cmd_apply()

2016-05-24 Thread Christian Couder
The 'read_stdin' variable doesn't need to be static and global to the
file. It can be local to cmd_apply(), so let's move it there.

This will make it easier to libify the apply functionality.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 5a1d65a..c911e4e 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -78,7 +78,6 @@ static enum ws_ignore {
 
 static const char *patch_input_file;
 static struct strbuf root = STRBUF_INIT;
-static int read_stdin = 1;
 
 static void parse_whitespace_option(const char *option)
 {
@@ -4517,6 +4516,7 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix_)
int is_not_gitdir = !startup_info->have_repository;
int force_apply = 0;
int options = 0;
+   int read_stdin = 1;
 
const char *whitespace_option = NULL;
 
-- 
2.8.3.443.gaeee61e

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


[PATCH v3 11/49] builtin/apply: move 'check' global into 'struct apply_state'

2016-05-24 Thread Christian Couder
To libify the apply functionality the 'check' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 14 +++---
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 6c36898..55a5541 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -25,13 +25,14 @@ struct apply_state {
const char *prefix;
int prefix_length;
 
+   /* These control what gets looked at and modified */
+   int check; /* preimage must match working tree, don't actually apply */
+
/* These boolean parameters control how the apply is done */
int unidiff_zero;
 };
 
 /*
- *  --check turns on checking that the working tree matches the
- *files that are being modified, but doesn't apply the patch
  *  --stat does just a diffstat, and doesn't actually apply
  *  --numstat does numeric diffstat, and doesn't actually apply
  *  --index-info shows the old and new index info for paths if available.
@@ -48,7 +49,6 @@ static int cached;
 static int diffstat;
 static int numstat;
 static int summary;
-static int check;
 static int apply = 1;
 static int apply_in_reverse;
 static int apply_with_reject;
@@ -2053,7 +2053,7 @@ static int parse_chunk(struct apply_state *state, char 
*buffer, unsigned long si
 * without metadata change.  A binary patch appears
 * empty to us here.
 */
-   if ((apply || check) &&
+   if ((apply || state->check) &&
(!patch->is_binary && !metadata_changes(patch)))
die(_("patch with only garbage at line %d"), 
state_linenr);
}
@@ -4440,7 +4440,7 @@ static int apply_patch(struct apply_state *state,
die(_("unable to read index file"));
}
 
-   if ((check || apply) &&
+   if ((state->check || apply) &&
check_patch_list(state, list) < 0 &&
!apply_with_reject)
exit(1);
@@ -4579,7 +4579,7 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix)
N_("show number of added and deleted lines in decimal 
notation")),
OPT_BOOL(0, "summary", ,
N_("instead of applying the patch, output a summary for 
the input")),
-   OPT_BOOL(0, "check", ,
+   OPT_BOOL(0, "check", ,
N_("instead of applying the patch, see if the patch is 
applicable")),
OPT_BOOL(0, "index", _index,
N_("make sure the patch is applicable to the current 
index")),
@@ -4644,7 +4644,7 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix)
}
if (apply_with_reject)
apply = apply_verbosely = 1;
-   if (!force_apply && (diffstat || numstat || summary || check || 
fake_ancestor))
+   if (!force_apply && (diffstat || numstat || summary || state.check || 
fake_ancestor))
apply = 0;
if (check_index && is_not_gitdir)
die(_("--index outside a repository"));
-- 
2.8.3.443.gaeee61e

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


Re: [PATCH] t0008: 4 tests fail with ksh88

2016-05-20 Thread Christian Couder
On Fri, May 20, 2016 at 6:10 PM, Junio C Hamano  wrote:
> Junio C Hamano  writes:
>
> From: Armin Kunaschik 
> Date: Fri, 20 May 2016 16:31:30 +0200
> Subject: [PATCH] t0008: 4 tests fail with ksh88
>
> In t0008, we have
>
> cat <<-EOF
> ...
> a/b/.gitignore:8:!on*   "a/b/one\"three"
> ...
> EOF
>
> ane expect that the backslash-dq is passed through literally.

s/ane/and/

> ksh88 eats \ and generates a wrong expect data to compare with.
>
> Using \\" works this around without breaking other POSIX shells
> (which collapse backslash-backslash to a single backslash), and
> ksh88 does so, too.
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


Re: [PATCH v2 4/4] bundle v3: the beginning

2016-05-20 Thread Christian Couder
I am responding to this 2+ month old email because I am investigating
adding an alternate object store at the same level as loose and packed
objects. This alternate object store could be used for large files. I
am working on this for GitLab. (Yeah, I am working, as a freelance,
for both Booking.com and GitLab these days.)

On Wed, Mar 2, 2016 at 9:32 PM, Junio C Hamano  wrote:
> The bundle v3 format introduces an ability to have the bundle header
> (which describes what references in the bundled history can be
> fetched, and what objects the receiving repository must have in
> order to unbundle it successfully) in one file, and the bundled pack
> stream data in a separate file.
>
> A v3 bundle file begins with a line with "# v3 git bundle", followed
> by zero or more "extended header" lines, and an empty line, finally
> followed by the list of prerequisites and references in the same
> format as v2 bundle.  If it uses the "split bundle" feature, there
> is a "data: $URL" extended header line, and nothing follows the list
> of prerequisites and references.  Also, "sha1: " extended header
> line may exist to help validating that the pack stream data matches
> the bundle header.
>
> A typical expected use of a split bundle is to help initial clone
> that involves a huge data transfer, and would go like this:
>
>  - Any repository people would clone and fetch from would regularly
>be repacked, and it is expected that there would be a packfile
>without prerequisites that holds all (or at least most) of the
>history of it (call it pack-$name.pack).
>
>  - After arranging that packfile to be downloadable over popular
>transfer methods used for serving static files (such as HTTP or
>HTTPS) that are easily resumable as $URL/pack-$name.pack, a v3
>bundle file (call it $name.bndl) can be prepared with an extended
>header "data: $URL/pack-$name.pack" to point at the download
>location for the packfile, and be served at "$URL/$name.bndl".
>
>  - An updated Git client, when trying to "git clone" from such a
>repository, may be redirected to $URL/$name.bndl", which would be
>a tiny text file (when split bundle feature is used).
>
>  - The client would then inspect the downloaded $name.bndl, learn
>that the corresponding packfile exists at $URL/pack-$name.pack,
>and downloads it as pack-$name.pack, until the download succeeds.
>This can easily be done with "wget --continue" equivalent over an
>unreliable link.  The checksum recorded on the "sha1: " header
>line is expected to be used by this downloader (not written yet).

I wonder if this mechanism could also be used or extended to clone and
fetch an alternate object database.

In [1], [2] and [3], and this was also discussed during the
Contributor Summit last month, Peff says that he started working on
alternate object database support a long time ago, and that the hard
part is a protocol extension to tell remotes that you can access some
objects in a different way.

If a Git client would download a "$name.bndl" v3 bundle file that
would have a "data: $URL/alt-odb-$name.odb" extended header, the Git
client would just need to download "$URL/alt-odb-$name.odb" and use
the alternate object database support on this file.

This way it would know all it has to know to access the objects in the
alternate database. The alternate object database may not contain the
real objects, if they are too big for example, but just files that
describe how to get the real objects.

>  - After fully downloading $name.bndl and pack-$name.pack and
>storing them next to each other, the client would clone from the
>$name.bndl; this would populate the newly created repository with
>reasonably recent history.
>
>  - Then the client can issue "git fetch" against the original
>repository to obtain the most recent part of the history created
>since the bundle was made.

[1] http://thread.gmane.org/gmane.comp.version-control.git/206886/focus=207040
[2] http://thread.gmane.org/gmane.comp.version-control.git/247171
[3] http://thread.gmane.org/gmane.comp.version-control.git/202902/focus=203020
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[PATCH v2 08/94] builtin/apply: introduce 'struct apply_state' to start libifying

2016-05-11 Thread Christian Couder
Currently commands that want to use the apply functionality have to launch
a "git apply" process which can be bad for performance.

Let's start libifying the apply functionality and to do that we first need
to get rid of the global variables in "builtin/apply.c".

This patch introduces "struct apply_state" into which all the previously
global variables will be moved. A new parameter called "state" that is a
pointer to the "apply_state" structure will come at the beginning of the
helper functions that need it and will be passed around the call chain.

To start let's move the "prefix" and "prefix_length" global variables into
"struct apply_state".

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 94 ++---
 1 file changed, 56 insertions(+), 38 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index c911e4e..ae068e7 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -21,6 +21,11 @@
 #include "ll-merge.h"
 #include "rerere.h"
 
+struct apply_state {
+   const char *prefix;
+   int prefix_length;
+};
+
 /*
  *  --check turns on checking that the working tree matches the
  *files that are being modified, but doesn't apply the patch
@@ -30,8 +35,6 @@
  *  --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 unidiff_zero;
@@ -748,7 +751,7 @@ static int count_slashes(const char *cp)
  * Given the string after "--- " or "+++ ", guess the appropriate
  * p_value for the given patch.
  */
-static int guess_p_value(const char *nameline)
+static int guess_p_value(struct apply_state *state, const char *nameline)
 {
char *name, *cp;
int val = -1;
@@ -761,17 +764,17 @@ static int guess_p_value(const char *nameline)
cp = strchr(name, '/');
if (!cp)
val = 0;
-   else if (prefix) {
+   else if (state->prefix) {
/*
 * Does it begin with "a/$our-prefix" and such?  Then this is
 * very likely to apply to our directory.
 */
-   if (!strncmp(name, prefix, prefix_length))
-   val = count_slashes(prefix);
+   if (!strncmp(name, state->prefix, state->prefix_length))
+   val = count_slashes(state->prefix);
else {
cp++;
-   if (!strncmp(cp, prefix, prefix_length))
-   val = count_slashes(prefix) + 1;
+   if (!strncmp(cp, state->prefix, state->prefix_length))
+   val = count_slashes(state->prefix) + 1;
}
}
free(name);
@@ -858,7 +861,10 @@ static int has_epoch_timestamp(const char *nameline)
  * files, we can happily check the index for a match, but for creating a
  * new file we should try to match whatever "patch" does. I have no idea.
  */
-static void parse_traditional_patch(const char *first, const char *second, 
struct patch *patch)
+static void parse_traditional_patch(struct apply_state *state,
+   const char *first,
+   const char *second,
+   struct patch *patch)
 {
char *name;
 
@@ -866,8 +872,8 @@ static void parse_traditional_patch(const char *first, 
const char *second, struc
second += 4;/* skip "+++ " */
if (!p_value_known) {
int p, q;
-   p = guess_p_value(first);
-   q = guess_p_value(second);
+   p = guess_p_value(state, first);
+   q = guess_p_value(state, second);
if (p < 0) p = q;
if (0 <= p && p == q) {
state_p_value = p;
@@ -1429,7 +1435,11 @@ static int parse_fragment_header(const char *line, int 
len, struct fragment *fra
return offset;
 }
 
-static int find_header(const char *line, unsigned long size, int *hdrsize, 
struct patch *patch)
+static int find_header(struct apply_state *state,
+  const char *line,
+  unsigned long size,
+  int *hdrsize,
+  struct patch *patch)
 {
unsigned long offset, len;
 
@@ -1506,7 +1516,7 @@ static int find_header(const char *line, unsigned long 
size, int *hdrsize, struc
continue;
 
/* Ok, we'll consider it a patch */
-   parse_traditional_patch(line, line+len, patch);
+   parse_traditional_patch(state, line, line+len, patch);

[PATCH v2 07/94] builtin/apply: move 'read_stdin' global into cmd_apply()

2016-05-11 Thread Christian Couder
The 'read_stdin' variable doesn't need to be static and global to the
file. It can be local to cmd_apply(), so let's move it there.

This will make it easier to libify the apply functionality.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 5a1d65a..c911e4e 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -78,7 +78,6 @@ static enum ws_ignore {
 
 static const char *patch_input_file;
 static struct strbuf root = STRBUF_INIT;
-static int read_stdin = 1;
 
 static void parse_whitespace_option(const char *option)
 {
@@ -4517,6 +4516,7 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix_)
int is_not_gitdir = !startup_info->have_repository;
int force_apply = 0;
int options = 0;
+   int read_stdin = 1;
 
const char *whitespace_option = NULL;
 
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 03/94] builtin/apply: avoid parameter shadowing 'linenr' global

2016-05-11 Thread Christian Couder
Let's just rename the global 'state_linenr' as it will become
'state->linenr' in a following patch.

This also avoid errors when compiling with -Wshadow and makes
it safer to later move global variables into a "state" struct.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 48 
 1 file changed, 24 insertions(+), 24 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index e133b38..705a9c8 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -144,7 +144,7 @@ static int max_change, max_len;
  * file (and how) we're patching right now.. The "is_"
  * things are flags, where -1 means "don't know yet".
  */
-static int linenr = 1;
+static int state_linenr = 1;
 
 /*
  * This represents one "hunk" from a patch, starting with
@@ -905,7 +905,7 @@ static void parse_traditional_patch(const char *first, 
const char *second, struc
}
}
if (!name)
-   die(_("unable to find filename in patch at line %d"), linenr);
+   die(_("unable to find filename in patch at line %d"), 
state_linenr);
 }
 
 static int gitdiff_hdrend(const char *line, struct patch *patch)
@@ -937,17 +937,17 @@ static void gitdiff_verify_name(const char *line, int 
isnull, char **name, int s
char *another;
if (isnull)
die(_("git apply: bad git-diff - expected /dev/null, 
got %s on line %d"),
-   *name, linenr);
+   *name, state_linenr);
another = find_name(line, NULL, state_p_value, TERM_TAB);
if (!another || memcmp(another, *name, len + 1))
die((side == DIFF_NEW_NAME) ?
_("git apply: bad git-diff - inconsistent new 
filename on line %d") :
-   _("git apply: bad git-diff - inconsistent old 
filename on line %d"), linenr);
+   _("git apply: bad git-diff - inconsistent old 
filename on line %d"), state_linenr);
free(another);
} else {
/* expect "/dev/null" */
if (memcmp("/dev/null", line, 9) || line[9] != '\n')
-   die(_("git apply: bad git-diff - expected /dev/null on 
line %d"), linenr);
+   die(_("git apply: bad git-diff - expected /dev/null on 
line %d"), state_linenr);
}
 }
 
@@ -1272,8 +1272,8 @@ static int parse_git_header(const char *line, int len, 
unsigned int size, struct
 
line += len;
size -= len;
-   linenr++;
-   for (offset = len ; size > 0 ; offset += len, size -= len, line += len, 
linenr++) {
+   state_linenr++;
+   for (offset = len ; size > 0 ; offset += len, size -= len, line += len, 
state_linenr++) {
static const struct opentry {
const char *str;
int (*fn)(const char *, struct patch *);
@@ -1440,7 +1440,7 @@ static int find_header(const char *line, unsigned long 
size, int *hdrsize, struc
patch->is_new = patch->is_delete = -1;
patch->old_mode = patch->new_mode = 0;
patch->old_name = patch->new_name = NULL;
-   for (offset = 0; size > 0; offset += len, size -= len, line += len, 
linenr++) {
+   for (offset = 0; size > 0; offset += len, size -= len, line += len, 
state_linenr++) {
unsigned long nextlen;
 
len = linelen(line, size);
@@ -1461,7 +1461,7 @@ static int find_header(const char *line, unsigned long 
size, int *hdrsize, struc
if (parse_fragment_header(line, len, ) < 0)
continue;
die(_("patch fragment without header at line %d: %.*s"),
-   linenr, (int)len-1, line);
+   state_linenr, (int)len-1, line);
}
 
if (size < len + 6)
@@ -1482,13 +1482,13 @@ static int find_header(const char *line, unsigned long 
size, int *hdrsize, struc
   "git diff header lacks filename 
information when removing "
   "%d leading pathname components 
(line %d)",
   state_p_value),
-   state_p_value, linenr);
+   state_p_value, state_linenr);
patch->old_name = xstrdup(patch->def_name);
patch->new_name = xstrdup(patch->def_name);
}
if (!patch-&

[PATCH v2 04/94] builtin/apply: avoid local variable shadowing 'len' parameter

2016-05-11 Thread Christian Couder
This is just a cleanup to avoid errors when compiling with -Wshadow and
to make it safer to later move global variables into a "state" struct.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 20 ++--
 1 file changed, 10 insertions(+), 10 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 705a9c8..bb8bf7f 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -2194,17 +2194,17 @@ static void update_pre_post_images(struct image 
*preimage,
fixed = preimage->buf;
 
for (i = reduced = ctx = 0; i < postimage->nr; i++) {
-   size_t len = postimage->line[i].len;
+   size_t l_len = postimage->line[i].len;
if (!(postimage->line[i].flag & LINE_COMMON)) {
/* an added line -- no counterparts in preimage */
-   memmove(new, old, len);
-   old += len;
-   new += len;
+   memmove(new, old, l_len);
+   old += l_len;
+   new += l_len;
continue;
}
 
/* a common context -- skip it in the original postimage */
-   old += len;
+   old += l_len;
 
/* and find the corresponding one in the fixed preimage */
while (ctx < preimage->nr &&
@@ -2223,11 +2223,11 @@ static void update_pre_post_images(struct image 
*preimage,
}
 
/* and copy it in, while fixing the line length */
-   len = preimage->line[ctx].len;
-   memcpy(new, fixed, len);
-   new += len;
-   fixed += len;
-   postimage->line[i].len = len;
+   l_len = preimage->line[ctx].len;
+   memcpy(new, fixed, l_len);
+   new += l_len;
+   fixed += l_len;
+   postimage->line[i].len = l_len;
ctx++;
}
 
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 00/94] libify apply and use lib in am

2016-05-11 Thread Christian Couder
-use-in-am25 

Performance numbers
~~~

Only tests on Linux have been performed. It could be interesting to
test on other platforms especially Windows and perhaps OSX too.

  - Around mid April Ævar did a huge many-hundred commit rebase on the
kernel with untracked cache.

command: git rebase --onto 1993b17 52bef0c 29dde7c

Vanilla "next" without split index:1m54.953s
Vanilla "next" with split index:   1m22.476s
This series on top of "next" without split index:  1m12.034s
This series on top of "next" with split index: 0m15.678s

Ævar used his Debian laptop with SSD.

  - Around mid April I tested rebasing 13 commits in Booking.com's
monorepo on a Red Hat 6.5 server with split-index and
GIT_TRACE_PERFORMANCE=1.

With Git v2.8.0, the rebase took 6.375888383 s, with the git am
command launched by the rebase command taking 3.705677431 s.

With this series on top of next, the rebase took 3.044529494 s, with
the git am command launched by the rebase command taking 0.583521168
s.


Christian Couder (94):
  builtin/apply: make gitdiff_verify_name() return void
  builtin/apply: avoid parameter shadowing 'p_value' global
  builtin/apply: avoid parameter shadowing 'linenr' global
  builtin/apply: avoid local variable shadowing 'len' parameter
  builtin/apply: extract line_by_line_fuzzy_match() from
match_fragment()
  builtin/apply: move 'options' variable into cmd_apply()
  builtin/apply: move 'read_stdin' global into cmd_apply()
  builtin/apply: introduce 'struct apply_state' to start libifying
  builtin/apply: move 'state' init into init_apply_state()
  builtin/apply: move 'unidiff_zero' global into 'struct apply_state'
  builtin/apply: move 'check' global into 'struct apply_state'
  builtin/apply: move 'check_index' global into 'struct apply_state'
  builtin/apply: move 'apply_in_reverse' global into 'struct
apply_state'
  builtin/apply: move 'apply_with_reject' global into 'struct
apply_state'
  builtin/apply: move 'apply_verbosely' global into 'struct apply_state'
  builtin/apply: move 'update_index' global into 'struct apply_state'
  builtin/apply: move 'allow_overlap' global into 'struct apply_state'
  builtin/apply: move 'cached' global into 'struct apply_state'
  builtin/apply: move 'diffstat' global into 'struct apply_state'
  builtin/apply: move 'numstat' global into 'struct apply_state'
  builtin/apply: move 'summary' global into 'struct apply_state'
  builtin/apply: move 'threeway' global into 'struct apply_state'
  builtin/apply: move 'no_add' global into 'struct apply_state'
  builtin/apply: move 'unsafe_paths' global into 'struct apply_state'
  builtin/apply: move 'line_termination' global into 'struct
apply_state'
  builtin/apply: move 'fake_ancestor' global into 'struct apply_state'
  builtin/apply: move 'p_context' global into 'struct apply_state'
  builtin/apply: move 'apply' global into 'struct apply_state'
  builtin/apply: move 'patch_input_file' global into 'struct
apply_state'
  builtin/apply: move 'limit_by_name' global into 'struct apply_state'
  builtin/apply: move 'has_include' global into 'struct apply_state'
  builtin/apply: move 'p_value' global into 'struct apply_state'
  builtin/apply: move 'p_value_known' global into 'struct apply_state'
  builtin/apply: move 'root' global into 'struct apply_state'
  builtin/apply: move 'whitespace_error' global into 'struct
apply_state'
  builtin/apply: move 'whitespace_option' into 'struct apply_state'
  builtin/apply: remove whitespace_option arg from
set_default_whitespace_mode()
  builtin/apply: move 'squelch_whitespace_errors' into 'struct
apply_state'
  builtin/apply: move 'applied_after_fixing_ws' into 'struct
apply_state'
  builtin/apply: move 'ws_error_action' into 'struct apply_state'
  builtin/apply: move 'ws_ignore_action' into 'struct apply_state'
  builtin/apply: move 'max_change' and 'max_len' into 'struct
apply_state'
  builtin/apply: move 'state_linenr' global into 'struct apply_state'
  builtin/apply: move 'fn_table' global into 'struct apply_state'
  builtin/apply: move 'symlink_changes' global into 'struct apply_state'
  builtin/apply: move 'state' check into check_apply_state()
  builtin/apply: move applying patches into apply_all_patches()
  builtin/apply: rename 'prefix_' parameter to 'prefix'
  builtin/apply: move 'lock_file' global into 'struct apply_state'
  builtin/apply: move 'newfd' global into 'struct apply_state'
  builtin/apply: make apply_patch() return -1 instead of die()ing
  builtin/apply: read_patch_file() return -1 instead of die()ing
  builtin/apply: make find_header() return -1 instead of die()ing
  builtin/apply: make parse_chunk() return a negative integer on error
  builtin/apply: make parse_single_patch() return -1 on error
  apply: move 'struct apply_state' to apply.h
  builtin/apply: make parse_whitespace_option() return -1 instead of
die()ing
  builti

[PATCH v2 01/94] builtin/apply: make gitdiff_verify_name() return void

2016-05-11 Thread Christian Couder
As the value returned by gitdiff_verify_name() is put into the
same variable that is passed as a parameter to this function,
it is simpler to pass the address of the variable and have
gitdiff_verify_name() change the variable itself.

This also makes it possible to later have this function return
-1 instead of die()ing in case of error.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 26 +-
 1 file changed, 13 insertions(+), 13 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 8e4da2e..fe5aebd 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -925,43 +925,43 @@ static int gitdiff_hdrend(const char *line, struct patch 
*patch)
 #define DIFF_OLD_NAME 0
 #define DIFF_NEW_NAME 1
 
-static char *gitdiff_verify_name(const char *line, int isnull, char 
*orig_name, int side)
+static void gitdiff_verify_name(const char *line, int isnull, char **name, int 
side)
 {
-   if (!orig_name && !isnull)
-   return find_name(line, NULL, p_value, TERM_TAB);
+   if (!*name && !isnull) {
+   *name = find_name(line, NULL, p_value, TERM_TAB);
+   return;
+   }
 
-   if (orig_name) {
-   int len = strlen(orig_name);
+   if (*name) {
+   int len = strlen(*name);
char *another;
if (isnull)
die(_("git apply: bad git-diff - expected /dev/null, 
got %s on line %d"),
-   orig_name, linenr);
+   *name, linenr);
another = find_name(line, NULL, p_value, TERM_TAB);
-   if (!another || memcmp(another, orig_name, len + 1))
+   if (!another || memcmp(another, *name, len + 1))
die((side == DIFF_NEW_NAME) ?
_("git apply: bad git-diff - inconsistent new 
filename on line %d") :
_("git apply: bad git-diff - inconsistent old 
filename on line %d"), linenr);
free(another);
-   return orig_name;
} else {
/* expect "/dev/null" */
if (memcmp("/dev/null", line, 9) || line[9] != '\n')
die(_("git apply: bad git-diff - expected /dev/null on 
line %d"), linenr);
-   return NULL;
}
 }
 
 static int gitdiff_oldname(const char *line, struct patch *patch)
 {
-   patch->old_name = gitdiff_verify_name(line, patch->is_new, 
patch->old_name,
- DIFF_OLD_NAME);
+   gitdiff_verify_name(line, patch->is_new, >old_name,
+   DIFF_OLD_NAME);
return 0;
 }
 
 static int gitdiff_newname(const char *line, struct patch *patch)
 {
-   patch->new_name = gitdiff_verify_name(line, patch->is_delete, 
patch->new_name,
- DIFF_NEW_NAME);
+   gitdiff_verify_name(line, patch->is_delete, >new_name,
+   DIFF_NEW_NAME);
return 0;
 }
 
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 09/94] builtin/apply: move 'state' init into init_apply_state()

2016-05-11 Thread Christian Couder
When the apply functionality will be libified, the 'struct apply_state'
will be used by different pieces of code.

To properly initialize a 'struct apply_state', let's provide a nice
and easy to use init_apply_state() function.

Helped-by: Eric Sunshine <sunsh...@sunshineco.com>
Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 23 ++-
 1 file changed, 14 insertions(+), 9 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index ae068e7..e133033 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4522,6 +4522,19 @@ static int option_parse_directory(const struct option 
*opt,
return 0;
 }
 
+static void init_apply_state(struct apply_state *state, const char *prefix)
+{
+   memset(state, 0, sizeof(*state));
+   state->prefix = prefix;
+   state->prefix_length = state->prefix ? strlen(state->prefix) : 0;
+
+   git_apply_config();
+   if (apply_default_whitespace)
+   parse_whitespace_option(apply_default_whitespace);
+   if (apply_default_ignorewhitespace)
+   parse_ignorewhitespace_option(apply_default_ignorewhitespace);
+}
+
 int cmd_apply(int argc, const char **argv, const char *prefix_)
 {
int i;
@@ -4603,15 +4616,7 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix_)
OPT_END()
};
 
-   memset(, 0, sizeof(state));
-   state.prefix = prefix_;
-   state.prefix_length = state.prefix ? strlen(state.prefix) : 0;
-
-   git_apply_config();
-   if (apply_default_whitespace)
-   parse_whitespace_option(apply_default_whitespace);
-   if (apply_default_ignorewhitespace)
-   parse_ignorewhitespace_option(apply_default_ignorewhitespace);
+   init_apply_state(, prefix_);
 
argc = parse_options(argc, argv, state.prefix, builtin_apply_options,
apply_usage, 0);
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 10/94] builtin/apply: move 'unidiff_zero' global into 'struct apply_state'

2016-05-11 Thread Christian Couder
To libify the apply functionality the 'unidiff_zero' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 42 --
 1 file changed, 24 insertions(+), 18 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index e133033..44ae95d 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -24,6 +24,8 @@
 struct apply_state {
const char *prefix;
int prefix_length;
+
+   int unidiff_zero;
 };
 
 /*
@@ -37,7 +39,6 @@ struct apply_state {
  */
 static int newfd = -1;
 
-static int unidiff_zero;
 static int state_p_value = 1;
 static int p_value_known;
 static int check_index;
@@ -2694,7 +2695,8 @@ static void update_image(struct image *img,
  * postimage) for the hunk.  Find lines that match "preimage" in "img" and
  * replace the part of "img" with "postimage" text.
  */
-static int apply_one_fragment(struct image *img, struct fragment *frag,
+static int apply_one_fragment(struct apply_state *state,
+ struct image *img, struct fragment *frag,
  int inaccurate_eof, unsigned ws_rule,
  int nth_fragment)
 {
@@ -2836,7 +2838,7 @@ static int apply_one_fragment(struct image *img, struct 
fragment *frag,
 * without leading context must match at the beginning.
 */
match_beginning = (!frag->oldpos ||
-  (frag->oldpos == 1 && !unidiff_zero));
+  (frag->oldpos == 1 && !state->unidiff_zero));
 
/*
 * A hunk without trailing lines must match at the end.
@@ -2844,7 +2846,7 @@ static int apply_one_fragment(struct image *img, struct 
fragment *frag,
 * from the lack of trailing lines if the patch was generated
 * with unidiff without any context.
 */
-   match_end = !unidiff_zero && !trailing;
+   match_end = !state->unidiff_zero && !trailing;
 
pos = frag->newpos ? (frag->newpos - 1) : 0;
preimage.buf = oldlines;
@@ -3067,7 +3069,7 @@ static int apply_binary(struct image *img, struct patch 
*patch)
return 0;
 }
 
-static int apply_fragments(struct image *img, struct patch *patch)
+static int apply_fragments(struct apply_state *state, struct image *img, 
struct patch *patch)
 {
struct fragment *frag = patch->fragments;
const char *name = patch->old_name ? patch->old_name : patch->new_name;
@@ -3080,7 +3082,7 @@ static int apply_fragments(struct image *img, struct 
patch *patch)
 
while (frag) {
nth++;
-   if (apply_one_fragment(img, frag, inaccurate_eof, ws_rule, 
nth)) {
+   if (apply_one_fragment(state, img, frag, inaccurate_eof, 
ws_rule, nth)) {
error(_("patch failed: %s:%ld"), name, frag->oldpos);
if (!apply_with_reject)
return -1;
@@ -3388,8 +3390,11 @@ static int load_current(struct image *image, struct 
patch *patch)
return 0;
 }
 
-static int try_threeway(struct image *image, struct patch *patch,
-   struct stat *st, const struct cache_entry *ce)
+static int try_threeway(struct apply_state *state,
+   struct image *image,
+   struct patch *patch,
+   struct stat *st,
+   const struct cache_entry *ce)
 {
unsigned char pre_sha1[20], post_sha1[20], our_sha1[20];
struct strbuf buf = STRBUF_INIT;
@@ -3415,7 +3420,7 @@ static int try_threeway(struct image *image, struct patch 
*patch,
img = strbuf_detach(, );
prepare_image(_image, img, len, 1);
/* Apply the patch to get the post image */
-   if (apply_fragments(_image, patch) < 0) {
+   if (apply_fragments(state, _image, patch) < 0) {
clear_image(_image);
return -1;
}
@@ -3459,7 +3464,8 @@ static int try_threeway(struct image *image, struct patch 
*patch,
return 0;
 }
 
-static int apply_data(struct patch *patch, struct stat *st, const struct 
cache_entry *ce)
+static int apply_data(struct apply_state *state, struct patch *patch,
+ struct stat *st, const struct cache_entry *ce)
 {
struct image image;
 
@@ -3467,9 +3473,9 @@ static int apply_data(struct patch *patch, struct stat 
*st, const struct cache_e
return -1;
 
if (patch->direct_to_threeway ||
-   apply_fragments(, patch) < 0) {
+   apply_fragments(state, , patch) < 0) {
/* Note: with --reject, apply_fragments() returns 0 */
-   if (!threeway || try_threeway(, patch, st, ce) < 0)
+

[PATCH v2 06/94] builtin/apply: move 'options' variable into cmd_apply()

2016-05-11 Thread Christian Couder
The 'options' variable doesn't need to be static and global to the
file. It can be local to cmd_apply(), so let's move it there.

This will make it easier to libify the apply functionality.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 7bab466..5a1d65a 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -79,7 +79,6 @@ static enum ws_ignore {
 static const char *patch_input_file;
 static struct strbuf root = STRBUF_INIT;
 static int read_stdin = 1;
-static int options;
 
 static void parse_whitespace_option(const char *option)
 {
@@ -4517,6 +4516,7 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix_)
int errs = 0;
int is_not_gitdir = !startup_info->have_repository;
int force_apply = 0;
+   int options = 0;
 
const char *whitespace_option = NULL;
 
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 05/94] builtin/apply: extract line_by_line_fuzzy_match() from match_fragment()

2016-05-11 Thread Christian Couder
The match_fragment() function is very big and contains a big special case
algorithm that does line by line fuzzy matching. So let's extract this
algorithm in a separate line_by_line_fuzzy_match() function.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 126 +++-
 1 file changed, 71 insertions(+), 55 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index bb8bf7f..7bab466 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -2242,6 +2242,74 @@ static void update_pre_post_images(struct image 
*preimage,
postimage->nr -= reduced;
 }
 
+static int line_by_line_fuzzy_match(struct image *img,
+   struct image *preimage,
+   struct image *postimage,
+   unsigned long try,
+   int try_lno,
+   int preimage_limit)
+{
+   int i;
+   size_t imgoff = 0;
+   size_t preoff = 0;
+   size_t postlen = postimage->len;
+   size_t extra_chars;
+   char *buf;
+   char *preimage_eof;
+   char *preimage_end;
+   struct strbuf fixed;
+   char *fixed_buf;
+   size_t fixed_len;
+
+   for (i = 0; i < preimage_limit; i++) {
+   size_t prelen = preimage->line[i].len;
+   size_t imglen = img->line[try_lno+i].len;
+
+   if (!fuzzy_matchlines(img->buf + try + imgoff, imglen,
+ preimage->buf + preoff, prelen))
+   return 0;
+   if (preimage->line[i].flag & LINE_COMMON)
+   postlen += imglen - prelen;
+   imgoff += imglen;
+   preoff += prelen;
+   }
+
+   /*
+* Ok, the preimage matches with whitespace fuzz.
+*
+* imgoff now holds the true length of the target that
+* matches the preimage before the end of the file.
+*
+* Count the number of characters in the preimage that fall
+* beyond the end of the file and make sure that all of them
+* are whitespace characters. (This can only happen if
+* we are removing blank lines at the end of the file.)
+*/
+   buf = preimage_eof = preimage->buf + preoff;
+   for ( ; i < preimage->nr; i++)
+   preoff += preimage->line[i].len;
+   preimage_end = preimage->buf + preoff;
+   for ( ; buf < preimage_end; buf++)
+   if (!isspace(*buf))
+   return 0;
+
+   /*
+* Update the preimage and the common postimage context
+* lines to use the same whitespace as the target.
+* If whitespace is missing in the target (i.e.
+* if the preimage extends beyond the end of the file),
+* use the whitespace from the preimage.
+*/
+   extra_chars = preimage_end - preimage_eof;
+   strbuf_init(, imgoff + extra_chars);
+   strbuf_add(, img->buf + try, imgoff);
+   strbuf_add(, preimage_eof, extra_chars);
+   fixed_buf = strbuf_detach(, _len);
+   update_pre_post_images(preimage, postimage,
+  fixed_buf, fixed_len, postlen);
+   return 1;
+}
+
 static int match_fragment(struct image *img,
  struct image *preimage,
  struct image *postimage,
@@ -2331,61 +2399,9 @@ static int match_fragment(struct image *img,
 * fuzzy matching. We collect all the line length information because
 * we need it to adjust whitespace if we match.
 */
-   if (ws_ignore_action == ignore_ws_change) {
-   size_t imgoff = 0;
-   size_t preoff = 0;
-   size_t postlen = postimage->len;
-   size_t extra_chars;
-   char *preimage_eof;
-   char *preimage_end;
-   for (i = 0; i < preimage_limit; i++) {
-   size_t prelen = preimage->line[i].len;
-   size_t imglen = img->line[try_lno+i].len;
-
-   if (!fuzzy_matchlines(img->buf + try + imgoff, imglen,
- preimage->buf + preoff, prelen))
-   return 0;
-   if (preimage->line[i].flag & LINE_COMMON)
-   postlen += imglen - prelen;
-   imgoff += imglen;
-   preoff += prelen;
-   }
-
-   /*
-* Ok, the preimage matches with whitespace fuzz.
-*
-* imgoff now holds the true length of the target that
-* matches the preimage before the end of the file.
-*
-* Count the number of characters in the p

[PATCH v2 54/94] builtin/apply: make parse_chunk() return a negative integer on error

2016-05-11 Thread Christian Couder
To libify `git apply` functionality we have to signal errors to the
caller instead of die()ing or exit()ing.

To do that in a compatible manner with the rest of the error handling
in builtin/apply.c, find_header() should return -1 instead of calling
die() or exit().

As parse_chunk() is called only by apply_patch() which already
returns -1 when an error happened, let's make apply_patch() return -1
when parse_chunk() returns -1.

If find_header() returns -2 because no patch header has been found, it
is ok for parse_chunk() to also return -2. If find_header() returns -1
because an error happened, it is ok for parse_chunk() to do the same.

Helped-by: Eric Sunshine <sunsh...@sunshineco.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 21 +
 1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 4212705..2380472 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -2101,22 +2101,22 @@ static int use_patch(struct apply_state *state, struct 
patch *p)
return !state->has_include;
 }
 
-
 /*
  * Read the patch text in "buffer" that extends for "size" bytes; stop
  * reading after seeing a single patch (i.e. changes to a single file).
  * Create fragments (i.e. patch hunks) and hang them to the given patch.
- * Return the number of bytes consumed, so that the caller can call us
- * again for the next patch.
+ *
+ * Returns:
+ *   -1 on error,
+ *   -2 if no header was found,
+ *   the number of bytes consumed otherwise,
+ * so that the caller can call us again for the next patch.
  */
 static int parse_chunk(struct apply_state *state, char *buffer, unsigned long 
size, struct patch *patch)
 {
int hdrsize, patchsize;
int offset = find_header(state, buffer, size, , patch);
 
-   if (offset == -1)
-   exit(1);
-
if (offset < 0)
return offset;
 
@@ -2176,8 +2176,9 @@ static int parse_chunk(struct apply_state *state, char 
*buffer, unsigned long si
 * empty to us here.
 */
if ((state->apply || state->check) &&
-   (!patch->is_binary && !metadata_changes(patch)))
-   die(_("patch with only garbage at line %d"), 
state->linenr);
+   (!patch->is_binary && !metadata_changes(patch))) {
+   return error(_("patch with only garbage at line %d"), 
state->linenr);
+   }
}
 
return offset + hdrsize + patchsize;
@@ -4557,6 +4558,10 @@ static int apply_patch(struct apply_state *state,
nr = parse_chunk(state, buf.buf + offset, buf.len - offset, 
patch);
if (nr < 0) {
free_patch(patch);
+   if (nr == -1) {
+   res = -1;
+   goto end;
+   }
break;
}
if (state->apply_in_reverse)
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 62/94] builtin/apply: move check_apply_state() to apply.c

2016-05-11 Thread Christian Couder
To libify `git apply` functionality we must make check_apply_state()
usable outside "builtin/apply.c".

Let's do that by moving it into "apply.c".

Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 apply.c | 29 +
 apply.h |  1 +
 builtin/apply.c | 29 -
 3 files changed, 30 insertions(+), 29 deletions(-)

diff --git a/apply.c b/apply.c
index 1e2b802..2128594 100644
--- a/apply.c
+++ b/apply.c
@@ -82,3 +82,32 @@ int init_apply_state(struct apply_state *state,
return 0;
 }
 
+int check_apply_state(struct apply_state *state, int force_apply)
+{
+   int is_not_gitdir = !startup_info->have_repository;
+
+   if (state->apply_with_reject && state->threeway)
+   return error("--reject and --3way cannot be used together.");
+   if (state->cached && state->threeway)
+   return error("--cached and --3way cannot be used together.");
+   if (state->threeway) {
+   if (is_not_gitdir)
+   return error(_("--3way outside a repository"));
+   state->check_index = 1;
+   }
+   if (state->apply_with_reject)
+   state->apply = state->apply_verbosely = 1;
+   if (!force_apply && (state->diffstat || state->numstat || 
state->summary || state->check || state->fake_ancestor))
+   state->apply = 0;
+   if (state->check_index && is_not_gitdir)
+   return error(_("--index outside a repository"));
+   if (state->cached) {
+   if (is_not_gitdir)
+   return error(_("--cached outside a repository"));
+   state->check_index = 1;
+   }
+   if (state->check_index)
+   state->unsafe_paths = 0;
+   return 0;
+}
+
diff --git a/apply.h b/apply.h
index f3b2ae4..5266612 100644
--- a/apply.h
+++ b/apply.h
@@ -120,5 +120,6 @@ extern int parse_ignorewhitespace_option(struct apply_state 
*state,
 extern int init_apply_state(struct apply_state *state,
const char *prefix,
struct lock_file *lock_file);
+extern int check_apply_state(struct apply_state *state, int force_apply);
 
 #endif
diff --git a/builtin/apply.c b/builtin/apply.c
index 2b3d10b..ae16f99 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4540,35 +4540,6 @@ static int option_parse_directory(const struct option 
*opt,
return 0;
 }
 
-static int check_apply_state(struct apply_state *state, int force_apply)
-{
-   int is_not_gitdir = !startup_info->have_repository;
-
-   if (state->apply_with_reject && state->threeway)
-   return error("--reject and --3way cannot be used together.");
-   if (state->cached && state->threeway)
-   return error("--cached and --3way cannot be used together.");
-   if (state->threeway) {
-   if (is_not_gitdir)
-   return error(_("--3way outside a repository"));
-   state->check_index = 1;
-   }
-   if (state->apply_with_reject)
-   state->apply = state->apply_verbosely = 1;
-   if (!force_apply && (state->diffstat || state->numstat || 
state->summary || state->check || state->fake_ancestor))
-   state->apply = 0;
-   if (state->check_index && is_not_gitdir)
-   return error(_("--index outside a repository"));
-   if (state->cached) {
-   if (is_not_gitdir)
-   return error(_("--cached outside a repository"));
-   state->check_index = 1;
-   }
-   if (state->check_index)
-   state->unsafe_paths = 0;
-   return 0;
-}
-
 static int apply_all_patches(struct apply_state *state,
 int argc,
 const char **argv,
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 42/94] builtin/apply: move 'max_change' and 'max_len' into 'struct apply_state'

2016-05-11 Thread Christian Couder
To libify the apply functionality the 'max_change' and 'max_len'
variables should not be static and global to the file. Let's move
them into 'struct apply_state'.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 49 +
 1 file changed, 25 insertions(+), 24 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index f8c3677..deba14c 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -76,6 +76,14 @@ struct apply_state {
int unsafe_paths;
int line_termination;
 
+   /*
+* For "diff-stat" like behaviour, we keep track of the biggest change
+* we've seen, and the longest filename. That allows us to do simple
+* scaling.
+*/
+   int max_change;
+   int max_len;
+
int p_value;
int p_value_known;
unsigned int p_context;
@@ -148,13 +156,6 @@ static void set_default_whitespace_mode(struct apply_state 
*state)
state->ws_error_action = (state->apply ? warn_on_ws_error : 
nowarn_ws_error);
 }
 
-/*
- * For "diff-stat" like behaviour, we keep track of the biggest change
- * we've seen, and the longest filename. That allows us to do simple
- * scaling.
- */
-static int max_change, max_len;
-
 /*
  * Various "current state", notably line numbers and what
  * file (and how) we're patching right now.. The "is_"
@@ -2179,7 +2180,7 @@ static const char pluses[] =
 static const char minuses[]=
 "--";
 
-static void show_stats(struct patch *patch)
+static void show_stats(struct apply_state *state, struct patch *patch)
 {
struct strbuf qname = STRBUF_INIT;
char *cp = patch->new_name ? patch->new_name : patch->old_name;
@@ -2190,7 +2191,7 @@ static void show_stats(struct patch *patch)
/*
 * "scale" the filename
 */
-   max = max_len;
+   max = state->max_len;
if (max > 50)
max = 50;
 
@@ -2213,13 +2214,13 @@ static void show_stats(struct patch *patch)
/*
 * scale the add/delete
 */
-   max = max + max_change > 70 ? 70 - max : max_change;
+   max = max + state->max_change > 70 ? 70 - max : state->max_change;
add = patch->lines_added;
del = patch->lines_deleted;
 
-   if (max_change > 0) {
-   int total = ((add + del) * max + max_change / 2) / max_change;
-   add = (add * max + max_change / 2) / max_change;
+   if (state->max_change > 0) {
+   int total = ((add + del) * max + state->max_change / 2) / 
state->max_change;
+   add = (add * max + state->max_change / 2) / state->max_change;
del = total - add;
}
printf("%5d %.*s%.*s\n", patch->lines_added + patch->lines_deleted,
@@ -4045,7 +4046,7 @@ static void build_fake_ancestor(struct patch *list, const 
char *filename)
discard_index();
 }
 
-static void stat_patch_list(struct patch *patch)
+static void stat_patch_list(struct apply_state *state, struct patch *patch)
 {
int files, adds, dels;
 
@@ -4053,7 +4054,7 @@ static void stat_patch_list(struct patch *patch)
files++;
adds += patch->lines_added;
dels += patch->lines_deleted;
-   show_stats(patch);
+   show_stats(state, patch);
}
 
print_stat_summary(stdout, files, adds, dels);
@@ -4151,25 +4152,25 @@ static void summary_patch_list(struct patch *patch)
}
 }
 
-static void patch_stats(struct patch *patch)
+static void patch_stats(struct apply_state *state, struct patch *patch)
 {
int lines = patch->lines_added + patch->lines_deleted;
 
-   if (lines > max_change)
-   max_change = lines;
+   if (lines > state->max_change)
+   state->max_change = lines;
if (patch->old_name) {
int len = quote_c_style(patch->old_name, NULL, NULL, 0);
if (!len)
len = strlen(patch->old_name);
-   if (len > max_len)
-   max_len = len;
+   if (len > state->max_len)
+   state->max_len = len;
}
if (patch->new_name) {
int len = quote_c_style(patch->new_name, NULL, NULL, 0);
if (!len)
len = strlen(patch->new_name);
-   if (len > max_len)
-   max_len = len;
+   if (len > state->max_len)
+   state->max_len = len;
}
 }
 
@@ -4526,7 +4527,7 @@ static int apply_patch(struct apply_state *state,
if

[PATCH v2 32/94] builtin/apply: move 'p_value' global into 'struct apply_state'

2016-05-11 Thread Christian Couder
To libify the apply functionality the 'p_value' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 151 +---
 1 file changed, 99 insertions(+), 52 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index f2ee8bf..4e476d5 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -62,12 +62,12 @@ struct apply_state {
int unsafe_paths;
int line_termination;
 
+   int p_value;
unsigned int p_context;
 };
 
 static int newfd = -1;
 
-static int state_p_value = 1;
 static int p_value_known;
 
 static const char * const apply_usage[] = {
@@ -888,24 +888,24 @@ static void parse_traditional_patch(struct apply_state 
*state,
q = guess_p_value(state, second);
if (p < 0) p = q;
if (0 <= p && p == q) {
-   state_p_value = p;
+   state->p_value = p;
p_value_known = 1;
}
}
if (is_dev_null(first)) {
patch->is_new = 1;
patch->is_delete = 0;
-   name = find_name_traditional(second, NULL, state_p_value);
+   name = find_name_traditional(second, NULL, state->p_value);
patch->new_name = name;
} else if (is_dev_null(second)) {
patch->is_new = 0;
patch->is_delete = 1;
-   name = find_name_traditional(first, NULL, state_p_value);
+   name = find_name_traditional(first, NULL, state->p_value);
patch->old_name = name;
} else {
char *first_name;
-   first_name = find_name_traditional(first, NULL, state_p_value);
-   name = find_name_traditional(second, first_name, state_p_value);
+   first_name = find_name_traditional(first, NULL, state->p_value);
+   name = find_name_traditional(second, first_name, 
state->p_value);
free(first_name);
if (has_epoch_timestamp(first)) {
patch->is_new = 1;
@@ -924,7 +924,9 @@ static void parse_traditional_patch(struct apply_state 
*state,
die(_("unable to find filename in patch at line %d"), 
state_linenr);
 }
 
-static int gitdiff_hdrend(const char *line, struct patch *patch)
+static int gitdiff_hdrend(struct apply_state *state,
+ const char *line,
+ struct patch *patch)
 {
return -1;
 }
@@ -941,10 +943,14 @@ static int gitdiff_hdrend(const char *line, struct patch 
*patch)
 #define DIFF_OLD_NAME 0
 #define DIFF_NEW_NAME 1
 
-static void gitdiff_verify_name(const char *line, int isnull, char **name, int 
side)
+static void gitdiff_verify_name(struct apply_state *state,
+   const char *line,
+   int isnull,
+   char **name,
+   int side)
 {
if (!*name && !isnull) {
-   *name = find_name(line, NULL, state_p_value, TERM_TAB);
+   *name = find_name(line, NULL, state->p_value, TERM_TAB);
return;
}
 
@@ -954,7 +960,7 @@ static void gitdiff_verify_name(const char *line, int 
isnull, char **name, int s
if (isnull)
die(_("git apply: bad git-diff - expected /dev/null, 
got %s on line %d"),
*name, state_linenr);
-   another = find_name(line, NULL, state_p_value, TERM_TAB);
+   another = find_name(line, NULL, state->p_value, TERM_TAB);
if (!another || memcmp(another, *name, len + 1))
die((side == DIFF_NEW_NAME) ?
_("git apply: bad git-diff - inconsistent new 
filename on line %d") :
@@ -967,81 +973,105 @@ static void gitdiff_verify_name(const char *line, int 
isnull, char **name, int s
}
 }
 
-static int gitdiff_oldname(const char *line, struct patch *patch)
+static int gitdiff_oldname(struct apply_state *state,
+  const char *line,
+  struct patch *patch)
 {
-   gitdiff_verify_name(line, patch->is_new, >old_name,
+   gitdiff_verify_name(state, line,
+   patch->is_new, >old_name,
DIFF_OLD_NAME);
return 0;
 }
 
-static int gitdiff_newname(const char *line, struct patch *patch)
+static int gitdiff_newname(struct apply_state *state,
+  const char *line,
+  struct patch *patch)
 {
-   gitdiff_verify_name(line, patch->is_delete, >new_name,
+   gitdiff

[PATCH v2 50/94] builtin/apply: move 'newfd' global into 'struct apply_state'

2016-05-11 Thread Christian Couder
To libify the apply functionality the 'newfd' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 9 +
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index cad2c56..ec55768 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -57,6 +57,7 @@ struct apply_state {
 * lock_file structures, it isn't safe to free(lock_file).
 */
struct lock_file *lock_file;
+   int newfd;
 
int apply;
int allow_overlap;
@@ -131,8 +132,6 @@ struct apply_state {
enum ws_ignore ws_ignore_action;
 };
 
-static int newfd = -1;
-
 static const char * const apply_usage[] = {
N_("git apply [] [...]"),
NULL
@@ -4561,8 +4560,8 @@ static int apply_patch(struct apply_state *state,
state->apply = 0;
 
state->update_index = state->check_index && state->apply;
-   if (state->update_index && newfd < 0)
-   newfd = hold_locked_index(state->lock_file, 1);
+   if (state->update_index && state->newfd < 0)
+   state->newfd = hold_locked_index(state->lock_file, 1);
 
if (state->check_index) {
if (read_cache() < 0)
@@ -4671,6 +4670,7 @@ static void init_apply_state(struct apply_state *state,
state->prefix = prefix;
state->prefix_length = state->prefix ? strlen(state->prefix) : 0;
state->lock_file = lock_file ? lock_file : xcalloc(1, 
sizeof(*lock_file));
+   state->newfd = -1;
state->apply = 1;
state->line_termination = '\n';
state->p_value = 1;
@@ -4780,6 +4780,7 @@ static int apply_all_patches(struct apply_state *state,
if (state->update_index) {
if (write_locked_index(_index, state->lock_file, 
COMMIT_LOCK))
die(_("Unable to write new index file"));
+   state->newfd = -1;
}
 
return !!errs;
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 84/94] builtin/am: use apply api in run_apply()

2016-05-11 Thread Christian Couder
This replaces run_apply() implementation with a new one that
uses the apply api that has been previously prepared in
apply.c and apply.h.

This shoud improve performance a lot in certain cases.

As the previous implementation was creating a new `git apply`
process to apply each patch, it could be slow on systems like
Windows where it is costly to create new processes.

Also the new `git apply` process had to read the index from
disk, and when the process was done the calling process
discarded its own index and read back from disk the new
index that had been created by the `git apply` process.

This could be very inefficient with big repositories that
have big index files, especially when the system decided
that it was a good idea to run the `git apply` processes on
a different processor core.

Also eliminating index reads enables further performance
improvements by using:

`git update-index --split-index`

For example here is a benchmark of a multi hundred commit
rebase on the Linux kernel on a Debian laptop with SSD:

command: git rebase --onto 1993b17 52bef0c 29dde7c

Vanilla "next" without split index:1m54.953s
Vanilla "next" with split index:   1m22.476s
This series on top of "next" without split index:  1m12.034s
This series on top of "next" with split index: 0m15.678s

(using branch "next" from mid April 2016.)

Benchmarked-by: Ævar Arnfjörð Bjarmason <ava...@gmail.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/am.c | 104 ---
 1 file changed, 86 insertions(+), 18 deletions(-)

diff --git a/builtin/am.c b/builtin/am.c
index d003939..cc66a48 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -28,6 +28,7 @@
 #include "rerere.h"
 #include "prompt.h"
 #include "mailinfo.h"
+#include "apply.h"
 
 /**
  * Returns 1 if the file is empty or does not exist, 0 otherwise.
@@ -1522,39 +1523,106 @@ static int parse_mail_rebase(struct am_state *state, 
const char *mail)
  */
 static int run_apply(const struct am_state *state, const char *index_file)
 {
-   struct child_process cp = CHILD_PROCESS_INIT;
-
-   cp.git_cmd = 1;
-
-   if (index_file)
-   argv_array_pushf(_array, "GIT_INDEX_FILE=%s", 
index_file);
+   struct argv_array apply_paths = ARGV_ARRAY_INIT;
+   struct argv_array apply_opts = ARGV_ARRAY_INIT;
+   struct apply_state apply_state;
+   int save_stdout_fd, save_stderr_fd;
+   int res, opts_left;
+   char *save_index_file;
+   static struct lock_file lock_file;
+
+   struct option am_apply_options[] = {
+   { OPTION_CALLBACK, 0, "whitespace", _state, N_("action"),
+   N_("detect new or modified lines that have whitespace 
errors"),
+   0, apply_option_parse_whitespace },
+   { OPTION_CALLBACK, 0, "ignore-space-change", _state, NULL,
+   N_("ignore changes in whitespace when finding context"),
+   PARSE_OPT_NOARG, apply_option_parse_space_change },
+   { OPTION_CALLBACK, 0, "ignore-whitespace", _state, NULL,
+   N_("ignore changes in whitespace when finding context"),
+   PARSE_OPT_NOARG, apply_option_parse_space_change },
+   { OPTION_CALLBACK, 0, "directory", _state, N_("root"),
+   N_("prepend  to all filenames"),
+   0, apply_option_parse_directory },
+   { OPTION_CALLBACK, 0, "exclude", _state, N_("path"),
+   N_("don't apply changes matching the given path"),
+   0, apply_option_parse_exclude },
+   { OPTION_CALLBACK, 0, "include", _state, N_("path"),
+   N_("apply changes matching the given path"),
+   0, apply_option_parse_include },
+   OPT_INTEGER('C', NULL, _state.p_context,
+   N_("ensure at least  lines of context 
match")),
+   { OPTION_CALLBACK, 'p', NULL, _state, N_("num"),
+   N_("remove  leading slashes from traditional diff 
paths"),
+   0, apply_option_parse_p },
+   OPT_BOOL(0, "reject", _state.apply_with_reject,
+   N_("leave the rejected hunks in corresponding *.rej 
files")),
+   OPT_END()
+   };
 
/*
 * If we are allowed to fall back on 3-way merge, don't give false
 * errors during the initial attempt.
 */
+
if (state->threeway && !index_file) {
-   cp.no_stdout = 1;
-

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

2016-05-11 Thread Christian Couder
To libify `git apply` functionality we have to signal errors to the
caller instead of die()ing.

To do that in a compatible manner with the rest of the error handling
in builtin/apply.c, parse_single_patch() should return -1 instead of
calling die().

Let's do that by using error() and let's adjust the related test
cases accordingly.

Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c| 17 +
 t/t4012-diff-binary.sh |  4 ++--
 2 files changed, 15 insertions(+), 6 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 2380472..58bcfeb 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -1776,6 +1776,10 @@ static int parse_fragment(struct apply_state *state,
  *
  * The (fragment->patch, fragment->size) pair points into the memory given
  * by the caller, not a copy, when we return.
+ *
+ * Returns:
+ *   -1 in case of error,
+ *   the number of bytes in the patch otherwise.
  */
 static int parse_single_patch(struct apply_state *state,
  const char *line,
@@ -1793,8 +1797,10 @@ static int parse_single_patch(struct apply_state *state,
fragment = xcalloc(1, sizeof(*fragment));
fragment->linenr = state->linenr;
len = parse_fragment(state, line, size, patch, fragment);
-   if (len <= 0)
-   die(_("corrupt patch at line %d"), state->linenr);
+   if (len <= 0) {
+   free(fragment);
+   return error(_("corrupt patch at line %d"), 
state->linenr);
+   }
fragment->patch = line;
fragment->size = len;
oldlines += fragment->oldlines;
@@ -1830,9 +1836,9 @@ static int parse_single_patch(struct apply_state *state,
patch->is_delete = 0;
 
if (0 < patch->is_new && oldlines)
-   die(_("new file %s depends on old contents"), patch->new_name);
+   return error(_("new file %s depends on old contents"), 
patch->new_name);
if (0 < patch->is_delete && newlines)
-   die(_("deleted file %s still has contents"), patch->old_name);
+   return error(_("deleted file %s still has contents"), 
patch->old_name);
if (!patch->is_delete && !newlines && context)
fprintf_ln(stderr,
   _("** warning: "
@@ -2134,6 +2140,9 @@ static int parse_chunk(struct apply_state *state, char 
*buffer, unsigned long si
   size - offset - hdrsize,
   patch);
 
+   if (patchsize < 0)
+   return -1;
+
if (!patchsize) {
static const char git_binary[] = "GIT binary patch\n";
int hd = hdrsize + offset;
diff --git a/t/t4012-diff-binary.sh b/t/t4012-diff-binary.sh
index 643d729..0a8af76 100755
--- a/t/t4012-diff-binary.sh
+++ b/t/t4012-diff-binary.sh
@@ -68,7 +68,7 @@ test_expect_success C_LOCALE_OUTPUT 'apply detecting corrupt 
patch correctly' '
sed -e "s/-CIT/xCIT/" broken &&
test_must_fail git apply --stat --summary broken 2>detected &&
detected=$(cat detected) &&
-   detected=$(expr "$detected" : "fatal.*at line \\([0-9]*\\)\$") &&
+   detected=$(expr "$detected" : "error.*at line \\([0-9]*\\)\$") &&
detected=$(sed -ne "${detected}p" broken) &&
test "$detected" = xCIT
 '
@@ -77,7 +77,7 @@ test_expect_success C_LOCALE_OUTPUT 'apply detecting corrupt 
patch correctly' '
git diff --binary | sed -e "s/-CIT/xCIT/" >broken &&
test_must_fail git apply --stat --summary broken 2>detected &&
detected=$(cat detected) &&
-   detected=$(expr "$detected" : "fatal.*at line \\([0-9]*\\)\$") &&
+   detected=$(expr "$detected" : "error.*at line \\([0-9]*\\)\$") &&
detected=$(sed -ne "${detected}p" broken) &&
test "$detected" = xCIT
 '
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 45/94] builtin/apply: move 'symlink_changes' global into 'struct apply_state'

2016-05-11 Thread Christian Couder
To libify the apply functionality the 'symlink_changes' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 49 +++--
 1 file changed, 27 insertions(+), 22 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 506e9ec..14286d2 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -34,6 +34,20 @@ enum ws_ignore {
ignore_ws_change
 };
 
+/*
+ * We need to keep track of how symlinks in the preimage are
+ * manipulated by the patches.  A patch to add a/b/c where a/b
+ * is a symlink should not be allowed to affect the directory
+ * the symlink points at, but if the same patch removes a/b,
+ * it is perfectly fine, as the patch removes a/b to make room
+ * to create a directory a/b so that a/b/c can be created.
+ *
+ * See also "struct string_list symlink_changes" in "struct
+ * apply_state".
+ */
+#define SYMLINK_GOES_AWAY 01
+#define SYMLINK_IN_RESULT 02
+
 struct apply_state {
const char *prefix;
int prefix_length;
@@ -61,6 +75,7 @@ struct apply_state {
struct string_list limit_by_name;
int has_include;
struct strbuf root;
+   struct string_list symlink_changes;
 
/*
 *  --check turns on checking that the working tree matches the
@@ -3713,52 +3728,42 @@ static int check_to_create(struct apply_state *state,
return 0;
 }
 
-/*
- * We need to keep track of how symlinks in the preimage are
- * manipulated by the patches.  A patch to add a/b/c where a/b
- * is a symlink should not be allowed to affect the directory
- * the symlink points at, but if the same patch removes a/b,
- * it is perfectly fine, as the patch removes a/b to make room
- * to create a directory a/b so that a/b/c can be created.
- */
-static struct string_list symlink_changes;
-#define SYMLINK_GOES_AWAY 01
-#define SYMLINK_IN_RESULT 02
-
-static uintptr_t register_symlink_changes(const char *path, uintptr_t what)
+static uintptr_t register_symlink_changes(struct apply_state *state,
+ const char *path,
+ uintptr_t what)
 {
struct string_list_item *ent;
 
-   ent = string_list_lookup(_changes, path);
+   ent = string_list_lookup(>symlink_changes, path);
if (!ent) {
-   ent = string_list_insert(_changes, path);
+   ent = string_list_insert(>symlink_changes, path);
ent->util = (void *)0;
}
ent->util = (void *)(what | ((uintptr_t)ent->util));
return (uintptr_t)ent->util;
 }
 
-static uintptr_t check_symlink_changes(const char *path)
+static uintptr_t check_symlink_changes(struct apply_state *state, const char 
*path)
 {
struct string_list_item *ent;
 
-   ent = string_list_lookup(_changes, path);
+   ent = string_list_lookup(>symlink_changes, path);
if (!ent)
return 0;
return (uintptr_t)ent->util;
 }
 
-static void prepare_symlink_changes(struct patch *patch)
+static void prepare_symlink_changes(struct apply_state *state, struct patch 
*patch)
 {
for ( ; patch; patch = patch->next) {
if ((patch->old_name && S_ISLNK(patch->old_mode)) &&
(patch->is_rename || patch->is_delete))
/* the symlink at patch->old_name is removed */
-   register_symlink_changes(patch->old_name, 
SYMLINK_GOES_AWAY);
+   register_symlink_changes(state, patch->old_name, 
SYMLINK_GOES_AWAY);
 
if (patch->new_name && S_ISLNK(patch->new_mode))
/* the symlink at patch->new_name is created or remains 
*/
-   register_symlink_changes(patch->new_name, 
SYMLINK_IN_RESULT);
+   register_symlink_changes(state, patch->new_name, 
SYMLINK_IN_RESULT);
}
 }
 
@@ -3772,7 +3777,7 @@ static int path_is_beyond_symlink_1(struct apply_state 
*state, struct strbuf *na
if (!name->len)
break;
name->buf[name->len] = '\0';
-   change = check_symlink_changes(name->buf);
+   change = check_symlink_changes(state, name->buf);
if (change & SYMLINK_IN_RESULT)
return 1;
if (change & SYMLINK_GOES_AWAY)
@@ -3941,7 +3946,7 @@ static int check_patch_list(struct apply_state *state, 
struct patch *patch)
 {
int err = 0;
 
-   prepare_symlink_changes(patch);
+   prepare_symlink_changes(state, patch);
prepare_fn_table(state, patch);
while (patch) {
if (state->apply_verbosely)
-- 
2.8.2.490

[PATCH v2 81/94] run-command: make dup_devnull() non static

2016-05-11 Thread Christian Couder
We will need this function in a later commit to redirect stdout
and stderr to /dev/null.

Helped-by: Johannes Sixt <j...@kdbg.org>
Helped-by: Johannes Schindelin <johannes.schinde...@gmx.de>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 run-command.c | 2 +-
 run-command.h | 6 ++
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/run-command.c b/run-command.c
index e4593cd..5d1cedf 100644
--- a/run-command.c
+++ b/run-command.c
@@ -85,7 +85,7 @@ static inline void close_pair(int fd[2])
 }
 
 #ifndef GIT_WINDOWS_NATIVE
-static inline void dup_devnull(int to)
+void dup_devnull(int to)
 {
int fd = open("/dev/null", O_RDWR);
if (fd < 0)
diff --git a/run-command.h b/run-command.h
index 11f76b0..e05ce7d 100644
--- a/run-command.h
+++ b/run-command.h
@@ -201,4 +201,10 @@ int run_processes_parallel(int n,
   task_finished_fn,
   void *pp_cb);
 
+/**
+ * Misc helper functions
+ */
+
+void dup_devnull(int to);
+
 #endif
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 59/94] builtin/apply: move init_apply_state() to apply.c

2016-05-11 Thread Christian Couder
To libify `git apply` functionality we must make init_apply_state()
usable outside "builtin/apply.c".

Let's do that by moving it into a new "apply.c".

Helped-by: Eric Sunshine <sunsh...@sunshineco.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 Makefile|  1 +
 apply.c | 83 +
 apply.h |  9 +++
 builtin/apply.c | 79 --
 4 files changed, 93 insertions(+), 79 deletions(-)
 create mode 100644 apply.c

diff --git a/Makefile b/Makefile
index 3f03366..b2a7b0c 100644
--- a/Makefile
+++ b/Makefile
@@ -683,6 +683,7 @@ LIB_OBJS += abspath.o
 LIB_OBJS += advice.o
 LIB_OBJS += alias.o
 LIB_OBJS += alloc.o
+LIB_OBJS += apply.o
 LIB_OBJS += archive.o
 LIB_OBJS += archive-tar.o
 LIB_OBJS += archive-zip.o
diff --git a/apply.c b/apply.c
new file mode 100644
index 000..508ea64
--- /dev/null
+++ b/apply.c
@@ -0,0 +1,83 @@
+#include "cache.h"
+#include "lockfile.h"
+#include "apply.h"
+
+static void git_apply_config(void)
+{
+   git_config_get_string_const("apply.whitespace", 
_default_whitespace);
+   git_config_get_string_const("apply.ignorewhitespace", 
_default_ignorewhitespace);
+   git_config(git_default_config, NULL);
+}
+
+int parse_whitespace_option(struct apply_state *state, const char *option)
+{
+   if (!option) {
+   state->ws_error_action = warn_on_ws_error;
+   return 0;
+   }
+   if (!strcmp(option, "warn")) {
+   state->ws_error_action = warn_on_ws_error;
+   return 0;
+   }
+   if (!strcmp(option, "nowarn")) {
+   state->ws_error_action = nowarn_ws_error;
+   return 0;
+   }
+   if (!strcmp(option, "error")) {
+   state->ws_error_action = die_on_ws_error;
+   return 0;
+   }
+   if (!strcmp(option, "error-all")) {
+   state->ws_error_action = die_on_ws_error;
+   state->squelch_whitespace_errors = 0;
+   return 0;
+   }
+   if (!strcmp(option, "strip") || !strcmp(option, "fix")) {
+   state->ws_error_action = correct_ws_error;
+   return 0;
+   }
+   return error(_("unrecognized whitespace option '%s'"), option);
+}
+
+int parse_ignorewhitespace_option(struct apply_state *state,
+ const char *option)
+{
+   if (!option || !strcmp(option, "no") ||
+   !strcmp(option, "false") || !strcmp(option, "never") ||
+   !strcmp(option, "none")) {
+   state->ws_ignore_action = ignore_ws_none;
+   return 0;
+   }
+   if (!strcmp(option, "change")) {
+   state->ws_ignore_action = ignore_ws_change;
+   return 0;
+   }
+   return error(_("unrecognized whitespace ignore option '%s'"), option);
+}
+
+void init_apply_state(struct apply_state *state,
+ const char *prefix,
+ struct lock_file *lock_file)
+{
+   memset(state, 0, sizeof(*state));
+   state->prefix = prefix;
+   state->prefix_length = state->prefix ? strlen(state->prefix) : 0;
+   state->lock_file = lock_file ? lock_file : xcalloc(1, 
sizeof(*lock_file));
+   state->newfd = -1;
+   state->apply = 1;
+   state->line_termination = '\n';
+   state->p_value = 1;
+   state->p_context = UINT_MAX;
+   state->squelch_whitespace_errors = 5;
+   state->ws_error_action = warn_on_ws_error;
+   state->ws_ignore_action = ignore_ws_none;
+   state->linenr = 1;
+   strbuf_init(>root, 0);
+
+   git_apply_config();
+   if (apply_default_whitespace && parse_whitespace_option(state, 
apply_default_whitespace))
+   exit(1);
+   if (apply_default_ignorewhitespace && 
parse_ignorewhitespace_option(state, apply_default_ignorewhitespace))
+   exit(1);
+}
+
diff --git a/apply.h b/apply.h
index aa11ea6..0f77f4d 100644
--- a/apply.h
+++ b/apply.h
@@ -112,4 +112,13 @@ struct apply_state {
enum ws_ignore ws_ignore_action;
 };
 
+extern int parse_whitespace_option(struct apply_state *state,
+  const char *option);
+extern int parse_ignorewhitespace_option(struct apply_state *state,
+const char *option);
+
+extern void init_apply_state(struct apply_state *state,
+const char *prefix,
+struct lock_file *lock_file);
+
 #endif
diff --git a/builtin/apply.c b/builtin/apply.c
index a7e..805c707 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -27

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

2016-05-11 Thread Christian Couder
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 <pclo...@gmail.com>
Helped-by: Johannes Schindelin <johannes.schinde...@gmx.de>
Helped-by: Eric Sunshine <sunsh...@sunshineco.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 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, "", 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, "", 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(_index, state->lock_file, 
COMMIT_LOCK))
-   die(_("Unable to write new index file"));
+   res = write_locked_index(_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(, force_apply))
exit(1);
 
-   return apply_all_patches(, argc, argv, options);
+   return !!apply_all_patches(, argc, argv, options);
 }
-- 
2.8.2.490.g3dabe57

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


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

2016-05-11 Thread Christian Couder
To libify `git apply` functionality we have to signal errors to the
caller instead of exit()ing.

To do that in a compatible manner with the rest of the error handling
in "builtin/apply.c", create_file() should just return what
add_conflicted_stages_file() and add_index_file() are returning
instead of calling exit().

Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 25 +
 1 file changed, 13 insertions(+), 12 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 0e20467..2b562db 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4259,7 +4259,7 @@ static int add_conflicted_stages_file(struct apply_state 
*state,
return 0;
 }
 
-static void create_file(struct apply_state *state, struct patch *patch)
+static int create_file(struct apply_state *state, struct patch *patch)
 {
char *path = patch->new_name;
unsigned mode = patch->new_mode;
@@ -4270,13 +4270,10 @@ static void create_file(struct apply_state *state, 
struct patch *patch)
mode = S_IFREG | 0644;
create_one_file(state, path, mode, buf, size);
 
-   if (patch->conflicted_threeway) {
-   if (add_conflicted_stages_file(state, patch))
-   exit(1);
-   } else {
-   if (add_index_file(state, path, mode, buf, size))
-   exit(1);
-   }
+   if (patch->conflicted_threeway)
+   return add_conflicted_stages_file(state, patch);
+   else
+   return add_index_file(state, path, mode, buf, size);
 }
 
 /* phase zero is to remove, phase one is to create */
@@ -4292,8 +4289,10 @@ static void write_out_one_result(struct apply_state 
*state,
return;
}
if (patch->is_new > 0 || patch->is_copy) {
-   if (phase == 1)
-   create_file(state, patch);
+   if (phase == 1) {
+   if (create_file(state, patch))
+   exit(1);
+   }
return;
}
/*
@@ -4304,8 +4303,10 @@ static void write_out_one_result(struct apply_state 
*state,
if (remove_file(state, patch, patch->is_rename))
exit(1);
}
-   if (phase == 1)
-   create_file(state, patch);
+   if (phase == 1) {
+   if (create_file(state, patch))
+   exit(1);
+   }
 }
 
 static int write_out_one_reject(struct apply_state *state, struct patch *patch)
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 57/94] builtin/apply: make parse_whitespace_option() return -1 instead of die()ing

2016-05-11 Thread Christian Couder
To libify `git apply` functionality we have to signal errors to the
caller instead of die()ing.

To do that in a compatible manner with the rest of the error handling
in builtin/apply.c, parse_whitespace_option() should return -1 using
error() instead of calling die().

Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 23 ---
 1 file changed, 12 insertions(+), 11 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index e42c8fe..657e986 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -27,34 +27,34 @@ static const char * const apply_usage[] = {
NULL
 };
 
-static void parse_whitespace_option(struct apply_state *state, const char 
*option)
+static int parse_whitespace_option(struct apply_state *state, const char 
*option)
 {
if (!option) {
state->ws_error_action = warn_on_ws_error;
-   return;
+   return 0;
}
if (!strcmp(option, "warn")) {
state->ws_error_action = warn_on_ws_error;
-   return;
+   return 0;
}
if (!strcmp(option, "nowarn")) {
state->ws_error_action = nowarn_ws_error;
-   return;
+   return 0;
}
if (!strcmp(option, "error")) {
state->ws_error_action = die_on_ws_error;
-   return;
+   return 0;
}
if (!strcmp(option, "error-all")) {
state->ws_error_action = die_on_ws_error;
state->squelch_whitespace_errors = 0;
-   return;
+   return 0;
}
if (!strcmp(option, "strip") || !strcmp(option, "fix")) {
state->ws_error_action = correct_ws_error;
-   return;
+   return 0;
}
-   die(_("unrecognized whitespace option '%s'"), option);
+   return error(_("unrecognized whitespace option '%s'"), option);
 }
 
 static void parse_ignorewhitespace_option(struct apply_state *state,
@@ -4578,7 +4578,8 @@ static int option_parse_whitespace(const struct option 
*opt,
 {
struct apply_state *state = opt->value;
state->whitespace_option = arg;
-   parse_whitespace_option(state, arg);
+   if (parse_whitespace_option(state, arg))
+   exit(1);
return 0;
 }
 
@@ -4612,8 +4613,8 @@ static void init_apply_state(struct apply_state *state,
strbuf_init(>root, 0);
 
git_apply_config();
-   if (apply_default_whitespace)
-   parse_whitespace_option(state, apply_default_whitespace);
+   if (apply_default_whitespace && parse_whitespace_option(state, 
apply_default_whitespace))
+   exit(1);
if (apply_default_ignorewhitespace)
parse_ignorewhitespace_option(state, 
apply_default_ignorewhitespace);
 }
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 58/94] builtin/apply: make parse_ignorewhitespace_option() return -1 instead of die()ing

2016-05-11 Thread Christian Couder
To libify `git apply` functionality we have to signal errors to the
caller instead of die()ing.

To do that in a compatible manner with the rest of the error handling
in "builtin/apply.c", parse_ignorewhitespace_option() should return
-1 using error() instead of calling die().

Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 14 +++---
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 657e986..a7e 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -57,20 +57,20 @@ static int parse_whitespace_option(struct apply_state 
*state, const char *option
return error(_("unrecognized whitespace option '%s'"), option);
 }
 
-static void parse_ignorewhitespace_option(struct apply_state *state,
- const char *option)
+static int parse_ignorewhitespace_option(struct apply_state *state,
+const char *option)
 {
if (!option || !strcmp(option, "no") ||
!strcmp(option, "false") || !strcmp(option, "never") ||
!strcmp(option, "none")) {
state->ws_ignore_action = ignore_ws_none;
-   return;
+   return 0;
}
if (!strcmp(option, "change")) {
state->ws_ignore_action = ignore_ws_change;
-   return;
+   return 0;
}
-   die(_("unrecognized whitespace ignore option '%s'"), option);
+   return error(_("unrecognized whitespace ignore option '%s'"), option);
 }
 
 static void set_default_whitespace_mode(struct apply_state *state)
@@ -4615,8 +4615,8 @@ static void init_apply_state(struct apply_state *state,
git_apply_config();
if (apply_default_whitespace && parse_whitespace_option(state, 
apply_default_whitespace))
exit(1);
-   if (apply_default_ignorewhitespace)
-   parse_ignorewhitespace_option(state, 
apply_default_ignorewhitespace);
+   if (apply_default_ignorewhitespace && 
parse_ignorewhitespace_option(state, apply_default_ignorewhitespace))
+   exit(1);
 }
 
 static void check_apply_state(struct apply_state *state, int force_apply)
-- 
2.8.2.490.g3dabe57

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


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

2016-05-11 Thread Christian Couder
To libify `git apply` functionality we have to signal errors to the
caller instead of die()ing.

To do that in a compatible manner with the rest of the error handling
in "builtin/apply.c", remove_file() should return -1 using error()
instead of calling die().

Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 17 +++--
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index a2cc099..52f36c2 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4075,17 +4075,18 @@ static void patch_stats(struct apply_state *state, 
struct patch *patch)
}
 }
 
-static void remove_file(struct apply_state *state, struct patch *patch, int 
rmdir_empty)
+static int remove_file(struct apply_state *state, struct patch *patch, int 
rmdir_empty)
 {
if (state->update_index) {
if (remove_file_from_cache(patch->old_name) < 0)
-   die(_("unable to remove %s from index"), 
patch->old_name);
+   return error(_("unable to remove %s from index"), 
patch->old_name);
}
if (!state->cached) {
if (!remove_or_warn(patch->old_mode, patch->old_name) && 
rmdir_empty) {
remove_path(patch->old_name);
}
}
+   return 0;
 }
 
 static void add_index_file(struct apply_state *state,
@@ -4264,8 +4265,10 @@ static void write_out_one_result(struct apply_state 
*state,
 int phase)
 {
if (patch->is_delete > 0) {
-   if (phase == 0)
-   remove_file(state, patch, 1);
+   if (phase == 0) {
+   if (remove_file(state, patch, 1))
+   exit(1);
+   }
return;
}
if (patch->is_new > 0 || patch->is_copy) {
@@ -4277,8 +4280,10 @@ static void write_out_one_result(struct apply_state 
*state,
 * Rename or modification boils down to the same
 * thing: remove the old, write the new
 */
-   if (phase == 0)
-   remove_file(state, patch, patch->is_rename);
+   if (phase == 0) {
+   if (remove_file(state, patch, patch->is_rename))
+   exit(1);
+   }
if (phase == 1)
create_file(state, patch);
 }
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 49/94] builtin/apply: move 'lock_file' global into 'struct apply_state'

2016-05-11 Thread Christian Couder
We cannot have a 'struct lock_file' allocated on the stack, as lockfile.c
keeps a linked list of all created lock_file structures. So let's make the
'lock_file' variable a pointer to a 'struct lock_file'

As the same instance of this struct can be reused, let's add an argument
to init_apply_state(), so that the caller can supply the same instance to
different calls. And let's alloc an instance in init_apply_state(), if the
caller doesn't want to supply one.

Helped-by: Eric Sunshine <sunsh...@sunshineco.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 19 +--
 1 file changed, 13 insertions(+), 6 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 2aea8ba..cad2c56 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -52,6 +52,12 @@ struct apply_state {
const char *prefix;
int prefix_length;
 
+   /*
+* Since lockfile.c keeps a linked list of all created
+* lock_file structures, it isn't safe to free(lock_file).
+*/
+   struct lock_file *lock_file;
+
int apply;
int allow_overlap;
int apply_in_reverse;
@@ -4504,8 +4510,6 @@ static int write_out_results(struct apply_state *state, 
struct patch *list)
return errs;
 }
 
-static struct lock_file lock_file;
-
 #define INACCURATE_EOF (1<<0)
 #define RECOUNT(1<<1)
 
@@ -4558,7 +4562,7 @@ static int apply_patch(struct apply_state *state,
 
state->update_index = state->check_index && state->apply;
if (state->update_index && newfd < 0)
-   newfd = hold_locked_index(_file, 1);
+   newfd = hold_locked_index(state->lock_file, 1);
 
if (state->check_index) {
if (read_cache() < 0)
@@ -4659,11 +4663,14 @@ static int option_parse_directory(const struct option 
*opt,
return 0;
 }
 
-static void init_apply_state(struct apply_state *state, const char *prefix)
+static void init_apply_state(struct apply_state *state,
+const char *prefix,
+struct lock_file *lock_file)
 {
memset(state, 0, sizeof(*state));
state->prefix = prefix;
state->prefix_length = state->prefix ? strlen(state->prefix) : 0;
+   state->lock_file = lock_file ? lock_file : xcalloc(1, 
sizeof(*lock_file));
state->apply = 1;
state->line_termination = '\n';
state->p_value = 1;
@@ -4771,7 +4778,7 @@ static int apply_all_patches(struct apply_state *state,
}
 
if (state->update_index) {
-   if (write_locked_index(_index, _file, COMMIT_LOCK))
+   if (write_locked_index(_index, state->lock_file, 
COMMIT_LOCK))
die(_("Unable to write new index file"));
}
 
@@ -4853,7 +4860,7 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix)
OPT_END()
};
 
-   init_apply_state(, prefix);
+   init_apply_state(, prefix, NULL);
 
argc = parse_options(argc, argv, state.prefix, builtin_apply_options,
apply_usage, 0);
-- 
2.8.2.490.g3dabe57

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


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

2016-05-11 Thread Christian Couder
To libify `git apply` functionality we have to signal errors to the
caller instead of die()ing.

To do that in a compatible manner with the rest of the error handling
in "builtin/apply.c", parse_traditional_patch() should return -1 using
error() instead of calling die().

Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 15 +--
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index dd212c9..8e82eea 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -755,10 +755,10 @@ static int has_epoch_timestamp(const char *nameline)
  * files, we can happily check the index for a match, but for creating a
  * new file we should try to match whatever "patch" does. I have no idea.
  */
-static void parse_traditional_patch(struct apply_state *state,
-   const char *first,
-   const char *second,
-   struct patch *patch)
+static int parse_traditional_patch(struct apply_state *state,
+  const char *first,
+  const char *second,
+  struct patch *patch)
 {
char *name;
 
@@ -803,7 +803,9 @@ static void parse_traditional_patch(struct apply_state 
*state,
}
}
if (!name)
-   die(_("unable to find filename in patch at line %d"), 
state->linenr);
+   return error(_("unable to find filename in patch at line %d"), 
state->linenr);
+
+   return 0;
 }
 
 static int gitdiff_hdrend(struct apply_state *state,
@@ -1462,7 +1464,8 @@ static int find_header(struct apply_state *state,
continue;
 
/* Ok, we'll consider it a patch */
-   parse_traditional_patch(state, line, line+len, patch);
+   if (parse_traditional_patch(state, line, line+len, patch))
+   return -1;
*hdrsize = len + nextlen;
state->linenr += 2;
return offset;
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 61/94] builtin/apply: make check_apply_state() return -1 instead of die()ing

2016-05-11 Thread Christian Couder
To libify `git apply` functionality we have to signal errors to the
caller instead of die()ing.

To do that in a compatible manner with the rest of the error handling
in "builtin/apply.c", check_apply_state() should return -1 using
error() instead of calling die().

Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 16 +---
 1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index b31f9eb..2b3d10b 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4540,17 +4540,17 @@ static int option_parse_directory(const struct option 
*opt,
return 0;
 }
 
-static void check_apply_state(struct apply_state *state, int force_apply)
+static int check_apply_state(struct apply_state *state, int force_apply)
 {
int is_not_gitdir = !startup_info->have_repository;
 
if (state->apply_with_reject && state->threeway)
-   die("--reject and --3way cannot be used together.");
+   return error("--reject and --3way cannot be used together.");
if (state->cached && state->threeway)
-   die("--cached and --3way cannot be used together.");
+   return error("--cached and --3way cannot be used together.");
if (state->threeway) {
if (is_not_gitdir)
-   die(_("--3way outside a repository"));
+   return error(_("--3way outside a repository"));
state->check_index = 1;
}
if (state->apply_with_reject)
@@ -4558,14 +4558,15 @@ static void check_apply_state(struct apply_state 
*state, int force_apply)
if (!force_apply && (state->diffstat || state->numstat || 
state->summary || state->check || state->fake_ancestor))
state->apply = 0;
if (state->check_index && is_not_gitdir)
-   die(_("--index outside a repository"));
+   return error(_("--index outside a repository"));
if (state->cached) {
if (is_not_gitdir)
-   die(_("--cached outside a repository"));
+   return error(_("--cached outside a repository"));
state->check_index = 1;
}
if (state->check_index)
state->unsafe_paths = 0;
+   return 0;
 }
 
 static int apply_all_patches(struct apply_state *state,
@@ -4730,7 +4731,8 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix)
argc = parse_options(argc, argv, state.prefix, builtin_apply_options,
apply_usage, 0);
 
-   check_apply_state(, force_apply);
+   if (check_apply_state(, force_apply))
+   exit(1);
 
return apply_all_patches(, argc, argv, options);
 }
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 56/94] apply: move 'struct apply_state' to apply.h

2016-05-11 Thread Christian Couder
To libify `git apply` functionality we must make 'struct apply_state'
usable outside "builtin/apply.c".

Let's do that by creating a new "apply.h" and moving
'struct apply_state' there.

Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 apply.h | 115 
 builtin/apply.c | 112 +-
 2 files changed, 116 insertions(+), 111 deletions(-)
 create mode 100644 apply.h

diff --git a/apply.h b/apply.h
new file mode 100644
index 000..aa11ea6
--- /dev/null
+++ b/apply.h
@@ -0,0 +1,115 @@
+#ifndef APPLY_H
+#define APPLY_H
+
+enum ws_error_action {
+   nowarn_ws_error,
+   warn_on_ws_error,
+   die_on_ws_error,
+   correct_ws_error
+};
+
+
+enum ws_ignore {
+   ignore_ws_none,
+   ignore_ws_change
+};
+
+/*
+ * We need to keep track of how symlinks in the preimage are
+ * manipulated by the patches.  A patch to add a/b/c where a/b
+ * is a symlink should not be allowed to affect the directory
+ * the symlink points at, but if the same patch removes a/b,
+ * it is perfectly fine, as the patch removes a/b to make room
+ * to create a directory a/b so that a/b/c can be created.
+ *
+ * See also "struct string_list symlink_changes" in "struct
+ * apply_state".
+ */
+#define SYMLINK_GOES_AWAY 01
+#define SYMLINK_IN_RESULT 02
+
+struct apply_state {
+   const char *prefix;
+   int prefix_length;
+
+   /*
+* Since lockfile.c keeps a linked list of all created
+* lock_file structures, it isn't safe to free(lock_file).
+*/
+   struct lock_file *lock_file;
+   int newfd;
+
+   int apply;
+   int allow_overlap;
+   int apply_in_reverse;
+   int apply_with_reject;
+   int apply_verbosely;
+
+   /* --cached updates only the cache without ever touching the working 
tree. */
+   int cached;
+
+   /* --stat does just a diffstat, and doesn't actually apply */
+   int diffstat;
+
+   /* --numstat does numeric diffstat, and doesn't actually apply */
+   int numstat;
+
+   int summary;
+   int threeway;
+   int no_add;
+   const char *fake_ancestor;
+   const char *patch_input_file;
+   struct string_list limit_by_name;
+   int has_include;
+   struct strbuf root;
+   struct string_list symlink_changes;
+
+   /*
+*  --check turns on checking that the working tree matches the
+*files that are being modified, but doesn't apply the patch
+*/
+   int check;
+
+   /* --index updates the cache as well. */
+   int check_index;
+
+   int unidiff_zero;
+   int update_index;
+   int unsafe_paths;
+   int line_termination;
+
+   /*
+* For "diff-stat" like behaviour, we keep track of the biggest change
+* we've seen, and the longest filename. That allows us to do simple
+* scaling.
+*/
+   int max_change;
+   int max_len;
+
+   /*
+* Various "current state", notably line numbers and what
+* file (and how) we're patching right now.. The "is_"
+* things are flags, where -1 means "don't know yet".
+*/
+   int linenr;
+
+   /*
+* Records filenames that have been touched, in order to handle
+* the case where more than one patches touch the same file.
+*/
+   struct string_list fn_table;
+
+   int p_value;
+   int p_value_known;
+   unsigned int p_context;
+
+   const char *whitespace_option;
+   int whitespace_error;
+   int squelch_whitespace_errors;
+   int applied_after_fixing_ws;
+
+   enum ws_error_action ws_error_action;
+   enum ws_ignore ws_ignore_action;
+};
+
+#endif
diff --git a/builtin/apply.c b/builtin/apply.c
index 58bcfeb..e42c8fe 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -20,117 +20,7 @@
 #include "xdiff-interface.h"
 #include "ll-merge.h"
 #include "rerere.h"
-
-enum ws_error_action {
-   nowarn_ws_error,
-   warn_on_ws_error,
-   die_on_ws_error,
-   correct_ws_error
-};
-
-
-enum ws_ignore {
-   ignore_ws_none,
-   ignore_ws_change
-};
-
-/*
- * We need to keep track of how symlinks in the preimage are
- * manipulated by the patches.  A patch to add a/b/c where a/b
- * is a symlink should not be allowed to affect the directory
- * the symlink points at, but if the same patch removes a/b,
- * it is perfectly fine, as the patch removes a/b to make room
- * to create a directory a/b so that a/b/c can be created.
- *
- * See also "struct string_list symlink_changes" in "struct
- * apply_state".
- */
-#define SYMLINK_GOES_AWAY 01
-#define SYMLINK_IN_RESULT 02
-
-struct apply_state {
-   const char *prefix;
-   int prefix_length;
-
-   /*
-* Since lockfile.c ke

[PATCH v2 31/94] builtin/apply: move 'has_include' global into 'struct apply_state'

2016-05-11 Thread Christian Couder
To libify the apply functionality the 'has_include' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 14bbcc2..f2ee8bf 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -46,6 +46,7 @@ struct apply_state {
const char *fake_ancestor;
const char *patch_input_file;
struct string_list limit_by_name;
+   int has_include;
 
/*
 *  --check turns on checking that the working tree matches the
@@ -1968,7 +1969,6 @@ static void prefix_patch(struct apply_state *state, 
struct patch *p)
  * include/exclude
  */
 
-static int has_include;
 static void add_name_limit(struct apply_state *state,
   const char *name,
   int exclude)
@@ -2004,7 +2004,7 @@ static int use_patch(struct apply_state *state, struct 
patch *p)
 * not used.  Otherwise, we saw bunch of exclude rules (or none)
 * and such a path is used.
 */
-   return !has_include;
+   return !state->has_include;
 }
 
 
@@ -4541,7 +4541,7 @@ static int option_parse_include(const struct option *opt,
 {
struct apply_state *state = opt->value;
add_name_limit(state, arg, 0);
-   has_include = 1;
+   state->has_include = 1;
return 0;
 }
 
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 26/94] builtin/apply: move 'fake_ancestor' global into 'struct apply_state'

2016-05-11 Thread Christian Couder
To libify the apply functionality the 'fake_ancestor' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

By the way remove a comment about '--index-info' that was renamed
'--build-fake-ancestor' in commit 26b28007689d27a921ea90e5a29fc8eb74b0d297
(apply: get rid of --index-info in favor of --build-fake-ancestor,
Sep 17 2007).

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 13 +
 1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index d699d1e..6f9a090 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -42,6 +42,7 @@ struct apply_state {
int summary;
int threeway;
int no_add;
+   const char *fake_ancestor;
 
/*
 *  --check turns on checking that the working tree matches the
@@ -58,15 +59,11 @@ struct apply_state {
int line_termination;
 };
 
-/*
- *  --index-info shows the old and new index info for paths if available.
- */
 static int newfd = -1;
 
 static int state_p_value = 1;
 static int p_value_known;
 static int apply = 1;
-static const char *fake_ancestor;
 static unsigned int p_context = UINT_MAX;
 static const char * const apply_usage[] = {
N_("git apply [] [...]"),
@@ -4494,8 +4491,8 @@ static int apply_patch(struct apply_state *state,
return 1;
}
 
-   if (fake_ancestor)
-   build_fake_ancestor(list, fake_ancestor);
+   if (state->fake_ancestor)
+   build_fake_ancestor(list, state->fake_ancestor);
 
if (state->diffstat)
stat_patch_list(list);
@@ -4629,7 +4626,7 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix_)
N_("also apply the patch (use with 
--stat/--summary/--check)")),
OPT_BOOL('3', "3way", ,
 N_( "attempt three-way merge if a patch does not 
apply")),
-   OPT_FILENAME(0, "build-fake-ancestor", _ancestor,
+   OPT_FILENAME(0, "build-fake-ancestor", _ancestor,
N_("build a temporary index based on embedded index 
information")),
/* Think twice before adding "--nul" synonym to this */
OPT_SET_INT('z', NULL, _termination,
@@ -4682,7 +4679,7 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix_)
}
if (state.apply_with_reject)
apply = state.apply_verbosely = 1;
-   if (!force_apply && (state.diffstat || state.numstat || state.summary 
|| state.check || fake_ancestor))
+   if (!force_apply && (state.diffstat || state.numstat || state.summary 
|| state.check || state.fake_ancestor))
apply = 0;
if (state.check_index && is_not_gitdir)
die(_("--index outside a repository"));
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 44/94] builtin/apply: move 'fn_table' global into 'struct apply_state'

2016-05-11 Thread Christian Couder
To libify the apply functionality the 'fn_table' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 45 +++--
 1 file changed, 23 insertions(+), 22 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 5c003a1..506e9ec 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -91,6 +91,12 @@ struct apply_state {
 */
int linenr;
 
+   /*
+* Records filenames that have been touched, in order to handle
+* the case where more than one patches touch the same file.
+*/
+   struct string_list fn_table;
+
int p_value;
int p_value_known;
unsigned int p_context;
@@ -282,13 +288,6 @@ struct image {
struct line *line;
 };
 
-/*
- * Records filenames that have been touched, in order to handle
- * the case where more than one patches touch the same file.
- */
-
-static struct string_list fn_table;
-
 static uint32_t hash_line(const char *cp, size_t len)
 {
size_t i;
@@ -3218,14 +3217,14 @@ static int read_file_or_gitlink(const struct 
cache_entry *ce, struct strbuf *buf
return read_blob_object(buf, ce->sha1, ce->ce_mode);
 }
 
-static struct patch *in_fn_table(const char *name)
+static struct patch *in_fn_table(struct apply_state *state, const char *name)
 {
struct string_list_item *item;
 
if (name == NULL)
return NULL;
 
-   item = string_list_lookup(_table, name);
+   item = string_list_lookup(>fn_table, name);
if (item != NULL)
return (struct patch *)item->util;
 
@@ -3257,7 +3256,7 @@ static int was_deleted(struct patch *patch)
return patch == PATH_WAS_DELETED;
 }
 
-static void add_to_fn_table(struct patch *patch)
+static void add_to_fn_table(struct apply_state *state, struct patch *patch)
 {
struct string_list_item *item;
 
@@ -3267,7 +3266,7 @@ static void add_to_fn_table(struct patch *patch)
 * file creations and copies
 */
if (patch->new_name != NULL) {
-   item = string_list_insert(_table, patch->new_name);
+   item = string_list_insert(>fn_table, patch->new_name);
item->util = patch;
}
 
@@ -3276,12 +3275,12 @@ static void add_to_fn_table(struct patch *patch)
 * later chunks shouldn't patch old names
 */
if ((patch->new_name == NULL) || (patch->is_rename)) {
-   item = string_list_insert(_table, patch->old_name);
+   item = string_list_insert(>fn_table, patch->old_name);
item->util = PATH_WAS_DELETED;
}
 }
 
-static void prepare_fn_table(struct patch *patch)
+static void prepare_fn_table(struct apply_state *state, struct patch *patch)
 {
/*
 * store information about incoming file deletion
@@ -3289,7 +3288,7 @@ static void prepare_fn_table(struct patch *patch)
while (patch) {
if ((patch->new_name == NULL) || (patch->is_rename)) {
struct string_list_item *item;
-   item = string_list_insert(_table, patch->old_name);
+   item = string_list_insert(>fn_table, 
patch->old_name);
item->util = PATH_TO_BE_DELETED;
}
patch = patch->next;
@@ -3310,7 +3309,9 @@ static int checkout_target(struct index_state *istate,
return 0;
 }
 
-static struct patch *previous_patch(struct patch *patch, int *gone)
+static struct patch *previous_patch(struct apply_state *state,
+   struct patch *patch,
+   int *gone)
 {
struct patch *previous;
 
@@ -3318,7 +3319,7 @@ static struct patch *previous_patch(struct patch *patch, 
int *gone)
if (patch->is_copy || patch->is_rename)
return NULL; /* "git" patches do not depend on the order */
 
-   previous = in_fn_table(patch->old_name);
+   previous = in_fn_table(state, patch->old_name);
if (!previous)
return NULL;
 
@@ -3387,7 +3388,7 @@ static int load_preimage(struct apply_state *state,
struct patch *previous;
int status;
 
-   previous = previous_patch(patch, );
+   previous = previous_patch(state, patch, );
if (status)
return error(_("path %s has been renamed/deleted"),
 patch->old_name);
@@ -3583,7 +3584,7 @@ static int apply_data(struct apply_state *state, struct 
patch *patch,
}
patch->result = image.buf;
patch->resultsize = image.len;
-   add_to_fn_table(patch);
+   add_to_fn_table(state, patch);
free(image.line_allocated);
 
  

[PATCH v2 48/94] builtin/apply: rename 'prefix_' parameter to 'prefix'

2016-05-11 Thread Christian Couder
This is just a small cleanup.

Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 67c64a5..2aea8ba 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4778,7 +4778,7 @@ static int apply_all_patches(struct apply_state *state,
return !!errs;
 }
 
-int cmd_apply(int argc, const char **argv, const char *prefix_)
+int cmd_apply(int argc, const char **argv, const char *prefix)
 {
int force_apply = 0;
int options = 0;
@@ -4853,7 +4853,7 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix_)
OPT_END()
};
 
-   init_apply_state(, prefix_);
+   init_apply_state(, prefix);
 
argc = parse_options(argc, argv, state.prefix, builtin_apply_options,
apply_usage, 0);
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 41/94] builtin/apply: move 'ws_ignore_action' into 'struct apply_state'

2016-05-11 Thread Christian Couder
To libify the apply functionality the 'ws_ignore_action' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 37 -
 1 file changed, 20 insertions(+), 17 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 10d45c7..f8c3677 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -28,6 +28,12 @@ enum ws_error_action {
correct_ws_error
 };
 
+
+enum ws_ignore {
+   ignore_ws_none,
+   ignore_ws_change
+};
+
 struct apply_state {
const char *prefix;
int prefix_length;
@@ -80,6 +86,7 @@ struct apply_state {
int applied_after_fixing_ws;
 
enum ws_error_action ws_error_action;
+   enum ws_ignore ws_ignore_action;
 };
 
 static int newfd = -1;
@@ -89,13 +96,6 @@ static const char * const apply_usage[] = {
NULL
 };
 
-
-static enum ws_ignore {
-   ignore_ws_none,
-   ignore_ws_change
-} ws_ignore_action = ignore_ws_none;
-
-
 static void parse_whitespace_option(struct apply_state *state, const char 
*option)
 {
if (!option) {
@@ -126,16 +126,17 @@ static void parse_whitespace_option(struct apply_state 
*state, const char *optio
die(_("unrecognized whitespace option '%s'"), option);
 }
 
-static void parse_ignorewhitespace_option(const char *option)
+static void parse_ignorewhitespace_option(struct apply_state *state,
+ const char *option)
 {
if (!option || !strcmp(option, "no") ||
!strcmp(option, "false") || !strcmp(option, "never") ||
!strcmp(option, "none")) {
-   ws_ignore_action = ignore_ws_none;
+   state->ws_ignore_action = ignore_ws_none;
return;
}
if (!strcmp(option, "change")) {
-   ws_ignore_action = ignore_ws_change;
+   state->ws_ignore_action = ignore_ws_change;
return;
}
die(_("unrecognized whitespace ignore option '%s'"), option);
@@ -2495,7 +2496,7 @@ static int match_fragment(struct apply_state *state,
 * fuzzy matching. We collect all the line length information because
 * we need it to adjust whitespace if we match.
 */
-   if (ws_ignore_action == ignore_ws_change)
+   if (state->ws_ignore_action == ignore_ws_change)
return line_by_line_fuzzy_match(img, preimage, postimage,
try, try_lno, preimage_limit);
 
@@ -4618,12 +4619,13 @@ static int option_parse_p(const struct option *opt,
 }
 
 static int option_parse_space_change(const struct option *opt,
- const char *arg, int unset)
+const char *arg, int unset)
 {
+   struct apply_state *state = opt->value;
if (unset)
-   ws_ignore_action = ignore_ws_none;
+   state->ws_ignore_action = ignore_ws_none;
else
-   ws_ignore_action = ignore_ws_change;
+   state->ws_ignore_action = ignore_ws_change;
return 0;
 }
 
@@ -4657,13 +4659,14 @@ static void init_apply_state(struct apply_state *state, 
const char *prefix)
state->p_context = UINT_MAX;
state->squelch_whitespace_errors = 5;
state->ws_error_action = warn_on_ws_error;
+   state->ws_ignore_action = ignore_ws_none;
strbuf_init(>root, 0);
 
git_apply_config();
if (apply_default_whitespace)
parse_whitespace_option(state, apply_default_whitespace);
if (apply_default_ignorewhitespace)
-   parse_ignorewhitespace_option(apply_default_ignorewhitespace);
+   parse_ignorewhitespace_option(state, 
apply_default_ignorewhitespace);
 }
 
 int cmd_apply(int argc, const char **argv, const char *prefix_)
@@ -4718,10 +4721,10 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix_)
{ OPTION_CALLBACK, 0, "whitespace", , N_("action"),
N_("detect new or modified lines that have whitespace 
errors"),
0, option_parse_whitespace },
-   { OPTION_CALLBACK, 0, "ignore-space-change", NULL, NULL,
+   { OPTION_CALLBACK, 0, "ignore-space-change", , NULL,
N_("ignore changes in whitespace when finding context"),
PARSE_OPT_NOARG, option_parse_space_change },
-   { OPTION_CALLBACK, 0, "ignore-whitespace", NULL, NULL,
+   { OPTION_CALLBACK, 0, "ignore-whitespace", , NULL,
N_("ignore changes in whitespace when finding context"),
   

[PATCH v2 47/94] builtin/apply: move applying patches into apply_all_patches()

2016-05-11 Thread Christian Couder
To libify the apply functionality we should provide a function to
apply many patches. Let's move the code to do that into a new
apply_all_patches() function.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 128 ++--
 1 file changed, 69 insertions(+), 59 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index e5f76d8..67c64a5 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4709,13 +4709,79 @@ static void check_apply_state(struct apply_state 
*state, int force_apply)
state->unsafe_paths = 0;
 }
 
-int cmd_apply(int argc, const char **argv, const char *prefix_)
+static int apply_all_patches(struct apply_state *state,
+int argc,
+const char **argv,
+int options)
 {
int i;
int errs = 0;
+   int read_stdin = 1;
+
+   for (i = 0; i < argc; i++) {
+   const char *arg = argv[i];
+   int fd;
+
+   if (!strcmp(arg, "-")) {
+   errs |= apply_patch(state, 0, "", options);
+   read_stdin = 0;
+   continue;
+   } else if (0 < state->prefix_length)
+   arg = prefix_filename(state->prefix,
+ state->prefix_length,
+ arg);
+
+   fd = open(arg, O_RDONLY);
+   if (fd < 0)
+   die_errno(_("can't open patch '%s'"), arg);
+   read_stdin = 0;
+   set_default_whitespace_mode(state);
+   errs |= apply_patch(state, fd, arg, options);
+   close(fd);
+   }
+   set_default_whitespace_mode(state);
+   if (read_stdin)
+   errs |= apply_patch(state, 0, "", options);
+
+   if (state->whitespace_error) {
+   if (state->squelch_whitespace_errors &&
+   state->squelch_whitespace_errors < state->whitespace_error) 
{
+   int squelched =
+   state->whitespace_error - 
state->squelch_whitespace_errors;
+   warning(Q_("squelched %d whitespace error",
+  "squelched %d whitespace errors",
+  squelched),
+   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);
+   if (state->applied_after_fixing_ws && state->apply)
+   warning("%d line%s applied after"
+   " fixing whitespace errors.",
+   state->applied_after_fixing_ws,
+   state->applied_after_fixing_ws == 1 ? "" : "s");
+   else if (state->whitespace_error)
+   warning(Q_("%d line adds whitespace errors.",
+  "%d lines add whitespace errors.",
+  state->whitespace_error),
+   state->whitespace_error);
+   }
+
+   if (state->update_index) {
+   if (write_locked_index(_index, _file, COMMIT_LOCK))
+   die(_("Unable to write new index file"));
+   }
+
+   return !!errs;
+}
+
+int cmd_apply(int argc, const char **argv, const char *prefix_)
+{
int force_apply = 0;
int options = 0;
-   int read_stdin = 1;
struct apply_state state;
 
struct option builtin_apply_options[] = {
@@ -4794,61 +4860,5 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix_)
 
check_apply_state(, force_apply);
 
-   for (i = 0; i < argc; i++) {
-   const char *arg = argv[i];
-   int fd;
-
-   if (!strcmp(arg, "-")) {
-   errs |= apply_patch(, 0, "", options);
-   read_stdin = 0;
-   continue;
-   } else if (0 < state.prefix_length)
-   arg = prefix_filename(state.prefix,
- state.prefix_length,
- arg);
-
-   fd = open(arg, O_RDONLY);
-   if (fd < 0)
-   die_errno(_("can't open patch '%

[PATCH v2 15/94] builtin/apply: move 'apply_verbosely' global into 'struct apply_state'

2016-05-11 Thread Christian Couder
To libify the apply functionality the 'apply_verbosely' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 26 +-
 1 file changed, 13 insertions(+), 13 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 53cc280..97af6ea 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -27,6 +27,7 @@ struct apply_state {
 
int apply_in_reverse;
int apply_with_reject;
+   int apply_verbosely;
 
/*
 *  --check turns on checking that the working tree matches the
@@ -56,7 +57,6 @@ static int diffstat;
 static int numstat;
 static int summary;
 static int apply = 1;
-static int apply_verbosely;
 static int allow_overlap;
 static int no_add;
 static int threeway;
@@ -2810,7 +2810,7 @@ static int apply_one_fragment(struct apply_state *state,
/* Ignore it, we already handled it */
break;
default:
-   if (apply_verbosely)
+   if (state->apply_verbosely)
error(_("invalid start of line: '%c'"), first);
applied_pos = -1;
goto out;
@@ -2925,7 +2925,7 @@ static int apply_one_fragment(struct apply_state *state,
apply = 0;
}
 
-   if (apply_verbosely && applied_pos != pos) {
+   if (state->apply_verbosely && applied_pos != pos) {
int offset = applied_pos - pos;
if (state->apply_in_reverse)
offset = 0 - offset;
@@ -2947,7 +2947,7 @@ static int apply_one_fragment(struct apply_state *state,
   leading, trailing, applied_pos+1);
update_image(img, applied_pos, , );
} else {
-   if (apply_verbosely)
+   if (state->apply_verbosely)
error(_("while searching for:\n%.*s"),
  (int)(old - oldlines), oldlines);
}
@@ -3861,7 +3861,7 @@ static int check_patch_list(struct apply_state *state, 
struct patch *patch)
prepare_symlink_changes(patch);
prepare_fn_table(patch);
while (patch) {
-   if (apply_verbosely)
+   if (state->apply_verbosely)
say_patch_name(stderr,
   _("Checking patch %s..."), patch);
err |= check_patch(state, patch);
@@ -4292,7 +4292,7 @@ static void write_out_one_result(struct patch *patch, int 
phase)
create_file(patch);
 }
 
-static int write_out_one_reject(struct patch *patch)
+static int write_out_one_reject(struct apply_state *state, struct patch *patch)
 {
FILE *rej;
char namebuf[PATH_MAX];
@@ -4307,7 +4307,7 @@ static int write_out_one_reject(struct patch *patch)
}
 
if (!cnt) {
-   if (apply_verbosely)
+   if (state->apply_verbosely)
say_patch_name(stderr,
   _("Applied patch %s cleanly."), patch);
return 0;
@@ -4363,7 +4363,7 @@ static int write_out_one_reject(struct patch *patch)
return -1;
 }
 
-static int write_out_results(struct patch *list)
+static int write_out_results(struct apply_state *state, struct patch *list)
 {
int phase;
int errs = 0;
@@ -4378,7 +4378,7 @@ static int write_out_results(struct patch *list)
else {
write_out_one_result(l, phase);
if (phase == 1) {
-   if (write_out_one_reject(l))
+   if (write_out_one_reject(state, l))
errs = 1;
if (l->conflicted_threeway) {
string_list_append(, 
l->new_name);
@@ -4442,7 +4442,7 @@ static int apply_patch(struct apply_state *state,
listp = >next;
}
else {
-   if (apply_verbosely)
+   if (state->apply_verbosely)
say_patch_name(stderr, _("Skipped patch 
'%s'."), patch);
free_patch(patch);
skipped_patch++;
@@ -4470,7 +4470,7 @@ static int apply_patch(struct apply_state *state,
!state->apply_with_reject)
exit(1);
 
-   if (apply && write_out_results(list)) {
+   if (apply && write_out_results(state, list)) {
if (state->appl

[PATCH v2 25/94] builtin/apply: move 'line_termination' global into 'struct apply_state'

2016-05-11 Thread Christian Couder
To libify the apply functionality the 'line_termination' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 12 +++-
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 9209af4..d699d1e 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -55,6 +55,7 @@ struct apply_state {
int unidiff_zero;
int update_index;
int unsafe_paths;
+   int line_termination;
 };
 
 /*
@@ -66,7 +67,6 @@ static int state_p_value = 1;
 static int p_value_known;
 static int apply = 1;
 static const char *fake_ancestor;
-static int line_termination = '\n';
 static unsigned int p_context = UINT_MAX;
 static const char * const apply_usage[] = {
N_("git apply [] [...]"),
@@ -3987,7 +3987,8 @@ static void stat_patch_list(struct patch *patch)
print_stat_summary(stdout, files, adds, dels);
 }
 
-static void numstat_patch_list(struct patch *patch)
+static void numstat_patch_list(struct apply_state *state,
+  struct patch *patch)
 {
for ( ; patch; patch = patch->next) {
const char *name;
@@ -3996,7 +3997,7 @@ static void numstat_patch_list(struct patch *patch)
printf("-\t-\t");
else
printf("%d\t%d\t", patch->lines_added, 
patch->lines_deleted);
-   write_name_quoted(name, stdout, line_termination);
+   write_name_quoted(name, stdout, state->line_termination);
}
 }
 
@@ -4500,7 +4501,7 @@ static int apply_patch(struct apply_state *state,
stat_patch_list(list);
 
if (state->numstat)
-   numstat_patch_list(list);
+   numstat_patch_list(state, list);
 
if (state->summary)
summary_patch_list(list);
@@ -4575,6 +4576,7 @@ static void init_apply_state(struct apply_state *state, 
const char *prefix)
memset(state, 0, sizeof(*state));
state->prefix = prefix;
state->prefix_length = state->prefix ? strlen(state->prefix) : 0;
+   state->line_termination = '\n';
 
git_apply_config();
if (apply_default_whitespace)
@@ -4630,7 +4632,7 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix_)
OPT_FILENAME(0, "build-fake-ancestor", _ancestor,
N_("build a temporary index based on embedded index 
information")),
/* Think twice before adding "--nul" synonym to this */
-   OPT_SET_INT('z', NULL, _termination,
+   OPT_SET_INT('z', NULL, _termination,
N_("paths are separated with NUL character"), '\0'),
OPT_INTEGER('C', NULL, _context,
N_("ensure at least  lines of context 
match")),
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 17/94] builtin/apply: move 'allow_overlap' global into 'struct apply_state'

2016-05-11 Thread Christian Couder
To libify the apply functionality the 'allow_overlap' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 11 ++-
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 635a9ff..8791b28 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -25,6 +25,7 @@ struct apply_state {
const char *prefix;
int prefix_length;
 
+   int allow_overlap;
int apply_in_reverse;
int apply_with_reject;
int apply_verbosely;
@@ -57,7 +58,6 @@ static int diffstat;
 static int numstat;
 static int summary;
 static int apply = 1;
-static int allow_overlap;
 static int no_add;
 static int threeway;
 static int unsafe_paths;
@@ -2632,7 +2632,8 @@ static void remove_last_line(struct image *img)
  * apply at applied_pos (counts in line numbers) in "img".
  * Update "img" to remove "preimage" and replace it with "postimage".
  */
-static void update_image(struct image *img,
+static void update_image(struct apply_state *state,
+struct image *img,
 int applied_pos,
 struct image *preimage,
 struct image *postimage)
@@ -2697,7 +2698,7 @@ static void update_image(struct image *img,
memcpy(img->line + applied_pos,
   postimage->line,
   postimage->nr * sizeof(*img->line));
-   if (!allow_overlap)
+   if (!state->allow_overlap)
for (i = 0; i < postimage->nr; i++)
img->line[applied_pos + i].flag |= LINE_PATCHED;
img->nr = nr;
@@ -2945,7 +2946,7 @@ static int apply_one_fragment(struct apply_state *state,
fprintf_ln(stderr, _("Context reduced to (%ld/%ld)"
 " to apply fragment at %d"),
   leading, trailing, applied_pos+1);
-   update_image(img, applied_pos, , );
+   update_image(state, img, applied_pos, , );
} else {
if (state->apply_verbosely)
error(_("while searching for:\n%.*s"),
@@ -4640,7 +4641,7 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix_)
N_("don't expect at least one line of context")),
OPT_BOOL(0, "reject", _with_reject,
N_("leave the rejected hunks in corresponding *.rej 
files")),
-   OPT_BOOL(0, "allow-overlap", _overlap,
+   OPT_BOOL(0, "allow-overlap", _overlap,
N_("allow overlapping hunks")),
OPT__VERBOSE(_verbosely, N_("be verbose")),
OPT_BIT(0, "inaccurate-eof", ,
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 23/94] builtin/apply: move 'no_add' global into 'struct apply_state'

2016-05-11 Thread Christian Couder
To libify the apply functionality the 'no_add' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 3650922..d699cd9 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -41,6 +41,7 @@ struct apply_state {
 
int summary;
int threeway;
+   int no_add;
 
/*
 *  --check turns on checking that the working tree matches the
@@ -63,7 +64,6 @@ static int newfd = -1;
 static int state_p_value = 1;
 static int p_value_known;
 static int apply = 1;
-static int no_add;
 static int unsafe_paths;
 static const char *fake_ancestor;
 static int line_termination = '\n';
@@ -2792,7 +2792,7 @@ static int apply_one_fragment(struct apply_state *state,
/* Fall-through for ' ' */
case '+':
/* --no-add does not add new lines */
-   if (first == '+' && no_add)
+   if (first == '+' && state->no_add)
break;
 
start = newlines.len;
@@ -4605,7 +4605,7 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix_)
{ OPTION_CALLBACK, 'p', NULL, NULL, N_("num"),
N_("remove  leading slashes from traditional diff 
paths"),
0, option_parse_p },
-   OPT_BOOL(0, "no-add", _add,
+   OPT_BOOL(0, "no-add", _add,
N_("ignore additions made by the patch")),
OPT_BOOL(0, "stat", ,
N_("instead of applying the patch, output diffstat for 
the input")),
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 18/94] builtin/apply: move 'cached' global into 'struct apply_state'

2016-05-11 Thread Christian Couder
To libify the apply functionality the 'cached' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 37 +
 1 file changed, 21 insertions(+), 16 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 8791b28..09af5dc 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -30,6 +30,9 @@ struct apply_state {
int apply_with_reject;
int apply_verbosely;
 
+   /* --cached updates only the cache without ever touching the working 
tree. */
+   int cached;
+
/*
 *  --check turns on checking that the working tree matches the
 *files that are being modified, but doesn't apply the patch
@@ -47,13 +50,11 @@ struct apply_state {
  *  --stat does just a diffstat, and doesn't actually apply
  *  --numstat does numeric diffstat, and doesn't actually apply
  *  --index-info shows the old and new index info for paths if available.
- *  --cached updates only the cache without ever touching the working tree.
  */
 static int newfd = -1;
 
 static int state_p_value = 1;
 static int p_value_known;
-static int cached;
 static int diffstat;
 static int numstat;
 static int summary;
@@ -3269,7 +3270,7 @@ static int load_patch_target(struct apply_state *state,
 const char *name,
 unsigned expected_mode)
 {
-   if (cached || state->check_index) {
+   if (state->cached || state->check_index) {
if (read_file_or_gitlink(ce, buf))
return error(_("read of %s failed"), name);
} else if (name) {
@@ -3542,7 +3543,7 @@ static int check_preimage(struct apply_state *state,
return error(_("path %s has been renamed/deleted"), old_name);
if (previous) {
st_mode = previous->new_mode;
-   } else if (!cached) {
+   } else if (!state->cached) {
stat_ret = lstat(old_name, st);
if (stat_ret && errno != ENOENT)
return error(_("%s: %s"), old_name, strerror(errno));
@@ -3560,9 +3561,9 @@ static int check_preimage(struct apply_state *state,
if (checkout_target(_index, *ce, st))
return -1;
}
-   if (!cached && verify_index_match(*ce, st))
+   if (!state->cached && verify_index_match(*ce, st))
return error(_("%s: does not match index"), old_name);
-   if (cached)
+   if (state->cached)
st_mode = (*ce)->ce_mode;
} else if (stat_ret < 0) {
if (patch->is_new < 0)
@@ -3570,7 +3571,7 @@ static int check_preimage(struct apply_state *state,
return error(_("%s: %s"), old_name, strerror(errno));
}
 
-   if (!cached && !previous)
+   if (!state->cached && !previous)
st_mode = ce_mode_from_stat(*ce, st->st_mode);
 
if (patch->is_new < 0)
@@ -3608,7 +3609,7 @@ static int check_to_create(struct apply_state *state,
cache_name_pos(new_name, strlen(new_name)) >= 0 &&
!ok_if_exists)
return EXISTS_IN_INDEX;
-   if (cached)
+   if (state->cached)
return 0;
 
if (!lstat(new_name, )) {
@@ -4102,7 +4103,7 @@ static void remove_file(struct apply_state *state, struct 
patch *patch, int rmdi
if (remove_file_from_cache(patch->old_name) < 0)
die(_("unable to remove %s from index"), 
patch->old_name);
}
-   if (!cached) {
+   if (!state->cached) {
if (!remove_or_warn(patch->old_mode, patch->old_name) && 
rmdir_empty) {
remove_path(patch->old_name);
}
@@ -4135,7 +4136,7 @@ static void add_index_file(struct apply_state *state,
get_sha1_hex(s, ce->sha1))
die(_("corrupt patch for submodule %s"), path);
} else {
-   if (!cached) {
+   if (!state->cached) {
if (lstat(path, ) < 0)
die_errno(_("unable to stat newly created file 
'%s'"),
  path);
@@ -4187,9 +4188,13 @@ static int try_create_file(const char *path, unsigned 
int mode, const char *buf,
  * which is true 99% of the time anyway. If they don't,
  * we create them and try again.
  */
-static void create_one_file(char *path, unsigned mode, const char *buf, 
unsigned long size)
+static void create_one_file(struct apply_state *st

[PATCH v2 30/94] builtin/apply: move 'limit_by_name' global into 'struct apply_state'

2016-05-11 Thread Christian Couder
To libify the apply functionality the 'limit_by_name' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 22 +-
 1 file changed, 13 insertions(+), 9 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index e43da9c..14bbcc2 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -45,6 +45,7 @@ struct apply_state {
int no_add;
const char *fake_ancestor;
const char *patch_input_file;
+   struct string_list limit_by_name;
 
/*
 *  --check turns on checking that the working tree matches the
@@ -1967,13 +1968,14 @@ static void prefix_patch(struct apply_state *state, 
struct patch *p)
  * include/exclude
  */
 
-static struct string_list limit_by_name;
 static int has_include;
-static void add_name_limit(const char *name, int exclude)
+static void add_name_limit(struct apply_state *state,
+  const char *name,
+  int exclude)
 {
struct string_list_item *it;
 
-   it = string_list_append(_by_name, name);
+   it = string_list_append(>limit_by_name, name);
it->util = exclude ? NULL : (void *) 1;
 }
 
@@ -1991,8 +1993,8 @@ static int use_patch(struct apply_state *state, struct 
patch *p)
}
 
/* See if it matches any of exclude/include rule */
-   for (i = 0; i < limit_by_name.nr; i++) {
-   struct string_list_item *it = _by_name.items[i];
+   for (i = 0; i < state->limit_by_name.nr; i++) {
+   struct string_list_item *it = >limit_by_name.items[i];
if (!wildmatch(it->string, pathname, 0, NULL))
return (it->util != NULL);
}
@@ -4529,14 +4531,16 @@ static void git_apply_config(void)
 static int option_parse_exclude(const struct option *opt,
const char *arg, int unset)
 {
-   add_name_limit(arg, 1);
+   struct apply_state *state = opt->value;
+   add_name_limit(state, arg, 1);
return 0;
 }
 
 static int option_parse_include(const struct option *opt,
const char *arg, int unset)
 {
-   add_name_limit(arg, 0);
+   struct apply_state *state = opt->value;
+   add_name_limit(state, arg, 0);
has_include = 1;
return 0;
 }
@@ -4607,10 +4611,10 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix_)
const char *whitespace_option = NULL;
 
struct option builtin_apply_options[] = {
-   { OPTION_CALLBACK, 0, "exclude", NULL, N_("path"),
+   { OPTION_CALLBACK, 0, "exclude", , N_("path"),
N_("don't apply changes matching the given path"),
0, option_parse_exclude },
-   { OPTION_CALLBACK, 0, "include", NULL, N_("path"),
+   { OPTION_CALLBACK, 0, "include", , N_("path"),
N_("apply changes matching the given path"),
0, option_parse_include },
{ OPTION_CALLBACK, 'p', NULL, NULL, N_("num"),
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 35/94] builtin/apply: move 'whitespace_error' global into 'struct apply_state'

2016-05-11 Thread Christian Couder
To libify the apply functionality the 'whitespace_error' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 27 ++-
 1 file changed, 14 insertions(+), 13 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 6b3540f..1684f25 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -66,6 +66,8 @@ struct apply_state {
int p_value;
int p_value_known;
unsigned int p_context;
+
+   int whitespace_error;
 };
 
 static int newfd = -1;
@@ -81,7 +83,6 @@ static enum ws_error_action {
die_on_ws_error,
correct_ws_error
 } ws_error_action = warn_on_ws_error;
-static int whitespace_error;
 static int squelch_whitespace_errors = 5;
 static int applied_after_fixing_ws;
 
@@ -1603,9 +1604,9 @@ static void record_ws_error(struct apply_state *state,
if (!result)
return;
 
-   whitespace_error++;
+   state->whitespace_error++;
if (squelch_whitespace_errors &&
-   squelch_whitespace_errors < whitespace_error)
+   squelch_whitespace_errors < state->whitespace_error)
return;
 
err = whitespace_error_string(result);
@@ -2862,7 +2863,7 @@ static int apply_one_fragment(struct apply_state *state,
 
start = newlines.len;
if (first != '+' ||
-   !whitespace_error ||
+   !state->whitespace_error ||
ws_error_action != correct_ws_error) {
strbuf_add(, patch + 1, plen);
}
@@ -4535,7 +4536,7 @@ static int apply_patch(struct apply_state *state,
if (!list && !skipped_patch)
die(_("unrecognized input"));
 
-   if (whitespace_error && (ws_error_action == die_on_ws_error))
+   if (state->whitespace_error && (ws_error_action == die_on_ws_error))
state->apply = 0;
 
state->update_index = state->check_index && state->apply;
@@ -4792,11 +4793,11 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix_)
set_default_whitespace_mode(, whitespace_option);
if (read_stdin)
errs |= apply_patch(, 0, "", options);
-   if (whitespace_error) {
+   if (state.whitespace_error) {
if (squelch_whitespace_errors &&
-   squelch_whitespace_errors < whitespace_error) {
+   squelch_whitespace_errors < state.whitespace_error) {
int squelched =
-   whitespace_error - squelch_whitespace_errors;
+   state.whitespace_error - 
squelch_whitespace_errors;
warning(Q_("squelched %d whitespace error",
   "squelched %d whitespace errors",
   squelched),
@@ -4805,18 +4806,18 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix_)
if (ws_error_action == die_on_ws_error)
die(Q_("%d line adds whitespace errors.",
   "%d lines add whitespace errors.",
-  whitespace_error),
-   whitespace_error);
+  state.whitespace_error),
+   state.whitespace_error);
if (applied_after_fixing_ws && state.apply)
warning("%d line%s applied after"
" fixing whitespace errors.",
applied_after_fixing_ws,
applied_after_fixing_ws == 1 ? "" : "s");
-   else if (whitespace_error)
+   else if (state.whitespace_error)
warning(Q_("%d line adds whitespace errors.",
   "%d lines add whitespace errors.",
-  whitespace_error),
-   whitespace_error);
+  state.whitespace_error),
+   state.whitespace_error);
}
 
if (state.update_index) {
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 24/94] builtin/apply: move 'unsafe_paths' global into 'struct apply_state'

2016-05-11 Thread Christian Couder
To libify the apply functionality the 'unsafe_paths' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 8 
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index d699cd9..9209af4 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -54,6 +54,7 @@ struct apply_state {
 
int unidiff_zero;
int update_index;
+   int unsafe_paths;
 };
 
 /*
@@ -64,7 +65,6 @@ static int newfd = -1;
 static int state_p_value = 1;
 static int p_value_known;
 static int apply = 1;
-static int unsafe_paths;
 static const char *fake_ancestor;
 static int line_termination = '\n';
 static unsigned int p_context = UINT_MAX;
@@ -3837,7 +3837,7 @@ static int check_patch(struct apply_state *state, struct 
patch *patch)
}
}
 
-   if (!unsafe_paths)
+   if (!state->unsafe_paths)
die_on_unsafe_path(patch);
 
/*
@@ -4621,7 +4621,7 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix_)
N_("make sure the patch is applicable to the current 
index")),
OPT_BOOL(0, "cached", ,
N_("apply a patch without touching the working tree")),
-   OPT_BOOL(0, "unsafe-paths", _paths,
+   OPT_BOOL(0, "unsafe-paths", _paths,
N_("accept a patch that touches outside the working 
area")),
OPT_BOOL(0, "apply", _apply,
N_("also apply the patch (use with 
--stat/--summary/--check)")),
@@ -4690,7 +4690,7 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix_)
state.check_index = 1;
}
if (state.check_index)
-   unsafe_paths = 0;
+   state.unsafe_paths = 0;
 
for (i = 0; i < argc; i++) {
const char *arg = argv[i];
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 33/94] builtin/apply: move 'p_value_known' global into 'struct apply_state'

2016-05-11 Thread Christian Couder
To libify the apply functionality the 'p_value_known' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 9 -
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 4e476d5..30eea9c 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -63,13 +63,12 @@ struct apply_state {
int line_termination;
 
int p_value;
+   int p_value_known;
unsigned int p_context;
 };
 
 static int newfd = -1;
 
-static int p_value_known;
-
 static const char * const apply_usage[] = {
N_("git apply [] [...]"),
NULL
@@ -882,14 +881,14 @@ static void parse_traditional_patch(struct apply_state 
*state,
 
first += 4; /* skip "--- " */
second += 4;/* skip "+++ " */
-   if (!p_value_known) {
+   if (!state->p_value_known) {
int p, q;
p = guess_p_value(state, first);
q = guess_p_value(state, second);
if (p < 0) p = q;
if (0 <= p && p == q) {
state->p_value = p;
-   p_value_known = 1;
+   state->p_value_known = 1;
}
}
if (is_dev_null(first)) {
@@ -4595,7 +4594,7 @@ static int option_parse_p(const struct option *opt,
 {
struct apply_state *state = opt->value;
state->p_value = atoi(arg);
-   p_value_known = 1;
+   state->p_value_known = 1;
return 0;
 }
 
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 46/94] builtin/apply: move 'state' check into check_apply_state()

2016-05-11 Thread Christian Couder
To libify the apply functionality we should provide a function
to check that the values in a 'struct apply_state' instance are
coherent. Let's move the code to do that into a new
check_apply_state() function.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 52 +---
 1 file changed, 29 insertions(+), 23 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 14286d2..e5f76d8 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4681,11 +4681,38 @@ static void init_apply_state(struct apply_state *state, 
const char *prefix)
parse_ignorewhitespace_option(state, 
apply_default_ignorewhitespace);
 }
 
+static void check_apply_state(struct apply_state *state, int force_apply)
+{
+   int is_not_gitdir = !startup_info->have_repository;
+
+   if (state->apply_with_reject && state->threeway)
+   die("--reject and --3way cannot be used together.");
+   if (state->cached && state->threeway)
+   die("--cached and --3way cannot be used together.");
+   if (state->threeway) {
+   if (is_not_gitdir)
+   die(_("--3way outside a repository"));
+   state->check_index = 1;
+   }
+   if (state->apply_with_reject)
+   state->apply = state->apply_verbosely = 1;
+   if (!force_apply && (state->diffstat || state->numstat || 
state->summary || state->check || state->fake_ancestor))
+   state->apply = 0;
+   if (state->check_index && is_not_gitdir)
+   die(_("--index outside a repository"));
+   if (state->cached) {
+   if (is_not_gitdir)
+   die(_("--cached outside a repository"));
+   state->check_index = 1;
+   }
+   if (state->check_index)
+   state->unsafe_paths = 0;
+}
+
 int cmd_apply(int argc, const char **argv, const char *prefix_)
 {
int i;
int errs = 0;
-   int is_not_gitdir = !startup_info->have_repository;
int force_apply = 0;
int options = 0;
int read_stdin = 1;
@@ -4765,28 +4792,7 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix_)
argc = parse_options(argc, argv, state.prefix, builtin_apply_options,
apply_usage, 0);
 
-   if (state.apply_with_reject && state.threeway)
-   die("--reject and --3way cannot be used together.");
-   if (state.cached && state.threeway)
-   die("--cached and --3way cannot be used together.");
-   if (state.threeway) {
-   if (is_not_gitdir)
-   die(_("--3way outside a repository"));
-   state.check_index = 1;
-   }
-   if (state.apply_with_reject)
-   state.apply = state.apply_verbosely = 1;
-   if (!force_apply && (state.diffstat || state.numstat || state.summary 
|| state.check || state.fake_ancestor))
-   state.apply = 0;
-   if (state.check_index && is_not_gitdir)
-   die(_("--index outside a repository"));
-   if (state.cached) {
-   if (is_not_gitdir)
-   die(_("--cached outside a repository"));
-   state.check_index = 1;
-   }
-   if (state.check_index)
-   state.unsafe_paths = 0;
+   check_apply_state(, force_apply);
 
for (i = 0; i < argc; i++) {
const char *arg = argv[i];
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 34/94] builtin/apply: move 'root' global into 'struct apply_state'

2016-05-11 Thread Christian Couder
To libify the apply functionality the 'root' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 81 ++---
 1 file changed, 48 insertions(+), 33 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 30eea9c..6b3540f 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -47,6 +47,7 @@ struct apply_state {
const char *patch_input_file;
struct string_list limit_by_name;
int has_include;
+   struct strbuf root;
 
/*
 *  --check turns on checking that the working tree matches the
@@ -90,8 +91,6 @@ static enum ws_ignore {
 } ws_ignore_action = ignore_ws_none;
 
 
-static struct strbuf root = STRBUF_INIT;
-
 static void parse_whitespace_option(const char *option)
 {
if (!option) {
@@ -481,7 +480,10 @@ static char *squash_slash(char *name)
return name;
 }
 
-static char *find_name_gnu(const char *line, const char *def, int p_value)
+static char *find_name_gnu(struct apply_state *state,
+  const char *line,
+  const char *def,
+  int p_value)
 {
struct strbuf name = STRBUF_INIT;
char *cp;
@@ -505,8 +507,8 @@ static char *find_name_gnu(const char *line, const char 
*def, int p_value)
}
 
strbuf_remove(, 0, cp - name.buf);
-   if (root.len)
-   strbuf_insert(, 0, root.buf, root.len);
+   if (state->root.len)
+   strbuf_insert(, 0, state->root.buf, state->root.len);
return squash_slash(strbuf_detach(, NULL));
 }
 
@@ -669,8 +671,12 @@ static size_t diff_timestamp_len(const char *line, size_t 
len)
return line + len - end;
 }
 
-static char *find_name_common(const char *line, const char *def,
- int p_value, const char *end, int terminate)
+static char *find_name_common(struct apply_state *state,
+ const char *line,
+ const char *def,
+ int p_value,
+ const char *end,
+ int terminate)
 {
int len;
const char *start = NULL;
@@ -708,32 +714,39 @@ static char *find_name_common(const char *line, const 
char *def,
return squash_slash(xstrdup(def));
}
 
-   if (root.len) {
-   char *ret = xstrfmt("%s%.*s", root.buf, len, start);
+   if (state->root.len) {
+   char *ret = xstrfmt("%s%.*s", state->root.buf, len, start);
return squash_slash(ret);
}
 
return squash_slash(xmemdupz(start, len));
 }
 
-static char *find_name(const char *line, char *def, int p_value, int terminate)
+static char *find_name(struct apply_state *state,
+  const char *line,
+  char *def,
+  int p_value,
+  int terminate)
 {
if (*line == '"') {
-   char *name = find_name_gnu(line, def, p_value);
+   char *name = find_name_gnu(state, line, def, p_value);
if (name)
return name;
}
 
-   return find_name_common(line, def, p_value, NULL, terminate);
+   return find_name_common(state, line, def, p_value, NULL, terminate);
 }
 
-static char *find_name_traditional(const char *line, char *def, int p_value)
+static char *find_name_traditional(struct apply_state *state,
+  const char *line,
+  char *def,
+  int p_value)
 {
size_t len;
size_t date_len;
 
if (*line == '"') {
-   char *name = find_name_gnu(line, def, p_value);
+   char *name = find_name_gnu(state, line, def, p_value);
if (name)
return name;
}
@@ -741,10 +754,10 @@ static char *find_name_traditional(const char *line, char 
*def, int p_value)
len = strchrnul(line, '\n') - line;
date_len = diff_timestamp_len(line, len);
if (!date_len)
-   return find_name_common(line, def, p_value, NULL, TERM_TAB);
+   return find_name_common(state, line, def, p_value, NULL, 
TERM_TAB);
len -= date_len;
 
-   return find_name_common(line, def, p_value, line + len, 0);
+   return find_name_common(state, line, def, p_value, line + len, 0);
 }
 
 static int count_slashes(const char *cp)
@@ -769,7 +782,7 @@ static int guess_p_value(struct apply_state *state, const 
char *nameline)
 
if (is_dev_null(nameline))
return -1;
-   name = find_name_traditional(nameline, NULL, 0);
+   name = find_name

[PATCH v2 39/94] builtin/apply: move 'applied_after_fixing_ws' into 'struct apply_state'

2016-05-11 Thread Christian Couder
To libify the apply functionality the 'applied_after_fixing_ws' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 10 +-
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index f84e301..e68fd2c 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -70,6 +70,7 @@ struct apply_state {
const char *whitespace_option;
int whitespace_error;
int squelch_whitespace_errors;
+   int applied_after_fixing_ws;
 };
 
 static int newfd = -1;
@@ -85,7 +86,6 @@ static enum ws_error_action {
die_on_ws_error,
correct_ws_error
 } ws_error_action = warn_on_ws_error;
-static int applied_after_fixing_ws;
 
 static enum ws_ignore {
ignore_ws_none,
@@ -2868,7 +2868,7 @@ static int apply_one_fragment(struct apply_state *state,
strbuf_add(, patch + 1, plen);
}
else {
-   ws_fix_copy(, patch + 1, plen, 
ws_rule, _after_fixing_ws);
+   ws_fix_copy(, patch + 1, plen, 
ws_rule, >applied_after_fixing_ws);
}
add_line_info(, newlines.buf + start, 
newlines.len - start,
  (first == '+' ? 0 : LINE_COMMON));
@@ -4806,11 +4806,11 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix_)
   "%d lines add whitespace errors.",
   state.whitespace_error),
state.whitespace_error);
-   if (applied_after_fixing_ws && state.apply)
+   if (state.applied_after_fixing_ws && state.apply)
warning("%d line%s applied after"
" fixing whitespace errors.",
-   applied_after_fixing_ws,
-   applied_after_fixing_ws == 1 ? "" : "s");
+   state.applied_after_fixing_ws,
+   state.applied_after_fixing_ws == 1 ? "" : "s");
else if (state.whitespace_error)
warning(Q_("%d line adds whitespace errors.",
   "%d lines add whitespace errors.",
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 36/94] builtin/apply: move 'whitespace_option' into 'struct apply_state'

2016-05-11 Thread Christian Couder
This will enable further refactoring, and it is more coherent and
simpler if all the option_parse_*() functions are passed a
'struct apply_state' instance in opt->value.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 13 ++---
 1 file changed, 6 insertions(+), 7 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 1684f25..4b9a5ff 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -67,6 +67,7 @@ struct apply_state {
int p_value_known;
unsigned int p_context;
 
+   const char *whitespace_option;
int whitespace_error;
 };
 
@@ -4625,9 +4626,9 @@ static int option_parse_space_change(const struct option 
*opt,
 static int option_parse_whitespace(const struct option *opt,
   const char *arg, int unset)
 {
-   const char **whitespace_option = opt->value;
+   struct apply_state *state = opt->value;
 
-   *whitespace_option = arg;
+   state->whitespace_option = arg;
parse_whitespace_option(arg);
return 0;
 }
@@ -4670,8 +4671,6 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix_)
int read_stdin = 1;
struct apply_state state;
 
-   const char *whitespace_option = NULL;
-
struct option builtin_apply_options[] = {
{ OPTION_CALLBACK, 0, "exclude", , N_("path"),
N_("don't apply changes matching the given path"),
@@ -4711,7 +4710,7 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix_)
N_("paths are separated with NUL character"), '\0'),
OPT_INTEGER('C', NULL, _context,
N_("ensure at least  lines of context 
match")),
-   { OPTION_CALLBACK, 0, "whitespace", _option, 
N_("action"),
+   { OPTION_CALLBACK, 0, "whitespace", , N_("action"),
N_("detect new or modified lines that have whitespace 
errors"),
0, option_parse_whitespace },
{ OPTION_CALLBACK, 0, "ignore-space-change", NULL, NULL,
@@ -4786,11 +4785,11 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix_)
if (fd < 0)
die_errno(_("can't open patch '%s'"), arg);
read_stdin = 0;
-   set_default_whitespace_mode(, whitespace_option);
+   set_default_whitespace_mode(, state.whitespace_option);
errs |= apply_patch(, fd, arg, options);
close(fd);
}
-   set_default_whitespace_mode(, whitespace_option);
+   set_default_whitespace_mode(, state.whitespace_option);
if (read_stdin)
errs |= apply_patch(, 0, "", options);
if (state.whitespace_error) {
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 37/94] builtin/apply: remove whitespace_option arg from set_default_whitespace_mode()

2016-05-11 Thread Christian Couder
A previous change has move the whitespace_option variable from cmd_apply
into 'struct apply_state', so that we can now avoid passing it separately
to set_default_whitespace_mode().

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 9 -
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 4b9a5ff..ab954b4 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -138,10 +138,9 @@ static void parse_ignorewhitespace_option(const char 
*option)
die(_("unrecognized whitespace ignore option '%s'"), option);
 }
 
-static void set_default_whitespace_mode(struct apply_state *state,
-   const char *whitespace_option)
+static void set_default_whitespace_mode(struct apply_state *state)
 {
-   if (!whitespace_option && !apply_default_whitespace)
+   if (!state->whitespace_option && !apply_default_whitespace)
ws_error_action = (state->apply ? warn_on_ws_error : 
nowarn_ws_error);
 }
 
@@ -4785,11 +4784,11 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix_)
if (fd < 0)
die_errno(_("can't open patch '%s'"), arg);
read_stdin = 0;
-   set_default_whitespace_mode(, state.whitespace_option);
+   set_default_whitespace_mode();
errs |= apply_patch(, fd, arg, options);
close(fd);
}
-   set_default_whitespace_mode(, state.whitespace_option);
+   set_default_whitespace_mode();
if (read_stdin)
errs |= apply_patch(, 0, "", options);
if (state.whitespace_error) {
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 12/94] builtin/apply: move 'check_index' global into 'struct apply_state'

2016-05-11 Thread Christian Couder
To libify the apply functionality the 'check_index' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 69 +
 1 file changed, 40 insertions(+), 29 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 6bf103a..5d8f410 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -30,6 +30,10 @@ struct apply_state {
 *files that are being modified, but doesn't apply the patch
 */
int check;
+
+   /* --index updates the cache as well. */
+   int check_index;
+
int unidiff_zero;
 };
 
@@ -37,14 +41,12 @@ struct apply_state {
  *  --stat does just a diffstat, and doesn't actually apply
  *  --numstat does numeric diffstat, and doesn't actually apply
  *  --index-info shows the old and new index info for paths if available.
- *  --index updates the cache as well.
  *  --cached updates only the cache without ever touching the working tree.
  */
 static int newfd = -1;
 
 static int state_p_value = 1;
 static int p_value_known;
-static int check_index;
 static int update_index;
 static int cached;
 static int diffstat;
@@ -3246,13 +3248,14 @@ static int verify_index_match(const struct cache_entry 
*ce, struct stat *st)
 
 #define SUBMODULE_PATCH_WITHOUT_INDEX 1
 
-static int load_patch_target(struct strbuf *buf,
+static int load_patch_target(struct apply_state *state,
+struct strbuf *buf,
 const struct cache_entry *ce,
 struct stat *st,
 const char *name,
 unsigned expected_mode)
 {
-   if (cached || check_index) {
+   if (cached || state->check_index) {
if (read_file_or_gitlink(ce, buf))
return error(_("read of %s failed"), name);
} else if (name) {
@@ -3278,7 +3281,8 @@ static int load_patch_target(struct strbuf *buf,
  * applying a non-git patch that incrementally updates the tree,
  * we read from the result of a previous diff.
  */
-static int load_preimage(struct image *image,
+static int load_preimage(struct apply_state *state,
+struct image *image,
 struct patch *patch, struct stat *st,
 const struct cache_entry *ce)
 {
@@ -3296,7 +3300,7 @@ static int load_preimage(struct image *image,
/* We have a patched copy in memory; use that. */
strbuf_add(, previous->result, previous->resultsize);
} else {
-   status = load_patch_target(, ce, st,
+   status = load_patch_target(state, , ce, st,
   patch->old_name, patch->old_mode);
if (status < 0)
return status;
@@ -3355,7 +3359,9 @@ static int three_way_merge(struct image *image,
  * the current contents of the new_name.  In no cases other than that
  * this function will be called.
  */
-static int load_current(struct image *image, struct patch *patch)
+static int load_current(struct apply_state *state,
+   struct image *image,
+   struct patch *patch)
 {
struct strbuf buf = STRBUF_INIT;
int status, pos;
@@ -3382,7 +3388,7 @@ static int load_current(struct image *image, struct patch 
*patch)
if (verify_index_match(ce, ))
return error(_("%s: does not match index"), name);
 
-   status = load_patch_target(, ce, , name, mode);
+   status = load_patch_target(state, , ce, , name, mode);
if (status < 0)
return status;
else if (status)
@@ -3432,11 +3438,11 @@ static int try_threeway(struct apply_state *state,
 
/* our_sha1[] is ours */
if (patch->is_new) {
-   if (load_current(_image, patch))
+   if (load_current(state, _image, patch))
return error("cannot read the current contents of '%s'",
 patch->new_name);
} else {
-   if (load_preimage(_image, patch, st, ce))
+   if (load_preimage(state, _image, patch, st, ce))
return error("cannot read the current contents of '%s'",
 patch->old_name);
}
@@ -3471,7 +3477,7 @@ static int apply_data(struct apply_state *state, struct 
patch *patch,
 {
struct image image;
 
-   if (load_preimage(, patch, st, ce) < 0)
+   if (load_preimage(state, , patch, st, ce) < 0)
return -1;
 
if (patch->direct_to_threeway ||
@@ -3502,7 +3508,10 @@ static int apply_data(struct apply_state *state, struct 
patch *patch,
  * check_p

[PATCH v2 38/94] builtin/apply: move 'squelch_whitespace_errors' into 'struct apply_state'

2016-05-11 Thread Christian Couder
To libify the apply functionality the 'squelch_whitespace_errors' variable 
should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 22 +++---
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index ab954b4..f84e301 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -69,6 +69,7 @@ struct apply_state {
 
const char *whitespace_option;
int whitespace_error;
+   int squelch_whitespace_errors;
 };
 
 static int newfd = -1;
@@ -84,7 +85,6 @@ static enum ws_error_action {
die_on_ws_error,
correct_ws_error
 } ws_error_action = warn_on_ws_error;
-static int squelch_whitespace_errors = 5;
 static int applied_after_fixing_ws;
 
 static enum ws_ignore {
@@ -93,7 +93,7 @@ static enum ws_ignore {
 } ws_ignore_action = ignore_ws_none;
 
 
-static void parse_whitespace_option(const char *option)
+static void parse_whitespace_option(struct apply_state *state, const char 
*option)
 {
if (!option) {
ws_error_action = warn_on_ws_error;
@@ -113,7 +113,7 @@ static void parse_whitespace_option(const char *option)
}
if (!strcmp(option, "error-all")) {
ws_error_action = die_on_ws_error;
-   squelch_whitespace_errors = 0;
+   state->squelch_whitespace_errors = 0;
return;
}
if (!strcmp(option, "strip") || !strcmp(option, "fix")) {
@@ -1605,8 +1605,8 @@ static void record_ws_error(struct apply_state *state,
return;
 
state->whitespace_error++;
-   if (squelch_whitespace_errors &&
-   squelch_whitespace_errors < state->whitespace_error)
+   if (state->squelch_whitespace_errors &&
+   state->squelch_whitespace_errors < state->whitespace_error)
return;
 
err = whitespace_error_string(result);
@@ -4626,9 +4626,8 @@ static int option_parse_whitespace(const struct option 
*opt,
   const char *arg, int unset)
 {
struct apply_state *state = opt->value;
-
state->whitespace_option = arg;
-   parse_whitespace_option(arg);
+   parse_whitespace_option(state, arg);
return 0;
 }
 
@@ -4651,11 +4650,12 @@ static void init_apply_state(struct apply_state *state, 
const char *prefix)
state->line_termination = '\n';
state->p_value = 1;
state->p_context = UINT_MAX;
+   state->squelch_whitespace_errors = 5;
strbuf_init(>root, 0);
 
git_apply_config();
if (apply_default_whitespace)
-   parse_whitespace_option(apply_default_whitespace);
+   parse_whitespace_option(state, apply_default_whitespace);
if (apply_default_ignorewhitespace)
parse_ignorewhitespace_option(apply_default_ignorewhitespace);
 }
@@ -4792,10 +4792,10 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix_)
if (read_stdin)
errs |= apply_patch(, 0, "", options);
if (state.whitespace_error) {
-   if (squelch_whitespace_errors &&
-   squelch_whitespace_errors < state.whitespace_error) {
+   if (state.squelch_whitespace_errors &&
+   state.squelch_whitespace_errors < state.whitespace_error) {
int squelched =
-   state.whitespace_error - 
squelch_whitespace_errors;
+   state.whitespace_error - 
state.squelch_whitespace_errors;
warning(Q_("squelched %d whitespace error",
   "squelched %d whitespace errors",
   squelched),
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 02/94] builtin/apply: avoid parameter shadowing 'p_value' global

2016-05-11 Thread Christian Couder
Let's just rename the global 'state_p_value' as it will become
'state->p_value' in a following patch.

This also avoid errors when compiling with -Wshadow and makes
it safer to later move global variables into a "state" struct.

Helped-by: Nguyễn Thái Ngọc Duy <pclo...@gmail.com>
Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 34 +-
 1 file changed, 17 insertions(+), 17 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index fe5aebd..e133b38 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -35,7 +35,7 @@ static int prefix_length = -1;
 static int newfd = -1;
 
 static int unidiff_zero;
-static int p_value = 1;
+static int state_p_value = 1;
 static int p_value_known;
 static int check_index;
 static int update_index;
@@ -872,24 +872,24 @@ static void parse_traditional_patch(const char *first, 
const char *second, struc
q = guess_p_value(second);
if (p < 0) p = q;
if (0 <= p && p == q) {
-   p_value = p;
+   state_p_value = p;
p_value_known = 1;
}
}
if (is_dev_null(first)) {
patch->is_new = 1;
patch->is_delete = 0;
-   name = find_name_traditional(second, NULL, p_value);
+   name = find_name_traditional(second, NULL, state_p_value);
patch->new_name = name;
} else if (is_dev_null(second)) {
patch->is_new = 0;
patch->is_delete = 1;
-   name = find_name_traditional(first, NULL, p_value);
+   name = find_name_traditional(first, NULL, state_p_value);
patch->old_name = name;
} else {
char *first_name;
-   first_name = find_name_traditional(first, NULL, p_value);
-   name = find_name_traditional(second, first_name, p_value);
+   first_name = find_name_traditional(first, NULL, state_p_value);
+   name = find_name_traditional(second, first_name, state_p_value);
free(first_name);
if (has_epoch_timestamp(first)) {
patch->is_new = 1;
@@ -928,7 +928,7 @@ static int gitdiff_hdrend(const char *line, struct patch 
*patch)
 static void gitdiff_verify_name(const char *line, int isnull, char **name, int 
side)
 {
if (!*name && !isnull) {
-   *name = find_name(line, NULL, p_value, TERM_TAB);
+   *name = find_name(line, NULL, state_p_value, TERM_TAB);
return;
}
 
@@ -938,7 +938,7 @@ static void gitdiff_verify_name(const char *line, int 
isnull, char **name, int s
if (isnull)
die(_("git apply: bad git-diff - expected /dev/null, 
got %s on line %d"),
*name, linenr);
-   another = find_name(line, NULL, p_value, TERM_TAB);
+   another = find_name(line, NULL, state_p_value, TERM_TAB);
if (!another || memcmp(another, *name, len + 1))
die((side == DIFF_NEW_NAME) ?
_("git apply: bad git-diff - inconsistent new 
filename on line %d") :
@@ -997,7 +997,7 @@ static int gitdiff_copysrc(const char *line, struct patch 
*patch)
 {
patch->is_copy = 1;
free(patch->old_name);
-   patch->old_name = find_name(line, NULL, p_value ? p_value - 1 : 0, 0);
+   patch->old_name = find_name(line, NULL, state_p_value ? state_p_value - 
1 : 0, 0);
return 0;
 }
 
@@ -1005,7 +1005,7 @@ static int gitdiff_copydst(const char *line, struct patch 
*patch)
 {
patch->is_copy = 1;
free(patch->new_name);
-   patch->new_name = find_name(line, NULL, p_value ? p_value - 1 : 0, 0);
+   patch->new_name = find_name(line, NULL, state_p_value ? state_p_value - 
1 : 0, 0);
return 0;
 }
 
@@ -1013,7 +1013,7 @@ static int gitdiff_renamesrc(const char *line, struct 
patch *patch)
 {
patch->is_rename = 1;
free(patch->old_name);
-   patch->old_name = find_name(line, NULL, p_value ? p_value - 1 : 0, 0);
+   patch->old_name = find_name(line, NULL, state_p_value ? state_p_value - 
1 : 0, 0);
return 0;
 }
 
@@ -1021,7 +1021,7 @@ static int gitdiff_renamedst(const char *line, struct 
patch *patch)
 {
patch->is_rename = 1;
free(patch->new_name);
-   patch->new_name = find_name(line, NULL, p_value ? p_value - 1 : 0, 0);
+   patch->new_name = find_name(line, NULL, state_p_value ? state_p_value - 
1 : 0, 0);
return 0;
 }
 
@@ -1092,10 +1092,10 @@ static const char *skip_tree_prefix(const char *line, 
int llen)
int nslash;
int i;
 
-   if (!

[PATCH v2 28/94] builtin/apply: move 'apply' global into 'struct apply_state'

2016-05-11 Thread Christian Couder
To libify the apply functionality the 'apply' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 31 +--
 1 file changed, 17 insertions(+), 14 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 2ba2b21..a3db284 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -25,6 +25,7 @@ struct apply_state {
const char *prefix;
int prefix_length;
 
+   int apply;
int allow_overlap;
int apply_in_reverse;
int apply_with_reject;
@@ -65,7 +66,7 @@ static int newfd = -1;
 
 static int state_p_value = 1;
 static int p_value_known;
-static int apply = 1;
+
 static const char * const apply_usage[] = {
N_("git apply [] [...]"),
NULL
@@ -135,10 +136,11 @@ static void parse_ignorewhitespace_option(const char 
*option)
die(_("unrecognized whitespace ignore option '%s'"), option);
 }
 
-static void set_default_whitespace_mode(const char *whitespace_option)
+static void set_default_whitespace_mode(struct apply_state *state,
+   const char *whitespace_option)
 {
if (!whitespace_option && !apply_default_whitespace)
-   ws_error_action = (apply ? warn_on_ws_error : nowarn_ws_error);
+   ws_error_action = (state->apply ? warn_on_ws_error : 
nowarn_ws_error);
 }
 
 /*
@@ -2067,7 +2069,7 @@ static int parse_chunk(struct apply_state *state, char 
*buffer, unsigned long si
 * without metadata change.  A binary patch appears
 * empty to us here.
 */
-   if ((apply || state->check) &&
+   if ((state->apply || state->check) &&
(!patch->is_binary && !metadata_changes(patch)))
die(_("patch with only garbage at line %d"), 
state_linenr);
}
@@ -2925,7 +2927,7 @@ static int apply_one_fragment(struct apply_state *state,
 * apply_data->apply_fragments->apply_one_fragment
 */
if (ws_error_action == die_on_ws_error)
-   apply = 0;
+   state->apply = 0;
}
 
if (state->apply_verbosely && applied_pos != pos) {
@@ -4469,9 +4471,9 @@ static int apply_patch(struct apply_state *state,
die(_("unrecognized input"));
 
if (whitespace_error && (ws_error_action == die_on_ws_error))
-   apply = 0;
+   state->apply = 0;
 
-   state->update_index = state->check_index && apply;
+   state->update_index = state->check_index && state->apply;
if (state->update_index && newfd < 0)
newfd = hold_locked_index(_file, 1);
 
@@ -4480,12 +4482,12 @@ static int apply_patch(struct apply_state *state,
die(_("unable to read index file"));
}
 
-   if ((state->check || apply) &&
+   if ((state->check || state->apply) &&
check_patch_list(state, list) < 0 &&
!state->apply_with_reject)
exit(1);
 
-   if (apply && write_out_results(state, list)) {
+   if (state->apply && write_out_results(state, list)) {
if (state->apply_with_reject)
exit(1);
/* with --3way, we still need to write the index out */
@@ -4574,6 +4576,7 @@ static void init_apply_state(struct apply_state *state, 
const char *prefix)
memset(state, 0, sizeof(*state));
state->prefix = prefix;
state->prefix_length = state->prefix ? strlen(state->prefix) : 0;
+   state->apply = 1;
state->line_termination = '\n';
state->p_context = UINT_MAX;
 
@@ -4680,9 +4683,9 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix_)
state.check_index = 1;
}
if (state.apply_with_reject)
-   apply = state.apply_verbosely = 1;
+   state.apply = state.apply_verbosely = 1;
if (!force_apply && (state.diffstat || state.numstat || state.summary 
|| state.check || state.fake_ancestor))
-   apply = 0;
+   state.apply = 0;
if (state.check_index && is_not_gitdir)
die(_("--index outside a repository"));
if (state.cached) {
@@ -4710,11 +4713,11 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix_)
if (fd < 0)
die_errno(_("can't open patch '%s'"), arg);
   

[PATCH v2 14/94] builtin/apply: move 'apply_with_reject' global into 'struct apply_state'

2016-05-11 Thread Christian Couder
To libify the apply functionality the 'apply_with_reject' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 14 +++---
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 73cef9b..53cc280 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -26,6 +26,7 @@ struct apply_state {
int prefix_length;
 
int apply_in_reverse;
+   int apply_with_reject;
 
/*
 *  --check turns on checking that the working tree matches the
@@ -55,7 +56,6 @@ static int diffstat;
 static int numstat;
 static int summary;
 static int apply = 1;
-static int apply_with_reject;
 static int apply_verbosely;
 static int allow_overlap;
 static int no_add;
@@ -3101,7 +3101,7 @@ static int apply_fragments(struct apply_state *state, 
struct image *img, struct
nth++;
if (apply_one_fragment(state, img, frag, inaccurate_eof, 
ws_rule, nth)) {
error(_("patch failed: %s:%ld"), name, frag->oldpos);
-   if (!apply_with_reject)
+   if (!state->apply_with_reject)
return -1;
frag->rejected = 1;
}
@@ -4467,11 +4467,11 @@ static int apply_patch(struct apply_state *state,
 
if ((state->check || apply) &&
check_patch_list(state, list) < 0 &&
-   !apply_with_reject)
+   !state->apply_with_reject)
exit(1);
 
if (apply && write_out_results(list)) {
-   if (apply_with_reject)
+   if (state->apply_with_reject)
exit(1);
/* with --3way, we still need to write the index out */
return 1;
@@ -4631,7 +4631,7 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix_)
N_("apply the patch in reverse")),
OPT_BOOL(0, "unidiff-zero", _zero,
N_("don't expect at least one line of context")),
-   OPT_BOOL(0, "reject", _with_reject,
+   OPT_BOOL(0, "reject", _with_reject,
N_("leave the rejected hunks in corresponding *.rej 
files")),
OPT_BOOL(0, "allow-overlap", _overlap,
N_("allow overlapping hunks")),
@@ -4653,7 +4653,7 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix_)
argc = parse_options(argc, argv, state.prefix, builtin_apply_options,
apply_usage, 0);
 
-   if (apply_with_reject && threeway)
+   if (state.apply_with_reject && threeway)
die("--reject and --3way cannot be used together.");
if (cached && threeway)
die("--cached and --3way cannot be used together.");
@@ -4662,7 +4662,7 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix_)
die(_("--3way outside a repository"));
state.check_index = 1;
}
-   if (apply_with_reject)
+   if (state.apply_with_reject)
apply = apply_verbosely = 1;
if (!force_apply && (diffstat || numstat || summary || state.check || 
fake_ancestor))
apply = 0;
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 27/94] builtin/apply: move 'p_context' global into 'struct apply_state'

2016-05-11 Thread Christian Couder
To libify the apply functionality the 'p_context' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 8 +---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 6f9a090..2ba2b21 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -57,6 +57,8 @@ struct apply_state {
int update_index;
int unsafe_paths;
int line_termination;
+
+   unsigned int p_context;
 };
 
 static int newfd = -1;
@@ -64,7 +66,6 @@ static int newfd = -1;
 static int state_p_value = 1;
 static int p_value_known;
 static int apply = 1;
-static unsigned int p_context = UINT_MAX;
 static const char * const apply_usage[] = {
N_("git apply [] [...]"),
NULL
@@ -2880,7 +2881,7 @@ static int apply_one_fragment(struct apply_state *state,
break;
 
/* Am I at my context limits? */
-   if ((leading <= p_context) && (trailing <= p_context))
+   if ((leading <= state->p_context) && (trailing <= 
state->p_context))
break;
if (match_beginning || match_end) {
match_beginning = match_end = 0;
@@ -4574,6 +4575,7 @@ static void init_apply_state(struct apply_state *state, 
const char *prefix)
state->prefix = prefix;
state->prefix_length = state->prefix ? strlen(state->prefix) : 0;
state->line_termination = '\n';
+   state->p_context = UINT_MAX;
 
git_apply_config();
if (apply_default_whitespace)
@@ -4631,7 +4633,7 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix_)
/* Think twice before adding "--nul" synonym to this */
OPT_SET_INT('z', NULL, _termination,
N_("paths are separated with NUL character"), '\0'),
-   OPT_INTEGER('C', NULL, _context,
+   OPT_INTEGER('C', NULL, _context,
N_("ensure at least  lines of context 
match")),
{ OPTION_CALLBACK, 0, "whitespace", _option, 
N_("action"),
N_("detect new or modified lines that have whitespace 
errors"),
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 11/94] builtin/apply: move 'check' global into 'struct apply_state'

2016-05-11 Thread Christian Couder
To libify the apply functionality the 'check' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 16 +---
 1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 44ae95d..6bf103a 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -25,12 +25,15 @@ struct apply_state {
const char *prefix;
int prefix_length;
 
+   /*
+*  --check turns on checking that the working tree matches the
+*files that are being modified, but doesn't apply the patch
+*/
+   int check;
int unidiff_zero;
 };
 
 /*
- *  --check turns on checking that the working tree matches the
- *files that are being modified, but doesn't apply the patch
  *  --stat does just a diffstat, and doesn't actually apply
  *  --numstat does numeric diffstat, and doesn't actually apply
  *  --index-info shows the old and new index info for paths if available.
@@ -47,7 +50,6 @@ static int cached;
 static int diffstat;
 static int numstat;
 static int summary;
-static int check;
 static int apply = 1;
 static int apply_in_reverse;
 static int apply_with_reject;
@@ -2052,7 +2054,7 @@ static int parse_chunk(struct apply_state *state, char 
*buffer, unsigned long si
 * without metadata change.  A binary patch appears
 * empty to us here.
 */
-   if ((apply || check) &&
+   if ((apply || state->check) &&
(!patch->is_binary && !metadata_changes(patch)))
die(_("patch with only garbage at line %d"), 
state_linenr);
}
@@ -4439,7 +4441,7 @@ static int apply_patch(struct apply_state *state,
die(_("unable to read index file"));
}
 
-   if ((check || apply) &&
+   if ((state->check || apply) &&
check_patch_list(state, list) < 0 &&
!apply_with_reject)
exit(1);
@@ -4573,7 +4575,7 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix_)
N_("show number of added and deleted lines in decimal 
notation")),
OPT_BOOL(0, "summary", ,
N_("instead of applying the patch, output a summary for 
the input")),
-   OPT_BOOL(0, "check", ,
+   OPT_BOOL(0, "check", ,
N_("instead of applying the patch, see if the patch is 
applicable")),
OPT_BOOL(0, "index", _index,
N_("make sure the patch is applicable to the current 
index")),
@@ -4638,7 +4640,7 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix_)
}
if (apply_with_reject)
apply = apply_verbosely = 1;
-   if (!force_apply && (diffstat || numstat || summary || check || 
fake_ancestor))
+   if (!force_apply && (diffstat || numstat || summary || state.check || 
fake_ancestor))
apply = 0;
if (check_index && is_not_gitdir)
die(_("--index outside a repository"));
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 21/94] builtin/apply: move 'summary' global into 'struct apply_state'

2016-05-11 Thread Christian Couder
To libify the apply functionality the 'summary' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 9 +
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 887c5d0..6216723 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -39,6 +39,8 @@ struct apply_state {
/* --numstat does numeric diffstat, and doesn't actually apply */
int numstat;
 
+   int summary;
+
/*
 *  --check turns on checking that the working tree matches the
 *files that are being modified, but doesn't apply the patch
@@ -59,7 +61,6 @@ static int newfd = -1;
 
 static int state_p_value = 1;
 static int p_value_known;
-static int summary;
 static int apply = 1;
 static int no_add;
 static int threeway;
@@ -4501,7 +4502,7 @@ static int apply_patch(struct apply_state *state,
if (state->numstat)
numstat_patch_list(list);
 
-   if (summary)
+   if (state->summary)
summary_patch_list(list);
 
free_patch_list(list);
@@ -4612,7 +4613,7 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix_)
OPT_NOOP_NOARG(0, "binary"),
OPT_BOOL(0, "numstat", ,
N_("show number of added and deleted lines in decimal 
notation")),
-   OPT_BOOL(0, "summary", ,
+   OPT_BOOL(0, "summary", ,
N_("instead of applying the patch, output a summary for 
the input")),
OPT_BOOL(0, "check", ,
N_("instead of applying the patch, see if the patch is 
applicable")),
@@ -4679,7 +4680,7 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix_)
}
if (state.apply_with_reject)
apply = state.apply_verbosely = 1;
-   if (!force_apply && (state.diffstat || state.numstat || summary || 
state.check || fake_ancestor))
+   if (!force_apply && (state.diffstat || state.numstat || state.summary 
|| state.check || fake_ancestor))
apply = 0;
if (state.check_index && is_not_gitdir)
die(_("--index outside a repository"));
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 88/94] apply: don't print on stdout when be_silent is set

2016-05-11 Thread Christian Couder
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 apply.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/apply.c b/apply.c
index 5459ee1..e0fdd1d 100644
--- a/apply.c
+++ b/apply.c
@@ -4669,13 +4669,13 @@ static int apply_patch(struct apply_state *state,
goto end;
}
 
-   if (state->diffstat)
+   if (state->diffstat && !state->be_silent)
stat_patch_list(state, list);
 
-   if (state->numstat)
+   if (state->numstat && !state->be_silent)
numstat_patch_list(state, list);
 
-   if (state->summary)
+   if (state->summary && !state->be_silent)
summary_patch_list(list);
 
 end:
-- 
2.8.2.490.g3dabe57

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


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

2016-05-11 Thread Christian Couder
To libify `git apply` functionality we have to signal errors to the
caller instead of die()ing.

To do that in a compatible manner with the rest of the error handling
in "builtin/apply.c", build_fake_ancestor() should return -1 using
error() instead of calling die().

Helped-by: Eric Sunshine <sunsh...@sunshineco.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 41 ++---
 1 file changed, 26 insertions(+), 15 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 06c1c16..a2cc099 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -3890,11 +3890,12 @@ static int preimage_sha1_in_gitlink_patch(struct patch 
*p, unsigned char sha1[20
 }
 
 /* Build an index that contains the just the files needed for a 3way merge */
-static void build_fake_ancestor(struct patch *list, const char *filename)
+static int build_fake_ancestor(struct patch *list, const char *filename)
 {
struct patch *patch;
struct index_state result = { NULL };
static struct lock_file lock;
+   int res;
 
/* Once we start supporting the reverse patch, it may be
 * worth showing the new sha1 prefix, but until then...
@@ -3912,31 +3913,38 @@ static void build_fake_ancestor(struct patch *list, 
const char *filename)
if (!preimage_sha1_in_gitlink_patch(patch, sha1))
; /* ok, the textual part looks sane */
else
-   die("sha1 information is lacking or useless for 
submodule %s",
-   name);
+   return error("sha1 information is lacking or "
+"useless for submodule %s", name);
} else if (!get_sha1_blob(patch->old_sha1_prefix, sha1)) {
; /* ok */
} else if (!patch->lines_added && !patch->lines_deleted) {
/* mode-only change: update the current */
if (get_current_sha1(patch->old_name, sha1))
-   die("mode change for %s, which is not "
-   "in current HEAD", name);
+   return error("mode change for %s, which is not "
+"in current HEAD", name);
} else
-   die("sha1 information is lacking or useless "
-   "(%s).", name);
+   return error("sha1 information is lacking or useless "
+"(%s).", name);
 
ce = make_cache_entry(patch->old_mode, sha1, name, 0, 0);
if (!ce)
-   die(_("make_cache_entry failed for path '%s'"), name);
-   if (add_index_entry(, ce, ADD_CACHE_OK_TO_ADD))
-   die ("Could not add %s to temporary index", name);
+   return error(_("make_cache_entry failed for path '%s'"),
+name);
+   if (add_index_entry(, ce, ADD_CACHE_OK_TO_ADD)) {
+   free(ce);
+   return error("Could not add %s to temporary index",
+name);
+   }
}
 
hold_lock_file_for_update(, filename, LOCK_DIE_ON_ERROR);
-   if (write_locked_index(, , COMMIT_LOCK))
-   die ("Could not write temporary index to %s", filename);
-
+   res = write_locked_index(, , COMMIT_LOCK);
discard_index();
+
+   if (res)
+   return error("Could not write temporary index to %s", filename);
+
+   return 0;
 }
 
 static void stat_patch_list(struct apply_state *state, struct patch *patch)
@@ -4475,8 +4483,11 @@ static int apply_patch(struct apply_state *state,
goto end;
}
 
-   if (state->fake_ancestor)
-   build_fake_ancestor(list, state->fake_ancestor);
+   if (state->fake_ancestor &&
+   build_fake_ancestor(list, state->fake_ancestor)) {
+   res = -1;
+   goto end;
+   }
 
if (state->diffstat)
stat_patch_list(state, list);
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 40/94] builtin/apply: move 'ws_error_action' into 'struct apply_state'

2016-05-11 Thread Christian Couder
To libify the apply functionality the 'ws_error_action' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 62 +++--
 1 file changed, 34 insertions(+), 28 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index e68fd2c..10d45c7 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -21,6 +21,13 @@
 #include "ll-merge.h"
 #include "rerere.h"
 
+enum ws_error_action {
+   nowarn_ws_error,
+   warn_on_ws_error,
+   die_on_ws_error,
+   correct_ws_error
+};
+
 struct apply_state {
const char *prefix;
int prefix_length;
@@ -71,6 +78,8 @@ struct apply_state {
int whitespace_error;
int squelch_whitespace_errors;
int applied_after_fixing_ws;
+
+   enum ws_error_action ws_error_action;
 };
 
 static int newfd = -1;
@@ -80,12 +89,6 @@ static const char * const apply_usage[] = {
NULL
 };
 
-static enum ws_error_action {
-   nowarn_ws_error,
-   warn_on_ws_error,
-   die_on_ws_error,
-   correct_ws_error
-} ws_error_action = warn_on_ws_error;
 
 static enum ws_ignore {
ignore_ws_none,
@@ -96,28 +99,28 @@ static enum ws_ignore {
 static void parse_whitespace_option(struct apply_state *state, const char 
*option)
 {
if (!option) {
-   ws_error_action = warn_on_ws_error;
+   state->ws_error_action = warn_on_ws_error;
return;
}
if (!strcmp(option, "warn")) {
-   ws_error_action = warn_on_ws_error;
+   state->ws_error_action = warn_on_ws_error;
return;
}
if (!strcmp(option, "nowarn")) {
-   ws_error_action = nowarn_ws_error;
+   state->ws_error_action = nowarn_ws_error;
return;
}
if (!strcmp(option, "error")) {
-   ws_error_action = die_on_ws_error;
+   state->ws_error_action = die_on_ws_error;
return;
}
if (!strcmp(option, "error-all")) {
-   ws_error_action = die_on_ws_error;
+   state->ws_error_action = die_on_ws_error;
state->squelch_whitespace_errors = 0;
return;
}
if (!strcmp(option, "strip") || !strcmp(option, "fix")) {
-   ws_error_action = correct_ws_error;
+   state->ws_error_action = correct_ws_error;
return;
}
die(_("unrecognized whitespace option '%s'"), option);
@@ -141,7 +144,7 @@ static void parse_ignorewhitespace_option(const char 
*option)
 static void set_default_whitespace_mode(struct apply_state *state)
 {
if (!state->whitespace_option && !apply_default_whitespace)
-   ws_error_action = (state->apply ? warn_on_ws_error : 
nowarn_ws_error);
+   state->ws_error_action = (state->apply ? warn_on_ws_error : 
nowarn_ws_error);
 }
 
 /*
@@ -1676,12 +1679,12 @@ static int parse_fragment(struct apply_state *state,
leading++;
trailing++;
if (!state->apply_in_reverse &&
-   ws_error_action == correct_ws_error)
+   state->ws_error_action == correct_ws_error)
check_whitespace(state, line, len, 
patch->ws_rule);
break;
case '-':
if (state->apply_in_reverse &&
-   ws_error_action != nowarn_ws_error)
+   state->ws_error_action != nowarn_ws_error)
check_whitespace(state, line, len, 
patch->ws_rule);
deleted++;
oldlines--;
@@ -1689,7 +1692,7 @@ static int parse_fragment(struct apply_state *state,
break;
case '+':
if (!state->apply_in_reverse &&
-   ws_error_action != nowarn_ws_error)
+   state->ws_error_action != nowarn_ws_error)
check_whitespace(state, line, len, 
patch->ws_rule);
added++;
newlines--;
@@ -2402,7 +2405,8 @@ static int line_by_line_fuzzy_match(struct image *img,
return 1;
 }
 
-static int match_fragment(struct image *img,
+static int match_fragment(struct apply_state *state,
+ struct image *img,
  struct image *preimage,
  struct image *postimage,
  unsigned long try,
@@ -2423,7 +2427,7 @@ static int match_fragm

[PATCH v2 29/94] builtin/apply: move 'patch_input_file' global into 'struct apply_state'

2016-05-11 Thread Christian Couder
To libify the apply functionality the 'patch_input_file' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 27 +--
 1 file changed, 17 insertions(+), 10 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index a3db284..e43da9c 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -44,6 +44,7 @@ struct apply_state {
int threeway;
int no_add;
const char *fake_ancestor;
+   const char *patch_input_file;
 
/*
 *  --check turns on checking that the working tree matches the
@@ -88,7 +89,6 @@ static enum ws_ignore {
 } ws_ignore_action = ignore_ws_none;
 
 
-static const char *patch_input_file;
 static struct strbuf root = STRBUF_INIT;
 
 static void parse_whitespace_option(const char *option)
@@ -1534,7 +1534,11 @@ static int find_header(struct apply_state *state,
return -1;
 }
 
-static void record_ws_error(unsigned result, const char *line, int len, int 
linenr)
+static void record_ws_error(struct apply_state *state,
+   unsigned result,
+   const char *line,
+   int len,
+   int linenr)
 {
char *err;
 
@@ -1548,15 +1552,18 @@ static void record_ws_error(unsigned result, const char 
*line, int len, int line
 
err = whitespace_error_string(result);
fprintf(stderr, "%s:%d: %s.\n%.*s\n",
-   patch_input_file, linenr, err, len, line);
+   state->patch_input_file, linenr, err, len, line);
free(err);
 }
 
-static void check_whitespace(const char *line, int len, unsigned ws_rule)
+static void check_whitespace(struct apply_state *state,
+const char *line,
+int len,
+unsigned ws_rule)
 {
unsigned result = ws_check(line + 1, len - 1, ws_rule);
 
-   record_ws_error(result, line + 1, len - 2, state_linenr);
+   record_ws_error(state, result, line + 1, len - 2, state_linenr);
 }
 
 /*
@@ -1611,12 +1618,12 @@ static int parse_fragment(struct apply_state *state,
trailing++;
if (!state->apply_in_reverse &&
ws_error_action == correct_ws_error)
-   check_whitespace(line, len, patch->ws_rule);
+   check_whitespace(state, line, len, 
patch->ws_rule);
break;
case '-':
if (state->apply_in_reverse &&
ws_error_action != nowarn_ws_error)
-   check_whitespace(line, len, patch->ws_rule);
+   check_whitespace(state, line, len, 
patch->ws_rule);
deleted++;
oldlines--;
trailing = 0;
@@ -1624,7 +1631,7 @@ static int parse_fragment(struct apply_state *state,
case '+':
if (!state->apply_in_reverse &&
ws_error_action != nowarn_ws_error)
-   check_whitespace(line, len, patch->ws_rule);
+   check_whitespace(state, line, len, 
patch->ws_rule);
added++;
newlines--;
trailing = 0;
@@ -2913,7 +2920,7 @@ static int apply_one_fragment(struct apply_state *state,
preimage.nr + applied_pos >= img->nr &&
(ws_rule & WS_BLANK_AT_EOF) &&
ws_error_action != nowarn_ws_error) {
-   record_ws_error(WS_BLANK_AT_EOF, "+", 1,
+   record_ws_error(state, WS_BLANK_AT_EOF, "+", 1,
found_new_blank_lines_at_end);
if (ws_error_action == correct_ws_error) {
while (new_blank_lines_at_end--)
@@ -4436,7 +4443,7 @@ static int apply_patch(struct apply_state *state,
struct patch *list = NULL, **listp = 
int skipped_patch = 0;
 
-   patch_input_file = filename;
+   state->patch_input_file = filename;
read_patch_file(, fd);
offset = 0;
while (offset < buf.len) {
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 60/94] apply: make init_apply_state() return -1 instead of exit()ing

2016-05-11 Thread Christian Couder
To libify `git apply` functionality we have to signal errors to the
caller instead of exit()ing.

To do that in a compatible manner with the rest of the error handling
in "builtin/apply.c", init_apply_state() should return -1 using
error() instead of calling exit().

Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 apply.c | 11 ++-
 apply.h |  6 +++---
 builtin/apply.c |  3 ++-
 3 files changed, 11 insertions(+), 9 deletions(-)

diff --git a/apply.c b/apply.c
index 508ea64..1e2b802 100644
--- a/apply.c
+++ b/apply.c
@@ -55,9 +55,9 @@ int parse_ignorewhitespace_option(struct apply_state *state,
return error(_("unrecognized whitespace ignore option '%s'"), option);
 }
 
-void init_apply_state(struct apply_state *state,
- const char *prefix,
- struct lock_file *lock_file)
+int init_apply_state(struct apply_state *state,
+const char *prefix,
+struct lock_file *lock_file)
 {
memset(state, 0, sizeof(*state));
state->prefix = prefix;
@@ -76,8 +76,9 @@ void init_apply_state(struct apply_state *state,
 
git_apply_config();
if (apply_default_whitespace && parse_whitespace_option(state, 
apply_default_whitespace))
-   exit(1);
+   return -1;
if (apply_default_ignorewhitespace && 
parse_ignorewhitespace_option(state, apply_default_ignorewhitespace))
-   exit(1);
+   return -1;
+   return 0;
 }
 
diff --git a/apply.h b/apply.h
index 0f77f4d..f3b2ae4 100644
--- a/apply.h
+++ b/apply.h
@@ -117,8 +117,8 @@ extern int parse_whitespace_option(struct apply_state 
*state,
 extern int parse_ignorewhitespace_option(struct apply_state *state,
 const char *option);
 
-extern void init_apply_state(struct apply_state *state,
-const char *prefix,
-struct lock_file *lock_file);
+extern int init_apply_state(struct apply_state *state,
+   const char *prefix,
+   struct lock_file *lock_file);
 
 #endif
diff --git a/builtin/apply.c b/builtin/apply.c
index 805c707..b31f9eb 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4724,7 +4724,8 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix)
OPT_END()
};
 
-   init_apply_state(, prefix, NULL);
+   if (init_apply_state(, prefix, NULL))
+   exit(1);
 
argc = parse_options(argc, argv, state.prefix, builtin_apply_options,
apply_usage, 0);
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 86/94] apply: add 'be_silent' variable to 'struct apply_state'

2016-05-11 Thread Christian Couder
This variable should prevent anything to be printed on both stderr
and stdout.

Let's not take care of stdout and apply_verbosely for now though,
as that will be taken care of in following patches.

Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 apply.c | 43 +--
 apply.h |  1 +
 2 files changed, 30 insertions(+), 14 deletions(-)

diff --git a/apply.c b/apply.c
index 7480ae8..f69a61a 100644
--- a/apply.c
+++ b/apply.c
@@ -1600,8 +1600,9 @@ static void record_ws_error(struct apply_state *state,
return;
 
err = whitespace_error_string(result);
-   fprintf(stderr, "%s:%d: %s.\n%.*s\n",
-   state->patch_input_file, linenr, err, len, line);
+   if (!state->be_silent)
+   fprintf(stderr, "%s:%d: %s.\n%.*s\n",
+   state->patch_input_file, linenr, err, len, line);
free(err);
 }
 
@@ -1796,7 +1797,7 @@ static int parse_single_patch(struct apply_state *state,
return error(_("new file %s depends on old contents"), 
patch->new_name);
if (0 < patch->is_delete && newlines)
return error(_("deleted file %s still has contents"), 
patch->old_name);
-   if (!patch->is_delete && !newlines && context)
+   if (!patch->is_delete && !newlines && context && !state->be_silent)
fprintf_ln(stderr,
   _("** warning: "
 "file %s becomes empty but is not deleted"),
@@ -3020,8 +3021,8 @@ static int apply_one_fragment(struct apply_state *state,
 * Warn if it was necessary to reduce the number
 * of context lines.
 */
-   if ((leading != frag->leading) ||
-   (trailing != frag->trailing))
+   if ((leading != frag->leading ||
+trailing != frag->trailing) && !state->be_silent)
fprintf_ln(stderr, _("Context reduced to (%ld/%ld)"
 " to apply fragment at %d"),
   leading, trailing, applied_pos+1);
@@ -3518,7 +3519,8 @@ static int try_threeway(struct apply_state *state,
 read_blob_object(, pre_sha1, patch->old_mode))
return error("repository lacks the necessary blob to fall back 
on 3-way merge.");
 
-   fprintf(stderr, "Falling back to three-way merge...\n");
+   if (!state->be_silent)
+   fprintf(stderr, "Falling back to three-way merge...\n");
 
img = strbuf_detach(, );
prepare_image(_image, img, len, 1);
@@ -3548,7 +3550,9 @@ static int try_threeway(struct apply_state *state,
status = three_way_merge(image, patch->new_name,
 pre_sha1, our_sha1, post_sha1);
if (status < 0) {
-   fprintf(stderr, "Failed to fall back on three-way merge...\n");
+   if (!state->be_silent)
+   fprintf(stderr,
+   "Failed to fall back on three-way merge...\n");
return status;
}
 
@@ -3560,9 +3564,15 @@ static int try_threeway(struct apply_state *state,
hashcpy(patch->threeway_stage[0].hash, pre_sha1);
hashcpy(patch->threeway_stage[1].hash, our_sha1);
hashcpy(patch->threeway_stage[2].hash, post_sha1);
-   fprintf(stderr, "Applied patch to '%s' with conflicts.\n", 
patch->new_name);
+   if (!state->be_silent)
+   fprintf(stderr,
+   "Applied patch to '%s' with conflicts.\n",
+   patch->new_name);
} else {
-   fprintf(stderr, "Applied patch to '%s' cleanly.\n", 
patch->new_name);
+   if (!state->be_silent)
+   fprintf(stderr,
+   "Applied patch to '%s' cleanly.\n",
+   patch->new_name);
}
return 0;
 }
@@ -4461,7 +4471,8 @@ static int write_out_one_reject(struct apply_state 
*state, struct patch *patch)
"Applying patch %%s with %d rejects...",
cnt),
cnt);
-   say_patch_name(stderr, sb.buf, patch);
+   if (!state->be_silent)
+   say_patch_name(stderr, sb.buf, patch);
strbuf_release();
 
cnt = strlen(patch->new_name);
@@ -4488,10 +4499,12 @@ static int write_out_one_reject(struct apply_state 
*state, struct patch *patch)
 frag;
 cnt++, frag = frag->next) {

[PATCH v2 85/94] write_or_die: use warning() instead of fprintf(stderr, ...)

2016-05-11 Thread Christian Couder
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 write_or_die.c | 6 ++
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/write_or_die.c b/write_or_die.c
index 49e80aa..c29f677 100644
--- a/write_or_die.c
+++ b/write_or_die.c
@@ -87,8 +87,7 @@ int write_or_whine_pipe(int fd, const void *buf, size_t 
count, const char *msg)
 {
if (write_in_full(fd, buf, count) < 0) {
check_pipe(errno);
-   fprintf(stderr, "%s: write error (%s)\n",
-   msg, strerror(errno));
+   warning("%s: write error (%s)\n", msg, strerror(errno));
return 0;
}
 
@@ -98,8 +97,7 @@ int write_or_whine_pipe(int fd, const void *buf, size_t 
count, const char *msg)
 int write_or_whine(int fd, const void *buf, size_t count, const char *msg)
 {
if (write_in_full(fd, buf, count) < 0) {
-   fprintf(stderr, "%s: write error (%s)\n",
-   msg, strerror(errno));
+   warning("%s: write error (%s)\n", msg, strerror(errno));
return 0;
}
 
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 92/94] am: use be_silent in 'struct apply_state' to shut up applying patches

2016-05-11 Thread Christian Couder
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/am.c | 29 -
 1 file changed, 8 insertions(+), 21 deletions(-)

diff --git a/builtin/am.c b/builtin/am.c
index cc66a48..c158c4d 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -1526,7 +1526,6 @@ static int run_apply(const struct am_state *state, const 
char *index_file)
struct argv_array apply_paths = ARGV_ARRAY_INIT;
struct argv_array apply_opts = ARGV_ARRAY_INIT;
struct apply_state apply_state;
-   int save_stdout_fd, save_stderr_fd;
int res, opts_left;
char *save_index_file;
static struct lock_file lock_file;
@@ -1560,18 +1559,6 @@ static int run_apply(const struct am_state *state, const 
char *index_file)
OPT_END()
};
 
-   /*
-* If we are allowed to fall back on 3-way merge, don't give false
-* errors during the initial attempt.
-*/
-
-   if (state->threeway && !index_file) {
-   save_stdout_fd = dup(1);
-   dup_devnull(1);
-   save_stderr_fd = dup(2);
-   dup_devnull(2);
-   }
-
if (index_file) {
save_index_file = get_index_file();
set_index_file((char *)index_file);
@@ -1594,6 +1581,14 @@ static int run_apply(const struct am_state *state, const 
char *index_file)
else
apply_state.check_index = 1;
 
+   /*
+* If we are allowed to fall back on 3-way merge, don't give false
+* errors during the initial attempt.
+*/
+
+   if (state->threeway && !index_file)
+   apply_state.be_silent = 1;
+
if (check_apply_state(_state, 0))
die("check_apply_state() failed");
 
@@ -1601,14 +1596,6 @@ static int run_apply(const struct am_state *state, const 
char *index_file)
 
res = apply_all_patches(_state, apply_paths.argc, 
apply_paths.argv, 0);
 
-   /* Restore stdout and stderr */
-   if (state->threeway && !index_file) {
-   dup2(save_stdout_fd, 1);
-   close(save_stdout_fd);
-   dup2(save_stderr_fd, 2);
-   close(save_stderr_fd);
-   }
-
if (index_file)
set_index_file(save_index_file);
 
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 66/94] builtin/apply: make gitdiff_*() return -1 on error

2016-05-11 Thread Christian Couder
To libify `git apply` functionality we have to signal errors to the
caller instead of die()ing.

To do that in a compatible manner with the rest of the error handling
in "builtin/apply.c", gitdiff_*() functions should return -1 using
error() instead of calling die().

A previous patch made it possible for gitdiff_*() functions to
return -1 in case of error. Let's take advantage of that to
make gitdiff_verify_name() return -1 on error, and to have
gitdiff_oldname() and gitdiff_newname() directly return
what gitdiff_verify_name() returns.

Helped-by: Nguyễn Thái Ngọc Duy <pclo...@gmail.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 40 +---
 1 file changed, 21 insertions(+), 19 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index b3a9c2e..42b0a24 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -827,54 +827,56 @@ static int gitdiff_hdrend(struct apply_state *state,
 #define DIFF_OLD_NAME 0
 #define DIFF_NEW_NAME 1
 
-static void gitdiff_verify_name(struct apply_state *state,
-   const char *line,
-   int isnull,
-   char **name,
-   int side)
+static int gitdiff_verify_name(struct apply_state *state,
+  const char *line,
+  int isnull,
+  char **name,
+  int side)
 {
if (!*name && !isnull) {
*name = find_name(state, line, NULL, state->p_value, TERM_TAB);
-   return;
+   return 0;
}
 
if (*name) {
int len = strlen(*name);
char *another;
if (isnull)
-   die(_("git apply: bad git-diff - expected /dev/null, 
got %s on line %d"),
-   *name, state->linenr);
+   return error(_("git apply: bad git-diff - expected 
/dev/null, got %s on line %d"),
+*name, state->linenr);
another = find_name(state, line, NULL, state->p_value, 
TERM_TAB);
-   if (!another || memcmp(another, *name, len + 1))
-   die((side == DIFF_NEW_NAME) ?
+   if (!another || memcmp(another, *name, len + 1)) {
+   free(another);
+   return error((side == DIFF_NEW_NAME) ?
_("git apply: bad git-diff - inconsistent new 
filename on line %d") :
_("git apply: bad git-diff - inconsistent old 
filename on line %d"), state->linenr);
+   }
free(another);
} else {
/* expect "/dev/null" */
if (memcmp("/dev/null", line, 9) || line[9] != '\n')
-   die(_("git apply: bad git-diff - expected /dev/null on 
line %d"), state->linenr);
+   return error(_("git apply: bad git-diff - expected 
/dev/null on line %d"), state->linenr);
}
+
+   return 0;
 }
 
 static int gitdiff_oldname(struct apply_state *state,
   const char *line,
   struct patch *patch)
 {
-   gitdiff_verify_name(state, line,
-   patch->is_new, >old_name,
-   DIFF_OLD_NAME);
-   return 0;
+   return gitdiff_verify_name(state, line,
+  patch->is_new, >old_name,
+  DIFF_OLD_NAME);
 }
 
 static int gitdiff_newname(struct apply_state *state,
   const char *line,
   struct patch *patch)
 {
-   gitdiff_verify_name(state, line,
-   patch->is_delete, >new_name,
-   DIFF_NEW_NAME);
-   return 0;
+   return gitdiff_verify_name(state, line,
+  patch->is_delete, >new_name,
+  DIFF_NEW_NAME);
 }
 
 static int gitdiff_oldmode(struct apply_state *state,
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 83/94] environment: add set_index_file()

2016-05-11 Thread Christian Couder
Introduce set_index_file() to be able to temporarily change the index file.

It should be used like this:

/* Save current index file */
old_index_file = get_index_file();
set_index_file((char *)tmp_index_file);

/* Do stuff that will use tmp_index_file as the index file */
...

/* When finished reset the index file */
set_index_file(old_index_file);

Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 cache.h   |  1 +
 environment.c | 10 ++
 2 files changed, 11 insertions(+)

diff --git a/cache.h b/cache.h
index 160f8e3..452d0ec 100644
--- a/cache.h
+++ b/cache.h
@@ -461,6 +461,7 @@ extern int is_inside_work_tree(void);
 extern const char *get_git_dir(void);
 extern const char *get_git_common_dir(void);
 extern char *get_object_directory(void);
+extern void set_index_file(char *index_file);
 extern char *get_index_file(void);
 extern char *get_graft_file(void);
 extern int set_git_dir(const char *path);
diff --git a/environment.c b/environment.c
index 57acb2f..9676d2a 100644
--- a/environment.c
+++ b/environment.c
@@ -290,6 +290,16 @@ int odb_pack_keep(char *name, size_t namesz, const 
unsigned char *sha1)
return open(name, O_RDWR|O_CREAT|O_EXCL, 0600);
 }
 
+/*
+ * Temporarily change the index file.
+ * Please save the current index file using get_index_file() before changing
+ * the index file. And when finished, reset it to the saved value.
+ */
+void set_index_file(char *index_file)
+{
+   git_index_file = index_file;
+}
+
 char *get_index_file(void)
 {
if (!git_index_file)
-- 
2.8.2.490.g3dabe57

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


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

2016-05-11 Thread Christian Couder
To libify `git apply` functionality we have to signal errors to the
caller instead of exit()ing.

To do that in a compatible manner with the rest of the error handling
in "builtin/apply.c", write_out_results() should return -1 instead of
calling exit().

Helped-by: Eric Sunshine <sunsh...@sunshineco.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 27 +--
 1 file changed, 21 insertions(+), 6 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index f06bf16..97bc704 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4373,6 +4373,12 @@ static int write_out_one_reject(struct apply_state 
*state, struct patch *patch)
return -1;
 }
 
+/*
+ * Returns:
+ *  -1 if an error happened
+ *   0 if the patch applied cleanly
+ *   1 if the patch did not apply cleanly
+ */
 static int write_out_results(struct apply_state *state, struct patch *list)
 {
int phase;
@@ -4386,8 +4392,10 @@ static int write_out_results(struct apply_state *state, 
struct patch *list)
if (l->rejected)
errs = 1;
else {
-   if (write_out_one_result(state, l, phase))
-   exit(1);
+   if (write_out_one_result(state, l, phase)) {
+   string_list_clear(, 0);
+   return -1;
+   }
if (phase == 1) {
if (write_out_one_reject(state, l))
errs = 1;
@@ -4497,10 +4505,17 @@ static int apply_patch(struct apply_state *state,
goto end;
}
 
-   if (state->apply && write_out_results(state, list)) {
-   /* with --3way, we still need to write the index out */
-   res = state->apply_with_reject ? -1 : 1;
-   goto end;
+   if (state->apply) {
+   int write_res = write_out_results(state, list);
+   if (write_res < 0) {
+   res = -1;
+   goto end;
+   }
+   if (write_res > 0) {
+   /* with --3way, we still need to write the index out */
+   res = state->apply_with_reject ? -1 : 1;
+   goto end;
+   }
}
 
if (state->fake_ancestor &&
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 67/94] builtin/apply: change die_on_unsafe_path() to check_unsafe_path()

2016-05-11 Thread Christian Couder
To libify `git apply` functionality we have to signal errors to the
caller instead of die()ing.

To do that in a compatible manner with the rest of the error handling
in "builtin/apply.c", die_on_unsafe_path() should return -1 using
error() instead of calling die(), so while doing that let's change
its name to check_unsafe_path().

Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 11 ++-
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 42b0a24..06c1c16 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -3698,7 +3698,7 @@ static int path_is_beyond_symlink(struct apply_state 
*state, const char *name_)
return ret;
 }
 
-static void die_on_unsafe_path(struct patch *patch)
+static int check_unsafe_path(struct patch *patch)
 {
const char *old_name = NULL;
const char *new_name = NULL;
@@ -3710,9 +3710,10 @@ static void die_on_unsafe_path(struct patch *patch)
new_name = patch->new_name;
 
if (old_name && !verify_path(old_name))
-   die(_("invalid path '%s'"), old_name);
+   return error(_("invalid path '%s'"), old_name);
if (new_name && !verify_path(new_name))
-   die(_("invalid path '%s'"), new_name);
+   return error(_("invalid path '%s'"), new_name);
+   return 0;
 }
 
 /*
@@ -3802,8 +3803,8 @@ static int check_patch(struct apply_state *state, struct 
patch *patch)
}
}
 
-   if (!state->unsafe_paths)
-   die_on_unsafe_path(patch);
+   if (!state->unsafe_paths && check_unsafe_path(patch))
+   return -1;
 
/*
 * An attempt to read from or delete a path that is beyond a
-- 
2.8.2.490.g3dabe57

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


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

2016-05-11 Thread Christian Couder
To libify `git apply` functionality we have to signal errors to the
caller instead of die()ing.

To do that in a compatible manner with the rest of the error handling
in "builtin/apply.c", add_conflicted_stages_file() should return -1
using error() instead of calling die().

Helped-by: Eric Sunshine <sunsh...@sunshineco.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 20 +---
 1 file changed, 13 insertions(+), 7 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 52f36c2..ca3502f 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4214,7 +4214,7 @@ static void create_one_file(struct apply_state *state,
die_errno(_("unable to write file '%s' mode %o"), path, mode);
 }
 
-static void add_conflicted_stages_file(struct apply_state *state,
+static int add_conflicted_stages_file(struct apply_state *state,
   struct patch *patch)
 {
int stage, namelen;
@@ -4222,7 +4222,7 @@ static void add_conflicted_stages_file(struct apply_state 
*state,
struct cache_entry *ce;
 
if (!state->update_index)
-   return;
+   return 0;
namelen = strlen(patch->new_name);
ce_size = cache_entry_size(namelen);
mode = patch->new_mode ? patch->new_mode : (S_IFREG | 0644);
@@ -4237,9 +4237,14 @@ static void add_conflicted_stages_file(struct 
apply_state *state,
ce->ce_flags = create_ce_flags(stage);
ce->ce_namelen = namelen;
hashcpy(ce->sha1, patch->threeway_stage[stage - 1].hash);
-   if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)
-   die(_("unable to add cache entry for %s"), 
patch->new_name);
+   if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0) {
+   free(ce);
+   return error(_("unable to add cache entry for %s"),
+patch->new_name);
+   }
}
+
+   return 0;
 }
 
 static void create_file(struct apply_state *state, struct patch *patch)
@@ -4253,9 +4258,10 @@ static void create_file(struct apply_state *state, 
struct patch *patch)
mode = S_IFREG | 0644;
create_one_file(state, path, mode, buf, size);
 
-   if (patch->conflicted_threeway)
-   add_conflicted_stages_file(state, patch);
-   else
+   if (patch->conflicted_threeway) {
+   if (add_conflicted_stages_file(state, patch))
+   exit(1);
+   } else
add_index_file(state, path, mode, buf, size);
 }
 
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 91/94] apply: change error_routine when be_silent is set

2016-05-11 Thread Christian Couder
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 apply.c | 29 +
 apply.h |  3 +++
 2 files changed, 28 insertions(+), 4 deletions(-)

diff --git a/apply.c b/apply.c
index e0fdd1d..1dafc82 100644
--- a/apply.c
+++ b/apply.c
@@ -100,6 +100,11 @@ int init_apply_state(struct apply_state *state,
return 0;
 }
 
+static void mute_routine(const char *bla, va_list params)
+{
+   /* do nothing */
+}
+
 int check_apply_state(struct apply_state *state, int force_apply)
 {
int is_not_gitdir = !startup_info->have_repository;
@@ -132,6 +137,13 @@ int check_apply_state(struct apply_state *state, int 
force_apply)
if (state->be_silent && state->apply_verbosely)
return error(_("incompatible internal 'be_silent' and 
'apply_verbosely' flags"));
 
+   if (state->be_silent) {
+   state->saved_error_routine = get_error_routine();
+   state->saved_warn_routine = get_warn_routine();
+   set_error_routine(mute_routine);
+   set_warn_routine(mute_routine);
+   }
+
return 0;
 }
 
@@ -4750,6 +4762,7 @@ int apply_all_patches(struct apply_state *state,
 {
int i;
int res;
+   int retval = -1;
int errs = 0;
int read_stdin = 1;
 
@@ -4822,17 +4835,25 @@ int apply_all_patches(struct apply_state *state,
if (state->update_index) {
res = write_locked_index(_index, state->lock_file, 
COMMIT_LOCK);
state->newfd = -1;
-   if (res)
-   return error(_("Unable to write new index file"));
+   if (res) {
+   error(_("Unable to write new index file"));
+   goto rollback_end;
+   }
}
 
-   return !!errs;
+   retval = !!errs;
 
 rollback_end:
if (state->newfd >= 0) {
rollback_lock_file(state->lock_file);
state->newfd = -1;
}
-   return -1;
+
+   if (state->be_silent) {
+   set_error_routine(state->saved_error_routine);
+   set_warn_routine(state->saved_warn_routine);
+   }
+
+   return retval;
 }
 
diff --git a/apply.h b/apply.h
index 2dd3706..029b79f 100644
--- a/apply.h
+++ b/apply.h
@@ -46,6 +46,9 @@ struct apply_state {
int apply_verbosely;
int be_silent;
 
+   void (*saved_error_routine)(const char *err, va_list params);
+   void (*saved_warn_routine)(const char *warn, va_list params);
+
/* --cached updates only the cache without ever touching the working 
tree. */
int cached;
 
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 89/94] usage: add set_warn_routine()

2016-05-11 Thread Christian Couder
There are already set_die_routine() and set_error_routine(),
so let's add set_warn_routine() as this will be needed in a
following commit.

Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 git-compat-util.h | 1 +
 usage.c   | 5 +
 2 files changed, 6 insertions(+)

diff --git a/git-compat-util.h b/git-compat-util.h
index 1f8b5f3..987eb99 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -438,6 +438,7 @@ static inline int const_error(void)
 
 extern void set_die_routine(NORETURN_PTR void (*routine)(const char *err, 
va_list params));
 extern void set_error_routine(void (*routine)(const char *err, va_list 
params));
+extern void set_warn_routine(void (*routine)(const char *warn, va_list 
params));
 extern void set_die_is_recursing_routine(int (*routine)(void));
 extern void set_error_handle(FILE *);
 
diff --git a/usage.c b/usage.c
index 82ff131..8fbedb3 100644
--- a/usage.c
+++ b/usage.c
@@ -70,6 +70,11 @@ void set_error_routine(void (*routine)(const char *err, 
va_list params))
error_routine = routine;
 }
 
+void set_warn_routine(void (*routine)(const char *warn, va_list params))
+{
+   warn_routine = routine;
+}
+
 void set_die_is_recursing_routine(int (*routine)(void))
 {
die_is_recursing = routine;
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 77/94] builtin/apply: rename option parsing functions

2016-05-11 Thread Christian Couder
As these functions are going to be part of the libified
apply api, let's give them a name that is more specific
to the apply api.

Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 40 
 1 file changed, 20 insertions(+), 20 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 8c31617..f05dc96 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4565,16 +4565,16 @@ end:
return res;
 }
 
-static int option_parse_exclude(const struct option *opt,
-   const char *arg, int unset)
+static int apply_option_parse_exclude(const struct option *opt,
+ const char *arg, int unset)
 {
struct apply_state *state = opt->value;
add_name_limit(state, arg, 1);
return 0;
 }
 
-static int option_parse_include(const struct option *opt,
-   const char *arg, int unset)
+static int apply_option_parse_include(const struct option *opt,
+ const char *arg, int unset)
 {
struct apply_state *state = opt->value;
add_name_limit(state, arg, 0);
@@ -4582,9 +4582,9 @@ static int option_parse_include(const struct option *opt,
return 0;
 }
 
-static int option_parse_p(const struct option *opt,
- const char *arg,
- int unset)
+static int apply_option_parse_p(const struct option *opt,
+   const char *arg,
+   int unset)
 {
struct apply_state *state = opt->value;
state->p_value = atoi(arg);
@@ -4592,8 +4592,8 @@ static int option_parse_p(const struct option *opt,
return 0;
 }
 
-static int option_parse_space_change(const struct option *opt,
-const char *arg, int unset)
+static int apply_option_parse_space_change(const struct option *opt,
+  const char *arg, int unset)
 {
struct apply_state *state = opt->value;
if (unset)
@@ -4603,8 +4603,8 @@ static int option_parse_space_change(const struct option 
*opt,
return 0;
 }
 
-static int option_parse_whitespace(const struct option *opt,
-  const char *arg, int unset)
+static int apply_option_parse_whitespace(const struct option *opt,
+const char *arg, int unset)
 {
struct apply_state *state = opt->value;
state->whitespace_option = arg;
@@ -4613,8 +4613,8 @@ static int option_parse_whitespace(const struct option 
*opt,
return 0;
 }
 
-static int option_parse_directory(const struct option *opt,
- const char *arg, int unset)
+static int apply_option_parse_directory(const struct option *opt,
+   const char *arg, int unset)
 {
struct apply_state *state = opt->value;
strbuf_reset(>root);
@@ -4714,13 +4714,13 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix)
struct option builtin_apply_options[] = {
{ OPTION_CALLBACK, 0, "exclude", , N_("path"),
N_("don't apply changes matching the given path"),
-   0, option_parse_exclude },
+   0, apply_option_parse_exclude },
{ OPTION_CALLBACK, 0, "include", , N_("path"),
N_("apply changes matching the given path"),
-   0, option_parse_include },
+   0, apply_option_parse_include },
{ OPTION_CALLBACK, 'p', NULL, , N_("num"),
N_("remove  leading slashes from traditional diff 
paths"),
-   0, option_parse_p },
+   0, apply_option_parse_p },
OPT_BOOL(0, "no-add", _add,
N_("ignore additions made by the patch")),
OPT_BOOL(0, "stat", ,
@@ -4752,13 +4752,13 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix)
N_("ensure at least  lines of context 
match")),
{ OPTION_CALLBACK, 0, "whitespace", , N_("action"),
N_("detect new or modified lines that have whitespace 
errors"),
-   0, option_parse_whitespace },
+   0, apply_option_parse_whitespace },
{ OPTION_CALLBACK, 0, "ignore-space-change", , NULL,
N_("ignore changes in whitespace when finding context"),
-   PARSE_OPT_NOARG, option_parse_space_change },
+   PARSE_OPT_NOARG, apply_option_parse_space_change },
   

[PATCH v2 82/94] apply: roll back index lock file in case of error

2016-05-11 Thread Christian Couder
According to the lockfile API, when finished with a lockfile, one
should either commit it or roll it back.

This is even more important now that the same lockfile can be passed
to init_apply_state() many times to be reused by series of calls to
the apply lib functions.

Helped-by: Johannes Schindelin <johannes.schinde...@gmx.de>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 apply.c | 31 +--
 1 file changed, 21 insertions(+), 10 deletions(-)

diff --git a/apply.c b/apply.c
index 3285bf7..7480ae8 100644
--- a/apply.c
+++ b/apply.c
@@ -4739,7 +4739,7 @@ int apply_all_patches(struct apply_state *state,
if (!strcmp(arg, "-")) {
res = apply_patch(state, 0, "", options);
if (res < 0)
-   return -1;
+   goto rollback_end;
errs |= res;
read_stdin = 0;
continue;
@@ -4749,21 +4749,23 @@ int apply_all_patches(struct apply_state *state,
  arg);
 
fd = open(arg, O_RDONLY);
-   if (fd < 0)
-   return error(_("can't open patch '%s': %s"), arg, 
strerror(errno));
+   if (fd < 0) {
+   error(_("can't open patch '%s': %s"), arg, 
strerror(errno));
+   goto rollback_end;
+   }
read_stdin = 0;
set_default_whitespace_mode(state);
res = apply_patch(state, fd, arg, options);
close(fd);
if (res < 0)
-   return -1;
+   goto rollback_end;
errs |= res;
}
set_default_whitespace_mode(state);
if (read_stdin) {
res = apply_patch(state, 0, "", options);
if (res < 0)
-   return -1;
+   goto rollback_end;
errs |= res;
}
 
@@ -4777,11 +4779,13 @@ int apply_all_patches(struct apply_state *state,
   squelched),
squelched);
}
-   if (state->ws_error_action == die_on_ws_error)
-   return error(Q_("%d line adds whitespace errors.",
-   "%d lines add whitespace errors.",
-   state->whitespace_error),
-state->whitespace_error);
+   if (state->ws_error_action == die_on_ws_error) {
+   error(Q_("%d line adds whitespace errors.",
+"%d lines add whitespace errors.",
+state->whitespace_error),
+ state->whitespace_error);
+   goto rollback_end;
+   }
if (state->applied_after_fixing_ws && state->apply)
warning("%d line%s applied after"
" fixing whitespace errors.",
@@ -4802,5 +4806,12 @@ int apply_all_patches(struct apply_state *state,
}
 
return !!errs;
+
+rollback_end:
+   if (state->newfd >= 0) {
+   rollback_lock_file(state->lock_file);
+   state->newfd = -1;
+   }
+   return -1;
 }
 
-- 
2.8.2.490.g3dabe57

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


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

2016-05-11 Thread Christian Couder
To libify `git apply` functionality we have to signal errors to the
caller instead of die()ing.

To do that in a compatible manner with the rest of the error handling
in "builtin/apply.c", add_index_file() should return -1 using error()
instead of calling die().

Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 48 +++-
 1 file changed, 31 insertions(+), 17 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index ca3502f..0e20467 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4089,11 +4089,11 @@ static int remove_file(struct apply_state *state, 
struct patch *patch, int rmdir
return 0;
 }
 
-static void add_index_file(struct apply_state *state,
-  const char *path,
-  unsigned mode,
-  void *buf,
-  unsigned long size)
+static int add_index_file(struct apply_state *state,
+ const char *path,
+ unsigned mode,
+ void *buf,
+ unsigned long size)
 {
struct stat st;
struct cache_entry *ce;
@@ -4101,7 +4101,7 @@ static void add_index_file(struct apply_state *state,
unsigned ce_size = cache_entry_size(namelen);
 
if (!state->update_index)
-   return;
+   return 0;
 
ce = xcalloc(1, ce_size);
memcpy(ce->name, path, namelen);
@@ -4112,20 +4112,32 @@ static void add_index_file(struct apply_state *state,
const char *s;
 
if (!skip_prefix(buf, "Subproject commit ", ) ||
-   get_sha1_hex(s, ce->sha1))
-   die(_("corrupt patch for submodule %s"), path);
+   get_sha1_hex(s, ce->sha1)) {
+   free(ce);
+   return error(_("corrupt patch for submodule %s"), path);
+   }
} else {
if (!state->cached) {
-   if (lstat(path, ) < 0)
-   die_errno(_("unable to stat newly created file 
'%s'"),
- path);
+   if (lstat(path, ) < 0) {
+   free(ce);
+   return error(_("unable to stat newly "
+  "created file '%s': %s"),
+path, strerror(errno));
+   }
fill_stat_cache_info(ce, );
}
-   if (write_sha1_file(buf, size, blob_type, ce->sha1) < 0)
-   die(_("unable to create backing store for newly created 
file %s"), path);
+   if (write_sha1_file(buf, size, blob_type, ce->sha1) < 0) {
+   free(ce);
+   return error(_("unable to create backing store "
+  "for newly created file %s"), path);
+   }
}
-   if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)
-   die(_("unable to add cache entry for %s"), path);
+   if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0) {
+   free(ce);
+   return error(_("unable to add cache entry for %s"), path);
+   }
+
+   return 0;
 }
 
 static int try_create_file(const char *path, unsigned int mode, const char 
*buf, unsigned long size)
@@ -4261,8 +4273,10 @@ static void create_file(struct apply_state *state, 
struct patch *patch)
if (patch->conflicted_threeway) {
if (add_conflicted_stages_file(state, patch))
exit(1);
-   } else
-   add_index_file(state, path, mode, buf, size);
+   } else {
+   if (add_index_file(state, path, mode, buf, size))
+   exit(1);
+   }
 }
 
 /* phase zero is to remove, phase one is to create */
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 94/94] builtin/apply: add a cli option for be_silent

2016-05-11 Thread Christian Couder
Let's make it possible to request a silent operation on the
command line.

Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/builtin/apply.c b/builtin/apply.c
index ce12769..397ef26 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -70,6 +70,8 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
OPT_BOOL(0, "allow-overlap", _overlap,
N_("allow overlapping hunks")),
OPT__VERBOSE(_verbosely, N_("be verbose")),
+   OPT_BOOL(0, "silent", _silent,
+   N_("do not print any output")),
OPT_BIT(0, "inaccurate-eof", ,
N_("tolerate incorrectly detected missing new-line at 
the end of file"),
APPLY_OPT_INACCURATE_EOF),
-- 
2.8.2.490.g3dabe57

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


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

2016-05-11 Thread Christian Couder
To libify `git apply` functionality we have to signal errors to the
caller instead of exit()ing.

To do that in a compatible manner with the rest of the error handling
in "builtin/apply.c", write_out_one_result() should just return what
remove_file() and create_file() are returning instead of calling
exit().

Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 38 --
 1 file changed, 16 insertions(+), 22 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 2b562db..f06bf16 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4277,36 +4277,29 @@ static int create_file(struct apply_state *state, 
struct patch *patch)
 }
 
 /* phase zero is to remove, phase one is to create */
-static void write_out_one_result(struct apply_state *state,
-struct patch *patch,
-int phase)
+static int write_out_one_result(struct apply_state *state,
+   struct patch *patch,
+   int phase)
 {
if (patch->is_delete > 0) {
-   if (phase == 0) {
-   if (remove_file(state, patch, 1))
-   exit(1);
-   }
-   return;
+   if (phase == 0)
+   return remove_file(state, patch, 1);
+   return 0;
}
if (patch->is_new > 0 || patch->is_copy) {
-   if (phase == 1) {
-   if (create_file(state, patch))
-   exit(1);
-   }
-   return;
+   if (phase == 1)
+   return create_file(state, patch);
+   return 0;
}
/*
 * Rename or modification boils down to the same
 * thing: remove the old, write the new
 */
-   if (phase == 0) {
-   if (remove_file(state, patch, patch->is_rename))
-   exit(1);
-   }
-   if (phase == 1) {
-   if (create_file(state, patch))
-   exit(1);
-   }
+   if (phase == 0)
+   return remove_file(state, patch, patch->is_rename);
+   if (phase == 1)
+   return create_file(state, patch);
+   return 0;
 }
 
 static int write_out_one_reject(struct apply_state *state, struct patch *patch)
@@ -4393,7 +4386,8 @@ static int write_out_results(struct apply_state *state, 
struct patch *list)
if (l->rejected)
errs = 1;
else {
-   write_out_one_result(state, l, phase);
+   if (write_out_one_result(state, l, phase))
+   exit(1);
if (phase == 1) {
if (write_out_one_reject(state, l))
errs = 1;
-- 
2.8.2.490.g3dabe57

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


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

2016-05-11 Thread Christian Couder
To libify `git apply` functionality we have to signal errors to the
caller instead of die()ing.

To do that in a compatible manner with the rest of the error handling
in "builtin/apply.c", try_create_file() should return -1 in case of
error.

Unfortunately try_create_file() currently returns -1 to signal a
recoverable error. To fix that, let's make it return 1 in case of
a recoverable error and -1 in case of an unrecoverable error.

Helped-by: Eric Sunshine <sunsh...@sunshineco.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 41 ++---
 1 file changed, 30 insertions(+), 11 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 97bc704..eaf7b8f 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4140,38 +4140,45 @@ static int add_index_file(struct apply_state *state,
return 0;
 }
 
+/*
+ * Returns:
+ *  -1 if an unrecoverable error happened
+ *   0 if everything went well
+ *   1 if a recoverable error happened
+ */
 static int try_create_file(const char *path, unsigned int mode, const char 
*buf, unsigned long size)
 {
-   int fd;
+   int fd, res;
struct strbuf nbuf = STRBUF_INIT;
 
if (S_ISGITLINK(mode)) {
struct stat st;
if (!lstat(path, ) && S_ISDIR(st.st_mode))
return 0;
-   return mkdir(path, 0777);
+   return !!mkdir(path, 0777);
}
 
if (has_symlinks && S_ISLNK(mode))
/* Although buf:size is counted string, it also is NUL
 * terminated.
 */
-   return symlink(buf, path);
+   return !!symlink(buf, path);
 
fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 
0666);
if (fd < 0)
-   return -1;
+   return 1;
 
if (convert_to_working_tree(path, buf, size, )) {
size = nbuf.len;
buf  = nbuf.buf;
}
-   write_or_die(fd, buf, size);
+   res = !write_or_whine_pipe(fd, buf, size, path);
strbuf_release();
 
-   if (close(fd) < 0)
-   die_errno(_("closing file '%s'"), path);
-   return 0;
+   if (close(fd) < 0 && !res)
+   return error(_("closing file '%s': %s"), path, strerror(errno));
+
+   return res ? -1 : 0;
 }
 
 /*
@@ -4185,15 +4192,24 @@ static void create_one_file(struct apply_state *state,
const char *buf,
unsigned long size)
 {
+   int res;
+
if (state->cached)
return;
-   if (!try_create_file(path, mode, buf, size))
+
+   res = try_create_file(path, mode, buf, size);
+   if (res < 0)
+   exit(1);
+   if (!res)
return;
 
if (errno == ENOENT) {
if (safe_create_leading_directories(path))
return;
-   if (!try_create_file(path, mode, buf, size))
+   res = try_create_file(path, mode, buf, size);
+   if (res < 0)
+   exit(1);
+   if (!res)
return;
}
 
@@ -4212,7 +4228,10 @@ static void create_one_file(struct apply_state *state,
for (;;) {
char newpath[PATH_MAX];
mksnpath(newpath, sizeof(newpath), "%s~%u", path, nr);
-   if (!try_create_file(newpath, mode, buf, size)) {
+   res = try_create_file(newpath, mode, buf, size);
+   if (res < 0)
+   exit(1);
+   if (!res) {
if (!rename(newpath, path))
return;
unlink_or_warn(newpath);
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 78/94] apply: rename and move opt constants to apply.h

2016-05-11 Thread Christian Couder
The constants for the "inaccurate-eof" and the "recount" options will
be used in both "apply.c" and "builtin/apply.c", so they need to go
into "apply.h", and therefore they need a name that is more specific
to the API they belong to.

Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 apply.h |  3 +++
 builtin/apply.c | 11 ---
 2 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/apply.h b/apply.h
index 5266612..60e0c2f 100644
--- a/apply.h
+++ b/apply.h
@@ -122,4 +122,7 @@ extern int init_apply_state(struct apply_state *state,
struct lock_file *lock_file);
 extern int check_apply_state(struct apply_state *state, int force_apply);
 
+#define APPLY_OPT_INACCURATE_EOF   (1<<0)
+#define APPLY_OPT_RECOUNT  (1<<1)
+
 #endif
diff --git a/builtin/apply.c b/builtin/apply.c
index f05dc96..9ce177b 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4448,9 +4448,6 @@ static int write_out_results(struct apply_state *state, 
struct patch *list)
return errs;
 }
 
-#define INACCURATE_EOF (1<<0)
-#define RECOUNT(1<<1)
-
 /*
  * Try to apply a patch.
  *
@@ -4479,8 +4476,8 @@ static int apply_patch(struct apply_state *state,
int nr;
 
patch = xcalloc(1, sizeof(*patch));
-   patch->inaccurate_eof = !!(options & INACCURATE_EOF);
-   patch->recount =  !!(options & RECOUNT);
+   patch->inaccurate_eof = !!(options & APPLY_OPT_INACCURATE_EOF);
+   patch->recount =  !!(options & APPLY_OPT_RECOUNT);
nr = parse_chunk(state, buf.buf + offset, buf.len - offset, 
patch);
if (nr < 0) {
free_patch(patch);
@@ -4770,10 +4767,10 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix)
OPT__VERBOSE(_verbosely, N_("be verbose")),
OPT_BIT(0, "inaccurate-eof", ,
N_("tolerate incorrectly detected missing new-line at 
the end of file"),
-   INACCURATE_EOF),
+   APPLY_OPT_INACCURATE_EOF),
OPT_BIT(0, "recount", ,
N_("do not trust the line counts in the hunk headers"),
-   RECOUNT),
+   APPLY_OPT_RECOUNT),
{ OPTION_CALLBACK, 0, "directory", , N_("root"),
N_("prepend  to all filenames"),
0, apply_option_parse_directory },
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 53/94] builtin/apply: make find_header() return -1 instead of die()ing

2016-05-11 Thread Christian Couder
To libify `git apply` functionality we have to signal errors to the
caller instead of die()ing.

To do that in a compatible manner with the rest of the error handling
in builtin/apply.c, find_header() should return -1 using error()
instead of calling die().

Unfortunately find_header() already returns -1 when no header is found,
so let's make it return -2 instead in this case.

Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c   | 33 ++---
 t/t4254-am-corrupt.sh |  2 +-
 2 files changed, 23 insertions(+), 12 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index a166d70..4212705 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -1529,6 +1529,14 @@ static int parse_fragment_header(const char *line, int 
len, struct fragment *fra
return offset;
 }
 
+/*
+ * Find file diff header
+ *
+ * Returns:
+ *  -1 in case of error
+ *  -2 if no header was found
+ *   the size of the header in bytes (called "offset") otherwise
+ */
 static int find_header(struct apply_state *state,
   const char *line,
   unsigned long size,
@@ -1562,8 +1570,8 @@ static int find_header(struct apply_state *state,
struct fragment dummy;
if (parse_fragment_header(line, len, ) < 0)
continue;
-   die(_("patch fragment without header at line %d: %.*s"),
-   state->linenr, (int)len-1, line);
+   return error(_("patch fragment without header at line 
%d: %.*s"),
+state->linenr, (int)len-1, line);
}
 
if (size < len + 6)
@@ -1579,18 +1587,18 @@ static int find_header(struct apply_state *state,
continue;
if (!patch->old_name && !patch->new_name) {
if (!patch->def_name)
-   die(Q_("git diff header lacks filename 
information when removing "
-  "%d leading pathname component 
(line %d)",
-  "git diff header lacks filename 
information when removing "
-  "%d leading pathname components 
(line %d)",
-  state->p_value),
-   state->p_value, state->linenr);
+   return error(Q_("git diff header lacks 
filename information when removing "
+   "%d leading pathname 
component (line %d)",
+   "git diff header lacks 
filename information when removing "
+   "%d leading pathname 
components (line %d)",
+   state->p_value),
+state->p_value, 
state->linenr);
patch->old_name = xstrdup(patch->def_name);
patch->new_name = xstrdup(patch->def_name);
}
if (!patch->is_delete && !patch->new_name)
-   die("git diff header lacks filename information 
"
-   "(line %d)", state->linenr);
+   return error("git diff header lacks filename 
information "
+"(line %d)", state->linenr);
patch->is_toplevel_relative = 1;
*hdrsize = git_hdr_len;
return offset;
@@ -1615,7 +1623,7 @@ static int find_header(struct apply_state *state,
state->linenr += 2;
return offset;
}
-   return -1;
+   return -2;
 }
 
 static void record_ws_error(struct apply_state *state,
@@ -2106,6 +2114,9 @@ static int parse_chunk(struct apply_state *state, char 
*buffer, unsigned long si
int hdrsize, patchsize;
int offset = find_header(state, buffer, size, , patch);
 
+   if (offset == -1)
+   exit(1);
+
if (offset < 0)
return offset;
 
diff --git a/t/t4254-am-corrupt.sh b/t/t4254-am-corrupt.sh
index 85716dd..9bd7dd2 100755
--- a/t/t4254-am-corrupt.sh
+++ b/t/t4254-am-corrupt.sh
@@ -29,7 +29,7 @@ test_expect_success 'try to apply corrupted patch' '
 '
 
 test_expect_success 'compare diagnostic; ensure file is still here' '
-   echo "fatal: git diff header lacks filename information (line 4)"

[PATCH v2 80/94] apply: make some parsing functions static again

2016-05-11 Thread Christian Couder
Some parsing functions that were used in both "apply.c" and
"builtin/apply.c" are now only used in the former, so they
can be made static to "apply.c".

Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 apply.c | 6 +++---
 apply.h | 5 -
 2 files changed, 3 insertions(+), 8 deletions(-)

diff --git a/apply.c b/apply.c
index 537221b..3285bf7 100644
--- a/apply.c
+++ b/apply.c
@@ -27,7 +27,7 @@ static void git_apply_config(void)
git_config(git_default_config, NULL);
 }
 
-int parse_whitespace_option(struct apply_state *state, const char *option)
+static int parse_whitespace_option(struct apply_state *state, const char 
*option)
 {
if (!option) {
state->ws_error_action = warn_on_ws_error;
@@ -57,8 +57,8 @@ int parse_whitespace_option(struct apply_state *state, const 
char *option)
return error(_("unrecognized whitespace option '%s'"), option);
 }
 
-int parse_ignorewhitespace_option(struct apply_state *state,
- const char *option)
+static int parse_ignorewhitespace_option(struct apply_state *state,
+const char *option)
 {
if (!option || !strcmp(option, "no") ||
!strcmp(option, "false") || !strcmp(option, "never") ||
diff --git a/apply.h b/apply.h
index c8b79ce..27b26a2 100644
--- a/apply.h
+++ b/apply.h
@@ -112,11 +112,6 @@ struct apply_state {
enum ws_ignore ws_ignore_action;
 };
 
-extern int parse_whitespace_option(struct apply_state *state,
-  const char *option);
-extern int parse_ignorewhitespace_option(struct apply_state *state,
-const char *option);
-
 extern int apply_option_parse_exclude(const struct option *opt,
  const char *arg, int unset);
 extern int apply_option_parse_include(const struct option *opt,
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 52/94] builtin/apply: read_patch_file() return -1 instead of die()ing

2016-05-11 Thread Christian Couder
To libify `git apply` functionality we have to signal errors to the
caller instead of die()ing. Let's do that by using error() instead
of die()ing in read_patch_file().

Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 8 +---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index d95630c..a166d70 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -445,10 +445,10 @@ static void say_patch_name(FILE *output, const char *fmt, 
struct patch *patch)
 
 #define SLOP (16)
 
-static void read_patch_file(struct strbuf *sb, int fd)
+static int read_patch_file(struct strbuf *sb, int fd)
 {
if (strbuf_read(sb, fd, 0) < 0)
-   die_errno("git apply: failed to read");
+   return error("git apply: failed to read: %s", strerror(errno));
 
/*
 * Make sure that we have some slop in the buffer
@@ -457,6 +457,7 @@ static void read_patch_file(struct strbuf *sb, int fd)
 */
strbuf_grow(sb, SLOP);
memset(sb->buf + sb->len, 0, SLOP);
+   return 0;
 }
 
 static unsigned long linelen(const char *buffer, unsigned long size)
@@ -4532,7 +4533,8 @@ static int apply_patch(struct apply_state *state,
int res = 0;
 
state->patch_input_file = filename;
-   read_patch_file(, fd);
+   if (read_patch_file(, fd))
+   return -1;
offset = 0;
while (offset < buf.len) {
struct patch *patch;
-- 
2.8.2.490.g3dabe57

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


[PATCH v2 51/94] builtin/apply: make apply_patch() return -1 instead of die()ing

2016-05-11 Thread Christian Couder
To libify `git apply` functionality we have to signal errors
to the caller instead of die()ing.

As a first step in this direction, let's make apply_patch() return
-1 in case of errors instead of dying. For now its only caller
apply_all_patches() will exit(1) when apply_patch() return -1.

In a later patch, apply_all_patches() will return -1 too instead of
exiting.

Helped-by: Eric Sunshine <sunsh...@sunshineco.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 54 +++---
 1 file changed, 39 insertions(+), 15 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index ec55768..d95630c 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4512,6 +4512,14 @@ static int write_out_results(struct apply_state *state, 
struct patch *list)
 #define INACCURATE_EOF (1<<0)
 #define RECOUNT(1<<1)
 
+/*
+ * Try to apply a patch.
+ *
+ * Returns:
+ *  -1 if an error happened
+ *   0 if the patch applied
+ *   1 if the patch did not apply
+ */
 static int apply_patch(struct apply_state *state,
   int fd,
   const char *filename,
@@ -4521,6 +4529,7 @@ static int apply_patch(struct apply_state *state,
struct strbuf buf = STRBUF_INIT; /* owns the patch text */
struct patch *list = NULL, **listp = 
int skipped_patch = 0;
+   int res = 0;
 
state->patch_input_file = filename;
read_patch_file(, fd);
@@ -4553,8 +4562,10 @@ static int apply_patch(struct apply_state *state,
offset += nr;
}
 
-   if (!list && !skipped_patch)
-   die(_("unrecognized input"));
+   if (!list && !skipped_patch) {
+   res = error(_("unrecognized input"));
+   goto end;
+   }
 
if (state->whitespace_error && (state->ws_error_action == 
die_on_ws_error))
state->apply = 0;
@@ -4563,21 +4574,22 @@ static int apply_patch(struct apply_state *state,
if (state->update_index && state->newfd < 0)
state->newfd = hold_locked_index(state->lock_file, 1);
 
-   if (state->check_index) {
-   if (read_cache() < 0)
-   die(_("unable to read index file"));
+   if (state->check_index && read_cache() < 0) {
+   res = error(_("unable to read index file"));
+   goto end;
}
 
if ((state->check || state->apply) &&
check_patch_list(state, list) < 0 &&
-   !state->apply_with_reject)
-   exit(1);
+   !state->apply_with_reject) {
+   res = -1;
+   goto end;
+   }
 
if (state->apply && write_out_results(state, list)) {
-   if (state->apply_with_reject)
-   exit(1);
/* with --3way, we still need to write the index out */
-   return 1;
+   res = state->apply_with_reject ? -1 : 1;
+   goto end;
}
 
if (state->fake_ancestor)
@@ -4592,10 +4604,11 @@ static int apply_patch(struct apply_state *state,
if (state->summary)
summary_patch_list(list);
 
+end:
free_patch_list(list);
strbuf_release();
string_list_clear(>fn_table, 0);
-   return 0;
+   return res;
 }
 
 static void git_apply_config(void)
@@ -4722,6 +4735,7 @@ static int apply_all_patches(struct apply_state *state,
 int options)
 {
int i;
+   int res;
int errs = 0;
int read_stdin = 1;
 
@@ -4730,7 +4744,10 @@ static int apply_all_patches(struct apply_state *state,
int fd;
 
if (!strcmp(arg, "-")) {
-   errs |= apply_patch(state, 0, "", options);
+   res = apply_patch(state, 0, "", options);
+   if (res < 0)
+   exit(1);
+   errs |= res;
read_stdin = 0;
continue;
} else if (0 < state->prefix_length)
@@ -4743,12 +4760,19 @@ static int apply_all_patches(struct apply_state *state,
die_errno(_("can't open patch '%s'"), arg);
read_stdin = 0;
set_default_whitespace_mode(state);
-   errs |= apply_patch(state, fd, arg, options);
+   res = apply_patch(state, fd, arg, options);
+   if (res < 0)
+   exit(1);
+   errs |= res;
close(fd);
}
set_default_whitespace_mode(state);
-   if (read_stdin)
-   errs |= apply_patch(state, 0, "",

[PATCH v2 43/94] builtin/apply: move 'state_linenr' global into 'struct apply_state'

2016-05-11 Thread Christian Couder
To libify the apply functionality the 'state_linenr' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbel...@google.com>
Signed-off-by: Christian Couder <chrisc...@tuxfamily.org>
---
 builtin/apply.c | 75 ++---
 1 file changed, 40 insertions(+), 35 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index deba14c..5c003a1 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -84,6 +84,13 @@ struct apply_state {
int max_change;
int max_len;
 
+   /*
+* Various "current state", notably line numbers and what
+* file (and how) we're patching right now.. The "is_"
+* things are flags, where -1 means "don't know yet".
+*/
+   int linenr;
+
int p_value;
int p_value_known;
unsigned int p_context;
@@ -156,13 +163,6 @@ static void set_default_whitespace_mode(struct apply_state 
*state)
state->ws_error_action = (state->apply ? warn_on_ws_error : 
nowarn_ws_error);
 }
 
-/*
- * Various "current state", notably line numbers and what
- * file (and how) we're patching right now.. The "is_"
- * things are flags, where -1 means "don't know yet".
- */
-static int state_linenr = 1;
-
 /*
  * This represents one "hunk" from a patch, starting with
  * "@@ -oldpos,oldlines +newpos,newlines @@" marker.  The
@@ -939,7 +939,7 @@ static void parse_traditional_patch(struct apply_state 
*state,
}
}
if (!name)
-   die(_("unable to find filename in patch at line %d"), 
state_linenr);
+   die(_("unable to find filename in patch at line %d"), 
state->linenr);
 }
 
 static int gitdiff_hdrend(struct apply_state *state,
@@ -977,17 +977,17 @@ static void gitdiff_verify_name(struct apply_state *state,
char *another;
if (isnull)
die(_("git apply: bad git-diff - expected /dev/null, 
got %s on line %d"),
-   *name, state_linenr);
+   *name, state->linenr);
another = find_name(state, line, NULL, state->p_value, 
TERM_TAB);
if (!another || memcmp(another, *name, len + 1))
die((side == DIFF_NEW_NAME) ?
_("git apply: bad git-diff - inconsistent new 
filename on line %d") :
-   _("git apply: bad git-diff - inconsistent old 
filename on line %d"), state_linenr);
+   _("git apply: bad git-diff - inconsistent old 
filename on line %d"), state->linenr);
free(another);
} else {
/* expect "/dev/null" */
if (memcmp("/dev/null", line, 9) || line[9] != '\n')
-   die(_("git apply: bad git-diff - expected /dev/null on 
line %d"), state_linenr);
+   die(_("git apply: bad git-diff - expected /dev/null on 
line %d"), state->linenr);
}
 }
 
@@ -1350,8 +1350,8 @@ static int parse_git_header(struct apply_state *state,
 
line += len;
size -= len;
-   state_linenr++;
-   for (offset = len ; size > 0 ; offset += len, size -= len, line += len, 
state_linenr++) {
+   state->linenr++;
+   for (offset = len ; size > 0 ; offset += len, size -= len, line += len, 
state->linenr++) {
static const struct opentry {
const char *str;
int (*fn)(struct apply_state *, const char *, struct 
patch *);
@@ -1522,7 +1522,7 @@ static int find_header(struct apply_state *state,
patch->is_new = patch->is_delete = -1;
patch->old_mode = patch->new_mode = 0;
patch->old_name = patch->new_name = NULL;
-   for (offset = 0; size > 0; offset += len, size -= len, line += len, 
state_linenr++) {
+   for (offset = 0; size > 0; offset += len, size -= len, line += len, 
state->linenr++) {
unsigned long nextlen;
 
len = linelen(line, size);
@@ -1543,7 +1543,7 @@ static int find_header(struct apply_state *state,
if (parse_fragment_header(line, len, ) < 0)
continue;
die(_("patch fragment without header at line %d: %.*s"),
-   state_linenr, (int)len-1, line);
+   state->linenr, (int)len-1, line);
}
 
if (size < len + 6)
@@ -1564,13 +1564,13 @@ static int find_header(struct apply_state *state,
   "git diff hea

<    10   11   12   13   14   15   16   17   18   19   >