git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [PATCH 00/83] libify apply and use lib in am
@ 2016-04-24 13:33 Christian Couder
  2016-04-24 13:33 ` [PATCH 01/83] builtin/apply: make gitdiff_verify_name() return void Christian Couder
                   ` (79 more replies)
  0 siblings, 80 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

This is a patch series about libifying `git apply` functionality, and
using this libified functionality in `git am`, so that no 'git apply'
process is spawn anymore. This makes `git am` significantly faster, so
`git rebase`, when it uses the am backend, is also significantly
faster.

This has initially been discussed in the following thread:

http://thread.gmane.org/gmane.comp.version-control.git/287236/

A RFC patch series was sent previously that only got rid of the global
variables and refactored the code around a bit:

http://thread.gmane.org/gmane.comp.version-control.git/288489/

This new patch series is built on top of that previous work, so
patches 1/83 to 47/83 are from the previous RFC patch series and
patches after that are new.

Sorry if this patch series is a bit long. I can split it into two or
more series if it is prefered.

The benefits are not just related to not creating new processes. When
`git am` launched a `git apply` process, this new process had to read
the index from disk. Then after the `git apply`process had terminated,
`git am` dropped its index and read the index from disk to get the
index that had been modified by the `git apply`process. This was
inefficient and also prevented the split-index mechanism to provide
many performance benefits.

Performance numbers:

  - A few days ago Æ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.

  - Some days ago 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.

No tests on Windows have been performed, but it could be interesting
to test on this platform.

Christian Couder (83):
  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: introduce 'struct apply_state' to start libifying
  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 'read_stdin' global into cmd_apply()
  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 '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' init into init_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: get rid of the 'newfd' global
  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: libify parse_whitespace_option()
  builtin/apply: libify parse_ignorewhitespace_option()
  builtin/apply: move init_apply_state() to apply.c
  apply: libify init_apply_state()
  builtin/apply: libify check_apply_state()
  builtin/apply: move check_apply_state() to apply.c
  builtin/apply: make apply_all_patches() return -1 on error
  builtin/apply: make parse_traditional_patch() return -1 on error
  builtin/apply: make gitdiff_verify_name() return -1 on error
  builtin/apply: change die_on_unsafe_path() to check_unsafe_path()
  builtin/apply: make build_fake_ancestor() return -1 on error
  builtin/apply: make remove_file() return -1 on error
  builtin/apply: make add_conflicted_stages_file() return -1 on error
  builtin/apply: make add_index_file() return -1 on error
  builtin/apply: make create_file() return -1 on error
  builtin/apply: make write_out_one_result() return -1 on error
  builtin/apply: make write_out_results() return -1 on error
  builtin/apply: make try_create_file() return -1 on error
  builtin/apply: make create_one_file() return -1 on error
  builtin/apply: rename option parsing functions
  apply: rename and move opt constants to apply.h
  Move libified code from builtin/apply.c to apply.{c,h}
  apply: make some parsing functions static again
  run-command: make dup_devnull() non static
  apply: roll back index in case of error
  environment: add set_index_file()
  builtin/am: use apply api in run_apply()

 Makefile               |    1 +
 apply.c                | 4796 ++++++++++++++++++++++++++++++++++++++++++++++++
 apply.h                |  149 ++
 builtin/am.c           |  103 +-
 builtin/apply.c        | 4665 +---------------------------------------------
 cache.h                |    1 +
 environment.c          |    5 +
 run-command.c          |    2 +-
 run-command.h          |    6 +
 t/t4012-diff-binary.sh |    4 +-
 t/t4254-am-corrupt.sh  |    2 +-
 11 files changed, 5096 insertions(+), 4638 deletions(-)
 create mode 100644 apply.c
 create mode 100644 apply.h

-- 
2.8.1.300.g5fed0c0

^ permalink raw reply	[flat|nested] 156+ messages in thread

* [PATCH 01/83] builtin/apply: make gitdiff_verify_name() return void
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 02/83] builtin/apply: avoid parameter shadowing 'p_value' global Christian Couder
                   ` (78 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, 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.

Signed-off-by: Christian Couder <chriscool@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, &patch->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, &patch->new_name,
+			    DIFF_NEW_NAME);
 	return 0;
 }
 
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 02/83] builtin/apply: avoid parameter shadowing 'p_value' global
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
  2016-04-24 13:33 ` [PATCH 01/83] builtin/apply: make gitdiff_verify_name() return void Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 03/83] builtin/apply: avoid parameter shadowing 'linenr' global Christian Couder
                   ` (77 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

Let's just rename the global 'state_p_value' as it will become
'state->p_value' in a following patch.

Signed-off-by: Christian Couder <chriscool@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 (!p_value)
+	if (!state_p_value)
 		return (llen && line[0] == '/') ? NULL : line;
 
-	nslash = p_value;
+	nslash = state_p_value;
 	for (i = 0; i < llen; i++) {
 		int ch = line[i];
 		if (ch == '/' && --nslash <= 0)
@@ -1481,8 +1481,8 @@ static int find_header(const char *line, unsigned long size, int *hdrsize, struc
 					       "%d leading pathname component (line %d)",
 					       "git diff header lacks filename information when removing "
 					       "%d leading pathname components (line %d)",
-					       p_value),
-					    p_value, linenr);
+					       state_p_value),
+					    state_p_value, linenr);
 				patch->old_name = xstrdup(patch->def_name);
 				patch->new_name = xstrdup(patch->def_name);
 			}
@@ -4461,7 +4461,7 @@ static int option_parse_include(const struct option *opt,
 static int option_parse_p(const struct option *opt,
 			  const char *arg, int unset)
 {
-	p_value = atoi(arg);
+	state_p_value = atoi(arg);
 	p_value_known = 1;
 	return 0;
 }
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 03/83] builtin/apply: avoid parameter shadowing 'linenr' global
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
  2016-04-24 13:33 ` [PATCH 01/83] builtin/apply: make gitdiff_verify_name() return void Christian Couder
  2016-04-24 13:33 ` [PATCH 02/83] builtin/apply: avoid parameter shadowing 'p_value' global Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-27 16:27   ` Junio C Hamano
  2016-04-24 13:33 ` [PATCH 04/83] builtin/apply: avoid local variable shadowing 'len' parameter Christian Couder
                   ` (76 subsequent siblings)
  79 siblings, 1 reply; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index e133b38..7115dc2 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -1516,7 +1516,7 @@ static int find_header(const char *line, unsigned long size, int *hdrsize, struc
 	return -1;
 }
 
-static void record_ws_error(unsigned result, const char *line, int len, int linenr)
+static void record_ws_error(unsigned result, const char *line, int len, int l_nr)
 {
 	char *err;
 
@@ -1530,7 +1530,7 @@ 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);
+		patch_input_file, l_nr, err, len, line);
 	free(err);
 }
 
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 04/83] builtin/apply: avoid local variable shadowing 'len' parameter
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (2 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 03/83] builtin/apply: avoid parameter shadowing 'linenr' global Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 05/83] builtin/apply: extract line_by_line_fuzzy_match() from match_fragment() Christian Couder
                   ` (75 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 7115dc2..78849e4 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.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 05/83] builtin/apply: extract line_by_line_fuzzy_match() from match_fragment()
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (3 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 04/83] builtin/apply: avoid local variable shadowing 'len' parameter Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-25 18:50   ` Stefan Beller
  2016-04-24 13:33 ` [PATCH 06/83] builtin/apply: move 'options' variable into cmd_apply() Christian Couder
                   ` (74 subsequent siblings)
  79 siblings, 1 reply; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, 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.

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 builtin/apply.c | 129 ++++++++++++++++++++++++++++++++------------------------
 1 file changed, 73 insertions(+), 56 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 78849e4..02239d9 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(&fixed, imgoff + extra_chars);
+	strbuf_add(&fixed, img->buf + try, imgoff);
+	strbuf_add(&fixed, preimage_eof, extra_chars);
+	fixed_buf = strbuf_detach(&fixed, &fixed_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,
@@ -2251,7 +2319,7 @@ static int match_fragment(struct image *img,
 			  int match_beginning, int match_end)
 {
 	int i;
-	char *fixed_buf, *buf, *orig, *target;
+	char *fixed_buf, *orig, *target;
 	struct strbuf fixed;
 	size_t fixed_len, postlen;
 	int preimage_limit;
@@ -2312,6 +2380,7 @@ static int match_fragment(struct image *img,
 		 * There must be one non-blank context line that match
 		 * a line before the end of img.
 		 */
+		char *buf;
 		char *buf_end;
 
 		buf = preimage->buf;
@@ -2331,61 +2400,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 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(&fixed, imgoff + extra_chars);
-		strbuf_add(&fixed, img->buf + try, imgoff);
-		strbuf_add(&fixed, preimage_eof, extra_chars);
-		fixed_buf = strbuf_detach(&fixed, &fixed_len);
-		update_pre_post_images(preimage, postimage,
-				fixed_buf, fixed_len, postlen);
-		return 1;
-	}
+	if (ws_ignore_action == ignore_ws_change)
+		return line_by_line_fuzzy_match(img, preimage, postimage,
+						try, try_lno, preimage_limit);
 
 	if (ws_error_action != correct_ws_error)
 		return 0;
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 06/83] builtin/apply: move 'options' variable into cmd_apply()
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (4 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 05/83] builtin/apply: extract line_by_line_fuzzy_match() from match_fragment() Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 07/83] builtin/apply: introduce 'struct apply_state' to start libifying Christian Couder
                   ` (73 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 02239d9..8fd8dbc 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)
 {
@@ -4518,6 +4517,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.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 07/83] builtin/apply: introduce 'struct apply_state' to start libifying
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (5 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 06/83] builtin/apply: move 'options' variable into cmd_apply() Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 08/83] builtin/apply: move 'unidiff_zero' global into 'struct apply_state' Christian Couder
                   ` (72 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, 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".

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 8fd8dbc..51e6af4 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;
@@ -749,7 +752,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;
@@ -762,17 +765,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);
@@ -859,7 +862,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;
 
@@ -867,8 +873,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;
@@ -1430,7 +1436,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;
 
@@ -1507,7 +1517,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);
 		*hdrsize = len + nextlen;
 		linenr += 2;
 		return offset;
@@ -1914,21 +1924,21 @@ static int parse_binary(char *buffer, unsigned long size, struct patch *patch)
 	return used;
 }
 
-static void prefix_one(char **name)
+static void prefix_one(struct apply_state *state, char **name)
 {
 	char *old_name = *name;
 	if (!old_name)
 		return;
-	*name = xstrdup(prefix_filename(prefix, prefix_length, *name));
+	*name = xstrdup(prefix_filename(state->prefix, state->prefix_length, *name));
 	free(old_name);
 }
 
-static void prefix_patch(struct patch *p)
+static void prefix_patch(struct apply_state *state, struct patch *p)
 {
-	if (!prefix || p->is_toplevel_relative)
+	if (!state->prefix || p->is_toplevel_relative)
 		return;
-	prefix_one(&p->new_name);
-	prefix_one(&p->old_name);
+	prefix_one(state, &p->new_name);
+	prefix_one(state, &p->old_name);
 }
 
 /*
@@ -1945,16 +1955,16 @@ static void add_name_limit(const char *name, int exclude)
 	it->util = exclude ? NULL : (void *) 1;
 }
 
-static int use_patch(struct patch *p)
+static int use_patch(struct apply_state *state, struct patch *p)
 {
 	const char *pathname = p->new_name ? p->new_name : p->old_name;
 	int i;
 
 	/* Paths outside are not touched regardless of "--include" */
-	if (0 < prefix_length) {
+	if (0 < state->prefix_length) {
 		int pathlen = strlen(pathname);
-		if (pathlen <= prefix_length ||
-		    memcmp(prefix, pathname, prefix_length))
+		if (pathlen <= state->prefix_length ||
+		    memcmp(state->prefix, pathname, state->prefix_length))
 			return 0;
 	}
 
@@ -1981,17 +1991,17 @@ static int use_patch(struct patch *p)
  * Return the number of bytes consumed, so that the caller can call us
  * again for the next patch.
  */
-static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
+static int parse_chunk(struct apply_state *state, char *buffer, unsigned long size, struct patch *patch)
 {
 	int hdrsize, patchsize;
-	int offset = find_header(buffer, size, &hdrsize, patch);
+	int offset = find_header(state, buffer, size, &hdrsize, patch);
 
 	if (offset < 0)
 		return offset;
 
-	prefix_patch(patch);
+	prefix_patch(state, patch);
 
-	if (!use_patch(patch))
+	if (!use_patch(state, patch))
 		patch->ws_rule = 0;
 	else
 		patch->ws_rule = whitespace_rule(patch->new_name
@@ -4369,7 +4379,10 @@ static struct lock_file lock_file;
 #define INACCURATE_EOF	(1<<0)
 #define RECOUNT		(1<<1)
 
-static int apply_patch(int fd, const char *filename, int options)
+static int apply_patch(struct apply_state *state,
+		       int fd,
+		       const char *filename,
+		       int options)
 {
 	size_t offset;
 	struct strbuf buf = STRBUF_INIT; /* owns the patch text */
@@ -4386,14 +4399,14 @@ static int apply_patch(int fd, const char *filename, int options)
 		patch = xcalloc(1, sizeof(*patch));
 		patch->inaccurate_eof = !!(options & INACCURATE_EOF);
 		patch->recount =  !!(options & RECOUNT);
-		nr = parse_chunk(buf.buf + offset, buf.len - offset, patch);
+		nr = parse_chunk(state, buf.buf + offset, buf.len - offset, patch);
 		if (nr < 0) {
 			free_patch(patch);
 			break;
 		}
 		if (apply_in_reverse)
 			reverse_patches(patch);
-		if (use_patch(patch)) {
+		if (use_patch(state, patch)) {
 			patch_stats(patch);
 			*listp = patch;
 			listp = &patch->next;
@@ -4518,6 +4531,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;
+	struct apply_state state;
 
 	const char *whitespace_option = NULL;
 
@@ -4590,15 +4604,17 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 		OPT_END()
 	};
 
-	prefix = prefix_;
-	prefix_length = prefix ? strlen(prefix) : 0;
+	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);
 
-	argc = parse_options(argc, argv, prefix, builtin_apply_options,
+	argc = parse_options(argc, argv, state.prefix, builtin_apply_options,
 			apply_usage, 0);
 
 	if (apply_with_reject && threeway)
@@ -4629,23 +4645,25 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 		int fd;
 
 		if (!strcmp(arg, "-")) {
-			errs |= apply_patch(0, "<stdin>", options);
+			errs |= apply_patch(&state, 0, "<stdin>", options);
 			read_stdin = 0;
 			continue;
-		} else if (0 < prefix_length)
-			arg = prefix_filename(prefix, prefix_length, arg);
+		} 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(whitespace_option);
-		errs |= apply_patch(fd, arg, options);
+		errs |= apply_patch(&state, fd, arg, options);
 		close(fd);
 	}
 	set_default_whitespace_mode(whitespace_option);
 	if (read_stdin)
-		errs |= apply_patch(0, "<stdin>", options);
+		errs |= apply_patch(&state, 0, "<stdin>", options);
 	if (whitespace_error) {
 		if (squelch_whitespace_errors &&
 		    squelch_whitespace_errors < whitespace_error) {
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 08/83] builtin/apply: move 'unidiff_zero' global into 'struct apply_state'
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (6 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 07/83] builtin/apply: introduce 'struct apply_state' to start libifying Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 09/83] builtin/apply: move 'check' " Christian Couder
                   ` (71 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 builtin/apply.c | 42 ++++++++++++++++++++++++------------------
 1 file changed, 24 insertions(+), 18 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 51e6af4..ad81210 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;
@@ -2696,7 +2697,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)
 {
@@ -2838,7 +2840,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.
@@ -2846,7 +2848,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;
@@ -3069,7 +3071,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;
@@ -3082,7 +3084,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;
@@ -3390,8 +3392,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;
@@ -3417,7 +3422,7 @@ static int try_threeway(struct image *image, struct patch *patch,
 	img = strbuf_detach(&buf, &len);
 	prepare_image(&tmp_image, img, len, 1);
 	/* Apply the patch to get the post image */
-	if (apply_fragments(&tmp_image, patch) < 0) {
+	if (apply_fragments(state, &tmp_image, patch) < 0) {
 		clear_image(&tmp_image);
 		return -1;
 	}
@@ -3461,7 +3466,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;
 
@@ -3469,9 +3475,9 @@ static int apply_data(struct patch *patch, struct stat *st, const struct cache_e
 		return -1;
 
 	if (patch->direct_to_threeway ||
-	    apply_fragments(&image, patch) < 0) {
+	    apply_fragments(state, &image, patch) < 0) {
 		/* Note: with --reject, apply_fragments() returns 0 */
-		if (!threeway || try_threeway(&image, patch, st, ce) < 0)
+		if (!threeway || try_threeway(state, &image, patch, st, ce) < 0)
 			return -1;
 	}
 	patch->result = image.buf;
@@ -3719,7 +3725,7 @@ static void die_on_unsafe_path(struct patch *patch)
  * Check and apply the patch in-core; leave the result in patch->result
  * for the caller to write it out to the final destination.
  */
-static int check_patch(struct patch *patch)
+static int check_patch(struct apply_state *state, struct patch *patch)
 {
 	struct stat st;
 	const char *old_name = patch->old_name;
@@ -3818,13 +3824,13 @@ static int check_patch(struct patch *patch)
 		return error(_("affected file '%s' is beyond a symbolic link"),
 			     patch->new_name);
 
-	if (apply_data(patch, &st, ce) < 0)
+	if (apply_data(state, patch, &st, ce) < 0)
 		return error(_("%s: patch does not apply"), name);
 	patch->rejected = 0;
 	return 0;
 }
 
-static int check_patch_list(struct patch *patch)
+static int check_patch_list(struct apply_state *state, struct patch *patch)
 {
 	int err = 0;
 
@@ -3834,7 +3840,7 @@ static int check_patch_list(struct patch *patch)
 		if (apply_verbosely)
 			say_patch_name(stderr,
 				       _("Checking patch %s..."), patch);
-		err |= check_patch(patch);
+		err |= check_patch(state, patch);
 		patch = patch->next;
 	}
 	return err;
@@ -4436,7 +4442,7 @@ static int apply_patch(struct apply_state *state,
 	}
 
 	if ((check || apply) &&
-	    check_patch_list(list) < 0 &&
+	    check_patch_list(state, list) < 0 &&
 	    !apply_with_reject)
 		exit(1);
 
@@ -4585,7 +4591,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 			PARSE_OPT_NOARG, option_parse_space_change },
 		OPT_BOOL('R', "reverse", &apply_in_reverse,
 			N_("apply the patch in reverse")),
-		OPT_BOOL(0, "unidiff-zero", &unidiff_zero,
+		OPT_BOOL(0, "unidiff-zero", &state.unidiff_zero,
 			N_("don't expect at least one line of context")),
 		OPT_BOOL(0, "reject", &apply_with_reject,
 			N_("leave the rejected hunks in corresponding *.rej files")),
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 09/83] builtin/apply: move 'check' global into 'struct apply_state'
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (7 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 08/83] builtin/apply: move 'unidiff_zero' global into 'struct apply_state' Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-25 18:57   ` Stefan Beller
  2016-04-24 13:33 ` [PATCH 10/83] builtin/apply: move 'check_index' " Christian Couder
                   ` (70 subsequent siblings)
  79 siblings, 1 reply; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index ad81210..6c628f6 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;
@@ -2053,7 +2055,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"), linenr);
 	}
@@ -4441,7 +4443,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);
@@ -4561,7 +4563,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", &summary,
 			N_("instead of applying the patch, output a summary for the input")),
-		OPT_BOOL(0, "check", &check,
+		OPT_BOOL(0, "check", &state.check,
 			N_("instead of applying the patch, see if the patch is applicable")),
 		OPT_BOOL(0, "index", &check_index,
 			N_("make sure the patch is applicable to the current index")),
@@ -4634,7 +4636,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.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 10/83] builtin/apply: move 'check_index' global into 'struct apply_state'
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (8 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 09/83] builtin/apply: move 'check' " Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-26 20:25   ` Junio C Hamano
  2016-04-24 13:33 ` [PATCH 11/83] builtin/apply: move 'apply_in_reverse' " Christian Couder
                   ` (69 subsequent siblings)
  79 siblings, 1 reply; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 6c628f6..3f8671c 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;
@@ -3248,13 +3250,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) {
@@ -3280,7 +3283,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)
 {
@@ -3298,7 +3302,7 @@ static int load_preimage(struct image *image,
 		/* We have a patched copy in memory; use that. */
 		strbuf_add(&buf, previous->result, previous->resultsize);
 	} else {
-		status = load_patch_target(&buf, ce, st,
+		status = load_patch_target(state, &buf, ce, st,
 					   patch->old_name, patch->old_mode);
 		if (status < 0)
 			return status;
@@ -3357,7 +3361,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;
@@ -3384,7 +3390,7 @@ static int load_current(struct image *image, struct patch *patch)
 	if (verify_index_match(ce, &st))
 		return error(_("%s: does not match index"), name);
 
-	status = load_patch_target(&buf, ce, &st, name, mode);
+	status = load_patch_target(state, &buf, ce, &st, name, mode);
 	if (status < 0)
 		return status;
 	else if (status)
@@ -3434,11 +3440,11 @@ static int try_threeway(struct apply_state *state,
 
 	/* our_sha1[] is ours */
 	if (patch->is_new) {
-		if (load_current(&tmp_image, patch))
+		if (load_current(state, &tmp_image, patch))
 			return error("cannot read the current contents of '%s'",
 				     patch->new_name);
 	} else {
-		if (load_preimage(&tmp_image, patch, st, ce))
+		if (load_preimage(state, &tmp_image, patch, st, ce))
 			return error("cannot read the current contents of '%s'",
 				     patch->old_name);
 	}
@@ -3473,7 +3479,7 @@ static int apply_data(struct apply_state *state, struct patch *patch,
 {
 	struct image image;
 
-	if (load_preimage(&image, patch, st, ce) < 0)
+	if (load_preimage(state, &image, patch, st, ce) < 0)
 		return -1;
 
 	if (patch->direct_to_threeway ||
@@ -3504,7 +3510,10 @@ static int apply_data(struct apply_state *state, struct patch *patch,
  * check_patch() separately makes sure (and errors out otherwise) that
  * the path the patch creates does not exist in the current tree.
  */
-static int check_preimage(struct patch *patch, struct cache_entry **ce, struct stat *st)
+static int check_preimage(struct apply_state *state,
+			  struct patch *patch,
+			  struct cache_entry **ce,
+			  struct stat *st)
 {
 	const char *old_name = patch->old_name;
 	struct patch *previous = NULL;
@@ -3527,7 +3536,7 @@ static int check_preimage(struct patch *patch, struct cache_entry **ce, struct s
 			return error(_("%s: %s"), old_name, strerror(errno));
 	}
 
-	if (check_index && !previous) {
+	if (state->check_index && !previous) {
 		int pos = cache_name_pos(old_name, strlen(old_name));
 		if (pos < 0) {
 			if (patch->is_new < 0)
@@ -3577,11 +3586,13 @@ static int check_preimage(struct patch *patch, struct cache_entry **ce, struct s
 #define EXISTS_IN_INDEX 1
 #define EXISTS_IN_WORKTREE 2
 
-static int check_to_create(const char *new_name, int ok_if_exists)
+static int check_to_create(struct apply_state *state,
+			   const char *new_name,
+			   int ok_if_exists)
 {
 	struct stat nst;
 
-	if (check_index &&
+	if (state->check_index &&
 	    cache_name_pos(new_name, strlen(new_name)) >= 0 &&
 	    !ok_if_exists)
 		return EXISTS_IN_INDEX;
@@ -3657,7 +3668,7 @@ static void prepare_symlink_changes(struct patch *patch)
 	}
 }
 
-static int path_is_beyond_symlink_1(struct strbuf *name)
+static int path_is_beyond_symlink_1(struct apply_state *state, struct strbuf *name)
 {
 	do {
 		unsigned int change;
@@ -3678,7 +3689,7 @@ static int path_is_beyond_symlink_1(struct strbuf *name)
 			continue;
 
 		/* otherwise, check the preimage */
-		if (check_index) {
+		if (state->check_index) {
 			struct cache_entry *ce;
 
 			ce = cache_file_exists(name->buf, name->len, ignore_case);
@@ -3693,14 +3704,14 @@ static int path_is_beyond_symlink_1(struct strbuf *name)
 	return 0;
 }
 
-static int path_is_beyond_symlink(const char *name_)
+static int path_is_beyond_symlink(struct apply_state *state, const char *name_)
 {
 	int ret;
 	struct strbuf name = STRBUF_INIT;
 
 	assert(*name_ != '\0');
 	strbuf_addstr(&name, name_);
-	ret = path_is_beyond_symlink_1(&name);
+	ret = path_is_beyond_symlink_1(state, &name);
 	strbuf_release(&name);
 
 	return ret;
@@ -3740,7 +3751,7 @@ static int check_patch(struct apply_state *state, struct patch *patch)
 
 	patch->rejected = 1; /* we will drop this after we succeed */
 
-	status = check_preimage(patch, &ce, &st);
+	status = check_preimage(state, patch, &ce, &st);
 	if (status)
 		return status;
 	old_name = patch->old_name;
@@ -3767,7 +3778,7 @@ static int check_patch(struct apply_state *state, struct patch *patch)
 
 	if (new_name &&
 	    ((0 < patch->is_new) || patch->is_rename || patch->is_copy)) {
-		int err = check_to_create(new_name, ok_if_exists);
+		int err = check_to_create(state, new_name, ok_if_exists);
 
 		if (err && threeway) {
 			patch->direct_to_threeway = 1;
@@ -3822,7 +3833,7 @@ static int check_patch(struct apply_state *state, struct patch *patch)
 	 * is not deposited to a path that is beyond a symbolic link
 	 * here.
 	 */
-	if (!patch->is_delete && path_is_beyond_symlink(patch->new_name))
+	if (!patch->is_delete && path_is_beyond_symlink(state, patch->new_name))
 		return error(_("affected file '%s' is beyond a symbolic link"),
 			     patch->new_name);
 
@@ -4434,11 +4445,11 @@ static int apply_patch(struct apply_state *state,
 	if (whitespace_error && (ws_error_action == die_on_ws_error))
 		apply = 0;
 
-	update_index = check_index && apply;
+	update_index = state->check_index && apply;
 	if (update_index && newfd < 0)
 		newfd = hold_locked_index(&lock_file, 1);
 
-	if (check_index) {
+	if (state->check_index) {
 		if (read_cache() < 0)
 			die(_("unable to read index file"));
 	}
@@ -4565,7 +4576,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 			N_("instead of applying the patch, output a summary for the input")),
 		OPT_BOOL(0, "check", &state.check,
 			N_("instead of applying the patch, see if the patch is applicable")),
-		OPT_BOOL(0, "index", &check_index,
+		OPT_BOOL(0, "index", &state.check_index,
 			N_("make sure the patch is applicable to the current index")),
 		OPT_BOOL(0, "cached", &cached,
 			N_("apply a patch without touching the working tree")),
@@ -4632,20 +4643,20 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 	if (threeway) {
 		if (is_not_gitdir)
 			die(_("--3way outside a repository"));
-		check_index = 1;
+		state.check_index = 1;
 	}
 	if (apply_with_reject)
 		apply = apply_verbosely = 1;
 	if (!force_apply && (diffstat || numstat || summary || state.check || fake_ancestor))
 		apply = 0;
-	if (check_index && is_not_gitdir)
+	if (state.check_index && is_not_gitdir)
 		die(_("--index outside a repository"));
 	if (cached) {
 		if (is_not_gitdir)
 			die(_("--cached outside a repository"));
-		check_index = 1;
+		state.check_index = 1;
 	}
-	if (check_index)
+	if (state.check_index)
 		unsafe_paths = 0;
 
 	for (i = 0; i < argc; i++) {
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 11/83] builtin/apply: move 'apply_in_reverse' global into 'struct apply_state'
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (9 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 10/83] builtin/apply: move 'check_index' " Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 12/83] builtin/apply: move 'apply_with_reject' " Christian Couder
                   ` (68 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 builtin/apply.c | 51 ++++++++++++++++++++++++++++++++-------------------
 1 file changed, 32 insertions(+), 19 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 3f8671c..755e0e3 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -25,6 +25,8 @@ struct apply_state {
 	const char *prefix;
 	int prefix_length;
 
+	int apply_in_reverse;
+
 	/*
 	 *  --check turns on checking that the working tree matches the
 	 *    files that are being modified, but doesn't apply the patch
@@ -53,7 +55,6 @@ static int diffstat;
 static int numstat;
 static int summary;
 static int apply = 1;
-static int apply_in_reverse;
 static int apply_with_reject;
 static int apply_verbosely;
 static int allow_overlap;
@@ -1561,8 +1562,11 @@ static void check_whitespace(const char *line, int len, unsigned ws_rule)
  * between a "---" that is part of a patch, and a "---" that starts
  * the next patch is to look at the line counts..
  */
-static int parse_fragment(const char *line, unsigned long size,
-			  struct patch *patch, struct fragment *fragment)
+static int parse_fragment(struct apply_state *state,
+			  const char *line,
+			  unsigned long size,
+			  struct patch *patch,
+			  struct fragment *fragment)
 {
 	int added, deleted;
 	int len = linelen(line, size), offset;
@@ -1602,12 +1606,12 @@ static int parse_fragment(const char *line, unsigned long size,
 			if (!deleted && !added)
 				leading++;
 			trailing++;
-			if (!apply_in_reverse &&
+			if (!state->apply_in_reverse &&
 			    ws_error_action == correct_ws_error)
 				check_whitespace(line, len, patch->ws_rule);
 			break;
 		case '-':
-			if (apply_in_reverse &&
+			if (state->apply_in_reverse &&
 			    ws_error_action != nowarn_ws_error)
 				check_whitespace(line, len, patch->ws_rule);
 			deleted++;
@@ -1615,7 +1619,7 @@ static int parse_fragment(const char *line, unsigned long size,
 			trailing = 0;
 			break;
 		case '+':
-			if (!apply_in_reverse &&
+			if (!state->apply_in_reverse &&
 			    ws_error_action != nowarn_ws_error)
 				check_whitespace(line, len, patch->ws_rule);
 			added++;
@@ -1671,7 +1675,10 @@ static int parse_fragment(const char *line, unsigned long size,
  * The (fragment->patch, fragment->size) pair points into the memory given
  * by the caller, not a copy, when we return.
  */
-static int parse_single_patch(const char *line, unsigned long size, struct patch *patch)
+static int parse_single_patch(struct apply_state *state,
+			      const char *line,
+			      unsigned long size,
+			      struct patch *patch)
 {
 	unsigned long offset = 0;
 	unsigned long oldlines = 0, newlines = 0, context = 0;
@@ -1683,7 +1690,7 @@ static int parse_single_patch(const char *line, unsigned long size, struct patch
 
 		fragment = xcalloc(1, sizeof(*fragment));
 		fragment->linenr = linenr;
-		len = parse_fragment(line, size, patch, fragment);
+		len = parse_fragment(state, line, size, patch, fragment);
 		if (len <= 0)
 			die(_("corrupt patch at line %d"), linenr);
 		fragment->patch = line;
@@ -2013,8 +2020,10 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
 						 ? patch->new_name
 						 : patch->old_name);
 
-	patchsize = parse_single_patch(buffer + offset + hdrsize,
-				       size - offset - hdrsize, patch);
+	patchsize = parse_single_patch(state,
+				       buffer + offset + hdrsize,
+				       size - offset - hdrsize,
+				       patch);
 
 	if (!patchsize) {
 		static const char git_binary[] = "GIT binary patch\n";
@@ -2747,7 +2756,7 @@ static int apply_one_fragment(struct apply_state *state,
 		if (len < size && patch[len] == '\\')
 			plen--;
 		first = *patch;
-		if (apply_in_reverse) {
+		if (state->apply_in_reverse) {
 			if (first == '-')
 				first = '+';
 			else if (first == '+')
@@ -2920,7 +2929,7 @@ static int apply_one_fragment(struct apply_state *state,
 
 		if (apply_verbosely && applied_pos != pos) {
 			int offset = applied_pos - pos;
-			if (apply_in_reverse)
+			if (state->apply_in_reverse)
 				offset = 0 - offset;
 			fprintf_ln(stderr,
 				   Q_("Hunk #%d succeeded at %d (offset %d line).",
@@ -2954,7 +2963,9 @@ out:
 	return (applied_pos < 0);
 }
 
-static int apply_binary_fragment(struct image *img, struct patch *patch)
+static int apply_binary_fragment(struct apply_state *state,
+				 struct image *img,
+				 struct patch *patch)
 {
 	struct fragment *fragment = patch->fragments;
 	unsigned long len;
@@ -2967,7 +2978,7 @@ static int apply_binary_fragment(struct image *img, struct patch *patch)
 			     patch->old_name);
 
 	/* Binary patch is irreversible without the optional second hunk */
-	if (apply_in_reverse) {
+	if (state->apply_in_reverse) {
 		if (!fragment->next)
 			return error("cannot reverse-apply a binary patch "
 				     "without the reverse hunk to '%s'",
@@ -3000,7 +3011,9 @@ static int apply_binary_fragment(struct image *img, struct patch *patch)
  * but the preimage prepared by the caller in "img" is freed here
  * or in the helper function apply_binary_fragment() this calls.
  */
-static int apply_binary(struct image *img, struct patch *patch)
+static int apply_binary(struct apply_state *state,
+			struct image *img,
+			struct patch *patch)
 {
 	const char *name = patch->old_name ? patch->old_name : patch->new_name;
 	unsigned char sha1[20];
@@ -3061,7 +3074,7 @@ static int apply_binary(struct image *img, struct patch *patch)
 		 * apply the patch data to it, which is stored
 		 * in the patch->fragments->{patch,size}.
 		 */
-		if (apply_binary_fragment(img, patch))
+		if (apply_binary_fragment(state, img, patch))
 			return error(_("binary patch does not apply to '%s'"),
 				     name);
 
@@ -3084,7 +3097,7 @@ static int apply_fragments(struct apply_state *state, struct image *img, struct
 	int nth = 0;
 
 	if (patch->is_binary)
-		return apply_binary(img, patch);
+		return apply_binary(state, img, patch);
 
 	while (frag) {
 		nth++;
@@ -4423,7 +4436,7 @@ static int apply_patch(struct apply_state *state,
 			free_patch(patch);
 			break;
 		}
-		if (apply_in_reverse)
+		if (state->apply_in_reverse)
 			reverse_patches(patch);
 		if (use_patch(state, patch)) {
 			patch_stats(patch);
@@ -4602,7 +4615,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 		{ OPTION_CALLBACK, 0, "ignore-whitespace", NULL, NULL,
 			N_("ignore changes in whitespace when finding context"),
 			PARSE_OPT_NOARG, option_parse_space_change },
-		OPT_BOOL('R', "reverse", &apply_in_reverse,
+		OPT_BOOL('R', "reverse", &state.apply_in_reverse,
 			N_("apply the patch in reverse")),
 		OPT_BOOL(0, "unidiff-zero", &state.unidiff_zero,
 			N_("don't expect at least one line of context")),
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 12/83] builtin/apply: move 'apply_with_reject' global into 'struct apply_state'
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (10 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 11/83] builtin/apply: move 'apply_in_reverse' " Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 13/83] builtin/apply: move 'apply_verbosely' " Christian Couder
                   ` (67 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 755e0e3..67560e4 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;
@@ -3103,7 +3103,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;
 		}
@@ -4469,11 +4469,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;
@@ -4619,7 +4619,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 			N_("apply the patch in reverse")),
 		OPT_BOOL(0, "unidiff-zero", &state.unidiff_zero,
 			N_("don't expect at least one line of context")),
-		OPT_BOOL(0, "reject", &apply_with_reject,
+		OPT_BOOL(0, "reject", &state.apply_with_reject,
 			N_("leave the rejected hunks in corresponding *.rej files")),
 		OPT_BOOL(0, "allow-overlap", &allow_overlap,
 			N_("allow overlapping hunks")),
@@ -4649,7 +4649,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.");
@@ -4658,7 +4658,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.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 13/83] builtin/apply: move 'apply_verbosely' global into 'struct apply_state'
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (11 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 12/83] builtin/apply: move 'apply_with_reject' " Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 14/83] builtin/apply: move 'update_index' " Christian Couder
                   ` (66 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 67560e4..154679e 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;
@@ -2812,7 +2812,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;
@@ -2927,7 +2927,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;
@@ -2949,7 +2949,7 @@ static int apply_one_fragment(struct apply_state *state,
 				   leading, trailing, applied_pos+1);
 		update_image(img, applied_pos, &preimage, &postimage);
 	} else {
-		if (apply_verbosely)
+		if (state->apply_verbosely)
 			error(_("while searching for:\n%.*s"),
 			      (int)(old - oldlines), oldlines);
 	}
@@ -3863,7 +3863,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);
@@ -4294,7 +4294,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];
@@ -4309,7 +4309,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;
@@ -4365,7 +4365,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;
@@ -4380,7 +4380,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(&cpath, l->new_name);
@@ -4444,7 +4444,7 @@ static int apply_patch(struct apply_state *state,
 			listp = &patch->next;
 		}
 		else {
-			if (apply_verbosely)
+			if (state->apply_verbosely)
 				say_patch_name(stderr, _("Skipped patch '%s'."), patch);
 			free_patch(patch);
 			skipped_patch++;
@@ -4472,7 +4472,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->apply_with_reject)
 			exit(1);
 		/* with --3way, we still need to write the index out */
@@ -4623,7 +4623,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 			N_("leave the rejected hunks in corresponding *.rej files")),
 		OPT_BOOL(0, "allow-overlap", &allow_overlap,
 			N_("allow overlapping hunks")),
-		OPT__VERBOSE(&apply_verbosely, N_("be verbose")),
+		OPT__VERBOSE(&state.apply_verbosely, N_("be verbose")),
 		OPT_BIT(0, "inaccurate-eof", &options,
 			N_("tolerate incorrectly detected missing new-line at the end of file"),
 			INACCURATE_EOF),
@@ -4659,7 +4659,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 		state.check_index = 1;
 	}
 	if (state.apply_with_reject)
-		apply = apply_verbosely = 1;
+		apply = state.apply_verbosely = 1;
 	if (!force_apply && (diffstat || numstat || summary || state.check || fake_ancestor))
 		apply = 0;
 	if (state.check_index && is_not_gitdir)
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 14/83] builtin/apply: move 'update_index' global into 'struct apply_state'
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (12 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 13/83] builtin/apply: move 'apply_verbosely' " Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 15/83] builtin/apply: move 'allow_overlap' " Christian Couder
                   ` (65 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 builtin/apply.c | 46 +++++++++++++++++++++++++++-------------------
 1 file changed, 27 insertions(+), 19 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 154679e..b57be2c 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -39,6 +39,8 @@ struct apply_state {
 	int check_index;
 
 	int unidiff_zero;
+
+	int update_index;
 };
 
 /*
@@ -51,7 +53,6 @@ static int newfd = -1;
 
 static int state_p_value = 1;
 static int p_value_known;
-static int update_index;
 static int cached;
 static int diffstat;
 static int numstat;
@@ -4097,9 +4098,9 @@ static void patch_stats(struct patch *patch)
 	}
 }
 
-static void remove_file(struct patch *patch, int rmdir_empty)
+static void remove_file(struct apply_state *state, struct patch *patch, int rmdir_empty)
 {
-	if (update_index) {
+	if (state->update_index) {
 		if (remove_file_from_cache(patch->old_name) < 0)
 			die(_("unable to remove %s from index"), patch->old_name);
 	}
@@ -4110,14 +4111,18 @@ static void remove_file(struct patch *patch, int rmdir_empty)
 	}
 }
 
-static void add_index_file(const char *path, unsigned mode, void *buf, unsigned long size)
+static void add_index_file(struct apply_state *state,
+			   const char *path,
+			   unsigned mode,
+			   void *buf,
+			   unsigned long size)
 {
 	struct stat st;
 	struct cache_entry *ce;
 	int namelen = strlen(path);
 	unsigned ce_size = cache_entry_size(namelen);
 
-	if (!update_index)
+	if (!state->update_index)
 		return;
 
 	ce = xcalloc(1, ce_size);
@@ -4227,13 +4232,14 @@ static void create_one_file(char *path, unsigned mode, const char *buf, unsigned
 	die_errno(_("unable to write file '%s' mode %o"), path, mode);
 }
 
-static void add_conflicted_stages_file(struct patch *patch)
+static void add_conflicted_stages_file(struct apply_state *state,
+				       struct patch *patch)
 {
 	int stage, namelen;
 	unsigned ce_size, mode;
 	struct cache_entry *ce;
 
-	if (!update_index)
+	if (!state->update_index)
 		return;
 	namelen = strlen(patch->new_name);
 	ce_size = cache_entry_size(namelen);
@@ -4254,7 +4260,7 @@ static void add_conflicted_stages_file(struct patch *patch)
 	}
 }
 
-static void create_file(struct patch *patch)
+static void create_file(struct apply_state *state, struct patch *patch)
 {
 	char *path = patch->new_name;
 	unsigned mode = patch->new_mode;
@@ -4266,22 +4272,24 @@ static void create_file(struct patch *patch)
 	create_one_file(path, mode, buf, size);
 
 	if (patch->conflicted_threeway)
-		add_conflicted_stages_file(patch);
+		add_conflicted_stages_file(state, patch);
 	else
-		add_index_file(path, mode, buf, size);
+		add_index_file(state, path, mode, buf, size);
 }
 
 /* phase zero is to remove, phase one is to create */
-static void write_out_one_result(struct patch *patch, int phase)
+static void write_out_one_result(struct apply_state *state,
+				 struct patch *patch,
+				 int phase)
 {
 	if (patch->is_delete > 0) {
 		if (phase == 0)
-			remove_file(patch, 1);
+			remove_file(state, patch, 1);
 		return;
 	}
 	if (patch->is_new > 0 || patch->is_copy) {
 		if (phase == 1)
-			create_file(patch);
+			create_file(state, patch);
 		return;
 	}
 	/*
@@ -4289,9 +4297,9 @@ static void write_out_one_result(struct patch *patch, int phase)
 	 * thing: remove the old, write the new
 	 */
 	if (phase == 0)
-		remove_file(patch, patch->is_rename);
+		remove_file(state, patch, patch->is_rename);
 	if (phase == 1)
-		create_file(patch);
+		create_file(state, patch);
 }
 
 static int write_out_one_reject(struct apply_state *state, struct patch *patch)
@@ -4378,7 +4386,7 @@ static int write_out_results(struct apply_state *state, struct patch *list)
 			if (l->rejected)
 				errs = 1;
 			else {
-				write_out_one_result(l, phase);
+				write_out_one_result(state, l, phase);
 				if (phase == 1) {
 					if (write_out_one_reject(state, l))
 						errs = 1;
@@ -4458,8 +4466,8 @@ static int apply_patch(struct apply_state *state,
 	if (whitespace_error && (ws_error_action == die_on_ws_error))
 		apply = 0;
 
-	update_index = state->check_index && apply;
-	if (update_index && newfd < 0)
+	state->update_index = state->check_index && apply;
+	if (state->update_index && newfd < 0)
 		newfd = hold_locked_index(&lock_file, 1);
 
 	if (state->check_index) {
@@ -4723,7 +4731,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 				whitespace_error);
 	}
 
-	if (update_index) {
+	if (state.update_index) {
 		if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
 			die(_("Unable to write new index file"));
 	}
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 15/83] builtin/apply: move 'allow_overlap' global into 'struct apply_state'
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (13 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 14/83] builtin/apply: move 'update_index' " Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 16/83] builtin/apply: move 'cached' " Christian Couder
                   ` (64 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index b57be2c..a5dff99 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;
@@ -58,7 +59,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;
@@ -2635,7 +2635,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)
@@ -2700,7 +2701,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;
@@ -2948,7 +2949,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, &preimage, &postimage);
+		update_image(state, img, applied_pos, &preimage, &postimage);
 	} else {
 		if (state->apply_verbosely)
 			error(_("while searching for:\n%.*s"),
@@ -4629,7 +4630,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", &state.apply_with_reject,
 			N_("leave the rejected hunks in corresponding *.rej files")),
-		OPT_BOOL(0, "allow-overlap", &allow_overlap,
+		OPT_BOOL(0, "allow-overlap", &state.allow_overlap,
 			N_("allow overlapping hunks")),
 		OPT__VERBOSE(&state.apply_verbosely, N_("be verbose")),
 		OPT_BIT(0, "inaccurate-eof", &options,
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 16/83] builtin/apply: move 'cached' global into 'struct apply_state'
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (14 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 15/83] builtin/apply: move 'allow_overlap' " Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 17/83] builtin/apply: move 'diffstat' " Christian Couder
                   ` (63 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index a5dff99..ba828df 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
@@ -48,13 +51,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;
@@ -3272,7 +3273,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) {
@@ -3545,7 +3546,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));
@@ -3563,9 +3564,9 @@ static int check_preimage(struct apply_state *state,
 			if (checkout_target(&the_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)
@@ -3573,7 +3574,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)
@@ -3611,7 +3612,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, &nst)) {
@@ -4105,7 +4106,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);
 		}
@@ -4138,7 +4139,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, &st) < 0)
 				die_errno(_("unable to stat newly created file '%s'"),
 					  path);
@@ -4190,9 +4191,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 *state,
+			    char *path,
+			    unsigned mode,
+			    const char *buf,
+			    unsigned long size)
 {
-	if (cached)
+	if (state->cached)
 		return;
 	if (!try_create_file(path, mode, buf, size))
 		return;
@@ -4270,7 +4275,7 @@ static void create_file(struct apply_state *state, struct patch *patch)
 
 	if (!mode)
 		mode = S_IFREG | 0644;
-	create_one_file(path, mode, buf, size);
+	create_one_file(state, path, mode, buf, size);
 
 	if (patch->conflicted_threeway)
 		add_conflicted_stages_file(state, patch);
@@ -4600,7 +4605,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 			N_("instead of applying the patch, see if the patch is applicable")),
 		OPT_BOOL(0, "index", &state.check_index,
 			N_("make sure the patch is applicable to the current index")),
-		OPT_BOOL(0, "cached", &cached,
+		OPT_BOOL(0, "cached", &state.cached,
 			N_("apply a patch without touching the working tree")),
 		OPT_BOOL(0, "unsafe-paths", &unsafe_paths,
 			N_("accept a patch that touches outside the working area")),
@@ -4660,7 +4665,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 
 	if (state.apply_with_reject && threeway)
 		die("--reject and --3way cannot be used together.");
-	if (cached && threeway)
+	if (state.cached && threeway)
 		die("--cached and --3way cannot be used together.");
 	if (threeway) {
 		if (is_not_gitdir)
@@ -4673,7 +4678,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 		apply = 0;
 	if (state.check_index && is_not_gitdir)
 		die(_("--index outside a repository"));
-	if (cached) {
+	if (state.cached) {
 		if (is_not_gitdir)
 			die(_("--cached outside a repository"));
 		state.check_index = 1;
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 17/83] builtin/apply: move 'diffstat' global into 'struct apply_state'
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (15 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 16/83] builtin/apply: move 'cached' " Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 18/83] builtin/apply: move 'numstat' " Christian Couder
                   ` (62 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index ba828df..d90948a 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -33,6 +33,9 @@ struct apply_state {
 	/* --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;
+
 	/*
 	 *  --check turns on checking that the working tree matches the
 	 *    files that are being modified, but doesn't apply the patch
@@ -48,7 +51,6 @@ 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.
  */
@@ -56,7 +58,6 @@ static int newfd = -1;
 
 static int state_p_value = 1;
 static int p_value_known;
-static int diffstat;
 static int numstat;
 static int summary;
 static int apply = 1;
@@ -4496,7 +4497,7 @@ static int apply_patch(struct apply_state *state,
 	if (fake_ancestor)
 		build_fake_ancestor(list, fake_ancestor);
 
-	if (diffstat)
+	if (state->diffstat)
 		stat_patch_list(list);
 
 	if (numstat)
@@ -4593,7 +4594,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 			0, option_parse_p },
 		OPT_BOOL(0, "no-add", &no_add,
 			N_("ignore additions made by the patch")),
-		OPT_BOOL(0, "stat", &diffstat,
+		OPT_BOOL(0, "stat", &state.diffstat,
 			N_("instead of applying the patch, output diffstat for the input")),
 		OPT_NOOP_NOARG(0, "allow-binary-replacement"),
 		OPT_NOOP_NOARG(0, "binary"),
@@ -4674,7 +4675,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 && (diffstat || numstat || summary || state.check || fake_ancestor))
+	if (!force_apply && (state.diffstat || numstat || summary || state.check || fake_ancestor))
 		apply = 0;
 	if (state.check_index && is_not_gitdir)
 		die(_("--index outside a repository"));
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 18/83] builtin/apply: move 'numstat' global into 'struct apply_state'
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (16 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 17/83] builtin/apply: move 'diffstat' " Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-25 21:40   ` Stefan Beller
  2016-04-24 13:33 ` [PATCH 19/83] builtin/apply: move 'summary' " Christian Couder
                   ` (61 subsequent siblings)
  79 siblings, 1 reply; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index d90948a..16d78f9 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -36,6 +36,9 @@ struct apply_state {
 	/* --stat does just a diffstat, and doesn't actually apply */
 	int diffstat;
 
+	/* --numstat does numeric diffstat, and doesn't actually apply */
+	int numstat;
+
 	/*
 	 *  --check turns on checking that the working tree matches the
 	 *    files that are being modified, but doesn't apply the patch
@@ -51,14 +54,12 @@ struct apply_state {
 };
 
 /*
- *  --numstat does numeric diffstat, and doesn't actually apply
  *  --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 numstat;
 static int summary;
 static int apply = 1;
 static int no_add;
@@ -4500,7 +4501,7 @@ static int apply_patch(struct apply_state *state,
 	if (state->diffstat)
 		stat_patch_list(list);
 
-	if (numstat)
+	if (state->numstat)
 		numstat_patch_list(list);
 
 	if (summary)
@@ -4598,7 +4599,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 			N_("instead of applying the patch, output diffstat for the input")),
 		OPT_NOOP_NOARG(0, "allow-binary-replacement"),
 		OPT_NOOP_NOARG(0, "binary"),
-		OPT_BOOL(0, "numstat", &numstat,
+		OPT_BOOL(0, "numstat", &state.numstat,
 			N_("show number of added and deleted lines in decimal notation")),
 		OPT_BOOL(0, "summary", &summary,
 			N_("instead of applying the patch, output a summary for the input")),
@@ -4675,7 +4676,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 || numstat || summary || state.check || fake_ancestor))
+	if (!force_apply && (state.diffstat || state.numstat || summary || state.check || fake_ancestor))
 		apply = 0;
 	if (state.check_index && is_not_gitdir)
 		die(_("--index outside a repository"));
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 19/83] builtin/apply: move 'summary' global into 'struct apply_state'
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (17 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 18/83] builtin/apply: move 'numstat' " Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 20/83] builtin/apply: move 'threeway' " Christian Couder
                   ` (60 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 16d78f9..e488879 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
@@ -60,7 +62,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;
@@ -4504,7 +4505,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);
@@ -4601,7 +4602,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 		OPT_NOOP_NOARG(0, "binary"),
 		OPT_BOOL(0, "numstat", &state.numstat,
 			N_("show number of added and deleted lines in decimal notation")),
-		OPT_BOOL(0, "summary", &summary,
+		OPT_BOOL(0, "summary", &state.summary,
 			N_("instead of applying the patch, output a summary for the input")),
 		OPT_BOOL(0, "check", &state.check,
 			N_("instead of applying the patch, see if the patch is applicable")),
@@ -4676,7 +4677,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.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 20/83] builtin/apply: move 'threeway' global into 'struct apply_state'
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (18 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 19/83] builtin/apply: move 'summary' " Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 21/83] builtin/apply: move 'no-add' " Christian Couder
                   ` (59 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index e488879..33a1f8f 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -41,6 +41,8 @@ struct apply_state {
 
 	int summary;
 
+	int threeway;
+
 	/*
 	 *  --check turns on checking that the working tree matches the
 	 *    files that are being modified, but doesn't apply the patch
@@ -64,7 +66,6 @@ static int state_p_value = 1;
 static int p_value_known;
 static int apply = 1;
 static int no_add;
-static int threeway;
 static int unsafe_paths;
 static const char *fake_ancestor;
 static int line_termination = '\n';
@@ -3504,7 +3505,7 @@ static int apply_data(struct apply_state *state, struct patch *patch,
 	if (patch->direct_to_threeway ||
 	    apply_fragments(state, &image, patch) < 0) {
 		/* Note: with --reject, apply_fragments() returns 0 */
-		if (!threeway || try_threeway(state, &image, patch, st, ce) < 0)
+		if (!state->threeway || try_threeway(state, &image, patch, st, ce) < 0)
 			return -1;
 	}
 	patch->result = image.buf;
@@ -3799,7 +3800,7 @@ static int check_patch(struct apply_state *state, struct patch *patch)
 	    ((0 < patch->is_new) || patch->is_rename || patch->is_copy)) {
 		int err = check_to_create(state, new_name, ok_if_exists);
 
-		if (err && threeway) {
+		if (err && state->threeway) {
 			patch->direct_to_threeway = 1;
 		} else switch (err) {
 		case 0:
@@ -4614,7 +4615,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 			N_("accept a patch that touches outside the working area")),
 		OPT_BOOL(0, "apply", &force_apply,
 			N_("also apply the patch (use with --stat/--summary/--check)")),
-		OPT_BOOL('3', "3way", &threeway,
+		OPT_BOOL('3', "3way", &state.threeway,
 			 N_( "attempt three-way merge if a patch does not apply")),
 		OPT_FILENAME(0, "build-fake-ancestor", &fake_ancestor,
 			N_("build a temporary index based on embedded index information")),
@@ -4666,11 +4667,11 @@ 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 && threeway)
+	if (state.apply_with_reject && state.threeway)
 		die("--reject and --3way cannot be used together.");
-	if (state.cached && threeway)
+	if (state.cached && state.threeway)
 		die("--cached and --3way cannot be used together.");
-	if (threeway) {
+	if (state.threeway) {
 		if (is_not_gitdir)
 			die(_("--3way outside a repository"));
 		state.check_index = 1;
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 21/83] builtin/apply: move 'no-add' global into 'struct apply_state'
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (19 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 20/83] builtin/apply: move 'threeway' " Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 22/83] builtin/apply: move 'unsafe_paths' " Christian Couder
                   ` (58 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 33a1f8f..506357c 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -43,6 +43,8 @@ struct apply_state {
 
 	int threeway;
 
+	int no_add;
+
 	/*
 	 *  --check turns on checking that the working tree matches the
 	 *    files that are being modified, but doesn't apply the patch
@@ -65,7 +67,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';
@@ -2796,7 +2797,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;
@@ -4595,7 +4596,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 		{ OPTION_CALLBACK, 'p', NULL, NULL, N_("num"),
 			N_("remove <num> leading slashes from traditional diff paths"),
 			0, option_parse_p },
-		OPT_BOOL(0, "no-add", &no_add,
+		OPT_BOOL(0, "no-add", &state.no_add,
 			N_("ignore additions made by the patch")),
 		OPT_BOOL(0, "stat", &state.diffstat,
 			N_("instead of applying the patch, output diffstat for the input")),
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 22/83] builtin/apply: move 'unsafe_paths' global into 'struct apply_state'
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (20 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 21/83] builtin/apply: move 'no-add' " Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-26 20:27   ` Junio C Hamano
  2016-04-24 13:33 ` [PATCH 23/83] builtin/apply: move 'line_termination' " Christian Couder
                   ` (57 subsequent siblings)
  79 siblings, 1 reply; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 506357c..c45e481 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -57,6 +57,8 @@ struct apply_state {
 	int unidiff_zero;
 
 	int update_index;
+
+	int unsafe_paths;
 };
 
 /*
@@ -67,7 +69,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;
@@ -3842,7 +3843,7 @@ static int check_patch(struct apply_state *state, struct patch *patch)
 		}
 	}
 
-	if (!unsafe_paths)
+	if (!state->unsafe_paths)
 		die_on_unsafe_path(patch);
 
 	/*
@@ -4612,7 +4613,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", &state.cached,
 			N_("apply a patch without touching the working tree")),
-		OPT_BOOL(0, "unsafe-paths", &unsafe_paths,
+		OPT_BOOL(0, "unsafe-paths", &state.unsafe_paths,
 			N_("accept a patch that touches outside the working area")),
 		OPT_BOOL(0, "apply", &force_apply,
 			N_("also apply the patch (use with --stat/--summary/--check)")),
@@ -4689,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.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 23/83] builtin/apply: move 'line_termination' global into 'struct apply_state'
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (21 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 22/83] builtin/apply: move 'unsafe_paths' " Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 24/83] builtin/apply: move 'fake_ancestor' " Christian Couder
                   ` (56 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index c45e481..228595d 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -59,6 +59,8 @@ struct apply_state {
 	int update_index;
 
 	int unsafe_paths;
+
+	int line_termination;
 };
 
 /*
@@ -70,7 +72,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 [<options>] [<patch>...]"),
@@ -3993,7 +3994,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;
@@ -4002,7 +4004,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);
 	}
 }
 
@@ -4506,7 +4508,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);
@@ -4622,7 +4624,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 		OPT_FILENAME(0, "build-fake-ancestor", &fake_ancestor,
 			N_("build a temporary index based on embedded index information")),
 		/* Think twice before adding "--nul" synonym to this */
-		OPT_SET_INT('z', NULL, &line_termination,
+		OPT_SET_INT('z', NULL, &state.line_termination,
 			N_("paths are separated with NUL character"), '\0'),
 		OPT_INTEGER('C', NULL, &p_context,
 				N_("ensure at least <n> lines of context match")),
@@ -4659,6 +4661,7 @@ int cmd_apply(int argc, const char **argv, 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)
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 24/83] builtin/apply: move 'fake_ancestor' global into 'struct apply_state'
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (22 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 23/83] builtin/apply: move 'line_termination' " Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 25/83] builtin/apply: move 'p_context' " Christian Couder
                   ` (55 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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).

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 228595d..f69f982 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;
 
+	const char *fake_ancestor;
+
 	int summary;
 
 	int threeway;
@@ -63,15 +65,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 [<options>] [<patch>...]"),
@@ -4501,8 +4499,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);
@@ -4621,7 +4619,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", &state.threeway,
 			 N_( "attempt three-way merge if a patch does not apply")),
-		OPT_FILENAME(0, "build-fake-ancestor", &fake_ancestor,
+		OPT_FILENAME(0, "build-fake-ancestor", &state.fake_ancestor,
 			N_("build a temporary index based on embedded index information")),
 		/* Think twice before adding "--nul" synonym to this */
 		OPT_SET_INT('z', NULL, &state.line_termination,
@@ -4683,7 +4681,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.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 25/83] builtin/apply: move 'p_context' global into 'struct apply_state'
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (23 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 24/83] builtin/apply: move 'fake_ancestor' " Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 26/83] builtin/apply: move 'apply' " Christian Couder
                   ` (54 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index f69f982..b6d2343 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -63,6 +63,8 @@ struct apply_state {
 	int unsafe_paths;
 
 	int line_termination;
+
+	unsigned int p_context;
 };
 
 static int newfd = -1;
@@ -70,7 +72,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 [<options>] [<patch>...]"),
 	NULL
@@ -2888,7 +2889,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;
@@ -4624,7 +4625,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, &state.line_termination,
 			N_("paths are separated with NUL character"), '\0'),
-		OPT_INTEGER('C', NULL, &p_context,
+		OPT_INTEGER('C', NULL, &state.p_context,
 				N_("ensure at least <n> lines of context match")),
 		{ OPTION_CALLBACK, 0, "whitespace", &whitespace_option, N_("action"),
 			N_("detect new or modified lines that have whitespace errors"),
@@ -4660,6 +4661,7 @@ int cmd_apply(int argc, const char **argv, 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)
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 26/83] builtin/apply: move 'apply' global into 'struct apply_state'
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (24 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 25/83] builtin/apply: move 'p_context' " Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 27/83] builtin/apply: move 'read_stdin' global into cmd_apply() Christian Couder
                   ` (53 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index b6d2343..699cabf 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;
@@ -71,7 +72,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 [<options>] [<patch>...]"),
 	NULL
@@ -142,10 +143,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);
 }
 
 /*
@@ -2074,7 +2076,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"), linenr);
 	}
@@ -2933,7 +2935,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) {
@@ -4477,9 +4479,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(&lock_file, 1);
 
@@ -4488,12 +4490,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 */
@@ -4660,6 +4662,7 @@ int cmd_apply(int argc, const char **argv, 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;
 
@@ -4682,9 +4685,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) {
@@ -4712,11 +4715,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(&state, fd, arg, options);
 		close(fd);
 	}
-	set_default_whitespace_mode(whitespace_option);
+	set_default_whitespace_mode(&state, whitespace_option);
 	if (read_stdin)
 		errs |= apply_patch(&state, 0, "<stdin>", options);
 	if (whitespace_error) {
@@ -4734,7 +4737,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 			       "%d lines add whitespace errors.",
 			       whitespace_error),
 			    whitespace_error);
-		if (applied_after_fixing_ws && apply)
+		if (applied_after_fixing_ws && state.apply)
 			warning("%d line%s applied after"
 				" fixing whitespace errors.",
 				applied_after_fixing_ws,
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 27/83] builtin/apply: move 'read_stdin' global into cmd_apply()
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (25 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 26/83] builtin/apply: move 'apply' " Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-26 20:28   ` Junio C Hamano
  2016-04-24 13:33 ` [PATCH 28/83] builtin/apply: move 'patch_input_file' global into 'struct apply_state' Christian Couder
                   ` (52 subsequent siblings)
  79 siblings, 1 reply; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 699cabf..be237d1 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -96,7 +96,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)
 {
@@ -4586,6 +4585,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;
 	struct apply_state state;
 
 	const char *whitespace_option = NULL;
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 28/83] builtin/apply: move 'patch_input_file' global into 'struct apply_state'
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (26 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 27/83] builtin/apply: move 'read_stdin' global into cmd_apply() Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 29/83] builtin/apply: move 'limit_by_name' " Christian Couder
                   ` (51 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index be237d1..ab311c1 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -66,6 +66,8 @@ struct apply_state {
 	int line_termination;
 
 	unsigned int p_context;
+
+	const char *patch_input_file;
 };
 
 static int newfd = -1;
@@ -94,7 +96,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)
@@ -1540,7 +1541,11 @@ static int find_header(struct apply_state *state,
 	return -1;
 }
 
-static void record_ws_error(unsigned result, const char *line, int len, int l_nr)
+static void record_ws_error(struct apply_state *state,
+			    unsigned result,
+			    const char *line,
+			    int len,
+			    int l_nr)
 {
 	char *err;
 
@@ -1554,15 +1559,18 @@ static void record_ws_error(unsigned result, const char *line, int len, int l_nr
 
 	err = whitespace_error_string(result);
 	fprintf(stderr, "%s:%d: %s.\n%.*s\n",
-		patch_input_file, l_nr, err, len, line);
+		state->patch_input_file, l_nr, 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, linenr);
+	record_ws_error(state, result, line + 1, len - 2, linenr);
 }
 
 /*
@@ -1617,12 +1625,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;
@@ -1630,7 +1638,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;
@@ -2920,7 +2928,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--)
@@ -4443,7 +4451,7 @@ static int apply_patch(struct apply_state *state,
 	struct patch *list = NULL, **listp = &list;
 	int skipped_patch = 0;
 
-	patch_input_file = filename;
+	state->patch_input_file = filename;
 	read_patch_file(&buf, fd);
 	offset = 0;
 	while (offset < buf.len) {
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 29/83] builtin/apply: move 'limit_by_name' global into 'struct apply_state'
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (27 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 28/83] builtin/apply: move 'patch_input_file' global into 'struct apply_state' Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 30/83] builtin/apply: move 'has_include' " Christian Couder
                   ` (50 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index ab311c1..c8b9bf0 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -68,6 +68,8 @@ struct apply_state {
 	unsigned int p_context;
 
 	const char *patch_input_file;
+
+	struct string_list limit_by_name;
 };
 
 static int newfd = -1;
@@ -1974,13 +1976,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(&limit_by_name, name);
+	it = string_list_append(&state->limit_by_name, name);
 	it->util = exclude ? NULL : (void *) 1;
 }
 
@@ -1998,8 +2001,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 = &limit_by_name.items[i];
+	for (i = 0; i < state->limit_by_name.nr; i++) {
+		struct string_list_item *it = &state->limit_by_name.items[i];
 		if (!wildmatch(it->string, pathname, 0, NULL))
 			return (it->util != NULL);
 	}
@@ -4537,14 +4540,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;
 }
@@ -4599,10 +4604,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", &state, 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", &state, N_("path"),
 			N_("apply changes matching the given path"),
 			0, option_parse_include },
 		{ OPTION_CALLBACK, 'p', NULL, NULL, N_("num"),
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 30/83] builtin/apply: move 'has_include' global into 'struct apply_state'
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (28 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 29/83] builtin/apply: move 'limit_by_name' " Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 31/83] builtin/apply: move 'p_value' " Christian Couder
                   ` (49 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index c8b9bf0..0717cd2 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -70,6 +70,7 @@ struct apply_state {
 	const char *patch_input_file;
 
 	struct string_list limit_by_name;
+	int has_include;
 };
 
 static int newfd = -1;
@@ -1976,7 +1977,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)
@@ -2012,7 +2012,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;
 }
 
 
@@ -4550,7 +4550,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.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 31/83] builtin/apply: move 'p_value' global into 'struct apply_state'
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (29 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 30/83] builtin/apply: move 'has_include' " Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 32/83] builtin/apply: move 'p_value_known' " Christian Couder
                   ` (48 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 builtin/apply.c | 151 +++++++++++++++++++++++++++++++++++++-------------------
 1 file changed, 99 insertions(+), 52 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 0717cd2..979849c 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -65,6 +65,7 @@ struct apply_state {
 
 	int line_termination;
 
+	int p_value;
 	unsigned int p_context;
 
 	const char *patch_input_file;
@@ -75,7 +76,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[] = {
@@ -896,24 +896,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;
@@ -932,7 +932,9 @@ static void parse_traditional_patch(struct apply_state *state,
 		die(_("unable to find filename in patch at line %d"), 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;
 }
@@ -949,10 +951,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;
 	}
 
@@ -962,7 +968,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, 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") :
@@ -975,81 +981,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, &patch->old_name,
+	gitdiff_verify_name(state, line,
+			    patch->is_new, &patch->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, &patch->new_name,
+	gitdiff_verify_name(state, line,
+			    patch->is_delete, &patch->new_name,
 			    DIFF_NEW_NAME);
 	return 0;
 }
 
-static int gitdiff_oldmode(const char *line, struct patch *patch)
+static int gitdiff_oldmode(struct apply_state *state,
+			   const char *line,
+			   struct patch *patch)
 {
 	patch->old_mode = strtoul(line, NULL, 8);
 	return 0;
 }
 
-static int gitdiff_newmode(const char *line, struct patch *patch)
+static int gitdiff_newmode(struct apply_state *state,
+			   const char *line,
+			   struct patch *patch)
 {
 	patch->new_mode = strtoul(line, NULL, 8);
 	return 0;
 }
 
-static int gitdiff_delete(const char *line, struct patch *patch)
+static int gitdiff_delete(struct apply_state *state,
+			  const char *line,
+			  struct patch *patch)
 {
 	patch->is_delete = 1;
 	free(patch->old_name);
 	patch->old_name = xstrdup_or_null(patch->def_name);
-	return gitdiff_oldmode(line, patch);
+	return gitdiff_oldmode(state, line, patch);
 }
 
-static int gitdiff_newfile(const char *line, struct patch *patch)
+static int gitdiff_newfile(struct apply_state *state,
+			   const char *line,
+			   struct patch *patch)
 {
 	patch->is_new = 1;
 	free(patch->new_name);
 	patch->new_name = xstrdup_or_null(patch->def_name);
-	return gitdiff_newmode(line, patch);
+	return gitdiff_newmode(state, line, patch);
 }
 
-static int gitdiff_copysrc(const char *line, struct patch *patch)
+static int gitdiff_copysrc(struct apply_state *state,
+			   const char *line,
+			   struct patch *patch)
 {
 	patch->is_copy = 1;
 	free(patch->old_name);
-	patch->old_name = find_name(line, NULL, state_p_value ? state_p_value - 1 : 0, 0);
+	patch->old_name = find_name(line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
 	return 0;
 }
 
-static int gitdiff_copydst(const char *line, struct patch *patch)
+static int gitdiff_copydst(struct apply_state *state,
+			   const char *line,
+			   struct patch *patch)
 {
 	patch->is_copy = 1;
 	free(patch->new_name);
-	patch->new_name = find_name(line, NULL, state_p_value ? state_p_value - 1 : 0, 0);
+	patch->new_name = find_name(line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
 	return 0;
 }
 
-static int gitdiff_renamesrc(const char *line, struct patch *patch)
+static int gitdiff_renamesrc(struct apply_state *state,
+			     const char *line,
+			     struct patch *patch)
 {
 	patch->is_rename = 1;
 	free(patch->old_name);
-	patch->old_name = find_name(line, NULL, state_p_value ? state_p_value - 1 : 0, 0);
+	patch->old_name = find_name(line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
 	return 0;
 }
 
-static int gitdiff_renamedst(const char *line, struct patch *patch)
+static int gitdiff_renamedst(struct apply_state *state,
+			     const char *line,
+			     struct patch *patch)
 {
 	patch->is_rename = 1;
 	free(patch->new_name);
-	patch->new_name = find_name(line, NULL, state_p_value ? state_p_value - 1 : 0, 0);
+	patch->new_name = find_name(line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
 	return 0;
 }
 
-static int gitdiff_similarity(const char *line, struct patch *patch)
+static int gitdiff_similarity(struct apply_state *state,
+			      const char *line,
+			      struct patch *patch)
 {
 	unsigned long val = strtoul(line, NULL, 10);
 	if (val <= 100)
@@ -1057,7 +1087,9 @@ static int gitdiff_similarity(const char *line, struct patch *patch)
 	return 0;
 }
 
-static int gitdiff_dissimilarity(const char *line, struct patch *patch)
+static int gitdiff_dissimilarity(struct apply_state *state,
+				 const char *line,
+				 struct patch *patch)
 {
 	unsigned long val = strtoul(line, NULL, 10);
 	if (val <= 100)
@@ -1065,7 +1097,9 @@ static int gitdiff_dissimilarity(const char *line, struct patch *patch)
 	return 0;
 }
 
-static int gitdiff_index(const char *line, struct patch *patch)
+static int gitdiff_index(struct apply_state *state,
+			 const char *line,
+			 struct patch *patch)
 {
 	/*
 	 * index line is N hexadecimal, "..", N hexadecimal,
@@ -1102,7 +1136,9 @@ static int gitdiff_index(const char *line, struct patch *patch)
  * This is normal for a diff that doesn't change anything: we'll fall through
  * into the next diff. Tell the parser to break out.
  */
-static int gitdiff_unrecognized(const char *line, struct patch *patch)
+static int gitdiff_unrecognized(struct apply_state *state,
+				const char *line,
+				struct patch *patch)
 {
 	return -1;
 }
@@ -1111,15 +1147,17 @@ static int gitdiff_unrecognized(const char *line, struct patch *patch)
  * Skip p_value leading components from "line"; as we do not accept
  * absolute paths, return NULL in that case.
  */
-static const char *skip_tree_prefix(const char *line, int llen)
+static const char *skip_tree_prefix(struct apply_state *state,
+				    const char *line,
+				    int llen)
 {
 	int nslash;
 	int i;
 
-	if (!state_p_value)
+	if (!state->p_value)
 		return (llen && line[0] == '/') ? NULL : line;
 
-	nslash = state_p_value;
+	nslash = state->p_value;
 	for (i = 0; i < llen; i++) {
 		int ch = line[i];
 		if (ch == '/' && --nslash <= 0)
@@ -1136,7 +1174,9 @@ static const char *skip_tree_prefix(const char *line, int llen)
  * creation or deletion of an empty file.  In any of these cases,
  * both sides are the same name under a/ and b/ respectively.
  */
-static char *git_header_name(const char *line, int llen)
+static char *git_header_name(struct apply_state *state,
+			     const char *line,
+			     int llen)
 {
 	const char *name;
 	const char *second = NULL;
@@ -1154,7 +1194,7 @@ static char *git_header_name(const char *line, int llen)
 			goto free_and_fail1;
 
 		/* strip the a/b prefix including trailing slash */
-		cp = skip_tree_prefix(first.buf, first.len);
+		cp = skip_tree_prefix(state, first.buf, first.len);
 		if (!cp)
 			goto free_and_fail1;
 		strbuf_remove(&first, 0, cp - first.buf);
@@ -1171,7 +1211,7 @@ static char *git_header_name(const char *line, int llen)
 		if (*second == '"') {
 			if (unquote_c_style(&sp, second, NULL))
 				goto free_and_fail1;
-			cp = skip_tree_prefix(sp.buf, sp.len);
+			cp = skip_tree_prefix(state, sp.buf, sp.len);
 			if (!cp)
 				goto free_and_fail1;
 			/* They must match, otherwise ignore */
@@ -1182,7 +1222,7 @@ static char *git_header_name(const char *line, int llen)
 		}
 
 		/* unquoted second */
-		cp = skip_tree_prefix(second, line + llen - second);
+		cp = skip_tree_prefix(state, second, line + llen - second);
 		if (!cp)
 			goto free_and_fail1;
 		if (line + llen - cp != first.len ||
@@ -1197,7 +1237,7 @@ static char *git_header_name(const char *line, int llen)
 	}
 
 	/* unquoted first name */
-	name = skip_tree_prefix(line, llen);
+	name = skip_tree_prefix(state, line, llen);
 	if (!name)
 		return NULL;
 
@@ -1213,7 +1253,7 @@ static char *git_header_name(const char *line, int llen)
 			if (unquote_c_style(&sp, second, NULL))
 				goto free_and_fail2;
 
-			np = skip_tree_prefix(sp.buf, sp.len);
+			np = skip_tree_prefix(state, sp.buf, sp.len);
 			if (!np)
 				goto free_and_fail2;
 
@@ -1257,7 +1297,7 @@ static char *git_header_name(const char *line, int llen)
 			 */
 			if (!name[len + 1])
 				return NULL; /* no postimage name */
-			second = skip_tree_prefix(name + len + 1,
+			second = skip_tree_prefix(state, name + len + 1,
 						  line_len - (len + 1));
 			if (!second)
 				return NULL;
@@ -1273,7 +1313,11 @@ static char *git_header_name(const char *line, int llen)
 }
 
 /* Verify that we recognize the lines following a git header */
-static int parse_git_header(const char *line, int len, unsigned int size, struct patch *patch)
+static int parse_git_header(struct apply_state *state,
+			    const char *line,
+			    int len,
+			    unsigned int size,
+			    struct patch *patch)
 {
 	unsigned long offset;
 
@@ -1287,7 +1331,7 @@ static int parse_git_header(const char *line, int len, unsigned int size, struct
 	 * or removing or adding empty files), so we get
 	 * the default name from the header.
 	 */
-	patch->def_name = git_header_name(line, len);
+	patch->def_name = git_header_name(state, line, len);
 	if (patch->def_name && root.len) {
 		char *s = xstrfmt("%s%s", root.buf, patch->def_name);
 		free(patch->def_name);
@@ -1300,7 +1344,7 @@ static int parse_git_header(const char *line, int len, unsigned int size, struct
 	for (offset = len ; size > 0 ; offset += len, size -= len, line += len, linenr++) {
 		static const struct opentry {
 			const char *str;
-			int (*fn)(const char *, struct patch *);
+			int (*fn)(struct apply_state *, const char *, struct patch *);
 		} optable[] = {
 			{ "@@ -", gitdiff_hdrend },
 			{ "--- ", gitdiff_oldname },
@@ -1330,7 +1374,7 @@ static int parse_git_header(const char *line, int len, unsigned int size, struct
 			int oplen = strlen(p->str);
 			if (len < oplen || memcmp(p->str, line, oplen))
 				continue;
-			if (p->fn(line + oplen, patch) < 0)
+			if (p->fn(state, line + oplen, patch) < 0)
 				return offset;
 			break;
 		}
@@ -1500,7 +1544,7 @@ static int find_header(struct apply_state *state,
 		 * or mode change, so we handle that specially
 		 */
 		if (!memcmp("diff --git ", line, 11)) {
-			int git_hdr_len = parse_git_header(line, len, size, patch);
+			int git_hdr_len = parse_git_header(state, line, len, size, patch);
 			if (git_hdr_len <= len)
 				continue;
 			if (!patch->old_name && !patch->new_name) {
@@ -1509,8 +1553,8 @@ static int find_header(struct apply_state *state,
 					       "%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, linenr);
+					       state->p_value),
+					    state->p_value, linenr);
 				patch->old_name = xstrdup(patch->def_name);
 				patch->new_name = xstrdup(patch->def_name);
 			}
@@ -4555,9 +4599,11 @@ static int option_parse_include(const struct option *opt,
 }
 
 static int option_parse_p(const struct option *opt,
-			  const char *arg, int unset)
+			  const char *arg,
+			  int unset)
 {
-	state_p_value = atoi(arg);
+	struct apply_state *state = opt->value;
+	state->p_value = atoi(arg);
 	p_value_known = 1;
 	return 0;
 }
@@ -4610,7 +4656,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 		{ OPTION_CALLBACK, 0, "include", &state, N_("path"),
 			N_("apply changes matching the given path"),
 			0, option_parse_include },
-		{ OPTION_CALLBACK, 'p', NULL, NULL, N_("num"),
+		{ OPTION_CALLBACK, 'p', NULL, &state, N_("num"),
 			N_("remove <num> leading slashes from traditional diff paths"),
 			0, option_parse_p },
 		OPT_BOOL(0, "no-add", &state.no_add,
@@ -4677,6 +4723,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 	state.prefix_length = state.prefix ? strlen(state.prefix) : 0;
 	state.apply = 1;
 	state.line_termination = '\n';
+	state.p_value = 1;
 	state.p_context = UINT_MAX;
 
 	git_apply_config();
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 32/83] builtin/apply: move 'p_value_known' global into 'struct apply_state'
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (30 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 31/83] builtin/apply: move 'p_value' " Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 33/83] builtin/apply: move 'root' " Christian Couder
                   ` (47 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 979849c..fecdb66 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -66,6 +66,7 @@ struct apply_state {
 	int line_termination;
 
 	int p_value;
+	int p_value_known;
 	unsigned int p_context;
 
 	const char *patch_input_file;
@@ -76,8 +77,6 @@ struct apply_state {
 
 static int newfd = -1;
 
-static int p_value_known;
-
 static const char * const apply_usage[] = {
 	N_("git apply [<options>] [<patch>...]"),
 	NULL
@@ -890,14 +889,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)) {
@@ -4604,7 +4603,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.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 33/83] builtin/apply: move 'root' global into 'struct apply_state'
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (31 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 32/83] builtin/apply: move 'p_value_known' " Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-25 21:50   ` Stefan Beller
  2016-04-24 13:33 ` [PATCH 34/83] builtin/apply: move 'whitespace_error' " Christian Couder
                   ` (46 subsequent siblings)
  79 siblings, 1 reply; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 builtin/apply.c | 82 ++++++++++++++++++++++++++++++++++-----------------------
 1 file changed, 49 insertions(+), 33 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index fecdb66..209a1b4 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -73,6 +73,8 @@ struct apply_state {
 
 	struct string_list limit_by_name;
 	int has_include;
+
+	struct strbuf root;
 };
 
 static int newfd = -1;
@@ -98,8 +100,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) {
@@ -489,7 +489,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;
@@ -513,8 +516,8 @@ static char *find_name_gnu(const char *line, const char *def, int p_value)
 	}
 
 	strbuf_remove(&name, 0, cp - name.buf);
-	if (root.len)
-		strbuf_insert(&name, 0, root.buf, root.len);
+	if (state->root.len)
+		strbuf_insert(&name, 0, state->root.buf, state->root.len);
 	return squash_slash(strbuf_detach(&name, NULL));
 }
 
@@ -677,8 +680,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;
@@ -716,32 +723,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;
 	}
@@ -749,10 +763,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)
@@ -777,7 +791,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_traditional(state, nameline, NULL, 0);
 	if (!name)
 		return -1;
 	cp = strchr(name, '/');
@@ -902,17 +916,17 @@ static void parse_traditional_patch(struct apply_state *state,
 	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(state, 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(state, 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(state, first, NULL, state->p_value);
+		name = find_name_traditional(state, second, first_name, state->p_value);
 		free(first_name);
 		if (has_epoch_timestamp(first)) {
 			patch->is_new = 1;
@@ -957,7 +971,7 @@ static void gitdiff_verify_name(struct apply_state *state,
 				int side)
 {
 	if (!*name && !isnull) {
-		*name = find_name(line, NULL, state->p_value, TERM_TAB);
+		*name = find_name(state, line, NULL, state->p_value, TERM_TAB);
 		return;
 	}
 
@@ -967,7 +981,7 @@ static void gitdiff_verify_name(struct apply_state *state,
 		if (isnull)
 			die(_("git apply: bad git-diff - expected /dev/null, got %s on line %d"),
 			    *name, linenr);
-		another = find_name(line, NULL, state->p_value, TERM_TAB);
+		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") :
@@ -1042,7 +1056,7 @@ static int gitdiff_copysrc(struct apply_state *state,
 {
 	patch->is_copy = 1;
 	free(patch->old_name);
-	patch->old_name = find_name(line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
+	patch->old_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
 	return 0;
 }
 
@@ -1052,7 +1066,7 @@ static int gitdiff_copydst(struct apply_state *state,
 {
 	patch->is_copy = 1;
 	free(patch->new_name);
-	patch->new_name = find_name(line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
+	patch->new_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
 	return 0;
 }
 
@@ -1062,7 +1076,7 @@ static int gitdiff_renamesrc(struct apply_state *state,
 {
 	patch->is_rename = 1;
 	free(patch->old_name);
-	patch->old_name = find_name(line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
+	patch->old_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
 	return 0;
 }
 
@@ -1072,7 +1086,7 @@ static int gitdiff_renamedst(struct apply_state *state,
 {
 	patch->is_rename = 1;
 	free(patch->new_name);
-	patch->new_name = find_name(line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
+	patch->new_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
 	return 0;
 }
 
@@ -1331,8 +1345,8 @@ static int parse_git_header(struct apply_state *state,
 	 * the default name from the header.
 	 */
 	patch->def_name = git_header_name(state, line, len);
-	if (patch->def_name && root.len) {
-		char *s = xstrfmt("%s%s", root.buf, patch->def_name);
+	if (patch->def_name && state->root.len) {
+		char *s = xstrfmt("%s%s", state->root.buf, patch->def_name);
 		free(patch->def_name);
 		patch->def_name = s;
 	}
@@ -4630,9 +4644,10 @@ static int option_parse_whitespace(const struct option *opt,
 static int option_parse_directory(const struct option *opt,
 				  const char *arg, int unset)
 {
-	strbuf_reset(&root);
-	strbuf_addstr(&root, arg);
-	strbuf_complete(&root, '/');
+	struct apply_state *state = opt->value;
+	strbuf_reset(&state->root);
+	strbuf_addstr(&state->root, arg);
+	strbuf_complete(&state->root, '/');
 	return 0;
 }
 
@@ -4711,7 +4726,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 		OPT_BIT(0, "recount", &options,
 			N_("do not trust the line counts in the hunk headers"),
 			RECOUNT),
-		{ OPTION_CALLBACK, 0, "directory", NULL, N_("root"),
+		{ OPTION_CALLBACK, 0, "directory", &state, N_("root"),
 			N_("prepend <root> to all filenames"),
 			0, option_parse_directory },
 		OPT_END()
@@ -4724,6 +4739,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 	state.line_termination = '\n';
 	state.p_value = 1;
 	state.p_context = UINT_MAX;
+	strbuf_init(&state.root, 0);
 
 	git_apply_config();
 	if (apply_default_whitespace)
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 34/83] builtin/apply: move 'whitespace_error' global into 'struct apply_state'
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (32 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 33/83] builtin/apply: move 'root' " Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 35/83] builtin/apply: move 'whitespace_option' " Christian Couder
                   ` (45 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 209a1b4..9c4f67f 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -75,6 +75,8 @@ struct apply_state {
 	int has_include;
 
 	struct strbuf root;
+
+	int whitespace_error;
 };
 
 static int newfd = -1;
@@ -90,7 +92,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;
 
@@ -1612,9 +1613,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);
@@ -2872,7 +2873,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(&newlines, patch + 1, plen);
 			}
@@ -4545,7 +4546,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;
@@ -4797,11 +4798,11 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 	set_default_whitespace_mode(&state, whitespace_option);
 	if (read_stdin)
 		errs |= apply_patch(&state, 0, "<stdin>", 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),
@@ -4810,18 +4811,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.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 35/83] builtin/apply: move 'whitespace_option' into 'struct apply_state'
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (33 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 34/83] builtin/apply: move 'whitespace_error' " Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 36/83] builtin/apply: remove whitespace_option arg from set_default_whitespace_mode() Christian Couder
                   ` (44 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

This will enable further refactoring.

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 9c4f67f..64bc3e9 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -76,6 +76,7 @@ struct apply_state {
 
 	struct strbuf root;
 
+	const char *whitespace_option;
 	int whitespace_error;
 };
 
@@ -4635,9 +4636,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;
 }
@@ -4662,8 +4663,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", &state, N_("path"),
 			N_("don't apply changes matching the given path"),
@@ -4703,7 +4702,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 			N_("paths are separated with NUL character"), '\0'),
 		OPT_INTEGER('C', NULL, &state.p_context,
 				N_("ensure at least <n> lines of context match")),
-		{ OPTION_CALLBACK, 0, "whitespace", &whitespace_option, N_("action"),
+		{ OPTION_CALLBACK, 0, "whitespace", &state, N_("action"),
 			N_("detect new or modified lines that have whitespace errors"),
 			0, option_parse_whitespace },
 		{ OPTION_CALLBACK, 0, "ignore-space-change", NULL, NULL,
@@ -4791,11 +4790,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(&state, state.whitespace_option);
 		errs |= apply_patch(&state, fd, arg, options);
 		close(fd);
 	}
-	set_default_whitespace_mode(&state, whitespace_option);
+	set_default_whitespace_mode(&state, state.whitespace_option);
 	if (read_stdin)
 		errs |= apply_patch(&state, 0, "<stdin>", options);
 	if (state.whitespace_error) {
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 36/83] builtin/apply: remove whitespace_option arg from set_default_whitespace_mode()
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (34 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 35/83] builtin/apply: move 'whitespace_option' " Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 37/83] builtin/apply: move 'squelch_whitespace_errors' into 'struct apply_state' Christian Couder
                   ` (43 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 64bc3e9..401e111 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -147,10 +147,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);
 }
 
@@ -4790,11 +4789,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, state.whitespace_option);
+		set_default_whitespace_mode(&state);
 		errs |= apply_patch(&state, fd, arg, options);
 		close(fd);
 	}
-	set_default_whitespace_mode(&state, state.whitespace_option);
+	set_default_whitespace_mode(&state);
 	if (read_stdin)
 		errs |= apply_patch(&state, 0, "<stdin>", options);
 	if (state.whitespace_error) {
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 37/83] builtin/apply: move 'squelch_whitespace_errors' into 'struct apply_state'
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (35 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 36/83] builtin/apply: remove whitespace_option arg from set_default_whitespace_mode() Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 38/83] builtin/apply: move 'applied_after_fixing_ws' " Christian Couder
                   ` (42 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 401e111..259f256 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -78,6 +78,7 @@ struct apply_state {
 
 	const char *whitespace_option;
 	int whitespace_error;
+	int squelch_whitespace_errors;
 };
 
 static int newfd = -1;
@@ -93,7 +94,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 {
@@ -102,7 +102,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;
@@ -122,7 +122,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")) {
@@ -1614,8 +1614,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);
@@ -4636,9 +4636,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;
 }
 
@@ -4738,11 +4737,12 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 	state.line_termination = '\n';
 	state.p_value = 1;
 	state.p_context = UINT_MAX;
+	state.squelch_whitespace_errors = 5;
 	strbuf_init(&state.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);
 
@@ -4797,10 +4797,10 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 	if (read_stdin)
 		errs |= apply_patch(&state, 0, "<stdin>", 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.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 38/83] builtin/apply: move 'applied_after_fixing_ws' into 'struct apply_state'
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (36 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 37/83] builtin/apply: move 'squelch_whitespace_errors' into 'struct apply_state' Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 39/83] builtin/apply: move 'ws_error_action' " Christian Couder
                   ` (41 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 259f256..52602af 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -79,6 +79,7 @@ struct apply_state {
 	const char *whitespace_option;
 	int whitespace_error;
 	int squelch_whitespace_errors;
+	int applied_after_fixing_ws;
 };
 
 static int newfd = -1;
@@ -94,7 +95,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,
@@ -2878,7 +2878,7 @@ static int apply_one_fragment(struct apply_state *state,
 				strbuf_add(&newlines, patch + 1, plen);
 			}
 			else {
-				ws_fix_copy(&newlines, patch + 1, plen, ws_rule, &applied_after_fixing_ws);
+				ws_fix_copy(&newlines, patch + 1, plen, ws_rule, &state->applied_after_fixing_ws);
 			}
 			add_line_info(&postimage, newlines.buf + start, newlines.len - start,
 				      (first == '+' ? 0 : LINE_COMMON));
@@ -4811,11 +4811,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.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 39/83] builtin/apply: move 'ws_error_action' into 'struct apply_state'
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (37 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 38/83] builtin/apply: move 'applied_after_fixing_ws' " Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-26 20:36   ` Junio C Hamano
  2016-04-24 13:33 ` [PATCH 40/83] builtin/apply: move 'ws_ignore_action' " Christian Couder
                   ` (40 subsequent siblings)
  79 siblings, 1 reply; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 52602af..7d91454 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;
@@ -80,6 +87,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;
@@ -89,12 +98,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,
@@ -105,28 +108,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);
@@ -150,7 +153,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);
 }
 
 /*
@@ -1685,12 +1688,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--;
@@ -1698,7 +1701,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--;
@@ -2411,7 +2414,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,
@@ -2432,7 +2436,7 @@ static int match_fragment(struct image *img,
 		preimage_limit = preimage->nr;
 		if (match_end && (preimage->nr + try_lno != img->nr))
 			return 0;
-	} else if (ws_error_action == correct_ws_error &&
+	} else if (state->ws_error_action == correct_ws_error &&
 		   (ws_rule & WS_BLANK_AT_EOF)) {
 		/*
 		 * This hunk extends beyond the end of img, and we are
@@ -2505,7 +2509,7 @@ static int match_fragment(struct image *img,
 		return line_by_line_fuzzy_match(img, preimage, postimage,
 						try, try_lno, preimage_limit);
 
-	if (ws_error_action != correct_ws_error)
+	if (state->ws_error_action != correct_ws_error)
 		return 0;
 
 	/*
@@ -2617,7 +2621,8 @@ static int match_fragment(struct image *img,
 	return 0;
 }
 
-static int find_pos(struct image *img,
+static int find_pos(struct apply_state *state,
+		    struct image *img,
 		    struct image *preimage,
 		    struct image *postimage,
 		    int line,
@@ -2661,7 +2666,7 @@ static int find_pos(struct image *img,
 	try_lno = line;
 
 	for (i = 0; ; i++) {
-		if (match_fragment(img, preimage, postimage,
+		if (match_fragment(state, img, preimage, postimage,
 				   try, try_lno, ws_rule,
 				   match_beginning, match_end))
 			return try_lno;
@@ -2874,7 +2879,7 @@ static int apply_one_fragment(struct apply_state *state,
 			start = newlines.len;
 			if (first != '+' ||
 			    !state->whitespace_error ||
-			    ws_error_action != correct_ws_error) {
+			    state->ws_error_action != correct_ws_error) {
 				strbuf_add(&newlines, patch + 1, plen);
 			}
 			else {
@@ -2952,7 +2957,7 @@ static int apply_one_fragment(struct apply_state *state,
 
 	for (;;) {
 
-		applied_pos = find_pos(img, &preimage, &postimage, pos,
+		applied_pos = find_pos(state, img, &preimage, &postimage, pos,
 				       ws_rule, match_beginning, match_end);
 
 		if (applied_pos >= 0)
@@ -2988,10 +2993,10 @@ static int apply_one_fragment(struct apply_state *state,
 		if (new_blank_lines_at_end &&
 		    preimage.nr + applied_pos >= img->nr &&
 		    (ws_rule & WS_BLANK_AT_EOF) &&
-		    ws_error_action != nowarn_ws_error) {
+		    state->ws_error_action != nowarn_ws_error) {
 			record_ws_error(state, WS_BLANK_AT_EOF, "+", 1,
 					found_new_blank_lines_at_end);
-			if (ws_error_action == correct_ws_error) {
+			if (state->ws_error_action == correct_ws_error) {
 				while (new_blank_lines_at_end--)
 					remove_last_line(&postimage);
 			}
@@ -3002,7 +3007,7 @@ static int apply_one_fragment(struct apply_state *state,
 			 * apply_patch->check_patch_list->check_patch->
 			 * apply_data->apply_fragments->apply_one_fragment
 			 */
-			if (ws_error_action == die_on_ws_error)
+			if (state->ws_error_action == die_on_ws_error)
 				state->apply = 0;
 		}
 
@@ -4546,7 +4551,7 @@ static int apply_patch(struct apply_state *state,
 	if (!list && !skipped_patch)
 		die(_("unrecognized input"));
 
-	if (state->whitespace_error && (ws_error_action == die_on_ws_error))
+	if (state->whitespace_error && (state->ws_error_action == die_on_ws_error))
 		state->apply = 0;
 
 	state->update_index = state->check_index && state->apply;
@@ -4738,6 +4743,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 	state.p_value = 1;
 	state.p_context = UINT_MAX;
 	state.squelch_whitespace_errors = 5;
+	state.ws_error_action = warn_on_ws_error;
 	strbuf_init(&state.root, 0);
 
 	git_apply_config();
@@ -4806,7 +4812,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 				   squelched),
 				squelched);
 		}
-		if (ws_error_action == die_on_ws_error)
+		if (state.ws_error_action == die_on_ws_error)
 			die(Q_("%d line adds whitespace errors.",
 			       "%d lines add whitespace errors.",
 			       state.whitespace_error),
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 40/83] builtin/apply: move 'ws_ignore_action' into 'struct apply_state'
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (38 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 39/83] builtin/apply: move 'ws_error_action' " Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 41/83] builtin/apply: move 'max_change' and 'max_len' " Christian Couder
                   ` (39 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 7d91454..fa07b33 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;
@@ -89,6 +95,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;
@@ -98,13 +105,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) {
@@ -135,16 +135,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);
@@ -2505,7 +2506,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);
 
@@ -4628,12 +4629,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;
 }
 
@@ -4708,10 +4710,10 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 		{ OPTION_CALLBACK, 0, "whitespace", &state, 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", &state, 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", &state, NULL,
 			N_("ignore changes in whitespace when finding context"),
 			PARSE_OPT_NOARG, option_parse_space_change },
 		OPT_BOOL('R', "reverse", &state.apply_in_reverse,
@@ -4744,13 +4746,14 @@ int cmd_apply(int argc, const char **argv, 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(&state.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);
 
 	argc = parse_options(argc, argv, state.prefix, builtin_apply_options,
 			apply_usage, 0);
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 41/83] builtin/apply: move 'max_change' and 'max_len' into 'struct apply_state'
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (39 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 40/83] builtin/apply: move 'ws_ignore_action' " Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 42/83] builtin/apply: move 'linenr' global " Christian Couder
                   ` (38 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index fa07b33..bb58174 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -78,6 +78,14 @@ struct apply_state {
 
 	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;
@@ -158,13 +166,6 @@ static void set_default_whitespace_mode(struct apply_state *state)
 }
 
 /*
- * 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_xxxx"
  * things are flags, where -1 means "don't know yet".
@@ -2188,7 +2189,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;
@@ -2199,7 +2200,7 @@ static void show_stats(struct patch *patch)
 	/*
 	 * "scale" the filename
 	 */
-	max = max_len;
+	max = state->max_len;
 	if (max > 50)
 		max = 50;
 
@@ -2222,13 +2223,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,
@@ -4055,7 +4056,7 @@ static void build_fake_ancestor(struct patch *list, const char *filename)
 	discard_index(&result);
 }
 
-static void stat_patch_list(struct patch *patch)
+static void stat_patch_list(struct apply_state *state, struct patch *patch)
 {
 	int files, adds, dels;
 
@@ -4063,7 +4064,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);
@@ -4161,25 +4162,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;
 	}
 }
 
@@ -4536,7 +4537,7 @@ static int apply_patch(struct apply_state *state,
 		if (state->apply_in_reverse)
 			reverse_patches(patch);
 		if (use_patch(state, patch)) {
-			patch_stats(patch);
+			patch_stats(state, patch);
 			*listp = patch;
 			listp = &patch->next;
 		}
@@ -4580,7 +4581,7 @@ static int apply_patch(struct apply_state *state,
 		build_fake_ancestor(list, state->fake_ancestor);
 
 	if (state->diffstat)
-		stat_patch_list(list);
+		stat_patch_list(state, list);
 
 	if (state->numstat)
 		numstat_patch_list(state, list);
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 42/83] builtin/apply: move 'linenr' global into 'struct apply_state'
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (40 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 41/83] builtin/apply: move 'max_change' and 'max_len' " Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 43/83] builtin/apply: move 'fn_table' " Christian Couder
                   ` (37 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index bb58174..f387340 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -86,6 +86,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_xxxx"
+	 * things are flags, where -1 means "don't know yet".
+	 */
+	int linenr;
+
 	int p_value;
 	int p_value_known;
 	unsigned int p_context;
@@ -166,13 +173,6 @@ static void set_default_whitespace_mode(struct apply_state *state)
 }
 
 /*
- * Various "current state", notably line numbers and what
- * file (and how) we're patching right now.. The "is_xxxx"
- * things are flags, where -1 means "don't know yet".
- */
-static int linenr = 1;
-
-/*
  * This represents one "hunk" from a patch, starting with
  * "@@ -oldpos,oldlines +newpos,newlines @@" marker.  The
  * patch text is pointed at by patch, and its byte length
@@ -948,7 +948,7 @@ static void parse_traditional_patch(struct apply_state *state,
 		}
 	}
 	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(struct apply_state *state,
@@ -986,17 +986,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, 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"), 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);
 	}
 }
 
@@ -1359,8 +1359,8 @@ static int parse_git_header(struct apply_state *state,
 
 	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)(struct apply_state *, const char *, struct patch *);
@@ -1531,7 +1531,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, linenr++) {
+	for (offset = 0; size > 0; offset += len, size -= len, line += len, state->linenr++) {
 		unsigned long nextlen;
 
 		len = linelen(line, size);
@@ -1552,7 +1552,7 @@ static int find_header(struct apply_state *state,
 			if (parse_fragment_header(line, len, &dummy) < 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)
@@ -1573,13 +1573,13 @@ static int find_header(struct apply_state *state,
 					       "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->is_delete && !patch->new_name)
 				die("git diff header lacks filename information "
-				    "(line %d)", linenr);
+				    "(line %d)", state->linenr);
 			patch->is_toplevel_relative = 1;
 			*hdrsize = git_hdr_len;
 			return offset;
@@ -1601,7 +1601,7 @@ static int find_header(struct apply_state *state,
 		/* Ok, we'll consider it a patch */
 		parse_traditional_patch(state, line, line+len, patch);
 		*hdrsize = len + nextlen;
-		linenr += 2;
+		state->linenr += 2;
 		return offset;
 	}
 	return -1;
@@ -1636,7 +1636,7 @@ static void check_whitespace(struct apply_state *state,
 {
 	unsigned result = ws_check(line + 1, len - 1, ws_rule);
 
-	record_ws_error(state, result, line + 1, len - 2, linenr);
+	record_ws_error(state, result, line + 1, len - 2, state->linenr);
 }
 
 /*
@@ -1669,11 +1669,11 @@ static int parse_fragment(struct apply_state *state,
 	/* Parse the thing.. */
 	line += len;
 	size -= len;
-	linenr++;
+	state->linenr++;
 	added = deleted = 0;
 	for (offset = len;
 	     0 < size;
-	     offset += len, size -= len, line += len, linenr++) {
+	     offset += len, size -= len, line += len, state->linenr++) {
 		if (!oldlines && !newlines)
 			break;
 		len = linelen(line, size);
@@ -1772,10 +1772,10 @@ static int parse_single_patch(struct apply_state *state,
 		int len;
 
 		fragment = xcalloc(1, sizeof(*fragment));
-		fragment->linenr = linenr;
+		fragment->linenr = state->linenr;
 		len = parse_fragment(state, line, size, patch, fragment);
 		if (len <= 0)
-			die(_("corrupt patch at line %d"), linenr);
+			die(_("corrupt patch at line %d"), state->linenr);
 		fragment->patch = line;
 		fragment->size = len;
 		oldlines += fragment->oldlines;
@@ -1861,7 +1861,8 @@ static char *inflate_it(const void *data, unsigned long size,
  * points at an allocated memory that the caller must free, so
  * it is marked as "->free_patch = 1".
  */
-static struct fragment *parse_binary_hunk(char **buf_p,
+static struct fragment *parse_binary_hunk(struct apply_state *state,
+					  char **buf_p,
 					  unsigned long *sz_p,
 					  int *status_p,
 					  int *used_p)
@@ -1903,13 +1904,13 @@ static struct fragment *parse_binary_hunk(char **buf_p,
 	else
 		return NULL;
 
-	linenr++;
+	state->linenr++;
 	buffer += llen;
 	while (1) {
 		int byte_length, max_byte_length, newsize;
 		llen = linelen(buffer, size);
 		used += llen;
-		linenr++;
+		state->linenr++;
 		if (llen == 1) {
 			/* consume the blank line */
 			buffer++;
@@ -1963,7 +1964,7 @@ static struct fragment *parse_binary_hunk(char **buf_p,
 	free(data);
 	*status_p = -1;
 	error(_("corrupt binary patch at line %d: %.*s"),
-	      linenr-1, llen-1, buffer);
+	      state->linenr-1, llen-1, buffer);
 	return NULL;
 }
 
@@ -1972,7 +1973,10 @@ static struct fragment *parse_binary_hunk(char **buf_p,
  *   -1 in case of error,
  *   the length of the parsed binary patch otherwise
  */
-static int parse_binary(char *buffer, unsigned long size, struct patch *patch)
+static int parse_binary(struct apply_state *state,
+			char *buffer,
+			unsigned long size,
+			struct patch *patch)
 {
 	/*
 	 * We have read "GIT binary patch\n"; what follows is a line
@@ -1993,15 +1997,15 @@ static int parse_binary(char *buffer, unsigned long size, struct patch *patch)
 	int status;
 	int used, used_1;
 
-	forward = parse_binary_hunk(&buffer, &size, &status, &used);
+	forward = parse_binary_hunk(state, &buffer, &size, &status, &used);
 	if (!forward && !status)
 		/* there has to be one hunk (forward hunk) */
-		return error(_("unrecognized binary patch at line %d"), linenr-1);
+		return error(_("unrecognized binary patch at line %d"), state->linenr-1);
 	if (status)
 		/* otherwise we already gave an error message */
 		return status;
 
-	reverse = parse_binary_hunk(&buffer, &size, &status, &used_1);
+	reverse = parse_binary_hunk(state, &buffer, &size, &status, &used_1);
 	if (reverse)
 		used += used_1;
 	else if (status) {
@@ -2116,8 +2120,8 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
 		if (llen == sizeof(git_binary) - 1 &&
 		    !memcmp(git_binary, buffer + hd, llen)) {
 			int used;
-			linenr++;
-			used = parse_binary(buffer + hd + llen,
+			state->linenr++;
+			used = parse_binary(state, buffer + hd + llen,
 					    size - hd - llen, patch);
 			if (used < 0)
 				return -1;
@@ -2137,7 +2141,7 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
 				int len = strlen(binhdr[i]);
 				if (len < size - hd &&
 				    !memcmp(binhdr[i], buffer + hd, len)) {
-					linenr++;
+					state->linenr++;
 					patch->is_binary = 1;
 					patchsize = llen;
 					break;
@@ -2151,7 +2155,7 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
 		 */
 		if ((state->apply || state->check) &&
 		    (!patch->is_binary && !metadata_changes(patch)))
-			die(_("patch with only garbage at line %d"), linenr);
+			die(_("patch with only garbage at line %d"), state->linenr);
 	}
 
 	return offset + hdrsize + patchsize;
@@ -4748,6 +4752,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 	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(&state.root, 0);
 
 	git_apply_config();
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 43/83] builtin/apply: move 'fn_table' global into 'struct apply_state'
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (41 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 42/83] builtin/apply: move 'linenr' global " Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 44/83] builtin/apply: move 'symlink_changes' " Christian Couder
                   ` (36 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index f387340..b4c2bd8 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -93,6 +93,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;
@@ -291,13 +297,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;
@@ -3228,14 +3227,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(&fn_table, name);
+	item = string_list_lookup(&state->fn_table, name);
 	if (item != NULL)
 		return (struct patch *)item->util;
 
@@ -3267,7 +3266,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;
 
@@ -3277,7 +3276,7 @@ static void add_to_fn_table(struct patch *patch)
 	 * file creations and copies
 	 */
 	if (patch->new_name != NULL) {
-		item = string_list_insert(&fn_table, patch->new_name);
+		item = string_list_insert(&state->fn_table, patch->new_name);
 		item->util = patch;
 	}
 
@@ -3286,12 +3285,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(&fn_table, patch->old_name);
+		item = string_list_insert(&state->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
@@ -3299,7 +3298,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(&fn_table, patch->old_name);
+			item = string_list_insert(&state->fn_table, patch->old_name);
 			item->util = PATH_TO_BE_DELETED;
 		}
 		patch = patch->next;
@@ -3320,7 +3319,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;
 
@@ -3328,7 +3329,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;
 
@@ -3397,7 +3398,7 @@ static int load_preimage(struct apply_state *state,
 	struct patch *previous;
 	int status;
 
-	previous = previous_patch(patch, &status);
+	previous = previous_patch(state, patch, &status);
 	if (status)
 		return error(_("path %s has been renamed/deleted"),
 			     patch->old_name);
@@ -3593,7 +3594,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);
 
 	if (0 < patch->is_delete && patch->resultsize)
@@ -3627,7 +3628,7 @@ static int check_preimage(struct apply_state *state,
 		return 0;
 
 	assert(patch->is_new <= 0);
-	previous = previous_patch(patch, &status);
+	previous = previous_patch(state, patch, &status);
 
 	if (status)
 		return error(_("path %s has been renamed/deleted"), old_name);
@@ -3873,7 +3874,7 @@ static int check_patch(struct apply_state *state, struct patch *patch)
 	 * B and rename from A to B is handled the same way by asking
 	 * was_deleted().
 	 */
-	if ((tpatch = in_fn_table(new_name)) &&
+	if ((tpatch = in_fn_table(state, new_name)) &&
 	    (was_deleted(tpatch) || to_be_deleted(tpatch)))
 		ok_if_exists = 1;
 	else
@@ -3951,7 +3952,7 @@ static int check_patch_list(struct apply_state *state, struct patch *patch)
 	int err = 0;
 
 	prepare_symlink_changes(patch);
-	prepare_fn_table(patch);
+	prepare_fn_table(state, patch);
 	while (patch) {
 		if (state->apply_verbosely)
 			say_patch_name(stderr,
@@ -4595,7 +4596,7 @@ static int apply_patch(struct apply_state *state,
 
 	free_patch_list(list);
 	strbuf_release(&buf);
-	string_list_clear(&fn_table, 0);
+	string_list_clear(&state->fn_table, 0);
 	return 0;
 }
 
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 44/83] builtin/apply: move 'symlink_changes' global into 'struct apply_state'
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (42 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 43/83] builtin/apply: move 'fn_table' " Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 45/83] builtin/apply: move 'state' init into init_apply_state() Christian Couder
                   ` (35 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index b4c2bd8..8508607 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;
@@ -99,6 +113,8 @@ struct apply_state {
 	 */
 	struct string_list fn_table;
 
+	struct string_list symlink_changes;
+
 	int p_value;
 	int p_value_known;
 	unsigned int p_context;
@@ -3723,52 +3739,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(&symlink_changes, path);
+	ent = string_list_lookup(&state->symlink_changes, path);
 	if (!ent) {
-		ent = string_list_insert(&symlink_changes, path);
+		ent = string_list_insert(&state->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(&symlink_changes, path);
+	ent = string_list_lookup(&state->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);
 	}
 }
 
@@ -3782,7 +3788,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)
@@ -3951,7 +3957,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.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 45/83] builtin/apply: move 'state' init into init_apply_state()
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (43 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 44/83] builtin/apply: move 'symlink_changes' " Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-25  7:32   ` Eric Sunshine
  2016-04-24 13:33 ` [PATCH 46/83] builtin/apply: move 'state' check into check_apply_state() Christian Couder
                   ` (34 subsequent siblings)
  79 siblings, 1 reply; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 8508607..f2dffa9 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4670,6 +4670,28 @@ 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;
+	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(&state->root, 0);
+
+	git_apply_config();
+	if (apply_default_whitespace)
+		parse_whitespace_option(state, apply_default_whitespace);
+	if (apply_default_ignorewhitespace)
+		parse_ignorewhitespace_option(state, apply_default_ignorewhitespace);
+}
+
 int cmd_apply(int argc, const char **argv, const char *prefix_)
 {
 	int i;
@@ -4749,24 +4771,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 		OPT_END()
 	};
 
-	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_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(&state.root, 0);
-
-	git_apply_config();
-	if (apply_default_whitespace)
-		parse_whitespace_option(&state, apply_default_whitespace);
-	if (apply_default_ignorewhitespace)
-		parse_ignorewhitespace_option(&state, apply_default_ignorewhitespace);
+	init_apply_state(&state, prefix_);
 
 	argc = parse_options(argc, argv, state.prefix, builtin_apply_options,
 			apply_usage, 0);
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 46/83] builtin/apply: move 'state' check into check_apply_state()
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (44 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 45/83] builtin/apply: move 'state' init into init_apply_state() Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 47/83] builtin/apply: move applying patches into apply_all_patches() Christian Couder
                   ` (33 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index f2dffa9..94bf120 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4692,11 +4692,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;
@@ -4776,28 +4803,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(&state, force_apply);
 
 	for (i = 0; i < argc; i++) {
 		const char *arg = argv[i];
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 47/83] builtin/apply: move applying patches into apply_all_patches()
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (45 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 46/83] builtin/apply: move 'state' check into check_apply_state() Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-25 22:00   ` Stefan Beller
  2016-04-24 13:33 ` [PATCH 48/83] builtin/apply: rename 'prefix_' parameter to 'prefix' Christian Couder
                   ` (32 subsequent siblings)
  79 siblings, 1 reply; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 builtin/apply.c | 128 ++++++++++++++++++++++++++++++--------------------------
 1 file changed, 69 insertions(+), 59 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 94bf120..787426f 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4720,13 +4720,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, "<stdin>", 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, "<stdin>", 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(&the_index, &lock_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[] = {
@@ -4805,61 +4871,5 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 
 	check_apply_state(&state, force_apply);
 
-	for (i = 0; i < argc; i++) {
-		const char *arg = argv[i];
-		int fd;
-
-		if (!strcmp(arg, "-")) {
-			errs |= apply_patch(&state, 0, "<stdin>", 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, "<stdin>", 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(&the_index, &lock_file, COMMIT_LOCK))
-			die(_("Unable to write new index file"));
-	}
-
-	return !!errs;
+	return apply_all_patches(&state, argc, argv, options);
 }
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 48/83] builtin/apply: rename 'prefix_' parameter to 'prefix'
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (46 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 47/83] builtin/apply: move applying patches into apply_all_patches() Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 49/83] builtin/apply: move 'lock_file' global into 'struct apply_state' Christian Couder
                   ` (31 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 787426f..6c0b153 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4670,10 +4670,10 @@ 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)
 {
 	memset(state, 0, sizeof(*state));
-	state->prefix = prefix_;
+	state->prefix = prefix;
 	state->prefix_length = state->prefix ? strlen(state->prefix) : 0;
 	state->apply = 1;
 	state->line_termination = '\n';
@@ -4789,7 +4789,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;
@@ -4864,7 +4864,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 		OPT_END()
 	};
 
-	init_apply_state(&state, prefix_);
+	init_apply_state(&state, prefix);
 
 	argc = parse_options(argc, argv, state.prefix, builtin_apply_options,
 			apply_usage, 0);
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 49/83] builtin/apply: move 'lock_file' global into 'struct apply_state'
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (47 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 48/83] builtin/apply: rename 'prefix_' parameter to 'prefix' Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-25  7:50   ` Eric Sunshine
  2016-04-24 13:33 ` [PATCH 50/83] builtin/apply: get rid of the 'newfd' global Christian Couder
                   ` (30 subsequent siblings)
  79 siblings, 1 reply; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, 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'
and let's alloc the struct when needed.

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 6c0b153..d26419a 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;
@@ -4515,8 +4521,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)
 
@@ -4568,8 +4572,10 @@ 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(&lock_file, 1);
+	if (state->update_index && newfd < 0) {
+		state->lock_file = xcalloc(1, sizeof(struct lock_file));
+		newfd = hold_locked_index(state->lock_file, 1);
+	}
 
 	if (state->check_index) {
 		if (read_cache() < 0)
@@ -4782,7 +4788,7 @@ static int apply_all_patches(struct apply_state *state,
 	}
 
 	if (state->update_index) {
-		if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
+		if (write_locked_index(&the_index, state->lock_file, COMMIT_LOCK))
 			die(_("Unable to write new index file"));
 	}
 
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 50/83] builtin/apply: get rid of the 'newfd' global
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (48 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 49/83] builtin/apply: move 'lock_file' global into 'struct apply_state' Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 51/83] builtin/apply: make apply_patch() return -1 instead of die()ing Christian Couder
                   ` (29 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

The 'newfd' global is useless now as we can just test
for "lock_file == NULL" instead of "newfd < 0".

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

diff --git a/builtin/apply.c b/builtin/apply.c
index d26419a..84ff2da 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -141,8 +141,6 @@ struct apply_state {
 	enum ws_ignore ws_ignore_action;
 };
 
-static int newfd = -1;
-
 static const char * const apply_usage[] = {
 	N_("git apply [<options>] [<patch>...]"),
 	NULL
@@ -4572,9 +4570,9 @@ 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) {
+	if (state->update_index && state->lock_file == NULL) {
 		state->lock_file = xcalloc(1, sizeof(struct lock_file));
-		newfd = hold_locked_index(state->lock_file, 1);
+		hold_locked_index(state->lock_file, 1);
 	}
 
 	if (state->check_index) {
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 51/83] builtin/apply: make apply_patch() return -1 instead of die()ing
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (49 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 50/83] builtin/apply: get rid of the 'newfd' global Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-26  1:20   ` Eric Sunshine
  2016-04-24 13:33 ` [PATCH 52/83] builtin/apply: read_patch_file() " Christian Couder
                   ` (28 subsequent siblings)
  79 siblings, 1 reply; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, 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.

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 84ff2da..bc209f5 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4522,6 +4522,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,
@@ -4564,7 +4572,7 @@ static int apply_patch(struct apply_state *state,
 	}
 
 	if (!list && !skipped_patch)
-		die(_("unrecognized input"));
+		return error(_("unrecognized input"));
 
 	if (state->whitespace_error && (state->ws_error_action == die_on_ws_error))
 		state->apply = 0;
@@ -4575,19 +4583,17 @@ static int apply_patch(struct apply_state *state,
 		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)
+		return error(_("unable to read index file"));
 
 	if ((state->check || state->apply) &&
 	    check_patch_list(state, list) < 0 &&
 	    !state->apply_with_reject)
-		exit(1);
+		return -1;
 
 	if (state->apply && write_out_results(state, list)) {
 		if (state->apply_with_reject)
-			exit(1);
+			return -1;
 		/* with --3way, we still need to write the index out */
 		return 1;
 	}
@@ -4730,6 +4736,7 @@ static int apply_all_patches(struct apply_state *state,
 			     int options)
 {
 	int i;
+	int res;
 	int errs = 0;
 	int read_stdin = 1;
 
@@ -4738,7 +4745,10 @@ static int apply_all_patches(struct apply_state *state,
 		int fd;
 
 		if (!strcmp(arg, "-")) {
-			errs |= apply_patch(state, 0, "<stdin>", options);
+			res = apply_patch(state, 0, "<stdin>", options);
+			if (res < 0)
+				exit(1);
+			errs |= res;
 			read_stdin = 0;
 			continue;
 		} else if (0 < state->prefix_length)
@@ -4751,12 +4761,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, "<stdin>", options);
+	if (read_stdin) {
+		res = apply_patch(state, 0, "<stdin>", options);
+		if (res < 0)
+			exit(1);
+		errs |= res;
+	}
 
 	if (state->whitespace_error) {
 		if (state->squelch_whitespace_errors &&
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 52/83] builtin/apply: read_patch_file() return -1 instead of die()ing
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (50 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 51/83] builtin/apply: make apply_patch() return -1 instead of die()ing Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 53/83] builtin/apply: make find_header() " Christian Couder
                   ` (27 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index bc209f5..c0bb24c 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -454,10 +454,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
@@ -466,6 +466,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)
@@ -4541,7 +4542,8 @@ static int apply_patch(struct apply_state *state,
 	int skipped_patch = 0;
 
 	state->patch_input_file = filename;
-	read_patch_file(&buf, fd);
+	if (read_patch_file(&buf, fd))
+		return -1;
 	offset = 0;
 	while (offset < buf.len) {
 		struct patch *patch;
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 53/83] builtin/apply: make find_header() return -1 instead of die()ing
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (51 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 52/83] builtin/apply: read_patch_file() " Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-25 13:18   ` Duy Nguyen
  2016-04-27 18:08   ` Eric Sunshine
  2016-04-24 13:33 ` [PATCH 54/83] builtin/apply: make parse_chunk() return a negative integer on error Christian Couder
                   ` (26 subsequent siblings)
  79 siblings, 2 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

To be compatible with the rest of the error handling in builtin/apply.c,
find_header() should return -1 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 <chriscool@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 c0bb24c..825ffe9 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -1538,6 +1538,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,
@@ -1571,8 +1579,8 @@ static int find_header(struct apply_state *state,
 			struct fragment dummy;
 			if (parse_fragment_header(line, len, &dummy) < 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)
@@ -1588,18 +1596,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;
@@ -1624,7 +1632,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,
@@ -2115,6 +2123,9 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
 	int hdrsize, patchsize;
 	int offset = find_header(state, buffer, size, &hdrsize, 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)" >expected &&
+	echo "error: git diff header lacks filename information (line 4)" >expected &&
 	test_path_is_file f &&
 	test_cmp expected actual
 '
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 54/83] builtin/apply: make parse_chunk() return a negative integer on error
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (52 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 53/83] builtin/apply: make find_header() " Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-05-01 19:04   ` Eric Sunshine
  2016-04-24 13:33 ` [PATCH 55/83] builtin/apply: make parse_single_patch() return -1 " Christian Couder
                   ` (25 subsequent siblings)
  79 siblings, 1 reply; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

This negative number can be -2 if no patch header has been found,
otherwise it is -1.

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

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 825ffe9..e1d6c8b 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -2110,22 +2110,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, &hdrsize, patch);
 
-	if (offset == -1)
-		exit(1);
-
 	if (offset < 0)
 		return offset;
 
@@ -2185,8 +2185,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;
@@ -4566,6 +4567,8 @@ 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)
+				return -1;
 			break;
 		}
 		if (state->apply_in_reverse)
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 55/83] builtin/apply: make parse_single_patch() return -1 on error
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (53 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 54/83] builtin/apply: make parse_chunk() return a negative integer on error Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-05-01 19:32   ` Eric Sunshine
  2016-04-24 13:33 ` [PATCH 56/83] apply: move 'struct apply_state' to apply.h Christian Couder
                   ` (24 subsequent siblings)
  79 siblings, 1 reply; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

Signed-off-by: Christian Couder <chriscool@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 e1d6c8b..de5c745 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -1785,6 +1785,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,
@@ -1802,8 +1806,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;
@@ -1839,9 +1845,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: "
@@ -2143,6 +2149,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/" <output >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.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 56/83] apply: move 'struct apply_state' to apply.h
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (54 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 55/83] builtin/apply: make parse_single_patch() return -1 " Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 57/83] builtin/apply: libify parse_whitespace_option() Christian Couder
                   ` (23 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 apply.h         | 124 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 builtin/apply.c | 121 +-----------------------------------------------------
 2 files changed, 125 insertions(+), 120 deletions(-)
 create mode 100644 apply.h

diff --git a/apply.h b/apply.h
new file mode 100644
index 0000000..98aafc6
--- /dev/null
+++ b/apply.h
@@ -0,0 +1,124 @@
+#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 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;
+
+	const char *fake_ancestor;
+
+	int summary;
+
+	int threeway;
+
+	int no_add;
+
+	/*
+	 *  --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_xxxx"
+	 * 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;
+
+	struct string_list symlink_changes;
+
+	int p_value;
+	int p_value_known;
+	unsigned int p_context;
+
+	const char *patch_input_file;
+
+	struct string_list limit_by_name;
+	int has_include;
+
+	struct strbuf root;
+
+	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 de5c745..1c1ac7d 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -20,126 +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 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;
-	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;
-
-	const char *fake_ancestor;
-
-	int summary;
-
-	int threeway;
-
-	int no_add;
-
-	/*
-	 *  --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_xxxx"
-	 * 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;
-
-	struct string_list symlink_changes;
-
-	int p_value;
-	int p_value_known;
-	unsigned int p_context;
-
-	const char *patch_input_file;
-
-	struct string_list limit_by_name;
-	int has_include;
-
-	struct strbuf root;
-
-	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;
-};
+#include "apply.h"
 
 static const char * const apply_usage[] = {
 	N_("git apply [<options>] [<patch>...]"),
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 57/83] builtin/apply: libify parse_whitespace_option()
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (55 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 56/83] apply: move 'struct apply_state' to apply.h Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 58/83] builtin/apply: libify parse_ignorewhitespace_option() Christian Couder
                   ` (22 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 1c1ac7d..8d96f70 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,
@@ -4572,7 +4572,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;
 }
 
@@ -4602,8 +4603,8 @@ static void init_apply_state(struct apply_state *state, const char *prefix)
 	strbuf_init(&state->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.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 58/83] builtin/apply: libify parse_ignorewhitespace_option()
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (56 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 57/83] builtin/apply: libify parse_whitespace_option() Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-04-24 13:33 ` [PATCH 59/83] builtin/apply: move init_apply_state() to apply.c Christian Couder
                   ` (21 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 8d96f70..2f89922 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)
@@ -4605,8 +4605,8 @@ static void init_apply_state(struct apply_state *state, const char *prefix)
 	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.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 59/83] builtin/apply: move init_apply_state() to apply.c
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (57 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 58/83] builtin/apply: libify parse_ignorewhitespace_option() Christian Couder
@ 2016-04-24 13:33 ` Christian Couder
  2016-05-01 19:37   ` Eric Sunshine
  2016-04-24 13:34 ` [PATCH 60/83] apply: libify init_apply_state() Christian Couder
                   ` (20 subsequent siblings)
  79 siblings, 1 reply; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:33 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 Makefile        |  1 +
 apply.c         | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 apply.h         |  8 ++++++
 builtin/apply.c | 75 -----------------------------------------------------
 4 files changed, 89 insertions(+), 75 deletions(-)
 create mode 100644 apply.c

diff --git a/Makefile b/Makefile
index a83e322..a8616fc 100644
--- a/Makefile
+++ b/Makefile
@@ -684,6 +684,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 0000000..9c5f258
--- /dev/null
+++ b/apply.c
@@ -0,0 +1,80 @@
+#include "cache.h"
+#include "apply.h"
+
+
+
+static void git_apply_config(void)
+{
+	git_config_get_string_const("apply.whitespace", &apply_default_whitespace);
+	git_config_get_string_const("apply.ignorewhitespace", &apply_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)
+{
+	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_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(&state->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 98aafc6..70ab658 100644
--- a/apply.h
+++ b/apply.h
@@ -121,4 +121,12 @@ 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);
+
+
 #endif
diff --git a/builtin/apply.c b/builtin/apply.c
index 2f89922..1d958fa 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -27,52 +27,6 @@ static const char * const apply_usage[] = {
 	NULL
 };
 
-static 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);
-}
-
-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 0;
-	}
-	if (!strcmp(option, "change")) {
-		state->ws_ignore_action = ignore_ws_change;
-		return 0;
-	}
-	return error(_("unrecognized whitespace ignore option '%s'"), option);
-}
-
 static void set_default_whitespace_mode(struct apply_state *state)
 {
 	if (!state->whitespace_option && !apply_default_whitespace)
@@ -4522,13 +4476,6 @@ static int apply_patch(struct apply_state *state,
 	return 0;
 }
 
-static void git_apply_config(void)
-{
-	git_config_get_string_const("apply.whitespace", &apply_default_whitespace);
-	git_config_get_string_const("apply.ignorewhitespace", &apply_default_ignorewhitespace);
-	git_config(git_default_config, NULL);
-}
-
 static int option_parse_exclude(const struct option *opt,
 				const char *arg, int unset)
 {
@@ -4587,28 +4534,6 @@ 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;
-	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(&state->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);
-}
-
 static void check_apply_state(struct apply_state *state, int force_apply)
 {
 	int is_not_gitdir = !startup_info->have_repository;
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 60/83] apply: libify init_apply_state()
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (58 preceding siblings ...)
  2016-04-24 13:33 ` [PATCH 59/83] builtin/apply: move init_apply_state() to apply.c Christian Couder
@ 2016-04-24 13:34 ` Christian Couder
  2016-04-24 13:34 ` [PATCH 61/83] builtin/apply: libify check_apply_state() Christian Couder
                   ` (19 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:34 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/apply.c b/apply.c
index 9c5f258..11bec48 100644
--- a/apply.c
+++ b/apply.c
@@ -56,7 +56,7 @@ 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)
+int init_apply_state(struct apply_state *state, const char *prefix)
 {
 	memset(state, 0, sizeof(*state));
 	state->prefix = prefix;
@@ -73,8 +73,9 @@ void init_apply_state(struct apply_state *state, const char *prefix)
 
 	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 70ab658..021e9e3 100644
--- a/apply.h
+++ b/apply.h
@@ -126,7 +126,7 @@ 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);
+extern int init_apply_state(struct apply_state *state, const char *prefix);
 
 
 #endif
diff --git a/builtin/apply.c b/builtin/apply.c
index 1d958fa..e3ee199 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4717,7 +4717,8 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
 		OPT_END()
 	};
 
-	init_apply_state(&state, prefix);
+	if (init_apply_state(&state, prefix))
+		exit(1);
 
 	argc = parse_options(argc, argv, state.prefix, builtin_apply_options,
 			apply_usage, 0);
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 61/83] builtin/apply: libify check_apply_state()
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (59 preceding siblings ...)
  2016-04-24 13:34 ` [PATCH 60/83] apply: libify init_apply_state() Christian Couder
@ 2016-04-24 13:34 ` Christian Couder
  2016-04-25 13:26   ` Duy Nguyen
  2016-04-24 13:34 ` [PATCH 62/83] builtin/apply: move check_apply_state() to apply.c Christian Couder
                   ` (18 subsequent siblings)
  79 siblings, 1 reply; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:34 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index e3ee199..7576ec5 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4534,17 +4534,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)
@@ -4552,14 +4552,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,
@@ -4723,7 +4724,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(&state, force_apply);
+	if (check_apply_state(&state, force_apply))
+		exit(1);
 
 	return apply_all_patches(&state, argc, argv, options);
 }
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 62/83] builtin/apply: move check_apply_state() to apply.c
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (60 preceding siblings ...)
  2016-04-24 13:34 ` [PATCH 61/83] builtin/apply: libify check_apply_state() Christian Couder
@ 2016-04-24 13:34 ` Christian Couder
  2016-04-24 13:34 ` [PATCH 63/83] builtin/apply: make apply_all_patches() return -1 on error Christian Couder
                   ` (17 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:34 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/apply.c b/apply.c
index 11bec48..b29cc64 100644
--- a/apply.c
+++ b/apply.c
@@ -79,3 +79,32 @@ int init_apply_state(struct apply_state *state, const char *prefix)
 	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 021e9e3..35d4d15 100644
--- a/apply.h
+++ b/apply.h
@@ -127,6 +127,6 @@ extern int parse_ignorewhitespace_option(struct apply_state *state,
 					 const char *option);
 
 extern int init_apply_state(struct apply_state *state, const char *prefix);
-
+extern int check_apply_state(struct apply_state *state, int force_apply);
 
 #endif
diff --git a/builtin/apply.c b/builtin/apply.c
index 7576ec5..eab5ae1 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4534,35 +4534,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.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 63/83] builtin/apply: make apply_all_patches() return -1 on error
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (61 preceding siblings ...)
  2016-04-24 13:34 ` [PATCH 62/83] builtin/apply: move check_apply_state() to apply.c Christian Couder
@ 2016-04-24 13:34 ` Christian Couder
  2016-04-25 13:30   ` Duy Nguyen
  2016-05-01 21:03   ` Eric Sunshine
  2016-04-24 13:34 ` [PATCH 64/83] builtin/apply: make parse_traditional_patch() " Christian Couder
                   ` (16 subsequent siblings)
  79 siblings, 2 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:34 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index eab5ae1..73247c7 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4551,7 +4551,7 @@ static int apply_all_patches(struct apply_state *state,
 		if (!strcmp(arg, "-")) {
 			res = apply_patch(state, 0, "<stdin>", options);
 			if (res < 0)
-				exit(1);
+				return -1;
 			errs |= res;
 			read_stdin = 0;
 			continue;
@@ -4562,12 +4562,12 @@ 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);
 		if (res < 0)
-			exit(1);
+			return -1;
 		errs |= res;
 		close(fd);
 	}
@@ -4575,7 +4575,7 @@ static int apply_all_patches(struct apply_state *state,
 	if (read_stdin) {
 		res = apply_patch(state, 0, "<stdin>", options);
 		if (res < 0)
-			exit(1);
+			return -1;
 		errs |= res;
 	}
 
@@ -4590,10 +4590,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.",
@@ -4608,7 +4608,7 @@ static int apply_all_patches(struct apply_state *state,
 
 	if (state->update_index) {
 		if (write_locked_index(&the_index, state->lock_file, COMMIT_LOCK))
-			die(_("Unable to write new index file"));
+			return error(_("Unable to write new index file"));
 	}
 
 	return !!errs;
@@ -4698,5 +4698,8 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
 	if (check_apply_state(&state, force_apply))
 		exit(1);
 
-	return apply_all_patches(&state, argc, argv, options);
+	if (apply_all_patches(&state, argc, argv, options))
+		exit(1);
+
+	return 0;
 }
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 64/83] builtin/apply: make parse_traditional_patch() return -1 on error
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (62 preceding siblings ...)
  2016-04-24 13:34 ` [PATCH 63/83] builtin/apply: make apply_all_patches() return -1 on error Christian Couder
@ 2016-04-24 13:34 ` Christian Couder
  2016-04-24 13:34 ` [PATCH 65/83] builtin/apply: make gitdiff_verify_name() " Christian Couder
                   ` (15 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:34 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 73247c7..6b8ba2a 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.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 65/83] builtin/apply: make gitdiff_verify_name() return -1 on error
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (63 preceding siblings ...)
  2016-04-24 13:34 ` [PATCH 64/83] builtin/apply: make parse_traditional_patch() " Christian Couder
@ 2016-04-24 13:34 ` Christian Couder
  2016-04-25 13:36   ` Duy Nguyen
  2016-04-24 13:34 ` [PATCH 66/83] builtin/apply: change die_on_unsafe_path() to check_unsafe_path() Christian Couder
                   ` (14 subsequent siblings)
  79 siblings, 1 reply; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:34 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 6b8ba2a..268356b 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -812,7 +812,7 @@ static int gitdiff_hdrend(struct apply_state *state,
 			  const char *line,
 			  struct patch *patch)
 {
-	return -1;
+	return 1;
 }
 
 /*
@@ -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, &patch->old_name,
-			    DIFF_OLD_NAME);
-	return 0;
+	return gitdiff_verify_name(state, line,
+				   patch->is_new, &patch->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, &patch->new_name,
-			    DIFF_NEW_NAME);
-	return 0;
+	return gitdiff_verify_name(state, line,
+				   patch->is_delete, &patch->new_name,
+				   DIFF_NEW_NAME);
 }
 
 static int gitdiff_oldmode(struct apply_state *state,
@@ -1016,7 +1018,7 @@ static int gitdiff_unrecognized(struct apply_state *state,
 				const char *line,
 				struct patch *patch)
 {
-	return -1;
+	return 1;
 }
 
 /*
@@ -1248,9 +1250,13 @@ static int parse_git_header(struct apply_state *state,
 		for (i = 0; i < ARRAY_SIZE(optable); i++) {
 			const struct opentry *p = optable + i;
 			int oplen = strlen(p->str);
+			int res;
 			if (len < oplen || memcmp(p->str, line, oplen))
 				continue;
-			if (p->fn(state, line + oplen, patch) < 0)
+			res = p->fn(state, line + oplen, patch);
+			if (res < 0)
+				return -1;
+			if (res > 0)
 				return offset;
 			break;
 		}
@@ -1429,6 +1435,8 @@ static int find_header(struct apply_state *state,
 		 */
 		if (!memcmp("diff --git ", line, 11)) {
 			int git_hdr_len = parse_git_header(state, line, len, size, patch);
+			if (git_hdr_len < 0)
+				return -1;
 			if (git_hdr_len <= len)
 				continue;
 			if (!patch->old_name && !patch->new_name) {
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 66/83] builtin/apply: change die_on_unsafe_path() to check_unsafe_path()
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (64 preceding siblings ...)
  2016-04-24 13:34 ` [PATCH 65/83] builtin/apply: make gitdiff_verify_name() " Christian Couder
@ 2016-04-24 13:34 ` Christian Couder
  2016-04-24 13:34 ` [PATCH 67/83] builtin/apply: make build_fake_ancestor() return -1 on error Christian Couder
                   ` (13 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:34 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 268356b..8b7a8e0 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -3699,7 +3699,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;
@@ -3711,9 +3711,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;
 }
 
 /*
@@ -3803,8 +3804,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.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 67/83] builtin/apply: make build_fake_ancestor() return -1 on error
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (65 preceding siblings ...)
  2016-04-24 13:34 ` [PATCH 66/83] builtin/apply: change die_on_unsafe_path() to check_unsafe_path() Christian Couder
@ 2016-04-24 13:34 ` Christian Couder
  2016-05-02  7:32   ` Eric Sunshine
  2016-04-24 13:34 ` [PATCH 68/83] builtin/apply: make remove_file() " Christian Couder
                   ` (12 subsequent siblings)
  79 siblings, 1 reply; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:34 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 8b7a8e0..69bb94c 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -3891,7 +3891,7 @@ 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 };
@@ -3913,31 +3913,34 @@ 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);
+			return error(_("make_cache_entry failed for path '%s'"),
+				     name);
 		if (add_index_entry(&result, ce, ADD_CACHE_OK_TO_ADD))
-			die ("Could not add %s to temporary index", name);
+			return error("Could not add %s to temporary index",
+				     name);
 	}
 
 	hold_lock_file_for_update(&lock, filename, LOCK_DIE_ON_ERROR);
 	if (write_locked_index(&result, &lock, COMMIT_LOCK))
-		die ("Could not write temporary index to %s", filename);
+		return error("Could not write temporary index to %s", filename);
 
 	discard_index(&result);
+	return 0;
 }
 
 static void stat_patch_list(struct apply_state *state, struct patch *patch)
@@ -4470,8 +4473,9 @@ static int apply_patch(struct apply_state *state,
 		return 1;
 	}
 
-	if (state->fake_ancestor)
-		build_fake_ancestor(list, state->fake_ancestor);
+	if (state->fake_ancestor &&
+	    build_fake_ancestor(list, state->fake_ancestor))
+		return -1;
 
 	if (state->diffstat)
 		stat_patch_list(state, list);
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 68/83] builtin/apply: make remove_file() return -1 on error
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (66 preceding siblings ...)
  2016-04-24 13:34 ` [PATCH 67/83] builtin/apply: make build_fake_ancestor() return -1 on error Christian Couder
@ 2016-04-24 13:34 ` Christian Couder
  2016-04-24 13:34 ` [PATCH 69/83] builtin/apply: make add_conflicted_stages_file() " Christian Couder
                   ` (11 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:34 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 69bb94c..89118c1 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4071,17 +4071,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,
@@ -4260,8 +4261,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) {
@@ -4273,8 +4276,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.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 69/83] builtin/apply: make add_conflicted_stages_file() return -1 on error
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (67 preceding siblings ...)
  2016-04-24 13:34 ` [PATCH 68/83] builtin/apply: make remove_file() " Christian Couder
@ 2016-04-24 13:34 ` Christian Couder
  2016-05-02  7:36   ` Eric Sunshine
  2016-04-24 13:34 ` [PATCH 70/83] builtin/apply: make add_index_file() " Christian Couder
                   ` (10 subsequent siblings)
  79 siblings, 1 reply; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:34 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 89118c1..5a5be49 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4210,7 +4210,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;
@@ -4218,7 +4218,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);
@@ -4234,8 +4234,11 @@ static void add_conflicted_stages_file(struct apply_state *state,
 		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);
+			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)
@@ -4249,9 +4252,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.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 70/83] builtin/apply: make add_index_file() return -1 on error
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (68 preceding siblings ...)
  2016-04-24 13:34 ` [PATCH 69/83] builtin/apply: make add_conflicted_stages_file() " Christian Couder
@ 2016-04-24 13:34 ` Christian Couder
  2016-04-24 13:34 ` [PATCH 71/83] builtin/apply: make create_file() " Christian Couder
                   ` (9 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:34 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 5a5be49..e021fad 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4085,11 +4085,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;
@@ -4097,7 +4097,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);
@@ -4108,20 +4108,32 @@ static void add_index_file(struct apply_state *state,
 		const char *s;
 
 		if (!skip_prefix(buf, "Subproject commit ", &s) ||
-		    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, &st) < 0)
-				die_errno(_("unable to stat newly created file '%s'"),
-					  path);
+			if (lstat(path, &st) < 0) {
+				free(ce);
+				return error(_("unable to stat newly "
+					       "created file '%s': %s"),
+					     path, strerror(errno));
+			}
 			fill_stat_cache_info(ce, &st);
 		}
-		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) {
+		free(ce);
+		return error(_("unable to add cache entry for %s"), path);
 	}
-	if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)
-		die(_("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)
@@ -4255,8 +4267,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.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 71/83] builtin/apply: make create_file() return -1 on error
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (69 preceding siblings ...)
  2016-04-24 13:34 ` [PATCH 70/83] builtin/apply: make add_index_file() " Christian Couder
@ 2016-04-24 13:34 ` Christian Couder
  2016-04-24 13:34 ` [PATCH 72/83] builtin/apply: make write_out_one_result() " Christian Couder
                   ` (8 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:34 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index e021fad..ebac9ee 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4253,7 +4253,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;
@@ -4264,13 +4264,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 */
@@ -4286,8 +4283,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;
 	}
 	/*
@@ -4298,8 +4297,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.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 72/83] builtin/apply: make write_out_one_result() return -1 on error
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (70 preceding siblings ...)
  2016-04-24 13:34 ` [PATCH 71/83] builtin/apply: make create_file() " Christian Couder
@ 2016-04-24 13:34 ` Christian Couder
  2016-04-24 13:34 ` [PATCH 73/83] builtin/apply: make write_out_results() " Christian Couder
                   ` (7 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:34 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index ebac9ee..dae89e2 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4271,36 +4271,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)
@@ -4387,7 +4380,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.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 73/83] builtin/apply: make write_out_results() return -1 on error
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (71 preceding siblings ...)
  2016-04-24 13:34 ` [PATCH 72/83] builtin/apply: make write_out_one_result() " Christian Couder
@ 2016-04-24 13:34 ` Christian Couder
  2016-05-02 17:42   ` Eric Sunshine
  2016-04-24 13:34 ` [PATCH 74/83] builtin/apply: make try_create_file() " Christian Couder
                   ` (6 subsequent siblings)
  79 siblings, 1 reply; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:34 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index dae89e2..49ef4c9 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4367,6 +4367,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;
@@ -4381,7 +4387,7 @@ static int write_out_results(struct apply_state *state, struct patch *list)
 				errs = 1;
 			else {
 				if (write_out_one_result(state, l, phase))
-					exit(1);
+					return -1;
 				if (phase == 1) {
 					if (write_out_one_reject(state, l))
 						errs = 1;
@@ -4484,11 +4490,16 @@ static int apply_patch(struct apply_state *state,
 	    !state->apply_with_reject)
 		return -1;
 
-	if (state->apply && write_out_results(state, list)) {
-		if (state->apply_with_reject)
+	if (state->apply) {
+		int res = write_out_results(state, list);
+		if (res < 0)
 			return -1;
-		/* with --3way, we still need to write the index out */
-		return 1;
+		if (res > 0) {
+			if (state->apply_with_reject)
+				return -1;
+			/* with --3way, we still need to write the index out */
+			return 1;
+		}
 	}
 
 	if (state->fake_ancestor &&
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 74/83] builtin/apply: make try_create_file() return -1 on error
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (72 preceding siblings ...)
  2016-04-24 13:34 ` [PATCH 73/83] builtin/apply: make write_out_results() " Christian Couder
@ 2016-04-24 13:34 ` Christian Couder
  2016-05-02 18:01   ` Eric Sunshine
  2016-04-24 13:34 ` [PATCH 75/83] builtin/apply: make create_one_file() " Christian Couder
                   ` (5 subsequent siblings)
  79 siblings, 1 reply; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:34 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 49ef4c9..32c38f0 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4136,6 +4136,12 @@ 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;
@@ -4145,28 +4151,32 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,
 		struct stat st;
 		if (!lstat(path, &st) && 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, &nbuf)) {
 		size = nbuf.len;
 		buf  = nbuf.buf;
 	}
-	write_or_die(fd, buf, size);
+
+	if (!write_or_whine_pipe(fd, buf, size, path)) {
+		strbuf_release(&nbuf);
+		return -1;
+	}
 	strbuf_release(&nbuf);
 
 	if (close(fd) < 0)
-		die_errno(_("closing file '%s'"), path);
+		return error(_("closing file '%s': %s"), path, strerror(errno));
 	return 0;
 }
 
@@ -4181,16 +4191,25 @@ 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)
 		return;
+	if (res < 0)
+		exit(1);
 
 	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)
 			return;
+		if (res < 0)
+			exit(1);
 	}
 
 	if (errno == EEXIST || errno == EACCES) {
@@ -4208,12 +4227,15 @@ 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) {
 				if (!rename(newpath, path))
 					return;
 				unlink_or_warn(newpath);
 				break;
 			}
+			if (res < 0)
+				exit(1);
 			if (errno != EEXIST)
 				break;
 			++nr;
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 75/83] builtin/apply: make create_one_file() return -1 on error
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (73 preceding siblings ...)
  2016-04-24 13:34 ` [PATCH 74/83] builtin/apply: make try_create_file() " Christian Couder
@ 2016-04-24 13:34 ` Christian Couder
  2016-04-24 13:34 ` [PATCH 76/83] builtin/apply: rename option parsing functions Christian Couder
                   ` (4 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:34 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 32c38f0..5bd5154 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4184,32 +4184,36 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,
  * We optimistically assume that the directories exist,
  * which is true 99% of the time anyway. If they don't,
  * we create them and try again.
+ *
+ * Returns:
+ *   -1 on error
+ *   0 otherwise
  */
-static void create_one_file(struct apply_state *state,
-			    char *path,
-			    unsigned mode,
-			    const char *buf,
-			    unsigned long size)
+static int create_one_file(struct apply_state *state,
+			   char *path,
+			   unsigned mode,
+			   const char *buf,
+			   unsigned long size)
 {
 	int res;
 
 	if (state->cached)
-		return;
+		return 0;
 
 	res = try_create_file(path, mode, buf, size);
 	if (!res)
-		return;
+		return 0;
 	if (res < 0)
-		exit(1);
+		return -1;
 
 	if (errno == ENOENT) {
 		if (safe_create_leading_directories(path))
-			return;
+			return 0;
 		res = try_create_file(path, mode, buf, size);
 		if (!res)
-			return;
+			return 0;
 		if (res < 0)
-			exit(1);
+			return -1;
 	}
 
 	if (errno == EEXIST || errno == EACCES) {
@@ -4230,18 +4234,19 @@ static void create_one_file(struct apply_state *state,
 			res = try_create_file(newpath, mode, buf, size);
 			if (!res) {
 				if (!rename(newpath, path))
-					return;
+					return 0;
 				unlink_or_warn(newpath);
 				break;
 			}
 			if (res < 0)
-				exit(1);
+				return -1;
 			if (errno != EEXIST)
 				break;
 			++nr;
 		}
 	}
-	die_errno(_("unable to write file '%s' mode %o"), path, mode);
+	return error(_("unable to write file '%s' mode %o: %s"),
+		     path, mode, strerror(errno));
 }
 
 static int add_conflicted_stages_file(struct apply_state *state,
@@ -4284,7 +4289,8 @@ static int create_file(struct apply_state *state, struct patch *patch)
 
 	if (!mode)
 		mode = S_IFREG | 0644;
-	create_one_file(state, path, mode, buf, size);
+	if (create_one_file(state, path, mode, buf, size))
+		return -1;
 
 	if (patch->conflicted_threeway)
 		return add_conflicted_stages_file(state, patch);
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 76/83] builtin/apply: rename option parsing functions
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (74 preceding siblings ...)
  2016-04-24 13:34 ` [PATCH 75/83] builtin/apply: make create_one_file() " Christian Couder
@ 2016-04-24 13:34 ` Christian Couder
  2016-04-24 13:34 ` [PATCH 77/83] apply: rename and move opt constants to apply.h Christian Couder
                   ` (3 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:34 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, 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 <chriscool@tuxfamily.org>
---
 builtin/apply.c | 40 ++++++++++++++++++++--------------------
 1 file changed, 20 insertions(+), 20 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 5bd5154..5910c5c 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4549,16 +4549,16 @@ static int apply_patch(struct apply_state *state,
 	return 0;
 }
 
-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);
@@ -4566,9 +4566,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);
@@ -4576,8 +4576,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)
@@ -4587,8 +4587,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;
@@ -4597,8 +4597,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(&state->root);
@@ -4696,13 +4696,13 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
 	struct option builtin_apply_options[] = {
 		{ OPTION_CALLBACK, 0, "exclude", &state, N_("path"),
 			N_("don't apply changes matching the given path"),
-			0, option_parse_exclude },
+			0, apply_option_parse_exclude },
 		{ OPTION_CALLBACK, 0, "include", &state, N_("path"),
 			N_("apply changes matching the given path"),
-			0, option_parse_include },
+			0, apply_option_parse_include },
 		{ OPTION_CALLBACK, 'p', NULL, &state, N_("num"),
 			N_("remove <num> leading slashes from traditional diff paths"),
-			0, option_parse_p },
+			0, apply_option_parse_p },
 		OPT_BOOL(0, "no-add", &state.no_add,
 			N_("ignore additions made by the patch")),
 		OPT_BOOL(0, "stat", &state.diffstat,
@@ -4734,13 +4734,13 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
 				N_("ensure at least <n> lines of context match")),
 		{ OPTION_CALLBACK, 0, "whitespace", &state, 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", &state, NULL,
 			N_("ignore changes in whitespace when finding context"),
-			PARSE_OPT_NOARG, option_parse_space_change },
+			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, option_parse_space_change },
+			PARSE_OPT_NOARG, apply_option_parse_space_change },
 		OPT_BOOL('R', "reverse", &state.apply_in_reverse,
 			N_("apply the patch in reverse")),
 		OPT_BOOL(0, "unidiff-zero", &state.unidiff_zero,
@@ -4758,7 +4758,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
 			RECOUNT),
 		{ OPTION_CALLBACK, 0, "directory", &state, N_("root"),
 			N_("prepend <root> to all filenames"),
-			0, option_parse_directory },
+			0, apply_option_parse_directory },
 		OPT_END()
 	};
 
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* [PATCH 77/83] apply: rename and move opt constants to apply.h
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (75 preceding siblings ...)
  2016-04-24 13:34 ` [PATCH 76/83] builtin/apply: rename option parsing functions Christian Couder
@ 2016-04-24 13:34 ` Christian Couder
  2016-04-24 15:23 ` [PATCH 00/83] libify apply and use lib in am Ramsay Jones
                   ` (2 subsequent siblings)
  79 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 13:34 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

Signed-off-by: Christian Couder <chriscool@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 35d4d15..3df0128 100644
--- a/apply.h
+++ b/apply.h
@@ -129,4 +129,7 @@ extern int parse_ignorewhitespace_option(struct apply_state *state,
 extern int init_apply_state(struct apply_state *state, const char *prefix);
 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 5910c5c..1c53c7e 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4443,9 +4443,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.
  *
@@ -4473,8 +4470,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);
@@ -4752,10 +4749,10 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
 		OPT__VERBOSE(&state.apply_verbosely, N_("be verbose")),
 		OPT_BIT(0, "inaccurate-eof", &options,
 			N_("tolerate incorrectly detected missing new-line at the end of file"),
-			INACCURATE_EOF),
+			APPLY_OPT_INACCURATE_EOF),
 		OPT_BIT(0, "recount", &options,
 			N_("do not trust the line counts in the hunk headers"),
-			RECOUNT),
+			APPLY_OPT_RECOUNT),
 		{ OPTION_CALLBACK, 0, "directory", &state, N_("root"),
 			N_("prepend <root> to all filenames"),
 			0, apply_option_parse_directory },
-- 
2.8.1.300.g5fed0c0

^ permalink raw reply related	[flat|nested] 156+ messages in thread

* Re: [PATCH 00/83] libify apply and use lib in am
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (76 preceding siblings ...)
  2016-04-24 13:34 ` [PATCH 77/83] apply: rename and move opt constants to apply.h Christian Couder
@ 2016-04-24 15:23 ` Ramsay Jones
  2016-04-24 16:27   ` Christian Couder
  2016-04-25  9:02 ` Duy Nguyen
       [not found] ` <1461504863-15946-79-git-send-email-chriscool@tuxfamily.org>
  79 siblings, 1 reply; 156+ messages in thread
From: Ramsay Jones @ 2016-04-24 15:23 UTC (permalink / raw)
  To: Christian Couder, git
  Cc: Junio C Hamano, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder



On 24/04/16 14:33, Christian Couder wrote:
> This is a patch series about libifying `git apply` functionality, and
> using this libified functionality in `git am`, so that no 'git apply'
> process is spawn anymore. This makes `git am` significantly faster, so
> `git rebase`, when it uses the am backend, is also significantly
> faster.

I just tried to git-am these patches, but patch #78 did not make it
to the list.

(Also, patches #59 and #62 both issue a 'new blank line at EOF' warning).

ATB,
Ramsay Jones

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 00/83] libify apply and use lib in am
  2016-04-24 15:23 ` [PATCH 00/83] libify apply and use lib in am Ramsay Jones
@ 2016-04-24 16:27   ` Christian Couder
  2016-04-24 16:56     ` Christian Couder
  0 siblings, 1 reply; 156+ messages in thread
From: Christian Couder @ 2016-04-24 16:27 UTC (permalink / raw)
  To: Ramsay Jones
  Cc: git, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

On Sun, Apr 24, 2016 at 5:23 PM, Ramsay Jones
<ramsay@ramsayjones.plus.com> wrote:
>
>
> On 24/04/16 14:33, Christian Couder wrote:
>> This is a patch series about libifying `git apply` functionality, and
>> using this libified functionality in `git am`, so that no 'git apply'
>> process is spawn anymore. This makes `git am` significantly faster, so
>> `git rebase`, when it uses the am backend, is also significantly
>> faster.
>
> I just tried to git-am these patches, but patch #78 did not make it
> to the list.

That's strange because gmail tells me it has been sent, and it made it
to chriscool@tuxfamily.org.

I use send-email and smtp.gmail.com. Just after sending patch #78 the
connection to smtp.gmail.com was closed with:

4.7.0 Try again later, closing connection. (MAIL) j6sm6717101wjb.29 - gsmtp

Then I had to wait a few minutes before I could send the last patches.

> (Also, patches #59 and #62 both issue a 'new blank line at EOF' warning).

Ok, I will take a look at that.

Thanks,
Christian.

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 00/83] libify apply and use lib in am
  2016-04-24 16:27   ` Christian Couder
@ 2016-04-24 16:56     ` Christian Couder
  2016-04-24 17:42       ` Ramsay Jones
  2016-04-25 15:09       ` Johannes Schindelin
  0 siblings, 2 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-24 16:56 UTC (permalink / raw)
  To: Ramsay Jones
  Cc: git, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

On Sun, Apr 24, 2016 at 6:27 PM, Christian Couder
<christian.couder@gmail.com> wrote:
> On Sun, Apr 24, 2016 at 5:23 PM, Ramsay Jones
> <ramsay@ramsayjones.plus.com> wrote:
>>
>>
>> On 24/04/16 14:33, Christian Couder wrote:
>>> This is a patch series about libifying `git apply` functionality, and
>>> using this libified functionality in `git am`, so that no 'git apply'
>>> process is spawn anymore. This makes `git am` significantly faster, so
>>> `git rebase`, when it uses the am backend, is also significantly
>>> faster.
>>
>> I just tried to git-am these patches, but patch #78 did not make it
>> to the list.
>
> That's strange because gmail tells me it has been sent, and it made it
> to chriscool@tuxfamily.org.

Instead of waiting for the patch to appear on the list, you might want
to use branch libify-apply-use-in-am25 in my GitHub repo:

https://github.com/chriscool/git/commits/libify-apply-use-in-am25

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 00/83] libify apply and use lib in am
  2016-04-24 16:56     ` Christian Couder
@ 2016-04-24 17:42       ` Ramsay Jones
  2016-04-25  0:14         ` Duy Nguyen
  2016-04-25 15:09       ` Johannes Schindelin
  1 sibling, 1 reply; 156+ messages in thread
From: Ramsay Jones @ 2016-04-24 17:42 UTC (permalink / raw)
  To: Christian Couder
  Cc: git, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder



On 24/04/16 17:56, Christian Couder wrote:
> On Sun, Apr 24, 2016 at 6:27 PM, Christian Couder
> <christian.couder@gmail.com> wrote:
>> On Sun, Apr 24, 2016 at 5:23 PM, Ramsay Jones
>> <ramsay@ramsayjones.plus.com> wrote:
>>>
>>>
>>> On 24/04/16 14:33, Christian Couder wrote:
>>>> This is a patch series about libifying `git apply` functionality, and
>>>> using this libified functionality in `git am`, so that no 'git apply'
>>>> process is spawn anymore. This makes `git am` significantly faster, so
>>>> `git rebase`, when it uses the am backend, is also significantly
>>>> faster.
>>>
>>> I just tried to git-am these patches, but patch #78 did not make it
>>> to the list.
>>
>> That's strange because gmail tells me it has been sent, and it made it
>> to chriscool@tuxfamily.org.
> 
> Instead of waiting for the patch to appear on the list, you might want
> to use branch libify-apply-use-in-am25 in my GitHub repo:
> 
> https://github.com/chriscool/git/commits/libify-apply-use-in-am25

Hmm, that branch doesn't correspond directly to the patches you sent
out (there are 86 commits, some marked with draft. I think commit d13d2ac
corresponds kinda to patch #83, but .... ).

I think I'll wait to see the patches as you intend them to be seen. ;-)

ATB,
Ramsay Jones

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 00/83] libify apply and use lib in am
  2016-04-24 17:42       ` Ramsay Jones
@ 2016-04-25  0:14         ` Duy Nguyen
  2016-04-25  9:15           ` Christian Couder
  0 siblings, 1 reply; 156+ messages in thread
From: Duy Nguyen @ 2016-04-25  0:14 UTC (permalink / raw)
  To: Ramsay Jones
  Cc: Christian Couder, git, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Johannes Schindelin, Stefan Beller, Matthieu Moy,
	Christian Couder

On Mon, Apr 25, 2016 at 12:42 AM, Ramsay Jones
<ramsay@ramsayjones.plus.com> wrote:
>
>
> On 24/04/16 17:56, Christian Couder wrote:
>> On Sun, Apr 24, 2016 at 6:27 PM, Christian Couder
>> <christian.couder@gmail.com> wrote:
>>> On Sun, Apr 24, 2016 at 5:23 PM, Ramsay Jones
>>> <ramsay@ramsayjones.plus.com> wrote:
>>>>
>>>>
>>>> On 24/04/16 14:33, Christian Couder wrote:
>>>>> This is a patch series about libifying `git apply` functionality, and
>>>>> using this libified functionality in `git am`, so that no 'git apply'
>>>>> process is spawn anymore. This makes `git am` significantly faster, so
>>>>> `git rebase`, when it uses the am backend, is also significantly
>>>>> faster.
>>>>
>>>> I just tried to git-am these patches, but patch #78 did not make it
>>>> to the list.
>>>
>>> That's strange because gmail tells me it has been sent, and it made it
>>> to chriscool@tuxfamily.org.

#78 moves 4000k lines around and ends up ~260k in size, I think it may
have hit vger limit.

>> Instead of waiting for the patch to appear on the list, you might want
>> to use branch libify-apply-use-in-am25 in my GitHub repo:
>>
>> https://github.com/chriscool/git/commits/libify-apply-use-in-am25
>
> Hmm, that branch doesn't correspond directly to the patches you sent
> out (there are 86 commits, some marked with draft. I think commit d13d2ac
> corresponds kinda to patch #83, but .... ).
>
> I think I'll wait to see the patches as you intend them to be seen. ;-)

I git-am'd the series then compared with the rebased version of
libify-apply-use-in-am25 on master. 33198a1 (i.e.
libify-apply-use-in-am25^) matches what was sent in content (didn't
compare commit messages).
-- 
Duy

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 45/83] builtin/apply: move 'state' init into init_apply_state()
  2016-04-24 13:33 ` [PATCH 45/83] builtin/apply: move 'state' init into init_apply_state() Christian Couder
@ 2016-04-25  7:32   ` Eric Sunshine
  2016-04-27 12:00     ` Christian Couder
  0 siblings, 1 reply; 156+ messages in thread
From: Eric Sunshine @ 2016-04-25  7:32 UTC (permalink / raw)
  To: Christian Couder
  Cc: Git List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

On Sun, Apr 24, 2016 at 9:33 AM, Christian Couder
<christian.couder@gmail.com> wrote:
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---
> diff --git a/builtin/apply.c b/builtin/apply.c
> @@ -4670,6 +4670,28 @@ static int option_parse_directory(const struct option *opt,
> +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_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(&state->root, 0);
> +
> +       git_apply_config();
> +       if (apply_default_whitespace)
> +               parse_whitespace_option(state, apply_default_whitespace);
> +       if (apply_default_ignorewhitespace)
> +               parse_ignorewhitespace_option(state, apply_default_ignorewhitespace);
> +}

Minor:

If factoring out this code from cmd_apply() into init_apply_state()
was done as a preparatory patch before introduction of 'apply_state',
then each new 'state->foo=...' line would already be at its final
location when added by its respective patch.

Doing so would also provide an obvious opportunity to name the
'prefix' argument to init_apply_state() "prefix" rather than the odd
"prefix_".

>  int cmd_apply(int argc, const char **argv, const char *prefix_)
>  {
>         int i;
> @@ -4749,24 +4771,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
>                 OPT_END()
>         };
>
> -       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_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(&state.root, 0);
> -
> -       git_apply_config();
> -       if (apply_default_whitespace)
> -               parse_whitespace_option(&state, apply_default_whitespace);
> -       if (apply_default_ignorewhitespace)
> -               parse_ignorewhitespace_option(&state, apply_default_ignorewhitespace);
> +       init_apply_state(&state, prefix_);
>
>         argc = parse_options(argc, argv, state.prefix, builtin_apply_options,
>                         apply_usage, 0);
> --
> 2.8.1.300.g5fed0c0

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 49/83] builtin/apply: move 'lock_file' global into 'struct apply_state'
  2016-04-24 13:33 ` [PATCH 49/83] builtin/apply: move 'lock_file' global into 'struct apply_state' Christian Couder
@ 2016-04-25  7:50   ` Eric Sunshine
  2016-04-25 17:55     ` Junio C Hamano
  2016-04-30 19:39     ` Christian Couder
  0 siblings, 2 replies; 156+ messages in thread
From: Eric Sunshine @ 2016-04-25  7:50 UTC (permalink / raw)
  To: Christian Couder
  Cc: Git List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

On Sun, Apr 24, 2016 at 9:33 AM, Christian Couder
<christian.couder@gmail.com> wrote:
> We cannot have a 'struct lock_file' allocated on the stack, as lockfile.c
> keeps a linked list of all created lock_file structures.

By talking about "the stack" here, I suppose you mean that your
initial idea was to move the global lock_file into cmd_apply() or
something?

> So let's make the 'lock_file' variable a pointer to a 'struct lock_file'
> and let's alloc the struct when needed.
>
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---
> diff --git a/builtin/apply.c b/builtin/apply.c
> @@ -52,6 +52,12 @@ struct apply_state {
> +       /*
> +        * 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;

Is there ever a time when lock_file is removed from the list (such as
upon successful write of the index), in which case it would be safe to
free() this?

> @@ -4515,8 +4521,6 @@ static int write_out_results(struct apply_state *state, struct patch *list)
>         return errs;
>  }
>
> -static struct lock_file lock_file;

Does the static lock_file in build_fake_ancestor() deserve the same
sort of treatment? (I haven't traced the code enough to answer this.)

>  #define INACCURATE_EOF (1<<0)
>  #define RECOUNT                (1<<1)

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 00/83] libify apply and use lib in am
  2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
                   ` (77 preceding siblings ...)
  2016-04-24 15:23 ` [PATCH 00/83] libify apply and use lib in am Ramsay Jones
@ 2016-04-25  9:02 ` Duy Nguyen
  2016-04-25  9:57   ` Christian Couder
       [not found] ` <1461504863-15946-79-git-send-email-chriscool@tuxfamily.org>
  79 siblings, 1 reply; 156+ messages in thread
From: Duy Nguyen @ 2016-04-25  9:02 UTC (permalink / raw)
  To: Christian Couder
  Cc: Git Mailing List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Johannes Schindelin, Stefan Beller, Matthieu Moy,
	Christian Couder

On Sun, Apr 24, 2016 at 8:33 PM, Christian Couder
<christian.couder@gmail.com> wrote:
> Sorry if this patch series is a bit long. I can split it into two or
> more series if it is prefered.

I suspect you deliberately made the series long just to show off how
good new git-rebase is ;-)

> Performance numbers:
>
>   - A few days ago Æ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

I was a bit puzzled why split-index helped so much. It shouldn't have
in my opinion unless you paired it with index-helper, to cut down both
read and write time. So I ran some tests. Long story short, I think we
can achieve this gain (and a little more) even without split-index.

I ran my measurement patch [1] with and without your series used this
series as rebase material. Without the series, the picture is not so
surprising. We run git-apply 80+ times, each consists of this sequence

read index
write index (cache tree updates only)
read index again
optionally initialize name hash (when new entries are added, I guess)
read packed-refs
write index

With this series, we run a single git-apply which does

read index (and sharedindex too if in split-index mode)
initialize name hash
write index 80+ times

This explains why I guessed it wrong: when you write only, not read
back, of course index-helper is not required. And with split-index you
only write as many entries as you touch (which is usually a small
number compared to worktree's size), so writing 80+ times with
split-index is a lot faster than write 80+ times with whole index.

But why write so many times when nobody reads it? We only need to
write before git-apply exits, either after successfully applying the
whole series, or after it stops at conflicts, and maybe even at die()
and SIGINT. Yes if git-apply segfaults, then the index update is lost,
but in such a case, it's usually a good idea to restart fresh anyway.
When you only write index once (or twice) it won't matter if
split-index is used.

[1] http://article.gmane.org/gmane.comp.version-control.git/292001
-- 
Duy

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 00/83] libify apply and use lib in am
  2016-04-25  0:14         ` Duy Nguyen
@ 2016-04-25  9:15           ` Christian Couder
  0 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-25  9:15 UTC (permalink / raw)
  To: Duy Nguyen
  Cc: Ramsay Jones, git, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Johannes Schindelin, Stefan Beller, Matthieu Moy,
	Christian Couder

On Mon, Apr 25, 2016 at 2:14 AM, Duy Nguyen <pclouds@gmail.com> wrote:
> On Mon, Apr 25, 2016 at 12:42 AM, Ramsay Jones
> <ramsay@ramsayjones.plus.com> wrote:
>>
>>
>> On 24/04/16 17:56, Christian Couder wrote:
>>> On Sun, Apr 24, 2016 at 6:27 PM, Christian Couder
>>> <christian.couder@gmail.com> wrote:
>>>> On Sun, Apr 24, 2016 at 5:23 PM, Ramsay Jones
>>>> <ramsay@ramsayjones.plus.com> wrote:
>>>>>
>>>>> I just tried to git-am these patches, but patch #78 did not make it
>>>>> to the list.
>>>>
>>>> That's strange because gmail tells me it has been sent, and it made it
>>>> to chriscool@tuxfamily.org.
>
> #78 moves 4000k lines around and ends up ~260k in size, I think it may
> have hit vger limit.

Yeah probably. Thanks for looking at this.

I think I will have to split it into smaller patches in a v2.

>>> Instead of waiting for the patch to appear on the list, you might want
>>> to use branch libify-apply-use-in-am25 in my GitHub repo:
>>>
>>> https://github.com/chriscool/git/commits/libify-apply-use-in-am25
>>
>> Hmm, that branch doesn't correspond directly to the patches you sent
>> out (there are 86 commits, some marked with draft. I think commit d13d2ac
>> corresponds kinda to patch #83, but .... ).
>>
>> I think I'll wait to see the patches as you intend them to be seen. ;-)
>
> I git-am'd the series then compared with the rebased version of
> libify-apply-use-in-am25 on master. 33198a1 (i.e.
> libify-apply-use-in-am25^) matches what was sent in content (didn't
> compare commit messages).

Thanks for checking.

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 00/83] libify apply and use lib in am
  2016-04-25  9:02 ` Duy Nguyen
@ 2016-04-25  9:57   ` Christian Couder
  2016-04-25 10:11     ` Duy Nguyen
  0 siblings, 1 reply; 156+ messages in thread
From: Christian Couder @ 2016-04-25  9:57 UTC (permalink / raw)
  To: Duy Nguyen
  Cc: Git Mailing List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Johannes Schindelin, Stefan Beller, Matthieu Moy,
	Christian Couder

On Mon, Apr 25, 2016 at 11:02 AM, Duy Nguyen <pclouds@gmail.com> wrote:
> On Sun, Apr 24, 2016 at 8:33 PM, Christian Couder
> <christian.couder@gmail.com> wrote:
>> Sorry if this patch series is a bit long. I can split it into two or
>> more series if it is prefered.
>
> I suspect you deliberately made the series long just to show off how
> good new git-rebase is ;-)

Yeah, and `git am` too :-)

>> Performance numbers:
>>
>>   - A few days ago Æ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
>
> I was a bit puzzled why split-index helped so much. It shouldn't have
> in my opinion unless you paired it with index-helper, to cut down both
> read and write time. So I ran some tests. Long story short, I think we
> can achieve this gain (and a little more) even without split-index.

Yeah, perhaps. For now though Ævar and myself would be happy to just
have a config option for split-index :-)

The other performance numbers I mentioned show that now the `git am`
part of a 13 commit long rebase is reduced from 58% to 19% of the
whole rebase time. So further improvements in speeding up `git am`
could only make such a rebase at most 19% faster.

> I ran my measurement patch [1] with and without your series used this
> series as rebase material. Without the series, the picture is not so
> surprising. We run git-apply 80+ times, each consists of this sequence
>
> read index
> write index (cache tree updates only)
> read index again
> optionally initialize name hash (when new entries are added, I guess)
> read packed-refs
> write index
>
> With this series, we run a single git-apply which does
>
> read index (and sharedindex too if in split-index mode)
> initialize name hash
> write index 80+ times
>
> This explains why I guessed it wrong: when you write only, not read
> back, of course index-helper is not required. And with split-index you
> only write as many entries as you touch (which is usually a small
> number compared to worktree's size), so writing 80+ times with
> split-index is a lot faster than write 80+ times with whole index.

Yeah, I tried to explain in the cover letter and in the last patch why
this series enables split-index to give great results, but I think you
are much better at explaining it.

> But why write so many times when nobody reads it? We only need to
> write before git-apply exits,

You mean `git am` here I think.

> either after successfully applying the
> whole series, or after it stops at conflicts, and maybe even at die()
> and SIGINT. Yes if git-apply segfaults,

Here too.

> then the index update is lost,
> but in such a case, it's usually a good idea to restart fresh anyway.
> When you only write index once (or twice) it won't matter if
> split-index is used.

Yeah I agree, but it would need further work, that can be done after
this series is merged.
And I am not sure if the potential gains on a typical rebase would be worth it.

Thanks,
Christian.

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 00/83] libify apply and use lib in am
  2016-04-25  9:57   ` Christian Couder
@ 2016-04-25 10:11     ` Duy Nguyen
  0 siblings, 0 replies; 156+ messages in thread
From: Duy Nguyen @ 2016-04-25 10:11 UTC (permalink / raw)
  To: Christian Couder
  Cc: Git Mailing List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Johannes Schindelin, Stefan Beller, Matthieu Moy,
	Christian Couder

On Mon, Apr 25, 2016 at 4:57 PM, Christian Couder
<christian.couder@gmail.com> wrote:
>> But why write so many times when nobody reads it? We only need to
>> write before git-apply exits,
>
> You mean `git am` here I think.
>
>> either after successfully applying the
>> whole series, or after it stops at conflicts, and maybe even at die()
>> and SIGINT. Yes if git-apply segfaults,
>
> Here too.

Yep it's git-am. I didn't read the series, I simply ran and misread
the traces a bit.

>> then the index update is lost,
>> but in such a case, it's usually a good idea to restart fresh anyway.
>> When you only write index once (or twice) it won't matter if
>> split-index is used.
>
> Yeah I agree, but it would need further work, that can be done after
> this series is merged.

Sure.

> And I am not sure if the potential gains on a typical rebase would be worth it.

I didn't point it out, but in pathological cases where your patch
series touches a lot of (or even every) files in the worktree, the
gain from split-index lowers and could even disappear. I don't know
how often that can happen in real life though.

Also, if you start to use split-index often, please note that I
haven't addressed the sharedindex.* pruning part (it's labeled
"experimental" for a reason), you may have to un-split the index and
rm $GIT_DIR/sharedindex.* manually from time to time to keep disk
usage down.
-- 
Duy

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 53/83] builtin/apply: make find_header() return -1 instead of die()ing
  2016-04-24 13:33 ` [PATCH 53/83] builtin/apply: make find_header() " Christian Couder
@ 2016-04-25 13:18   ` Duy Nguyen
  2016-04-27 18:10     ` Eric Sunshine
  2016-04-27 18:08   ` Eric Sunshine
  1 sibling, 1 reply; 156+ messages in thread
From: Duy Nguyen @ 2016-04-25 13:18 UTC (permalink / raw)
  To: Christian Couder
  Cc: Git Mailing List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Johannes Schindelin, Stefan Beller, Matthieu Moy,
	Christian Couder

On Sun, Apr 24, 2016 at 8:33 PM, Christian Couder
<christian.couder@gmail.com> wrote:
> To be compatible with the rest of the error handling in builtin/apply.c,
> find_header() should return -1 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.

I don't think this is a good way to go. Too many magic numbers. I
don't have a better option though. Maybe returning names instead of
numbers would help a bit.

>  test_expect_success 'compare diagnostic; ensure file is still here' '
> -       echo "fatal: git diff header lacks filename information (line 4)" >expected &&
> +       echo "error: git diff header lacks filename information (line 4)" >expected &&
>         test_path_is_file f &&
>         test_cmp expected actual

Not your fault. But this test_cmp should be test_i18ncmp. I don't know
if anybody has still been testing with gettext poison though. May
probably not worth changing.
-- 
Duy

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 61/83] builtin/apply: libify check_apply_state()
  2016-04-24 13:34 ` [PATCH 61/83] builtin/apply: libify check_apply_state() Christian Couder
@ 2016-04-25 13:26   ` Duy Nguyen
  2016-05-01 16:58     ` Christian Couder
  0 siblings, 1 reply; 156+ messages in thread
From: Duy Nguyen @ 2016-04-25 13:26 UTC (permalink / raw)
  To: Christian Couder
  Cc: Git Mailing List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Johannes Schindelin, Stefan Beller, Matthieu Moy,
	Christian Couder

On Sun, Apr 24, 2016 at 8:34 PM, Christian Couder
<christian.couder@gmail.com> wrote:
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---
>  builtin/apply.c | 16 +++++++++-------
>  1 file changed, 9 insertions(+), 7 deletions(-)
>
> diff --git a/builtin/apply.c b/builtin/apply.c
> index e3ee199..7576ec5 100644
> --- a/builtin/apply.c
> +++ b/builtin/apply.c
> @@ -4534,17 +4534,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.");

Sweet opportunity to _() these messages since it clear shows in this
patch that other messages of the same category are _()'d in this
function. Could be done as a separate patch. But I'm also ok if you
say "no".

>         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)
> @@ -4552,14 +4552,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;
>  }
-- 
Duy

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 63/83] builtin/apply: make apply_all_patches() return -1 on error
  2016-04-24 13:34 ` [PATCH 63/83] builtin/apply: make apply_all_patches() return -1 on error Christian Couder
@ 2016-04-25 13:30   ` Duy Nguyen
  2016-05-01 18:32     ` Christian Couder
  2016-05-01 21:03   ` Eric Sunshine
  1 sibling, 1 reply; 156+ messages in thread
From: Duy Nguyen @ 2016-04-25 13:30 UTC (permalink / raw)
  To: Christian Couder
  Cc: Git Mailing List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Johannes Schindelin, Stefan Beller, Matthieu Moy,
	Christian Couder

On Sun, Apr 24, 2016 at 8:34 PM, Christian Couder
<christian.couder@gmail.com> wrote:
>         if (state->update_index) {
>                 if (write_locked_index(&the_index, state->lock_file, COMMIT_LOCK))
> -                       die(_("Unable to write new index file"));
> +                       return error(_("Unable to write new index file"));
>         }
>
>         return !!errs;
> @@ -4698,5 +4698,8 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
>         if (check_apply_state(&state, force_apply))
>                 exit(1);
>
> -       return apply_all_patches(&state, argc, argv, options);
> +       if (apply_all_patches(&state, argc, argv, options))
> +               exit(1);

Abusing exit() too much? This is cmd_apply(). A return should be
enough and you can do the !! trick that is used in
apply_all_patches(), shown just a little bit above.

> +
> +       return 0;
>  }
> --
> 2.8.1.300.g5fed0c0
>



-- 
Duy

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 65/83] builtin/apply: make gitdiff_verify_name() return -1 on error
  2016-04-24 13:34 ` [PATCH 65/83] builtin/apply: make gitdiff_verify_name() " Christian Couder
@ 2016-04-25 13:36   ` Duy Nguyen
  2016-05-01 19:31     ` Christian Couder
  0 siblings, 1 reply; 156+ messages in thread
From: Duy Nguyen @ 2016-04-25 13:36 UTC (permalink / raw)
  To: Christian Couder
  Cc: Git Mailing List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Johannes Schindelin, Stefan Beller, Matthieu Moy,
	Christian Couder

On Sun, Apr 24, 2016 at 8:34 PM, Christian Couder
<christian.couder@gmail.com> wrote:
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---
>  builtin/apply.c | 52 ++++++++++++++++++++++++++++++----------------------
>  1 file changed, 30 insertions(+), 22 deletions(-)
>
> diff --git a/builtin/apply.c b/builtin/apply.c
> index 6b8ba2a..268356b 100644
> --- a/builtin/apply.c
> +++ b/builtin/apply.c
> @@ -812,7 +812,7 @@ static int gitdiff_hdrend(struct apply_state *state,
>                           const char *line,
>                           struct patch *patch)
>  {
> -       return -1;
> +       return 1;
>  }

These are in a function group that will be called as p->fn() in
parse_git_header(). Is it possible to isolate them in a separate
patch? This patch can follow after, which covers only
gitdiff_verify_name() returning -1 and its call sites.

>  static int gitdiff_oldmode(struct apply_state *state,
> @@ -1016,7 +1018,7 @@ static int gitdiff_unrecognized(struct apply_state *state,
>                                 const char *line,
>                                 struct patch *patch)
>  {
> -       return -1;
> +       return 1;
>  }
>
>  /*
> @@ -1248,9 +1250,13 @@ static int parse_git_header(struct apply_state *state,
>                 for (i = 0; i < ARRAY_SIZE(optable); i++) {
>                         const struct opentry *p = optable + i;
>                         int oplen = strlen(p->str);
> +                       int res;
>                         if (len < oplen || memcmp(p->str, line, oplen))
>                                 continue;
> -                       if (p->fn(state, line + oplen, patch) < 0)
> +                       res = p->fn(state, line + oplen, patch);
> +                       if (res < 0)
> +                               return -1;
> +                       if (res > 0)
>                                 return offset;
>                         break;
>                 }
-- 
Duy

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 78/83] Move libified code from builtin/apply.c to apply.{c,h}
       [not found] ` <1461504863-15946-79-git-send-email-chriscool@tuxfamily.org>
@ 2016-04-25 13:46   ` Duy Nguyen
  2016-05-04 10:39     ` Christian Couder
  0 siblings, 1 reply; 156+ messages in thread
From: Duy Nguyen @ 2016-04-25 13:46 UTC (permalink / raw)
  To: Christian Couder
  Cc: Git Mailing List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Johannes Schindelin, Stefan Beller, Matthieu Moy,
	Christian Couder

On Sun, Apr 24, 2016 at 8:34 PM, Christian Couder
<christian.couder@gmail.com> wrote:
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---
>  apply.c         | 4678 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
>  apply.h         |   19 +
>  builtin/apply.c | 4677 ------------------------------------------------------
>  3 files changed, 4695 insertions(+), 4679 deletions(-)

Maybe if you can rearrange functions in the right order in
builtin/apply.c first and move everything in one patch (i.e. apply.c
is created here), rename detection kicks in realizes apply.c is a
rename of (old) builtin/apply.c and only shows the diff of (new)
builtin/apply.c which is about 90 lines... I haven't tested if this is
true though.
-- 
Duy

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 00/83] libify apply and use lib in am
  2016-04-24 16:56     ` Christian Couder
  2016-04-24 17:42       ` Ramsay Jones
@ 2016-04-25 15:09       ` Johannes Schindelin
  1 sibling, 0 replies; 156+ messages in thread
From: Johannes Schindelin @ 2016-04-25 15:09 UTC (permalink / raw)
  To: Christian Couder
  Cc: Ramsay Jones, git, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Stefan Beller, Matthieu Moy,
	Christian Couder

Hi Chris,

On Sun, 24 Apr 2016, Christian Couder wrote:

> On Sun, Apr 24, 2016 at 6:27 PM, Christian Couder
> <christian.couder@gmail.com> wrote:
> > On Sun, Apr 24, 2016 at 5:23 PM, Ramsay Jones
> > <ramsay@ramsayjones.plus.com> wrote:
> >>
> >>
> >> On 24/04/16 14:33, Christian Couder wrote:
> >>> This is a patch series about libifying `git apply` functionality, and
> >>> using this libified functionality in `git am`, so that no 'git apply'
> >>> process is spawn anymore. This makes `git am` significantly faster, so
> >>> `git rebase`, when it uses the am backend, is also significantly
> >>> faster.
> >>
> >> I just tried to git-am these patches, but patch #78 did not make it
> >> to the list.
> >
> > That's strange because gmail tells me it has been sent, and it made it
> > to chriscool@tuxfamily.org.
> 
> Instead of waiting for the patch to appear on the list, you might want
> to use branch libify-apply-use-in-am25 in my GitHub repo:
> 
> https://github.com/chriscool/git/commits/libify-apply-use-in-am25

Thanks for this. In particular with longer patch series, I find mail-based
patch submissions *really* cumbersome, not only on the submitter's side
but also on the consumers' side.

I wonder, however, why you use numbers in the branch name to version
things? I thought Git allowed for more advanced versioning than that...

:-)

Ciao,
Dscho

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 49/83] builtin/apply: move 'lock_file' global into 'struct apply_state'
  2016-04-25  7:50   ` Eric Sunshine
@ 2016-04-25 17:55     ` Junio C Hamano
  2016-04-28 16:30       ` Christian Couder
  2016-04-30 19:39     ` Christian Couder
  1 sibling, 1 reply; 156+ messages in thread
From: Junio C Hamano @ 2016-04-25 17:55 UTC (permalink / raw)
  To: Eric Sunshine
  Cc: Christian Couder, Git List, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

Eric Sunshine <sunshine@sunshineco.com> writes:

>> +       /*
>> +        * 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;
>
> Is there ever a time when lock_file is removed from the list (such as
> upon successful write of the index), in which case it would be safe to
> free() this?

I do not think you need to think about "free"ing.

Even if the libified version of the apply internal can be called
multiple times to process multiple patch inputs, there is no need to
run multiple instances of it yet.  And a lockfile, after the caller
finishes interacting with one file using it by calling commit or
rollback, can be reused to interact with another file.

So the cleanest would be to:

 * make the caller of apply API responsible for preparing a static
   or (allocating and leaking) lockfile instance,

 * make it pass a pointer to the lockfile to the apply API so that
   it can store it in apply_state, and

 * have the caller use apply API feeding one patch at a time in a
   loop, allowing the same lockfile instance used for each
   "invocation".

I sounded as if I were advocating non-reentrant implementation in
the introductory paragraph, but that is not the intention.  If you
want to do N threads, you would prepare N lockfile instances, give
them to N apply API instances to be stored in N apply_state
instances, and you would have N parallel applications, if you wanted
to.

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 05/83] builtin/apply: extract line_by_line_fuzzy_match() from match_fragment()
  2016-04-24 13:33 ` [PATCH 05/83] builtin/apply: extract line_by_line_fuzzy_match() from match_fragment() Christian Couder
@ 2016-04-25 18:50   ` Stefan Beller
  2016-04-26 16:15     ` Christian Couder
  0 siblings, 1 reply; 156+ messages in thread
From: Stefan Beller @ 2016-04-25 18:50 UTC (permalink / raw)
  To: Christian Couder
  Cc: git@vger.kernel.org, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Matthieu Moy,
	Christian Couder

> @@ -2251,7 +2319,7 @@ static int match_fragment(struct image *img,
>                           int match_beginning, int match_end)
>  {
>         int i;
> -       char *fixed_buf, *buf, *orig, *target;
> +       char *fixed_buf, *orig, *target;
>         struct strbuf fixed;
>         size_t fixed_len, postlen;
>         int preimage_limit;
> @@ -2312,6 +2380,7 @@ static int match_fragment(struct image *img,
>                  * There must be one non-blank context line that match
>                  * a line before the end of img.
>                  */
> +               char *buf;

patches 1-4 looking good, with no comment from me. Here is the first spot to
comment on.

It's not clear why we need to declare buf here? Oh wait it is. It's just
moved from the start of the function. But why do it in this patch?
It seems unrelated to the general intent of the patch. No need to reroll
for this nit alone, it just took me a while to figure out it was an unrelated
thing.


>                 char *buf_end;
>
>                 buf = preimage->buf;

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 09/83] builtin/apply: move 'check' global into 'struct apply_state'
  2016-04-24 13:33 ` [PATCH 09/83] builtin/apply: move 'check' " Christian Couder
@ 2016-04-25 18:57   ` Stefan Beller
  2016-04-26 16:26     ` Christian Couder
  0 siblings, 1 reply; 156+ messages in thread
From: Stefan Beller @ 2016-04-25 18:57 UTC (permalink / raw)
  To: Christian Couder
  Cc: git@vger.kernel.org, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Matthieu Moy,
	Christian Couder

On Sun, Apr 24, 2016 at 6:33 AM, Christian Couder
<christian.couder@gmail.com> wrote:
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---
>  builtin/apply.c | 16 +++++++++-------
>  1 file changed, 9 insertions(+), 7 deletions(-)
>
> diff --git a/builtin/apply.c b/builtin/apply.c
> index ad81210..6c628f6 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

This is true, but at this part of the file/code we rather want to know what
`check` does, instead of what the command line option --check does.
(They are 2 different things, though one leading to the other one?) How about:

    /*
     * Only check the files to be modified, but do not modify the files.
     */


>  /*
> - *  --check turns on checking that the working tree matches the
> - *    files that are being modified, but doesn't apply the patch

Oh I see it was moved from here. Not sure if we want to rename
comments along the way or just keep it in this series.

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 18/83] builtin/apply: move 'numstat' global into 'struct apply_state'
  2016-04-24 13:33 ` [PATCH 18/83] builtin/apply: move 'numstat' " Christian Couder
@ 2016-04-25 21:40   ` Stefan Beller
  2016-04-26 16:35     ` Christian Couder
  0 siblings, 1 reply; 156+ messages in thread
From: Stefan Beller @ 2016-04-25 21:40 UTC (permalink / raw)
  To: Christian Couder
  Cc: git@vger.kernel.org, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Matthieu Moy,
	Christian Couder

On Sun, Apr 24, 2016 at 6:33 AM, Christian Couder
<christian.couder@gmail.com> wrote:
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---
>  builtin/apply.c | 11 ++++++-----
>  1 file changed, 6 insertions(+), 5 deletions(-)
>
> diff --git a/builtin/apply.c b/builtin/apply.c
> index d90948a..16d78f9 100644
> --- a/builtin/apply.c
> +++ b/builtin/apply.c
> @@ -36,6 +36,9 @@ struct apply_state {
>         /* --stat does just a diffstat, and doesn't actually apply */
>         int diffstat;
>
> +       /* --numstat does numeric diffstat, and doesn't actually apply */
> +       int numstat;
> +
>         /*
>          *  --check turns on checking that the working tree matches the
>          *    files that are being modified, but doesn't apply the patch
> @@ -51,14 +54,12 @@ struct apply_state {
>  };
>
>  /*
> - *  --numstat does numeric diffstat, and doesn't actually apply
>   *  --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 numstat;
>  static int summary;
>  static int apply = 1;
>  static int no_add;
> @@ -4500,7 +4501,7 @@ static int apply_patch(struct apply_state *state,
>         if (state->diffstat)
>                 stat_patch_list(list);
>
> -       if (numstat)
> +       if (state->numstat)
>                 numstat_patch_list(list);
>
>         if (summary)
> @@ -4598,7 +4599,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
>                         N_("instead of applying the patch, output diffstat for the input")),
>                 OPT_NOOP_NOARG(0, "allow-binary-replacement"),
>                 OPT_NOOP_NOARG(0, "binary"),
> -               OPT_BOOL(0, "numstat", &numstat,
> +               OPT_BOOL(0, "numstat", &state.numstat,
>                         N_("show number of added and deleted lines in decimal notation")),
>                 OPT_BOOL(0, "summary", &summary,
>                         N_("instead of applying the patch, output a summary for the input")),
> @@ -4675,7 +4676,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 || numstat || summary || state.check || fake_ancestor))
> +       if (!force_apply && (state.diffstat || state.numstat || summary || state.check || fake_ancestor))

Mental note: This patch is just doing a mechanical conversion, so it
is fine to check for many "state.FOOs" here.

However later we may want to move this out to a static oneliner like:

    static int really_apply(state *s) {
      return s->diffstat || s->numstat || ...;
    }

(with a better name obviously)


>                 apply = 0;
>         if (state.check_index && is_not_gitdir)
>                 die(_("--index outside a repository"));
> --
> 2.8.1.300.g5fed0c0
>
> --
> To unsubscribe from this list: send the line "unsubscribe git" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 33/83] builtin/apply: move 'root' global into 'struct apply_state'
  2016-04-24 13:33 ` [PATCH 33/83] builtin/apply: move 'root' " Christian Couder
@ 2016-04-25 21:50   ` Stefan Beller
  2016-04-25 21:54     ` Stefan Beller
  2016-04-26 20:13     ` Christian Couder
  0 siblings, 2 replies; 156+ messages in thread
From: Stefan Beller @ 2016-04-25 21:50 UTC (permalink / raw)
  To: Christian Couder
  Cc: git@vger.kernel.org, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Matthieu Moy,
	Christian Couder

On Sun, Apr 24, 2016 at 6:33 AM, Christian Couder
<christian.couder@gmail.com> wrote:
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---
>  builtin/apply.c | 82 ++++++++++++++++++++++++++++++++++-----------------------
>  1 file changed, 49 insertions(+), 33 deletions(-)
>
> diff --git a/builtin/apply.c b/builtin/apply.c
> index fecdb66..209a1b4 100644
> --- a/builtin/apply.c
> +++ b/builtin/apply.c
> @@ -73,6 +73,8 @@ struct apply_state {
>
>         struct string_list limit_by_name;
>         int has_include;
> +
> +       struct strbuf root;
>  };
>
>  static int newfd = -1;
> @@ -98,8 +100,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) {
> @@ -489,7 +489,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;
> @@ -513,8 +516,8 @@ static char *find_name_gnu(const char *line, const char *def, int p_value)
>         }
>
>         strbuf_remove(&name, 0, cp - name.buf);
> -       if (root.len)
> -               strbuf_insert(&name, 0, root.buf, root.len);
> +       if (state->root.len)
> +               strbuf_insert(&name, 0, state->root.buf, state->root.len);
>         return squash_slash(strbuf_detach(&name, NULL));
>  }
>
> @@ -677,8 +680,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;
> @@ -716,32 +723,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;
>         }
> @@ -749,10 +763,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)
> @@ -777,7 +791,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_traditional(state, nameline, NULL, 0);
>         if (!name)
>                 return -1;
>         cp = strchr(name, '/');
> @@ -902,17 +916,17 @@ static void parse_traditional_patch(struct apply_state *state,
>         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(state, 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(state, 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(state, first, NULL, state->p_value);
> +               name = find_name_traditional(state, second, first_name, state->p_value);
>                 free(first_name);
>                 if (has_epoch_timestamp(first)) {
>                         patch->is_new = 1;
> @@ -957,7 +971,7 @@ static void gitdiff_verify_name(struct apply_state *state,
>                                 int side)
>  {
>         if (!*name && !isnull) {
> -               *name = find_name(line, NULL, state->p_value, TERM_TAB);
> +               *name = find_name(state, line, NULL, state->p_value, TERM_TAB);
>                 return;
>         }
>
> @@ -967,7 +981,7 @@ static void gitdiff_verify_name(struct apply_state *state,
>                 if (isnull)
>                         die(_("git apply: bad git-diff - expected /dev/null, got %s on line %d"),
>                             *name, linenr);
> -               another = find_name(line, NULL, state->p_value, TERM_TAB);
> +               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") :
> @@ -1042,7 +1056,7 @@ static int gitdiff_copysrc(struct apply_state *state,
>  {
>         patch->is_copy = 1;
>         free(patch->old_name);
> -       patch->old_name = find_name(line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
> +       patch->old_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
>         return 0;
>  }
>
> @@ -1052,7 +1066,7 @@ static int gitdiff_copydst(struct apply_state *state,
>  {
>         patch->is_copy = 1;
>         free(patch->new_name);
> -       patch->new_name = find_name(line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
> +       patch->new_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
>         return 0;
>  }
>
> @@ -1062,7 +1076,7 @@ static int gitdiff_renamesrc(struct apply_state *state,
>  {
>         patch->is_rename = 1;
>         free(patch->old_name);
> -       patch->old_name = find_name(line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
> +       patch->old_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
>         return 0;
>  }
>
> @@ -1072,7 +1086,7 @@ static int gitdiff_renamedst(struct apply_state *state,
>  {
>         patch->is_rename = 1;
>         free(patch->new_name);
> -       patch->new_name = find_name(line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
> +       patch->new_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);
>         return 0;
>  }
>
> @@ -1331,8 +1345,8 @@ static int parse_git_header(struct apply_state *state,
>          * the default name from the header.
>          */
>         patch->def_name = git_header_name(state, line, len);
> -       if (patch->def_name && root.len) {
> -               char *s = xstrfmt("%s%s", root.buf, patch->def_name);
> +       if (patch->def_name && state->root.len) {
> +               char *s = xstrfmt("%s%s", state->root.buf, patch->def_name);
>                 free(patch->def_name);
>                 patch->def_name = s;
>         }
> @@ -4630,9 +4644,10 @@ static int option_parse_whitespace(const struct option *opt,
>  static int option_parse_directory(const struct option *opt,
>                                   const char *arg, int unset)
>  {
> -       strbuf_reset(&root);
> -       strbuf_addstr(&root, arg);
> -       strbuf_complete(&root, '/');
> +       struct apply_state *state = opt->value;

Or even

    struct strbuf root = ((state*)opt->value)->root;

and then keep the next lines as is?

> +       strbuf_reset(&state->root);
> +       strbuf_addstr(&state->root, arg);
> +       strbuf_complete(&state->root, '/');
>         return 0;
>  }
>
> @@ -4711,7 +4726,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
>                 OPT_BIT(0, "recount", &options,
>                         N_("do not trust the line counts in the hunk headers"),
>                         RECOUNT),
> -               { OPTION_CALLBACK, 0, "directory", NULL, N_("root"),
> +               { OPTION_CALLBACK, 0, "directory", &state, N_("root"),
>                         N_("prepend <root> to all filenames"),
>                         0, option_parse_directory },
>                 OPT_END()
> @@ -4724,6 +4739,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
>         state.line_termination = '\n';
>         state.p_value = 1;
>         state.p_context = UINT_MAX;
> +       strbuf_init(&state.root, 0);

Eventually we want to have some sort of `init_apply_state` function or
a define which has all the values, I guess?

Compare for example to sliding_window.h, where
we have

    struct sliding_view {
        ...
        struct strbuf buf;
    };

    #define SLIDING_VIEW_INIT(..., STRBUF_INIT }

>
>         git_apply_config();
>         if (apply_default_whitespace)
> --
> 2.8.1.300.g5fed0c0
>
> --
> To unsubscribe from this list: send the line "unsubscribe git" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 33/83] builtin/apply: move 'root' global into 'struct apply_state'
  2016-04-25 21:50   ` Stefan Beller
@ 2016-04-25 21:54     ` Stefan Beller
  2016-04-26 20:13     ` Christian Couder
  1 sibling, 0 replies; 156+ messages in thread
From: Stefan Beller @ 2016-04-25 21:54 UTC (permalink / raw)
  To: Christian Couder
  Cc: git@vger.kernel.org, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Matthieu Moy,
	Christian Couder

>
> Eventually we want to have some sort of `init_apply_state` function or
> a define which has all the values, I guess?
>
> Compare for example to sliding_window.h, where
> we have
>
>     struct sliding_view {
>         ...
>         struct strbuf buf;
>     };
>
>     #define SLIDING_VIEW_INIT(..., STRBUF_INIT }

Nevermind, just read patch
"builtin/apply: move 'state' init into init_apply_state()"

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 47/83] builtin/apply: move applying patches into apply_all_patches()
  2016-04-24 13:33 ` [PATCH 47/83] builtin/apply: move applying patches into apply_all_patches() Christian Couder
@ 2016-04-25 22:00   ` Stefan Beller
  2016-04-27 15:51     ` Christian Couder
  0 siblings, 1 reply; 156+ messages in thread
From: Stefan Beller @ 2016-04-25 22:00 UTC (permalink / raw)
  To: Christian Couder
  Cc: git@vger.kernel.org, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Matthieu Moy,
	Christian Couder

On Sun, Apr 24, 2016 at 6:33 AM, Christian Couder
<christian.couder@gmail.com> wrote:
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>

Up to this patch, have a
Reviewed-by: Stefan Beller <sbeller@google.com>
in case you want to split the series in here (as indicated in the
cover letter, this was the last
patch rerolled, the next patches are new and may need more discussion).

I had some nits, but they cleared up in later patches.

Thanks,
Stefan

> ---
>  builtin/apply.c | 128 ++++++++++++++++++++++++++++++--------------------------
>  1 file changed, 69 insertions(+), 59 deletions(-)
>
> diff --git a/builtin/apply.c b/builtin/apply.c
> index 94bf120..787426f 100644
> --- a/builtin/apply.c
> +++ b/builtin/apply.c
> @@ -4720,13 +4720,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, "<stdin>", 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, "<stdin>", 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(&the_index, &lock_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[] = {
> @@ -4805,61 +4871,5 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
>
>         check_apply_state(&state, force_apply);
>
> -       for (i = 0; i < argc; i++) {
> -               const char *arg = argv[i];
> -               int fd;
> -
> -               if (!strcmp(arg, "-")) {
> -                       errs |= apply_patch(&state, 0, "<stdin>", 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, "<stdin>", 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(&the_index, &lock_file, COMMIT_LOCK))
> -                       die(_("Unable to write new index file"));
> -       }
> -
> -       return !!errs;
> +       return apply_all_patches(&state, argc, argv, options);
>  }
> --
> 2.8.1.300.g5fed0c0
>

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 51/83] builtin/apply: make apply_patch() return -1 instead of die()ing
  2016-04-24 13:33 ` [PATCH 51/83] builtin/apply: make apply_patch() return -1 instead of die()ing Christian Couder
@ 2016-04-26  1:20   ` Eric Sunshine
  2016-04-30 19:41     ` Christian Couder
  0 siblings, 1 reply; 156+ messages in thread
From: Eric Sunshine @ 2016-04-26  1:20 UTC (permalink / raw)
  To: Christian Couder
  Cc: Git List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

On Sun, Apr 24, 2016 at 9:33 AM, Christian Couder
<christian.couder@gmail.com> wrote:
> 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.
>
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---
> diff --git a/builtin/apply.c b/builtin/apply.c
> @@ -4522,6 +4522,14 @@ static int write_out_results(struct apply_state *state, struct patch *list)
>  static int apply_patch(struct apply_state *state,
>                        int fd,
>                        const char *filename,
> @@ -4564,7 +4572,7 @@ static int apply_patch(struct apply_state *state,
>         }
>
>         if (!list && !skipped_patch)
> -               die(_("unrecognized input"));
> +               return error(_("unrecognized input"));
>
>         if (state->whitespace_error && (state->ws_error_action == die_on_ws_error))
>                 state->apply = 0;
> @@ -4575,19 +4583,17 @@ static int apply_patch(struct apply_state *state,
>                 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)
> +               return error(_("unable to read index file"));
>
>         if ((state->check || state->apply) &&
>             check_patch_list(state, list) < 0 &&
>             !state->apply_with_reject)
> -               exit(1);
> +               return -1;
>
>         if (state->apply && write_out_results(state, list)) {
>                 if (state->apply_with_reject)
> -                       exit(1);
> +                       return -1;
>                 /* with --3way, we still need to write the index out */
>                 return 1;
>         }

Are these new 'returns' leaking 'list', 'buf', and 'fn_table' which
otherwise get released at the end of the function?

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 05/83] builtin/apply: extract line_by_line_fuzzy_match() from match_fragment()
  2016-04-25 18:50   ` Stefan Beller
@ 2016-04-26 16:15     ` Christian Couder
  2016-04-26 20:20       ` Junio C Hamano
  0 siblings, 1 reply; 156+ messages in thread
From: Christian Couder @ 2016-04-26 16:15 UTC (permalink / raw)
  To: Stefan Beller
  Cc: git@vger.kernel.org, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Matthieu Moy,
	Christian Couder

On Mon, Apr 25, 2016 at 8:50 PM, Stefan Beller <sbeller@google.com> wrote:
>> @@ -2251,7 +2319,7 @@ static int match_fragment(struct image *img,
>>                           int match_beginning, int match_end)
>>  {
>>         int i;
>> -       char *fixed_buf, *buf, *orig, *target;
>> +       char *fixed_buf, *orig, *target;
>>         struct strbuf fixed;
>>         size_t fixed_len, postlen;
>>         int preimage_limit;
>> @@ -2312,6 +2380,7 @@ static int match_fragment(struct image *img,
>>                  * There must be one non-blank context line that match
>>                  * a line before the end of img.
>>                  */
>> +               char *buf;
>
> patches 1-4 looking good, with no comment from me. Here is the first spot to
> comment on.
>
> It's not clear why we need to declare buf here? Oh wait it is. It's just
> moved from the start of the function. But why do it in this patch?
> It seems unrelated to the general intent of the patch. No need to reroll
> for this nit alone, it just took me a while to figure out it was an unrelated
> thing.

Yeah, I agree it's a bit unrelated. But rather than add another patch
to an already long series just for this, I added the following to the
commit message:

While at it, let's reduce the scope of "char *buf" in match_fragment().

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 09/83] builtin/apply: move 'check' global into 'struct apply_state'
  2016-04-25 18:57   ` Stefan Beller
@ 2016-04-26 16:26     ` Christian Couder
  2016-04-26 16:31       ` Stefan Beller
  0 siblings, 1 reply; 156+ messages in thread
From: Christian Couder @ 2016-04-26 16:26 UTC (permalink / raw)
  To: Stefan Beller
  Cc: git@vger.kernel.org, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Matthieu Moy,
	Christian Couder

On Mon, Apr 25, 2016 at 8:57 PM, Stefan Beller <sbeller@google.com> wrote:
> On Sun, Apr 24, 2016 at 6:33 AM, Christian Couder
> <christian.couder@gmail.com> wrote:
>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
>> ---
>>  builtin/apply.c | 16 +++++++++-------
>>  1 file changed, 9 insertions(+), 7 deletions(-)
>>
>> diff --git a/builtin/apply.c b/builtin/apply.c
>> index ad81210..6c628f6 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
>
> This is true, but at this part of the file/code we rather want to know what
> `check` does, instead of what the command line option --check does.
> (They are 2 different things, though one leading to the other one?) How about:
>
>     /*
>      * Only check the files to be modified, but do not modify the files.
>      */
>
>
>>  /*
>> - *  --check turns on checking that the working tree matches the
>> - *    files that are being modified, but doesn't apply the patch
>
> Oh I see it was moved from here. Not sure if we want to rename
> comments along the way or just keep it in this series.

I kept the existing comments when they were still relevant.
It could be a cleanup to change them to something like what you
suggest, but as it is not important for this series which is already
long, I prefer to leave it for now.

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 09/83] builtin/apply: move 'check' global into 'struct apply_state'
  2016-04-26 16:26     ` Christian Couder
@ 2016-04-26 16:31       ` Stefan Beller
  0 siblings, 0 replies; 156+ messages in thread
From: Stefan Beller @ 2016-04-26 16:31 UTC (permalink / raw)
  To: Christian Couder
  Cc: git@vger.kernel.org, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Matthieu Moy,
	Christian Couder

On Tue, Apr 26, 2016 at 9:26 AM, Christian Couder
<christian.couder@gmail.com> wrote:
>>
>>>  /*
>>> - *  --check turns on checking that the working tree matches the
>>> - *    files that are being modified, but doesn't apply the patch
>>
>> Oh I see it was moved from here. Not sure if we want to rename
>> comments along the way or just keep it in this series.
>
> I kept the existing comments when they were still relevant.
> It could be a cleanup to change them to something like what you
> suggest, but as it is not important for this series which is already
> long, I prefer to leave it for now.

Yeah that was my conclusion as well after some thought.

Thanks,
Stefan

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 18/83] builtin/apply: move 'numstat' global into 'struct apply_state'
  2016-04-25 21:40   ` Stefan Beller
@ 2016-04-26 16:35     ` Christian Couder
  0 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-26 16:35 UTC (permalink / raw)
  To: Stefan Beller
  Cc: git@vger.kernel.org, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Matthieu Moy,
	Christian Couder

On Mon, Apr 25, 2016 at 11:40 PM, Stefan Beller <sbeller@google.com> wrote:
> On Sun, Apr 24, 2016 at 6:33 AM, Christian Couder
> <christian.couder@gmail.com> wrote:
>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
>> ---
>>  builtin/apply.c | 11 ++++++-----
>>  1 file changed, 6 insertions(+), 5 deletions(-)
>>
>> diff --git a/builtin/apply.c b/builtin/apply.c
>> index d90948a..16d78f9 100644
>> --- a/builtin/apply.c
>> +++ b/builtin/apply.c
>> @@ -36,6 +36,9 @@ struct apply_state {
>>         /* --stat does just a diffstat, and doesn't actually apply */
>>         int diffstat;
>>
>> +       /* --numstat does numeric diffstat, and doesn't actually apply */
>> +       int numstat;
>> +
>>         /*
>>          *  --check turns on checking that the working tree matches the
>>          *    files that are being modified, but doesn't apply the patch
>> @@ -51,14 +54,12 @@ struct apply_state {
>>  };
>>
>>  /*
>> - *  --numstat does numeric diffstat, and doesn't actually apply
>>   *  --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 numstat;
>>  static int summary;
>>  static int apply = 1;
>>  static int no_add;
>> @@ -4500,7 +4501,7 @@ static int apply_patch(struct apply_state *state,
>>         if (state->diffstat)
>>                 stat_patch_list(list);
>>
>> -       if (numstat)
>> +       if (state->numstat)
>>                 numstat_patch_list(list);
>>
>>         if (summary)
>> @@ -4598,7 +4599,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
>>                         N_("instead of applying the patch, output diffstat for the input")),
>>                 OPT_NOOP_NOARG(0, "allow-binary-replacement"),
>>                 OPT_NOOP_NOARG(0, "binary"),
>> -               OPT_BOOL(0, "numstat", &numstat,
>> +               OPT_BOOL(0, "numstat", &state.numstat,
>>                         N_("show number of added and deleted lines in decimal notation")),
>>                 OPT_BOOL(0, "summary", &summary,
>>                         N_("instead of applying the patch, output a summary for the input")),
>> @@ -4675,7 +4676,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 || numstat || summary || state.check || fake_ancestor))
>> +       if (!force_apply && (state.diffstat || state.numstat || summary || state.check || fake_ancestor))
>
> Mental note: This patch is just doing a mechanical conversion, so it
> is fine to check for many "state.FOOs" here.
>
> However later we may want to move this out to a static oneliner like:
>
>     static int really_apply(state *s) {
>       return s->diffstat || s->numstat || ...;
>     }
>
> (with a better name obviously)

Yeah, this is another cleanup that could be done.
I added it to a list and will try to take care of it later.

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 33/83] builtin/apply: move 'root' global into 'struct apply_state'
  2016-04-25 21:50   ` Stefan Beller
  2016-04-25 21:54     ` Stefan Beller
@ 2016-04-26 20:13     ` Christian Couder
  1 sibling, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-26 20:13 UTC (permalink / raw)
  To: Stefan Beller
  Cc: git@vger.kernel.org, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Matthieu Moy,
	Christian Couder

On Mon, Apr 25, 2016 at 11:50 PM, Stefan Beller <sbeller@google.com> wrote:
> On Sun, Apr 24, 2016 at 6:33 AM, Christian Couder
> <christian.couder@gmail.com> wrote:
>
>> @@ -4630,9 +4644,10 @@ static int option_parse_whitespace(const struct option *opt,
>>  static int option_parse_directory(const struct option *opt,
>>                                   const char *arg, int unset)
>>  {
>> -       strbuf_reset(&root);
>> -       strbuf_addstr(&root, arg);
>> -       strbuf_complete(&root, '/');
>> +       struct apply_state *state = opt->value;
>
> Or even
>
>     struct strbuf root = ((state*)opt->value)->root;
>
> and then keep the next lines as is?

I found it more coherent to have all the option_parse_*() functions do:

    struct apply_state *state = opt->value;

as their first instruction.

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 05/83] builtin/apply: extract line_by_line_fuzzy_match() from match_fragment()
  2016-04-26 16:15     ` Christian Couder
@ 2016-04-26 20:20       ` Junio C Hamano
  2016-04-27 13:31         ` Christian Couder
  0 siblings, 1 reply; 156+ messages in thread
From: Junio C Hamano @ 2016-04-26 20:20 UTC (permalink / raw)
  To: Christian Couder
  Cc: Stefan Beller, git@vger.kernel.org, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Matthieu Moy,
	Christian Couder

Christian Couder <christian.couder@gmail.com> writes:

>> It's not clear why we need to declare buf here? Oh wait it is. It's just
>> moved from the start of the function. But why do it in this patch?
>> It seems unrelated to the general intent of the patch. No need to reroll
>> for this nit alone, it just took me a while to figure out it was an unrelated
>> thing.
>
> Yeah, I agree it's a bit unrelated. But rather than add another patch
> to an already long series just for this,...

Isn't not doing the unrelated move at all a more sensible
alternative, if that change does not deserve its own place in the
series after all?

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 10/83] builtin/apply: move 'check_index' global into 'struct apply_state'
  2016-04-24 13:33 ` [PATCH 10/83] builtin/apply: move 'check_index' " Christian Couder
@ 2016-04-26 20:25   ` Junio C Hamano
  0 siblings, 0 replies; 156+ messages in thread
From: Junio C Hamano @ 2016-04-26 20:25 UTC (permalink / raw)
  To: Christian Couder
  Cc: git, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

Christian Couder <christian.couder@gmail.com> writes:

> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---
>  builtin/apply.c | 69 +++++++++++++++++++++++++++++++++------------------------
>  1 file changed, 40 insertions(+), 29 deletions(-)
>
> diff --git a/builtin/apply.c b/builtin/apply.c
> index 6c628f6..3f8671c 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;

I like the way this series moves only a few variables at a time to
limit the scope of each step.  I would have expected check-index and
cached to touch pretty much the same codepaths (the latter would
involve a subset of the codepaths involved for the former), but
doing them separately is fine.

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 22/83] builtin/apply: move 'unsafe_paths' global into 'struct apply_state'
  2016-04-24 13:33 ` [PATCH 22/83] builtin/apply: move 'unsafe_paths' " Christian Couder
@ 2016-04-26 20:27   ` Junio C Hamano
  2016-04-27 12:14     ` Christian Couder
  0 siblings, 1 reply; 156+ messages in thread
From: Junio C Hamano @ 2016-04-26 20:27 UTC (permalink / raw)
  To: Christian Couder
  Cc: git, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

Christian Couder <christian.couder@gmail.com> writes:

> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---
>  builtin/apply.c | 9 +++++----
>  1 file changed, 5 insertions(+), 4 deletions(-)
>
> diff --git a/builtin/apply.c b/builtin/apply.c
> index 506357c..c45e481 100644
> --- a/builtin/apply.c
> +++ b/builtin/apply.c
> @@ -57,6 +57,8 @@ struct apply_state {
>  	int unidiff_zero;
>  
>  	int update_index;
> +
> +	int unsafe_paths;
>  };

Having said 

    I like the way this series moves only a few variables at a time to
    limit the scope of each step.

it gets irritating to see all these unnecessary blank lines in the
structure definition added by each step, which would mean all of
these patches need to fix them in the next reroll.

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 27/83] builtin/apply: move 'read_stdin' global into cmd_apply()
  2016-04-24 13:33 ` [PATCH 27/83] builtin/apply: move 'read_stdin' global into cmd_apply() Christian Couder
@ 2016-04-26 20:28   ` Junio C Hamano
  2016-04-27 13:13     ` Christian Couder
  0 siblings, 1 reply; 156+ messages in thread
From: Junio C Hamano @ 2016-04-26 20:28 UTC (permalink / raw)
  To: Christian Couder
  Cc: git, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

Christian Couder <christian.couder@gmail.com> writes:

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

Makes sense but do so immediately next to 06/83, "options" thing?

> diff --git a/builtin/apply.c b/builtin/apply.c
> index 699cabf..be237d1 100644
> --- a/builtin/apply.c
> +++ b/builtin/apply.c
> @@ -96,7 +96,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)
>  {
> @@ -4586,6 +4585,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;
>  	struct apply_state state;
>  
>  	const char *whitespace_option = NULL;

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 39/83] builtin/apply: move 'ws_error_action' into 'struct apply_state'
  2016-04-24 13:33 ` [PATCH 39/83] builtin/apply: move 'ws_error_action' " Christian Couder
@ 2016-04-26 20:36   ` Junio C Hamano
  2016-04-26 20:40     ` Junio C Hamano
  2016-04-27 13:23     ` Christian Couder
  0 siblings, 2 replies; 156+ messages in thread
From: Junio C Hamano @ 2016-04-26 20:36 UTC (permalink / raw)
  To: Christian Couder
  Cc: git, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

Christian Couder <christian.couder@gmail.com> writes:

> +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;
> @@ -80,6 +87,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;
> @@ -89,12 +98,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;

This is a good example of a variable that needs initialization,
which is turned into a field in apply_state that needs
initialization.  It is done here:

> @@ -4738,6 +4743,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
>  	state.p_value = 1;
>  	state.p_context = UINT_MAX;
>  	state.squelch_whitespace_errors = 5;
> +	state.ws_error_action = warn_on_ws_error;
>  	strbuf_init(&state.root, 0);

and we already have these random initial values described here.

As I do not expect that cmd_apply() which is a moral equivalent of
main() will stay to be the only one who wants to see a reasonably
initialized apply_state(), I think the patch that introduced the
very first version of "struct apply_state" should also introduce a
helper function to initialize it, i.e.

	static void init_apply_state(struct apply_state *s,
        			     const char *prefix)
        {
		memset(s, '\0', sizeof(*s));
                s->prefix = prefix;
                s->prefix_length = s->prefix ? strlen(s->prefix) : 0;
	}

in [PATCH 7/xx].

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 39/83] builtin/apply: move 'ws_error_action' into 'struct apply_state'
  2016-04-26 20:36   ` Junio C Hamano
@ 2016-04-26 20:40     ` Junio C Hamano
  2016-04-27 13:23     ` Christian Couder
  1 sibling, 0 replies; 156+ messages in thread
From: Junio C Hamano @ 2016-04-26 20:40 UTC (permalink / raw)
  To: Christian Couder
  Cc: git, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

Junio C Hamano <gitster@pobox.com> writes:

> As I do not expect that cmd_apply() which is a moral equivalent of
> main() will stay to be the only one who wants to see a reasonably
> initialized apply_state(), I think the patch that introduced the
> very first version of "struct apply_state" should also introduce a
> helper function to initialize it, i.e.
>
> 	static void init_apply_state(struct apply_state *s,
>         			     const char *prefix)
>         {
> 		memset(s, '\0', sizeof(*s));
>                 s->prefix = prefix;
>                 s->prefix_length = s->prefix ? strlen(s->prefix) : 0;
> 	}
>
> in [PATCH 7/xx].

Just to avoid misunderstanding, I do not mean to say that the
init-apply-state helper that should have been introduced in 07/xx
would gain a new caller-supplied parameter ws_error_action.  This
step would have a patch to the function that does something like:

 static void init_apply_state(struct apply_state *s,
                                 const char *prefix)
 {
         memset(s, '\0', sizeof(*s));
         s->prefix = prefix;
         s->prefix_length = s->prefix ? strlen(s->prefix) : 0;
         ...
+        s->ws_error_action = warn_on_ws_error;
 }

without having the caller supply what error_action should the state
be initialized with.

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 45/83] builtin/apply: move 'state' init into init_apply_state()
  2016-04-25  7:32   ` Eric Sunshine
@ 2016-04-27 12:00     ` Christian Couder
  0 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-27 12:00 UTC (permalink / raw)
  To: Eric Sunshine
  Cc: Git List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

On Mon, Apr 25, 2016 at 9:32 AM, Eric Sunshine <sunshine@sunshineco.com> wrote:
> On Sun, Apr 24, 2016 at 9:33 AM, Christian Couder
> <christian.couder@gmail.com> wrote:
>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
>> ---
>> diff --git a/builtin/apply.c b/builtin/apply.c
>> @@ -4670,6 +4670,28 @@ static int option_parse_directory(const struct option *opt,
>> +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_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(&state->root, 0);
>> +
>> +       git_apply_config();
>> +       if (apply_default_whitespace)
>> +               parse_whitespace_option(state, apply_default_whitespace);
>> +       if (apply_default_ignorewhitespace)
>> +               parse_ignorewhitespace_option(state, apply_default_ignorewhitespace);
>> +}
>
> Minor:
>
> If factoring out this code from cmd_apply() into init_apply_state()
> was done as a preparatory patch before introduction of 'apply_state',
> then each new 'state->foo=...' line would already be at its final
> location when added by its respective patch.

Yeah I agree. So I did something like that.
And by the way while doing it I saw that Junio also suggested a similar change.

The little difference with what you and Junio suggest is that the
patch that creates init_apply_state() is just after the patch that
introduces 'struct apply_state'. I think it is a bit cleaner this way,
as if I create init_apply_state() first, then I would have to change
its arguments in the next patch that introduces 'struct apply_state'.

> Doing so would also provide an obvious opportunity to name the
> 'prefix' argument to init_apply_state() "prefix" rather than the odd
> "prefix_".

Yeah, I did that too.

Thanks,
Christian.

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 22/83] builtin/apply: move 'unsafe_paths' global into 'struct apply_state'
  2016-04-26 20:27   ` Junio C Hamano
@ 2016-04-27 12:14     ` Christian Couder
  2016-04-27 15:26       ` Junio C Hamano
  0 siblings, 1 reply; 156+ messages in thread
From: Christian Couder @ 2016-04-27 12:14 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Jeff King, Ævar Arnfjörð, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

On Tue, Apr 26, 2016 at 10:27 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Christian Couder <christian.couder@gmail.com> writes:
>
>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
>> ---
>>  builtin/apply.c | 9 +++++----
>>  1 file changed, 5 insertions(+), 4 deletions(-)
>>
>> diff --git a/builtin/apply.c b/builtin/apply.c
>> index 506357c..c45e481 100644
>> --- a/builtin/apply.c
>> +++ b/builtin/apply.c
>> @@ -57,6 +57,8 @@ struct apply_state {
>>       int unidiff_zero;
>>
>>       int update_index;
>> +
>> +     int unsafe_paths;
>>  };
>
> Having said
>
>     I like the way this series moves only a few variables at a time to
>     limit the scope of each step.
>
> it gets irritating to see all these unnecessary blank lines in the
> structure definition added by each step, which would mean all of
> these patches need to fix them in the next reroll.

The reason I added some blank lines is that I moved comments that were
all in one block at the top.
I moved each comment near the related variables and sometimes a
comment is related to 2 variables, like in the extract below the
comment that starts with "For "diff-stat" like behaviour,...":

------------------------------------------------------

    /* --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_xxxx"
     * things are flags, where -1 means "don't know yet".
     */
    int linenr;

------------------------------------------------------

If I remove all the blank lines, I think it will make it harder to
understand which comment belong to which variable(s).

Maybe a compromise would be to just remove blank lines between the
variables that don't have any related comment like "unidiff_zero",
"update_index", "unsafe_paths" and "line_termination" above.

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 27/83] builtin/apply: move 'read_stdin' global into cmd_apply()
  2016-04-26 20:28   ` Junio C Hamano
@ 2016-04-27 13:13     ` Christian Couder
  0 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-27 13:13 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Jeff King, Ævar Arnfjörð, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

On Tue, Apr 26, 2016 at 10:28 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Christian Couder <christian.couder@gmail.com> writes:
>
>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
>> ---
>>  builtin/apply.c | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> Makes sense but do so immediately next to 06/83, "options" thing?

Ok, in the next reroll this patch will be immediately next to 06/83.

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 39/83] builtin/apply: move 'ws_error_action' into 'struct apply_state'
  2016-04-26 20:36   ` Junio C Hamano
  2016-04-26 20:40     ` Junio C Hamano
@ 2016-04-27 13:23     ` Christian Couder
  1 sibling, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-27 13:23 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Jeff King, Ævar Arnfjörð, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

On Tue, Apr 26, 2016 at 10:36 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Christian Couder <christian.couder@gmail.com> writes:
>
>> +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;
>> @@ -80,6 +87,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;
>> @@ -89,12 +98,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;
>
> This is a good example of a variable that needs initialization,
> which is turned into a field in apply_state that needs
> initialization.  It is done here:
>
>> @@ -4738,6 +4743,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
>>       state.p_value = 1;
>>       state.p_context = UINT_MAX;
>>       state.squelch_whitespace_errors = 5;
>> +     state.ws_error_action = warn_on_ws_error;
>>       strbuf_init(&state.root, 0);
>
> and we already have these random initial values described here.
>
> As I do not expect that cmd_apply() which is a moral equivalent of
> main() will stay to be the only one who wants to see a reasonably
> initialized apply_state(), I think the patch that introduced the
> very first version of "struct apply_state" should also introduce a
> helper function to initialize it, i.e.
>
>         static void init_apply_state(struct apply_state *s,
>                                      const char *prefix)
>         {
>                 memset(s, '\0', sizeof(*s));
>                 s->prefix = prefix;
>                 s->prefix_length = s->prefix ? strlen(s->prefix) : 0;
>         }
>
> in [PATCH 7/xx].

Yeah, Eric also made nearly the same comment, so in the next reroll
the init_apply_state() is introduced in the patch immediately after
the patch that creates 'struct apply_state' (rather than much later in
the series). I prefer to not introduce it in the same patch as the
patch that creates 'struct apply_state' is already quite big. (But if
you insist, yeah I will squash both patches together.)

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 05/83] builtin/apply: extract line_by_line_fuzzy_match() from match_fragment()
  2016-04-26 20:20       ` Junio C Hamano
@ 2016-04-27 13:31         ` Christian Couder
  0 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-27 13:31 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: Stefan Beller, git@vger.kernel.org, Jeff King,
	Ævar Arnfjörð, Karsten Blees, Nguyen Thai Ngoc Duy,
	Johannes Schindelin, Matthieu Moy, Christian Couder

On Tue, Apr 26, 2016 at 10:20 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Christian Couder <christian.couder@gmail.com> writes:
>
>>> It's not clear why we need to declare buf here? Oh wait it is. It's just
>>> moved from the start of the function. But why do it in this patch?
>>> It seems unrelated to the general intent of the patch. No need to reroll
>>> for this nit alone, it just took me a while to figure out it was an unrelated
>>> thing.
>>
>> Yeah, I agree it's a bit unrelated. But rather than add another patch
>> to an already long series just for this,...
>
> Isn't not doing the unrelated move at all a more sensible
> alternative, if that change does not deserve its own place in the
> series after all?

Ok, I removed the unrelated move.

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 22/83] builtin/apply: move 'unsafe_paths' global into 'struct apply_state'
  2016-04-27 12:14     ` Christian Couder
@ 2016-04-27 15:26       ` Junio C Hamano
  0 siblings, 0 replies; 156+ messages in thread
From: Junio C Hamano @ 2016-04-27 15:26 UTC (permalink / raw)
  To: Christian Couder
  Cc: git, Jeff King, Ævar Arnfjörð, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

Christian Couder <christian.couder@gmail.com> writes:

> The reason I added some blank lines is that I moved comments that were
> all in one block at the top.
> I moved each comment near the related variables and sometimes a
> comment is related to 2 variables, like in the extract below the
> comment that starts with "For "diff-stat" like behaviour,...":

Don't.

If you have four things there, and two are related, you would do

    /* used for --distim option */
    int distim_weight;
    int distim_target;

    int unrelated;

    int another;

And the first two are shown clearly to be related and unrelated to
the other ones.  That sound nice in theory, but think what you would
do when you want to comment further on one of the related ones.  You
would likely do this:

    /* used for --distim option */
    int distim_weight;
   -int distim_target;
   +int distim_target; /* which dosh to distim? */

    int unrelated;

For the same reason, if you are to comment on one of the ungrouped
things, you would do this:


   -int unrelated;
   +int unrelated; /* this is not used for --distim */

    int another;

Notice that a blank between unrelated and another is not helping
anything?  There is no reason to keep blanks in between each and
every unrelated things.

See how "struct diff_options" is done for a better example.  A blank
line every once in a while that roups related things together is
good.  Blindly applying it to all groups of one defeats the benefit
by making the result harder to read.

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 47/83] builtin/apply: move applying patches into apply_all_patches()
  2016-04-25 22:00   ` Stefan Beller
@ 2016-04-27 15:51     ` Christian Couder
  0 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-27 15:51 UTC (permalink / raw)
  To: Stefan Beller
  Cc: git@vger.kernel.org, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Matthieu Moy,
	Christian Couder

On Tue, Apr 26, 2016 at 12:00 AM, Stefan Beller <sbeller@google.com> wrote:
> On Sun, Apr 24, 2016 at 6:33 AM, Christian Couder
> <christian.couder@gmail.com> wrote:
>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
>
> Up to this patch, have a
> Reviewed-by: Stefan Beller <sbeller@google.com>
> in case you want to split the series in here (as indicated in the
> cover letter, this was the last
> patch rerolled, the next patches are new and may need more discussion).
>
> I had some nits, but they cleared up in later patches.

Thanks for your review! I will add your Reviewed-by in the next reroll.

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 03/83] builtin/apply: avoid parameter shadowing 'linenr' global
  2016-04-24 13:33 ` [PATCH 03/83] builtin/apply: avoid parameter shadowing 'linenr' global Christian Couder
@ 2016-04-27 16:27   ` Junio C Hamano
  2016-04-27 17:07     ` Christian Couder
  0 siblings, 1 reply; 156+ messages in thread
From: Junio C Hamano @ 2016-04-27 16:27 UTC (permalink / raw)
  To: Christian Couder
  Cc: git, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

Christian Couder <christian.couder@gmail.com> writes:

> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---

I think 02/83 that renamed the global-to-be-moved-to-state to
state_p_value was brilliant, and this should follow suit; you would
be moving linenr into the state eventually in later steps, right?

>  builtin/apply.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/builtin/apply.c b/builtin/apply.c
> index e133b38..7115dc2 100644
> --- a/builtin/apply.c
> +++ b/builtin/apply.c
> @@ -1516,7 +1516,7 @@ static int find_header(const char *line, unsigned long size, int *hdrsize, struc
>  	return -1;
>  }
>  
> -static void record_ws_error(unsigned result, const char *line, int len, int linenr)
> +static void record_ws_error(unsigned result, const char *line, int len, int l_nr)
>  {
>  	char *err;
>  
> @@ -1530,7 +1530,7 @@ 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);
> +		patch_input_file, l_nr, err, len, line);
>  	free(err);
>  }

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 03/83] builtin/apply: avoid parameter shadowing 'linenr' global
  2016-04-27 16:27   ` Junio C Hamano
@ 2016-04-27 17:07     ` Christian Couder
  0 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-27 17:07 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Jeff King, Ævar Arnfjörð, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

On Wed, Apr 27, 2016 at 6:27 PM, Junio C Hamano <gitster@pobox.com> wrote:
>
> I think 02/83 that renamed the global-to-be-moved-to-state to
> state_p_value was brilliant, and this should follow suit; you would
> be moving linenr into the state eventually in later steps, right?

Yeah, ok, I will do the same thing to this patch as I did to 02/83.

Thanks.

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 53/83] builtin/apply: make find_header() return -1 instead of die()ing
  2016-04-24 13:33 ` [PATCH 53/83] builtin/apply: make find_header() " Christian Couder
  2016-04-25 13:18   ` Duy Nguyen
@ 2016-04-27 18:08   ` Eric Sunshine
  2016-05-01 16:45     ` Christian Couder
  1 sibling, 1 reply; 156+ messages in thread
From: Eric Sunshine @ 2016-04-27 18:08 UTC (permalink / raw)
  To: Christian Couder
  Cc: Git List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

On Sun, Apr 24, 2016 at 9:33 AM, Christian Couder
<christian.couder@gmail.com> wrote:
> To be compatible with the rest of the error handling in builtin/apply.c,
> find_header() should return -1 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 <chriscool@tuxfamily.org>
> ---
> diff --git a/builtin/apply.c b/builtin/apply.c
> @@ -1588,18 +1596,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);

I realize that the caller in this patch currently just die()'s, and I
haven't looked at subsequent patches yet, but is this new 'return'
going to cause the caller to start leaking patch->old_name and
patch->new_name which are xstrdup()'d just above?

>                         patch->is_toplevel_relative = 1;
>                         *hdrsize = git_hdr_len;
>                         return offset;
> @@ -2115,6 +2123,9 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
>         int hdrsize, patchsize;
>         int offset = find_header(state, buffer, size, &hdrsize, patch);
>
> +       if (offset == -1)
> +               exit(1);
> +
>         if (offset < 0)
>                 return offset;

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 53/83] builtin/apply: make find_header() return -1 instead of die()ing
  2016-04-25 13:18   ` Duy Nguyen
@ 2016-04-27 18:10     ` Eric Sunshine
  2016-05-01 16:53       ` Christian Couder
  0 siblings, 1 reply; 156+ messages in thread
From: Eric Sunshine @ 2016-04-27 18:10 UTC (permalink / raw)
  To: Duy Nguyen
  Cc: Christian Couder, Git Mailing List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Johannes Schindelin, Stefan Beller, Matthieu Moy,
	Christian Couder

On Mon, Apr 25, 2016 at 9:18 AM, Duy Nguyen <pclouds@gmail.com> wrote:
> On Sun, Apr 24, 2016 at 8:33 PM, Christian Couder
> <christian.couder@gmail.com> wrote:
>> To be compatible with the rest of the error handling in builtin/apply.c,
>> find_header() should return -1 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.
>
> I don't think this is a good way to go. Too many magic numbers. I
> don't have a better option though. Maybe returning names instead of
> numbers would help a bit.

I suppose 'hdrsize' could signal this extra condition. For instance,
always return -1 on error, and set hdrsize to 0 for header not found
(unless 0 is a valid size?), and -1 for any other error.

But perhaps that's getting too clever...

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 49/83] builtin/apply: move 'lock_file' global into 'struct apply_state'
  2016-04-25 17:55     ` Junio C Hamano
@ 2016-04-28 16:30       ` Christian Couder
  2016-04-28 20:17         ` Junio C Hamano
  0 siblings, 1 reply; 156+ messages in thread
From: Christian Couder @ 2016-04-28 16:30 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: Eric Sunshine, Git List, Jeff King, Ævar Arnfjörð,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

On Mon, Apr 25, 2016 at 7:55 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Eric Sunshine <sunshine@sunshineco.com> writes:
>
>>> +       /*
>>> +        * 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;
>>
>> Is there ever a time when lock_file is removed from the list (such as
>> upon successful write of the index), in which case it would be safe to
>> free() this?
>
> I do not think you need to think about "free"ing.

Yeah, lockfile.h says:

 * * Automatic cruft removal. If the program exits after we lock a
 *   file but before the changes have been committed, we want to make
 *   sure that we remove the lockfile. This is done by remembering the
 *   lockfiles we have created in a linked list and setting up an
 *   `atexit(3)` handler and a signal handler that clean up the
 *   lockfiles. This mechanism ensures that outstanding lockfiles are
 *   cleaned up if the program exits (including when `die()` is
 *   called) or if the program is terminated by a signal.

and:

 * The caller:
 *
 * * Allocates a `struct lock_file` either as a static variable or on
 *   the heap, initialized to zeros. Once you use the structure to
 *   call the `hold_lock_file_for_*()` family of functions, it belongs
 *   to the lockfile subsystem and its storage must remain valid
 *   throughout the life of the program (i.e. you cannot use an
 *   on-stack variable to hold this structure).

> Even if the libified version of the apply internal can be called
> multiple times to process multiple patch inputs, there is no need to
> run multiple instances of it yet.  And a lockfile, after the caller
> finishes interacting with one file using it by calling commit or
> rollback, can be reused to interact with another file.
>
> So the cleanest would be to:
>
>  * make the caller of apply API responsible for preparing a static
>    or (allocating and leaking) lockfile instance,

Ok.

>  * make it pass a pointer to the lockfile to the apply API so that
>    it can store it in apply_state, and

Ok, I will add a new "struct lock_file *" parameter to
init_apply_state(), for the caller of the apply API to do that.
Though I think it should be ok for the caller to pass a NULL pointer
and in this case have init_apply_state() allocate the lockfile file
instance.

>  * have the caller use apply API feeding one patch at a time in a
>    loop, allowing the same lockfile instance used for each
>    "invocation".

Ok, the same lockfile instance will be used for each invocation.

> I sounded as if I were advocating non-reentrant implementation in
> the introductory paragraph, but that is not the intention.  If you
> want to do N threads, you would prepare N lockfile instances, give
> them to N apply API instances to be stored in N apply_state
> instances, and you would have N parallel applications, if you wanted
> to.

Thanks,
Christian.

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 49/83] builtin/apply: move 'lock_file' global into 'struct apply_state'
  2016-04-28 16:30       ` Christian Couder
@ 2016-04-28 20:17         ` Junio C Hamano
  0 siblings, 0 replies; 156+ messages in thread
From: Junio C Hamano @ 2016-04-28 20:17 UTC (permalink / raw)
  To: Christian Couder
  Cc: Eric Sunshine, Git List, Jeff King, Ævar Arnfjörð,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

Christian Couder <christian.couder@gmail.com> writes:

>> I do not think you need to think about "free"ing.
>
> Yeah, lockfile.h says:
> ...
> and:
> ...

Yup, we are now on the same page.

>> Even if the libified version of the apply internal can be called
>> multiple times to process multiple patch inputs, there is no need to
>> run multiple instances of it yet.  And a lockfile, after the caller
>> finishes interacting with one file using it by calling commit or
>> rollback, can be reused to interact with another file.

lockfile.h says this about the above paragraph, which is a more
important part ;-)

 * When finished writing, the caller can:
 * ...
 * Even after the lockfile is committed or rolled back, the
 * `lock_file` object must not be freed or altered by the caller.
 * However, it may be reused; just pass it to another call of
 * `hold_lock_file_for_update()`.

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 49/83] builtin/apply: move 'lock_file' global into 'struct apply_state'
  2016-04-25  7:50   ` Eric Sunshine
  2016-04-25 17:55     ` Junio C Hamano
@ 2016-04-30 19:39     ` Christian Couder
  1 sibling, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-30 19:39 UTC (permalink / raw)
  To: Eric Sunshine
  Cc: Git List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

On Mon, Apr 25, 2016 at 9:50 AM, Eric Sunshine <sunshine@sunshineco.com> wrote:
>> @@ -4515,8 +4521,6 @@ static int write_out_results(struct apply_state *state, struct patch *list)
>>         return errs;
>>  }
>>
>> -static struct lock_file lock_file;
>
> Does the static lock_file in build_fake_ancestor() deserve the same
> sort of treatment? (I haven't traced the code enough to answer this.)

Maybe yes we could do the same thing for this static lock_file, but
this can be done later, and it could be a bit involved, so I prefer to
not touch that for now.

We are using the lock_file like this in build_fake_ancestor():

    hold_lock_file_for_update(&lock, filename, LOCK_DIE_ON_ERROR);
    if (write_locked_index(&result, &lock, COMMIT_LOCK))
        return error("Could not write temporary index to %s", filename);

so it looks like it is safe to call build_fake_ancestor() many times
as long as it is not called by different threads.

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 51/83] builtin/apply: make apply_patch() return -1 instead of die()ing
  2016-04-26  1:20   ` Eric Sunshine
@ 2016-04-30 19:41     ` Christian Couder
  0 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-04-30 19:41 UTC (permalink / raw)
  To: Eric Sunshine
  Cc: Git List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

On Tue, Apr 26, 2016 at 3:20 AM, Eric Sunshine <sunshine@sunshineco.com> wrote:
> On Sun, Apr 24, 2016 at 9:33 AM, Christian Couder
> <christian.couder@gmail.com> wrote:
>> 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.
>>
>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
>> ---
>> diff --git a/builtin/apply.c b/builtin/apply.c
>> @@ -4522,6 +4522,14 @@ static int write_out_results(struct apply_state *state, struct patch *list)
>>  static int apply_patch(struct apply_state *state,
>>                        int fd,
>>                        const char *filename,
>> @@ -4564,7 +4572,7 @@ static int apply_patch(struct apply_state *state,
>>         }
>>
>>         if (!list && !skipped_patch)
>> -               die(_("unrecognized input"));
>> +               return error(_("unrecognized input"));
>>
>>         if (state->whitespace_error && (state->ws_error_action == die_on_ws_error))
>>                 state->apply = 0;
>> @@ -4575,19 +4583,17 @@ static int apply_patch(struct apply_state *state,
>>                 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)
>> +               return error(_("unable to read index file"));
>>
>>         if ((state->check || state->apply) &&
>>             check_patch_list(state, list) < 0 &&
>>             !state->apply_with_reject)
>> -               exit(1);
>> +               return -1;
>>
>>         if (state->apply && write_out_results(state, list)) {
>>                 if (state->apply_with_reject)
>> -                       exit(1);
>> +                       return -1;
>>                 /* with --3way, we still need to write the index out */
>>                 return 1;
>>         }
>
> Are these new 'returns' leaking 'list', 'buf', and 'fn_table' which
> otherwise get released at the end of the function?

Yeah, you are right, I will fix that. Thanks.

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 53/83] builtin/apply: make find_header() return -1 instead of die()ing
  2016-04-27 18:08   ` Eric Sunshine
@ 2016-05-01 16:45     ` Christian Couder
  0 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-05-01 16:45 UTC (permalink / raw)
  To: Eric Sunshine
  Cc: Git List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

On Wed, Apr 27, 2016 at 8:08 PM, Eric Sunshine <sunshine@sunshineco.com> wrote:
> On Sun, Apr 24, 2016 at 9:33 AM, Christian Couder
> <christian.couder@gmail.com> wrote:
>> To be compatible with the rest of the error handling in builtin/apply.c,
>> find_header() should return -1 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 <chriscool@tuxfamily.org>
>> ---
>> diff --git a/builtin/apply.c b/builtin/apply.c
>> @@ -1588,18 +1596,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);
>
> I realize that the caller in this patch currently just die()'s, and I
> haven't looked at subsequent patches yet, but is this new 'return'
> going to cause the caller to start leaking patch->old_name and
> patch->new_name which are xstrdup()'d just above?

I think it is ok because find_header() is called only from
parse_chunk() which is only called by apply_patch() and apply_patch()
calls "free_patch(patch)" when parse_chunk() returns a negative
integer.
And free_patch() frees old_name and new_name.

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 53/83] builtin/apply: make find_header() return -1 instead of die()ing
  2016-04-27 18:10     ` Eric Sunshine
@ 2016-05-01 16:53       ` Christian Couder
  0 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-05-01 16:53 UTC (permalink / raw)
  To: Eric Sunshine
  Cc: Duy Nguyen, Git Mailing List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Johannes Schindelin, Stefan Beller, Matthieu Moy,
	Christian Couder

On Wed, Apr 27, 2016 at 8:10 PM, Eric Sunshine <sunshine@sunshineco.com> wrote:
> On Mon, Apr 25, 2016 at 9:18 AM, Duy Nguyen <pclouds@gmail.com> wrote:
>> On Sun, Apr 24, 2016 at 8:33 PM, Christian Couder
>> <christian.couder@gmail.com> wrote:
>>> To be compatible with the rest of the error handling in builtin/apply.c,
>>> find_header() should return -1 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.
>>
>> I don't think this is a good way to go. Too many magic numbers. I
>> don't have a better option though. Maybe returning names instead of
>> numbers would help a bit.
>
> I suppose 'hdrsize' could signal this extra condition. For instance,
> always return -1 on error, and set hdrsize to 0 for header not found
> (unless 0 is a valid size?), and -1 for any other error.
>
> But perhaps that's getting too clever...

Yeah, I don't think it would make reading the patch simpler.

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 61/83] builtin/apply: libify check_apply_state()
  2016-04-25 13:26   ` Duy Nguyen
@ 2016-05-01 16:58     ` Christian Couder
  0 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-05-01 16:58 UTC (permalink / raw)
  To: Duy Nguyen
  Cc: Git Mailing List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Johannes Schindelin, Stefan Beller, Matthieu Moy,
	Christian Couder

On Mon, Apr 25, 2016 at 3:26 PM, Duy Nguyen <pclouds@gmail.com> wrote:
> On Sun, Apr 24, 2016 at 8:34 PM, Christian Couder
> <christian.couder@gmail.com> wrote:
>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
>> ---
>>  builtin/apply.c | 16 +++++++++-------
>>  1 file changed, 9 insertions(+), 7 deletions(-)
>>
>> diff --git a/builtin/apply.c b/builtin/apply.c
>> index e3ee199..7576ec5 100644
>> --- a/builtin/apply.c
>> +++ b/builtin/apply.c
>> @@ -4534,17 +4534,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.");
>
> Sweet opportunity to _() these messages since it clear shows in this
> patch that other messages of the same category are _()'d in this
> function. Could be done as a separate patch. But I'm also ok if you
> say "no".

I have added it to my todo list of things that should be done after this series.

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 63/83] builtin/apply: make apply_all_patches() return -1 on error
  2016-04-25 13:30   ` Duy Nguyen
@ 2016-05-01 18:32     ` Christian Couder
  0 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-05-01 18:32 UTC (permalink / raw)
  To: Duy Nguyen
  Cc: Git Mailing List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Johannes Schindelin, Stefan Beller, Matthieu Moy,
	Christian Couder

On Mon, Apr 25, 2016 at 3:30 PM, Duy Nguyen <pclouds@gmail.com> wrote:
> On Sun, Apr 24, 2016 at 8:34 PM, Christian Couder
> <christian.couder@gmail.com> wrote:
>>         if (state->update_index) {
>>                 if (write_locked_index(&the_index, state->lock_file, COMMIT_LOCK))
>> -                       die(_("Unable to write new index file"));
>> +                       return error(_("Unable to write new index file"));
>>         }
>>
>>         return !!errs;
>> @@ -4698,5 +4698,8 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
>>         if (check_apply_state(&state, force_apply))
>>                 exit(1);
>>
>> -       return apply_all_patches(&state, argc, argv, options);
>> +       if (apply_all_patches(&state, argc, argv, options))
>> +               exit(1);
>
> Abusing exit() too much? This is cmd_apply(). A return should be
> enough and you can do the !! trick that is used in
> apply_all_patches(), shown just a little bit above.

Ok, I will use:

    return !!apply_all_patches(&state, argc, argv, options);

Thanks,
Christian.

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 54/83] builtin/apply: make parse_chunk() return a negative integer on error
  2016-04-24 13:33 ` [PATCH 54/83] builtin/apply: make parse_chunk() return a negative integer on error Christian Couder
@ 2016-05-01 19:04   ` Eric Sunshine
  2016-05-01 19:51     ` Christian Couder
  0 siblings, 1 reply; 156+ messages in thread
From: Eric Sunshine @ 2016-05-01 19:04 UTC (permalink / raw)
  To: Christian Couder
  Cc: Git List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

On Sun, Apr 24, 2016 at 9:33 AM, Christian Couder
<christian.couder@gmail.com> wrote:
> This negative number can be -2 if no patch header has been found,
> otherwise it is -1.
>
> As parse_chunk() is called only by apply_patch() which already
> returns -1 when an error happened, let's make it return -1 when
> parse_chunk() returns -1.
>
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---
> diff --git a/builtin/apply.c b/builtin/apply.c
> @@ -4566,6 +4567,8 @@ 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)
> +                               return -1;

Same comment as 51/83 about this leaking 'list', 'buf', and 'fn_table'.

>                         break;
>                 }
>                 if (state->apply_in_reverse)

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 65/83] builtin/apply: make gitdiff_verify_name() return -1 on error
  2016-04-25 13:36   ` Duy Nguyen
@ 2016-05-01 19:31     ` Christian Couder
  0 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-05-01 19:31 UTC (permalink / raw)
  To: Duy Nguyen
  Cc: Git Mailing List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Johannes Schindelin, Stefan Beller, Matthieu Moy,
	Christian Couder

On Mon, Apr 25, 2016 at 3:36 PM, Duy Nguyen <pclouds@gmail.com> wrote:
> On Sun, Apr 24, 2016 at 8:34 PM, Christian Couder
> <christian.couder@gmail.com> wrote:
>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
>> ---
>>  builtin/apply.c | 52 ++++++++++++++++++++++++++++++----------------------
>>  1 file changed, 30 insertions(+), 22 deletions(-)
>>
>> diff --git a/builtin/apply.c b/builtin/apply.c
>> index 6b8ba2a..268356b 100644
>> --- a/builtin/apply.c
>> +++ b/builtin/apply.c
>> @@ -812,7 +812,7 @@ static int gitdiff_hdrend(struct apply_state *state,
>>                           const char *line,
>>                           struct patch *patch)
>>  {
>> -       return -1;
>> +       return 1;
>>  }
>
> These are in a function group that will be called as p->fn() in
> parse_git_header(). Is it possible to isolate them in a separate
> patch? This patch can follow after, which covers only
> gitdiff_verify_name() returning -1 and its call sites.

Yeah, I had planned to do something like that when developing the
series but I forgot.

Thanks,
Christian.

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 55/83] builtin/apply: make parse_single_patch() return -1 on error
  2016-04-24 13:33 ` [PATCH 55/83] builtin/apply: make parse_single_patch() return -1 " Christian Couder
@ 2016-05-01 19:32   ` Eric Sunshine
  0 siblings, 0 replies; 156+ messages in thread
From: Eric Sunshine @ 2016-05-01 19:32 UTC (permalink / raw)
  To: Christian Couder
  Cc: Git List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

On Sun, Apr 24, 2016 at 9:33 AM, Christian Couder
<christian.couder@gmail.com> wrote:
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---
> diff --git a/builtin/apply.c b/builtin/apply.c
> @@ -1802,8 +1806,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);

No fragment leak here, okay.

> +               }
>                 fragment->patch = line;
>                 fragment->size = len;
>                 oldlines += fragment->oldlines;
> @@ -1839,9 +1845,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);

These two return -1 and don't manually release the fragments, which
might seem like a leak, however...

>         if (!patch->is_delete && !newlines && context)
>                 fprintf_ln(stderr,
>                            _("** warning: "
> @@ -2143,6 +2149,9 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
>                                        size - offset - hdrsize,
>                                        patch);
>
> +       if (patchsize < 0)
> +               return -1;

This forwards -1 to its caller, and the caller, apply_patch() does
release the fragments when it sees -1, so there does not seem to be
any (new) leak. Good.

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 59/83] builtin/apply: move init_apply_state() to apply.c
  2016-04-24 13:33 ` [PATCH 59/83] builtin/apply: move init_apply_state() to apply.c Christian Couder
@ 2016-05-01 19:37   ` Eric Sunshine
  2016-05-01 20:19     ` Christian Couder
  0 siblings, 1 reply; 156+ messages in thread
From: Eric Sunshine @ 2016-05-01 19:37 UTC (permalink / raw)
  To: Christian Couder
  Cc: Git List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

On Sun, Apr 24, 2016 at 9:33 AM, Christian Couder
<christian.couder@gmail.com> wrote:
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---
> diff --git a/apply.c b/apply.c
> @@ -0,0 +1,80 @@
> +#include "cache.h"
> +#include "apply.h"
> +
> +
> +

Too many blank lines?

> +static void git_apply_config(void)
> +{
> +       git_config_get_string_const("apply.whitespace", &apply_default_whitespace);
> +       git_config_get_string_const("apply.ignorewhitespace", &apply_default_ignorewhitespace);
> +       git_config(git_default_config, NULL);
> +}

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 54/83] builtin/apply: make parse_chunk() return a negative integer on error
  2016-05-01 19:04   ` Eric Sunshine
@ 2016-05-01 19:51     ` Christian Couder
  0 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-05-01 19:51 UTC (permalink / raw)
  To: Eric Sunshine
  Cc: Git List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

On Sun, May 1, 2016 at 9:04 PM, Eric Sunshine <sunshine@sunshineco.com> wrote:
> On Sun, Apr 24, 2016 at 9:33 AM, Christian Couder
> <christian.couder@gmail.com> wrote:
>> This negative number can be -2 if no patch header has been found,
>> otherwise it is -1.
>>
>> As parse_chunk() is called only by apply_patch() which already
>> returns -1 when an error happened, let's make it return -1 when
>> parse_chunk() returns -1.
>>
>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
>> ---
>> diff --git a/builtin/apply.c b/builtin/apply.c
>> @@ -4566,6 +4567,8 @@ 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)
>> +                               return -1;
>
> Same comment as 51/83 about this leaking 'list', 'buf', and 'fn_table'.

Ok, thanks!

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 59/83] builtin/apply: move init_apply_state() to apply.c
  2016-05-01 19:37   ` Eric Sunshine
@ 2016-05-01 20:19     ` Christian Couder
  0 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-05-01 20:19 UTC (permalink / raw)
  To: Eric Sunshine
  Cc: Git List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

On Sun, May 1, 2016 at 9:37 PM, Eric Sunshine <sunshine@sunshineco.com> wrote:
> On Sun, Apr 24, 2016 at 9:33 AM, Christian Couder
> <christian.couder@gmail.com> wrote:
>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
>> ---
>> diff --git a/apply.c b/apply.c
>> @@ -0,0 +1,80 @@
>> +#include "cache.h"
>> +#include "apply.h"
>> +
>> +
>> +
>
> Too many blank lines?

Ok, I will remove 2 of them.

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 63/83] builtin/apply: make apply_all_patches() return -1 on error
  2016-04-24 13:34 ` [PATCH 63/83] builtin/apply: make apply_all_patches() return -1 on error Christian Couder
  2016-04-25 13:30   ` Duy Nguyen
@ 2016-05-01 21:03   ` Eric Sunshine
  2016-05-02  7:09     ` Johannes Schindelin
  2016-05-03 13:01     ` Christian Couder
  1 sibling, 2 replies; 156+ messages in thread
From: Eric Sunshine @ 2016-05-01 21:03 UTC (permalink / raw)
  To: Christian Couder
  Cc: Git List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

On Sun, Apr 24, 2016 at 9:34 AM, Christian Couder
<christian.couder@gmail.com> wrote:
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---
> diff --git a/builtin/apply.c b/builtin/apply.c
> @@ -4562,12 +4562,12 @@ 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);
>                 if (res < 0)
> -                       exit(1);
> +                       return -1;

This leaks 'fd', doesn't it?

>                 errs |= res;
>                 close(fd);
>         }
> @@ -4590,10 +4590,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);

How does this new 'return' relate to the logic below which updates the
index? Does the index need to be updated here now too?

>                 if (state->applied_after_fixing_ws && state->apply)
>                         warning("%d line%s applied after"
>                                 " fixing whitespace errors.",
> @@ -4608,7 +4608,7 @@ static int apply_all_patches(struct apply_state *state,
>
>         if (state->update_index) {
>                 if (write_locked_index(&the_index, state->lock_file, COMMIT_LOCK))
> -                       die(_("Unable to write new index file"));
> +                       return error(_("Unable to write new index file"));
>         }
>
>         return !!errs;
> @@ -4698,5 +4698,8 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
>         if (check_apply_state(&state, force_apply))
>                 exit(1);
>
> -       return apply_all_patches(&state, argc, argv, options);
> +       if (apply_all_patches(&state, argc, argv, options))
> +               exit(1);
> +
> +       return 0;
>  }

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 63/83] builtin/apply: make apply_all_patches() return -1 on error
  2016-05-01 21:03   ` Eric Sunshine
@ 2016-05-02  7:09     ` Johannes Schindelin
  2016-05-03 13:01     ` Christian Couder
  1 sibling, 0 replies; 156+ messages in thread
From: Johannes Schindelin @ 2016-05-02  7:09 UTC (permalink / raw)
  To: Eric Sunshine
  Cc: Christian Couder, Git List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Stefan Beller, Matthieu Moy,
	Christian Couder

Hi Eric,

On Sun, 1 May 2016, Eric Sunshine wrote:

> On Sun, Apr 24, 2016 at 9:34 AM, Christian Couder
> <christian.couder@gmail.com> wrote:
> > Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> > ---
> > diff --git a/builtin/apply.c b/builtin/apply.c
> > @@ -4562,12 +4562,12 @@ 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);
> >                 if (res < 0)
> > -                       exit(1);
> > +                       return -1;
> 
> This leaks 'fd', doesn't it?

Indeed it does. I thought that I had sent a mail about fd leakage, just
resent it. Such a leakage prevented the test suite from passing after
applying this patch series on Windows and patching up several obvious
compile problems.

Ciao,
Dscho

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 67/83] builtin/apply: make build_fake_ancestor() return -1 on error
  2016-04-24 13:34 ` [PATCH 67/83] builtin/apply: make build_fake_ancestor() return -1 on error Christian Couder
@ 2016-05-02  7:32   ` Eric Sunshine
  2016-05-03 13:52     ` Christian Couder
  0 siblings, 1 reply; 156+ messages in thread
From: Eric Sunshine @ 2016-05-02  7:32 UTC (permalink / raw)
  To: Christian Couder
  Cc: Git List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

On Sun, Apr 24, 2016 at 9:34 AM, Christian Couder
<christian.couder@gmail.com> wrote:
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---
> diff --git a/builtin/apply.c b/builtin/apply.c
> @@ -3913,31 +3913,34 @@ static void build_fake_ancestor(struct patch *list, const char *filename)
>                 ce = make_cache_entry(patch->old_mode, sha1, name, 0, 0);
>                 if (!ce)
> -                       die(_("make_cache_entry failed for path '%s'"), name);
> +                       return error(_("make_cache_entry failed for path '%s'"),
> +                                    name);
>                 if (add_index_entry(&result, ce, ADD_CACHE_OK_TO_ADD))
> -                       die ("Could not add %s to temporary index", name);
> +                       return error("Could not add %s to temporary index",
> +                                    name);

What happens to 'ce' here? Does it leak?

>         }
>
>         hold_lock_file_for_update(&lock, filename, LOCK_DIE_ON_ERROR);
>         if (write_locked_index(&result, &lock, COMMIT_LOCK))
> -               die ("Could not write temporary index to %s", filename);
> +               return error("Could not write temporary index to %s", filename);

What happens with 'result'? Does it need to be discarded?

>         discard_index(&result);
> +       return 0;
>  }
>
>  static void stat_patch_list(struct apply_state *state, struct patch *patch)
> @@ -4470,8 +4473,9 @@ static int apply_patch(struct apply_state *state,
>                 return 1;
>         }
>
> -       if (state->fake_ancestor)
> -               build_fake_ancestor(list, state->fake_ancestor);
> +       if (state->fake_ancestor &&
> +           build_fake_ancestor(list, state->fake_ancestor))
> +               return -1;

Same comment as earlier patches: Leaking 'list', 'buf', 'fn_table'.

>         if (state->diffstat)
>                 stat_patch_list(state, list);

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 69/83] builtin/apply: make add_conflicted_stages_file() return -1 on error
  2016-04-24 13:34 ` [PATCH 69/83] builtin/apply: make add_conflicted_stages_file() " Christian Couder
@ 2016-05-02  7:36   ` Eric Sunshine
  2016-05-03 14:33     ` Christian Couder
  0 siblings, 1 reply; 156+ messages in thread
From: Eric Sunshine @ 2016-05-02  7:36 UTC (permalink / raw)
  To: Christian Couder
  Cc: Git List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

On Sun, Apr 24, 2016 at 9:34 AM, Christian Couder
<christian.couder@gmail.com> wrote:
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---
> diff --git a/builtin/apply.c b/builtin/apply.c
> @@ -4234,8 +4234,11 @@ static void add_conflicted_stages_file(struct apply_state *state,
>                 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);
> +                       return error(_("unable to add cache entry for %s"),
> +                                    patch->new_name);

Is this leaking 'ce' (which is allocated a few lines above the shown context)?

>         }
> +
> +       return 0;
>  }

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 73/83] builtin/apply: make write_out_results() return -1 on error
  2016-04-24 13:34 ` [PATCH 73/83] builtin/apply: make write_out_results() " Christian Couder
@ 2016-05-02 17:42   ` Eric Sunshine
  2016-05-03 14:55     ` Christian Couder
  0 siblings, 1 reply; 156+ messages in thread
From: Eric Sunshine @ 2016-05-02 17:42 UTC (permalink / raw)
  To: Christian Couder
  Cc: Git List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

On Sun, Apr 24, 2016 at 9:34 AM, Christian Couder
<christian.couder@gmail.com> wrote:
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---
> diff --git a/builtin/apply.c b/builtin/apply.c
> @@ -4381,7 +4387,7 @@ static int write_out_results(struct apply_state *state, struct patch *list)
>                                 errs = 1;
>                         else {
>                                 if (write_out_one_result(state, l, phase))
> -                                       exit(1);
> +                                       return -1;

Isn't this leaking 'string_list cpath'?

>                                 if (phase == 1) {
>                                         if (write_out_one_reject(state, l))
>                                                 errs = 1;
> @@ -4484,11 +4490,16 @@ static int apply_patch(struct apply_state *state,
>             !state->apply_with_reject)
>                 return -1;
>
> -       if (state->apply && write_out_results(state, list)) {
> -               if (state->apply_with_reject)
> +       if (state->apply) {
> +               int res = write_out_results(state, list);
> +               if (res < 0)
>                         return -1;

Mentioned previously: Leaking 'list', 'buf', 'fn_table'.

> -               /* with --3way, we still need to write the index out */
> -               return 1;
> +               if (res > 0) {
> +                       if (state->apply_with_reject)
> +                               return -1;
> +                       /* with --3way, we still need to write the index out */
> +                       return 1;

Not the fault of this patch, but this 'return' in the original code
was also leaking 'list', 'buf', 'fn_table', right?

> +               }
>         }
>
>         if (state->fake_ancestor &&

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 74/83] builtin/apply: make try_create_file() return -1 on error
  2016-04-24 13:34 ` [PATCH 74/83] builtin/apply: make try_create_file() " Christian Couder
@ 2016-05-02 18:01   ` Eric Sunshine
  2016-05-03 16:24     ` Christian Couder
  0 siblings, 1 reply; 156+ messages in thread
From: Eric Sunshine @ 2016-05-02 18:01 UTC (permalink / raw)
  To: Christian Couder
  Cc: Git List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

On Sun, Apr 24, 2016 at 9:34 AM, Christian Couder
<christian.couder@gmail.com> wrote:
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---
> diff --git a/builtin/apply.c b/builtin/apply.c
> @@ -4145,28 +4151,32 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,
>         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, &nbuf)) {
>                 size = nbuf.len;
>                 buf  = nbuf.buf;
>         }
> -       write_or_die(fd, buf, size);
> +
> +       if (!write_or_whine_pipe(fd, buf, size, path)) {
> +               strbuf_release(&nbuf);
> +               return -1;

This is leaking 'fd'.

> +       }
>         strbuf_release(&nbuf);
>
>         if (close(fd) < 0)
> -               die_errno(_("closing file '%s'"), path);
> +               return error(_("closing file '%s': %s"), path, strerror(errno));
>         return 0;
>  }
>
> @@ -4208,12 +4227,15 @@ 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) {
>                                 if (!rename(newpath, path))
>                                         return;
>                                 unlink_or_warn(newpath);
>                                 break;
>                         }
> +                       if (res < 0)
> +                               exit(1);

Two issues:

Getting the error case out of the way early (moving this 'if' just
after 'res=...') would make it easier to reason about the remaining
logic.

It's already difficult to understand what the below 'errno' check is
testing. try_create_file(), rename(), or unlink_or_warn()? Plopping
this new error handling conditional in front of it divorces the
'errno' check even further from whatever it is testing.

>                         if (errno != EEXIST)
>                                 break;
>                         ++nr;

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 63/83] builtin/apply: make apply_all_patches() return -1 on error
  2016-05-01 21:03   ` Eric Sunshine
  2016-05-02  7:09     ` Johannes Schindelin
@ 2016-05-03 13:01     ` Christian Couder
  1 sibling, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-05-03 13:01 UTC (permalink / raw)
  To: Eric Sunshine
  Cc: Git List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

On Sun, May 1, 2016 at 11:03 PM, Eric Sunshine <sunshine@sunshineco.com> wrote:
>> @@ -4590,10 +4590,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);
>
> How does this new 'return' relate to the logic below which updates the
> index? Does the index need to be updated here now too?

I think it is the right thing to do not to update the index when there
are whitespaces and state->ws_error_action == die_on_ws_error.

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 67/83] builtin/apply: make build_fake_ancestor() return -1 on error
  2016-05-02  7:32   ` Eric Sunshine
@ 2016-05-03 13:52     ` Christian Couder
  0 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-05-03 13:52 UTC (permalink / raw)
  To: Eric Sunshine
  Cc: Git List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

On Mon, May 2, 2016 at 9:32 AM, Eric Sunshine <sunshine@sunshineco.com> wrote:
> On Sun, Apr 24, 2016 at 9:34 AM, Christian Couder
> <christian.couder@gmail.com> wrote:
>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
>> ---
>> diff --git a/builtin/apply.c b/builtin/apply.c
>> @@ -3913,31 +3913,34 @@ static void build_fake_ancestor(struct patch *list, const char *filename)
>>                 ce = make_cache_entry(patch->old_mode, sha1, name, 0, 0);
>>                 if (!ce)
>> -                       die(_("make_cache_entry failed for path '%s'"), name);
>> +                       return error(_("make_cache_entry failed for path '%s'"),
>> +                                    name);
>>                 if (add_index_entry(&result, ce, ADD_CACHE_OK_TO_ADD))
>> -                       die ("Could not add %s to temporary index", name);
>> +                       return error("Could not add %s to temporary index",
>> +                                    name);
>
> What happens to 'ce' here? Does it leak?

Yeah, I think it leaks.

>>         }
>>
>>         hold_lock_file_for_update(&lock, filename, LOCK_DIE_ON_ERROR);
>>         if (write_locked_index(&result, &lock, COMMIT_LOCK))
>> -               die ("Could not write temporary index to %s", filename);
>> +               return error("Could not write temporary index to %s", filename);
>
> What happens with 'result'? Does it need to be discarded?

Yeah, I think so.

>>         discard_index(&result);
>> +       return 0;
>>  }
>>
>>  static void stat_patch_list(struct apply_state *state, struct patch *patch)
>> @@ -4470,8 +4473,9 @@ static int apply_patch(struct apply_state *state,
>>                 return 1;
>>         }
>>
>> -       if (state->fake_ancestor)
>> -               build_fake_ancestor(list, state->fake_ancestor);
>> +       if (state->fake_ancestor &&
>> +           build_fake_ancestor(list, state->fake_ancestor))
>> +               return -1;
>
> Same comment as earlier patches: Leaking 'list', 'buf', 'fn_table'.

Ok, I will take care of these.

Thanks,
Christian.

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 69/83] builtin/apply: make add_conflicted_stages_file() return -1 on error
  2016-05-02  7:36   ` Eric Sunshine
@ 2016-05-03 14:33     ` Christian Couder
  0 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-05-03 14:33 UTC (permalink / raw)
  To: Eric Sunshine
  Cc: Git List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

On Mon, May 2, 2016 at 9:36 AM, Eric Sunshine <sunshine@sunshineco.com> wrote:
> On Sun, Apr 24, 2016 at 9:34 AM, Christian Couder
> <christian.couder@gmail.com> wrote:
>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
>> ---
>> diff --git a/builtin/apply.c b/builtin/apply.c
>> @@ -4234,8 +4234,11 @@ static void add_conflicted_stages_file(struct apply_state *state,
>>                 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);
>> +                       return error(_("unable to add cache entry for %s"),
>> +                                    patch->new_name);
>
> Is this leaking 'ce' (which is allocated a few lines above the shown context)?

Yes, thanks.

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 73/83] builtin/apply: make write_out_results() return -1 on error
  2016-05-02 17:42   ` Eric Sunshine
@ 2016-05-03 14:55     ` Christian Couder
  0 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-05-03 14:55 UTC (permalink / raw)
  To: Eric Sunshine
  Cc: Git List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

On Mon, May 2, 2016 at 7:42 PM, Eric Sunshine <sunshine@sunshineco.com> wrote:
> On Sun, Apr 24, 2016 at 9:34 AM, Christian Couder
> <christian.couder@gmail.com> wrote:
>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
>> ---
>> diff --git a/builtin/apply.c b/builtin/apply.c
>> @@ -4381,7 +4387,7 @@ static int write_out_results(struct apply_state *state, struct patch *list)
>>                                 errs = 1;
>>                         else {
>>                                 if (write_out_one_result(state, l, phase))
>> -                                       exit(1);
>> +                                       return -1;
>
> Isn't this leaking 'string_list cpath'?

Yes, it is. Will fix.

>>                                 if (phase == 1) {
>>                                         if (write_out_one_reject(state, l))
>>                                                 errs = 1;
>> @@ -4484,11 +4490,16 @@ static int apply_patch(struct apply_state *state,
>>             !state->apply_with_reject)
>>                 return -1;
>>
>> -       if (state->apply && write_out_results(state, list)) {
>> -               if (state->apply_with_reject)
>> +       if (state->apply) {
>> +               int res = write_out_results(state, list);
>> +               if (res < 0)
>>                         return -1;
>
> Mentioned previously: Leaking 'list', 'buf', 'fn_table'.

Yeah, already fixed.

>> -               /* with --3way, we still need to write the index out */
>> -               return 1;
>> +               if (res > 0) {
>> +                       if (state->apply_with_reject)
>> +                               return -1;
>> +                       /* with --3way, we still need to write the index out */
>> +                       return 1;
>
> Not the fault of this patch, but this 'return' in the original code
> was also leaking 'list', 'buf', 'fn_table', right?

Yeah, already fixed.

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 74/83] builtin/apply: make try_create_file() return -1 on error
  2016-05-02 18:01   ` Eric Sunshine
@ 2016-05-03 16:24     ` Christian Couder
  0 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-05-03 16:24 UTC (permalink / raw)
  To: Eric Sunshine
  Cc: Git List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

On Mon, May 2, 2016 at 8:01 PM, Eric Sunshine <sunshine@sunshineco.com> wrote:
> On Sun, Apr 24, 2016 at 9:34 AM, Christian Couder
> <christian.couder@gmail.com> wrote:
>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
>> ---
>> diff --git a/builtin/apply.c b/builtin/apply.c
>> @@ -4145,28 +4151,32 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,
>>         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, &nbuf)) {
>>                 size = nbuf.len;
>>                 buf  = nbuf.buf;
>>         }
>> -       write_or_die(fd, buf, size);
>> +
>> +       if (!write_or_whine_pipe(fd, buf, size, path)) {
>> +               strbuf_release(&nbuf);
>> +               return -1;
>
> This is leaking 'fd'.

Ok, fixed.

>> @@ -4208,12 +4227,15 @@ 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) {
>>                                 if (!rename(newpath, path))
>>                                         return;
>>                                 unlink_or_warn(newpath);
>>                                 break;
>>                         }
>> +                       if (res < 0)
>> +                               exit(1);
>
> Two issues:
>
> Getting the error case out of the way early (moving this 'if' just
> after 'res=...') would make it easier to reason about the remaining
> logic.

Ok, I moved up the error cases.

> It's already difficult to understand what the below 'errno' check is
> testing. try_create_file(), rename(), or unlink_or_warn()? Plopping
> this new error handling conditional in front of it divorces the
> 'errno' check even further from whatever it is testing.

Yeah, but I don't see what I could do about that.

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 78/83] Move libified code from builtin/apply.c to apply.{c,h}
  2016-04-25 13:46   ` [PATCH 78/83] Move libified code from builtin/apply.c to apply.{c,h} Duy Nguyen
@ 2016-05-04 10:39     ` Christian Couder
  2016-05-04 11:32       ` Duy Nguyen
  0 siblings, 1 reply; 156+ messages in thread
From: Christian Couder @ 2016-05-04 10:39 UTC (permalink / raw)
  To: Duy Nguyen
  Cc: Git Mailing List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Johannes Schindelin, Stefan Beller, Matthieu Moy,
	Christian Couder

On Mon, Apr 25, 2016 at 3:46 PM, Duy Nguyen <pclouds@gmail.com> wrote:
> On Sun, Apr 24, 2016 at 8:34 PM, Christian Couder
> <christian.couder@gmail.com> wrote:
>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
>> ---
>>  apply.c         | 4678 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
>>  apply.h         |   19 +
>>  builtin/apply.c | 4677 ------------------------------------------------------
>>  3 files changed, 4695 insertions(+), 4679 deletions(-)
>
> Maybe if you can rearrange functions in the right order in
> builtin/apply.c first and move everything in one patch (i.e. apply.c
> is created here), rename detection kicks in realizes apply.c is a
> rename of (old) builtin/apply.c and only shows the diff of (new)
> builtin/apply.c which is about 90 lines... I haven't tested if this is
> true though.

Right now I get:

> git log --summary -M -C -B -1 20f1d27
commit 20f1d274609f5dde2eaaa279e7ee79fd5ef9c849
Author: Christian Couder <chriscool@tuxfamily.org>
Date:   Fri Apr 22 20:55:46 2016 +0200

    Move libified code from builtin/apply.c to apply.{c,h}

    Signed-off-by: Christian Couder <chriscool@tuxfamily.org>

 copy builtin/apply.c => apply.c (96%)
 rewrite builtin/apply.c (96%)

And using format-patch:

> git format-patch -M -C -B -1  -o ../../patches/test-libify-apply-use-in-am/ 20f1d27
../../patches/test-libify-apply-use-in-am/0001-Move-libified-code-from-builtin-apply.c-to-apply.-c-.patch
> wc ../../patches/test-libify-apply-use-in-am/0001-Move-libified-code-from-builtin-apply.c-to-apply.-c-.patch
  5264  23426 147133
../../patches/test-libify-apply-use-in-am/0001-Move-libified-code-from-builtin-apply.c-to-apply.-c-.patch

The previous patch was:

> wc ../../patches/libify-apply-use-in-am25/0078-Move-libified-code-from-builtin-apply.c-to-apply.-c-.patch
  9436  41887 254435
../../patches/libify-apply-use-in-am25/0078-Move-libified-code-from-builtin-apply.c-to-apply.-c-.patch

So it is better, though not really as small as I would like.

I am also not so sure that moving everything in one patch would make
things much smaller.
By the way at one point I have to create apply.h from some of the
content of builtin/apply.c (like the struct apply_state) and I don't
know if you are talking about that too.

If you want you can try using the latest version of the series:

https://github.com/chriscool/git/commits/libify-apply-use-in-am39

Right now I think I will just use -M -C -B with format-patch for v2.

Thanks,
Christian.

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 78/83] Move libified code from builtin/apply.c to apply.{c,h}
  2016-05-04 10:39     ` Christian Couder
@ 2016-05-04 11:32       ` Duy Nguyen
  2016-05-05  8:50         ` Christian Couder
  0 siblings, 1 reply; 156+ messages in thread
From: Duy Nguyen @ 2016-05-04 11:32 UTC (permalink / raw)
  To: Christian Couder
  Cc: Git Mailing List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Johannes Schindelin, Stefan Beller, Matthieu Moy,
	Christian Couder

On Wed, May 4, 2016 at 5:39 PM, Christian Couder
<christian.couder@gmail.com> wrote:
> On Mon, Apr 25, 2016 at 3:46 PM, Duy Nguyen <pclouds@gmail.com> wrote:
>> On Sun, Apr 24, 2016 at 8:34 PM, Christian Couder
>> <christian.couder@gmail.com> wrote:
>>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
>>> ---
>>>  apply.c         | 4678 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
>>>  apply.h         |   19 +
>>>  builtin/apply.c | 4677 ------------------------------------------------------
>>>  3 files changed, 4695 insertions(+), 4679 deletions(-)
>>
>> Maybe if you can rearrange functions in the right order in
>> builtin/apply.c first and move everything in one patch (i.e. apply.c
>> is created here), rename detection kicks in realizes apply.c is a
>> rename of (old) builtin/apply.c and only shows the diff of (new)
>> builtin/apply.c which is about 90 lines... I haven't tested if this is
>> true though.
>
> Right now I get:
>
>> git log --summary -M -C -B -1 20f1d27
> commit 20f1d274609f5dde2eaaa279e7ee79fd5ef9c849
> Author: Christian Couder <chriscool@tuxfamily.org>
> Date:   Fri Apr 22 20:55:46 2016 +0200
>
>     Move libified code from builtin/apply.c to apply.{c,h}
>
>     Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
>
>  copy builtin/apply.c => apply.c (96%)
>  rewrite builtin/apply.c (96%)

Ah.. I forgot about -B to break rename pairs! This looks good. I
really need to go back to diff rename hints series, so we can annotate
this commit and don't have to use -B every time.

> And using format-patch:
>
>> git format-patch -M -C -B -1  -o ../../patches/test-libify-apply-use-in-am/ 20f1d27
> ../../patches/test-libify-apply-use-in-am/0001-Move-libified-code-from-builtin-apply.c-to-apply.-c-.patch
>> wc ../../patches/test-libify-apply-use-in-am/0001-Move-libified-code-from-builtin-apply.c-to-apply.-c-.patch
>   5264  23426 147133

It's a lot of lines, but if you look at diff stat, it's 244 insertions
and 4906 deletions. I _think_ deletions are something reviewers don't
have to pay much attention to (trusting that git moves code
correctly), so only 244 lines to review are really nice. But I may be
wrong.
-- 
Duy

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 78/83] Move libified code from builtin/apply.c to apply.{c,h}
  2016-05-04 11:32       ` Duy Nguyen
@ 2016-05-05  8:50         ` Christian Couder
  2016-05-06 20:18           ` Christian Couder
  0 siblings, 1 reply; 156+ messages in thread
From: Christian Couder @ 2016-05-05  8:50 UTC (permalink / raw)
  To: Duy Nguyen
  Cc: Git Mailing List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Johannes Schindelin, Stefan Beller, Matthieu Moy,
	Christian Couder

On Wed, May 4, 2016 at 1:32 PM, Duy Nguyen <pclouds@gmail.com> wrote:
> On Wed, May 4, 2016 at 5:39 PM, Christian Couder
> <christian.couder@gmail.com> wrote:
>>
>> Right now I get:
>>
>>> git log --summary -M -C -B -1 20f1d27
>> commit 20f1d274609f5dde2eaaa279e7ee79fd5ef9c849
>> Author: Christian Couder <chriscool@tuxfamily.org>
>> Date:   Fri Apr 22 20:55:46 2016 +0200
>>
>>     Move libified code from builtin/apply.c to apply.{c,h}
>>
>>     Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
>>
>>  copy builtin/apply.c => apply.c (96%)
>>  rewrite builtin/apply.c (96%)
>
> Ah.. I forgot about -B to break rename pairs! This looks good. I
> really need to go back to diff rename hints series, so we can annotate
> this commit and don't have to use -B every time.
>
>> And using format-patch:
>>
>>> git format-patch -M -C -B -1  -o ../../patches/test-libify-apply-use-in-am/ 20f1d27
>> ../../patches/test-libify-apply-use-in-am/0001-Move-libified-code-from-builtin-apply.c-to-apply.-c-.patch
>>> wc ../../patches/test-libify-apply-use-in-am/0001-Move-libified-code-from-builtin-apply.c-to-apply.-c-.patch
>>   5264  23426 147133
>
> It's a lot of lines, but if you look at diff stat, it's 244 insertions
> and 4906 deletions. I _think_ deletions are something reviewers don't
> have to pay much attention to (trusting that git moves code
> correctly), so only 244 lines to review are really nice. But I may be
> wrong.

Yeah, you are right. I hadn't looked at the diff stat. Thanks.

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 78/83] Move libified code from builtin/apply.c to apply.{c,h}
  2016-05-05  8:50         ` Christian Couder
@ 2016-05-06 20:18           ` Christian Couder
  2016-05-06 21:07             ` Junio C Hamano
  0 siblings, 1 reply; 156+ messages in thread
From: Christian Couder @ 2016-05-06 20:18 UTC (permalink / raw)
  To: Duy Nguyen
  Cc: Git Mailing List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Johannes Schindelin, Stefan Beller, Matthieu Moy,
	Christian Couder

On Thu, May 5, 2016 at 10:50 AM, Christian Couder
<christian.couder@gmail.com> wrote:
> On Wed, May 4, 2016 at 1:32 PM, Duy Nguyen <pclouds@gmail.com> wrote:
>> On Wed, May 4, 2016 at 5:39 PM, Christian Couder
>> <christian.couder@gmail.com> wrote:
>>>
>>> Right now I get:
>>>
>>>> git log --summary -M -C -B -1 20f1d27
>>> commit 20f1d274609f5dde2eaaa279e7ee79fd5ef9c849
>>> Author: Christian Couder <chriscool@tuxfamily.org>
>>> Date:   Fri Apr 22 20:55:46 2016 +0200
>>>
>>>     Move libified code from builtin/apply.c to apply.{c,h}
>>>
>>>     Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
>>>
>>>  copy builtin/apply.c => apply.c (96%)
>>>  rewrite builtin/apply.c (96%)
>>
>> Ah.. I forgot about -B to break rename pairs! This looks good. I
>> really need to go back to diff rename hints series, so we can annotate
>> this commit and don't have to use -B every time.
>>
>>> And using format-patch:
>>>
>>>> git format-patch -M -C -B -1  -o ../../patches/test-libify-apply-use-in-am/ 20f1d27
>>> ../../patches/test-libify-apply-use-in-am/0001-Move-libified-code-from-builtin-apply.c-to-apply.-c-.patch
>>>> wc ../../patches/test-libify-apply-use-in-am/0001-Move-libified-code-from-builtin-apply.c-to-apply.-c-.patch
>>>   5264  23426 147133

By the way does someone know how such a patch can be applied?

I get the following:

> git reset --keep 20f1d27~
> git am ../../patches/test-libify-apply-use-in-am/0001-Move-libified-code-from-builtin-apply.c-to-apply.-c-.patch
Applying: Move libified code from builtin/apply.c to apply.{c,h}
error: apply.c: already exists in index
Patch failed at 0001 Move libified code from builtin/apply.c to apply.{c,h}
The copy of the patch that failed is found in:
   /home/christian/git/git/.git/rebase-apply/patch
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".

With git apply I get:

> git am --abort
> git apply -v ../../patches/test-libify-apply-use-in-am/0001-Move-libified-code-from-builtin-apply.c-to-apply.-c-.patch
Checking patch builtin/apply.c => apply.c...
error: apply.c: already exists in working directory
Checking patch apply.h...
Checking patch builtin/apply.c...

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 78/83] Move libified code from builtin/apply.c to apply.{c,h}
  2016-05-06 20:18           ` Christian Couder
@ 2016-05-06 21:07             ` Junio C Hamano
  2016-05-08 20:11               ` Christian Couder
  0 siblings, 1 reply; 156+ messages in thread
From: Junio C Hamano @ 2016-05-06 21:07 UTC (permalink / raw)
  To: Christian Couder
  Cc: Duy Nguyen, Git Mailing List, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Johannes Schindelin, Stefan Beller, Matthieu Moy,
	Christian Couder

Christian Couder <christian.couder@gmail.com> writes:

> By the way does someone know how such a patch can be applied?

Perhaps this one?

    http://thread.gmane.org/gmane.linux.kernel/1879635

^ permalink raw reply	[flat|nested] 156+ messages in thread

* Re: [PATCH 78/83] Move libified code from builtin/apply.c to apply.{c,h}
  2016-05-06 21:07             ` Junio C Hamano
@ 2016-05-08 20:11               ` Christian Couder
  0 siblings, 0 replies; 156+ messages in thread
From: Christian Couder @ 2016-05-08 20:11 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: Duy Nguyen, Git Mailing List, Jeff King,
	Ævar Arnfjörð, Karsten Blees, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

On Fri, May 6, 2016 at 11:07 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Christian Couder <christian.couder@gmail.com> writes:
>
>> By the way does someone know how such a patch can be applied?
>
> I think in mid Feb 2015 I sent out a message that says "'diff -B -M'
> gives a broken result, do not use it".
> I do not have time to dig the mail archive right now.
>
> Perhaps this one?
>
>     http://thread.gmane.org/gmane.linux.kernel/1879635

I get something strange with only -B:

$ git reset --keep 20f1d27~
$ git format-patch -B -1  -o ../../patches/test-libify-apply-use-in-am/ 20f1d27
../../patches/test-libify-apply-use-in-am/0001-Move-libified-code-from-builtin-apply.c-to-apply.-c-.patch
$ wc ../../patches/test-libify-apply-use-in-am/0001-Move-libified-code-from-builtin-apply.c-to-apply.-c-.patch
  5264  23426 147127
../../patches/test-libify-apply-use-in-am/0001-Move-libified-code-from-builtin-apply.c-to-apply.-c-.patch

I get a conflict with `git am`:

$ git am --3way
../../patches/test-libify-apply-use-in-am/0001-Move-libified-code-from-builtin-apply.c-to-apply.-c-.patch
Applying: Move libified code from builtin/apply.c to apply.{c,h}
Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
Auto-merging apply.c
CONFLICT (add/add): Merge conflict in apply.c
error: Failed to merge in the changes.
Patch failed at 0001 Move libified code from builtin/apply.c to apply.{c,h}
The copy of the patch that failed is found in: .git/rebase-apply/patch
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".

But it works using `git apply --3way`:

$ git reset --hard HEAD
HEAD is now at 557f659 apply: rename and move opt constants to apply.h
$ git apply --3way
../../patches/test-libify-apply-use-in-am/0001-Move-libified-code-from-builtin-apply.c-to-apply.-c-.patch
Falling back to three-way merge...
Applied patch to 'apply.c' cleanly.
$ git am --continue
Applying: Move libified code from builtin/apply.c to apply.{c,h}

^ permalink raw reply	[flat|nested] 156+ messages in thread

end of thread, other threads:[~2016-05-08 20:11 UTC | newest]

Thread overview: 156+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-04-24 13:33 [PATCH 00/83] libify apply and use lib in am Christian Couder
2016-04-24 13:33 ` [PATCH 01/83] builtin/apply: make gitdiff_verify_name() return void Christian Couder
2016-04-24 13:33 ` [PATCH 02/83] builtin/apply: avoid parameter shadowing 'p_value' global Christian Couder
2016-04-24 13:33 ` [PATCH 03/83] builtin/apply: avoid parameter shadowing 'linenr' global Christian Couder
2016-04-27 16:27   ` Junio C Hamano
2016-04-27 17:07     ` Christian Couder
2016-04-24 13:33 ` [PATCH 04/83] builtin/apply: avoid local variable shadowing 'len' parameter Christian Couder
2016-04-24 13:33 ` [PATCH 05/83] builtin/apply: extract line_by_line_fuzzy_match() from match_fragment() Christian Couder
2016-04-25 18:50   ` Stefan Beller
2016-04-26 16:15     ` Christian Couder
2016-04-26 20:20       ` Junio C Hamano
2016-04-27 13:31         ` Christian Couder
2016-04-24 13:33 ` [PATCH 06/83] builtin/apply: move 'options' variable into cmd_apply() Christian Couder
2016-04-24 13:33 ` [PATCH 07/83] builtin/apply: introduce 'struct apply_state' to start libifying Christian Couder
2016-04-24 13:33 ` [PATCH 08/83] builtin/apply: move 'unidiff_zero' global into 'struct apply_state' Christian Couder
2016-04-24 13:33 ` [PATCH 09/83] builtin/apply: move 'check' " Christian Couder
2016-04-25 18:57   ` Stefan Beller
2016-04-26 16:26     ` Christian Couder
2016-04-26 16:31       ` Stefan Beller
2016-04-24 13:33 ` [PATCH 10/83] builtin/apply: move 'check_index' " Christian Couder
2016-04-26 20:25   ` Junio C Hamano
2016-04-24 13:33 ` [PATCH 11/83] builtin/apply: move 'apply_in_reverse' " Christian Couder
2016-04-24 13:33 ` [PATCH 12/83] builtin/apply: move 'apply_with_reject' " Christian Couder
2016-04-24 13:33 ` [PATCH 13/83] builtin/apply: move 'apply_verbosely' " Christian Couder
2016-04-24 13:33 ` [PATCH 14/83] builtin/apply: move 'update_index' " Christian Couder
2016-04-24 13:33 ` [PATCH 15/83] builtin/apply: move 'allow_overlap' " Christian Couder
2016-04-24 13:33 ` [PATCH 16/83] builtin/apply: move 'cached' " Christian Couder
2016-04-24 13:33 ` [PATCH 17/83] builtin/apply: move 'diffstat' " Christian Couder
2016-04-24 13:33 ` [PATCH 18/83] builtin/apply: move 'numstat' " Christian Couder
2016-04-25 21:40   ` Stefan Beller
2016-04-26 16:35     ` Christian Couder
2016-04-24 13:33 ` [PATCH 19/83] builtin/apply: move 'summary' " Christian Couder
2016-04-24 13:33 ` [PATCH 20/83] builtin/apply: move 'threeway' " Christian Couder
2016-04-24 13:33 ` [PATCH 21/83] builtin/apply: move 'no-add' " Christian Couder
2016-04-24 13:33 ` [PATCH 22/83] builtin/apply: move 'unsafe_paths' " Christian Couder
2016-04-26 20:27   ` Junio C Hamano
2016-04-27 12:14     ` Christian Couder
2016-04-27 15:26       ` Junio C Hamano
2016-04-24 13:33 ` [PATCH 23/83] builtin/apply: move 'line_termination' " Christian Couder
2016-04-24 13:33 ` [PATCH 24/83] builtin/apply: move 'fake_ancestor' " Christian Couder
2016-04-24 13:33 ` [PATCH 25/83] builtin/apply: move 'p_context' " Christian Couder
2016-04-24 13:33 ` [PATCH 26/83] builtin/apply: move 'apply' " Christian Couder
2016-04-24 13:33 ` [PATCH 27/83] builtin/apply: move 'read_stdin' global into cmd_apply() Christian Couder
2016-04-26 20:28   ` Junio C Hamano
2016-04-27 13:13     ` Christian Couder
2016-04-24 13:33 ` [PATCH 28/83] builtin/apply: move 'patch_input_file' global into 'struct apply_state' Christian Couder
2016-04-24 13:33 ` [PATCH 29/83] builtin/apply: move 'limit_by_name' " Christian Couder
2016-04-24 13:33 ` [PATCH 30/83] builtin/apply: move 'has_include' " Christian Couder
2016-04-24 13:33 ` [PATCH 31/83] builtin/apply: move 'p_value' " Christian Couder
2016-04-24 13:33 ` [PATCH 32/83] builtin/apply: move 'p_value_known' " Christian Couder
2016-04-24 13:33 ` [PATCH 33/83] builtin/apply: move 'root' " Christian Couder
2016-04-25 21:50   ` Stefan Beller
2016-04-25 21:54     ` Stefan Beller
2016-04-26 20:13     ` Christian Couder
2016-04-24 13:33 ` [PATCH 34/83] builtin/apply: move 'whitespace_error' " Christian Couder
2016-04-24 13:33 ` [PATCH 35/83] builtin/apply: move 'whitespace_option' " Christian Couder
2016-04-24 13:33 ` [PATCH 36/83] builtin/apply: remove whitespace_option arg from set_default_whitespace_mode() Christian Couder
2016-04-24 13:33 ` [PATCH 37/83] builtin/apply: move 'squelch_whitespace_errors' into 'struct apply_state' Christian Couder
2016-04-24 13:33 ` [PATCH 38/83] builtin/apply: move 'applied_after_fixing_ws' " Christian Couder
2016-04-24 13:33 ` [PATCH 39/83] builtin/apply: move 'ws_error_action' " Christian Couder
2016-04-26 20:36   ` Junio C Hamano
2016-04-26 20:40     ` Junio C Hamano
2016-04-27 13:23     ` Christian Couder
2016-04-24 13:33 ` [PATCH 40/83] builtin/apply: move 'ws_ignore_action' " Christian Couder
2016-04-24 13:33 ` [PATCH 41/83] builtin/apply: move 'max_change' and 'max_len' " Christian Couder
2016-04-24 13:33 ` [PATCH 42/83] builtin/apply: move 'linenr' global " Christian Couder
2016-04-24 13:33 ` [PATCH 43/83] builtin/apply: move 'fn_table' " Christian Couder
2016-04-24 13:33 ` [PATCH 44/83] builtin/apply: move 'symlink_changes' " Christian Couder
2016-04-24 13:33 ` [PATCH 45/83] builtin/apply: move 'state' init into init_apply_state() Christian Couder
2016-04-25  7:32   ` Eric Sunshine
2016-04-27 12:00     ` Christian Couder
2016-04-24 13:33 ` [PATCH 46/83] builtin/apply: move 'state' check into check_apply_state() Christian Couder
2016-04-24 13:33 ` [PATCH 47/83] builtin/apply: move applying patches into apply_all_patches() Christian Couder
2016-04-25 22:00   ` Stefan Beller
2016-04-27 15:51     ` Christian Couder
2016-04-24 13:33 ` [PATCH 48/83] builtin/apply: rename 'prefix_' parameter to 'prefix' Christian Couder
2016-04-24 13:33 ` [PATCH 49/83] builtin/apply: move 'lock_file' global into 'struct apply_state' Christian Couder
2016-04-25  7:50   ` Eric Sunshine
2016-04-25 17:55     ` Junio C Hamano
2016-04-28 16:30       ` Christian Couder
2016-04-28 20:17         ` Junio C Hamano
2016-04-30 19:39     ` Christian Couder
2016-04-24 13:33 ` [PATCH 50/83] builtin/apply: get rid of the 'newfd' global Christian Couder
2016-04-24 13:33 ` [PATCH 51/83] builtin/apply: make apply_patch() return -1 instead of die()ing Christian Couder
2016-04-26  1:20   ` Eric Sunshine
2016-04-30 19:41     ` Christian Couder
2016-04-24 13:33 ` [PATCH 52/83] builtin/apply: read_patch_file() " Christian Couder
2016-04-24 13:33 ` [PATCH 53/83] builtin/apply: make find_header() " Christian Couder
2016-04-25 13:18   ` Duy Nguyen
2016-04-27 18:10     ` Eric Sunshine
2016-05-01 16:53       ` Christian Couder
2016-04-27 18:08   ` Eric Sunshine
2016-05-01 16:45     ` Christian Couder
2016-04-24 13:33 ` [PATCH 54/83] builtin/apply: make parse_chunk() return a negative integer on error Christian Couder
2016-05-01 19:04   ` Eric Sunshine
2016-05-01 19:51     ` Christian Couder
2016-04-24 13:33 ` [PATCH 55/83] builtin/apply: make parse_single_patch() return -1 " Christian Couder
2016-05-01 19:32   ` Eric Sunshine
2016-04-24 13:33 ` [PATCH 56/83] apply: move 'struct apply_state' to apply.h Christian Couder
2016-04-24 13:33 ` [PATCH 57/83] builtin/apply: libify parse_whitespace_option() Christian Couder
2016-04-24 13:33 ` [PATCH 58/83] builtin/apply: libify parse_ignorewhitespace_option() Christian Couder
2016-04-24 13:33 ` [PATCH 59/83] builtin/apply: move init_apply_state() to apply.c Christian Couder
2016-05-01 19:37   ` Eric Sunshine
2016-05-01 20:19     ` Christian Couder
2016-04-24 13:34 ` [PATCH 60/83] apply: libify init_apply_state() Christian Couder
2016-04-24 13:34 ` [PATCH 61/83] builtin/apply: libify check_apply_state() Christian Couder
2016-04-25 13:26   ` Duy Nguyen
2016-05-01 16:58     ` Christian Couder
2016-04-24 13:34 ` [PATCH 62/83] builtin/apply: move check_apply_state() to apply.c Christian Couder
2016-04-24 13:34 ` [PATCH 63/83] builtin/apply: make apply_all_patches() return -1 on error Christian Couder
2016-04-25 13:30   ` Duy Nguyen
2016-05-01 18:32     ` Christian Couder
2016-05-01 21:03   ` Eric Sunshine
2016-05-02  7:09     ` Johannes Schindelin
2016-05-03 13:01     ` Christian Couder
2016-04-24 13:34 ` [PATCH 64/83] builtin/apply: make parse_traditional_patch() " Christian Couder
2016-04-24 13:34 ` [PATCH 65/83] builtin/apply: make gitdiff_verify_name() " Christian Couder
2016-04-25 13:36   ` Duy Nguyen
2016-05-01 19:31     ` Christian Couder
2016-04-24 13:34 ` [PATCH 66/83] builtin/apply: change die_on_unsafe_path() to check_unsafe_path() Christian Couder
2016-04-24 13:34 ` [PATCH 67/83] builtin/apply: make build_fake_ancestor() return -1 on error Christian Couder
2016-05-02  7:32   ` Eric Sunshine
2016-05-03 13:52     ` Christian Couder
2016-04-24 13:34 ` [PATCH 68/83] builtin/apply: make remove_file() " Christian Couder
2016-04-24 13:34 ` [PATCH 69/83] builtin/apply: make add_conflicted_stages_file() " Christian Couder
2016-05-02  7:36   ` Eric Sunshine
2016-05-03 14:33     ` Christian Couder
2016-04-24 13:34 ` [PATCH 70/83] builtin/apply: make add_index_file() " Christian Couder
2016-04-24 13:34 ` [PATCH 71/83] builtin/apply: make create_file() " Christian Couder
2016-04-24 13:34 ` [PATCH 72/83] builtin/apply: make write_out_one_result() " Christian Couder
2016-04-24 13:34 ` [PATCH 73/83] builtin/apply: make write_out_results() " Christian Couder
2016-05-02 17:42   ` Eric Sunshine
2016-05-03 14:55     ` Christian Couder
2016-04-24 13:34 ` [PATCH 74/83] builtin/apply: make try_create_file() " Christian Couder
2016-05-02 18:01   ` Eric Sunshine
2016-05-03 16:24     ` Christian Couder
2016-04-24 13:34 ` [PATCH 75/83] builtin/apply: make create_one_file() " Christian Couder
2016-04-24 13:34 ` [PATCH 76/83] builtin/apply: rename option parsing functions Christian Couder
2016-04-24 13:34 ` [PATCH 77/83] apply: rename and move opt constants to apply.h Christian Couder
2016-04-24 15:23 ` [PATCH 00/83] libify apply and use lib in am Ramsay Jones
2016-04-24 16:27   ` Christian Couder
2016-04-24 16:56     ` Christian Couder
2016-04-24 17:42       ` Ramsay Jones
2016-04-25  0:14         ` Duy Nguyen
2016-04-25  9:15           ` Christian Couder
2016-04-25 15:09       ` Johannes Schindelin
2016-04-25  9:02 ` Duy Nguyen
2016-04-25  9:57   ` Christian Couder
2016-04-25 10:11     ` Duy Nguyen
     [not found] ` <1461504863-15946-79-git-send-email-chriscool@tuxfamily.org>
2016-04-25 13:46   ` [PATCH 78/83] Move libified code from builtin/apply.c to apply.{c,h} Duy Nguyen
2016-05-04 10:39     ` Christian Couder
2016-05-04 11:32       ` Duy Nguyen
2016-05-05  8:50         ` Christian Couder
2016-05-06 20:18           ` Christian Couder
2016-05-06 21:07             ` Junio C Hamano
2016-05-08 20:11               ` Christian Couder

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).