All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 00/94] libify apply and use lib in am
@ 2016-05-11 13:16 Christian Couder
  2016-05-11 13:16 ` [PATCH v2 01/94] builtin/apply: make gitdiff_verify_name() return void Christian Couder
                   ` (94 more replies)
  0 siblings, 95 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:16 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

Goal
~~~~

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.

Previous discussions and patches series
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This has initially been discussed in the following thread:

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

Then a RFC patch series was sent:

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

Then v1 was sent:

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

Highlevel view of the pqtches in the series
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This new patch series is built on top of the above previous work.

  - Patches 1/94 to 47/94 are here from the RFC patch series.

They get rid of most of the global variables in builtin/apply.c by
moving the variables into a new 'struct apply_state', and they
refactor the code around a bit.

They haven't changed much since v1. There are mostly variables that
have been moved around inside 'struct apply_state' or inside
match_fragment(), new lines that have removed in 'struct apply_state'
and function parameters that have been renamed.

  - Patches 48/94 to 84/94 were new in v1.

They finish libifying the apply functionality that was in
builtin/apply.c and move it into apply.{c,h}. And they use this
libified functionality in git am so that it doesn't launch git apply
processes any more.

There are many changes in these patches, as there were a lot of great
suggestions from many reviewers to improve on v1:

      - Lockfile management changes as suggested by Junio.
      - Many memory leaks were fixed as suggested by Eric.
      - A patch was splitted into two as suggested by Duy.
      - Some file descriptors have been closed as suggested by Dscho.
      - Some early return were moved up as suggested by Eric.
      - Many commit messages were improved.

I am not very happy that I added some gotos, but it looked like a not
too bad way to make sure that the memory and file descriptor cleanups
were done before returning from some functions.

  - Patches 85/94 to 94/94 are new and can be considered RFC.

They implement a way to make the libified apply silent. It is a new
feature in the libified apply functionality.

I consider that the apply functionality is properly libified before
these patches, and that they should be in a separate series, but
unfortunately using the libified apply in "git am" unmasks the fact
that "git am", since it was a shell script, has been silencing the
apply functionality by "futzing with file descriptors". And
unfortunately this makes some reviewers unhappy.

The last patch 94/94 adds a --silent command line option to git apply.
This is not necessary, but some scripts could perhaps use it, and it
could make it easier to test the new silent feature.

By the way there are no tests yet for this new feature, and I am not
sure at all that "--silent" and "be_silent" are good names. Also I
will perhaps be asked to move or merge this part of this series, to
remove the "futzing with file descriptors" earlier in the former
patches. That's why this part of the series can be considered RFC.

General comments
~~~~~~~~~~~~~~~~

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

I can also send diffs between this version and the previous one, but
for now I'd rather not send them in this email, as it would make it
very long.

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.

Using this series as rebase material, Duy explains it like this:

 > 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

(See: http://thread.gmane.org/gmane.comp.version-control.git/292324/focus=292460)

Links
~~~~~

This patch series is available here:

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

The previous version, v1, is available here:

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

Performance numbers
~~~~~~~~~~~~~~~~~~~

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

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

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

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

Ævar used his Debian laptop with SSD.

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

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

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


Christian Couder (94):
  builtin/apply: make gitdiff_verify_name() return void
  builtin/apply: avoid parameter shadowing 'p_value' global
  builtin/apply: avoid parameter shadowing 'linenr' global
  builtin/apply: avoid local variable shadowing 'len' parameter
  builtin/apply: extract line_by_line_fuzzy_match() from
    match_fragment()
  builtin/apply: move 'options' variable into cmd_apply()
  builtin/apply: move 'read_stdin' global into cmd_apply()
  builtin/apply: introduce 'struct apply_state' to start libifying
  builtin/apply: move 'state' init into init_apply_state()
  builtin/apply: move 'unidiff_zero' global into 'struct apply_state'
  builtin/apply: move 'check' global into 'struct apply_state'
  builtin/apply: move 'check_index' global into 'struct apply_state'
  builtin/apply: move 'apply_in_reverse' global into 'struct
    apply_state'
  builtin/apply: move 'apply_with_reject' global into 'struct
    apply_state'
  builtin/apply: move 'apply_verbosely' global into 'struct apply_state'
  builtin/apply: move 'update_index' global into 'struct apply_state'
  builtin/apply: move 'allow_overlap' global into 'struct apply_state'
  builtin/apply: move 'cached' global into 'struct apply_state'
  builtin/apply: move 'diffstat' global into 'struct apply_state'
  builtin/apply: move 'numstat' global into 'struct apply_state'
  builtin/apply: move 'summary' global into 'struct apply_state'
  builtin/apply: move 'threeway' global into 'struct apply_state'
  builtin/apply: move 'no_add' global into 'struct apply_state'
  builtin/apply: move 'unsafe_paths' global into 'struct apply_state'
  builtin/apply: move 'line_termination' global into 'struct
    apply_state'
  builtin/apply: move 'fake_ancestor' global into 'struct apply_state'
  builtin/apply: move 'p_context' global into 'struct apply_state'
  builtin/apply: move 'apply' global into 'struct apply_state'
  builtin/apply: move 'patch_input_file' global into 'struct
    apply_state'
  builtin/apply: move 'limit_by_name' global into 'struct apply_state'
  builtin/apply: move 'has_include' global into 'struct apply_state'
  builtin/apply: move 'p_value' global into 'struct apply_state'
  builtin/apply: move 'p_value_known' global into 'struct apply_state'
  builtin/apply: move 'root' global into 'struct apply_state'
  builtin/apply: move 'whitespace_error' global into 'struct
    apply_state'
  builtin/apply: move 'whitespace_option' into 'struct apply_state'
  builtin/apply: remove whitespace_option arg from
    set_default_whitespace_mode()
  builtin/apply: move 'squelch_whitespace_errors' into 'struct
    apply_state'
  builtin/apply: move 'applied_after_fixing_ws' into 'struct
    apply_state'
  builtin/apply: move 'ws_error_action' into 'struct apply_state'
  builtin/apply: move 'ws_ignore_action' into 'struct apply_state'
  builtin/apply: move 'max_change' and 'max_len' into 'struct
    apply_state'
  builtin/apply: move 'state_linenr' global into 'struct apply_state'
  builtin/apply: move 'fn_table' global into 'struct apply_state'
  builtin/apply: move 'symlink_changes' global into 'struct apply_state'
  builtin/apply: move 'state' check into check_apply_state()
  builtin/apply: move applying patches into apply_all_patches()
  builtin/apply: rename 'prefix_' parameter to 'prefix'
  builtin/apply: move 'lock_file' global into 'struct apply_state'
  builtin/apply: move 'newfd' global into 'struct apply_state'
  builtin/apply: make apply_patch() return -1 instead of die()ing
  builtin/apply: read_patch_file() return -1 instead of die()ing
  builtin/apply: make find_header() return -1 instead of die()ing
  builtin/apply: make parse_chunk() return a negative integer on error
  builtin/apply: make parse_single_patch() return -1 on error
  apply: move 'struct apply_state' to apply.h
  builtin/apply: make parse_whitespace_option() return -1 instead of
    die()ing
  builtin/apply: make parse_ignorewhitespace_option() return -1 instead
    of die()ing
  builtin/apply: move init_apply_state() to apply.c
  apply: make init_apply_state() return -1 instead of exit()ing
  builtin/apply: make check_apply_state() return -1 instead of die()ing
  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_*() return 1 at end of header
  builtin/apply: make gitdiff_*() 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 lock file in case of error
  environment: add set_index_file()
  builtin/am: use apply api in run_apply()
  write_or_die: use warning() instead of fprintf(stderr, ...)
  apply: add 'be_silent' variable to 'struct apply_state'
  apply: make 'be_silent' incomatible with 'apply_verbosely'
  apply: don't print on stdout when be_silent is set
  usage: add set_warn_routine()
  usage: add get_error_routine() and get_warn_routine()
  apply: change error_routine when be_silent is set
  am: use be_silent in 'struct apply_state' to shut up applying patches
  run-command: make dup_devnull() static again
  builtin/apply: add a cli option for be_silent

 Makefile                   |    1 +
 builtin/apply.c => apply.c | 1750 ++++++++--------
 apply.h                    |  146 ++
 builtin/am.c               |   91 +-
 builtin/apply.c            | 4762 +-------------------------------------------
 cache.h                    |    1 +
 environment.c              |   10 +
 git-compat-util.h          |    3 +
 run-command.c              |    2 +-
 t/t4012-diff-binary.sh     |    4 +-
 t/t4254-am-corrupt.sh      |    2 +-
 usage.c                    |   15 +
 write_or_die.c             |    6 +-
 13 files changed, 1324 insertions(+), 5469 deletions(-)
 copy builtin/apply.c => apply.c (73%)
 create mode 100644 apply.h
 rewrite builtin/apply.c (98%)

-- 
2.8.2.490.g3dabe57

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

* [PATCH v2 01/94] builtin/apply: make gitdiff_verify_name() return void
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
@ 2016-05-11 13:16 ` Christian Couder
  2016-05-12 19:06   ` Junio C Hamano
  2016-05-11 13:16 ` [PATCH v2 02/94] builtin/apply: avoid parameter shadowing 'p_value' global Christian Couder
                   ` (93 subsequent siblings)
  94 siblings, 1 reply; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:16 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	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.

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

Reviewed-by: Stefan Beller <sbeller@google.com>
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.2.490.g3dabe57

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

* [PATCH v2 02/94] builtin/apply: avoid parameter shadowing 'p_value' global
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
  2016-05-11 13:16 ` [PATCH v2 01/94] builtin/apply: make gitdiff_verify_name() return void Christian Couder
@ 2016-05-11 13:16 ` Christian Couder
  2016-05-12 19:09   ` Junio C Hamano
  2016-05-11 13:16 ` [PATCH v2 03/94] builtin/apply: avoid parameter shadowing 'linenr' global Christian Couder
                   ` (92 subsequent siblings)
  94 siblings, 1 reply; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:16 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

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

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

Helped-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Reviewed-by: Stefan Beller <sbeller@google.com>
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.2.490.g3dabe57

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

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

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

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

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

diff --git a/builtin/apply.c b/builtin/apply.c
index e133b38..705a9c8 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -144,7 +144,7 @@ static int max_change, max_len;
  * file (and how) we're patching right now.. The "is_xxxx"
  * things are flags, where -1 means "don't know yet".
  */
-static int linenr = 1;
+static int state_linenr = 1;
 
 /*
  * This represents one "hunk" from a patch, starting with
@@ -905,7 +905,7 @@ static void parse_traditional_patch(const char *first, const char *second, struc
 		}
 	}
 	if (!name)
-		die(_("unable to find filename in patch at line %d"), linenr);
+		die(_("unable to find filename in patch at line %d"), state_linenr);
 }
 
 static int gitdiff_hdrend(const char *line, struct patch *patch)
@@ -937,17 +937,17 @@ static void gitdiff_verify_name(const char *line, int isnull, char **name, int s
 		char *another;
 		if (isnull)
 			die(_("git apply: bad git-diff - expected /dev/null, got %s on line %d"),
-			    *name, linenr);
+			    *name, state_linenr);
 		another = find_name(line, NULL, state_p_value, TERM_TAB);
 		if (!another || memcmp(another, *name, len + 1))
 			die((side == DIFF_NEW_NAME) ?
 			    _("git apply: bad git-diff - inconsistent new filename on line %d") :
-			    _("git apply: bad git-diff - inconsistent old filename on line %d"), linenr);
+			    _("git apply: bad git-diff - inconsistent old filename on line %d"), state_linenr);
 		free(another);
 	} else {
 		/* expect "/dev/null" */
 		if (memcmp("/dev/null", line, 9) || line[9] != '\n')
-			die(_("git apply: bad git-diff - expected /dev/null on line %d"), linenr);
+			die(_("git apply: bad git-diff - expected /dev/null on line %d"), state_linenr);
 	}
 }
 
@@ -1272,8 +1272,8 @@ static int parse_git_header(const char *line, int len, unsigned int size, struct
 
 	line += len;
 	size -= len;
-	linenr++;
-	for (offset = len ; size > 0 ; offset += len, size -= len, line += len, linenr++) {
+	state_linenr++;
+	for (offset = len ; size > 0 ; offset += len, size -= len, line += len, state_linenr++) {
 		static const struct opentry {
 			const char *str;
 			int (*fn)(const char *, struct patch *);
@@ -1440,7 +1440,7 @@ static int find_header(const char *line, unsigned long size, int *hdrsize, struc
 	patch->is_new = patch->is_delete = -1;
 	patch->old_mode = patch->new_mode = 0;
 	patch->old_name = patch->new_name = NULL;
-	for (offset = 0; size > 0; offset += len, size -= len, line += len, linenr++) {
+	for (offset = 0; size > 0; offset += len, size -= len, line += len, state_linenr++) {
 		unsigned long nextlen;
 
 		len = linelen(line, size);
@@ -1461,7 +1461,7 @@ static int find_header(const char *line, unsigned long size, int *hdrsize, struc
 			if (parse_fragment_header(line, len, &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)
@@ -1482,13 +1482,13 @@ static int find_header(const char *line, unsigned long size, int *hdrsize, struc
 					       "git diff header lacks filename information when removing "
 					       "%d leading pathname components (line %d)",
 					       state_p_value),
-					    state_p_value, linenr);
+					    state_p_value, state_linenr);
 				patch->old_name = xstrdup(patch->def_name);
 				patch->new_name = xstrdup(patch->def_name);
 			}
 			if (!patch->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;
@@ -1510,7 +1510,7 @@ static int find_header(const char *line, unsigned long size, int *hdrsize, struc
 		/* Ok, we'll consider it a patch */
 		parse_traditional_patch(line, line+len, patch);
 		*hdrsize = len + nextlen;
-		linenr += 2;
+		state_linenr += 2;
 		return offset;
 	}
 	return -1;
@@ -1538,7 +1538,7 @@ static void check_whitespace(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(result, line + 1, len - 2, state_linenr);
 }
 
 /*
@@ -1568,11 +1568,11 @@ static int parse_fragment(const char *line, unsigned long size,
 	/* 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);
@@ -1668,10 +1668,10 @@ static int parse_single_patch(const char *line, unsigned long size, struct patch
 		int len;
 
 		fragment = xcalloc(1, sizeof(*fragment));
-		fragment->linenr = linenr;
+		fragment->linenr = state_linenr;
 		len = parse_fragment(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;
@@ -1799,13 +1799,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++;
@@ -1859,7 +1859,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;
 }
 
@@ -1892,7 +1892,7 @@ static int parse_binary(char *buffer, unsigned long size, struct patch *patch)
 	forward = parse_binary_hunk(&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;
@@ -2010,7 +2010,7 @@ static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
 		if (llen == sizeof(git_binary) - 1 &&
 		    !memcmp(git_binary, buffer + hd, llen)) {
 			int used;
-			linenr++;
+			state_linenr++;
 			used = parse_binary(buffer + hd + llen,
 					    size - hd - llen, patch);
 			if (used < 0)
@@ -2031,7 +2031,7 @@ static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
 				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;
@@ -2045,7 +2045,7 @@ static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)
 		 */
 		if ((apply || 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;
-- 
2.8.2.490.g3dabe57

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

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

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

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

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

* [PATCH v2 05/94] builtin/apply: extract line_by_line_fuzzy_match() from match_fragment()
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (3 preceding siblings ...)
  2016-05-11 13:16 ` [PATCH v2 04/94] builtin/apply: avoid local variable shadowing 'len' parameter Christian Couder
@ 2016-05-11 13:16 ` Christian Couder
  2016-05-12 19:20   ` Junio C Hamano
  2016-05-11 13:16 ` [PATCH v2 06/94] builtin/apply: move 'options' variable into cmd_apply() Christian Couder
                   ` (89 subsequent siblings)
  94 siblings, 1 reply; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:16 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	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.

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

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

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

* [PATCH v2 06/94] builtin/apply: move 'options' variable into cmd_apply()
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (4 preceding siblings ...)
  2016-05-11 13:16 ` [PATCH v2 05/94] builtin/apply: extract line_by_line_fuzzy_match() from match_fragment() Christian Couder
@ 2016-05-11 13:16 ` Christian Couder
  2016-05-11 13:16 ` [PATCH v2 07/94] builtin/apply: move 'read_stdin' global " Christian Couder
                   ` (88 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:16 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

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

This will make it easier to libify the apply functionality.

Reviewed-by: Stefan Beller <sbeller@google.com>
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 7bab466..5a1d65a 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -79,7 +79,6 @@ static enum ws_ignore {
 static const char *patch_input_file;
 static struct strbuf root = STRBUF_INIT;
 static int read_stdin = 1;
-static int options;
 
 static void parse_whitespace_option(const char *option)
 {
@@ -4517,6 +4516,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 	int errs = 0;
 	int is_not_gitdir = !startup_info->have_repository;
 	int force_apply = 0;
+	int options = 0;
 
 	const char *whitespace_option = NULL;
 
-- 
2.8.2.490.g3dabe57

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

* [PATCH v2 07/94] builtin/apply: move 'read_stdin' global into cmd_apply()
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (5 preceding siblings ...)
  2016-05-11 13:16 ` [PATCH v2 06/94] builtin/apply: move 'options' variable into cmd_apply() Christian Couder
@ 2016-05-11 13:16 ` Christian Couder
  2016-05-11 13:16 ` [PATCH v2 08/94] builtin/apply: introduce 'struct apply_state' to start libifying Christian Couder
                   ` (87 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:16 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

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

This will make it easier to libify the apply functionality.

Reviewed-by: Stefan Beller <sbeller@google.com>
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 5a1d65a..c911e4e 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -78,7 +78,6 @@ static enum ws_ignore {
 
 static const char *patch_input_file;
 static struct strbuf root = STRBUF_INIT;
-static int read_stdin = 1;
 
 static void parse_whitespace_option(const char *option)
 {
@@ -4517,6 +4516,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 	int is_not_gitdir = !startup_info->have_repository;
 	int force_apply = 0;
 	int options = 0;
+	int read_stdin = 1;
 
 	const char *whitespace_option = NULL;
 
-- 
2.8.2.490.g3dabe57

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

* [PATCH v2 08/94] builtin/apply: introduce 'struct apply_state' to start libifying
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (6 preceding siblings ...)
  2016-05-11 13:16 ` [PATCH v2 07/94] builtin/apply: move 'read_stdin' global " Christian Couder
@ 2016-05-11 13:16 ` Christian Couder
  2016-05-11 13:16 ` [PATCH v2 09/94] builtin/apply: move 'state' init into init_apply_state() Christian Couder
                   ` (86 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:16 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	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".

Reviewed-by: Stefan Beller <sbeller@google.com>
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 c911e4e..ae068e7 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -21,6 +21,11 @@
 #include "ll-merge.h"
 #include "rerere.h"
 
+struct apply_state {
+	const char *prefix;
+	int prefix_length;
+};
+
 /*
  *  --check turns on checking that the working tree matches the
  *    files that are being modified, but doesn't apply the patch
@@ -30,8 +35,6 @@
  *  --index updates the cache as well.
  *  --cached updates only the cache without ever touching the working tree.
  */
-static const char *prefix;
-static int prefix_length = -1;
 static int newfd = -1;
 
 static int unidiff_zero;
@@ -748,7 +751,7 @@ static int count_slashes(const char *cp)
  * Given the string after "--- " or "+++ ", guess the appropriate
  * p_value for the given patch.
  */
-static int guess_p_value(const char *nameline)
+static int guess_p_value(struct apply_state *state, const char *nameline)
 {
 	char *name, *cp;
 	int val = -1;
@@ -761,17 +764,17 @@ static int guess_p_value(const char *nameline)
 	cp = strchr(name, '/');
 	if (!cp)
 		val = 0;
-	else if (prefix) {
+	else if (state->prefix) {
 		/*
 		 * Does it begin with "a/$our-prefix" and such?  Then this is
 		 * very likely to apply to our directory.
 		 */
-		if (!strncmp(name, prefix, prefix_length))
-			val = count_slashes(prefix);
+		if (!strncmp(name, state->prefix, state->prefix_length))
+			val = count_slashes(state->prefix);
 		else {
 			cp++;
-			if (!strncmp(cp, prefix, prefix_length))
-				val = count_slashes(prefix) + 1;
+			if (!strncmp(cp, state->prefix, state->prefix_length))
+				val = count_slashes(state->prefix) + 1;
 		}
 	}
 	free(name);
@@ -858,7 +861,10 @@ static int has_epoch_timestamp(const char *nameline)
  * files, we can happily check the index for a match, but for creating a
  * new file we should try to match whatever "patch" does. I have no idea.
  */
-static void parse_traditional_patch(const char *first, const char *second, struct patch *patch)
+static void parse_traditional_patch(struct apply_state *state,
+				    const char *first,
+				    const char *second,
+				    struct patch *patch)
 {
 	char *name;
 
@@ -866,8 +872,8 @@ static void parse_traditional_patch(const char *first, const char *second, struc
 	second += 4;	/* skip "+++ " */
 	if (!p_value_known) {
 		int p, q;
-		p = guess_p_value(first);
-		q = guess_p_value(second);
+		p = guess_p_value(state, first);
+		q = guess_p_value(state, second);
 		if (p < 0) p = q;
 		if (0 <= p && p == q) {
 			state_p_value = p;
@@ -1429,7 +1435,11 @@ static int parse_fragment_header(const char *line, int len, struct fragment *fra
 	return offset;
 }
 
-static int find_header(const char *line, unsigned long size, int *hdrsize, struct patch *patch)
+static int find_header(struct apply_state *state,
+		       const char *line,
+		       unsigned long size,
+		       int *hdrsize,
+		       struct patch *patch)
 {
 	unsigned long offset, len;
 
@@ -1506,7 +1516,7 @@ static int find_header(const char *line, unsigned long size, int *hdrsize, struc
 			continue;
 
 		/* Ok, we'll consider it a patch */
-		parse_traditional_patch(line, line+len, patch);
+		parse_traditional_patch(state, line, line+len, patch);
 		*hdrsize = len + nextlen;
 		state_linenr += 2;
 		return offset;
@@ -1913,21 +1923,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);
 }
 
 /*
@@ -1944,16 +1954,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;
 	}
 
@@ -1980,17 +1990,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
@@ -4367,7 +4377,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 */
@@ -4384,14 +4397,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;
@@ -4517,6 +4530,7 @@ 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;
 
 	const char *whitespace_option = NULL;
 
@@ -4589,15 +4603,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)
@@ -4628,23 +4644,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.2.490.g3dabe57

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

* [PATCH v2 09/94] builtin/apply: move 'state' init into init_apply_state()
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (7 preceding siblings ...)
  2016-05-11 13:16 ` [PATCH v2 08/94] builtin/apply: introduce 'struct apply_state' to start libifying Christian Couder
@ 2016-05-11 13:16 ` Christian Couder
  2016-05-12 19:25   ` Junio C Hamano
  2016-05-11 13:16 ` [PATCH v2 10/94] builtin/apply: move 'unidiff_zero' global into 'struct apply_state' Christian Couder
                   ` (85 subsequent siblings)
  94 siblings, 1 reply; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:16 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

When the apply functionality will be libified, the 'struct apply_state'
will be used by different pieces of code.

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

Helped-by: Eric Sunshine <sunshine@sunshineco.com>
Reviewed-by: Stefan Beller <sbeller@google.com>
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 ae068e7..e133033 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4522,6 +4522,19 @@ static int option_parse_directory(const struct option *opt,
 	return 0;
 }
 
+static void init_apply_state(struct apply_state *state, const char *prefix)
+{
+	memset(state, 0, sizeof(*state));
+	state->prefix = prefix;
+	state->prefix_length = state->prefix ? strlen(state->prefix) : 0;
+
+	git_apply_config();
+	if (apply_default_whitespace)
+		parse_whitespace_option(apply_default_whitespace);
+	if (apply_default_ignorewhitespace)
+		parse_ignorewhitespace_option(apply_default_ignorewhitespace);
+}
+
 int cmd_apply(int argc, const char **argv, const char *prefix_)
 {
 	int i;
@@ -4603,15 +4616,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 		OPT_END()
 	};
 
-	memset(&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);
+	init_apply_state(&state, prefix_);
 
 	argc = parse_options(argc, argv, state.prefix, builtin_apply_options,
 			apply_usage, 0);
-- 
2.8.2.490.g3dabe57

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

* [PATCH v2 10/94] builtin/apply: move 'unidiff_zero' global into 'struct apply_state'
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (8 preceding siblings ...)
  2016-05-11 13:16 ` [PATCH v2 09/94] builtin/apply: move 'state' init into init_apply_state() Christian Couder
@ 2016-05-11 13:16 ` Christian Couder
  2016-05-12 19:28   ` Junio C Hamano
  2016-05-11 13:16 ` [PATCH v2 11/94] builtin/apply: move 'check' " Christian Couder
                   ` (84 subsequent siblings)
  94 siblings, 1 reply; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:16 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify the apply functionality the 'unidiff_zero' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

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

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

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

To libify the apply functionality the 'check' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

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

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

* [PATCH v2 12/94] builtin/apply: move 'check_index' global into 'struct apply_state'
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (10 preceding siblings ...)
  2016-05-11 13:16 ` [PATCH v2 11/94] builtin/apply: move 'check' " Christian Couder
@ 2016-05-11 13:16 ` Christian Couder
  2016-05-11 13:16 ` [PATCH v2 13/94] builtin/apply: move 'apply_in_reverse' " Christian Couder
                   ` (82 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:16 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify the apply functionality the 'check_index' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbeller@google.com>
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 6bf103a..5d8f410 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -30,6 +30,10 @@ struct apply_state {
 	 *    files that are being modified, but doesn't apply the patch
 	 */
 	int check;
+
+	/* --index updates the cache as well. */
+	int check_index;
+
 	int unidiff_zero;
 };
 
@@ -37,14 +41,12 @@ struct apply_state {
  *  --stat does just a diffstat, and doesn't actually apply
  *  --numstat does numeric diffstat, and doesn't actually apply
  *  --index-info shows the old and new index info for paths if available.
- *  --index updates the cache as well.
  *  --cached updates only the cache without ever touching the working tree.
  */
 static int newfd = -1;
 
 static int state_p_value = 1;
 static int p_value_known;
-static int check_index;
 static int update_index;
 static int cached;
 static int diffstat;
@@ -3246,13 +3248,14 @@ static int verify_index_match(const struct cache_entry *ce, struct stat *st)
 
 #define SUBMODULE_PATCH_WITHOUT_INDEX 1
 
-static int load_patch_target(struct strbuf *buf,
+static int load_patch_target(struct apply_state *state,
+			     struct strbuf *buf,
 			     const struct cache_entry *ce,
 			     struct stat *st,
 			     const char *name,
 			     unsigned expected_mode)
 {
-	if (cached || check_index) {
+	if (cached || state->check_index) {
 		if (read_file_or_gitlink(ce, buf))
 			return error(_("read of %s failed"), name);
 	} else if (name) {
@@ -3278,7 +3281,8 @@ static int load_patch_target(struct strbuf *buf,
  * applying a non-git patch that incrementally updates the tree,
  * we read from the result of a previous diff.
  */
-static int load_preimage(struct image *image,
+static int load_preimage(struct apply_state *state,
+			 struct image *image,
 			 struct patch *patch, struct stat *st,
 			 const struct cache_entry *ce)
 {
@@ -3296,7 +3300,7 @@ static int load_preimage(struct image *image,
 		/* We have a patched copy in memory; use that. */
 		strbuf_add(&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;
@@ -3355,7 +3359,9 @@ static int three_way_merge(struct image *image,
  * the current contents of the new_name.  In no cases other than that
  * this function will be called.
  */
-static int load_current(struct image *image, struct patch *patch)
+static int load_current(struct apply_state *state,
+			struct image *image,
+			struct patch *patch)
 {
 	struct strbuf buf = STRBUF_INIT;
 	int status, pos;
@@ -3382,7 +3388,7 @@ static int load_current(struct image *image, struct patch *patch)
 	if (verify_index_match(ce, &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)
@@ -3432,11 +3438,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);
 	}
@@ -3471,7 +3477,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 ||
@@ -3502,7 +3508,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;
@@ -3525,7 +3534,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)
@@ -3575,11 +3584,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;
@@ -3655,7 +3666,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;
@@ -3676,7 +3687,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);
@@ -3691,14 +3702,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;
@@ -3738,7 +3749,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;
@@ -3765,7 +3776,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;
@@ -3820,7 +3831,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);
 
@@ -4432,11 +4443,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"));
 	}
@@ -4577,7 +4588,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")),
@@ -4636,20 +4647,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.2.490.g3dabe57

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

* [PATCH v2 13/94] builtin/apply: move 'apply_in_reverse' global into 'struct apply_state'
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (11 preceding siblings ...)
  2016-05-11 13:16 ` [PATCH v2 12/94] builtin/apply: move 'check_index' " Christian Couder
@ 2016-05-11 13:16 ` Christian Couder
  2016-05-11 13:16 ` [PATCH v2 14/94] builtin/apply: move 'apply_with_reject' " Christian Couder
                   ` (81 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:16 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify the apply functionality the 'apply_in_reverse' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbeller@google.com>
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 5d8f410..73cef9b 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;
@@ -1560,8 +1561,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;
@@ -1601,12 +1605,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++;
@@ -1614,7 +1618,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++;
@@ -1670,7 +1674,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;
@@ -1682,7 +1689,7 @@ static int parse_single_patch(const char *line, unsigned long size, struct patch
 
 		fragment = xcalloc(1, sizeof(*fragment));
 		fragment->linenr = state_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"), state_linenr);
 		fragment->patch = line;
@@ -2012,8 +2019,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";
@@ -2745,7 +2754,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 == '+')
@@ -2918,7 +2927,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).",
@@ -2952,7 +2961,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;
@@ -2965,7 +2976,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'",
@@ -2998,7 +3009,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];
@@ -3059,7 +3072,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);
 
@@ -3082,7 +3095,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++;
@@ -4421,7 +4434,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);
@@ -4614,7 +4627,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.2.490.g3dabe57

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

* [PATCH v2 14/94] builtin/apply: move 'apply_with_reject' global into 'struct apply_state'
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (12 preceding siblings ...)
  2016-05-11 13:16 ` [PATCH v2 13/94] builtin/apply: move 'apply_in_reverse' " Christian Couder
@ 2016-05-11 13:16 ` Christian Couder
  2016-05-11 13:16 ` [PATCH v2 15/94] builtin/apply: move 'apply_verbosely' " Christian Couder
                   ` (80 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:16 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify the apply functionality the 'apply_with_reject' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

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

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

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

To libify the apply functionality the 'apply_verbosely' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

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

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

* [PATCH v2 16/94] builtin/apply: move 'update_index' global into 'struct apply_state'
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (14 preceding siblings ...)
  2016-05-11 13:16 ` [PATCH v2 15/94] builtin/apply: move 'apply_verbosely' " Christian Couder
@ 2016-05-11 13:16 ` Christian Couder
  2016-05-12 19:31   ` Junio C Hamano
  2016-05-11 13:16 ` [PATCH v2 17/94] builtin/apply: move 'allow_overlap' " Christian Couder
                   ` (78 subsequent siblings)
  94 siblings, 1 reply; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:16 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify the apply functionality the 'update_index' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 97af6ea..635a9ff 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -39,6 +39,7 @@ struct apply_state {
 	int check_index;
 
 	int unidiff_zero;
+	int update_index;
 };
 
 /*
@@ -51,7 +52,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;
@@ -4095,9 +4095,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);
 	}
@@ -4108,14 +4108,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);
@@ -4225,13 +4229,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);
@@ -4252,7 +4257,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;
@@ -4264,22 +4269,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;
 	}
 	/*
@@ -4287,9 +4294,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)
@@ -4376,7 +4383,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;
@@ -4456,8 +4463,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) {
@@ -4727,7 +4734,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.2.490.g3dabe57

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

* [PATCH v2 17/94] builtin/apply: move 'allow_overlap' global into 'struct apply_state'
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (15 preceding siblings ...)
  2016-05-11 13:16 ` [PATCH v2 16/94] builtin/apply: move 'update_index' " Christian Couder
@ 2016-05-11 13:16 ` Christian Couder
  2016-05-11 13:16 ` [PATCH v2 18/94] builtin/apply: move 'cached' " Christian Couder
                   ` (77 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:16 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify the apply functionality the 'allow_overlap' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbeller@google.com>
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 635a9ff..8791b28 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -25,6 +25,7 @@ struct apply_state {
 	const char *prefix;
 	int prefix_length;
 
+	int allow_overlap;
 	int apply_in_reverse;
 	int apply_with_reject;
 	int apply_verbosely;
@@ -57,7 +58,6 @@ static int diffstat;
 static int numstat;
 static int summary;
 static int apply = 1;
-static int allow_overlap;
 static int no_add;
 static int threeway;
 static int unsafe_paths;
@@ -2632,7 +2632,8 @@ static void remove_last_line(struct image *img)
  * apply at applied_pos (counts in line numbers) in "img".
  * Update "img" to remove "preimage" and replace it with "postimage".
  */
-static void update_image(struct image *img,
+static void update_image(struct apply_state *state,
+			 struct image *img,
 			 int applied_pos,
 			 struct image *preimage,
 			 struct image *postimage)
@@ -2697,7 +2698,7 @@ static void update_image(struct image *img,
 	memcpy(img->line + applied_pos,
 	       postimage->line,
 	       postimage->nr * sizeof(*img->line));
-	if (!allow_overlap)
+	if (!state->allow_overlap)
 		for (i = 0; i < postimage->nr; i++)
 			img->line[applied_pos + i].flag |= LINE_PATCHED;
 	img->nr = nr;
@@ -2945,7 +2946,7 @@ static int apply_one_fragment(struct apply_state *state,
 			fprintf_ln(stderr, _("Context reduced to (%ld/%ld)"
 					     " to apply fragment at %d"),
 				   leading, trailing, applied_pos+1);
-		update_image(img, applied_pos, &preimage, &postimage);
+		update_image(state, img, applied_pos, &preimage, &postimage);
 	} else {
 		if (state->apply_verbosely)
 			error(_("while searching for:\n%.*s"),
@@ -4640,7 +4641,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 			N_("don't expect at least one line of context")),
 		OPT_BOOL(0, "reject", &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.2.490.g3dabe57

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

* [PATCH v2 18/94] builtin/apply: move 'cached' global into 'struct apply_state'
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (16 preceding siblings ...)
  2016-05-11 13:16 ` [PATCH v2 17/94] builtin/apply: move 'allow_overlap' " Christian Couder
@ 2016-05-11 13:16 ` Christian Couder
  2016-05-12 19:33   ` Junio C Hamano
  2016-05-11 13:16 ` [PATCH v2 19/94] builtin/apply: move 'diffstat' " Christian Couder
                   ` (76 subsequent siblings)
  94 siblings, 1 reply; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:16 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify the apply functionality the 'cached' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

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

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

* [PATCH v2 19/94] builtin/apply: move 'diffstat' global into 'struct apply_state'
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (17 preceding siblings ...)
  2016-05-11 13:16 ` [PATCH v2 18/94] builtin/apply: move 'cached' " Christian Couder
@ 2016-05-11 13:16 ` Christian Couder
  2016-05-11 13:16 ` [PATCH v2 20/94] builtin/apply: move 'numstat' " Christian Couder
                   ` (75 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:16 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify the apply functionality the 'diffstat' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbeller@google.com>
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 09af5dc..43c7198 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
@@ -47,7 +50,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.
  */
@@ -55,7 +57,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;
@@ -4493,7 +4494,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)
@@ -4604,7 +4605,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"),
@@ -4677,7 +4678,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.2.490.g3dabe57

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

* [PATCH v2 20/94] builtin/apply: move 'numstat' global into 'struct apply_state'
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (18 preceding siblings ...)
  2016-05-11 13:16 ` [PATCH v2 19/94] builtin/apply: move 'diffstat' " Christian Couder
@ 2016-05-11 13:16 ` Christian Couder
  2016-05-11 13:16 ` [PATCH v2 21/94] builtin/apply: move 'summary' " Christian Couder
                   ` (74 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:16 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify the apply functionality the 'numstat' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbeller@google.com>
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 43c7198..887c5d0 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
@@ -50,14 +53,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;
@@ -4497,7 +4498,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)
@@ -4609,7 +4610,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")),
@@ -4678,7 +4679,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 	}
 	if (state.apply_with_reject)
 		apply = state.apply_verbosely = 1;
-	if (!force_apply && (state.diffstat || 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.2.490.g3dabe57

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

* [PATCH v2 21/94] builtin/apply: move 'summary' global into 'struct apply_state'
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (19 preceding siblings ...)
  2016-05-11 13:16 ` [PATCH v2 20/94] builtin/apply: move 'numstat' " Christian Couder
@ 2016-05-11 13:16 ` Christian Couder
  2016-05-11 13:16 ` [PATCH v2 22/94] builtin/apply: move 'threeway' " Christian Couder
                   ` (73 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:16 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify the apply functionality the 'summary' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbeller@google.com>
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 887c5d0..6216723 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -39,6 +39,8 @@ struct apply_state {
 	/* --numstat does numeric diffstat, and doesn't actually apply */
 	int numstat;
 
+	int summary;
+
 	/*
 	 *  --check turns on checking that the working tree matches the
 	 *    files that are being modified, but doesn't apply the patch
@@ -59,7 +61,6 @@ static int newfd = -1;
 
 static int state_p_value = 1;
 static int p_value_known;
-static int summary;
 static int apply = 1;
 static int no_add;
 static int threeway;
@@ -4501,7 +4502,7 @@ static int apply_patch(struct apply_state *state,
 	if (state->numstat)
 		numstat_patch_list(list);
 
-	if (summary)
+	if (state->summary)
 		summary_patch_list(list);
 
 	free_patch_list(list);
@@ -4612,7 +4613,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 		OPT_NOOP_NOARG(0, "binary"),
 		OPT_BOOL(0, "numstat", &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")),
@@ -4679,7 +4680,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 	}
 	if (state.apply_with_reject)
 		apply = state.apply_verbosely = 1;
-	if (!force_apply && (state.diffstat || state.numstat || summary || state.check || fake_ancestor))
+	if (!force_apply && (state.diffstat || state.numstat || state.summary || state.check || fake_ancestor))
 		apply = 0;
 	if (state.check_index && is_not_gitdir)
 		die(_("--index outside a repository"));
-- 
2.8.2.490.g3dabe57

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

* [PATCH v2 22/94] builtin/apply: move 'threeway' global into 'struct apply_state'
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (20 preceding siblings ...)
  2016-05-11 13:16 ` [PATCH v2 21/94] builtin/apply: move 'summary' " Christian Couder
@ 2016-05-11 13:16 ` Christian Couder
  2016-05-12 19:41   ` Junio C Hamano
  2016-05-11 13:16 ` [PATCH v2 23/94] builtin/apply: move 'no_add' " Christian Couder
                   ` (72 subsequent siblings)
  94 siblings, 1 reply; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:16 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify the apply functionality the 'threeway' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbeller@google.com>
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 6216723..3650922 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -40,6 +40,7 @@ struct apply_state {
 	int numstat;
 
 	int summary;
+	int threeway;
 
 	/*
 	 *  --check turns on checking that the working tree matches the
@@ -63,7 +64,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';
@@ -3501,7 +3501,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;
@@ -3796,7 +3796,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:
@@ -4625,7 +4625,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")),
@@ -4669,11 +4669,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.2.490.g3dabe57

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

* [PATCH v2 23/94] builtin/apply: move 'no_add' global into 'struct apply_state'
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (21 preceding siblings ...)
  2016-05-11 13:16 ` [PATCH v2 22/94] builtin/apply: move 'threeway' " Christian Couder
@ 2016-05-11 13:16 ` Christian Couder
  2016-05-11 13:16 ` [PATCH v2 24/94] builtin/apply: move 'unsafe_paths' " Christian Couder
                   ` (71 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:16 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify the apply functionality the 'no_add' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbeller@google.com>
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 3650922..d699cd9 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -41,6 +41,7 @@ struct apply_state {
 
 	int summary;
 	int threeway;
+	int no_add;
 
 	/*
 	 *  --check turns on checking that the working tree matches the
@@ -63,7 +64,6 @@ static int newfd = -1;
 static int state_p_value = 1;
 static int p_value_known;
 static int apply = 1;
-static int no_add;
 static int unsafe_paths;
 static const char *fake_ancestor;
 static int line_termination = '\n';
@@ -2792,7 +2792,7 @@ static int apply_one_fragment(struct apply_state *state,
 		/* Fall-through for ' ' */
 		case '+':
 			/* --no-add does not add new lines */
-			if (first == '+' && no_add)
+			if (first == '+' && state->no_add)
 				break;
 
 			start = newlines.len;
@@ -4605,7 +4605,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 		{ OPTION_CALLBACK, 'p', NULL, NULL, N_("num"),
 			N_("remove <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.2.490.g3dabe57

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

* [PATCH v2 24/94] builtin/apply: move 'unsafe_paths' global into 'struct apply_state'
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (22 preceding siblings ...)
  2016-05-11 13:16 ` [PATCH v2 23/94] builtin/apply: move 'no_add' " Christian Couder
@ 2016-05-11 13:16 ` Christian Couder
  2016-05-11 13:16 ` [PATCH v2 25/94] builtin/apply: move 'line_termination' " Christian Couder
                   ` (70 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:16 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify the apply functionality the 'unsafe_paths' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbeller@google.com>
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 d699cd9..9209af4 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -54,6 +54,7 @@ struct apply_state {
 
 	int unidiff_zero;
 	int update_index;
+	int unsafe_paths;
 };
 
 /*
@@ -64,7 +65,6 @@ static int newfd = -1;
 static int state_p_value = 1;
 static int p_value_known;
 static int apply = 1;
-static int unsafe_paths;
 static const char *fake_ancestor;
 static int line_termination = '\n';
 static unsigned int p_context = UINT_MAX;
@@ -3837,7 +3837,7 @@ static int check_patch(struct apply_state *state, struct patch *patch)
 		}
 	}
 
-	if (!unsafe_paths)
+	if (!state->unsafe_paths)
 		die_on_unsafe_path(patch);
 
 	/*
@@ -4621,7 +4621,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 			N_("make sure the patch is applicable to the current index")),
 		OPT_BOOL(0, "cached", &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)")),
@@ -4690,7 +4690,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 		state.check_index = 1;
 	}
 	if (state.check_index)
-		unsafe_paths = 0;
+		state.unsafe_paths = 0;
 
 	for (i = 0; i < argc; i++) {
 		const char *arg = argv[i];
-- 
2.8.2.490.g3dabe57

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

* [PATCH v2 25/94] builtin/apply: move 'line_termination' global into 'struct apply_state'
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (23 preceding siblings ...)
  2016-05-11 13:16 ` [PATCH v2 24/94] builtin/apply: move 'unsafe_paths' " Christian Couder
@ 2016-05-11 13:16 ` Christian Couder
  2016-05-11 13:16 ` [PATCH v2 26/94] builtin/apply: move 'fake_ancestor' " Christian Couder
                   ` (69 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:16 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify the apply functionality the 'line_termination' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 9209af4..d699d1e 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -55,6 +55,7 @@ struct apply_state {
 	int unidiff_zero;
 	int update_index;
 	int unsafe_paths;
+	int line_termination;
 };
 
 /*
@@ -66,7 +67,6 @@ static int state_p_value = 1;
 static int p_value_known;
 static int apply = 1;
 static const char *fake_ancestor;
-static int line_termination = '\n';
 static unsigned int p_context = UINT_MAX;
 static const char * const apply_usage[] = {
 	N_("git apply [<options>] [<patch>...]"),
@@ -3987,7 +3987,8 @@ static void stat_patch_list(struct patch *patch)
 	print_stat_summary(stdout, files, adds, dels);
 }
 
-static void numstat_patch_list(struct patch *patch)
+static void numstat_patch_list(struct apply_state *state,
+			       struct patch *patch)
 {
 	for ( ; patch; patch = patch->next) {
 		const char *name;
@@ -3996,7 +3997,7 @@ static void numstat_patch_list(struct patch *patch)
 			printf("-\t-\t");
 		else
 			printf("%d\t%d\t", patch->lines_added, patch->lines_deleted);
-		write_name_quoted(name, stdout, line_termination);
+		write_name_quoted(name, stdout, state->line_termination);
 	}
 }
 
@@ -4500,7 +4501,7 @@ static int apply_patch(struct apply_state *state,
 		stat_patch_list(list);
 
 	if (state->numstat)
-		numstat_patch_list(list);
+		numstat_patch_list(state, list);
 
 	if (state->summary)
 		summary_patch_list(list);
@@ -4575,6 +4576,7 @@ static void init_apply_state(struct apply_state *state, const char *prefix)
 	memset(state, 0, sizeof(*state));
 	state->prefix = prefix;
 	state->prefix_length = state->prefix ? strlen(state->prefix) : 0;
+	state->line_termination = '\n';
 
 	git_apply_config();
 	if (apply_default_whitespace)
@@ -4630,7 +4632,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 		OPT_FILENAME(0, "build-fake-ancestor", &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")),
-- 
2.8.2.490.g3dabe57

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

* [PATCH v2 26/94] builtin/apply: move 'fake_ancestor' global into 'struct apply_state'
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (24 preceding siblings ...)
  2016-05-11 13:16 ` [PATCH v2 25/94] builtin/apply: move 'line_termination' " Christian Couder
@ 2016-05-11 13:16 ` Christian Couder
  2016-05-11 13:16 ` [PATCH v2 27/94] builtin/apply: move 'p_context' " Christian Couder
                   ` (68 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:16 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify the apply functionality the 'fake_ancestor' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

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

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

diff --git a/builtin/apply.c b/builtin/apply.c
index d699d1e..6f9a090 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -42,6 +42,7 @@ struct apply_state {
 	int summary;
 	int threeway;
 	int no_add;
+	const char *fake_ancestor;
 
 	/*
 	 *  --check turns on checking that the working tree matches the
@@ -58,15 +59,11 @@ struct apply_state {
 	int line_termination;
 };
 
-/*
- *  --index-info shows the old and new index info for paths if available.
- */
 static int newfd = -1;
 
 static int state_p_value = 1;
 static int p_value_known;
 static int apply = 1;
-static const char *fake_ancestor;
 static unsigned int p_context = UINT_MAX;
 static const char * const apply_usage[] = {
 	N_("git apply [<options>] [<patch>...]"),
@@ -4494,8 +4491,8 @@ static int apply_patch(struct apply_state *state,
 		return 1;
 	}
 
-	if (fake_ancestor)
-		build_fake_ancestor(list, fake_ancestor);
+	if (state->fake_ancestor)
+		build_fake_ancestor(list, state->fake_ancestor);
 
 	if (state->diffstat)
 		stat_patch_list(list);
@@ -4629,7 +4626,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 			N_("also apply the patch (use with --stat/--summary/--check)")),
 		OPT_BOOL('3', "3way", &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,
@@ -4682,7 +4679,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 	}
 	if (state.apply_with_reject)
 		apply = state.apply_verbosely = 1;
-	if (!force_apply && (state.diffstat || state.numstat || state.summary || state.check || fake_ancestor))
+	if (!force_apply && (state.diffstat || state.numstat || state.summary || state.check || state.fake_ancestor))
 		apply = 0;
 	if (state.check_index && is_not_gitdir)
 		die(_("--index outside a repository"));
-- 
2.8.2.490.g3dabe57

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

* [PATCH v2 27/94] builtin/apply: move 'p_context' global into 'struct apply_state'
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (25 preceding siblings ...)
  2016-05-11 13:16 ` [PATCH v2 26/94] builtin/apply: move 'fake_ancestor' " Christian Couder
@ 2016-05-11 13:16 ` Christian Couder
  2016-05-11 13:16 ` [PATCH v2 28/94] builtin/apply: move 'apply' " Christian Couder
                   ` (67 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:16 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify the apply functionality the 'p_context' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbeller@google.com>
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 6f9a090..2ba2b21 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -57,6 +57,8 @@ struct apply_state {
 	int update_index;
 	int unsafe_paths;
 	int line_termination;
+
+	unsigned int p_context;
 };
 
 static int newfd = -1;
@@ -64,7 +66,6 @@ static int newfd = -1;
 static int state_p_value = 1;
 static int p_value_known;
 static int apply = 1;
-static unsigned int p_context = UINT_MAX;
 static const char * const apply_usage[] = {
 	N_("git apply [<options>] [<patch>...]"),
 	NULL
@@ -2880,7 +2881,7 @@ static int apply_one_fragment(struct apply_state *state,
 			break;
 
 		/* Am I at my context limits? */
-		if ((leading <= p_context) && (trailing <= p_context))
+		if ((leading <= state->p_context) && (trailing <= state->p_context))
 			break;
 		if (match_beginning || match_end) {
 			match_beginning = match_end = 0;
@@ -4574,6 +4575,7 @@ static void init_apply_state(struct apply_state *state, const char *prefix)
 	state->prefix = prefix;
 	state->prefix_length = state->prefix ? strlen(state->prefix) : 0;
 	state->line_termination = '\n';
+	state->p_context = UINT_MAX;
 
 	git_apply_config();
 	if (apply_default_whitespace)
@@ -4631,7 +4633,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 		/* Think twice before adding "--nul" synonym to this */
 		OPT_SET_INT('z', NULL, &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"),
-- 
2.8.2.490.g3dabe57

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

* [PATCH v2 28/94] builtin/apply: move 'apply' global into 'struct apply_state'
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (26 preceding siblings ...)
  2016-05-11 13:16 ` [PATCH v2 27/94] builtin/apply: move 'p_context' " Christian Couder
@ 2016-05-11 13:16 ` Christian Couder
  2016-05-11 13:16 ` [PATCH v2 29/94] builtin/apply: move 'patch_input_file' " Christian Couder
                   ` (66 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:16 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify the apply functionality the 'apply' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

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

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

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

To libify the apply functionality the 'patch_input_file' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

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

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

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

* [PATCH v2 30/94] builtin/apply: move 'limit_by_name' global into 'struct apply_state'
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (28 preceding siblings ...)
  2016-05-11 13:16 ` [PATCH v2 29/94] builtin/apply: move 'patch_input_file' " Christian Couder
@ 2016-05-11 13:16 ` Christian Couder
  2016-05-11 13:16 ` [PATCH v2 31/94] builtin/apply: move 'has_include' " Christian Couder
                   ` (64 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:16 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify the apply functionality the 'limit_by_name' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

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

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

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

* [PATCH v2 31/94] builtin/apply: move 'has_include' global into 'struct apply_state'
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (29 preceding siblings ...)
  2016-05-11 13:16 ` [PATCH v2 30/94] builtin/apply: move 'limit_by_name' " Christian Couder
@ 2016-05-11 13:16 ` Christian Couder
  2016-05-11 13:16 ` [PATCH v2 32/94] builtin/apply: move 'p_value' " Christian Couder
                   ` (63 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:16 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify the apply functionality the 'has_include' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

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

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

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

To libify the apply functionality the 'p_value' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbeller@google.com>
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 f2ee8bf..4e476d5 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -62,12 +62,12 @@ struct apply_state {
 	int unsafe_paths;
 	int line_termination;
 
+	int p_value;
 	unsigned int p_context;
 };
 
 static int newfd = -1;
 
-static int state_p_value = 1;
 static int p_value_known;
 
 static const char * const apply_usage[] = {
@@ -888,24 +888,24 @@ static void parse_traditional_patch(struct apply_state *state,
 		q = guess_p_value(state, second);
 		if (p < 0) p = q;
 		if (0 <= p && p == q) {
-			state_p_value = p;
+			state->p_value = p;
 			p_value_known = 1;
 		}
 	}
 	if (is_dev_null(first)) {
 		patch->is_new = 1;
 		patch->is_delete = 0;
-		name = find_name_traditional(second, NULL, state_p_value);
+		name = find_name_traditional(second, NULL, state->p_value);
 		patch->new_name = name;
 	} else if (is_dev_null(second)) {
 		patch->is_new = 0;
 		patch->is_delete = 1;
-		name = find_name_traditional(first, NULL, state_p_value);
+		name = find_name_traditional(first, NULL, state->p_value);
 		patch->old_name = name;
 	} else {
 		char *first_name;
-		first_name = find_name_traditional(first, NULL, state_p_value);
-		name = find_name_traditional(second, first_name, state_p_value);
+		first_name = find_name_traditional(first, NULL, state->p_value);
+		name = find_name_traditional(second, first_name, state->p_value);
 		free(first_name);
 		if (has_epoch_timestamp(first)) {
 			patch->is_new = 1;
@@ -924,7 +924,9 @@ static void parse_traditional_patch(struct apply_state *state,
 		die(_("unable to find filename in patch at line %d"), state_linenr);
 }
 
-static int gitdiff_hdrend(const char *line, struct patch *patch)
+static int gitdiff_hdrend(struct apply_state *state,
+			  const char *line,
+			  struct patch *patch)
 {
 	return -1;
 }
@@ -941,10 +943,14 @@ static int gitdiff_hdrend(const char *line, struct patch *patch)
 #define DIFF_OLD_NAME 0
 #define DIFF_NEW_NAME 1
 
-static void gitdiff_verify_name(const char *line, int isnull, char **name, int side)
+static void gitdiff_verify_name(struct apply_state *state,
+				const char *line,
+				int isnull,
+				char **name,
+				int side)
 {
 	if (!*name && !isnull) {
-		*name = find_name(line, NULL, state_p_value, TERM_TAB);
+		*name = find_name(line, NULL, state->p_value, TERM_TAB);
 		return;
 	}
 
@@ -954,7 +960,7 @@ static void gitdiff_verify_name(const char *line, int isnull, char **name, int s
 		if (isnull)
 			die(_("git apply: bad git-diff - expected /dev/null, got %s on line %d"),
 			    *name, state_linenr);
-		another = find_name(line, NULL, state_p_value, TERM_TAB);
+		another = find_name(line, NULL, state->p_value, TERM_TAB);
 		if (!another || memcmp(another, *name, len + 1))
 			die((side == DIFF_NEW_NAME) ?
 			    _("git apply: bad git-diff - inconsistent new filename on line %d") :
@@ -967,81 +973,105 @@ static void gitdiff_verify_name(const char *line, int isnull, char **name, int s
 	}
 }
 
-static int gitdiff_oldname(const char *line, struct patch *patch)
+static int gitdiff_oldname(struct apply_state *state,
+			   const char *line,
+			   struct patch *patch)
 {
-	gitdiff_verify_name(line, patch->is_new, &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)
@@ -1049,7 +1079,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)
@@ -1057,7 +1089,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,
@@ -1094,7 +1128,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;
 }
@@ -1103,15 +1139,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)
@@ -1128,7 +1166,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;
@@ -1146,7 +1186,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);
@@ -1163,7 +1203,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 */
@@ -1174,7 +1214,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 ||
@@ -1189,7 +1229,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;
 
@@ -1205,7 +1245,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;
 
@@ -1249,7 +1289,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;
@@ -1265,7 +1305,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;
 
@@ -1279,7 +1323,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);
@@ -1292,7 +1336,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, state_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 },
@@ -1322,7 +1366,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;
 		}
@@ -1492,7 +1536,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) {
@@ -1501,8 +1545,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, state_linenr);
+					       state->p_value),
+					    state->p_value, state_linenr);
 				patch->old_name = xstrdup(patch->def_name);
 				patch->new_name = xstrdup(patch->def_name);
 			}
@@ -4546,9 +4590,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;
 }
@@ -4589,6 +4635,7 @@ static void init_apply_state(struct apply_state *state, 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();
@@ -4617,7 +4664,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,
-- 
2.8.2.490.g3dabe57

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

* [PATCH v2 33/94] builtin/apply: move 'p_value_known' global into 'struct apply_state'
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (31 preceding siblings ...)
  2016-05-11 13:16 ` [PATCH v2 32/94] builtin/apply: move 'p_value' " Christian Couder
@ 2016-05-11 13:16 ` Christian Couder
  2016-05-12 19:43   ` Junio C Hamano
  2016-05-11 13:16 ` [PATCH v2 34/94] builtin/apply: move 'root' " Christian Couder
                   ` (61 subsequent siblings)
  94 siblings, 1 reply; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:16 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify the apply functionality the 'p_value_known' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

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

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

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

To libify the apply functionality the 'root' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 30eea9c..6b3540f 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -47,6 +47,7 @@ struct apply_state {
 	const char *patch_input_file;
 	struct string_list limit_by_name;
 	int has_include;
+	struct strbuf root;
 
 	/*
 	 *  --check turns on checking that the working tree matches the
@@ -90,8 +91,6 @@ static enum ws_ignore {
 } ws_ignore_action = ignore_ws_none;
 
 
-static struct strbuf root = STRBUF_INIT;
-
 static void parse_whitespace_option(const char *option)
 {
 	if (!option) {
@@ -481,7 +480,10 @@ static char *squash_slash(char *name)
 	return name;
 }
 
-static char *find_name_gnu(const char *line, const char *def, int p_value)
+static char *find_name_gnu(struct apply_state *state,
+			   const char *line,
+			   const char *def,
+			   int p_value)
 {
 	struct strbuf name = STRBUF_INIT;
 	char *cp;
@@ -505,8 +507,8 @@ static char *find_name_gnu(const char *line, const char *def, int p_value)
 	}
 
 	strbuf_remove(&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));
 }
 
@@ -669,8 +671,12 @@ static size_t diff_timestamp_len(const char *line, size_t len)
 	return line + len - end;
 }
 
-static char *find_name_common(const char *line, const char *def,
-			      int p_value, const char *end, int terminate)
+static char *find_name_common(struct apply_state *state,
+			      const char *line,
+			      const char *def,
+			      int p_value,
+			      const char *end,
+			      int terminate)
 {
 	int len;
 	const char *start = NULL;
@@ -708,32 +714,39 @@ static char *find_name_common(const char *line, const char *def,
 			return squash_slash(xstrdup(def));
 	}
 
-	if (root.len) {
-		char *ret = xstrfmt("%s%.*s", root.buf, len, start);
+	if (state->root.len) {
+		char *ret = xstrfmt("%s%.*s", state->root.buf, len, start);
 		return squash_slash(ret);
 	}
 
 	return squash_slash(xmemdupz(start, len));
 }
 
-static char *find_name(const char *line, char *def, int p_value, int terminate)
+static char *find_name(struct apply_state *state,
+		       const char *line,
+		       char *def,
+		       int p_value,
+		       int terminate)
 {
 	if (*line == '"') {
-		char *name = find_name_gnu(line, def, p_value);
+		char *name = find_name_gnu(state, line, def, p_value);
 		if (name)
 			return name;
 	}
 
-	return find_name_common(line, def, p_value, NULL, terminate);
+	return find_name_common(state, line, def, p_value, NULL, terminate);
 }
 
-static char *find_name_traditional(const char *line, char *def, int p_value)
+static char *find_name_traditional(struct apply_state *state,
+				   const char *line,
+				   char *def,
+				   int p_value)
 {
 	size_t len;
 	size_t date_len;
 
 	if (*line == '"') {
-		char *name = find_name_gnu(line, def, p_value);
+		char *name = find_name_gnu(state, line, def, p_value);
 		if (name)
 			return name;
 	}
@@ -741,10 +754,10 @@ static char *find_name_traditional(const char *line, char *def, int p_value)
 	len = strchrnul(line, '\n') - line;
 	date_len = diff_timestamp_len(line, len);
 	if (!date_len)
-		return find_name_common(line, def, p_value, NULL, TERM_TAB);
+		return find_name_common(state, line, def, p_value, NULL, TERM_TAB);
 	len -= date_len;
 
-	return find_name_common(line, def, p_value, line + len, 0);
+	return find_name_common(state, line, def, p_value, line + len, 0);
 }
 
 static int count_slashes(const char *cp)
@@ -769,7 +782,7 @@ static int guess_p_value(struct apply_state *state, const char *nameline)
 
 	if (is_dev_null(nameline))
 		return -1;
-	name = find_name_traditional(nameline, NULL, 0);
+	name = find_name_traditional(state, nameline, NULL, 0);
 	if (!name)
 		return -1;
 	cp = strchr(name, '/');
@@ -894,17 +907,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;
@@ -949,7 +962,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;
 	}
 
@@ -959,7 +972,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, state_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") :
@@ -1034,7 +1047,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;
 }
 
@@ -1044,7 +1057,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;
 }
 
@@ -1054,7 +1067,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;
 }
 
@@ -1064,7 +1077,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;
 }
 
@@ -1323,8 +1336,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;
 	}
@@ -4621,9 +4634,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;
 }
 
@@ -4636,6 +4650,7 @@ static void init_apply_state(struct apply_state *state, const char *prefix)
 	state->line_termination = '\n';
 	state->p_value = 1;
 	state->p_context = UINT_MAX;
+	strbuf_init(&state->root, 0);
 
 	git_apply_config();
 	if (apply_default_whitespace)
@@ -4719,7 +4734,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()
-- 
2.8.2.490.g3dabe57

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

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

To libify the apply functionality the 'whitespace_error' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

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

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

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

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

Reviewed-by: Stefan Beller <sbeller@google.com>
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 1684f25..4b9a5ff 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -67,6 +67,7 @@ struct apply_state {
 	int p_value_known;
 	unsigned int p_context;
 
+	const char *whitespace_option;
 	int whitespace_error;
 };
 
@@ -4625,9 +4626,9 @@ static int option_parse_space_change(const struct option *opt,
 static int option_parse_whitespace(const struct option *opt,
 				   const char *arg, int unset)
 {
-	const char **whitespace_option = opt->value;
+	struct apply_state *state = opt->value;
 
-	*whitespace_option = arg;
+	state->whitespace_option = arg;
 	parse_whitespace_option(arg);
 	return 0;
 }
@@ -4670,8 +4671,6 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 	int read_stdin = 1;
 	struct apply_state state;
 
-	const char *whitespace_option = NULL;
-
 	struct option builtin_apply_options[] = {
 		{ OPTION_CALLBACK, 0, "exclude", &state, N_("path"),
 			N_("don't apply changes matching the given path"),
@@ -4711,7 +4710,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 			N_("paths are separated with NUL character"), '\0'),
 		OPT_INTEGER('C', NULL, &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,
@@ -4786,11 +4785,11 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 		if (fd < 0)
 			die_errno(_("can't open patch '%s'"), arg);
 		read_stdin = 0;
-		set_default_whitespace_mode(&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.2.490.g3dabe57

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

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

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

Reviewed-by: Stefan Beller <sbeller@google.com>
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 4b9a5ff..ab954b4 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -138,10 +138,9 @@ static void parse_ignorewhitespace_option(const char *option)
 	die(_("unrecognized whitespace ignore option '%s'"), option);
 }
 
-static void set_default_whitespace_mode(struct apply_state *state,
-					const char *whitespace_option)
+static void set_default_whitespace_mode(struct apply_state *state)
 {
-	if (!whitespace_option && !apply_default_whitespace)
+	if (!state->whitespace_option && !apply_default_whitespace)
 		ws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error);
 }
 
@@ -4785,11 +4784,11 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 		if (fd < 0)
 			die_errno(_("can't open patch '%s'"), arg);
 		read_stdin = 0;
-		set_default_whitespace_mode(&state, 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.2.490.g3dabe57

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

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

To libify the apply functionality the 'squelch_whitespace_errors' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbeller@google.com>
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 ab954b4..f84e301 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -69,6 +69,7 @@ struct apply_state {
 
 	const char *whitespace_option;
 	int whitespace_error;
+	int squelch_whitespace_errors;
 };
 
 static int newfd = -1;
@@ -84,7 +85,6 @@ static enum ws_error_action {
 	die_on_ws_error,
 	correct_ws_error
 } ws_error_action = warn_on_ws_error;
-static int squelch_whitespace_errors = 5;
 static int applied_after_fixing_ws;
 
 static enum ws_ignore {
@@ -93,7 +93,7 @@ static enum ws_ignore {
 } ws_ignore_action = ignore_ws_none;
 
 
-static void parse_whitespace_option(const char *option)
+static void parse_whitespace_option(struct apply_state *state, const char *option)
 {
 	if (!option) {
 		ws_error_action = warn_on_ws_error;
@@ -113,7 +113,7 @@ static void parse_whitespace_option(const char *option)
 	}
 	if (!strcmp(option, "error-all")) {
 		ws_error_action = die_on_ws_error;
-		squelch_whitespace_errors = 0;
+		state->squelch_whitespace_errors = 0;
 		return;
 	}
 	if (!strcmp(option, "strip") || !strcmp(option, "fix")) {
@@ -1605,8 +1605,8 @@ static void record_ws_error(struct apply_state *state,
 		return;
 
 	state->whitespace_error++;
-	if (squelch_whitespace_errors &&
-	    squelch_whitespace_errors < state->whitespace_error)
+	if (state->squelch_whitespace_errors &&
+	    state->squelch_whitespace_errors < state->whitespace_error)
 		return;
 
 	err = whitespace_error_string(result);
@@ -4626,9 +4626,8 @@ static int option_parse_whitespace(const struct option *opt,
 				   const char *arg, int unset)
 {
 	struct apply_state *state = opt->value;
-
 	state->whitespace_option = arg;
-	parse_whitespace_option(arg);
+	parse_whitespace_option(state, arg);
 	return 0;
 }
 
@@ -4651,11 +4650,12 @@ static void init_apply_state(struct apply_state *state, const char *prefix)
 	state->line_termination = '\n';
 	state->p_value = 1;
 	state->p_context = UINT_MAX;
+	state->squelch_whitespace_errors = 5;
 	strbuf_init(&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);
 }
@@ -4792,10 +4792,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.2.490.g3dabe57

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

* [PATCH v2 39/94] builtin/apply: move 'applied_after_fixing_ws' into 'struct apply_state'
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (37 preceding siblings ...)
  2016-05-11 13:16 ` [PATCH v2 38/94] builtin/apply: move 'squelch_whitespace_errors' into 'struct apply_state' Christian Couder
@ 2016-05-11 13:16 ` Christian Couder
  2016-05-11 13:16 ` [PATCH v2 40/94] builtin/apply: move 'ws_error_action' " Christian Couder
                   ` (55 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:16 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify the apply functionality the 'applied_after_fixing_ws' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbeller@google.com>
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 f84e301..e68fd2c 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -70,6 +70,7 @@ struct apply_state {
 	const char *whitespace_option;
 	int whitespace_error;
 	int squelch_whitespace_errors;
+	int applied_after_fixing_ws;
 };
 
 static int newfd = -1;
@@ -85,7 +86,6 @@ static enum ws_error_action {
 	die_on_ws_error,
 	correct_ws_error
 } ws_error_action = warn_on_ws_error;
-static int applied_after_fixing_ws;
 
 static enum ws_ignore {
 	ignore_ws_none,
@@ -2868,7 +2868,7 @@ static int apply_one_fragment(struct apply_state *state,
 				strbuf_add(&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));
@@ -4806,11 +4806,11 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 			       "%d lines add whitespace errors.",
 			       state.whitespace_error),
 			    state.whitespace_error);
-		if (applied_after_fixing_ws && state.apply)
+		if (state.applied_after_fixing_ws && state.apply)
 			warning("%d line%s applied after"
 				" fixing whitespace errors.",
-				applied_after_fixing_ws,
-				applied_after_fixing_ws == 1 ? "" : "s");
+				state.applied_after_fixing_ws,
+				state.applied_after_fixing_ws == 1 ? "" : "s");
 		else if (state.whitespace_error)
 			warning(Q_("%d line adds whitespace errors.",
 				   "%d lines add whitespace errors.",
-- 
2.8.2.490.g3dabe57

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

* [PATCH v2 40/94] builtin/apply: move 'ws_error_action' into 'struct apply_state'
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (38 preceding siblings ...)
  2016-05-11 13:16 ` [PATCH v2 39/94] builtin/apply: move 'applied_after_fixing_ws' " Christian Couder
@ 2016-05-11 13:16 ` Christian Couder
  2016-05-12 19:48   ` Junio C Hamano
  2016-05-11 13:16 ` [PATCH v2 41/94] builtin/apply: move 'ws_ignore_action' " Christian Couder
                   ` (54 subsequent siblings)
  94 siblings, 1 reply; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:16 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify the apply functionality the 'ws_error_action' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Signed-off-by: Christian Couder <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 e68fd2c..10d45c7 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -21,6 +21,13 @@
 #include "ll-merge.h"
 #include "rerere.h"
 
+enum ws_error_action {
+	nowarn_ws_error,
+	warn_on_ws_error,
+	die_on_ws_error,
+	correct_ws_error
+};
+
 struct apply_state {
 	const char *prefix;
 	int prefix_length;
@@ -71,6 +78,8 @@ struct apply_state {
 	int whitespace_error;
 	int squelch_whitespace_errors;
 	int applied_after_fixing_ws;
+
+	enum ws_error_action ws_error_action;
 };
 
 static int newfd = -1;
@@ -80,12 +89,6 @@ static const char * const apply_usage[] = {
 	NULL
 };
 
-static enum ws_error_action {
-	nowarn_ws_error,
-	warn_on_ws_error,
-	die_on_ws_error,
-	correct_ws_error
-} ws_error_action = warn_on_ws_error;
 
 static enum ws_ignore {
 	ignore_ws_none,
@@ -96,28 +99,28 @@ static enum ws_ignore {
 static void parse_whitespace_option(struct apply_state *state, const char *option)
 {
 	if (!option) {
-		ws_error_action = warn_on_ws_error;
+		state->ws_error_action = warn_on_ws_error;
 		return;
 	}
 	if (!strcmp(option, "warn")) {
-		ws_error_action = warn_on_ws_error;
+		state->ws_error_action = warn_on_ws_error;
 		return;
 	}
 	if (!strcmp(option, "nowarn")) {
-		ws_error_action = nowarn_ws_error;
+		state->ws_error_action = nowarn_ws_error;
 		return;
 	}
 	if (!strcmp(option, "error")) {
-		ws_error_action = die_on_ws_error;
+		state->ws_error_action = die_on_ws_error;
 		return;
 	}
 	if (!strcmp(option, "error-all")) {
-		ws_error_action = die_on_ws_error;
+		state->ws_error_action = die_on_ws_error;
 		state->squelch_whitespace_errors = 0;
 		return;
 	}
 	if (!strcmp(option, "strip") || !strcmp(option, "fix")) {
-		ws_error_action = correct_ws_error;
+		state->ws_error_action = correct_ws_error;
 		return;
 	}
 	die(_("unrecognized whitespace option '%s'"), option);
@@ -141,7 +144,7 @@ static void parse_ignorewhitespace_option(const char *option)
 static void set_default_whitespace_mode(struct apply_state *state)
 {
 	if (!state->whitespace_option && !apply_default_whitespace)
-		ws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error);
+		state->ws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error);
 }
 
 /*
@@ -1676,12 +1679,12 @@ static int parse_fragment(struct apply_state *state,
 				leading++;
 			trailing++;
 			if (!state->apply_in_reverse &&
-			    ws_error_action == correct_ws_error)
+			    state->ws_error_action == correct_ws_error)
 				check_whitespace(state, line, len, patch->ws_rule);
 			break;
 		case '-':
 			if (state->apply_in_reverse &&
-			    ws_error_action != nowarn_ws_error)
+			    state->ws_error_action != nowarn_ws_error)
 				check_whitespace(state, line, len, patch->ws_rule);
 			deleted++;
 			oldlines--;
@@ -1689,7 +1692,7 @@ static int parse_fragment(struct apply_state *state,
 			break;
 		case '+':
 			if (!state->apply_in_reverse &&
-			    ws_error_action != nowarn_ws_error)
+			    state->ws_error_action != nowarn_ws_error)
 				check_whitespace(state, line, len, patch->ws_rule);
 			added++;
 			newlines--;
@@ -2402,7 +2405,8 @@ static int line_by_line_fuzzy_match(struct image *img,
 	return 1;
 }
 
-static int match_fragment(struct image *img,
+static int match_fragment(struct apply_state *state,
+			  struct image *img,
 			  struct image *preimage,
 			  struct image *postimage,
 			  unsigned long try,
@@ -2423,7 +2427,7 @@ static int match_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
@@ -2495,7 +2499,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;
 
 	/*
@@ -2607,7 +2611,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,
@@ -2651,7 +2656,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;
@@ -2864,7 +2869,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 {
@@ -2942,7 +2947,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)
@@ -2978,10 +2983,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);
 			}
@@ -2992,7 +2997,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;
 		}
 
@@ -4536,7 +4541,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;
@@ -4651,6 +4656,7 @@ static void init_apply_state(struct apply_state *state, 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();
@@ -4801,7 +4807,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.2.490.g3dabe57

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

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

To libify the apply functionality the 'ws_ignore_action' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbeller@google.com>
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 10d45c7..f8c3677 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -28,6 +28,12 @@ enum ws_error_action {
 	correct_ws_error
 };
 
+
+enum ws_ignore {
+	ignore_ws_none,
+	ignore_ws_change
+};
+
 struct apply_state {
 	const char *prefix;
 	int prefix_length;
@@ -80,6 +86,7 @@ struct apply_state {
 	int applied_after_fixing_ws;
 
 	enum ws_error_action ws_error_action;
+	enum ws_ignore ws_ignore_action;
 };
 
 static int newfd = -1;
@@ -89,13 +96,6 @@ static const char * const apply_usage[] = {
 	NULL
 };
 
-
-static enum ws_ignore {
-	ignore_ws_none,
-	ignore_ws_change
-} ws_ignore_action = ignore_ws_none;
-
-
 static void parse_whitespace_option(struct apply_state *state, const char *option)
 {
 	if (!option) {
@@ -126,16 +126,17 @@ static void parse_whitespace_option(struct apply_state *state, const char *optio
 	die(_("unrecognized whitespace option '%s'"), option);
 }
 
-static void parse_ignorewhitespace_option(const char *option)
+static void parse_ignorewhitespace_option(struct apply_state *state,
+					  const char *option)
 {
 	if (!option || !strcmp(option, "no") ||
 	    !strcmp(option, "false") || !strcmp(option, "never") ||
 	    !strcmp(option, "none")) {
-		ws_ignore_action = ignore_ws_none;
+		state->ws_ignore_action = ignore_ws_none;
 		return;
 	}
 	if (!strcmp(option, "change")) {
-		ws_ignore_action = ignore_ws_change;
+		state->ws_ignore_action = ignore_ws_change;
 		return;
 	}
 	die(_("unrecognized whitespace ignore option '%s'"), option);
@@ -2495,7 +2496,7 @@ static int match_fragment(struct apply_state *state,
 	 * fuzzy matching. We collect all the line length information because
 	 * we need it to adjust whitespace if we match.
 	 */
-	if (ws_ignore_action == ignore_ws_change)
+	if (state->ws_ignore_action == ignore_ws_change)
 		return line_by_line_fuzzy_match(img, preimage, postimage,
 						try, try_lno, preimage_limit);
 
@@ -4618,12 +4619,13 @@ static int option_parse_p(const struct option *opt,
 }
 
 static int option_parse_space_change(const struct option *opt,
-			  const char *arg, int unset)
+				     const char *arg, int unset)
 {
+	struct apply_state *state = opt->value;
 	if (unset)
-		ws_ignore_action = ignore_ws_none;
+		state->ws_ignore_action = ignore_ws_none;
 	else
-		ws_ignore_action = ignore_ws_change;
+		state->ws_ignore_action = ignore_ws_change;
 	return 0;
 }
 
@@ -4657,13 +4659,14 @@ static void init_apply_state(struct apply_state *state, const char *prefix)
 	state->p_context = UINT_MAX;
 	state->squelch_whitespace_errors = 5;
 	state->ws_error_action = warn_on_ws_error;
+	state->ws_ignore_action = ignore_ws_none;
 	strbuf_init(&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);
 }
 
 int cmd_apply(int argc, const char **argv, const char *prefix_)
@@ -4718,10 +4721,10 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 		{ OPTION_CALLBACK, 0, "whitespace", &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,
-- 
2.8.2.490.g3dabe57

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

* [PATCH v2 42/94] builtin/apply: move 'max_change' and 'max_len' into 'struct apply_state'
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (40 preceding siblings ...)
  2016-05-11 13:16 ` [PATCH v2 41/94] builtin/apply: move 'ws_ignore_action' " Christian Couder
@ 2016-05-11 13:16 ` Christian Couder
  2016-05-11 13:16 ` [PATCH v2 43/94] builtin/apply: move 'state_linenr' global " Christian Couder
                   ` (52 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:16 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify the apply functionality the 'max_change' and 'max_len'
variables should not be static and global to the file. Let's move
them into 'struct apply_state'.

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

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

* [PATCH v2 43/94] builtin/apply: move 'state_linenr' global into 'struct apply_state'
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (41 preceding siblings ...)
  2016-05-11 13:16 ` [PATCH v2 42/94] builtin/apply: move 'max_change' and 'max_len' " Christian Couder
@ 2016-05-11 13:16 ` Christian Couder
  2016-05-11 13:16 ` [PATCH v2 44/94] builtin/apply: move 'fn_table' " Christian Couder
                   ` (51 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:16 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify the apply functionality the 'state_linenr' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

Reviewed-by: Stefan Beller <sbeller@google.com>
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 deba14c..5c003a1 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -84,6 +84,13 @@ struct apply_state {
 	int max_change;
 	int max_len;
 
+	/*
+	 * Various "current state", notably line numbers and what
+	 * file (and how) we're patching right now.. The "is_xxxx"
+	 * things are flags, where -1 means "don't know yet".
+	 */
+	int linenr;
+
 	int p_value;
 	int p_value_known;
 	unsigned int p_context;
@@ -156,13 +163,6 @@ static void set_default_whitespace_mode(struct apply_state *state)
 		state->ws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error);
 }
 
-/*
- * Various "current state", notably line numbers and what
- * file (and how) we're patching right now.. The "is_xxxx"
- * things are flags, where -1 means "don't know yet".
- */
-static int state_linenr = 1;
-
 /*
  * This represents one "hunk" from a patch, starting with
  * "@@ -oldpos,oldlines +newpos,newlines @@" marker.  The
@@ -939,7 +939,7 @@ static void parse_traditional_patch(struct apply_state *state,
 		}
 	}
 	if (!name)
-		die(_("unable to find filename in patch at line %d"), state_linenr);
+		die(_("unable to find filename in patch at line %d"), state->linenr);
 }
 
 static int gitdiff_hdrend(struct apply_state *state,
@@ -977,17 +977,17 @@ static void gitdiff_verify_name(struct apply_state *state,
 		char *another;
 		if (isnull)
 			die(_("git apply: bad git-diff - expected /dev/null, got %s on line %d"),
-			    *name, state_linenr);
+			    *name, state->linenr);
 		another = find_name(state, line, NULL, state->p_value, TERM_TAB);
 		if (!another || memcmp(another, *name, len + 1))
 			die((side == DIFF_NEW_NAME) ?
 			    _("git apply: bad git-diff - inconsistent new filename on line %d") :
-			    _("git apply: bad git-diff - inconsistent old filename on line %d"), state_linenr);
+			    _("git apply: bad git-diff - inconsistent old filename on line %d"), state->linenr);
 		free(another);
 	} else {
 		/* expect "/dev/null" */
 		if (memcmp("/dev/null", line, 9) || line[9] != '\n')
-			die(_("git apply: bad git-diff - expected /dev/null on line %d"), state_linenr);
+			die(_("git apply: bad git-diff - expected /dev/null on line %d"), state->linenr);
 	}
 }
 
@@ -1350,8 +1350,8 @@ static int parse_git_header(struct apply_state *state,
 
 	line += len;
 	size -= len;
-	state_linenr++;
-	for (offset = len ; size > 0 ; offset += len, size -= len, line += len, state_linenr++) {
+	state->linenr++;
+	for (offset = len ; size > 0 ; offset += len, size -= len, line += len, state->linenr++) {
 		static const struct opentry {
 			const char *str;
 			int (*fn)(struct apply_state *, const char *, struct patch *);
@@ -1522,7 +1522,7 @@ static int find_header(struct apply_state *state,
 	patch->is_new = patch->is_delete = -1;
 	patch->old_mode = patch->new_mode = 0;
 	patch->old_name = patch->new_name = NULL;
-	for (offset = 0; size > 0; offset += len, size -= len, line += len, state_linenr++) {
+	for (offset = 0; size > 0; offset += len, size -= len, line += len, state->linenr++) {
 		unsigned long nextlen;
 
 		len = linelen(line, size);
@@ -1543,7 +1543,7 @@ static int find_header(struct apply_state *state,
 			if (parse_fragment_header(line, len, &dummy) < 0)
 				continue;
 			die(_("patch fragment without header at line %d: %.*s"),
-			    state_linenr, (int)len-1, line);
+			    state->linenr, (int)len-1, line);
 		}
 
 		if (size < len + 6)
@@ -1564,13 +1564,13 @@ static int find_header(struct apply_state *state,
 					       "git diff header lacks filename information when removing "
 					       "%d leading pathname components (line %d)",
 					       state->p_value),
-					    state->p_value, state_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)", state_linenr);
+				    "(line %d)", state->linenr);
 			patch->is_toplevel_relative = 1;
 			*hdrsize = git_hdr_len;
 			return offset;
@@ -1592,7 +1592,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;
-		state_linenr += 2;
+		state->linenr += 2;
 		return offset;
 	}
 	return -1;
@@ -1627,7 +1627,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, state_linenr);
+	record_ws_error(state, result, line + 1, len - 2, state->linenr);
 }
 
 /*
@@ -1660,11 +1660,11 @@ static int parse_fragment(struct apply_state *state,
 	/* Parse the thing.. */
 	line += len;
 	size -= len;
-	state_linenr++;
+	state->linenr++;
 	added = deleted = 0;
 	for (offset = len;
 	     0 < size;
-	     offset += len, size -= len, line += len, state_linenr++) {
+	     offset += len, size -= len, line += len, state->linenr++) {
 		if (!oldlines && !newlines)
 			break;
 		len = linelen(line, size);
@@ -1763,10 +1763,10 @@ static int parse_single_patch(struct apply_state *state,
 		int len;
 
 		fragment = xcalloc(1, sizeof(*fragment));
-		fragment->linenr = state_linenr;
+		fragment->linenr = state->linenr;
 		len = parse_fragment(state, line, size, patch, fragment);
 		if (len <= 0)
-			die(_("corrupt patch at line %d"), state_linenr);
+			die(_("corrupt patch at line %d"), state->linenr);
 		fragment->patch = line;
 		fragment->size = len;
 		oldlines += fragment->oldlines;
@@ -1852,7 +1852,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)
@@ -1894,13 +1895,13 @@ static struct fragment *parse_binary_hunk(char **buf_p,
 	else
 		return NULL;
 
-	state_linenr++;
+	state->linenr++;
 	buffer += llen;
 	while (1) {
 		int byte_length, max_byte_length, newsize;
 		llen = linelen(buffer, size);
 		used += llen;
-		state_linenr++;
+		state->linenr++;
 		if (llen == 1) {
 			/* consume the blank line */
 			buffer++;
@@ -1954,7 +1955,7 @@ static struct fragment *parse_binary_hunk(char **buf_p,
 	free(data);
 	*status_p = -1;
 	error(_("corrupt binary patch at line %d: %.*s"),
-	      state_linenr-1, llen-1, buffer);
+	      state->linenr-1, llen-1, buffer);
 	return NULL;
 }
 
@@ -1963,7 +1964,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
@@ -1984,15 +1988,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"), state_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) {
@@ -2107,8 +2111,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;
-			state_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;
@@ -2128,7 +2132,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)) {
-					state_linenr++;
+					state->linenr++;
 					patch->is_binary = 1;
 					patchsize = llen;
 					break;
@@ -2142,7 +2146,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"), state_linenr);
+			die(_("patch with only garbage at line %d"), state->linenr);
 	}
 
 	return offset + hdrsize + patchsize;
@@ -4661,6 +4665,7 @@ static void init_apply_state(struct apply_state *state, 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.2.490.g3dabe57

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

* [PATCH v2 44/94] builtin/apply: move 'fn_table' global into 'struct apply_state'
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (42 preceding siblings ...)
  2016-05-11 13:16 ` [PATCH v2 43/94] builtin/apply: move 'state_linenr' global " Christian Couder
@ 2016-05-11 13:16 ` Christian Couder
  2016-05-11 13:16 ` [PATCH v2 45/94] builtin/apply: move 'symlink_changes' " Christian Couder
                   ` (50 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:16 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify the apply functionality the 'fn_table' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

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

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

* [PATCH v2 45/94] builtin/apply: move 'symlink_changes' global into 'struct apply_state'
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (43 preceding siblings ...)
  2016-05-11 13:16 ` [PATCH v2 44/94] builtin/apply: move 'fn_table' " Christian Couder
@ 2016-05-11 13:16 ` Christian Couder
  2016-05-11 13:16 ` [PATCH v2 46/94] builtin/apply: move 'state' check into check_apply_state() Christian Couder
                   ` (49 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:16 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify the apply functionality the 'symlink_changes' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

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

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

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

* [PATCH v2 46/94] builtin/apply: move 'state' check into check_apply_state()
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (44 preceding siblings ...)
  2016-05-11 13:16 ` [PATCH v2 45/94] builtin/apply: move 'symlink_changes' " Christian Couder
@ 2016-05-11 13:16 ` Christian Couder
  2016-05-11 13:16 ` [PATCH v2 47/94] builtin/apply: move applying patches into apply_all_patches() Christian Couder
                   ` (48 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:16 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

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

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

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

* [PATCH v2 47/94] builtin/apply: move applying patches into apply_all_patches()
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (45 preceding siblings ...)
  2016-05-11 13:16 ` [PATCH v2 46/94] builtin/apply: move 'state' check into check_apply_state() Christian Couder
@ 2016-05-11 13:16 ` Christian Couder
  2016-05-11 13:16 ` [PATCH v2 48/94] builtin/apply: rename 'prefix_' parameter to 'prefix' Christian Couder
                   ` (47 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:16 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify the apply functionality we should provide a function to
apply many patches. Let's move the code to do that into a new
apply_all_patches() function.

Reviewed-by: Stefan Beller <sbeller@google.com>
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 e5f76d8..67c64a5 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4709,13 +4709,79 @@ static void check_apply_state(struct apply_state *state, int force_apply)
 		state->unsafe_paths = 0;
 }
 
-int cmd_apply(int argc, const char **argv, const char *prefix_)
+static int apply_all_patches(struct apply_state *state,
+			     int argc,
+			     const char **argv,
+			     int options)
 {
 	int i;
 	int errs = 0;
+	int read_stdin = 1;
+
+	for (i = 0; i < argc; i++) {
+		const char *arg = argv[i];
+		int fd;
+
+		if (!strcmp(arg, "-")) {
+			errs |= apply_patch(state, 0, "<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[] = {
@@ -4794,61 +4860,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.2.490.g3dabe57

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

* [PATCH v2 48/94] builtin/apply: rename 'prefix_' parameter to 'prefix'
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (46 preceding siblings ...)
  2016-05-11 13:16 ` [PATCH v2 47/94] builtin/apply: move applying patches into apply_all_patches() Christian Couder
@ 2016-05-11 13:16 ` Christian Couder
  2016-05-12 19:56   ` Junio C Hamano
  2016-05-11 13:17 ` [PATCH v2 49/94] builtin/apply: move 'lock_file' global into 'struct apply_state' Christian Couder
                   ` (46 subsequent siblings)
  94 siblings, 1 reply; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:16 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

This is just a small cleanup.

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

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

* [PATCH v2 49/94] builtin/apply: move 'lock_file' global into 'struct apply_state'
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (47 preceding siblings ...)
  2016-05-11 13:16 ` [PATCH v2 48/94] builtin/apply: rename 'prefix_' parameter to 'prefix' Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-11 13:17 ` [PATCH v2 50/94] builtin/apply: move 'newfd' " Christian Couder
                   ` (45 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	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'

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

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

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

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

* [PATCH v2 50/94] builtin/apply: move 'newfd' global into 'struct apply_state'
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (48 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 49/94] builtin/apply: move 'lock_file' global into 'struct apply_state' Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-11 13:17 ` [PATCH v2 51/94] builtin/apply: make apply_patch() return -1 instead of die()ing Christian Couder
                   ` (44 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify the apply functionality the 'newfd' variable should
not be static and global to the file. Let's move it into
'struct apply_state'.

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

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

* [PATCH v2 51/94] builtin/apply: make apply_patch() return -1 instead of die()ing
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (49 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 50/94] builtin/apply: move 'newfd' " Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-11 13:17 ` [PATCH v2 52/94] builtin/apply: read_patch_file() " Christian Couder
                   ` (43 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	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.

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

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

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

* [PATCH v2 52/94] builtin/apply: read_patch_file() return -1 instead of die()ing
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (50 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 51/94] builtin/apply: make apply_patch() return -1 instead of die()ing Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-16  1:56   ` Eric Sunshine
  2016-05-11 13:17 ` [PATCH v2 53/94] builtin/apply: make find_header() " Christian Couder
                   ` (42 subsequent siblings)
  94 siblings, 1 reply; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

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

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

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

* [PATCH v2 53/94] builtin/apply: make find_header() return -1 instead of die()ing
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (51 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 52/94] builtin/apply: read_patch_file() " Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-11 13:17 ` [PATCH v2 54/94] builtin/apply: make parse_chunk() return a negative integer on error Christian Couder
                   ` (41 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify `git apply` functionality we have to signal errors to the
caller instead of die()ing.

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

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

Signed-off-by: Christian Couder <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 a166d70..4212705 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -1529,6 +1529,14 @@ static int parse_fragment_header(const char *line, int len, struct fragment *fra
 	return offset;
 }
 
+/*
+ * Find file diff header
+ *
+ * Returns:
+ *  -1 in case of error
+ *  -2 if no header was found
+ *   the size of the header in bytes (called "offset") otherwise
+ */
 static int find_header(struct apply_state *state,
 		       const char *line,
 		       unsigned long size,
@@ -1562,8 +1570,8 @@ static int find_header(struct apply_state *state,
 			struct fragment dummy;
 			if (parse_fragment_header(line, len, &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)
@@ -1579,18 +1587,18 @@ static int find_header(struct apply_state *state,
 				continue;
 			if (!patch->old_name && !patch->new_name) {
 				if (!patch->def_name)
-					die(Q_("git diff header lacks filename information when removing "
-					       "%d leading pathname component (line %d)",
-					       "git diff header lacks filename information when removing "
-					       "%d leading pathname components (line %d)",
-					       state->p_value),
-					    state->p_value, state->linenr);
+					return error(Q_("git diff header lacks filename information when removing "
+							"%d leading pathname component (line %d)",
+							"git diff header lacks filename information when removing "
+							"%d leading pathname components (line %d)",
+							state->p_value),
+						     state->p_value, state->linenr);
 				patch->old_name = xstrdup(patch->def_name);
 				patch->new_name = xstrdup(patch->def_name);
 			}
 			if (!patch->is_delete && !patch->new_name)
-				die("git diff header lacks filename information "
-				    "(line %d)", state->linenr);
+				return error("git diff header lacks filename information "
+					     "(line %d)", state->linenr);
 			patch->is_toplevel_relative = 1;
 			*hdrsize = git_hdr_len;
 			return offset;
@@ -1615,7 +1623,7 @@ static int find_header(struct apply_state *state,
 		state->linenr += 2;
 		return offset;
 	}
-	return -1;
+	return -2;
 }
 
 static void record_ws_error(struct apply_state *state,
@@ -2106,6 +2114,9 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
 	int hdrsize, patchsize;
 	int offset = find_header(state, buffer, size, &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.2.490.g3dabe57

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

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

To libify `git apply` functionality we have to signal errors to the
caller instead of die()ing or exit()ing.

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

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

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

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

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

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

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

To libify `git apply` functionality we have to signal errors to the
caller instead of die()ing.

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

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

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

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

* [PATCH v2 56/94] apply: move 'struct apply_state' to apply.h
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (54 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 55/94] builtin/apply: make parse_single_patch() return -1 " Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-16  3:10   ` Eric Sunshine
  2016-05-11 13:17 ` [PATCH v2 57/94] builtin/apply: make parse_whitespace_option() return -1 instead of die()ing Christian Couder
                   ` (38 subsequent siblings)
  94 siblings, 1 reply; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify `git apply` functionality we must make 'struct apply_state'
usable outside "builtin/apply.c".

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

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

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

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

* [PATCH v2 57/94] builtin/apply: make parse_whitespace_option() return -1 instead of die()ing
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (55 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 56/94] apply: move 'struct apply_state' to apply.h Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-11 13:17 ` [PATCH v2 58/94] builtin/apply: make parse_ignorewhitespace_option() " Christian Couder
                   ` (37 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify `git apply` functionality we have to signal errors to the
caller instead of die()ing.

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

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

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

* [PATCH v2 58/94] builtin/apply: make parse_ignorewhitespace_option() return -1 instead of die()ing
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (56 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 57/94] builtin/apply: make parse_whitespace_option() return -1 instead of die()ing Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-11 13:17 ` [PATCH v2 59/94] builtin/apply: move init_apply_state() to apply.c Christian Couder
                   ` (36 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify `git apply` functionality we have to signal errors to the
caller instead of die()ing.

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

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

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

* [PATCH v2 59/94] builtin/apply: move init_apply_state() to apply.c
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (57 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 58/94] builtin/apply: make parse_ignorewhitespace_option() " Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-16  3:16   ` Eric Sunshine
  2016-05-11 13:17 ` [PATCH v2 60/94] apply: make init_apply_state() return -1 instead of exit()ing Christian Couder
                   ` (35 subsequent siblings)
  94 siblings, 1 reply; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify `git apply` functionality we must make init_apply_state()
usable outside "builtin/apply.c".

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

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

diff --git a/Makefile b/Makefile
index 3f03366..b2a7b0c 100644
--- a/Makefile
+++ b/Makefile
@@ -683,6 +683,7 @@ LIB_OBJS += abspath.o
 LIB_OBJS += advice.o
 LIB_OBJS += alias.o
 LIB_OBJS += alloc.o
+LIB_OBJS += apply.o
 LIB_OBJS += archive.o
 LIB_OBJS += archive-tar.o
 LIB_OBJS += archive-zip.o
diff --git a/apply.c b/apply.c
new file mode 100644
index 0000000..508ea64
--- /dev/null
+++ b/apply.c
@@ -0,0 +1,83 @@
+#include "cache.h"
+#include "lockfile.h"
+#include "apply.h"
+
+static void git_apply_config(void)
+{
+	git_config_get_string_const("apply.whitespace", &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,
+		      struct lock_file *lock_file)
+{
+	memset(state, 0, sizeof(*state));
+	state->prefix = prefix;
+	state->prefix_length = state->prefix ? strlen(state->prefix) : 0;
+	state->lock_file = lock_file ? lock_file : xcalloc(1, sizeof(*lock_file));
+	state->newfd = -1;
+	state->apply = 1;
+	state->line_termination = '\n';
+	state->p_value = 1;
+	state->p_context = UINT_MAX;
+	state->squelch_whitespace_errors = 5;
+	state->ws_error_action = warn_on_ws_error;
+	state->ws_ignore_action = ignore_ws_none;
+	state->linenr = 1;
+	strbuf_init(&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 aa11ea6..0f77f4d 100644
--- a/apply.h
+++ b/apply.h
@@ -112,4 +112,13 @@ struct apply_state {
 	enum ws_ignore ws_ignore_action;
 };
 
+extern int parse_whitespace_option(struct apply_state *state,
+				   const char *option);
+extern int parse_ignorewhitespace_option(struct apply_state *state,
+					 const char *option);
+
+extern void init_apply_state(struct apply_state *state,
+			     const char *prefix,
+			     struct lock_file *lock_file);
+
 #endif
diff --git a/builtin/apply.c b/builtin/apply.c
index a7ebbbb..805c707 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)
@@ -4528,13 +4482,6 @@ end:
 	return res;
 }
 
-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)
 {
@@ -4593,32 +4540,6 @@ static int option_parse_directory(const struct option *opt,
 	return 0;
 }
 
-static void init_apply_state(struct apply_state *state,
-			     const char *prefix,
-			     struct lock_file *lock_file)
-{
-	memset(state, 0, sizeof(*state));
-	state->prefix = prefix;
-	state->prefix_length = state->prefix ? strlen(state->prefix) : 0;
-	state->lock_file = lock_file ? lock_file : xcalloc(1, sizeof(*lock_file));
-	state->newfd = -1;
-	state->apply = 1;
-	state->line_termination = '\n';
-	state->p_value = 1;
-	state->p_context = UINT_MAX;
-	state->squelch_whitespace_errors = 5;
-	state->ws_error_action = warn_on_ws_error;
-	state->ws_ignore_action = ignore_ws_none;
-	state->linenr = 1;
-	strbuf_init(&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.2.490.g3dabe57

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

* [PATCH v2 60/94] apply: make init_apply_state() return -1 instead of exit()ing
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (58 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 59/94] builtin/apply: move init_apply_state() to apply.c Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-16  3:37   ` Eric Sunshine
  2016-05-11 13:17 ` [PATCH v2 61/94] builtin/apply: make check_apply_state() return -1 instead of die()ing Christian Couder
                   ` (34 subsequent siblings)
  94 siblings, 1 reply; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify `git apply` functionality we have to signal errors to the
caller instead of exit()ing.

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

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

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

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

* [PATCH v2 61/94] builtin/apply: make check_apply_state() return -1 instead of die()ing
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (59 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 60/94] apply: make init_apply_state() return -1 instead of exit()ing Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-11 13:17 ` [PATCH v2 62/94] builtin/apply: move check_apply_state() to apply.c Christian Couder
                   ` (33 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify `git apply` functionality we have to signal errors to the
caller instead of die()ing.

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

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

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

* [PATCH v2 62/94] builtin/apply: move check_apply_state() to apply.c
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (60 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 61/94] builtin/apply: make check_apply_state() return -1 instead of die()ing Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-11 13:17 ` [PATCH v2 63/94] builtin/apply: make apply_all_patches() return -1 on error Christian Couder
                   ` (32 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify `git apply` functionality we must make check_apply_state()
usable outside "builtin/apply.c".

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

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

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

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

* [PATCH v2 63/94] builtin/apply: make apply_all_patches() return -1 on error
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (61 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 62/94] builtin/apply: move check_apply_state() to apply.c Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-16  3:44   ` Eric Sunshine
  2016-05-11 13:17 ` [PATCH v2 64/94] builtin/apply: make parse_traditional_patch() " Christian Couder
                   ` (31 subsequent siblings)
  94 siblings, 1 reply; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

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

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

Helped-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Helped-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
Helped-by: Eric Sunshine <sunshine@sunshineco.com>
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 ae16f99..dd212c9 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4557,7 +4557,7 @@ static int apply_all_patches(struct apply_state *state,
 		if (!strcmp(arg, "-")) {
 			res = apply_patch(state, 0, "<stdin>", options);
 			if (res < 0)
-				exit(1);
+				return -1;
 			errs |= res;
 			read_stdin = 0;
 			continue;
@@ -4568,20 +4568,20 @@ static int apply_all_patches(struct apply_state *state,
 
 		fd = open(arg, O_RDONLY);
 		if (fd < 0)
-			die_errno(_("can't open patch '%s'"), arg);
+			return error(_("can't open patch '%s': %s"), arg, strerror(errno));
 		read_stdin = 0;
 		set_default_whitespace_mode(state);
 		res = apply_patch(state, fd, arg, options);
+		close(fd);
 		if (res < 0)
-			exit(1);
+			return -1;
 		errs |= res;
-		close(fd);
 	}
 	set_default_whitespace_mode(state);
 	if (read_stdin) {
 		res = apply_patch(state, 0, "<stdin>", options);
 		if (res < 0)
-			exit(1);
+			return -1;
 		errs |= res;
 	}
 
@@ -4596,10 +4596,10 @@ static int apply_all_patches(struct apply_state *state,
 				squelched);
 		}
 		if (state->ws_error_action == die_on_ws_error)
-			die(Q_("%d line adds whitespace errors.",
-			       "%d lines add whitespace errors.",
-			       state->whitespace_error),
-			    state->whitespace_error);
+			return error(Q_("%d line adds whitespace errors.",
+					"%d lines add whitespace errors.",
+					state->whitespace_error),
+				     state->whitespace_error);
 		if (state->applied_after_fixing_ws && state->apply)
 			warning("%d line%s applied after"
 				" fixing whitespace errors.",
@@ -4613,9 +4613,10 @@ static int apply_all_patches(struct apply_state *state,
 	}
 
 	if (state->update_index) {
-		if (write_locked_index(&the_index, state->lock_file, COMMIT_LOCK))
-			die(_("Unable to write new index file"));
+		res = write_locked_index(&the_index, state->lock_file, COMMIT_LOCK);
 		state->newfd = -1;
+		if (res)
+			return error(_("Unable to write new index file"));
 	}
 
 	return !!errs;
@@ -4705,5 +4706,5 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
 	if (check_apply_state(&state, force_apply))
 		exit(1);
 
-	return apply_all_patches(&state, argc, argv, options);
+	return !!apply_all_patches(&state, argc, argv, options);
 }
-- 
2.8.2.490.g3dabe57

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

* [PATCH v2 64/94] builtin/apply: make parse_traditional_patch() return -1 on error
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (62 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 63/94] builtin/apply: make apply_all_patches() return -1 on error Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-11 13:17 ` [PATCH v2 65/94] builtin/apply: make gitdiff_*() return 1 at end of header Christian Couder
                   ` (30 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify `git apply` functionality we have to signal errors to the
caller instead of die()ing.

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

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

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

* [PATCH v2 65/94] builtin/apply: make gitdiff_*() return 1 at end of header
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (63 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 64/94] builtin/apply: make parse_traditional_patch() " Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-11 13:17 ` [PATCH v2 66/94] builtin/apply: make gitdiff_*() return -1 on error Christian Couder
                   ` (29 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

The gitdiff_*() functions that are called as p->fn() in parse_git_header()
should return 1 instead of -1 in case of end of header or unrecognized
input, as these are not real errors. It just instructs the parser to break
out.

This makes it possible for gitdiff_*() functions to return -1 in case of a
real error. This will be done in a following patch.

Helped-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 builtin/apply.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/builtin/apply.c b/builtin/apply.c
index 8e82eea..b3a9c2e 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;
 }
 
 /*
@@ -1016,7 +1016,7 @@ static int gitdiff_unrecognized(struct apply_state *state,
 				const char *line,
 				struct patch *patch)
 {
-	return -1;
+	return 1;
 }
 
 /*
@@ -1248,9 +1248,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 +1433,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.2.490.g3dabe57

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

* [PATCH v2 66/94] builtin/apply: make gitdiff_*() return -1 on error
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (64 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 65/94] builtin/apply: make gitdiff_*() return 1 at end of header Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-11 13:17 ` [PATCH v2 67/94] builtin/apply: change die_on_unsafe_path() to check_unsafe_path() Christian Couder
                   ` (28 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify `git apply` functionality we have to signal errors to the
caller instead of die()ing.

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

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

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

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

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

* [PATCH v2 67/94] builtin/apply: change die_on_unsafe_path() to check_unsafe_path()
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (65 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 66/94] builtin/apply: make gitdiff_*() return -1 on error Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-11 13:17 ` [PATCH v2 68/94] builtin/apply: make build_fake_ancestor() return -1 on error Christian Couder
                   ` (27 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify `git apply` functionality we have to signal errors to the
caller instead of die()ing.

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

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

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

* [PATCH v2 68/94] builtin/apply: make build_fake_ancestor() return -1 on error
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (66 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 67/94] builtin/apply: change die_on_unsafe_path() to check_unsafe_path() Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-11 13:17 ` [PATCH v2 69/94] builtin/apply: make remove_file() " Christian Couder
                   ` (26 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify `git apply` functionality we have to signal errors to the
caller instead of die()ing.

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

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

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

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

* [PATCH v2 69/94] builtin/apply: make remove_file() return -1 on error
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (67 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 68/94] builtin/apply: make build_fake_ancestor() return -1 on error Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-11 13:17 ` [PATCH v2 70/94] builtin/apply: make add_conflicted_stages_file() " Christian Couder
                   ` (25 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify `git apply` functionality we have to signal errors to the
caller instead of die()ing.

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

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

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

* [PATCH v2 70/94] builtin/apply: make add_conflicted_stages_file() return -1 on error
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (68 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 69/94] builtin/apply: make remove_file() " Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-11 13:17 ` [PATCH v2 71/94] builtin/apply: make add_index_file() " Christian Couder
                   ` (24 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify `git apply` functionality we have to signal errors to the
caller instead of die()ing.

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

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

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

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

* [PATCH v2 71/94] builtin/apply: make add_index_file() return -1 on error
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (69 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 70/94] builtin/apply: make add_conflicted_stages_file() " Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-11 13:17 ` [PATCH v2 72/94] builtin/apply: make create_file() " Christian Couder
                   ` (23 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify `git apply` functionality we have to signal errors to the
caller instead of die()ing.

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

Signed-off-by: Christian Couder <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 ca3502f..0e20467 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4089,11 +4089,11 @@ static int remove_file(struct apply_state *state, struct patch *patch, int rmdir
 	return 0;
 }
 
-static void add_index_file(struct apply_state *state,
-			   const char *path,
-			   unsigned mode,
-			   void *buf,
-			   unsigned long size)
+static int add_index_file(struct apply_state *state,
+			  const char *path,
+			  unsigned mode,
+			  void *buf,
+			  unsigned long size)
 {
 	struct stat st;
 	struct cache_entry *ce;
@@ -4101,7 +4101,7 @@ static void add_index_file(struct apply_state *state,
 	unsigned ce_size = cache_entry_size(namelen);
 
 	if (!state->update_index)
-		return;
+		return 0;
 
 	ce = xcalloc(1, ce_size);
 	memcpy(ce->name, path, namelen);
@@ -4112,20 +4112,32 @@ static void add_index_file(struct apply_state *state,
 		const char *s;
 
 		if (!skip_prefix(buf, "Subproject commit ", &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)
-		die(_("unable to add cache entry for %s"), path);
+	if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0) {
+		free(ce);
+		return error(_("unable to add cache entry for %s"), path);
+	}
+
+	return 0;
 }
 
 static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)
@@ -4261,8 +4273,10 @@ static void create_file(struct apply_state *state, struct patch *patch)
 	if (patch->conflicted_threeway) {
 		if (add_conflicted_stages_file(state, patch))
 			exit(1);
-	} else
-		add_index_file(state, path, mode, buf, size);
+	} else {
+		if (add_index_file(state, path, mode, buf, size))
+			exit(1);
+	}
 }
 
 /* phase zero is to remove, phase one is to create */
-- 
2.8.2.490.g3dabe57

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

* [PATCH v2 72/94] builtin/apply: make create_file() return -1 on error
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (70 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 71/94] builtin/apply: make add_index_file() " Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-11 13:17 ` [PATCH v2 73/94] builtin/apply: make write_out_one_result() " Christian Couder
                   ` (22 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify `git apply` functionality we have to signal errors to the
caller instead of exit()ing.

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

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

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

* [PATCH v2 73/94] builtin/apply: make write_out_one_result() return -1 on error
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (71 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 72/94] builtin/apply: make create_file() " Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-11 13:17 ` [PATCH v2 74/94] builtin/apply: make write_out_results() " Christian Couder
                   ` (21 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify `git apply` functionality we have to signal errors to the
caller instead of exit()ing.

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

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

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

* [PATCH v2 74/94] builtin/apply: make write_out_results() return -1 on error
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (72 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 73/94] builtin/apply: make write_out_one_result() " Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-11 13:17 ` [PATCH v2 75/94] builtin/apply: make try_create_file() " Christian Couder
                   ` (20 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify `git apply` functionality we have to signal errors to the
caller instead of exit()ing.

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

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

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

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

* [PATCH v2 75/94] builtin/apply: make try_create_file() return -1 on error
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (73 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 74/94] builtin/apply: make write_out_results() " Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-11 13:17 ` [PATCH v2 76/94] builtin/apply: make create_one_file() " Christian Couder
                   ` (19 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify `git apply` functionality we have to signal errors to the
caller instead of die()ing.

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

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

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

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

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

* [PATCH v2 76/94] builtin/apply: make create_one_file() return -1 on error
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (74 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 75/94] builtin/apply: make try_create_file() " Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-11 13:17 ` [PATCH v2 77/94] builtin/apply: rename option parsing functions Christian Couder
                   ` (18 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

To libify `git apply` functionality we have to signal errors to the
caller instead of exit()ing.

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

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 eaf7b8f..8c31617 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4185,32 +4185,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 < 0)
-		exit(1);
+		return -1;
 	if (!res)
-		return;
+		return 0;
 
 	if (errno == ENOENT) {
 		if (safe_create_leading_directories(path))
-			return;
+			return 0;
 		res = try_create_file(path, mode, buf, size);
 		if (res < 0)
-			exit(1);
+			return -1;
 		if (!res)
-			return;
+			return 0;
 	}
 
 	if (errno == EEXIST || errno == EACCES) {
@@ -4230,10 +4234,10 @@ static void create_one_file(struct apply_state *state,
 			mksnpath(newpath, sizeof(newpath), "%s~%u", path, nr);
 			res = try_create_file(newpath, mode, buf, size);
 			if (res < 0)
-				exit(1);
+				return -1;
 			if (!res) {
 				if (!rename(newpath, path))
-					return;
+					return 0;
 				unlink_or_warn(newpath);
 				break;
 			}
@@ -4242,7 +4246,8 @@ static void create_one_file(struct apply_state *state,
 			++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,
@@ -4287,7 +4292,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.2.490.g3dabe57

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

* [PATCH v2 77/94] builtin/apply: rename option parsing functions
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (75 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 76/94] builtin/apply: make create_one_file() " Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-11 13:17 ` [PATCH v2 78/94] apply: rename and move opt constants to apply.h Christian Couder
                   ` (17 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	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 8c31617..f05dc96 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4565,16 +4565,16 @@ end:
 	return res;
 }
 
-static int option_parse_exclude(const struct option *opt,
-				const char *arg, int unset)
+static int apply_option_parse_exclude(const struct option *opt,
+				      const char *arg, int unset)
 {
 	struct apply_state *state = opt->value;
 	add_name_limit(state, arg, 1);
 	return 0;
 }
 
-static int option_parse_include(const struct option *opt,
-				const char *arg, int unset)
+static int apply_option_parse_include(const struct option *opt,
+				      const char *arg, int unset)
 {
 	struct apply_state *state = opt->value;
 	add_name_limit(state, arg, 0);
@@ -4582,9 +4582,9 @@ static int option_parse_include(const struct option *opt,
 	return 0;
 }
 
-static int option_parse_p(const struct option *opt,
-			  const char *arg,
-			  int unset)
+static int apply_option_parse_p(const struct option *opt,
+				const char *arg,
+				int unset)
 {
 	struct apply_state *state = opt->value;
 	state->p_value = atoi(arg);
@@ -4592,8 +4592,8 @@ static int option_parse_p(const struct option *opt,
 	return 0;
 }
 
-static int option_parse_space_change(const struct option *opt,
-				     const char *arg, int unset)
+static int apply_option_parse_space_change(const struct option *opt,
+					   const char *arg, int unset)
 {
 	struct apply_state *state = opt->value;
 	if (unset)
@@ -4603,8 +4603,8 @@ static int option_parse_space_change(const struct option *opt,
 	return 0;
 }
 
-static int option_parse_whitespace(const struct option *opt,
-				   const char *arg, int unset)
+static int apply_option_parse_whitespace(const struct option *opt,
+					 const char *arg, int unset)
 {
 	struct apply_state *state = opt->value;
 	state->whitespace_option = arg;
@@ -4613,8 +4613,8 @@ static int option_parse_whitespace(const struct option *opt,
 	return 0;
 }
 
-static int option_parse_directory(const struct option *opt,
-				  const char *arg, int unset)
+static int apply_option_parse_directory(const struct option *opt,
+					const char *arg, int unset)
 {
 	struct apply_state *state = opt->value;
 	strbuf_reset(&state->root);
@@ -4714,13 +4714,13 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
 	struct option builtin_apply_options[] = {
 		{ OPTION_CALLBACK, 0, "exclude", &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,
@@ -4752,13 +4752,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,
@@ -4776,7 +4776,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.2.490.g3dabe57

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

* [PATCH v2 78/94] apply: rename and move opt constants to apply.h
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (76 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 77/94] builtin/apply: rename option parsing functions Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-11 13:17 ` [PATCH v2 80/94] apply: make some parsing functions static again Christian Couder
                   ` (16 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

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

Signed-off-by: Christian Couder <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 5266612..60e0c2f 100644
--- a/apply.h
+++ b/apply.h
@@ -122,4 +122,7 @@ extern int init_apply_state(struct apply_state *state,
 			    struct lock_file *lock_file);
 extern int check_apply_state(struct apply_state *state, int force_apply);
 
+#define APPLY_OPT_INACCURATE_EOF	(1<<0)
+#define APPLY_OPT_RECOUNT		(1<<1)
+
 #endif
diff --git a/builtin/apply.c b/builtin/apply.c
index f05dc96..9ce177b 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4448,9 +4448,6 @@ static int write_out_results(struct apply_state *state, struct patch *list)
 	return errs;
 }
 
-#define INACCURATE_EOF	(1<<0)
-#define RECOUNT		(1<<1)
-
 /*
  * Try to apply a patch.
  *
@@ -4479,8 +4476,8 @@ static int apply_patch(struct apply_state *state,
 		int nr;
 
 		patch = xcalloc(1, sizeof(*patch));
-		patch->inaccurate_eof = !!(options & INACCURATE_EOF);
-		patch->recount =  !!(options & RECOUNT);
+		patch->inaccurate_eof = !!(options & APPLY_OPT_INACCURATE_EOF);
+		patch->recount =  !!(options & APPLY_OPT_RECOUNT);
 		nr = parse_chunk(state, buf.buf + offset, buf.len - offset, patch);
 		if (nr < 0) {
 			free_patch(patch);
@@ -4770,10 +4767,10 @@ int cmd_apply(int argc, const char **argv, const char *prefix)
 		OPT__VERBOSE(&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.2.490.g3dabe57

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

* [PATCH v2 80/94] apply: make some parsing functions static again
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (77 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 78/94] apply: rename and move opt constants to apply.h Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-11 13:17 ` [PATCH v2 81/94] run-command: make dup_devnull() non static Christian Couder
                   ` (15 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

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

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

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

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

* [PATCH v2 81/94] run-command: make dup_devnull() non static
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (78 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 80/94] apply: make some parsing functions static again Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-11 13:17 ` [PATCH v2 82/94] apply: roll back index lock file in case of error Christian Couder
                   ` (14 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

We will need this function in a later commit to redirect stdout
and stderr to /dev/null.

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

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

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

* [PATCH v2 82/94] apply: roll back index lock file in case of error
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (79 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 81/94] run-command: make dup_devnull() non static Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-11 13:17 ` [PATCH v2 83/94] environment: add set_index_file() Christian Couder
                   ` (13 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

According to the lockfile API, when finished with a lockfile, one
should either commit it or roll it back.

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

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

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

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

* [PATCH v2 83/94] environment: add set_index_file()
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (80 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 82/94] apply: roll back index lock file in case of error Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-11 13:17 ` [PATCH v2 84/94] builtin/am: use apply api in run_apply() Christian Couder
                   ` (12 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

Introduce set_index_file() to be able to temporarily change the index file.

It should be used like this:

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

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

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

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

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

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

* [PATCH v2 84/94] builtin/am: use apply api in run_apply()
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (81 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 83/94] environment: add set_index_file() Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-11 13:17 ` [PATCH v2 85/94] write_or_die: use warning() instead of fprintf(stderr, ...) Christian Couder
                   ` (11 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

This replaces run_apply() implementation with a new one that
uses the apply api that has been previously prepared in
apply.c and apply.h.

This shoud improve performance a lot in certain cases.

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

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

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

Also eliminating index reads enables further performance
improvements by using:

`git update-index --split-index`

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

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

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

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

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

diff --git a/builtin/am.c b/builtin/am.c
index d003939..cc66a48 100644
--- a/builtin/am.c
+++ b/builtin/am.c
@@ -28,6 +28,7 @@
 #include "rerere.h"
 #include "prompt.h"
 #include "mailinfo.h"
+#include "apply.h"
 
 /**
  * Returns 1 if the file is empty or does not exist, 0 otherwise.
@@ -1522,39 +1523,106 @@ static int parse_mail_rebase(struct am_state *state, const char *mail)
  */
 static int run_apply(const struct am_state *state, const char *index_file)
 {
-	struct child_process cp = CHILD_PROCESS_INIT;
-
-	cp.git_cmd = 1;
-
-	if (index_file)
-		argv_array_pushf(&cp.env_array, "GIT_INDEX_FILE=%s", index_file);
+	struct argv_array apply_paths = ARGV_ARRAY_INIT;
+	struct argv_array apply_opts = ARGV_ARRAY_INIT;
+	struct apply_state apply_state;
+	int save_stdout_fd, save_stderr_fd;
+	int res, opts_left;
+	char *save_index_file;
+	static struct lock_file lock_file;
+
+	struct option am_apply_options[] = {
+		{ OPTION_CALLBACK, 0, "whitespace", &apply_state, N_("action"),
+			N_("detect new or modified lines that have whitespace errors"),
+			0, apply_option_parse_whitespace },
+		{ OPTION_CALLBACK, 0, "ignore-space-change", &apply_state, NULL,
+			N_("ignore changes in whitespace when finding context"),
+			PARSE_OPT_NOARG, apply_option_parse_space_change },
+		{ OPTION_CALLBACK, 0, "ignore-whitespace", &apply_state, NULL,
+			N_("ignore changes in whitespace when finding context"),
+			PARSE_OPT_NOARG, apply_option_parse_space_change },
+		{ OPTION_CALLBACK, 0, "directory", &apply_state, N_("root"),
+			N_("prepend <root> to all filenames"),
+			0, apply_option_parse_directory },
+		{ OPTION_CALLBACK, 0, "exclude", &apply_state, N_("path"),
+			N_("don't apply changes matching the given path"),
+			0, apply_option_parse_exclude },
+		{ OPTION_CALLBACK, 0, "include", &apply_state, N_("path"),
+			N_("apply changes matching the given path"),
+			0, apply_option_parse_include },
+		OPT_INTEGER('C', NULL, &apply_state.p_context,
+				N_("ensure at least <n> lines of context match")),
+		{ OPTION_CALLBACK, 'p', NULL, &apply_state, N_("num"),
+			N_("remove <num> leading slashes from traditional diff paths"),
+			0, apply_option_parse_p },
+		OPT_BOOL(0, "reject", &apply_state.apply_with_reject,
+			N_("leave the rejected hunks in corresponding *.rej files")),
+		OPT_END()
+	};
 
 	/*
 	 * If we are allowed to fall back on 3-way merge, don't give false
 	 * errors during the initial attempt.
 	 */
+
 	if (state->threeway && !index_file) {
-		cp.no_stdout = 1;
-		cp.no_stderr = 1;
+		save_stdout_fd = dup(1);
+		dup_devnull(1);
+		save_stderr_fd = dup(2);
+		dup_devnull(2);
 	}
 
-	argv_array_push(&cp.args, "apply");
+	if (index_file) {
+		save_index_file = get_index_file();
+		set_index_file((char *)index_file);
+	}
 
-	argv_array_pushv(&cp.args, state->git_apply_opts.argv);
+	if (init_apply_state(&apply_state, NULL, &lock_file))
+		die("init_apply_state() failed");
+
+	argv_array_push(&apply_opts, "apply");
+	argv_array_pushv(&apply_opts, state->git_apply_opts.argv);
+
+	opts_left = parse_options(apply_opts.argc, apply_opts.argv,
+				  NULL, am_apply_options, NULL, 0);
+
+	if (opts_left != 0)
+		die("unknown option passed thru to git apply");
 
 	if (index_file)
-		argv_array_push(&cp.args, "--cached");
+		apply_state.cached = 1;
 	else
-		argv_array_push(&cp.args, "--index");
+		apply_state.check_index = 1;
 
-	argv_array_push(&cp.args, am_path(state, "patch"));
+	if (check_apply_state(&apply_state, 0))
+		die("check_apply_state() failed");
 
-	if (run_command(&cp))
-		return -1;
+	argv_array_push(&apply_paths, am_path(state, "patch"));
 
-	/* Reload index as git-apply will have modified it. */
-	discard_cache();
-	read_cache_from(index_file ? index_file : get_index_file());
+	res = apply_all_patches(&apply_state, apply_paths.argc, apply_paths.argv, 0);
+
+	/* Restore stdout and stderr */
+	if (state->threeway && !index_file) {
+		dup2(save_stdout_fd, 1);
+		close(save_stdout_fd);
+		dup2(save_stderr_fd, 2);
+		close(save_stderr_fd);
+	}
+
+	if (index_file)
+		set_index_file(save_index_file);
+
+	argv_array_clear(&apply_paths);
+	argv_array_clear(&apply_opts);
+
+	if (res)
+		return res;
+
+	if (index_file) {
+		/* Reload index as apply_all_patches() will have modified it. */
+		discard_cache();
+		read_cache_from(index_file);
+	}
 
 	return 0;
 }
-- 
2.8.2.490.g3dabe57

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

* [PATCH v2 85/94] write_or_die: use warning() instead of fprintf(stderr, ...)
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (82 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 84/94] builtin/am: use apply api in run_apply() Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-11 13:17 ` [PATCH v2 86/94] apply: add 'be_silent' variable to 'struct apply_state' Christian Couder
                   ` (10 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

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

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

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

* [PATCH v2 86/94] apply: add 'be_silent' variable to 'struct apply_state'
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (83 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 85/94] write_or_die: use warning() instead of fprintf(stderr, ...) Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-11 13:17 ` [PATCH v2 87/94] apply: make 'be_silent' incomatible with 'apply_verbosely' Christian Couder
                   ` (9 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

This variable should prevent anything to be printed on both stderr
and stdout.

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

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

diff --git a/apply.c b/apply.c
index 7480ae8..f69a61a 100644
--- a/apply.c
+++ b/apply.c
@@ -1600,8 +1600,9 @@ static void record_ws_error(struct apply_state *state,
 		return;
 
 	err = whitespace_error_string(result);
-	fprintf(stderr, "%s:%d: %s.\n%.*s\n",
-		state->patch_input_file, linenr, err, len, line);
+	if (!state->be_silent)
+		fprintf(stderr, "%s:%d: %s.\n%.*s\n",
+			state->patch_input_file, linenr, err, len, line);
 	free(err);
 }
 
@@ -1796,7 +1797,7 @@ static int parse_single_patch(struct apply_state *state,
 		return error(_("new file %s depends on old contents"), patch->new_name);
 	if (0 < patch->is_delete && newlines)
 		return error(_("deleted file %s still has contents"), patch->old_name);
-	if (!patch->is_delete && !newlines && context)
+	if (!patch->is_delete && !newlines && context && !state->be_silent)
 		fprintf_ln(stderr,
 			   _("** warning: "
 			     "file %s becomes empty but is not deleted"),
@@ -3020,8 +3021,8 @@ static int apply_one_fragment(struct apply_state *state,
 		 * Warn if it was necessary to reduce the number
 		 * of context lines.
 		 */
-		if ((leading != frag->leading) ||
-		    (trailing != frag->trailing))
+		if ((leading != frag->leading ||
+		     trailing != frag->trailing) && !state->be_silent)
 			fprintf_ln(stderr, _("Context reduced to (%ld/%ld)"
 					     " to apply fragment at %d"),
 				   leading, trailing, applied_pos+1);
@@ -3518,7 +3519,8 @@ static int try_threeway(struct apply_state *state,
 		 read_blob_object(&buf, pre_sha1, patch->old_mode))
 		return error("repository lacks the necessary blob to fall back on 3-way merge.");
 
-	fprintf(stderr, "Falling back to three-way merge...\n");
+	if (!state->be_silent)
+		fprintf(stderr, "Falling back to three-way merge...\n");
 
 	img = strbuf_detach(&buf, &len);
 	prepare_image(&tmp_image, img, len, 1);
@@ -3548,7 +3550,9 @@ static int try_threeway(struct apply_state *state,
 	status = three_way_merge(image, patch->new_name,
 				 pre_sha1, our_sha1, post_sha1);
 	if (status < 0) {
-		fprintf(stderr, "Failed to fall back on three-way merge...\n");
+		if (!state->be_silent)
+			fprintf(stderr,
+				"Failed to fall back on three-way merge...\n");
 		return status;
 	}
 
@@ -3560,9 +3564,15 @@ static int try_threeway(struct apply_state *state,
 			hashcpy(patch->threeway_stage[0].hash, pre_sha1);
 		hashcpy(patch->threeway_stage[1].hash, our_sha1);
 		hashcpy(patch->threeway_stage[2].hash, post_sha1);
-		fprintf(stderr, "Applied patch to '%s' with conflicts.\n", patch->new_name);
+		if (!state->be_silent)
+			fprintf(stderr,
+				"Applied patch to '%s' with conflicts.\n",
+				patch->new_name);
 	} else {
-		fprintf(stderr, "Applied patch to '%s' cleanly.\n", patch->new_name);
+		if (!state->be_silent)
+			fprintf(stderr,
+				"Applied patch to '%s' cleanly.\n",
+				patch->new_name);
 	}
 	return 0;
 }
@@ -4461,7 +4471,8 @@ static int write_out_one_reject(struct apply_state *state, struct patch *patch)
 			    "Applying patch %%s with %d rejects...",
 			    cnt),
 		    cnt);
-	say_patch_name(stderr, sb.buf, patch);
+	if (!state->be_silent)
+		say_patch_name(stderr, sb.buf, patch);
 	strbuf_release(&sb);
 
 	cnt = strlen(patch->new_name);
@@ -4488,10 +4499,12 @@ static int write_out_one_reject(struct apply_state *state, struct patch *patch)
 	     frag;
 	     cnt++, frag = frag->next) {
 		if (!frag->rejected) {
-			fprintf_ln(stderr, _("Hunk #%d applied cleanly."), cnt);
+			if (!state->be_silent)
+				fprintf_ln(stderr, _("Hunk #%d applied cleanly."), cnt);
 			continue;
 		}
-		fprintf_ln(stderr, _("Rejected hunk #%d."), cnt);
+		if (!state->be_silent)
+			fprintf_ln(stderr, _("Rejected hunk #%d."), cnt);
 		fprintf(rej, "%.*s", frag->size, frag->patch);
 		if (frag->patch[frag->size-1] != '\n')
 			fputc('\n', rej);
@@ -4540,8 +4553,10 @@ static int write_out_results(struct apply_state *state, struct patch *list)
 		struct string_list_item *item;
 
 		string_list_sort(&cpath);
-		for_each_string_list_item(item, &cpath)
-			fprintf(stderr, "U %s\n", item->string);
+		if (!state->be_silent) {
+			for_each_string_list_item(item, &cpath)
+				fprintf(stderr, "U %s\n", item->string);
+		}
 		string_list_clear(&cpath, 0);
 
 		rerere(0);
diff --git a/apply.h b/apply.h
index 27b26a2..2dd3706 100644
--- a/apply.h
+++ b/apply.h
@@ -44,6 +44,7 @@ struct apply_state {
 	int apply_in_reverse;
 	int apply_with_reject;
 	int apply_verbosely;
+	int be_silent;
 
 	/* --cached updates only the cache without ever touching the working tree. */
 	int cached;
-- 
2.8.2.490.g3dabe57

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

* [PATCH v2 87/94] apply: make 'be_silent' incomatible with 'apply_verbosely'
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (84 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 86/94] apply: add 'be_silent' variable to 'struct apply_state' Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-11 13:17 ` [PATCH v2 88/94] apply: don't print on stdout when be_silent is set Christian Couder
                   ` (8 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

It should be an error to have both be_silent and apply_verbosely set,
so let's check that in check_apply_state().

And by the way let's not automatically set apply_verbosely when
be_silent is set.

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

diff --git a/apply.c b/apply.c
index f69a61a..5459ee1 100644
--- a/apply.c
+++ b/apply.c
@@ -113,8 +113,11 @@ int check_apply_state(struct apply_state *state, int force_apply)
 			return error(_("--3way outside a repository"));
 		state->check_index = 1;
 	}
-	if (state->apply_with_reject)
-		state->apply = state->apply_verbosely = 1;
+	if (state->apply_with_reject) {
+		state->apply = 1;
+		if (!state->be_silent)
+			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)
@@ -126,6 +129,9 @@ int check_apply_state(struct apply_state *state, int force_apply)
 	}
 	if (state->check_index)
 		state->unsafe_paths = 0;
+	if (state->be_silent && state->apply_verbosely)
+		return error(_("incompatible internal 'be_silent' and 'apply_verbosely' flags"));
+
 	return 0;
 }
 
-- 
2.8.2.490.g3dabe57

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

* [PATCH v2 88/94] apply: don't print on stdout when be_silent is set
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (85 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 87/94] apply: make 'be_silent' incomatible with 'apply_verbosely' Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-11 13:17 ` [PATCH v2 89/94] usage: add set_warn_routine() Christian Couder
                   ` (7 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

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

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

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

* [PATCH v2 89/94] usage: add set_warn_routine()
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (86 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 88/94] apply: don't print on stdout when be_silent is set Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-11 13:17 ` [PATCH v2 90/94] usage: add get_error_routine() and get_warn_routine() Christian Couder
                   ` (6 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

There are already set_die_routine() and set_error_routine(),
so let's add set_warn_routine() as this will be needed in a
following commit.

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

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

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

* [PATCH v2 90/94] usage: add get_error_routine() and get_warn_routine()
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (87 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 89/94] usage: add set_warn_routine() Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-11 13:17 ` [PATCH v2 91/94] apply: change error_routine when be_silent is set Christian Couder
                   ` (5 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

Let's make it possible to get the current error_routine and warn_routine,
so that we can store them before using set_error_routine() or
set_warn_routine() to use new ones.

This way we will be able put back the original routines, when we are done
with using new ones.

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

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

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

* [PATCH v2 91/94] apply: change error_routine when be_silent is set
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (88 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 90/94] usage: add get_error_routine() and get_warn_routine() Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-11 13:17 ` [PATCH v2 92/94] am: use be_silent in 'struct apply_state' to shut up applying patches Christian Couder
                   ` (4 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

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

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

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

* [PATCH v2 92/94] am: use be_silent in 'struct apply_state' to shut up applying patches
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (89 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 91/94] apply: change error_routine when be_silent is set Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-11 13:17 ` [PATCH v2 93/94] run-command: make dup_devnull() static again Christian Couder
                   ` (3 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

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

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

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

* [PATCH v2 93/94] run-command: make dup_devnull() static again
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (90 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 92/94] am: use be_silent in 'struct apply_state' to shut up applying patches Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-11 13:17 ` [PATCH v2 94/94] builtin/apply: add a cli option for be_silent Christian Couder
                   ` (2 subsequent siblings)
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

As there is no caller of dup_devnull() outside run-command.c any more,
let's make dup_devnull() static again.

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 run-command.c | 2 +-
 run-command.h | 6 ------
 2 files changed, 1 insertion(+), 7 deletions(-)

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

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

* [PATCH v2 94/94] builtin/apply: add a cli option for be_silent
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (91 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 93/94] run-command: make dup_devnull() static again Christian Couder
@ 2016-05-11 13:17 ` Christian Couder
  2016-05-12 17:06 ` [PATCH v2 00/94] libify apply and use lib in am Johannes Sixt
  2016-05-13  6:32 ` Johannes Schindelin
  94 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-11 13:17 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

Let's make it possible to request a silent operation on the
command line.

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

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

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

* Re: [PATCH v2 00/94] libify apply and use lib in am
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (92 preceding siblings ...)
  2016-05-11 13:17 ` [PATCH v2 94/94] builtin/apply: add a cli option for be_silent Christian Couder
@ 2016-05-12 17:06 ` Johannes Sixt
  2016-05-12 18:02   ` Christian Couder
  2016-05-12 19:04   ` Junio C Hamano
  2016-05-13  6:32 ` Johannes Schindelin
  94 siblings, 2 replies; 148+ messages in thread
From: Johannes Sixt @ 2016-05-12 17:06 UTC (permalink / raw)
  To: Christian Couder
  Cc: git, Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

Am 11.05.2016 um 15:16 schrieb 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.

I'm including this in my build on Windows. It passes the test suite.

I'll also use it in production for a while, although I am not a git-am 
consumer nor do I use git-rebase without -i, hence, my tests will 
probably only show that there is no bad fall-out.

-- Hannes

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

* Re: [PATCH v2 00/94] libify apply and use lib in am
  2016-05-12 17:06 ` [PATCH v2 00/94] libify apply and use lib in am Johannes Sixt
@ 2016-05-12 18:02   ` Christian Couder
  2016-06-09 21:10     ` Johannes Sixt
  2016-05-12 19:04   ` Junio C Hamano
  1 sibling, 1 reply; 148+ messages in thread
From: Christian Couder @ 2016-05-12 18:02 UTC (permalink / raw)
  To: Johannes Sixt
  Cc: git, Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

On Thu, May 12, 2016 at 7:06 PM, Johannes Sixt <j6t@kdbg.org> wrote:
> Am 11.05.2016 um 15:16 schrieb 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.
>
>
> I'm including this in my build on Windows. It passes the test suite.

Great! Thanks for the report!

> I'll also use it in production for a while, although I am not a git-am
> consumer nor do I use git-rebase without -i, hence, my tests will probably
> only show that there is no bad fall-out.

Thanks anyway,
Christian.

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

* Re: [PATCH v2 00/94] libify apply and use lib in am
  2016-05-12 17:06 ` [PATCH v2 00/94] libify apply and use lib in am Johannes Sixt
  2016-05-12 18:02   ` Christian Couder
@ 2016-05-12 19:04   ` Junio C Hamano
  2016-05-12 20:05     ` Christian Couder
  1 sibling, 1 reply; 148+ messages in thread
From: Junio C Hamano @ 2016-05-12 19:04 UTC (permalink / raw)
  To: Johannes Sixt
  Cc: Christian Couder, git, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

Johannes Sixt <j6t@kdbg.org> writes:

> I'll also use it in production for a while, although I am not a git-am
> consumer nor do I use git-rebase without -i, hence, my tests will
> probably only show that there is no bad fall-out.

It will probably only show that you do not use the part that was
touched by the series ;-)

As Christian said in 00/94, this probably needs to go in steps, as I
do not think anybody wants to review fouteen rounds of 90+ patch
series.  I thought the early 40+ patches in the series were at least
cursory reviewed already?

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

* Re: [PATCH v2 01/94] builtin/apply: make gitdiff_verify_name() return void
  2016-05-11 13:16 ` [PATCH v2 01/94] builtin/apply: make gitdiff_verify_name() return void Christian Couder
@ 2016-05-12 19:06   ` Junio C Hamano
  0 siblings, 0 replies; 148+ messages in thread
From: Junio C Hamano @ 2016-05-12 19:06 UTC (permalink / raw)
  To: Christian Couder
  Cc: git, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

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

> As the value returned by gitdiff_verify_name() is put into the
> same variable that is passed as a parameter to this function,
> it is simpler to pass the address of the variable and have
> gitdiff_verify_name() change the variable itself.
>
> This also makes it possible to later have this function return
> -1 instead of die()ing in case of error.
>
> Reviewed-by: Stefan Beller <sbeller@google.com>
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---

While I do not agree with "it is simpler" at all (it makes it harder
for callers if they ever want to use different variables--they would
need to invent a temporary variable just for that, and it makes it
no difference for existing callers that happen to use the same
variable), I like the way the real reason why we want to do this is
mentioned in the proposed log message.

Will queue.

>  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;
>  }

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

* Re: [PATCH v2 02/94] builtin/apply: avoid parameter shadowing 'p_value' global
  2016-05-11 13:16 ` [PATCH v2 02/94] builtin/apply: avoid parameter shadowing 'p_value' global Christian Couder
@ 2016-05-12 19:09   ` Junio C Hamano
  0 siblings, 0 replies; 148+ messages in thread
From: Junio C Hamano @ 2016-05-12 19:09 UTC (permalink / raw)
  To: Christian Couder
  Cc: git, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

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

> Let's just rename the global 'state_p_value' as it will become
> 'state->p_value' in a following patch.
>
> This also avoid errors when compiling with -Wshadow and makes
> it safer to later move global variables into a "state" struct.

Looks correctly done (I looked at remaining instances of "p_value").

Will queue.  Thanks.

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

* Re: [PATCH v2 03/94] builtin/apply: avoid parameter shadowing 'linenr' global
  2016-05-11 13:16 ` [PATCH v2 03/94] builtin/apply: avoid parameter shadowing 'linenr' global Christian Couder
@ 2016-05-12 19:11   ` Junio C Hamano
  0 siblings, 0 replies; 148+ messages in thread
From: Junio C Hamano @ 2016-05-12 19:11 UTC (permalink / raw)
  To: Christian Couder
  Cc: git, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

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

> Let's just rename the global 'state_linenr' as it will become
> 'state->linenr' in a following patch.
>
> This also avoid errors when compiling with -Wshadow and makes
> it safer to later move global variables into a "state" struct.

Looks correctly done (I looked at remaining instances of "linenr").

Will queue.  Thanks.

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

* Re: [PATCH v2 05/94] builtin/apply: extract line_by_line_fuzzy_match() from match_fragment()
  2016-05-11 13:16 ` [PATCH v2 05/94] builtin/apply: extract line_by_line_fuzzy_match() from match_fragment() Christian Couder
@ 2016-05-12 19:20   ` Junio C Hamano
  0 siblings, 0 replies; 148+ messages in thread
From: Junio C Hamano @ 2016-05-12 19:20 UTC (permalink / raw)
  To: Christian Couder
  Cc: git, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

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

> The match_fragment() function is very big and contains a big special case
> algorithm that does line by line fuzzy matching. So let's extract this
> algorithm in a separate line_by_line_fuzzy_match() function.
>
> Reviewed-by: Stefan Beller <sbeller@google.com>
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---

Looks good.

I am puzzled by the "git blame -w -M" output after applying this
patch, though.  The body of the new function came more or less
verbatim from the origina with reindentation.

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

* Re: [PATCH v2 09/94] builtin/apply: move 'state' init into init_apply_state()
  2016-05-11 13:16 ` [PATCH v2 09/94] builtin/apply: move 'state' init into init_apply_state() Christian Couder
@ 2016-05-12 19:25   ` Junio C Hamano
  0 siblings, 0 replies; 148+ messages in thread
From: Junio C Hamano @ 2016-05-12 19:25 UTC (permalink / raw)
  To: Christian Couder
  Cc: git, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

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

> When the apply functionality will be libified, the 'struct apply_state'
> will be used by different pieces of code.
>
> To properly initialize a 'struct apply_state', let's provide a nice
> and easy to use init_apply_state() function.

This probably should be done at 08/94, but I'll let it pass for now.
I am hoping 'prefix' is just one of a very few parameters this
function needs to take to initialize this (I do not think we want to
feed many different parameters to this function to initialize).

Will queue.

> Helped-by: Eric Sunshine <sunshine@sunshineco.com>
> Reviewed-by: Stefan Beller <sbeller@google.com>
> 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 ae068e7..e133033 100644
> --- a/builtin/apply.c
> +++ b/builtin/apply.c
> @@ -4522,6 +4522,19 @@ static int option_parse_directory(const struct option *opt,
>  	return 0;
>  }
>  
> +static void init_apply_state(struct apply_state *state, const char *prefix)
> +{
> +	memset(state, 0, sizeof(*state));
> +	state->prefix = prefix;
> +	state->prefix_length = state->prefix ? strlen(state->prefix) : 0;
> +
> +	git_apply_config();
> +	if (apply_default_whitespace)
> +		parse_whitespace_option(apply_default_whitespace);
> +	if (apply_default_ignorewhitespace)
> +		parse_ignorewhitespace_option(apply_default_ignorewhitespace);
> +}
> +
>  int cmd_apply(int argc, const char **argv, const char *prefix_)
>  {
>  	int i;
> @@ -4603,15 +4616,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
>  		OPT_END()
>  	};
>  
> -	memset(&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);
> +	init_apply_state(&state, prefix_);
>  
>  	argc = parse_options(argc, argv, state.prefix, builtin_apply_options,
>  			apply_usage, 0);

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

* Re: [PATCH v2 10/94] builtin/apply: move 'unidiff_zero' global into 'struct apply_state'
  2016-05-11 13:16 ` [PATCH v2 10/94] builtin/apply: move 'unidiff_zero' global into 'struct apply_state' Christian Couder
@ 2016-05-12 19:28   ` Junio C Hamano
  2016-05-12 20:18     ` Christian Couder
  0 siblings, 1 reply; 148+ messages in thread
From: Junio C Hamano @ 2016-05-12 19:28 UTC (permalink / raw)
  To: Christian Couder
  Cc: git, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

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

> To libify the apply functionality the 'unidiff_zero' variable should
> not be static and global to the file. Let's move it into
> 'struct apply_state'.
>
> Reviewed-by: Stefan Beller <sbeller@google.com>
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---

Looks correct; I would have expected from the flow of thought in the
series that p_value and linenr that were already mentioned would be
the first two to be put in this, but that would have to wait until
more functions are taught to pass the state around, so this ordering
would probably be fine.

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

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

* Re: [PATCH v2 16/94] builtin/apply: move 'update_index' global into 'struct apply_state'
  2016-05-11 13:16 ` [PATCH v2 16/94] builtin/apply: move 'update_index' " Christian Couder
@ 2016-05-12 19:31   ` Junio C Hamano
  0 siblings, 0 replies; 148+ messages in thread
From: Junio C Hamano @ 2016-05-12 19:31 UTC (permalink / raw)
  To: Christian Couder
  Cc: git, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

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

> To libify the apply functionality the 'update_index' variable should
> not be static and global to the file. Let's move it into
> 'struct apply_state'.
>
> Reviewed-by: Stefan Beller <sbeller@google.com>
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---
>  builtin/apply.c | 45 ++++++++++++++++++++++++++-------------------
>  1 file changed, 26 insertions(+), 19 deletions(-)
>
> diff --git a/builtin/apply.c b/builtin/apply.c
> index 97af6ea..635a9ff 100644
> --- a/builtin/apply.c
> +++ b/builtin/apply.c
> @@ -39,6 +39,7 @@ struct apply_state {
>  	int check_index;
>  
>  	int unidiff_zero;
> +	int update_index;

This should sit right next to check_index, I would think.

Otherwise looks correct.

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

* Re: [PATCH v2 18/94] builtin/apply: move 'cached' global into 'struct apply_state'
  2016-05-11 13:16 ` [PATCH v2 18/94] builtin/apply: move 'cached' " Christian Couder
@ 2016-05-12 19:33   ` Junio C Hamano
  0 siblings, 0 replies; 148+ messages in thread
From: Junio C Hamano @ 2016-05-12 19:33 UTC (permalink / raw)
  To: Christian Couder
  Cc: git, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

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

> To libify the apply functionality the 'cached' variable should
> not be static and global to the file. Let's move it into
> 'struct apply_state'.
>
> Reviewed-by: Stefan Beller <sbeller@google.com>
> 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 8791b28..09af5dc 100644
> --- a/builtin/apply.c
> +++ b/builtin/apply.c
> @@ -30,6 +30,9 @@ struct apply_state {
>  	int apply_with_reject;
>  	int apply_verbosely;
>  
> +	/* --cached updates only the cache without ever touching the working tree. */
> +	int cached;
> +

Again, this should sit right next to update_index and check_index.

Other than that, this step looks correct.

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

* Re: [PATCH v2 22/94] builtin/apply: move 'threeway' global into 'struct apply_state'
  2016-05-11 13:16 ` [PATCH v2 22/94] builtin/apply: move 'threeway' " Christian Couder
@ 2016-05-12 19:41   ` Junio C Hamano
  2016-05-12 20:26     ` Christian Couder
  0 siblings, 1 reply; 148+ messages in thread
From: Junio C Hamano @ 2016-05-12 19:41 UTC (permalink / raw)
  To: Christian Couder
  Cc: git, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

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

> To libify the apply functionality the 'threeway' variable should
> not be static and global to the file. Let's move it into
> 'struct apply_state'.
>
> Reviewed-by: Stefan Beller <sbeller@google.com>
> 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 6216723..3650922 100644
> --- a/builtin/apply.c
> +++ b/builtin/apply.c
> @@ -40,6 +40,7 @@ struct apply_state {
>  	int numstat;
>  
>  	int summary;
> +	int threeway;

This makes threeway look as if it is one of the cosmetic options
like stat/numstat/summary, doesn't it?  If anything, the blank
between numstat and summary should be moved below summary.  I'd
group knobs that affect "what is affected (check, check_index,
cached, etc.)", "how the application is done (allow-overlap,
threeway, in-reverse, etc.)" and "cosmetics" and place the ones in
the same group next to each other.

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

* Re: [PATCH v2 33/94] builtin/apply: move 'p_value_known' global into 'struct apply_state'
  2016-05-11 13:16 ` [PATCH v2 33/94] builtin/apply: move 'p_value_known' " Christian Couder
@ 2016-05-12 19:43   ` Junio C Hamano
  0 siblings, 0 replies; 148+ messages in thread
From: Junio C Hamano @ 2016-05-12 19:43 UTC (permalink / raw)
  To: Christian Couder
  Cc: git, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

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

> To libify the apply functionality the 'p_value_known' variable should
> not be static and global to the file. Let's move it into
> 'struct apply_state'.

This and p_value belong together, I would think, so this can be
squashed with 32/94 if the series is to be rerolled (this alone is
not a reason to reroll the series, though).

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

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

* Re: [PATCH v2 40/94] builtin/apply: move 'ws_error_action' into 'struct apply_state'
  2016-05-11 13:16 ` [PATCH v2 40/94] builtin/apply: move 'ws_error_action' " Christian Couder
@ 2016-05-12 19:48   ` Junio C Hamano
  0 siblings, 0 replies; 148+ messages in thread
From: Junio C Hamano @ 2016-05-12 19:48 UTC (permalink / raw)
  To: Christian Couder
  Cc: git, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

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

>  struct apply_state {
>  	const char *prefix;
>  	int prefix_length;
> @@ -71,6 +78,8 @@ struct apply_state {
>  	int whitespace_error;
>  	int squelch_whitespace_errors;
>  	int applied_after_fixing_ws;
> +
> +	enum ws_error_action ws_error_action;

It is very good that these whitespace-error related things are
sitting next to each other.  I do not think the extra blank line is
warranted, though.

If anything, I'd say error-action should come at the beginning (as
it is an end-user input that is set before the processing happens),
and other three are states (that get new values after processing is
done) that should be listed after, to preserve the rough
chronological order the variables are used (which translates to the
order the readers need to become aware of and understand these
variables).

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

* Re: [PATCH v2 48/94] builtin/apply: rename 'prefix_' parameter to 'prefix'
  2016-05-11 13:16 ` [PATCH v2 48/94] builtin/apply: rename 'prefix_' parameter to 'prefix' Christian Couder
@ 2016-05-12 19:56   ` Junio C Hamano
  2016-05-12 20:43     ` Junio C Hamano
  2016-05-13 19:42     ` Christian Couder
  0 siblings, 2 replies; 148+ messages in thread
From: Junio C Hamano @ 2016-05-12 19:56 UTC (permalink / raw)
  To: Christian Couder
  Cc: git, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

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

> This is just a small cleanup.

... which may have been better happened at 09/94.

Up to this point, the conversion looks quite sensible, even though I
think the organization of fields in apply_state do not look logical.

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

* Re: [PATCH v2 00/94] libify apply and use lib in am
  2016-05-12 19:04   ` Junio C Hamano
@ 2016-05-12 20:05     ` Christian Couder
  0 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-12 20:05 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: Johannes Sixt, git, Ævar Arnfjörð,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

On Thu, May 12, 2016 at 9:04 PM, Junio C Hamano <gitster@pobox.com> wrote:
>
> As Christian said in 00/94, this probably needs to go in steps, as I
> do not think anybody wants to review fouteen rounds of 90+ patch
> series.  I thought the early 40+ patches in the series were at least
> cursory reviewed already?

Yeah, Stefan said he was ok to give his Reviewed-by to v1 patches 01
to 47 and they haven't changed much from v1 to v2.

Here is the diff for those patches:

> git diff 57be628 66c994d
diff --git a/builtin/apply.c b/builtin/apply.c
index 787426f..67c64a5 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -67,13 +67,15 @@ struct apply_state {
        /* --numstat does numeric diffstat, and doesn't actually apply */
        int numstat;

-       const char *fake_ancestor;
-
        int summary;
-
        int threeway;
-
        int no_add;
+       const char *fake_ancestor;
+       const char *patch_input_file;
+       struct string_list limit_by_name;
+       int has_include;
+       struct strbuf root;
+       struct string_list symlink_changes;

        /*
         *  --check turns on checking that the working tree matches the
@@ -85,11 +87,8 @@ struct apply_state {
        int check_index;

        int unidiff_zero;
-
        int update_index;
-
        int unsafe_paths;
-
        int line_termination;

        /*
@@ -113,19 +112,10 @@ struct apply_state {
         */
        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;
@@ -1626,7 +1616,7 @@ static void record_ws_error(struct apply_state *state,
                            unsigned result,
                            const char *line,
                            int len,
-                           int l_nr)
+                           int linenr)
 {
        char *err;

@@ -1640,7 +1630,7 @@ static void record_ws_error(struct apply_state *state,

        err = whitespace_error_string(result);
        fprintf(stderr, "%s:%d: %s.\n%.*s\n",
-               state->patch_input_file, l_nr, err, len, line);
+               state->patch_input_file, linenr, err, len, line);
        free(err);
 }

@@ -2445,7 +2435,7 @@ static int match_fragment(struct apply_state *state,
                          int match_beginning, int match_end)
 {
        int i;
-       char *fixed_buf, *orig, *target;
+       char *fixed_buf, *buf, *orig, *target;
        struct strbuf fixed;
        size_t fixed_len, postlen;
        int preimage_limit;
@@ -2506,7 +2496,6 @@ static int match_fragment(struct apply_state *state,
                 * 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;
@@ -4670,10 +4659,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';

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

* Re: [PATCH v2 10/94] builtin/apply: move 'unidiff_zero' global into 'struct apply_state'
  2016-05-12 19:28   ` Junio C Hamano
@ 2016-05-12 20:18     ` Christian Couder
  0 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-12 20:18 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Ævar Arnfjörð,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

On Thu, May 12, 2016 at 9:28 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Christian Couder <christian.couder@gmail.com> writes:
>
>> To libify the apply functionality the 'unidiff_zero' variable should
>> not be static and global to the file. Let's move it into
>> 'struct apply_state'.
>>
>> Reviewed-by: Stefan Beller <sbeller@google.com>
>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
>> ---
>
> Looks correct; I would have expected from the flow of thought in the
> series that p_value and linenr that were already mentioned would be
> the first two to be put in this, but that would have to wait until
> more functions are taught to pass the state around, so this ordering
> would probably be fine.

Yeah, if the patches that put p_value and linenr into 'struct
apply_state' were earlier in the series, they would have to pass the
state around to a lot more functions, so they would have been very
big.

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

* Re: [PATCH v2 22/94] builtin/apply: move 'threeway' global into 'struct apply_state'
  2016-05-12 19:41   ` Junio C Hamano
@ 2016-05-12 20:26     ` Christian Couder
  2016-05-12 21:21       ` Junio C Hamano
  0 siblings, 1 reply; 148+ messages in thread
From: Christian Couder @ 2016-05-12 20:26 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Ævar Arnfjörð,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

On Thu, May 12, 2016 at 9:41 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Christian Couder <christian.couder@gmail.com> writes:
>
>> To libify the apply functionality the 'threeway' variable should
>> not be static and global to the file. Let's move it into
>> 'struct apply_state'.
>>
>> Reviewed-by: Stefan Beller <sbeller@google.com>
>> 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 6216723..3650922 100644
>> --- a/builtin/apply.c
>> +++ b/builtin/apply.c
>> @@ -40,6 +40,7 @@ struct apply_state {
>>       int numstat;
>>
>>       int summary;
>> +     int threeway;
>
> This makes threeway look as if it is one of the cosmetic options
> like stat/numstat/summary, doesn't it?  If anything, the blank
> between numstat and summary should be moved below summary.

There is a blank because there is a comment on the line before
numstat. The result looks like this:

    /* --cached updates only the cache without ever touching the
working tree. */
    int cached;

    /* --stat does just a diffstat, and doesn't actually apply */
    int diffstat;

    /* --numstat does numeric diffstat, and doesn't actually apply */
    int numstat;

    int summary;
    int threeway;
    int no_add;


>  I'd
> group knobs that affect "what is affected (check, check_index,
> cached, etc.)", "how the application is done (allow-overlap,
> threeway, in-reverse, etc.)" and "cosmetics" and place the ones in
> the same group next to each other.

Ok, I will try to group knobs like that, but the comments tend to
break the groups.

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

* Re: [PATCH v2 48/94] builtin/apply: rename 'prefix_' parameter to 'prefix'
  2016-05-12 19:56   ` Junio C Hamano
@ 2016-05-12 20:43     ` Junio C Hamano
  2016-05-13 19:45       ` Christian Couder
  2016-05-13 19:42     ` Christian Couder
  1 sibling, 1 reply; 148+ messages in thread
From: Junio C Hamano @ 2016-05-12 20:43 UTC (permalink / raw)
  To: Christian Couder
  Cc: git, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

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

> Up to this point, the conversion looks quite sensible, even though I
> think the organization of fields in apply_state do not look logical.

I'd stop here for now, as everything before this step looks
uncontroversial.  Anybody whose tasked to move the global state for
these variables into a structure would reach the samestate after
applying these 48 patches, modulo minor differences in the way the
comments would say things, how the patches are split and how the
fields in apply_state() are organized.

One thing that is missing is a counterpart of init_apply_state().
In the early part of patches where it added only "const char *"
borrowed from the caller and fields of intregral type, the lack of
clear_apply_state() did not mattter, but with a few fields with
"string_list" type, anybody who want to make repeated call into the
apply machinery would want a way to release the resource the
structure holds.

Because 49/94 is a step to add an unfreeable resource, this is a
good place to stop and then add the clean_apply_state() before that
happens, I would think.  After that, I think both the contributor
and the reviewers would benefit if these early patches are merged
early without waiting for the review of the remainder.

Thanks.

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

* Re: [PATCH v2 22/94] builtin/apply: move 'threeway' global into 'struct apply_state'
  2016-05-12 20:26     ` Christian Couder
@ 2016-05-12 21:21       ` Junio C Hamano
  0 siblings, 0 replies; 148+ messages in thread
From: Junio C Hamano @ 2016-05-12 21:21 UTC (permalink / raw)
  To: Christian Couder
  Cc: git, Ævar Arnfjörð,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

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

> Ok, I will try to group knobs like that, but the comments tend to
> break the groups.

By keeping the comment on a single field short, and reserve comment
occupying its own line(s) to comment on group, you can do

	/* These control what gets looked at and modified */
        int	apply;	/* this is not a dry-run */
        int	check_index; /* preimage must match the indexed version */
        int	cached; /* apply to the index only */

	/* These control cosmetic aspect of the output */
        int	diffstat; /* show diffstat */
        int	numstat; /* numerical stat */
        int	summary; /* report creation, deletion, etc. */

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

* Re: [PATCH v2 00/94] libify apply and use lib in am
  2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
                   ` (93 preceding siblings ...)
  2016-05-12 17:06 ` [PATCH v2 00/94] libify apply and use lib in am Johannes Sixt
@ 2016-05-13  6:32 ` Johannes Schindelin
  2016-05-13 18:49   ` Christian Couder
  94 siblings, 1 reply; 148+ messages in thread
From: Johannes Schindelin @ 2016-05-13  6:32 UTC (permalink / raw)
  To: Christian Couder
  Cc: git, Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Eric Sunshine, Ramsay Jones,
	Jeff King, Karsten Blees, Matthieu Moy, Christian Couder

Hi Chris,

On Wed, 11 May 2016, Christian Couder wrote:

> I consider that the apply functionality is properly libified before
> these patches, and that they should be in a separate series, but
> unfortunately using the libified apply in "git am" unmasks the fact that
> "git am", since it was a shell script, has been silencing the apply
> functionality by "futzing with file descriptors". And unfortunately this
> makes some reviewers unhappy.

It is a misrepresentation to claim that this makes some reviewers unhappy.
Speaking for myself, I am very happy. Despite having had to point out
that the previous iteration of this patch series had a serious flaw.

It is also incorrect to say that the shell script had been "futzing with
the file descriptors". You see, the shell script's *own* file descriptors
had been left completely unaffected by the redirection of the spawned
process' output. That was perfectly fine a thing to do, even if it
possibly hid fatal errors. Shell scripts are simply very limiting. The
problem was introduced by v1 of this patch series, which changed *the
caller's file descriptors* back and forth simply because the called code
no longer runs in a separate process. And *that* was, and is, improper.

> By the way there are no tests yet for this new feature, and I am not
> sure at all that "--silent" and "be_silent" are good names.

If you want to follow existing code's example, we typically call this
option "quiet".

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

It is preferred. Much.

Ciao,
Dscho

P.S.: Even two ~40-strong patch series are *really* painful to review. I
believe you can do a much better job at cutting this monster down into
palatable chunks, each with its own sweet little story.

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

* Re: [PATCH v2 00/94] libify apply and use lib in am
  2016-05-13  6:32 ` Johannes Schindelin
@ 2016-05-13 18:49   ` Christian Couder
  2016-05-14  6:26     ` Johannes Schindelin
  0 siblings, 1 reply; 148+ messages in thread
From: Christian Couder @ 2016-05-13 18:49 UTC (permalink / raw)
  To: Johannes Schindelin
  Cc: git, Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Eric Sunshine, Ramsay Jones,
	Jeff King, Karsten Blees, Matthieu Moy, Christian Couder

Hi Dscho,

On Fri, May 13, 2016 at 8:32 AM, Johannes Schindelin
<Johannes.Schindelin@gmx.de> wrote:
> Hi Chris,
>
> On Wed, 11 May 2016, Christian Couder wrote:
>
>> I consider that the apply functionality is properly libified before
>> these patches, and that they should be in a separate series, but
>> unfortunately using the libified apply in "git am" unmasks the fact that
>> "git am", since it was a shell script, has been silencing the apply
>> functionality by "futzing with file descriptors". And unfortunately this
>> makes some reviewers unhappy.
>
> It is a misrepresentation to claim that this makes some reviewers unhappy.
> Speaking for myself, I am very happy. Despite having had to point out
> that the previous iteration of this patch series had a serious flaw.
>
> It is also incorrect to say that the shell script had been "futzing with
> the file descriptors". You see, the shell script's *own* file descriptors
> had been left completely unaffected by the redirection of the spawned
> process' output. That was perfectly fine a thing to do, even if it
> possibly hid fatal errors. Shell scripts are simply very limiting. The
> problem was introduced by v1 of this patch series, which changed *the
> caller's file descriptors* back and forth simply because the called code
> no longer runs in a separate process. And *that* was, and is, improper.

I think we should just agree that we disagree on what we think the v1
was doing and move on to v2.

>> By the way there are no tests yet for this new feature, and I am not
>> sure at all that "--silent" and "be_silent" are good names.
>
> If you want to follow existing code's example, we typically call this
> option "quiet".

In the documentation there is:

Documentation/git-am.txt:--quiet::
Documentation/git-am.txt:       Be quiet. Only print error messages.
Documentation/git-branch.txt:--quiet::
Documentation/git-branch.txt:   Be more quiet when creating or
deleting a branch, suppressing
Documentation/git-branch.txt-   non-error messages.
Documentation/git-checkout-index.txt:--quiet::
Documentation/git-checkout-index.txt:   be quiet if files exist or are
not in the index
Documentation/git-checkout.txt:--quiet::
Documentation/git-checkout.txt- Quiet, suppress feedback messages.
Documentation/git-clean.txt:--quiet::
Documentation/git-clean.txt:    Be quiet, only report errors, but not
the files that are
Documentation/git-clean.txt-    successfully removed.
Documentation/git-clone.txt--q::
Documentation/git-clone.txt:    Operate quietly.  Progress is not
reported to the standard
Documentation/git-clone.txt-    error stream.
ocumentation/git-commit.txt:--quiet::
Documentation/git-commit.txt-   Suppress commit summary message.
Documentation/git-fast-import.txt:--quiet::
Documentation/git-fast-import.txt-      Disable all non-fatal output,
making fast-import silent when it
...

So it looks to me that --quiet means something like "don't tell the
story of your life, but in case of problem you are allowed to
complain". In other word --quiet generally doesn't suppress error
messages from error() or die().

On the contrary the new feature I tentatively called --silent does
suppress all output including error messages from error().

Now if people think that it is not worth making a difference between
the different behaviors, then I am ok to rename it --quiet, though I
wonder what will happen if people later want a --quiet that does only
what --quiet usually does to the other commands.

>> Sorry if this patch series is long. I can split it into two or more
>> series if it is prefered.
>
> It is preferred. Much.

Ok, I will split it then.

Thanks,
Christian.

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

* Re: [PATCH v2 48/94] builtin/apply: rename 'prefix_' parameter to 'prefix'
  2016-05-12 19:56   ` Junio C Hamano
  2016-05-12 20:43     ` Junio C Hamano
@ 2016-05-13 19:42     ` Christian Couder
  2016-05-24  8:15       ` Christian Couder
  1 sibling, 1 reply; 148+ messages in thread
From: Christian Couder @ 2016-05-13 19:42 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Ævar Arnfjörð,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

On Thu, May 12, 2016 at 9:56 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Christian Couder <christian.couder@gmail.com> writes:
>
>> This is just a small cleanup.
>
> ... which may have been better happened at 09/94.

Ok, I will squash it in 09/94.

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

* Re: [PATCH v2 48/94] builtin/apply: rename 'prefix_' parameter to 'prefix'
  2016-05-12 20:43     ` Junio C Hamano
@ 2016-05-13 19:45       ` Christian Couder
  2016-05-14 18:27         ` Junio C Hamano
  0 siblings, 1 reply; 148+ messages in thread
From: Christian Couder @ 2016-05-13 19:45 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Ævar Arnfjörð,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

On Thu, May 12, 2016 at 10:43 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Junio C Hamano <gitster@pobox.com> writes:
>
>> Up to this point, the conversion looks quite sensible, even though I
>> think the organization of fields in apply_state do not look logical.
>
> I'd stop here for now, as everything before this step looks
> uncontroversial.  Anybody whose tasked to move the global state for
> these variables into a structure would reach the samestate after
> applying these 48 patches, modulo minor differences in the way the
> comments would say things, how the patches are split and how the
> fields in apply_state() are organized.
>
> One thing that is missing is a counterpart of init_apply_state().
> In the early part of patches where it added only "const char *"
> borrowed from the caller and fields of intregral type, the lack of
> clear_apply_state() did not mattter, but with a few fields with
> "string_list" type, anybody who want to make repeated call into the
> apply machinery would want a way to release the resource the
> structure holds.
>
> Because 49/94 is a step to add an unfreeable resource, this is a
> good place to stop and then add the clean_apply_state() before that
> happens, I would think.  After that, I think both the contributor
> and the reviewers would benefit if these early patches are merged
> early without waiting for the review of the remainder.

Ok, I will add add the clean_apply_state() and resend the patches up
to that point soon, so that they can be merged early.

Thanks,
Christian.

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

* Re: [PATCH v2 00/94] libify apply and use lib in am
  2016-05-13 18:49   ` Christian Couder
@ 2016-05-14  6:26     ` Johannes Schindelin
  2016-05-14  9:19       ` Christian Couder
  0 siblings, 1 reply; 148+ messages in thread
From: Johannes Schindelin @ 2016-05-14  6:26 UTC (permalink / raw)
  To: Christian Couder
  Cc: git, Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Eric Sunshine, Ramsay Jones,
	Jeff King, Karsten Blees, Matthieu Moy, Christian Couder

Hi Chris,

On Fri, 13 May 2016, Christian Couder wrote:

> On Fri, May 13, 2016 at 8:32 AM, Johannes Schindelin
> <Johannes.Schindelin@gmx.de> wrote:
> >
> > On Wed, 11 May 2016, Christian Couder wrote:
> >
> >> I consider that the apply functionality is properly libified before
> >> these patches, and that they should be in a separate series, but
> >> unfortunately using the libified apply in "git am" unmasks the fact
> >> that "git am", since it was a shell script, has been silencing the
> >> apply functionality by "futzing with file descriptors". And
> >> unfortunately this makes some reviewers unhappy.
> >
> > It is a misrepresentation to claim that this makes some reviewers
> > unhappy.  Speaking for myself, I am very happy. Despite having had to
> > point out that the previous iteration of this patch series had a
> > serious flaw.
> >
> > It is also incorrect to say that the shell script had been "futzing
> > with the file descriptors". You see, the shell script's *own* file
> > descriptors had been left completely unaffected by the redirection of
> > the spawned process' output. That was perfectly fine a thing to do,
> > even if it possibly hid fatal errors. Shell scripts are simply very
> > limiting. The problem was introduced by v1 of this patch series, which
> > changed *the caller's file descriptors* back and forth simply because
> > the called code no longer runs in a separate process. And *that* was,
> > and is, improper.
> 
> I think we should just agree that we disagree on what we think the v1
> was doing and move on to v2.

It is obvious that you want to disagree. But that is counterproductive.

It is important to keep in mind, and needs to be remembered in future
libification efforts, that extra care is required when we no longer spawn
a separate process: you no longer have the option to change global state
*just* for the called process.

This is very true when redirecting (global) file descriptors. I struggle
with this myself in the rebase--helper branch, so it is not only you who
has to face, and address, this issue.

The same goes for die(), too, of course. And for statically allocated
memory. And even worse: when all of a sudden reusing static lockfile
structs. And, and, and. The list goes on.

It really comes back at us that we originally simple did not care about
cleaning up after us "because it is a short-lived process, anyway".

The thing is: this is not at all the philosophical discussion as which
your comment tries to color it. We *have* to address these issues when
libifying code. Yes, it's hard. Yes, it's tedious. Yes, I also want to
bitch about it.

And yes, it must be done.

> >> By the way there are no tests yet for this new feature, and I am not
> >> sure at all that "--silent" and "be_silent" are good names.
> >
> > If you want to follow existing code's example, we typically call this
> > option "quiet".
> 
> In the documentation there is: [... snip ...]
> ...
> 
> So it looks to me that --quiet means something like "don't tell the
> story of your life, but in case of problem you are allowed to
> complain". In other word --quiet generally doesn't suppress error
> messages from error() or die().

Right.

And if you care to take a step back, this is most likely what we want in
libified code.

Modulo die(), of course!

> On the contrary the new feature I tentatively called --silent does
> suppress all output including error messages from error().

And what would be the point of that? Now that we are libifying code, in
contrast to the spawned-process approach, we *can* discern between "prints
to stderr" and "displays an error". I'd wager that you won't find any
error() call in the code path that we want to silence.

> Now if people think that it is not worth making a difference between the
> different behaviors, then I am ok to rename it --quiet, though I wonder
> what will happen if people later want a --quiet that does only what
> --quiet usually does to the other commands.

You know, I get the distinct impression that you do not want my feedback
because you always want "people" to agree with my assessment. I hoped that
my arguments would make sense, but I guess I should spend my time
differently.

> >> Sorry if this patch series is long. I can split it into two or more
> >> series if it is prefered.
> >
> > It is preferred. Much.
> 
> Ok, I will split it then.

Thank you. Maybe you take me off of the Cc: list, too?

Thanks,
Dscho

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

* Re: [PATCH v2 00/94] libify apply and use lib in am
  2016-05-14  6:26     ` Johannes Schindelin
@ 2016-05-14  9:19       ` Christian Couder
  2016-05-14 18:31         ` Junio C Hamano
  0 siblings, 1 reply; 148+ messages in thread
From: Christian Couder @ 2016-05-14  9:19 UTC (permalink / raw)
  To: Johannes Schindelin
  Cc: git, Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Eric Sunshine, Ramsay Jones,
	Jeff King, Karsten Blees, Matthieu Moy, Christian Couder

On Sat, May 14, 2016 at 8:26 AM, Johannes Schindelin
<Johannes.Schindelin@gmx.de> wrote:

[...]

>> >> By the way there are no tests yet for this new feature, and I am not
>> >> sure at all that "--silent" and "be_silent" are good names.
>> >
>> > If you want to follow existing code's example, we typically call this
>> > option "quiet".
>>
>> In the documentation there is: [... snip ...]
>> ...
>>
>> So it looks to me that --quiet means something like "don't tell the
>> story of your life, but in case of problem you are allowed to
>> complain". In other word --quiet generally doesn't suppress error
>> messages from error() or die().
>
> Right.
>
> And if you care to take a step back, this is most likely what we want in
> libified code.

In the previous discussion, Junio asked:

"How far can you go with just set-error-routine?  Are there things,
other than the file descriptors, that you need to futz with in order
to covert that "we'd fallback, so this early round must be silent"
codepath?"

So it looks to me that the goal is to have something that replicate
the current behavior, which is to not even display messages from
error().

[...]

>> >> Sorry if this patch series is long. I can split it into two or more
>> >> series if it is prefered.
>> >
>> > It is preferred. Much.
>>
>> Ok, I will split it then.
>
> Thank you. Maybe you take me off of the Cc: list, too?

Ok I will do that.

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

* Re: [PATCH v2 48/94] builtin/apply: rename 'prefix_' parameter to 'prefix'
  2016-05-13 19:45       ` Christian Couder
@ 2016-05-14 18:27         ` Junio C Hamano
  2016-05-24  8:24           ` Christian Couder
  0 siblings, 1 reply; 148+ messages in thread
From: Junio C Hamano @ 2016-05-14 18:27 UTC (permalink / raw)
  To: Christian Couder
  Cc: git, Ævar Arnfjörð,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

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

> On Thu, May 12, 2016 at 10:43 PM, Junio C Hamano <gitster@pobox.com> wrote:
>> Junio C Hamano <gitster@pobox.com> writes:
>>
>>> Up to this point, the conversion looks quite sensible, even though I
>>> think the organization of fields in apply_state do not look logical.
>>
>> I'd stop here for now, as everything before this step looks
>> uncontroversial.  Anybody whose tasked to move the global state for
>> these variables into a structure would reach the samestate after
>> applying these 48 patches, modulo minor differences in the way the
>> comments would say things, how the patches are split and how the
>> fields in apply_state() are organized.
>>
>> One thing that is missing is a counterpart of init_apply_state().
>> In the early part of patches where it added only "const char *"
>> borrowed from the caller and fields of intregral type, the lack of
>> clear_apply_state() did not mattter, but with a few fields with
>> "string_list" type, anybody who want to make repeated call into the
>> apply machinery would want a way to release the resource the
>> structure holds.
>>
>> Because 49/94 is a step to add an unfreeable resource, this is a
>> good place to stop and then add the clean_apply_state() before that
>> happens, I would think.  After that, I think both the contributor
>> and the reviewers would benefit if these early patches are merged
>> early without waiting for the review of the remainder.
>
> Ok, I will add add the clean_apply_state() and resend the patches up
> to that point soon, so that they can be merged early.

Two more comments:

 - Just like the change between v1 and v2, if we were to introduce
   clear_apply_state(), it would be nicer if it were done early in
   the series, ideally at the same time as init_apply_state() gets
   introduced, or at least no later than the first field that holds
   resource that needs releasing is added.

 - I didn't double check if 50 and later still had changes that
   belong to this "early batch that moves things into a struct" (aka
   cc/apply-introduce-state) topic, as I stopped at 49 and saw the
   need for clear_apply_state().  So 48 may not be the ideal place
   to stop.  From a cursory read of their titles, perhaps 49, 50, 56
   and possibly 60 should be in this early series?  While 60 does
   sound like an immediate follow-up to 09/94, it depends on a few
   "die elimination" changes, so I do NOT think it must be in the
   early batch.

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

* Re: [PATCH v2 00/94] libify apply and use lib in am
  2016-05-14  9:19       ` Christian Couder
@ 2016-05-14 18:31         ` Junio C Hamano
  2016-05-14 19:37           ` Christian Couder
  0 siblings, 1 reply; 148+ messages in thread
From: Junio C Hamano @ 2016-05-14 18:31 UTC (permalink / raw)
  To: Christian Couder
  Cc: Johannes Schindelin, git, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Eric Sunshine, Ramsay Jones,
	Jeff King, Karsten Blees, Matthieu Moy, Christian Couder

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

>>> So it looks to me that --quiet means something like "don't tell the
>>> story of your life, but in case of problem you are allowed to
>>> complain". In other word --quiet generally doesn't suppress error
>>> messages from error() or die().
>>
>> Right.
>>
>> And if you care to take a step back, this is most likely what we want in
>> libified code.
>
> In the previous discussion, Junio asked:
>
> "How far can you go with just set-error-routine?  Are there things,
> other than the file descriptors, that you need to futz with in order
> to covert that "we'd fallback, so this early round must be silent"
> codepath?"
>
> So it looks to me that the goal is to have something that replicate
> the current behavior, which is to not even display messages from
> error().

Note that I was not endorsing the approach to use set-error-routine,
one of whose downside is that it is easy to silence everything
unconditionally.  Between --quiet/--silent, I wasn't making any
choice, as I hadn't even formed an opinion back when I wrote it.

I was merely asking "is use of set-error-routine the worst thing you
need to do, or do you need to do anything more gross?", half
anticipating "no, it turns out that I need to longjmp(3) from my
die-routine to come back from deep in the existing code because I do
not think I'd bother propagating the error status all the way along
the call stack", at which point my response would have been "Ugh,
use of set-error-routine to avoid properly refactoring, even if we
assume it is OK to squelch the errors unconditionally, is bad
enough.  If you need to longjmp(3), that's no longer a libification.
You are better off using the run_command() interface to give the
caller and the callee a proper isolation".

Libification is not just "now it runs inside a single process" at
all.  It is more about "it has proper abstraction and separation so
that callers have _more_ control than they used to (compared to
interacting with a spawned process) while retaining the control of
their own environment".

And as this thread showed, when turning an implementation that uses
run-command with program's standard error sent to /dev/null into a
"libified" implementation, we _gain_ a possibility to be more
selective in what we silence.  I think that exactly falls into
"callers have more control" category of libification benefit.

Quite honestly, I was sort of surprised by the quality of the
argument for proper libification Dscho has been making in this
thread, and that was why I didn't say much here.

Having said all that, as to --quiet/--silent, another way to surface
the "even more quiet" used is with "-q -q" (i.e. multiple levels of
quietness).  I am just saying this for the record without suggesting
it is better or worse suited for this case than what has been
discussed.

In any case, I do see the need for what your --silent option does
internally when a caller like "am" calls into the libified "apply"
machinery.  When we _know_ we will run a "fallback" invocation after
an initial "more strict" apply attempt fails, we do want the initial
invocation to be totally silent, not even with any error message,
because we know that it is sufficient to let the fallback invocation
show its error message (if it fails).  The fallback may even succeed
in which case error messages from the initial invocation serves only
as an explanation of the reason why we used fallback, which is not
interesting to the end user at all.

I however do not see a reason why you need to expose that feature to
the users of "git apply".  So I am not sure if any of us care deeply
the choice among --silent, --quiet and -q -q.

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

* Re: [PATCH v2 00/94] libify apply and use lib in am
  2016-05-14 18:31         ` Junio C Hamano
@ 2016-05-14 19:37           ` Christian Couder
  2016-05-15 18:30             ` Junio C Hamano
  0 siblings, 1 reply; 148+ messages in thread
From: Christian Couder @ 2016-05-14 19:37 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: Johannes Schindelin, git, Ævar Arnfjörð,
	Nguyen Thai Ngoc Duy, Stefan Beller, Eric Sunshine, Ramsay Jones,
	Jeff King, Karsten Blees, Matthieu Moy, Christian Couder

On Sat, May 14, 2016 at 8:31 PM, Junio C Hamano <gitster@pobox.com> wrote:
>
> I however do not see a reason why you need to expose that feature to
> the users of "git apply".  So I am not sure if any of us care deeply
> the choice among --silent, --quiet and -q -q.

About that I wrote in initial email above:

"The last patch 94/94 adds a --silent command line option to git apply.
This is not necessary, but some scripts could perhaps use it, and it
could make it easier to test the new silent feature."

I expose the feature in the last patch exactly because, if we don't
want to expose the feature, is is easy to do by just dropping the last
patch.
And I explain the reasons why it might be a good idea to expose the
feature, but I am also ok with just dropping the patch.

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

* Re: [PATCH v2 00/94] libify apply and use lib in am
  2016-05-14 19:37           ` Christian Couder
@ 2016-05-15 18:30             ` Junio C Hamano
  0 siblings, 0 replies; 148+ messages in thread
From: Junio C Hamano @ 2016-05-15 18:30 UTC (permalink / raw)
  To: Christian Couder
  Cc: Johannes Schindelin, git, Ævar Arnfjörð,
	Nguyen Thai Ngoc Duy, Stefan Beller, Eric Sunshine, Ramsay Jones,
	Jeff King, Karsten Blees, Matthieu Moy, Christian Couder

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

> On Sat, May 14, 2016 at 8:31 PM, Junio C Hamano <gitster@pobox.com> wrote:
>>
>> I however do not see a reason why you need to expose that feature to
>> the users of "git apply".  So I am not sure if any of us care deeply
>> the choice among --silent, --quiet and -q -q.
>
> About that I wrote in initial email above:
> ...

Because you are not bound to stick to what you thought before the
discussion, what you wrote before everybody commented to improve
your proposal does not really matter.  Otherwise, the time they
spent would be wasted.  What matters more is what conclusion you
drew after having input from others.

"That --silent is optional and cannot decide if I want to have the
option was what I said at the beginning" does not help the
discussion very much at this point.  People know that you couldn't
decide back then, and that is why they hopefully helped you to get
closer to deciding by discussing the series.

"OK, I now think we don't need 'apply --silent' because ..., so I'll
drop it" or "I still think we want 'apply --silent' because..., so
I'll keep it", followed by "thanks for discussing and helping" would
be a more appropriate answer, whichever conclusion you have drawn.

"Hmm, I still cannot decide." is also fine, by the way.

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

* Re: [PATCH v2 52/94] builtin/apply: read_patch_file() return -1 instead of die()ing
  2016-05-11 13:17 ` [PATCH v2 52/94] builtin/apply: read_patch_file() " Christian Couder
@ 2016-05-16  1:56   ` Eric Sunshine
  2016-05-16 17:19     ` Christian Couder
  0 siblings, 1 reply; 148+ messages in thread
From: Eric Sunshine @ 2016-05-16  1:56 UTC (permalink / raw)
  To: Christian Couder
  Cc: Git List, Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Ramsay Jones, Jeff King, Karsten Blees, Matthieu Moy,
	Christian Couder

On Wed, May 11, 2016 at 9:17 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. Let's do that by using error() instead
> of die()ing in read_patch_file().
>
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---
> diff --git a/builtin/apply.c b/builtin/apply.c
> @@ -445,10 +445,10 @@ static void say_patch_name(FILE *output, const char *fmt, struct patch *patch)
> -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));

When Duy's nd/error-errno series, which is in 'next', gets promoted to
'master', then this could become:

    return error_errno(...);

>         /*
>          * Make sure that we have some slop in the buffer
> @@ -457,6 +457,7 @@ static void read_patch_file(struct strbuf *sb, int fd)
>          */
>         strbuf_grow(sb, SLOP);
>         memset(sb->buf + sb->len, 0, SLOP);
> +       return 0;
>  }

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

* Re: [PATCH v2 54/94] builtin/apply: make parse_chunk() return a negative integer on error
  2016-05-11 13:17 ` [PATCH v2 54/94] builtin/apply: make parse_chunk() return a negative integer on error Christian Couder
@ 2016-05-16  3:04   ` Eric Sunshine
  2016-05-16 18:19     ` Christian Couder
  2016-06-08 15:14     ` Christian Couder
  0 siblings, 2 replies; 148+ messages in thread
From: Eric Sunshine @ 2016-05-16  3:04 UTC (permalink / raw)
  To: Christian Couder
  Cc: Git List, Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Ramsay Jones, Jeff King, Karsten Blees, Matthieu Moy,
	Christian Couder

On Wed, May 11, 2016 at 9:17 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 or exit()ing.
>
> To do that in a compatible manner with the rest of the error handling
> in builtin/apply.c, find_header() should return -1 instead of calling
> die() or exit().

Why is this talking about making find_header() return -1? Didn't that
happen in the previous patch?

> As parse_chunk() is called only by apply_patch() which already
> returns -1 when an error happened, let's make apply_patch() return -1
> when parse_chunk() returns -1.
>
> If find_header() returns -2 because no patch header has been found, it
> is ok for parse_chunk() to also return -2. If find_header() returns -1
> because an error happened, it is ok for parse_chunk() to do the same.
>
> Helped-by: Eric Sunshine <sunshine@sunshineco.com>
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---
> diff --git a/builtin/apply.c b/builtin/apply.c
> @@ -2176,8 +2176,9 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
>                  * empty to us here.
>                  */
>                 if ((state->apply || state->check) &&
> -                   (!patch->is_binary && !metadata_changes(patch)))
> -                       die(_("patch with only garbage at line %d"), state->linenr);
> +                   (!patch->is_binary && !metadata_changes(patch))) {
> +                       return error(_("patch with only garbage at line %d"), state->linenr);
> +               }

Unnecessary braces.

>         }
>
>         return offset + hdrsize + patchsize;

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

* Re: [PATCH v2 56/94] apply: move 'struct apply_state' to apply.h
  2016-05-11 13:17 ` [PATCH v2 56/94] apply: move 'struct apply_state' to apply.h Christian Couder
@ 2016-05-16  3:10   ` Eric Sunshine
  2016-05-16 16:03     ` Junio C Hamano
  0 siblings, 1 reply; 148+ messages in thread
From: Eric Sunshine @ 2016-05-16  3:10 UTC (permalink / raw)
  To: Christian Couder
  Cc: Git List, Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Ramsay Jones, Jeff King, Karsten Blees, Matthieu Moy,
	Christian Couder

On Wed, May 11, 2016 at 9:17 AM, Christian Couder
<christian.couder@gmail.com> wrote:
> To libify `git apply` functionality we must make 'struct apply_state'
> usable outside "builtin/apply.c".

Why is this patch plopped right in the middle of a bunch of other
patches which are making functions return -1 rather than die()ing?
Seems out of place.

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

I could easily see this header introduced at a very early stage when
'struct apply_state' itself is introduced, rather than introducing
'struct apply_state' in builtin/apply.c and later moving it here.

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

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

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

On Wed, May 11, 2016 at 9:17 AM, Christian Couder
<christian.couder@gmail.com> wrote:
> To libify `git apply` functionality we must make init_apply_state()
> usable outside "builtin/apply.c".
>
> Let's do that by moving it into a new "apply.c".

Similar to my comment about apply.h and 'struct apply_state', I can
easily see apply.c introduced very early in the conversion. In fact,
I'd imagine apply.c and apply.h being introduced by the same patch,
with that patch placing init_apply_state() in apply.c and 'struct
apply_state' in apply.h. However, I see that you're moving other
functions, as well, which already existed in builtin/apply.c, so I
guess that's why introduction of apply.c waited until this step.

> Helped-by: Eric Sunshine <sunshine@sunshineco.com>
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---
> diff --git a/apply.c b/apply.c
> @@ -0,0 +1,83 @@
> +#include "cache.h"
> +#include "lockfile.h"
> +#include "apply.h"
> +
> +static void git_apply_config(void)
> +{
> +       git_config_get_string_const("apply.whitespace", &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,
> +                     struct lock_file *lock_file)
> +{
> +       memset(state, 0, sizeof(*state));
> +       state->prefix = prefix;
> +       state->prefix_length = state->prefix ? strlen(state->prefix) : 0;
> +       state->lock_file = lock_file ? lock_file : xcalloc(1, sizeof(*lock_file));
> +       state->newfd = -1;
> +       state->apply = 1;
> +       state->line_termination = '\n';
> +       state->p_value = 1;
> +       state->p_context = UINT_MAX;
> +       state->squelch_whitespace_errors = 5;
> +       state->ws_error_action = warn_on_ws_error;
> +       state->ws_ignore_action = ignore_ws_none;
> +       state->linenr = 1;
> +       strbuf_init(&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 aa11ea6..0f77f4d 100644
> --- a/apply.h
> +++ b/apply.h
> @@ -112,4 +112,13 @@ struct apply_state {
>         enum ws_ignore ws_ignore_action;
>  };
>
> +extern int parse_whitespace_option(struct apply_state *state,
> +                                  const char *option);
> +extern int parse_ignorewhitespace_option(struct apply_state *state,
> +                                        const char *option);
> +
> +extern void init_apply_state(struct apply_state *state,
> +                            const char *prefix,
> +                            struct lock_file *lock_file);
> +
>  #endif

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

* Re: [PATCH v2 60/94] apply: make init_apply_state() return -1 instead of exit()ing
  2016-05-11 13:17 ` [PATCH v2 60/94] apply: make init_apply_state() return -1 instead of exit()ing Christian Couder
@ 2016-05-16  3:37   ` Eric Sunshine
  0 siblings, 0 replies; 148+ messages in thread
From: Eric Sunshine @ 2016-05-16  3:37 UTC (permalink / raw)
  To: Christian Couder
  Cc: Git List, Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Ramsay Jones, Jeff King, Karsten Blees, Matthieu Moy,
	Christian Couder

On Wed, May 11, 2016 at 9:17 AM, Christian Couder
<christian.couder@gmail.com> wrote:
> To libify `git apply` functionality we have to signal errors to the
> caller instead of exit()ing.
>
> To do that in a compatible manner with the rest of the error handling
> in "builtin/apply.c", init_apply_state() should return -1 using
> error() instead of calling exit().

This commit message seems to be lying. It says that the -1 comes from
error(), however, init_apply_state() just returns literal -1 in all
cases.

By the way, mentioning "return -1 using error()" in all of these
commit messages (not just this one) may be overkill. The fact that
that side-effect of error() is being used in some of these cases is an
implementation detail which doesn't necessarily merit mention in the
commit message.

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

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

* Re: [PATCH v2 63/94] builtin/apply: make apply_all_patches() return -1 on error
  2016-05-11 13:17 ` [PATCH v2 63/94] builtin/apply: make apply_all_patches() return -1 on error Christian Couder
@ 2016-05-16  3:44   ` Eric Sunshine
  2016-06-08 16:37     ` Christian Couder
  0 siblings, 1 reply; 148+ messages in thread
From: Eric Sunshine @ 2016-05-16  3:44 UTC (permalink / raw)
  To: Christian Couder
  Cc: Git List, Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Ramsay Jones, Jeff King, Karsten Blees, Matthieu Moy,
	Christian Couder

On Wed, May 11, 2016 at 9:17 AM, Christian Couder
<christian.couder@gmail.com> wrote:
> To finish libifying the apply functionality, apply_all_patches() should not
> die() or exit() in case of error, but return -1.
>
> While doing that we must take care that file descriptors are properly closed
> and, if needed, reset a sensible value.
>
> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---
> diff --git a/builtin/apply.c b/builtin/apply.c
> @@ -4613,9 +4613,10 @@ static int apply_all_patches(struct apply_state *state,
>         }
>
>         if (state->update_index) {
> -               if (write_locked_index(&the_index, state->lock_file, COMMIT_LOCK))
> -                       die(_("Unable to write new index file"));
> +               res = write_locked_index(&the_index, state->lock_file, COMMIT_LOCK);
>                 state->newfd = -1;

Does write_locked_index() unconditionally close the file descriptor
even when an error occurs? If not, then isn't this potentially leaking
'newfd'?

(My very cursory read of write_locked_index() seems to reveal that the
file descriptor may indeed remain open upon index write failure.)

> +               if (res)
> +                       return error(_("Unable to write new index file"));
>         }
>
>         return !!errs;

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

* Re: [PATCH v2 56/94] apply: move 'struct apply_state' to apply.h
  2016-05-16  3:10   ` Eric Sunshine
@ 2016-05-16 16:03     ` Junio C Hamano
  2016-06-08 15:25       ` Christian Couder
  0 siblings, 1 reply; 148+ messages in thread
From: Junio C Hamano @ 2016-05-16 16:03 UTC (permalink / raw)
  To: Eric Sunshine
  Cc: Christian Couder, Git List,
	Ævar Arnfjörð Bjarmason, Nguyen Thai Ngoc Duy,
	Stefan Beller, Johannes Schindelin, Ramsay Jones, Jeff King,
	Karsten Blees, Matthieu Moy, Christian Couder

Eric Sunshine <sunshine@sunshineco.com> writes:

> On Wed, May 11, 2016 at 9:17 AM, Christian Couder
> <christian.couder@gmail.com> wrote:
>> To libify `git apply` functionality we must make 'struct apply_state'
>> usable outside "builtin/apply.c".
>
> Why is this patch plopped right in the middle of a bunch of other
> patches which are making functions return -1 rather than die()ing?
> Seems out of place.

Two possible places that would make more sense are (1) when it is
introduced very early in the series, or (2) when it absorbed all the
file-scope-static global states in the middle of the series.  I think
either is fine.

That would be a good place to end the first batch of the topic.
Then the second batch would be "turn die() into error status that is
propagated upwards".

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

* Re: [PATCH v2 52/94] builtin/apply: read_patch_file() return -1 instead of die()ing
  2016-05-16  1:56   ` Eric Sunshine
@ 2016-05-16 17:19     ` Christian Couder
  0 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-16 17:19 UTC (permalink / raw)
  To: Eric Sunshine
  Cc: Git List, Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Ramsay Jones, Jeff King, Karsten Blees, Matthieu Moy,
	Christian Couder

On Mon, May 16, 2016 at 3:56 AM, Eric Sunshine <sunshine@sunshineco.com> wrote:
> On Wed, May 11, 2016 at 9:17 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. Let's do that by using error() instead
>> of die()ing in read_patch_file().
>>
>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
>> ---
>> diff --git a/builtin/apply.c b/builtin/apply.c
>> @@ -445,10 +445,10 @@ static void say_patch_name(FILE *output, const char *fmt, struct patch *patch)
>> -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));
>
> When Duy's nd/error-errno series, which is in 'next', gets promoted to
> 'master', then this could become:
>
>     return error_errno(...);

Yeah, sure.

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

* Re: [PATCH v2 54/94] builtin/apply: make parse_chunk() return a negative integer on error
  2016-05-16  3:04   ` Eric Sunshine
@ 2016-05-16 18:19     ` Christian Couder
  2016-06-08 15:14     ` Christian Couder
  1 sibling, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-16 18:19 UTC (permalink / raw)
  To: Eric Sunshine
  Cc: Git List, Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Ramsay Jones, Jeff King, Karsten Blees, Matthieu Moy,
	Christian Couder

On Mon, May 16, 2016 at 5:04 AM, Eric Sunshine <sunshine@sunshineco.com> wrote:
> On Wed, May 11, 2016 at 9:17 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 or exit()ing.
>>
>> To do that in a compatible manner with the rest of the error handling
>> in builtin/apply.c, find_header() should return -1 instead of calling
>> die() or exit().
>
> Why is this talking about making find_header() return -1? Didn't that
> happen in the previous patch?
>
>> As parse_chunk() is called only by apply_patch() which already
>> returns -1 when an error happened, let's make apply_patch() return -1
>> when parse_chunk() returns -1.
>>
>> If find_header() returns -2 because no patch header has been found, it
>> is ok for parse_chunk() to also return -2. If find_header() returns -1
>> because an error happened, it is ok for parse_chunk() to do the same.
>>
>> Helped-by: Eric Sunshine <sunshine@sunshineco.com>
>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
>> ---
>> diff --git a/builtin/apply.c b/builtin/apply.c
>> @@ -2176,8 +2176,9 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
>>                  * empty to us here.
>>                  */
>>                 if ((state->apply || state->check) &&
>> -                   (!patch->is_binary && !metadata_changes(patch)))
>> -                       die(_("patch with only garbage at line %d"), state->linenr);
>> +                   (!patch->is_binary && !metadata_changes(patch))) {
>> +                       return error(_("patch with only garbage at line %d"), state->linenr);
>> +               }
>
> Unnecessary braces.

Ok, will remove.

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

* Re: [PATCH v2 48/94] builtin/apply: rename 'prefix_' parameter to 'prefix'
  2016-05-13 19:42     ` Christian Couder
@ 2016-05-24  8:15       ` Christian Couder
  0 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-24  8:15 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Ævar Arnfjörð,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

On Fri, May 13, 2016 at 9:42 PM, Christian Couder
<christian.couder@gmail.com> wrote:
> On Thu, May 12, 2016 at 9:56 PM, Junio C Hamano <gitster@pobox.com> wrote:
>> Christian Couder <christian.couder@gmail.com> writes:
>>
>>> This is just a small cleanup.
>>
>> ... which may have been better happened at 09/94.
>
> Ok, I will squash it in 09/94.

This has been done in the v3 I just sent, though I forgot to mention
it in the changes.

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

* Re: [PATCH v2 48/94] builtin/apply: rename 'prefix_' parameter to 'prefix'
  2016-05-14 18:27         ` Junio C Hamano
@ 2016-05-24  8:24           ` Christian Couder
  0 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-05-24  8:24 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Ævar Arnfjörð,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

On Sat, May 14, 2016 at 8:27 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Christian Couder <christian.couder@gmail.com> writes:
>
>> On Thu, May 12, 2016 at 10:43 PM, Junio C Hamano <gitster@pobox.com> wrote:
>>> Junio C Hamano <gitster@pobox.com> writes:
>>>
>>>> Up to this point, the conversion looks quite sensible, even though I
>>>> think the organization of fields in apply_state do not look logical.
>>>
>>> I'd stop here for now, as everything before this step looks
>>> uncontroversial.  Anybody whose tasked to move the global state for
>>> these variables into a structure would reach the samestate after
>>> applying these 48 patches, modulo minor differences in the way the
>>> comments would say things, how the patches are split and how the
>>> fields in apply_state() are organized.
>>>
>>> One thing that is missing is a counterpart of init_apply_state().
>>> In the early part of patches where it added only "const char *"
>>> borrowed from the caller and fields of intregral type, the lack of
>>> clear_apply_state() did not mattter, but with a few fields with
>>> "string_list" type, anybody who want to make repeated call into the
>>> apply machinery would want a way to release the resource the
>>> structure holds.
>>>
>>> Because 49/94 is a step to add an unfreeable resource, this is a
>>> good place to stop and then add the clean_apply_state() before that
>>> happens, I would think.  After that, I think both the contributor
>>> and the reviewers would benefit if these early patches are merged
>>> early without waiting for the review of the remainder.
>>
>> Ok, I will add add the clean_apply_state() and resend the patches up
>> to that point soon, so that they can be merged early.
>
> Two more comments:
>
>  - Just like the change between v1 and v2, if we were to introduce
>    clear_apply_state(), it would be nicer if it were done early in
>    the series, ideally at the same time as init_apply_state() gets
>    introduced,

Yeah, I have done that in v3.

> or at least no later than the first field that holds
>    resource that needs releasing is added.
>
>  - I didn't double check if 50 and later still had changes that
>    belong to this "early batch that moves things into a struct" (aka
>    cc/apply-introduce-state) topic, as I stopped at 49 and saw the
>    need for clear_apply_state().  So 48 may not be the ideal place
>    to stop.  From a cursory read of their titles, perhaps 49, 50, 56
>    and possibly 60 should be in this early series?  While 60 does
>    sound like an immediate follow-up to 09/94, it depends on a few
>    "die elimination" changes, so I do NOT think it must be in the
>    early batch.

For v3 I stopped at patch 50 from v2.

Patch 56 moves 'struct apply_state' into apply.h and it can logically
belong to another patch series, as anyway 'struct apply_state' is not
usable by other code as long as the related functions
(init_apply_state(), clear_apply_state(), check_apply_state(), ...)
are not also moved to apply.{c,h}.

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

* Re: [PATCH v2 54/94] builtin/apply: make parse_chunk() return a negative integer on error
  2016-05-16  3:04   ` Eric Sunshine
  2016-05-16 18:19     ` Christian Couder
@ 2016-06-08 15:14     ` Christian Couder
  1 sibling, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-06-08 15:14 UTC (permalink / raw)
  To: Eric Sunshine
  Cc: Git List, Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Ramsay Jones, Jeff King, Karsten Blees, Matthieu Moy,
	Christian Couder

On Mon, May 16, 2016 at 5:04 AM, Eric Sunshine <sunshine@sunshineco.com> wrote:
> On Wed, May 11, 2016 at 9:17 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 or exit()ing.
>>
>> To do that in a compatible manner with the rest of the error handling
>> in builtin/apply.c, find_header() should return -1 instead of calling
>> die() or exit().
>
> Why is this talking about making find_header() return -1? Didn't that
> happen in the previous patch?

Yeah, it should be parse_chunk() not find_header().

This is fixed in my current branch.

Thanks,
Christian.

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

* Re: [PATCH v2 56/94] apply: move 'struct apply_state' to apply.h
  2016-05-16 16:03     ` Junio C Hamano
@ 2016-06-08 15:25       ` Christian Couder
  0 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-06-08 15:25 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: Eric Sunshine, Git List, Ævar Arnfjörð,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Ramsay Jones, Jeff King, Karsten Blees, Matthieu Moy,
	Christian Couder

On Mon, May 16, 2016 at 6:03 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Eric Sunshine <sunshine@sunshineco.com> writes:
>
>> On Wed, May 11, 2016 at 9:17 AM, Christian Couder
>> <christian.couder@gmail.com> wrote:
>>> To libify `git apply` functionality we must make 'struct apply_state'
>>> usable outside "builtin/apply.c".
>>
>> Why is this patch plopped right in the middle of a bunch of other
>> patches which are making functions return -1 rather than die()ing?
>> Seems out of place.
>
> Two possible places that would make more sense are (1) when it is
> introduced very early in the series, or (2) when it absorbed all the
> file-scope-static global states in the middle of the series.  I think
> either is fine.
>
> That would be a good place to end the first batch of the topic.
> Then the second batch would be "turn die() into error status that is
> propagated upwards".

I moved this patch at the beginning of second batch that I will send
hopefully soon...

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

* Re: [PATCH v2 63/94] builtin/apply: make apply_all_patches() return -1 on error
  2016-05-16  3:44   ` Eric Sunshine
@ 2016-06-08 16:37     ` Christian Couder
  2016-06-08 17:44       ` Eric Sunshine
  0 siblings, 1 reply; 148+ messages in thread
From: Christian Couder @ 2016-06-08 16:37 UTC (permalink / raw)
  To: Eric Sunshine
  Cc: Git List, Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Ramsay Jones, Jeff King, Karsten Blees, Matthieu Moy,
	Christian Couder

On Mon, May 16, 2016 at 5:44 AM, Eric Sunshine <sunshine@sunshineco.com> wrote:
> On Wed, May 11, 2016 at 9:17 AM, Christian Couder
> <christian.couder@gmail.com> wrote:
>> To finish libifying the apply functionality, apply_all_patches() should not
>> die() or exit() in case of error, but return -1.
>>
>> While doing that we must take care that file descriptors are properly closed
>> and, if needed, reset a sensible value.
>>
>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
>> ---
>> diff --git a/builtin/apply.c b/builtin/apply.c
>> @@ -4613,9 +4613,10 @@ static int apply_all_patches(struct apply_state *state,
>>         }
>>
>>         if (state->update_index) {
>> -               if (write_locked_index(&the_index, state->lock_file, COMMIT_LOCK))
>> -                       die(_("Unable to write new index file"));
>> +               res = write_locked_index(&the_index, state->lock_file, COMMIT_LOCK);
>>                 state->newfd = -1;
>
> Does write_locked_index() unconditionally close the file descriptor
> even when an error occurs? If not, then isn't this potentially leaking
> 'newfd'?
>
> (My very cursory read of write_locked_index() seems to reveal that the
> file descriptor may indeed remain open upon index write failure.)

You are right, it is leaking newfd if write_locked_index() fails.
The solution to that is to call `rollback_lock_file(state->lock_file)`
and the following patch was supposed to do that:

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

but it would do that only if `state->newfd >= 0` so we should set
state->newfd to -1 only if write_locked_index() succeeds.

I will fix this.

I am also going to add a comment to this patch saying that this patch
needs a following patch to call rollback_lock_file(state->lock_file)
in case of errors.

Or if you prefer, I can squash the patch that call
rollback_lock_file(state->lock_file) in case of errors into this
patch.

Thanks,
Christian.

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

* Re: [PATCH v2 63/94] builtin/apply: make apply_all_patches() return -1 on error
  2016-06-08 16:37     ` Christian Couder
@ 2016-06-08 17:44       ` Eric Sunshine
  2016-06-09 22:01         ` Christian Couder
  0 siblings, 1 reply; 148+ messages in thread
From: Eric Sunshine @ 2016-06-08 17:44 UTC (permalink / raw)
  To: Christian Couder
  Cc: Git List, Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Ramsay Jones, Jeff King, Karsten Blees, Matthieu Moy,
	Christian Couder

On Wed, Jun 8, 2016 at 12:37 PM, Christian Couder
<christian.couder@gmail.com> wrote:
> On Mon, May 16, 2016 at 5:44 AM, Eric Sunshine <sunshine@sunshineco.com> wrote:
>> On Wed, May 11, 2016 at 9:17 AM, 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"));
>>> +               res = write_locked_index(&the_index, state->lock_file, COMMIT_LOCK);
>>>                 state->newfd = -1;
>>
>> Does write_locked_index() unconditionally close the file descriptor
>> even when an error occurs? If not, then isn't this potentially leaking
>> 'newfd'?
>>
>> (My very cursory read of write_locked_index() seems to reveal that the
>> file descriptor may indeed remain open upon index write failure.)
>
> You are right, it is leaking newfd if write_locked_index() fails.
> The solution to that is to call `rollback_lock_file(state->lock_file)`
> and the following patch was supposed to do that:
>
> [PATCH v2 82/94] apply: roll back index lock file in case of error
>
> but it would do that only if `state->newfd >= 0` so we should set
> state->newfd to -1 only if write_locked_index() succeeds.
>
> I will fix this.
>
> I am also going to add a comment to this patch saying that this patch
> needs a following patch to call rollback_lock_file(state->lock_file)
> in case of errors.
>
> Or if you prefer, I can squash the patch that call
> rollback_lock_file(state->lock_file) in case of errors into this
> patch.

Squashing may indeed be preferable over leaving it in a "broken" state
until the next patch, though I haven't thought too hard about it.
Alternately, can the two patches somehow be swapped?

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

* Re: [PATCH v2 00/94] libify apply and use lib in am
  2016-05-12 18:02   ` Christian Couder
@ 2016-06-09 21:10     ` Johannes Sixt
  2016-06-10  6:40       ` Christian Couder
  2016-06-10  7:01       ` Johannes Schindelin
  0 siblings, 2 replies; 148+ messages in thread
From: Johannes Sixt @ 2016-06-09 21:10 UTC (permalink / raw)
  To: Christian Couder
  Cc: git, Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

Am 12.05.2016 um 20:02 schrieb Christian Couder:
> On Thu, May 12, 2016 at 7:06 PM, Johannes Sixt <j6t@kdbg.org> wrote:
>> Am 11.05.2016 um 15:16 schrieb 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.
>>
>>
>> I'm including this in my build on Windows. It passes the test suite.
>
> Great! Thanks for the report!
>
>> I'll also use it in production for a while, although I am not a git-am
>> consumer nor do I use git-rebase without -i, hence, my tests will probably
>> only show that there is no bad fall-out.

Meanwhile, I have retrained my muscle memory to stop before typing "-i" 
after "rebase" for an opportunity to consider whether bare rebase can be 
used.

What should I say? I am impressed. It's like 100 times faster than 
rebase -i (on Windows). I'm now using it whenever I can, and more often 
than not I plan my rebase workflow so that I can go ahead without -i.

Can't wait to test a re-roll on top of cc/apply-introduce-state!

-- Hannes

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

* Re: [PATCH v2 63/94] builtin/apply: make apply_all_patches() return -1 on error
  2016-06-08 17:44       ` Eric Sunshine
@ 2016-06-09 22:01         ` Christian Couder
  0 siblings, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-06-09 22:01 UTC (permalink / raw)
  To: Eric Sunshine
  Cc: Git List, Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Ramsay Jones, Jeff King, Karsten Blees, Matthieu Moy,
	Christian Couder

On Wed, Jun 8, 2016 at 7:44 PM, Eric Sunshine <sunshine@sunshineco.com> wrote:
> On Wed, Jun 8, 2016 at 12:37 PM, Christian Couder
> <christian.couder@gmail.com> wrote:
>> On Mon, May 16, 2016 at 5:44 AM, Eric Sunshine <sunshine@sunshineco.com> wrote:
>>> On Wed, May 11, 2016 at 9:17 AM, 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"));
>>>> +               res = write_locked_index(&the_index, state->lock_file, COMMIT_LOCK);
>>>>                 state->newfd = -1;
>>>
>>> Does write_locked_index() unconditionally close the file descriptor
>>> even when an error occurs? If not, then isn't this potentially leaking
>>> 'newfd'?
>>>
>>> (My very cursory read of write_locked_index() seems to reveal that the
>>> file descriptor may indeed remain open upon index write failure.)
>>
>> You are right, it is leaking newfd if write_locked_index() fails.
>> The solution to that is to call `rollback_lock_file(state->lock_file)`
>> and the following patch was supposed to do that:
>>
>> [PATCH v2 82/94] apply: roll back index lock file in case of error
>>
>> but it would do that only if `state->newfd >= 0` so we should set
>> state->newfd to -1 only if write_locked_index() succeeds.
>>
>> I will fix this.
>>
>> I am also going to add a comment to this patch saying that this patch
>> needs a following patch to call rollback_lock_file(state->lock_file)
>> in case of errors.
>>
>> Or if you prefer, I can squash the patch that call
>> rollback_lock_file(state->lock_file) in case of errors into this
>> patch.
>
> Squashing may indeed be preferable over leaving it in a "broken" state
> until the next patch, though I haven't thought too hard about it.
> Alternately, can the two patches somehow be swapped?

I just squashed them for now as the result looks reasonable.

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

* Re: [PATCH v2 00/94] libify apply and use lib in am
  2016-06-09 21:10     ` Johannes Sixt
@ 2016-06-10  6:40       ` Christian Couder
  2016-06-10  7:01       ` Johannes Schindelin
  1 sibling, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-06-10  6:40 UTC (permalink / raw)
  To: Johannes Sixt
  Cc: git, Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Johannes Schindelin,
	Eric Sunshine, Ramsay Jones, Jeff King, Karsten Blees,
	Matthieu Moy, Christian Couder

On Thu, Jun 9, 2016 at 11:10 PM, Johannes Sixt <j6t@kdbg.org> wrote:
> Am 12.05.2016 um 20:02 schrieb Christian Couder:
>>
>>> I'll also use it in production for a while, although I am not a git-am
>>> consumer nor do I use git-rebase without -i, hence, my tests will
>>> probably
>>> only show that there is no bad fall-out.
>
>
> Meanwhile, I have retrained my muscle memory to stop before typing "-i"
> after "rebase" for an opportunity to consider whether bare rebase can be
> used.
>
> What should I say? I am impressed. It's like 100 times faster than rebase -i
> (on Windows). I'm now using it whenever I can, and more often than not I
> plan my rebase workflow so that I can go ahead without -i.

:-D

Thanks for the report again!

> Can't wait to test a re-roll on top of cc/apply-introduce-state!

It should happen really soon now...

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

* Re: [PATCH v2 00/94] libify apply and use lib in am
  2016-06-09 21:10     ` Johannes Sixt
  2016-06-10  6:40       ` Christian Couder
@ 2016-06-10  7:01       ` Johannes Schindelin
  2016-06-10  8:59         ` Christian Couder
  1 sibling, 1 reply; 148+ messages in thread
From: Johannes Schindelin @ 2016-06-10  7:01 UTC (permalink / raw)
  To: Johannes Sixt
  Cc: Christian Couder, git, Junio C Hamano,
	Ævar Arnfjörð Bjarmason, Nguyen Thai Ngoc Duy,
	Stefan Beller, Eric Sunshine, Ramsay Jones, Jeff King,
	Karsten Blees, Matthieu Moy, Christian Couder

Hi Hannes,

On Thu, 9 Jun 2016, Johannes Sixt wrote:

> Meanwhile, I have retrained my muscle memory to stop before typing "-i" after
> "rebase" for an opportunity to consider whether bare rebase can be used.
> 
> What should I say? I am impressed. It's like 100 times faster than rebase -i
> (on Windows). I'm now using it whenever I can, and more often than not I plan
> my rebase workflow so that I can go ahead without -i.

That only means that I have to finalize my rebase--helper work (which I am
busy doing, I am at the valgrind stage).

I wonder whether that "100x" is a reliable number? ;-) FWIW I can measure
something like a 4x speedup of the interactive rebase on Windows when
running with the rebase--helper, and it is still noticably faster in my
Linux VM, too.

> Can't wait to test a re-roll on top of cc/apply-introduce-state!

I lost track in the meantime: were those issues with unclosed file handles
and unreleased memory in the error code paths addressed systematically? My
mail about that seems to have been left unanswered, almost as if my
concerns had been hand-waved away...

If those issues have indeed been addressed properly, and a public
repository reliably has the newest iteration of that patch series in a
branch without a versioned name, I will be happy to test it in Git for
Windows' SDK again.

Ciao,
Johannes

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

* Re: [PATCH v2 00/94] libify apply and use lib in am
  2016-06-10  7:01       ` Johannes Schindelin
@ 2016-06-10  8:59         ` Christian Couder
  2016-06-10 11:11           ` Johannes Schindelin
  0 siblings, 1 reply; 148+ messages in thread
From: Christian Couder @ 2016-06-10  8:59 UTC (permalink / raw)
  To: Johannes Schindelin
  Cc: Johannes Sixt, git, Junio C Hamano,
	Ævar Arnfjörð Bjarmason, Nguyen Thai Ngoc Duy,
	Stefan Beller, Eric Sunshine, Ramsay Jones, Jeff King,
	Karsten Blees, Matthieu Moy, Christian Couder

Hi Dscho,

On Fri, Jun 10, 2016 at 9:01 AM, Johannes Schindelin
<Johannes.Schindelin@gmx.de> wrote:
> Hi Hannes,
>
> On Thu, 9 Jun 2016, Johannes Sixt wrote:
>
>> Meanwhile, I have retrained my muscle memory to stop before typing "-i" after
>> "rebase" for an opportunity to consider whether bare rebase can be used.
>>
>> What should I say? I am impressed. It's like 100 times faster than rebase -i
>> (on Windows). I'm now using it whenever I can, and more often than not I plan
>> my rebase workflow so that I can go ahead without -i.
>
> That only means that I have to finalize my rebase--helper work (which I am
> busy doing, I am at the valgrind stage).
>
> I wonder whether that "100x" is a reliable number? ;-) FWIW I can measure
> something like a 4x speedup of the interactive rebase on Windows when
> running with the rebase--helper, and it is still noticably faster in my
> Linux VM, too.
>
>> Can't wait to test a re-roll on top of cc/apply-introduce-state!
>
> I lost track in the meantime: were those issues with unclosed file handles
> and unreleased memory in the error code paths addressed systematically? My
> mail about that seems to have been left unanswered, almost as if my
> concerns had been hand-waved away...

Haven't I answered to your email in this thread:

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

?

> If those issues have indeed been addressed properly, and a public
> repository reliably has the newest iteration of that patch series in a
> branch without a versioned name, I will be happy to test it in Git for
> Windows' SDK again.

This is the newest iteration:

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

Thanks for testing,
Christian.

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

* Re: [PATCH v2 00/94] libify apply and use lib in am
  2016-06-10  8:59         ` Christian Couder
@ 2016-06-10 11:11           ` Johannes Schindelin
  2016-06-10 17:04             ` Johannes Sixt
  0 siblings, 1 reply; 148+ messages in thread
From: Johannes Schindelin @ 2016-06-10 11:11 UTC (permalink / raw)
  To: Christian Couder
  Cc: Johannes Sixt, git, Junio C Hamano,
	Ævar Arnfjörð Bjarmason, Nguyen Thai Ngoc Duy,
	Stefan Beller, Eric Sunshine, Ramsay Jones, Jeff King,
	Karsten Blees, Matthieu Moy, Christian Couder

Hi Christian,

On Fri, 10 Jun 2016, Christian Couder wrote:

> On Fri, Jun 10, 2016 at 9:01 AM, Johannes Schindelin
> <Johannes.Schindelin@gmx.de> wrote:
> >
> > On Thu, 9 Jun 2016, Johannes Sixt wrote:
> >
> >> Meanwhile, I have retrained my muscle memory to stop before typing "-i" after
> >> "rebase" for an opportunity to consider whether bare rebase can be used.
> >>
> >> What should I say? I am impressed. It's like 100 times faster than rebase -i
> >> (on Windows). I'm now using it whenever I can, and more often than not I plan
> >> my rebase workflow so that I can go ahead without -i.
> >
> > That only means that I have to finalize my rebase--helper work (which I am
> > busy doing, I am at the valgrind stage).
> >
> > I wonder whether that "100x" is a reliable number? ;-) FWIW I can measure
> > something like a 4x speedup of the interactive rebase on Windows when
> > running with the rebase--helper, and it is still noticably faster in my
> > Linux VM, too.
> >
> >> Can't wait to test a re-roll on top of cc/apply-introduce-state!
> >
> > I lost track in the meantime: were those issues with unclosed file handles
> > and unreleased memory in the error code paths addressed systematically? My
> > mail about that seems to have been left unanswered, almost as if my
> > concerns had been hand-waved away...
> 
> Haven't I answered to your email in this thread:
> 
> http://thread.gmane.org/gmane.comp.version-control.git/292403/
> 
> ?

Not really. The reply (which I had not quite connected with my mail
because they were over a week apart) says this:

> I fixed this by moving the "close(fd)" call just after the
> "apply_patch()" call.

and this:

> I will have another look at the 2 other places where there are
> open()/close() or fopen()/fclose() calls.

but nothing about a careful, systematic investigation of all error code
paths. As a consequence, I fully expect to encounter test failures as soon
as I test your patch series again, simply because resources are still in
use when they should no longer be used. In other words, my expectations
are now lower than they have been before, my concerns are not at all
addressed.

> > If those issues have indeed been addressed properly, and a public
> > repository reliably has the newest iteration of that patch series in a
> > branch without a versioned name, I will be happy to test it in Git for
> > Windows' SDK again.
> 
> This is the newest iteration:
> 
> https://github.com/chriscool/git/commits/libify-apply-use-in-am65

And that cute 65 in the name is the revision.

Ciao,
Johannes

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

* Re: [PATCH v2 00/94] libify apply and use lib in am
  2016-06-10 11:11           ` Johannes Schindelin
@ 2016-06-10 17:04             ` Johannes Sixt
  2016-06-10 20:31               ` Christian Couder
  2016-06-11  7:02               ` Johannes Schindelin
  0 siblings, 2 replies; 148+ messages in thread
From: Johannes Sixt @ 2016-06-10 17:04 UTC (permalink / raw)
  To: Johannes Schindelin, Christian Couder
  Cc: git, Junio C Hamano, Ævar Arnfjörð Bjarmason,
	Nguyen Thai Ngoc Duy, Stefan Beller, Eric Sunshine, Ramsay Jones,
	Jeff King, Karsten Blees, Matthieu Moy, Christian Couder

Am 10.06.2016 um 13:11 schrieb Johannes Schindelin:
> On Fri, 10 Jun 2016, Christian Couder wrote:
>> On Fri, Jun 10, 2016 at 9:01 AM, Johannes Schindelin
>> <Johannes.Schindelin@gmx.de> wrote:
>>> I lost track in the meantime: were those issues with unclosed file handles
>>> and unreleased memory in the error code paths addressed systematically? My
>>> mail about that seems to have been left unanswered, almost as if my
>>> concerns had been hand-waved away...
>>
>> Haven't I answered to your email in this thread:
>>
>> http://thread.gmane.org/gmane.comp.version-control.git/292403/
>>
>> ?
>
> Not really. The reply (which I had not quite connected with my mail
> because they were over a week apart) says this:
>
>> I fixed this by moving the "close(fd)" call just after the
>> "apply_patch()" call.

This bug in v1 was discovered by the test suite and fixed in v2.

>
> and this:
>
>> I will have another look at the 2 other places where there are
>> open()/close() or fopen()/fclose() calls.
>
> but nothing about a careful, systematic investigation of all error code
> paths. As a consequence, I fully expect to encounter test failures as soon
> as I test your patch series again, simply because resources are still in
> use when they should no longer be used. In other words, my expectations
> are now lower than they have been before, my concerns are not at all
> addressed.

Do you trust the test suite to some degree? It passes after the above 
bug was fixed in v2. In addition, haven't found any problems so far 
during daily use.

>> This is the newest iteration:
>>
>> https://github.com/chriscool/git/commits/libify-apply-use-in-am65
>
> And that cute 65 in the name is the revision.

Yeah, that number is painful. I would appreciate an unversioned branch 
name, too.

-- Hannes

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

* Re: [PATCH v2 00/94] libify apply and use lib in am
  2016-06-10 17:04             ` Johannes Sixt
@ 2016-06-10 20:31               ` Christian Couder
  2016-06-11  7:02               ` Johannes Schindelin
  1 sibling, 0 replies; 148+ messages in thread
From: Christian Couder @ 2016-06-10 20:31 UTC (permalink / raw)
  To: Johannes Sixt
  Cc: Johannes Schindelin, git, Junio C Hamano,
	Ævar Arnfjörð Bjarmason, Nguyen Thai Ngoc Duy,
	Stefan Beller, Eric Sunshine, Ramsay Jones, Jeff King,
	Karsten Blees, Matthieu Moy, Christian Couder

On Fri, Jun 10, 2016 at 7:04 PM, Johannes Sixt <j6t@kdbg.org> wrote:
> Am 10.06.2016 um 13:11 schrieb Johannes Schindelin:
>>
>> Not really. The reply (which I had not quite connected with my mail
>> because they were over a week apart) says this:
>>
>>> I fixed this by moving the "close(fd)" call just after the
>>> "apply_patch()" call.
>
> This bug in v1 was discovered by the test suite and fixed in v2.
>
>> and this:
>>
>>> I will have another look at the 2 other places where there are
>>> open()/close() or fopen()/fclose() calls.
>>
>> but nothing about a careful, systematic investigation of all error code
>> paths. As a consequence, I fully expect to encounter test failures as soon
>> as I test your patch series again, simply because resources are still in
>> use when they should no longer be used. In other words, my expectations
>> are now lower than they have been before, my concerns are not at all
>> addressed.
>
> Do you trust the test suite to some degree? It passes after the above bug
> was fixed in v2. In addition, haven't found any problems so far during daily
> use.
>
>>> This is the newest iteration:
>>>
>>> https://github.com/chriscool/git/commits/libify-apply-use-in-am65
>>
>> And that cute 65 in the name is the revision.
>
> Yeah, that number is painful. I would appreciate an unversioned branch name,
> too.

Ok, you can use
https://github.com/chriscool/git/commits/libify-apply-use-in-am then.

Thanks,
Christian.

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

* Re: [PATCH v2 00/94] libify apply and use lib in am
  2016-06-10 17:04             ` Johannes Sixt
  2016-06-10 20:31               ` Christian Couder
@ 2016-06-11  7:02               ` Johannes Schindelin
  1 sibling, 0 replies; 148+ messages in thread
From: Johannes Schindelin @ 2016-06-11  7:02 UTC (permalink / raw)
  To: Johannes Sixt
  Cc: Christian Couder, git, Junio C Hamano,
	Ævar Arnfjörð Bjarmason, Nguyen Thai Ngoc Duy,
	Stefan Beller, Eric Sunshine, Ramsay Jones, Jeff King,
	Karsten Blees, Matthieu Moy, Christian Couder

Hi Hannes,

On Fri, 10 Jun 2016, Johannes Sixt wrote:

> Am 10.06.2016 um 13:11 schrieb Johannes Schindelin:
> > On Fri, 10 Jun 2016, Christian Couder wrote:
> >
> > > I fixed this by moving the "close(fd)" call just after the
> > > "apply_patch()" call.
> 
> This bug in v1 was discovered by the test suite and fixed in v2.

Maybe it would be a good idea to fix test failures before sending out an
80-strong patch series.

Just sayin'.

> > and this:
> >
> > > I will have another look at the 2 other places where there are
> > > open()/close() or fopen()/fclose() calls.
> >
> > but nothing about a careful, systematic investigation of all error code
> > paths. As a consequence, I fully expect to encounter test failures as soon
> > as I test your patch series again, simply because resources are still in
> > use when they should no longer be used. In other words, my expectations
> > are now lower than they have been before, my concerns are not at all
> > addressed.
> 
> Do you trust the test suite to some degree?

Yes. To *some* degree.

For comparison, my rebase--helper patches pass the test suite for more
than a month now. I still discovered two minor problems and fixed them
yesterday.

Our test suite (and *any* test suite, really) is in *no way* a substitute
for proper review. And the first review needs to be performed by the
developer herself.

Just compare the two options: to add tests for each and every corner case,
especially error code paths, or alternatively just going through the patch
manually, inspecting every error code path from the used-resource point of
view?

I am sure you agree that the latter is a much better use of everybody's
time, and also that that review will be most effective when performed by
the person most familiar with the patches.

> It passes after the above bug was fixed in v2. In addition, haven't
> found any problems so far during daily use.

I put much more stock into the latter than the former. The former is
required, to be sure, but by far not sufficient.

TBH I was quite disappointed when I tried to run v1 and found such a
simple bug right away, which made me wonder how many more not-so-simple
bugs were to be found. All that, while I really wanted this patch series
to be good enough to enter core Git's code base.

> > > This is the newest iteration:
> > >
> > > https://github.com/chriscool/git/commits/libify-apply-use-in-am65
> >
> > And that cute 65 in the name is the revision.
> 
> Yeah, that number is painful. I would appreciate an unversioned branch
> name, too.

To be frank, I think that a version number in a branch name is incorrect
Git usage. Version numbers are something for tags, not for branches (I
would understand partial version numbers in maintenance branches, of
course, because then they would not version the *branch* but convey the
purpose of the branch, as names should).

Ciao,
Johannes

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

end of thread, other threads:[~2016-06-11  7:02 UTC | newest]

Thread overview: 148+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-05-11 13:16 [PATCH v2 00/94] libify apply and use lib in am Christian Couder
2016-05-11 13:16 ` [PATCH v2 01/94] builtin/apply: make gitdiff_verify_name() return void Christian Couder
2016-05-12 19:06   ` Junio C Hamano
2016-05-11 13:16 ` [PATCH v2 02/94] builtin/apply: avoid parameter shadowing 'p_value' global Christian Couder
2016-05-12 19:09   ` Junio C Hamano
2016-05-11 13:16 ` [PATCH v2 03/94] builtin/apply: avoid parameter shadowing 'linenr' global Christian Couder
2016-05-12 19:11   ` Junio C Hamano
2016-05-11 13:16 ` [PATCH v2 04/94] builtin/apply: avoid local variable shadowing 'len' parameter Christian Couder
2016-05-11 13:16 ` [PATCH v2 05/94] builtin/apply: extract line_by_line_fuzzy_match() from match_fragment() Christian Couder
2016-05-12 19:20   ` Junio C Hamano
2016-05-11 13:16 ` [PATCH v2 06/94] builtin/apply: move 'options' variable into cmd_apply() Christian Couder
2016-05-11 13:16 ` [PATCH v2 07/94] builtin/apply: move 'read_stdin' global " Christian Couder
2016-05-11 13:16 ` [PATCH v2 08/94] builtin/apply: introduce 'struct apply_state' to start libifying Christian Couder
2016-05-11 13:16 ` [PATCH v2 09/94] builtin/apply: move 'state' init into init_apply_state() Christian Couder
2016-05-12 19:25   ` Junio C Hamano
2016-05-11 13:16 ` [PATCH v2 10/94] builtin/apply: move 'unidiff_zero' global into 'struct apply_state' Christian Couder
2016-05-12 19:28   ` Junio C Hamano
2016-05-12 20:18     ` Christian Couder
2016-05-11 13:16 ` [PATCH v2 11/94] builtin/apply: move 'check' " Christian Couder
2016-05-11 13:16 ` [PATCH v2 12/94] builtin/apply: move 'check_index' " Christian Couder
2016-05-11 13:16 ` [PATCH v2 13/94] builtin/apply: move 'apply_in_reverse' " Christian Couder
2016-05-11 13:16 ` [PATCH v2 14/94] builtin/apply: move 'apply_with_reject' " Christian Couder
2016-05-11 13:16 ` [PATCH v2 15/94] builtin/apply: move 'apply_verbosely' " Christian Couder
2016-05-11 13:16 ` [PATCH v2 16/94] builtin/apply: move 'update_index' " Christian Couder
2016-05-12 19:31   ` Junio C Hamano
2016-05-11 13:16 ` [PATCH v2 17/94] builtin/apply: move 'allow_overlap' " Christian Couder
2016-05-11 13:16 ` [PATCH v2 18/94] builtin/apply: move 'cached' " Christian Couder
2016-05-12 19:33   ` Junio C Hamano
2016-05-11 13:16 ` [PATCH v2 19/94] builtin/apply: move 'diffstat' " Christian Couder
2016-05-11 13:16 ` [PATCH v2 20/94] builtin/apply: move 'numstat' " Christian Couder
2016-05-11 13:16 ` [PATCH v2 21/94] builtin/apply: move 'summary' " Christian Couder
2016-05-11 13:16 ` [PATCH v2 22/94] builtin/apply: move 'threeway' " Christian Couder
2016-05-12 19:41   ` Junio C Hamano
2016-05-12 20:26     ` Christian Couder
2016-05-12 21:21       ` Junio C Hamano
2016-05-11 13:16 ` [PATCH v2 23/94] builtin/apply: move 'no_add' " Christian Couder
2016-05-11 13:16 ` [PATCH v2 24/94] builtin/apply: move 'unsafe_paths' " Christian Couder
2016-05-11 13:16 ` [PATCH v2 25/94] builtin/apply: move 'line_termination' " Christian Couder
2016-05-11 13:16 ` [PATCH v2 26/94] builtin/apply: move 'fake_ancestor' " Christian Couder
2016-05-11 13:16 ` [PATCH v2 27/94] builtin/apply: move 'p_context' " Christian Couder
2016-05-11 13:16 ` [PATCH v2 28/94] builtin/apply: move 'apply' " Christian Couder
2016-05-11 13:16 ` [PATCH v2 29/94] builtin/apply: move 'patch_input_file' " Christian Couder
2016-05-11 13:16 ` [PATCH v2 30/94] builtin/apply: move 'limit_by_name' " Christian Couder
2016-05-11 13:16 ` [PATCH v2 31/94] builtin/apply: move 'has_include' " Christian Couder
2016-05-11 13:16 ` [PATCH v2 32/94] builtin/apply: move 'p_value' " Christian Couder
2016-05-11 13:16 ` [PATCH v2 33/94] builtin/apply: move 'p_value_known' " Christian Couder
2016-05-12 19:43   ` Junio C Hamano
2016-05-11 13:16 ` [PATCH v2 34/94] builtin/apply: move 'root' " Christian Couder
2016-05-11 13:16 ` [PATCH v2 35/94] builtin/apply: move 'whitespace_error' " Christian Couder
2016-05-11 13:16 ` [PATCH v2 36/94] builtin/apply: move 'whitespace_option' " Christian Couder
2016-05-11 13:16 ` [PATCH v2 37/94] builtin/apply: remove whitespace_option arg from set_default_whitespace_mode() Christian Couder
2016-05-11 13:16 ` [PATCH v2 38/94] builtin/apply: move 'squelch_whitespace_errors' into 'struct apply_state' Christian Couder
2016-05-11 13:16 ` [PATCH v2 39/94] builtin/apply: move 'applied_after_fixing_ws' " Christian Couder
2016-05-11 13:16 ` [PATCH v2 40/94] builtin/apply: move 'ws_error_action' " Christian Couder
2016-05-12 19:48   ` Junio C Hamano
2016-05-11 13:16 ` [PATCH v2 41/94] builtin/apply: move 'ws_ignore_action' " Christian Couder
2016-05-11 13:16 ` [PATCH v2 42/94] builtin/apply: move 'max_change' and 'max_len' " Christian Couder
2016-05-11 13:16 ` [PATCH v2 43/94] builtin/apply: move 'state_linenr' global " Christian Couder
2016-05-11 13:16 ` [PATCH v2 44/94] builtin/apply: move 'fn_table' " Christian Couder
2016-05-11 13:16 ` [PATCH v2 45/94] builtin/apply: move 'symlink_changes' " Christian Couder
2016-05-11 13:16 ` [PATCH v2 46/94] builtin/apply: move 'state' check into check_apply_state() Christian Couder
2016-05-11 13:16 ` [PATCH v2 47/94] builtin/apply: move applying patches into apply_all_patches() Christian Couder
2016-05-11 13:16 ` [PATCH v2 48/94] builtin/apply: rename 'prefix_' parameter to 'prefix' Christian Couder
2016-05-12 19:56   ` Junio C Hamano
2016-05-12 20:43     ` Junio C Hamano
2016-05-13 19:45       ` Christian Couder
2016-05-14 18:27         ` Junio C Hamano
2016-05-24  8:24           ` Christian Couder
2016-05-13 19:42     ` Christian Couder
2016-05-24  8:15       ` Christian Couder
2016-05-11 13:17 ` [PATCH v2 49/94] builtin/apply: move 'lock_file' global into 'struct apply_state' Christian Couder
2016-05-11 13:17 ` [PATCH v2 50/94] builtin/apply: move 'newfd' " Christian Couder
2016-05-11 13:17 ` [PATCH v2 51/94] builtin/apply: make apply_patch() return -1 instead of die()ing Christian Couder
2016-05-11 13:17 ` [PATCH v2 52/94] builtin/apply: read_patch_file() " Christian Couder
2016-05-16  1:56   ` Eric Sunshine
2016-05-16 17:19     ` Christian Couder
2016-05-11 13:17 ` [PATCH v2 53/94] builtin/apply: make find_header() " Christian Couder
2016-05-11 13:17 ` [PATCH v2 54/94] builtin/apply: make parse_chunk() return a negative integer on error Christian Couder
2016-05-16  3:04   ` Eric Sunshine
2016-05-16 18:19     ` Christian Couder
2016-06-08 15:14     ` Christian Couder
2016-05-11 13:17 ` [PATCH v2 55/94] builtin/apply: make parse_single_patch() return -1 " Christian Couder
2016-05-11 13:17 ` [PATCH v2 56/94] apply: move 'struct apply_state' to apply.h Christian Couder
2016-05-16  3:10   ` Eric Sunshine
2016-05-16 16:03     ` Junio C Hamano
2016-06-08 15:25       ` Christian Couder
2016-05-11 13:17 ` [PATCH v2 57/94] builtin/apply: make parse_whitespace_option() return -1 instead of die()ing Christian Couder
2016-05-11 13:17 ` [PATCH v2 58/94] builtin/apply: make parse_ignorewhitespace_option() " Christian Couder
2016-05-11 13:17 ` [PATCH v2 59/94] builtin/apply: move init_apply_state() to apply.c Christian Couder
2016-05-16  3:16   ` Eric Sunshine
2016-05-11 13:17 ` [PATCH v2 60/94] apply: make init_apply_state() return -1 instead of exit()ing Christian Couder
2016-05-16  3:37   ` Eric Sunshine
2016-05-11 13:17 ` [PATCH v2 61/94] builtin/apply: make check_apply_state() return -1 instead of die()ing Christian Couder
2016-05-11 13:17 ` [PATCH v2 62/94] builtin/apply: move check_apply_state() to apply.c Christian Couder
2016-05-11 13:17 ` [PATCH v2 63/94] builtin/apply: make apply_all_patches() return -1 on error Christian Couder
2016-05-16  3:44   ` Eric Sunshine
2016-06-08 16:37     ` Christian Couder
2016-06-08 17:44       ` Eric Sunshine
2016-06-09 22:01         ` Christian Couder
2016-05-11 13:17 ` [PATCH v2 64/94] builtin/apply: make parse_traditional_patch() " Christian Couder
2016-05-11 13:17 ` [PATCH v2 65/94] builtin/apply: make gitdiff_*() return 1 at end of header Christian Couder
2016-05-11 13:17 ` [PATCH v2 66/94] builtin/apply: make gitdiff_*() return -1 on error Christian Couder
2016-05-11 13:17 ` [PATCH v2 67/94] builtin/apply: change die_on_unsafe_path() to check_unsafe_path() Christian Couder
2016-05-11 13:17 ` [PATCH v2 68/94] builtin/apply: make build_fake_ancestor() return -1 on error Christian Couder
2016-05-11 13:17 ` [PATCH v2 69/94] builtin/apply: make remove_file() " Christian Couder
2016-05-11 13:17 ` [PATCH v2 70/94] builtin/apply: make add_conflicted_stages_file() " Christian Couder
2016-05-11 13:17 ` [PATCH v2 71/94] builtin/apply: make add_index_file() " Christian Couder
2016-05-11 13:17 ` [PATCH v2 72/94] builtin/apply: make create_file() " Christian Couder
2016-05-11 13:17 ` [PATCH v2 73/94] builtin/apply: make write_out_one_result() " Christian Couder
2016-05-11 13:17 ` [PATCH v2 74/94] builtin/apply: make write_out_results() " Christian Couder
2016-05-11 13:17 ` [PATCH v2 75/94] builtin/apply: make try_create_file() " Christian Couder
2016-05-11 13:17 ` [PATCH v2 76/94] builtin/apply: make create_one_file() " Christian Couder
2016-05-11 13:17 ` [PATCH v2 77/94] builtin/apply: rename option parsing functions Christian Couder
2016-05-11 13:17 ` [PATCH v2 78/94] apply: rename and move opt constants to apply.h Christian Couder
2016-05-11 13:17 ` [PATCH v2 80/94] apply: make some parsing functions static again Christian Couder
2016-05-11 13:17 ` [PATCH v2 81/94] run-command: make dup_devnull() non static Christian Couder
2016-05-11 13:17 ` [PATCH v2 82/94] apply: roll back index lock file in case of error Christian Couder
2016-05-11 13:17 ` [PATCH v2 83/94] environment: add set_index_file() Christian Couder
2016-05-11 13:17 ` [PATCH v2 84/94] builtin/am: use apply api in run_apply() Christian Couder
2016-05-11 13:17 ` [PATCH v2 85/94] write_or_die: use warning() instead of fprintf(stderr, ...) Christian Couder
2016-05-11 13:17 ` [PATCH v2 86/94] apply: add 'be_silent' variable to 'struct apply_state' Christian Couder
2016-05-11 13:17 ` [PATCH v2 87/94] apply: make 'be_silent' incomatible with 'apply_verbosely' Christian Couder
2016-05-11 13:17 ` [PATCH v2 88/94] apply: don't print on stdout when be_silent is set Christian Couder
2016-05-11 13:17 ` [PATCH v2 89/94] usage: add set_warn_routine() Christian Couder
2016-05-11 13:17 ` [PATCH v2 90/94] usage: add get_error_routine() and get_warn_routine() Christian Couder
2016-05-11 13:17 ` [PATCH v2 91/94] apply: change error_routine when be_silent is set Christian Couder
2016-05-11 13:17 ` [PATCH v2 92/94] am: use be_silent in 'struct apply_state' to shut up applying patches Christian Couder
2016-05-11 13:17 ` [PATCH v2 93/94] run-command: make dup_devnull() static again Christian Couder
2016-05-11 13:17 ` [PATCH v2 94/94] builtin/apply: add a cli option for be_silent Christian Couder
2016-05-12 17:06 ` [PATCH v2 00/94] libify apply and use lib in am Johannes Sixt
2016-05-12 18:02   ` Christian Couder
2016-06-09 21:10     ` Johannes Sixt
2016-06-10  6:40       ` Christian Couder
2016-06-10  7:01       ` Johannes Schindelin
2016-06-10  8:59         ` Christian Couder
2016-06-10 11:11           ` Johannes Schindelin
2016-06-10 17:04             ` Johannes Sixt
2016-06-10 20:31               ` Christian Couder
2016-06-11  7:02               ` Johannes Schindelin
2016-05-12 19:04   ` Junio C Hamano
2016-05-12 20:05     ` Christian Couder
2016-05-13  6:32 ` Johannes Schindelin
2016-05-13 18:49   ` Christian Couder
2016-05-14  6:26     ` Johannes Schindelin
2016-05-14  9:19       ` Christian Couder
2016-05-14 18:31         ` Junio C Hamano
2016-05-14 19:37           ` Christian Couder
2016-05-15 18:30             ` Junio C Hamano

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.