git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RFC/PATCH 00/48] Libifying git apply
@ 2016-03-09 17:48 Christian Couder
  2016-03-09 17:48 ` [RFC/PATCH 01/48] builtin/apply: avoid parameter shadowing 'p_value' global Christian Couder
                   ` (49 more replies)
  0 siblings, 50 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:48 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

This is a patch series about libifying "git apply" functionality, to
be able to use this functionality in "git am" without spawning new
processes. This should make "git am" and "git rebase" significantly
faster.

This has been discussed in the following thread:

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

This RFC patch series for now just gets rid of the global variables
and refactors the code around a bit.

As suggested by Junio the global variables in builtin/apply.c are just
thrown into a single "apply_state" structure that is passed around the
callchain. A new parameter called "state" that is a pointer to the
"apply_state" structure comes at the beginning of the helper functions
that need it.

Before I make further changes to handle erroneous input and make the
libified functions not die() and properly clean things up, I'd be
happy to get some feedback.

One point I'd especially welcome feedback about is the fact that there
are many boolean options that are using OPT_BOOL(...), so they use an
int. And there are a few others that are using OPT_BIT(...), so they
use just a bit. I wonder if it is worth it to try to be consistent,
and maybe also to try to save some memory.

Related to this, some of the variables for these options have not been
moved into the "apply_state" structure, because they are not global to
the file, but maybe for consistency they should be.

Christian Couder (48):
  builtin/apply: avoid parameter shadowing 'p_value' global
  builtin/apply: avoid parameter shadowing 'linenr' global
  builtin/apply: avoid local variable shadowing 'len' parameter
  builtin/apply: extract line_by_line_fuzzy_match() from
    match_fragment()
  builtin/apply: move 'options' variable into cmd_apply()
  builtin/apply: introduce 'struct apply_state' to start libifying
  builtin/apply: move 'newfd' global into 'struct 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 'read_stdin' global into cmd_apply()
  builtin/apply: move 'lock_file' 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 'linenr' global into 'struct apply_state'
  builtin/apply: move 'fn_table' global into 'struct apply_state'
  builtin/apply: move 'symlink_changes' global into 'struct apply_state'
  builtin/apply: move 'state' init into init_apply_state()
  builtin/apply: move 'state' check into check_apply_state()
  builtin/apply: move applying patches into apply_all_patches()

 builtin/apply.c | 1424 +++++++++++++++++++++++++++++++------------------------
 1 file changed, 817 insertions(+), 607 deletions(-)

-- 
2.8.0.rc1.49.gca61272

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

* [RFC/PATCH 01/48] builtin/apply: avoid parameter shadowing 'p_value' global
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
@ 2016-03-09 17:48 ` Christian Couder
  2016-03-09 23:27   ` Junio C Hamano
  2016-03-09 17:48 ` [RFC/PATCH 02/48] builtin/apply: avoid parameter shadowing 'linenr' global Christian Couder
                   ` (48 subsequent siblings)
  49 siblings, 1 reply; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:48 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 42c610e..7d9be63 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -469,7 +469,7 @@ 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(const char *line, const char *def, int p_v)
 {
 	struct strbuf name = STRBUF_INIT;
 	char *cp;
@@ -483,7 +483,7 @@ static char *find_name_gnu(const char *line, const char *def, int p_value)
 		return NULL;
 	}
 
-	for (cp = name.buf; p_value; p_value--) {
+	for (cp = name.buf; p_v; p_v--) {
 		cp = strchr(cp, '/');
 		if (!cp) {
 			strbuf_release(&name);
@@ -658,12 +658,12 @@ static size_t diff_timestamp_len(const char *line, size_t len)
 }
 
 static char *find_name_common(const char *line, const char *def,
-			      int p_value, const char *end, int terminate)
+			      int p_v, const char *end, int terminate)
 {
 	int len;
 	const char *start = NULL;
 
-	if (p_value == 0)
+	if (p_v == 0)
 		start = line;
 	while (line != end) {
 		char c = *line;
@@ -675,7 +675,7 @@ static char *find_name_common(const char *line, const char *def,
 				break;
 		}
 		line++;
-		if (c == '/' && !--p_value)
+		if (c == '/' && !--p_v)
 			start = line;
 	}
 	if (!start)
@@ -704,24 +704,24 @@ static char *find_name_common(const char *line, const char *def,
 	return squash_slash(xmemdupz(start, len));
 }
 
-static char *find_name(const char *line, char *def, int p_value, int terminate)
+static char *find_name(const char *line, char *def, int p_v, int terminate)
 {
 	if (*line == '"') {
-		char *name = find_name_gnu(line, def, p_value);
+		char *name = find_name_gnu(line, def, p_v);
 		if (name)
 			return name;
 	}
 
-	return find_name_common(line, def, p_value, NULL, terminate);
+	return find_name_common(line, def, p_v, NULL, terminate);
 }
 
-static char *find_name_traditional(const char *line, char *def, int p_value)
+static char *find_name_traditional(const char *line, char *def, int p_v)
 {
 	size_t len;
 	size_t date_len;
 
 	if (*line == '"') {
-		char *name = find_name_gnu(line, def, p_value);
+		char *name = find_name_gnu(line, def, p_v);
 		if (name)
 			return name;
 	}
@@ -729,10 +729,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(line, def, p_v, NULL, TERM_TAB);
 	len -= date_len;
 
-	return find_name_common(line, def, p_value, line + len, 0);
+	return find_name_common(line, def, p_v, line + len, 0);
 }
 
 static int count_slashes(const char *cp)
-- 
2.8.0.rc1.49.gca61272

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

* [RFC/PATCH 02/48] builtin/apply: avoid parameter shadowing 'linenr' global
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
  2016-03-09 17:48 ` [RFC/PATCH 01/48] builtin/apply: avoid parameter shadowing 'p_value' global Christian Couder
@ 2016-03-09 17:48 ` Christian Couder
  2016-03-09 17:48 ` [RFC/PATCH 03/48] builtin/apply: avoid local variable shadowing 'len' parameter Christian Couder
                   ` (47 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:48 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 7d9be63..3f740c7 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -1525,7 +1525,7 @@ static int find_header(const char *line, unsigned long size, int *hdrsize, struc
 	return -1;
 }
 
-static void record_ws_error(unsigned result, const char *line, int len, int linenr)
+static void record_ws_error(unsigned result, const char *line, int len, int l_nr)
 {
 	char *err;
 
@@ -1539,7 +1539,7 @@ static void record_ws_error(unsigned result, const char *line, int len, int line
 
 	err = whitespace_error_string(result);
 	fprintf(stderr, "%s:%d: %s.\n%.*s\n",
-		patch_input_file, linenr, err, len, line);
+		patch_input_file, l_nr, err, len, line);
 	free(err);
 }
 
-- 
2.8.0.rc1.49.gca61272

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

* [RFC/PATCH 03/48] builtin/apply: avoid local variable shadowing 'len' parameter
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
  2016-03-09 17:48 ` [RFC/PATCH 01/48] builtin/apply: avoid parameter shadowing 'p_value' global Christian Couder
  2016-03-09 17:48 ` [RFC/PATCH 02/48] builtin/apply: avoid parameter shadowing 'linenr' global Christian Couder
@ 2016-03-09 17:48 ` Christian Couder
  2016-03-09 17:48 ` [RFC/PATCH 04/48] builtin/apply: extract line_by_line_fuzzy_match() from match_fragment() Christian Couder
                   ` (46 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:48 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 3f740c7..c99c859 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -2196,17 +2196,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 &&
@@ -2225,11 +2225,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.0.rc1.49.gca61272

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

* [RFC/PATCH 04/48] builtin/apply: extract line_by_line_fuzzy_match() from match_fragment()
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (2 preceding siblings ...)
  2016-03-09 17:48 ` [RFC/PATCH 03/48] builtin/apply: avoid local variable shadowing 'len' parameter Christian Couder
@ 2016-03-09 17:48 ` Christian Couder
  2016-03-09 22:55   ` Stefan Beller
  2016-03-09 17:48 ` [RFC/PATCH 05/48] builtin/apply: move 'options' variable into cmd_apply() Christian Couder
                   ` (45 subsequent siblings)
  49 siblings, 1 reply; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:48 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index c99c859..7d7a8ab 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -2244,6 +2244,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,
@@ -2253,7 +2321,7 @@ static int match_fragment(struct image *img,
 			  int match_beginning, int match_end)
 {
 	int i;
-	char *fixed_buf, *buf, *orig, *target;
+	char *fixed_buf, *orig, *target;
 	struct strbuf fixed;
 	size_t fixed_len, postlen;
 	int preimage_limit;
@@ -2314,6 +2382,7 @@ static int match_fragment(struct image *img,
 		 * There must be one non-blank context line that match
 		 * a line before the end of img.
 		 */
+		char *buf;
 		char *buf_end;
 
 		buf = preimage->buf;
@@ -2334,59 +2403,7 @@ static int match_fragment(struct image *img,
 	 * 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;
+		return line_by_line_fuzzy_match(img, preimage, postimage, try, try_lno, preimage_limit);
 	}
 
 	if (ws_error_action != correct_ws_error)
-- 
2.8.0.rc1.49.gca61272

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

* [RFC/PATCH 05/48] builtin/apply: move 'options' variable into cmd_apply()
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (3 preceding siblings ...)
  2016-03-09 17:48 ` [RFC/PATCH 04/48] builtin/apply: extract line_by_line_fuzzy_match() from match_fragment() Christian Couder
@ 2016-03-09 17:48 ` Christian Couder
  2016-03-09 17:48 ` [RFC/PATCH 06/48] builtin/apply: introduce 'struct apply_state' to start libifying Christian Couder
                   ` (44 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:48 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 7d7a8ab..838e46a 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)
 {
@@ -4516,6 +4515,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.0.rc1.49.gca61272

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

* [RFC/PATCH 06/48] builtin/apply: introduce 'struct apply_state' to start libifying
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (4 preceding siblings ...)
  2016-03-09 17:48 ` [RFC/PATCH 05/48] builtin/apply: move 'options' variable into cmd_apply() Christian Couder
@ 2016-03-09 17:48 ` Christian Couder
  2016-03-09 17:48 ` [RFC/PATCH 07/48] builtin/apply: move 'newfd' global into 'struct apply_state' Christian Couder
                   ` (43 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:48 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 838e46a..462e28f 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -21,6 +21,11 @@
 #include "ll-merge.h"
 #include "rerere.h"
 
+struct apply_state {
+	const char *prefix;
+	int prefix_length;
+};
+
 /*
  *  --check turns on checking that the working tree matches the
  *    files that are being modified, but doesn't apply the patch
@@ -30,8 +35,6 @@
  *  --index updates the cache as well.
  *  --cached updates only the cache without ever touching the working tree.
  */
-static const char *prefix;
-static int prefix_length = -1;
 static int newfd = -1;
 
 static int unidiff_zero;
@@ -749,7 +752,7 @@ static int count_slashes(const char *cp)
  * Given the string after "--- " or "+++ ", guess the appropriate
  * p_value for the given patch.
  */
-static int guess_p_value(const char *nameline)
+static int guess_p_value(struct apply_state *state, const char *nameline)
 {
 	char *name, *cp;
 	int val = -1;
@@ -762,17 +765,17 @@ static int guess_p_value(const char *nameline)
 	cp = strchr(name, '/');
 	if (!cp)
 		val = 0;
-	else if (prefix) {
+	else if (state->prefix) {
 		/*
 		 * Does it begin with "a/$our-prefix" and such?  Then this is
 		 * very likely to apply to our directory.
 		 */
-		if (!strncmp(name, prefix, prefix_length))
-			val = count_slashes(prefix);
+		if (!strncmp(name, state->prefix, state->prefix_length))
+			val = count_slashes(state->prefix);
 		else {
 			cp++;
-			if (!strncmp(cp, prefix, prefix_length))
-				val = count_slashes(prefix) + 1;
+			if (!strncmp(cp, state->prefix, state->prefix_length))
+				val = count_slashes(state->prefix) + 1;
 		}
 	}
 	free(name);
@@ -859,7 +862,10 @@ static int has_epoch_timestamp(const char *nameline)
  * files, we can happily check the index for a match, but for creating a
  * new file we should try to match whatever "patch" does. I have no idea.
  */
-static void parse_traditional_patch(const char *first, const char *second, struct patch *patch)
+static void parse_traditional_patch(struct apply_state *state,
+				    const char *first,
+				    const char *second,
+				    struct patch *patch)
 {
 	char *name;
 
@@ -867,8 +873,8 @@ static void parse_traditional_patch(const char *first, const char *second, struc
 	second += 4;	/* skip "+++ " */
 	if (!p_value_known) {
 		int p, q;
-		p = guess_p_value(first);
-		q = guess_p_value(second);
+		p = guess_p_value(state, first);
+		q = guess_p_value(state, second);
 		if (p < 0) p = q;
 		if (0 <= p && p == q) {
 			p_value = p;
@@ -1439,7 +1445,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;
 
@@ -1516,7 +1526,7 @@ static int find_header(const char *line, unsigned long size, int *hdrsize, struc
 			continue;
 
 		/* Ok, we'll consider it a patch */
-		parse_traditional_patch(line, line+len, patch);
+		parse_traditional_patch(state, line, line+len, patch);
 		*hdrsize = len + nextlen;
 		linenr += 2;
 		return offset;
@@ -1918,21 +1928,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);
 }
 
 /*
@@ -1949,16 +1959,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;
 	}
 
@@ -1985,17 +1995,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
@@ -4371,7 +4381,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 */
@@ -4388,12 +4401,12 @@ 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)
 			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;
@@ -4516,6 +4529,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 	int is_not_gitdir = !startup_info->have_repository;
 	int force_apply = 0;
 	int options = 0;
+	struct apply_state state;
 
 	const char *whitespace_option = NULL;
 
@@ -4588,15 +4602,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)
@@ -4627,23 +4643,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.0.rc1.49.gca61272

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

* [RFC/PATCH 07/48] builtin/apply: move 'newfd' global into 'struct apply_state'
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (5 preceding siblings ...)
  2016-03-09 17:48 ` [RFC/PATCH 06/48] builtin/apply: introduce 'struct apply_state' to start libifying Christian Couder
@ 2016-03-09 17:48 ` Christian Couder
  2016-03-09 17:48 ` [RFC/PATCH 08/48] builtin/apply: move 'unidiff_zero' " Christian Couder
                   ` (42 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:48 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 462e28f..d56dd9f 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -24,6 +24,7 @@
 struct apply_state {
 	const char *prefix;
 	int prefix_length;
+	int newfd;
 };
 
 /*
@@ -35,7 +36,6 @@ struct apply_state {
  *  --index updates the cache as well.
  *  --cached updates only the cache without ever touching the working tree.
  */
-static int newfd = -1;
 
 static int unidiff_zero;
 static int p_value = 1;
@@ -4425,8 +4425,8 @@ static int apply_patch(struct apply_state *state,
 		apply = 0;
 
 	update_index = check_index && apply;
-	if (update_index && newfd < 0)
-		newfd = hold_locked_index(&lock_file, 1);
+	if (update_index && state->newfd < 0)
+		state->newfd = hold_locked_index(&lock_file, 1);
 
 	if (check_index) {
 		if (read_cache() < 0)
@@ -4605,6 +4605,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 	memset(&state, 0, sizeof(state));
 	state.prefix = prefix_;
 	state.prefix_length = state.prefix ? strlen(state.prefix) : 0;
+	state.newfd = -1;
 
 	git_apply_config();
 	if (apply_default_whitespace)
-- 
2.8.0.rc1.49.gca61272

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

* [RFC/PATCH 08/48] builtin/apply: move 'unidiff_zero' global into 'struct apply_state'
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (6 preceding siblings ...)
  2016-03-09 17:48 ` [RFC/PATCH 07/48] builtin/apply: move 'newfd' global into 'struct apply_state' Christian Couder
@ 2016-03-09 17:48 ` Christian Couder
  2016-03-09 17:48 ` [RFC/PATCH 09/48] builtin/apply: move 'check' " Christian Couder
                   ` (41 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:48 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index d56dd9f..d5bc30f 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -25,6 +25,8 @@ struct apply_state {
 	const char *prefix;
 	int prefix_length;
 	int newfd;
+
+	int unidiff_zero;
 };
 
 /*
@@ -37,7 +39,6 @@ struct apply_state {
  *  --cached updates only the cache without ever touching the working tree.
  */
 
-static int unidiff_zero;
 static int p_value = 1;
 static int p_value_known;
 static int check_index;
@@ -2698,7 +2699,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)
 {
@@ -2840,7 +2842,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.
@@ -2848,7 +2850,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;
@@ -3071,7 +3073,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;
@@ -3084,7 +3086,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;
@@ -3392,8 +3394,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;
@@ -3419,7 +3424,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;
 	}
@@ -3463,7 +3468,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;
 
@@ -3471,9 +3477,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;
@@ -3721,7 +3727,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;
@@ -3820,13 +3826,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;
 
@@ -3836,7 +3842,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);
 
@@ -4583,7 +4589,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.0.rc1.49.gca61272

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

* [RFC/PATCH 09/48] builtin/apply: move 'check' global into 'struct apply_state'
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (7 preceding siblings ...)
  2016-03-09 17:48 ` [RFC/PATCH 08/48] builtin/apply: move 'unidiff_zero' " Christian Couder
@ 2016-03-09 17:48 ` Christian Couder
  2016-03-09 17:48 ` [RFC/PATCH 10/48] builtin/apply: move 'check_index' " Christian Couder
                   ` (40 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:48 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index d5bc30f..37879de 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -26,12 +26,15 @@ struct apply_state {
 	int prefix_length;
 	int newfd;
 
+	/*
+	 *  --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;
@@ -2055,7 +2057,7 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
 		 * without metadata change.  A binary patch appears
 		 * empty to us here.
 		 */
-		if ((apply || check) &&
+		if ((apply || state->check) &&
 		    (!patch->is_binary && !metadata_changes(patch)))
 			die(_("patch with only garbage at line %d"), linenr);
 	}
@@ -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);
@@ -4559,7 +4561,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")),
@@ -4633,7 +4635,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.0.rc1.49.gca61272

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

* [RFC/PATCH 10/48] builtin/apply: move 'check_index' global into 'struct apply_state'
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (8 preceding siblings ...)
  2016-03-09 17:48 ` [RFC/PATCH 09/48] builtin/apply: move 'check' " Christian Couder
@ 2016-03-09 17:48 ` Christian Couder
  2016-03-09 17:48 ` [RFC/PATCH 11/48] builtin/apply: move 'apply_in_reverse' " Christian Couder
                   ` (39 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:48 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 37879de..9bc01a8 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -31,6 +31,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;
 };
 
@@ -38,13 +42,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.
- *  --index updates the cache as well.
  *  --cached updates only the cache without ever touching the working tree.
  */
 
 static int p_value = 1;
 static int p_value_known;
-static int check_index;
 static int update_index;
 static int cached;
 static int diffstat;
@@ -3250,13 +3252,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) {
@@ -3282,7 +3285,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)
 {
@@ -3300,7 +3304,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;
@@ -3359,7 +3363,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;
@@ -3386,7 +3392,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)
@@ -3436,11 +3442,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);
 	}
@@ -3475,7 +3481,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 ||
@@ -3506,7 +3512,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;
@@ -3529,7 +3538,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)
@@ -3579,11 +3588,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;
@@ -3659,7 +3670,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;
@@ -3680,7 +3691,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);
@@ -3695,14 +3706,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;
@@ -3742,7 +3753,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;
@@ -3769,7 +3780,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;
@@ -3824,7 +3835,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 && state->newfd < 0)
 		state->newfd = hold_locked_index(&lock_file, 1);
 
-	if (check_index) {
+	if (state->check_index) {
 		if (read_cache() < 0)
 			die(_("unable to read index file"));
 	}
@@ -4563,7 +4574,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")),
@@ -4631,20 +4642,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.0.rc1.49.gca61272

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

* [RFC/PATCH 11/48] builtin/apply: move 'apply_in_reverse' global into 'struct apply_state'
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (9 preceding siblings ...)
  2016-03-09 17:48 ` [RFC/PATCH 10/48] builtin/apply: move 'check_index' " Christian Couder
@ 2016-03-09 17:48 ` Christian Couder
  2016-03-09 17:48 ` [RFC/PATCH 12/48] builtin/apply: move 'apply_with_reject' " Christian Couder
                   ` (38 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:48 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 9bc01a8..6bf3024 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -26,6 +26,8 @@ struct apply_state {
 	int prefix_length;
 	int newfd;
 
+	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;
@@ -1570,8 +1571,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;
@@ -1611,12 +1615,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++;
@@ -1624,7 +1628,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++;
@@ -1680,7 +1684,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;
@@ -1692,7 +1699,7 @@ static int parse_single_patch(const char *line, unsigned long size, struct patch
 
 		fragment = xcalloc(1, sizeof(*fragment));
 		fragment->linenr = linenr;
-		len = parse_fragment(line, size, patch, fragment);
+		len = parse_fragment(state, line, size, patch, fragment);
 		if (len <= 0)
 			die(_("corrupt patch at line %d"), linenr);
 		fragment->patch = line;
@@ -2017,8 +2024,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";
@@ -2749,7 +2758,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 == '+')
@@ -2922,7 +2931,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).",
@@ -2956,7 +2965,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;
@@ -2969,7 +2980,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'",
@@ -3002,7 +3013,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];
@@ -3063,7 +3076,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);
 
@@ -3086,7 +3099,7 @@ static int apply_fragments(struct apply_state *state, struct image *img, struct
 	int nth = 0;
 
 	if (patch->is_binary)
-		return apply_binary(img, patch);
+		return apply_binary(state, img, patch);
 
 	while (frag) {
 		nth++;
@@ -4423,7 +4436,7 @@ static int apply_patch(struct apply_state *state,
 		nr = parse_chunk(state, buf.buf + offset, buf.len - offset, patch);
 		if (nr < 0)
 			break;
-		if (apply_in_reverse)
+		if (state->apply_in_reverse)
 			reverse_patches(patch);
 		if (use_patch(state, patch)) {
 			patch_stats(patch);
@@ -4600,7 +4613,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.0.rc1.49.gca61272

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

* [RFC/PATCH 12/48] builtin/apply: move 'apply_with_reject' global into 'struct apply_state'
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (10 preceding siblings ...)
  2016-03-09 17:48 ` [RFC/PATCH 11/48] builtin/apply: move 'apply_in_reverse' " Christian Couder
@ 2016-03-09 17:48 ` Christian Couder
  2016-03-09 17:48 ` [RFC/PATCH 13/48] builtin/apply: move 'apply_verbosely' " Christian Couder
                   ` (37 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:48 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 6bf3024..fcb3c14 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -27,6 +27,7 @@ struct apply_state {
 	int newfd;
 
 	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;
@@ -3105,7 +3105,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;
@@ -4617,7 +4617,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")),
@@ -4648,7 +4648,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.");
@@ -4657,7 +4657,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.0.rc1.49.gca61272

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

* [RFC/PATCH 13/48] builtin/apply: move 'apply_verbosely' global into 'struct apply_state'
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (11 preceding siblings ...)
  2016-03-09 17:48 ` [RFC/PATCH 12/48] builtin/apply: move 'apply_with_reject' " Christian Couder
@ 2016-03-09 17:48 ` Christian Couder
  2016-03-09 17:48 ` [RFC/PATCH 14/48] builtin/apply: move 'update_index' " Christian Couder
                   ` (36 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:48 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index fcb3c14..4e0737c 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -28,6 +28,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;
@@ -2814,7 +2814,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;
@@ -2929,7 +2929,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;
@@ -2951,7 +2951,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);
 	}
@@ -3865,7 +3865,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);
@@ -4296,7 +4296,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];
@@ -4311,7 +4311,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;
@@ -4367,7 +4367,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;
@@ -4382,7 +4382,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);
@@ -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 */
@@ -4621,7 +4621,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),
@@ -4658,7 +4658,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.0.rc1.49.gca61272

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

* [RFC/PATCH 14/48] builtin/apply: move 'update_index' global into 'struct apply_state'
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (12 preceding siblings ...)
  2016-03-09 17:48 ` [RFC/PATCH 13/48] builtin/apply: move 'apply_verbosely' " Christian Couder
@ 2016-03-09 17:48 ` Christian Couder
  2016-03-09 17:48 ` [RFC/PATCH 15/48] builtin/apply: move 'allow_overlap' " Christian Couder
                   ` (35 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:48 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 4e0737c..a6026cc 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -40,6 +40,8 @@ struct apply_state {
 	int check_index;
 
 	int unidiff_zero;
+
+	int update_index;
 };
 
 /*
@@ -51,7 +53,6 @@ struct apply_state {
 
 static int p_value = 1;
 static int p_value_known;
-static int update_index;
 static int cached;
 static int diffstat;
 static int numstat;
@@ -4099,9 +4100,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);
 	}
@@ -4112,14 +4113,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);
@@ -4229,13 +4234,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);
@@ -4256,7 +4262,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;
@@ -4268,22 +4274,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;
 	}
 	/*
@@ -4291,9 +4299,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)
@@ -4380,7 +4388,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 +4464,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 && state->newfd < 0)
+	state->update_index = state->check_index && apply;
+	if (state->update_index && state->newfd < 0)
 		state->newfd = hold_locked_index(&lock_file, 1);
 
 	if (state->check_index) {
@@ -4722,7 +4730,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.0.rc1.49.gca61272

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

* [RFC/PATCH 15/48] builtin/apply: move 'allow_overlap' global into 'struct apply_state'
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (13 preceding siblings ...)
  2016-03-09 17:48 ` [RFC/PATCH 14/48] builtin/apply: move 'update_index' " Christian Couder
@ 2016-03-09 17:48 ` Christian Couder
  2016-03-09 17:48 ` [RFC/PATCH 16/48] builtin/apply: move 'cached' " Christian Couder
                   ` (34 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:48 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index a6026cc..e3ca5c9 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -26,6 +26,7 @@ struct apply_state {
 	int prefix_length;
 	int newfd;
 
+	int allow_overlap;
 	int apply_in_reverse;
 	int apply_with_reject;
 	int apply_verbosely;
@@ -58,7 +59,6 @@ static int diffstat;
 static int numstat;
 static int summary;
 static int apply = 1;
-static int allow_overlap;
 static int no_add;
 static int threeway;
 static int unsafe_paths;
@@ -2637,7 +2637,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)
@@ -2702,7 +2703,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;
@@ -2950,7 +2951,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"),
@@ -4627,7 +4628,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.0.rc1.49.gca61272

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

* [RFC/PATCH 16/48] builtin/apply: move 'cached' global into 'struct apply_state'
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (14 preceding siblings ...)
  2016-03-09 17:48 ` [RFC/PATCH 15/48] builtin/apply: move 'allow_overlap' " Christian Couder
@ 2016-03-09 17:48 ` Christian Couder
  2016-03-09 17:48 ` [RFC/PATCH 17/48] builtin/apply: move 'diffstat' " Christian Couder
                   ` (33 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:48 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index e3ca5c9..85aa817 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -31,6 +31,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
@@ -49,12 +52,10 @@ 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 p_value = 1;
 static int p_value_known;
-static int cached;
 static int diffstat;
 static int numstat;
 static int summary;
@@ -3274,7 +3275,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) {
@@ -3547,7 +3548,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));
@@ -3565,9 +3566,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)
@@ -3575,7 +3576,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)
@@ -3613,7 +3614,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)) {
@@ -4107,7 +4108,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);
 		}
@@ -4140,7 +4141,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);
@@ -4192,9 +4193,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;
@@ -4272,7 +4277,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);
@@ -4598,7 +4603,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")),
@@ -4659,7 +4664,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)
@@ -4672,7 +4677,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.0.rc1.49.gca61272

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

* [RFC/PATCH 17/48] builtin/apply: move 'diffstat' global into 'struct apply_state'
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (15 preceding siblings ...)
  2016-03-09 17:48 ` [RFC/PATCH 16/48] builtin/apply: move 'cached' " Christian Couder
@ 2016-03-09 17:48 ` Christian Couder
  2016-03-09 17:48 ` [RFC/PATCH 18/48] builtin/apply: move 'numstat' " Christian Couder
                   ` (32 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:48 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 85aa817..fd5cb7d 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -34,6 +34,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
@@ -49,14 +52,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.
  */
 
 static int p_value = 1;
 static int p_value_known;
-static int diffstat;
 static int numstat;
 static int summary;
 static int apply = 1;
@@ -4494,7 +4495,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)
@@ -4591,7 +4592,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"),
@@ -4673,7 +4674,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.0.rc1.49.gca61272

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

* [RFC/PATCH 18/48] builtin/apply: move 'numstat' global into 'struct apply_state'
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (16 preceding siblings ...)
  2016-03-09 17:48 ` [RFC/PATCH 17/48] builtin/apply: move 'diffstat' " Christian Couder
@ 2016-03-09 17:48 ` Christian Couder
  2016-03-09 17:48 ` [RFC/PATCH 19/48] builtin/apply: move 'summary' " Christian Couder
                   ` (31 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:48 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index fd5cb7d..0378465 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -37,6 +37,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
@@ -52,13 +55,11 @@ 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 p_value = 1;
 static int p_value_known;
-static int numstat;
 static int summary;
 static int apply = 1;
 static int no_add;
@@ -4498,7 +4499,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)
@@ -4596,7 +4597,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")),
@@ -4674,7 +4675,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 	}
 	if (state.apply_with_reject)
 		apply = state.apply_verbosely = 1;
-	if (!force_apply && (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.0.rc1.49.gca61272

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

* [RFC/PATCH 19/48] builtin/apply: move 'summary' global into 'struct apply_state'
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (17 preceding siblings ...)
  2016-03-09 17:48 ` [RFC/PATCH 18/48] builtin/apply: move 'numstat' " Christian Couder
@ 2016-03-09 17:48 ` Christian Couder
  2016-03-09 17:48 ` [RFC/PATCH 20/48] builtin/apply: move 'threeway' " Christian Couder
                   ` (30 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:48 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 0378465..1542dcf 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -40,6 +40,8 @@ struct apply_state {
 	/* --numstat does numeric diffstat, and doesn't actually apply */
 	int numstat;
 
+	int summary;
+
 	/*
 	 *  --check turns on checking that the working tree matches the
 	 *    files that are being modified, but doesn't apply the patch
@@ -60,7 +62,6 @@ struct apply_state {
 
 static int p_value = 1;
 static int p_value_known;
-static int summary;
 static int apply = 1;
 static int no_add;
 static int threeway;
@@ -4502,7 +4503,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);
@@ -4599,7 +4600,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")),
@@ -4675,7 +4676,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 	}
 	if (state.apply_with_reject)
 		apply = state.apply_verbosely = 1;
-	if (!force_apply && (state.diffstat || 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.0.rc1.49.gca61272

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

* [RFC/PATCH 20/48] builtin/apply: move 'threeway' global into 'struct apply_state'
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (18 preceding siblings ...)
  2016-03-09 17:48 ` [RFC/PATCH 19/48] builtin/apply: move 'summary' " Christian Couder
@ 2016-03-09 17:48 ` Christian Couder
  2016-03-09 17:48 ` [RFC/PATCH 21/48] builtin/apply: move 'no-add' " Christian Couder
                   ` (29 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:48 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 1542dcf..fab731f 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -42,6 +42,8 @@ struct apply_state {
 
 	int summary;
 
+	int threeway;
+
 	/*
 	 *  --check turns on checking that the working tree matches the
 	 *    files that are being modified, but doesn't apply the patch
@@ -64,7 +66,6 @@ static int 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';
@@ -3506,7 +3507,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;
@@ -3801,7 +3802,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:
@@ -4612,7 +4613,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")),
@@ -4665,11 +4666,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.0.rc1.49.gca61272

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

* [RFC/PATCH 21/48] builtin/apply: move 'no-add' global into 'struct apply_state'
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (19 preceding siblings ...)
  2016-03-09 17:48 ` [RFC/PATCH 20/48] builtin/apply: move 'threeway' " Christian Couder
@ 2016-03-09 17:48 ` Christian Couder
  2016-03-09 17:48 ` [RFC/PATCH 22/48] builtin/apply: move 'unsafe_paths' " Christian Couder
                   ` (28 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:48 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index fab731f..ae734cf 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -44,6 +44,8 @@ struct apply_state {
 
 	int threeway;
 
+	int no_add;
+
 	/*
 	 *  --check turns on checking that the working tree matches the
 	 *    files that are being modified, but doesn't apply the patch
@@ -65,7 +67,6 @@ struct apply_state {
 static int 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';
@@ -2798,7 +2799,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;
@@ -4593,7 +4594,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.0.rc1.49.gca61272

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

* [RFC/PATCH 22/48] builtin/apply: move 'unsafe_paths' global into 'struct apply_state'
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (20 preceding siblings ...)
  2016-03-09 17:48 ` [RFC/PATCH 21/48] builtin/apply: move 'no-add' " Christian Couder
@ 2016-03-09 17:48 ` Christian Couder
  2016-03-09 17:48 ` [RFC/PATCH 23/48] builtin/apply: move 'line_termination' " Christian Couder
                   ` (27 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:48 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index ae734cf..0c2fa2e 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -58,6 +58,8 @@ struct apply_state {
 	int unidiff_zero;
 
 	int update_index;
+
+	int unsafe_paths;
 };
 
 /*
@@ -67,7 +69,6 @@ struct apply_state {
 static int 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;
@@ -3844,7 +3845,7 @@ static int check_patch(struct apply_state *state, struct patch *patch)
 		}
 	}
 
-	if (!unsafe_paths)
+	if (!state->unsafe_paths)
 		die_on_unsafe_path(patch);
 
 	/*
@@ -4610,7 +4611,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)")),
@@ -4688,7 +4689,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.0.rc1.49.gca61272

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

* [RFC/PATCH 23/48] builtin/apply: move 'line_termination' global into 'struct apply_state'
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (21 preceding siblings ...)
  2016-03-09 17:48 ` [RFC/PATCH 22/48] builtin/apply: move 'unsafe_paths' " Christian Couder
@ 2016-03-09 17:48 ` Christian Couder
  2016-03-09 17:48 ` [RFC/PATCH 24/48] builtin/apply: move 'fake_ancestor' " Christian Couder
                   ` (26 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:48 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 0c2fa2e..cc90773 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -60,6 +60,8 @@ struct apply_state {
 	int update_index;
 
 	int unsafe_paths;
+
+	int line_termination;
 };
 
 /*
@@ -70,7 +72,6 @@ static int 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>...]"),
@@ -3995,7 +3996,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;
@@ -4004,7 +4006,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);
 	}
 }
 
@@ -4504,7 +4506,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);
@@ -4620,7 +4622,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")),
@@ -4658,6 +4660,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 	state.prefix = prefix_;
 	state.prefix_length = state.prefix ? strlen(state.prefix) : 0;
 	state.newfd = -1;
+	state.line_termination = '\n';
 
 	git_apply_config();
 	if (apply_default_whitespace)
-- 
2.8.0.rc1.49.gca61272

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

* [RFC/PATCH 24/48] builtin/apply: move 'fake_ancestor' global into 'struct apply_state'
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (22 preceding siblings ...)
  2016-03-09 17:48 ` [RFC/PATCH 23/48] builtin/apply: move 'line_termination' " Christian Couder
@ 2016-03-09 17:48 ` Christian Couder
  2016-03-09 17:48 ` [RFC/PATCH 25/48] builtin/apply: move 'p_context' " Christian Couder
                   ` (25 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:48 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

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

diff --git a/builtin/apply.c b/builtin/apply.c
index cc90773..5a2f0f4 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -40,6 +40,8 @@ struct apply_state {
 	/* --numstat does numeric diffstat, and doesn't actually apply */
 	int numstat;
 
+	const char *fake_ancestor;
+
 	int summary;
 
 	int threeway;
@@ -64,14 +66,9 @@ struct apply_state {
 	int line_termination;
 };
 
-/*
- *  --index-info shows the old and new index info for paths if available.
- */
-
 static int 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>...]"),
@@ -4499,8 +4496,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);
@@ -4619,7 +4616,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.0.rc1.49.gca61272

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

* [RFC/PATCH 25/48] builtin/apply: move 'p_context' global into 'struct apply_state'
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (23 preceding siblings ...)
  2016-03-09 17:48 ` [RFC/PATCH 24/48] builtin/apply: move 'fake_ancestor' " Christian Couder
@ 2016-03-09 17:48 ` Christian Couder
  2016-03-09 17:48 ` [RFC/PATCH 26/48] builtin/apply: move 'apply' " Christian Couder
                   ` (24 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:48 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 5a2f0f4..6e347e2 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -64,12 +64,13 @@ struct apply_state {
 	int unsafe_paths;
 
 	int line_termination;
+
+	unsigned int p_context;
 };
 
 static int 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
@@ -2889,7 +2890,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;
@@ -4621,7 +4622,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"),
@@ -4658,6 +4659,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 	state.prefix_length = state.prefix ? strlen(state.prefix) : 0;
 	state.newfd = -1;
 	state.line_termination = '\n';
+	state.p_context = UINT_MAX;
 
 	git_apply_config();
 	if (apply_default_whitespace)
-- 
2.8.0.rc1.49.gca61272

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

* [RFC/PATCH 26/48] builtin/apply: move 'apply' global into 'struct apply_state'
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (24 preceding siblings ...)
  2016-03-09 17:48 ` [RFC/PATCH 25/48] builtin/apply: move 'p_context' " Christian Couder
@ 2016-03-09 17:48 ` Christian Couder
  2016-03-09 17:48 ` [RFC/PATCH 27/48] builtin/apply: move 'read_stdin' global into cmd_apply() Christian Couder
                   ` (23 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:48 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 6e347e2..6342b61 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -26,6 +26,7 @@ struct apply_state {
 	int prefix_length;
 	int newfd;
 
+	int apply;
 	int allow_overlap;
 	int apply_in_reverse;
 	int apply_with_reject;
@@ -70,7 +71,7 @@ struct apply_state {
 
 static int p_value = 1;
 static int p_value_known;
-static int apply = 1;
+
 static const char * const apply_usage[] = {
 	N_("git apply [<options>] [<patch>...]"),
 	NULL
@@ -141,10 +142,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);
 }
 
 /*
@@ -2075,7 +2077,7 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
 		 * without metadata change.  A binary patch appears
 		 * empty to us here.
 		 */
-		if ((apply || state->check) &&
+		if ((state->apply || state->check) &&
 		    (!patch->is_binary && !metadata_changes(patch)))
 			die(_("patch with only garbage at line %d"), linenr);
 	}
@@ -2934,7 +2936,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) {
@@ -4474,9 +4476,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 && state->newfd < 0)
 		state->newfd = hold_locked_index(&lock_file, 1);
 
@@ -4485,12 +4487,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 */
@@ -4657,6 +4659,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 	memset(&state, 0, sizeof(state));
 	state.prefix = prefix_;
 	state.prefix_length = state.prefix ? strlen(state.prefix) : 0;
+	state.apply = 1;
 	state.newfd = -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.0.rc1.49.gca61272

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

* [RFC/PATCH 27/48] builtin/apply: move 'read_stdin' global into cmd_apply()
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (25 preceding siblings ...)
  2016-03-09 17:48 ` [RFC/PATCH 26/48] builtin/apply: move 'apply' " Christian Couder
@ 2016-03-09 17:48 ` Christian Couder
  2016-03-09 17:48 ` [RFC/PATCH 28/48] builtin/apply: move 'lock_file' global into 'struct apply_state' Christian Couder
                   ` (22 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:48 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 6342b61..44fdb9d 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -95,7 +95,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)
 {
@@ -4583,6 +4582,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 	int is_not_gitdir = !startup_info->have_repository;
 	int force_apply = 0;
 	int options = 0;
+	int read_stdin = 1;
 	struct apply_state state;
 
 	const char *whitespace_option = NULL;
-- 
2.8.0.rc1.49.gca61272

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

* [RFC/PATCH 28/48] builtin/apply: move 'lock_file' global into 'struct apply_state'
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (26 preceding siblings ...)
  2016-03-09 17:48 ` [RFC/PATCH 27/48] builtin/apply: move 'read_stdin' global into cmd_apply() Christian Couder
@ 2016-03-09 17:48 ` Christian Couder
  2016-03-09 17:48 ` [RFC/PATCH 29/48] builtin/apply: move 'patch_input_file' " Christian Couder
                   ` (21 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:48 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 44fdb9d..70bb51a 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -67,6 +67,8 @@ struct apply_state {
 	int line_termination;
 
 	unsigned int p_context;
+
+	struct lock_file lock_file;
 };
 
 static int p_value = 1;
@@ -4429,8 +4431,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)
 
@@ -4479,7 +4479,7 @@ static int apply_patch(struct apply_state *state,
 
 	state->update_index = state->check_index && state->apply;
 	if (state->update_index && state->newfd < 0)
-		state->newfd = hold_locked_index(&lock_file, 1);
+		state->newfd = hold_locked_index(&state->lock_file, 1);
 
 	if (state->check_index) {
 		if (read_cache() < 0)
@@ -4748,7 +4748,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 	}
 
 	if (state.update_index) {
-		if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))
+		if (write_locked_index(&the_index, &state.lock_file, COMMIT_LOCK))
 			die(_("Unable to write new index file"));
 	}
 
-- 
2.8.0.rc1.49.gca61272

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

* [RFC/PATCH 29/48] builtin/apply: move 'patch_input_file' global into 'struct apply_state'
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (27 preceding siblings ...)
  2016-03-09 17:48 ` [RFC/PATCH 28/48] builtin/apply: move 'lock_file' global into 'struct apply_state' Christian Couder
@ 2016-03-09 17:48 ` Christian Couder
  2016-03-09 17:48 ` [RFC/PATCH 30/48] builtin/apply: move 'limit_by_name' " Christian Couder
                   ` (20 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:48 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 70bb51a..e94594d 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -69,6 +69,8 @@ struct apply_state {
 	unsigned int p_context;
 
 	struct lock_file lock_file;
+
+	const char *patch_input_file;
 };
 
 static int p_value = 1;
@@ -95,7 +97,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)
@@ -1550,7 +1551,11 @@ static int find_header(struct apply_state *state,
 	return -1;
 }
 
-static void record_ws_error(unsigned result, const char *line, int len, int l_nr)
+static void record_ws_error(struct apply_state *state,
+			    unsigned result,
+			    const char *line,
+			    int len,
+			    int l_nr)
 {
 	char *err;
 
@@ -1564,15 +1569,18 @@ static void record_ws_error(unsigned result, const char *line, int len, int l_nr
 
 	err = whitespace_error_string(result);
 	fprintf(stderr, "%s:%d: %s.\n%.*s\n",
-		patch_input_file, l_nr, err, len, line);
+		state->patch_input_file, l_nr, err, len, line);
 	free(err);
 }
 
-static void check_whitespace(const char *line, int len, unsigned ws_rule)
+static void check_whitespace(struct apply_state *state,
+			     const char *line,
+			     int len,
+			     unsigned ws_rule)
 {
 	unsigned result = ws_check(line + 1, len - 1, ws_rule);
 
-	record_ws_error(result, line + 1, len - 2, linenr);
+	record_ws_error(state, result, line + 1, len - 2, linenr);
 }
 
 /*
@@ -1627,12 +1635,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;
@@ -1640,7 +1648,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;
@@ -2923,7 +2931,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--)
@@ -4444,7 +4452,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.0.rc1.49.gca61272

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

* [RFC/PATCH 30/48] builtin/apply: move 'limit_by_name' global into 'struct apply_state'
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (28 preceding siblings ...)
  2016-03-09 17:48 ` [RFC/PATCH 29/48] builtin/apply: move 'patch_input_file' " Christian Couder
@ 2016-03-09 17:48 ` Christian Couder
  2016-03-09 17:48 ` [RFC/PATCH 31/48] builtin/apply: move 'has_include' " Christian Couder
                   ` (19 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:48 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index e94594d..186e451 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -71,6 +71,8 @@ struct apply_state {
 	struct lock_file lock_file;
 
 	const char *patch_input_file;
+
+	struct string_list limit_by_name;
 };
 
 static int p_value = 1;
@@ -1979,13 +1981,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;
 }
 
@@ -2003,8 +2006,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);
 	}
@@ -4534,14 +4537,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;
 }
@@ -4596,10 +4601,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.0.rc1.49.gca61272

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

* [RFC/PATCH 31/48] builtin/apply: move 'has_include' global into 'struct apply_state'
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (29 preceding siblings ...)
  2016-03-09 17:48 ` [RFC/PATCH 30/48] builtin/apply: move 'limit_by_name' " Christian Couder
@ 2016-03-09 17:48 ` Christian Couder
  2016-03-09 17:49 ` [RFC/PATCH 32/48] builtin/apply: move 'p_value' " Christian Couder
                   ` (18 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:48 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 186e451..fa2940d 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -73,6 +73,7 @@ struct apply_state {
 	const char *patch_input_file;
 
 	struct string_list limit_by_name;
+	int has_include;
 };
 
 static int p_value = 1;
@@ -1981,7 +1982,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)
@@ -2017,7 +2017,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;
 }
 
 
@@ -4547,7 +4547,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.0.rc1.49.gca61272

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

* [RFC/PATCH 32/48] builtin/apply: move 'p_value' global into 'struct apply_state'
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (30 preceding siblings ...)
  2016-03-09 17:48 ` [RFC/PATCH 31/48] builtin/apply: move 'has_include' " Christian Couder
@ 2016-03-09 17:49 ` Christian Couder
  2016-03-09 17:49 ` [RFC/PATCH 33/48] builtin/apply: move 'p_value_known' " Christian Couder
                   ` (17 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:49 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index fa2940d..8d4a900 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -66,6 +66,7 @@ struct apply_state {
 
 	int line_termination;
 
+	int p_value;
 	unsigned int p_context;
 
 	struct lock_file lock_file;
@@ -76,7 +77,6 @@ struct apply_state {
 	int has_include;
 };
 
-static int p_value = 1;
 static int p_value_known;
 
 static const char * const apply_usage[] = {
@@ -897,24 +897,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) {
-			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;
@@ -933,7 +933,9 @@ static void parse_traditional_patch(struct apply_state *state,
 		die(_("unable to find filename in patch at line %d"), linenr);
 }
 
-static int gitdiff_hdrend(const char *line, struct patch *patch)
+static int gitdiff_hdrend(struct apply_state *state,
+			  const char *line,
+			  struct patch *patch)
 {
 	return -1;
 }
@@ -950,10 +952,14 @@ 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 char *gitdiff_verify_name(struct apply_state *state,
+				 const char *line,
+				 int isnull,
+				 char *orig_name,
+				 int side)
 {
 	if (!orig_name && !isnull)
-		return find_name(line, NULL, p_value, TERM_TAB);
+		return find_name(line, NULL, state->p_value, TERM_TAB);
 
 	if (orig_name) {
 		int len;
@@ -963,7 +969,7 @@ static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name,
 		len = strlen(name);
 		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") :
@@ -979,87 +985,111 @@ static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name,
 	}
 }
 
-static int gitdiff_oldname(const char *line, struct patch *patch)
+static int gitdiff_oldname(struct apply_state *state,
+			   const char *line,
+			   struct patch *patch)
 {
 	char *orig = patch->old_name;
-	patch->old_name = gitdiff_verify_name(line, patch->is_new, patch->old_name,
+	patch->old_name = gitdiff_verify_name(state, line,
+					      patch->is_new, patch->old_name,
 					      DIFF_OLD_NAME);
 	if (orig != patch->old_name)
 		free(orig);
 	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)
 {
 	char *orig = patch->new_name;
-	patch->new_name = gitdiff_verify_name(line, patch->is_delete, patch->new_name,
+	patch->new_name = gitdiff_verify_name(state, line,
+					      patch->is_delete, patch->new_name,
 					      DIFF_NEW_NAME);
 	if (orig != patch->new_name)
 		free(orig);
 	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, 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;
 }
 
-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, 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;
 }
 
-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, 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;
 }
 
-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, 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;
 }
 
-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)
@@ -1067,7 +1097,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)
@@ -1075,7 +1107,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,
@@ -1112,7 +1146,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;
 }
@@ -1121,15 +1157,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 (!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)
@@ -1146,7 +1184,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;
@@ -1164,7 +1204,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);
@@ -1181,7 +1221,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 */
@@ -1192,7 +1232,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 ||
@@ -1207,7 +1247,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;
 
@@ -1223,7 +1263,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;
 
@@ -1267,7 +1307,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;
@@ -1283,7 +1323,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;
 
@@ -1297,7 +1341,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);
@@ -1310,7 +1354,7 @@ static int parse_git_header(const char *line, int len, unsigned int size, struct
 	for (offset = len ; size > 0 ; offset += len, size -= len, line += len, linenr++) {
 		static const struct opentry {
 			const char *str;
-			int (*fn)(const char *, struct patch *);
+			int (*fn)(struct apply_state *, const char *, struct patch *);
 		} optable[] = {
 			{ "@@ -", gitdiff_hdrend },
 			{ "--- ", gitdiff_oldname },
@@ -1340,7 +1384,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;
 		}
@@ -1510,7 +1554,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) {
@@ -1519,8 +1563,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)",
-					       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);
 			}
@@ -4552,9 +4596,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)
 {
-	p_value = atoi(arg);
+	struct apply_state *state = opt->value;
+	state->p_value = atoi(arg);
 	p_value_known = 1;
 	return 0;
 }
@@ -4607,7 +4653,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,
@@ -4675,6 +4721,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 	state.apply = 1;
 	state.newfd = -1;
 	state.line_termination = '\n';
+	state.p_value = 1;
 	state.p_context = UINT_MAX;
 
 	git_apply_config();
-- 
2.8.0.rc1.49.gca61272

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

* [RFC/PATCH 33/48] builtin/apply: move 'p_value_known' global into 'struct apply_state'
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (31 preceding siblings ...)
  2016-03-09 17:49 ` [RFC/PATCH 32/48] builtin/apply: move 'p_value' " Christian Couder
@ 2016-03-09 17:49 ` Christian Couder
  2016-03-09 17:49 ` [RFC/PATCH 34/48] builtin/apply: move 'root' " Christian Couder
                   ` (16 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:49 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 8d4a900..cb995c8 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -67,6 +67,7 @@ struct apply_state {
 	int line_termination;
 
 	int p_value;
+	int p_value_known;
 	unsigned int p_context;
 
 	struct lock_file lock_file;
@@ -77,8 +78,6 @@ struct apply_state {
 	int has_include;
 };
 
-static int p_value_known;
-
 static const char * const apply_usage[] = {
 	N_("git apply [<options>] [<patch>...]"),
 	NULL
@@ -891,14 +890,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)) {
@@ -4601,7 +4600,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.0.rc1.49.gca61272

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

* [RFC/PATCH 34/48] builtin/apply: move 'root' global into 'struct apply_state'
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (32 preceding siblings ...)
  2016-03-09 17:49 ` [RFC/PATCH 33/48] builtin/apply: move 'p_value_known' " Christian Couder
@ 2016-03-09 17:49 ` Christian Couder
  2016-03-09 17:49 ` [RFC/PATCH 35/48] builtin/apply: move 'whitespace_error' " Christian Couder
                   ` (15 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:49 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index cb995c8..8571646 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -76,6 +76,8 @@ struct apply_state {
 
 	struct string_list limit_by_name;
 	int has_include;
+
+	struct strbuf root;
 };
 
 static const char * const apply_usage[] = {
@@ -99,8 +101,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) {
@@ -490,7 +490,10 @@ static char *squash_slash(char *name)
 	return name;
 }
 
-static char *find_name_gnu(const char *line, const char *def, int p_v)
+static char *find_name_gnu(struct apply_state *state,
+			   const char *line,
+			   const char *def,
+			   int p_v)
 {
 	struct strbuf name = STRBUF_INIT;
 	char *cp;
@@ -514,8 +517,8 @@ static char *find_name_gnu(const char *line, const char *def, int p_v)
 	}
 
 	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));
 }
 
@@ -678,8 +681,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_v, const char *end, int terminate)
+static char *find_name_common(struct apply_state *state,
+			      const char *line,
+			      const char *def,
+			      int p_v,
+			      const char *end,
+			      int terminate)
 {
 	int len;
 	const char *start = NULL;
@@ -717,32 +724,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_v, int terminate)
+static char *find_name(struct apply_state *state,
+		       const char *line,
+		       char *def,
+		       int p_v,
+		       int terminate)
 {
 	if (*line == '"') {
-		char *name = find_name_gnu(line, def, p_v);
+		char *name = find_name_gnu(state, line, def, p_v);
 		if (name)
 			return name;
 	}
 
-	return find_name_common(line, def, p_v, NULL, terminate);
+	return find_name_common(state, line, def, p_v, NULL, terminate);
 }
 
-static char *find_name_traditional(const char *line, char *def, int p_v)
+static char *find_name_traditional(struct apply_state *state,
+				   const char *line,
+				   char *def,
+				   int p_v)
 {
 	size_t len;
 	size_t date_len;
 
 	if (*line == '"') {
-		char *name = find_name_gnu(line, def, p_v);
+		char *name = find_name_gnu(state, line, def, p_v);
 		if (name)
 			return name;
 	}
@@ -750,10 +764,10 @@ static char *find_name_traditional(const char *line, char *def, int p_v)
 	len = strchrnul(line, '\n') - line;
 	date_len = diff_timestamp_len(line, len);
 	if (!date_len)
-		return find_name_common(line, def, p_v, NULL, TERM_TAB);
+		return find_name_common(state, line, def, p_v, NULL, TERM_TAB);
 	len -= date_len;
 
-	return find_name_common(line, def, p_v, line + len, 0);
+	return find_name_common(state, line, def, p_v, line + len, 0);
 }
 
 static int count_slashes(const char *cp)
@@ -778,7 +792,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, '/');
@@ -903,17 +917,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;
@@ -958,7 +972,7 @@ static char *gitdiff_verify_name(struct apply_state *state,
 				 int side)
 {
 	if (!orig_name && !isnull)
-		return find_name(line, NULL, state->p_value, TERM_TAB);
+		return find_name(state, line, NULL, state->p_value, TERM_TAB);
 
 	if (orig_name) {
 		int len;
@@ -968,7 +982,7 @@ static char *gitdiff_verify_name(struct apply_state *state,
 		len = strlen(name);
 		if (isnull)
 			die(_("git apply: bad git-diff - expected /dev/null, got %s on line %d"), name, linenr);
-		another = find_name(line, NULL, state->p_value, TERM_TAB);
+		another = find_name(state, line, NULL, state->p_value, TERM_TAB);
 		if (!another || memcmp(another, name, len + 1))
 			die((side == DIFF_NEW_NAME) ?
 			    _("git apply: bad git-diff - inconsistent new filename on line %d") :
@@ -1052,7 +1066,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;
 }
 
@@ -1062,7 +1076,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;
 }
 
@@ -1072,7 +1086,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;
 }
 
@@ -1082,7 +1096,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;
 }
 
@@ -1341,8 +1355,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;
 	}
@@ -4627,9 +4641,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;
 }
 
@@ -4708,7 +4723,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()
@@ -4722,6 +4737,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 	state.line_termination = '\n';
 	state.p_value = 1;
 	state.p_context = UINT_MAX;
+	strbuf_init(&state.root, 0);
 
 	git_apply_config();
 	if (apply_default_whitespace)
-- 
2.8.0.rc1.49.gca61272

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

* [RFC/PATCH 35/48] builtin/apply: move 'whitespace_error' global into 'struct apply_state'
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (33 preceding siblings ...)
  2016-03-09 17:49 ` [RFC/PATCH 34/48] builtin/apply: move 'root' " Christian Couder
@ 2016-03-09 17:49 ` Christian Couder
  2016-03-09 17:49 ` [RFC/PATCH 36/48] builtin/apply: move 'whitespace_option' " Christian Couder
                   ` (14 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:49 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 8571646..1d665c2 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -78,6 +78,8 @@ struct apply_state {
 	int has_include;
 
 	struct strbuf root;
+
+	int whitespace_error;
 };
 
 static const char * const apply_usage[] = {
@@ -91,7 +93,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;
 
@@ -1622,9 +1623,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);
@@ -2875,7 +2876,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);
 			}
@@ -4542,7 +4543,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;
@@ -4795,11 +4796,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),
@@ -4808,18 +4809,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.0.rc1.49.gca61272

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

* [RFC/PATCH 36/48] builtin/apply: move 'whitespace_option' into 'struct apply_state'
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (34 preceding siblings ...)
  2016-03-09 17:49 ` [RFC/PATCH 35/48] builtin/apply: move 'whitespace_error' " Christian Couder
@ 2016-03-09 17:49 ` Christian Couder
  2016-03-09 17:49 ` [RFC/PATCH 37/48] builtin/apply: remove whitespace_option arg from set_default_whitespace_mode() Christian Couder
                   ` (13 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:49 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

This will enable further refactoring.

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 1d665c2..63eb031 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -79,6 +79,7 @@ struct apply_state {
 
 	struct strbuf root;
 
+	const char *whitespace_option;
 	int whitespace_error;
 };
 
@@ -4632,9 +4633,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;
 }
@@ -4659,8 +4660,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"),
@@ -4700,7 +4699,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,
@@ -4789,11 +4788,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.0.rc1.49.gca61272

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

* [RFC/PATCH 37/48] builtin/apply: remove whitespace_option arg from set_default_whitespace_mode()
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (35 preceding siblings ...)
  2016-03-09 17:49 ` [RFC/PATCH 36/48] builtin/apply: move 'whitespace_option' " Christian Couder
@ 2016-03-09 17:49 ` Christian Couder
  2016-03-09 17:49 ` [RFC/PATCH 38/48] builtin/apply: move 'squelch_whitespace_errors' into 'struct apply_state' Christian Couder
                   ` (12 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:49 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 63eb031..3c00551 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -148,10 +148,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);
 }
 
@@ -4788,11 +4787,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.0.rc1.49.gca61272

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

* [RFC/PATCH 38/48] builtin/apply: move 'squelch_whitespace_errors' into 'struct apply_state'
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (36 preceding siblings ...)
  2016-03-09 17:49 ` [RFC/PATCH 37/48] builtin/apply: remove whitespace_option arg from set_default_whitespace_mode() Christian Couder
@ 2016-03-09 17:49 ` Christian Couder
  2016-03-09 17:49 ` [RFC/PATCH 39/48] builtin/apply: move 'applied_after_fixing_ws' " Christian Couder
                   ` (11 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:49 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 3c00551..a2e1e51 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -81,6 +81,7 @@ struct apply_state {
 
 	const char *whitespace_option;
 	int whitespace_error;
+	int squelch_whitespace_errors;
 };
 
 static const char * const apply_usage[] = {
@@ -94,7 +95,6 @@ static enum ws_error_action {
 	die_on_ws_error,
 	correct_ws_error
 } ws_error_action = warn_on_ws_error;
-static int squelch_whitespace_errors = 5;
 static int applied_after_fixing_ws;
 
 static enum ws_ignore {
@@ -103,7 +103,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;
@@ -123,7 +123,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")) {
@@ -1624,8 +1624,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);
@@ -4633,9 +4633,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;
 }
 
@@ -4736,11 +4735,12 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 	state.line_termination = '\n';
 	state.p_value = 1;
 	state.p_context = UINT_MAX;
+	state.squelch_whitespace_errors = 5;
 	strbuf_init(&state.root, 0);
 
 	git_apply_config();
 	if (apply_default_whitespace)
-		parse_whitespace_option(apply_default_whitespace);
+		parse_whitespace_option(&state, apply_default_whitespace);
 	if (apply_default_ignorewhitespace)
 		parse_ignorewhitespace_option(apply_default_ignorewhitespace);
 
@@ -4795,10 +4795,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.0.rc1.49.gca61272

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

* [RFC/PATCH 39/48] builtin/apply: move 'applied_after_fixing_ws' into 'struct apply_state'
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (37 preceding siblings ...)
  2016-03-09 17:49 ` [RFC/PATCH 38/48] builtin/apply: move 'squelch_whitespace_errors' into 'struct apply_state' Christian Couder
@ 2016-03-09 17:49 ` Christian Couder
  2016-03-09 17:49 ` [RFC/PATCH 40/48] builtin/apply: move 'ws_error_action' " Christian Couder
                   ` (10 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:49 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index a2e1e51..9a2b8af 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -82,6 +82,7 @@ struct apply_state {
 	const char *whitespace_option;
 	int whitespace_error;
 	int squelch_whitespace_errors;
+	int applied_after_fixing_ws;
 };
 
 static const char * const apply_usage[] = {
@@ -95,7 +96,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,
@@ -2881,7 +2881,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));
@@ -4809,11 +4809,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.0.rc1.49.gca61272

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

* [RFC/PATCH 40/48] builtin/apply: move 'ws_error_action' into 'struct apply_state'
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (38 preceding siblings ...)
  2016-03-09 17:49 ` [RFC/PATCH 39/48] builtin/apply: move 'applied_after_fixing_ws' " Christian Couder
@ 2016-03-09 17:49 ` Christian Couder
  2016-03-09 17:49 ` [RFC/PATCH 41/48] builtin/apply: move 'ws_ignore_action' " Christian Couder
                   ` (9 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:49 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 9a2b8af..d427c3c 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;
@@ -83,6 +90,8 @@ struct apply_state {
 	int whitespace_error;
 	int squelch_whitespace_errors;
 	int applied_after_fixing_ws;
+
+	enum ws_error_action ws_error_action;
 };
 
 static const char * const apply_usage[] = {
@@ -90,12 +99,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,
@@ -106,28 +109,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);
@@ -151,7 +154,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);
 }
 
 /*
@@ -1695,12 +1698,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--;
@@ -1708,7 +1711,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--;
@@ -2414,7 +2417,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,
@@ -2435,7 +2439,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
@@ -2508,7 +2512,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;
 
 	/*
@@ -2620,7 +2624,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,
@@ -2664,7 +2669,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;
@@ -2877,7 +2882,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 {
@@ -2955,7 +2960,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)
@@ -2991,10 +2996,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);
 			}
@@ -3005,7 +3010,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;
 		}
 
@@ -4543,7 +4548,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;
@@ -4736,6 +4741,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 	state.p_value = 1;
 	state.p_context = UINT_MAX;
 	state.squelch_whitespace_errors = 5;
+	state.ws_error_action = warn_on_ws_error;
 	strbuf_init(&state.root, 0);
 
 	git_apply_config();
@@ -4804,7 +4810,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.0.rc1.49.gca61272

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

* [RFC/PATCH 41/48] builtin/apply: move 'ws_ignore_action' into 'struct apply_state'
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (39 preceding siblings ...)
  2016-03-09 17:49 ` [RFC/PATCH 40/48] builtin/apply: move 'ws_error_action' " Christian Couder
@ 2016-03-09 17:49 ` Christian Couder
  2016-03-09 17:49 ` [RFC/PATCH 42/48] builtin/apply: move 'max_change' and 'max_len' " Christian Couder
                   ` (8 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:49 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index d427c3c..dbb9f0b 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;
@@ -92,6 +98,7 @@ struct apply_state {
 	int applied_after_fixing_ws;
 
 	enum ws_error_action ws_error_action;
+	enum ws_ignore ws_ignore_action;
 };
 
 static const char * const apply_usage[] = {
@@ -99,13 +106,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) {
@@ -136,16 +136,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);
@@ -2508,7 +2509,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);
 	}
 
@@ -4625,12 +4626,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;
 }
 
@@ -4705,10 +4707,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,
@@ -4742,13 +4744,14 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 	state.p_context = UINT_MAX;
 	state.squelch_whitespace_errors = 5;
 	state.ws_error_action = warn_on_ws_error;
+	state.ws_ignore_action = ignore_ws_none;
 	strbuf_init(&state.root, 0);
 
 	git_apply_config();
 	if (apply_default_whitespace)
 		parse_whitespace_option(&state, apply_default_whitespace);
 	if (apply_default_ignorewhitespace)
-		parse_ignorewhitespace_option(apply_default_ignorewhitespace);
+		parse_ignorewhitespace_option(&state, apply_default_ignorewhitespace);
 
 	argc = parse_options(argc, argv, state.prefix, builtin_apply_options,
 			apply_usage, 0);
-- 
2.8.0.rc1.49.gca61272

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

* [RFC/PATCH 42/48] builtin/apply: move 'max_change' and 'max_len' into 'struct apply_state'
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (40 preceding siblings ...)
  2016-03-09 17:49 ` [RFC/PATCH 41/48] builtin/apply: move 'ws_ignore_action' " Christian Couder
@ 2016-03-09 17:49 ` Christian Couder
  2016-03-09 17:49 ` [RFC/PATCH 43/48] builtin/apply: move 'linenr' global " Christian Couder
                   ` (7 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:49 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index dbb9f0b..79224fd 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -79,6 +79,14 @@ struct apply_state {
 
 	int line_termination;
 
+	/*
+	 * For "diff-stat" like behaviour, we keep track of the biggest change
+	 * we've seen, and the longest filename. That allows us to do simple
+	 * scaling.
+	 */
+	int max_change;
+	int max_len;
+
 	int p_value;
 	int p_value_known;
 	unsigned int p_context;
@@ -159,13 +167,6 @@ static void set_default_whitespace_mode(struct apply_state *state)
 }
 
 /*
- * For "diff-stat" like behaviour, we keep track of the biggest change
- * we've seen, and the longest filename. That allows us to do simple
- * scaling.
- */
-static int max_change, max_len;
-
-/*
  * Various "current state", notably line numbers and what
  * file (and how) we're patching right now.. The "is_xxxx"
  * things are flags, where -1 means "don't know yet".
@@ -2191,7 +2192,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;
@@ -2202,7 +2203,7 @@ static void show_stats(struct patch *patch)
 	/*
 	 * "scale" the filename
 	 */
-	max = max_len;
+	max = state->max_len;
 	if (max > 50)
 		max = 50;
 
@@ -2225,13 +2226,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,
@@ -4058,7 +4059,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;
 
@@ -4066,7 +4067,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);
@@ -4164,25 +4165,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;
 	}
 }
 
@@ -4535,7 +4536,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;
 		}
@@ -4577,7 +4578,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.0.rc1.49.gca61272

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

* [RFC/PATCH 43/48] builtin/apply: move 'linenr' global into 'struct apply_state'
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (41 preceding siblings ...)
  2016-03-09 17:49 ` [RFC/PATCH 42/48] builtin/apply: move 'max_change' and 'max_len' " Christian Couder
@ 2016-03-09 17:49 ` Christian Couder
  2016-03-09 17:49 ` [RFC/PATCH 44/48] builtin/apply: move 'fn_table' " Christian Couder
                   ` (6 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:49 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 79224fd..3808d60 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -87,6 +87,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;
@@ -167,13 +174,6 @@ static void set_default_whitespace_mode(struct apply_state *state)
 }
 
 /*
- * Various "current state", notably line numbers and what
- * file (and how) we're patching right now.. The "is_xxxx"
- * things are flags, where -1 means "don't know yet".
- */
-static int linenr = 1;
-
-/*
  * This represents one "hunk" from a patch, starting with
  * "@@ -oldpos,oldlines +newpos,newlines @@" marker.  The
  * patch text is pointed at by patch, and its byte length
@@ -949,7 +949,7 @@ static void parse_traditional_patch(struct apply_state *state,
 		}
 	}
 	if (!name)
-		die(_("unable to find filename in patch at line %d"), linenr);
+		die(_("unable to find filename in patch at line %d"), state->linenr);
 }
 
 static int gitdiff_hdrend(struct apply_state *state,
@@ -987,19 +987,19 @@ static char *gitdiff_verify_name(struct apply_state *state,
 		name = orig_name;
 		len = strlen(name);
 		if (isnull)
-			die(_("git apply: bad git-diff - expected /dev/null, got %s on line %d"), name, linenr);
+			die(_("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) ?
 			    _("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);
 		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);
+			die(_("git apply: bad git-diff - expected /dev/null on line %d"), state->linenr);
 		return NULL;
 	}
 }
@@ -1369,8 +1369,8 @@ static int parse_git_header(struct apply_state *state,
 
 	line += len;
 	size -= len;
-	linenr++;
-	for (offset = len ; size > 0 ; offset += len, size -= len, line += len, linenr++) {
+	state->linenr++;
+	for (offset = len ; size > 0 ; offset += len, size -= len, line += len, state->linenr++) {
 		static const struct opentry {
 			const char *str;
 			int (*fn)(struct apply_state *, const char *, struct patch *);
@@ -1541,7 +1541,7 @@ static int find_header(struct apply_state *state,
 	patch->is_new = patch->is_delete = -1;
 	patch->old_mode = patch->new_mode = 0;
 	patch->old_name = patch->new_name = NULL;
-	for (offset = 0; size > 0; offset += len, size -= len, line += len, linenr++) {
+	for (offset = 0; size > 0; offset += len, size -= len, line += len, state->linenr++) {
 		unsigned long nextlen;
 
 		len = linelen(line, size);
@@ -1562,7 +1562,7 @@ static int find_header(struct apply_state *state,
 			if (parse_fragment_header(line, len, &dummy) < 0)
 				continue;
 			die(_("patch fragment without header at line %d: %.*s"),
-			    linenr, (int)len-1, line);
+			    state->linenr, (int)len-1, line);
 		}
 
 		if (size < len + 6)
@@ -1583,13 +1583,13 @@ static int find_header(struct apply_state *state,
 					       "git diff header lacks filename information when removing "
 					       "%d leading pathname components (line %d)",
 					       state->p_value),
-					    state->p_value, linenr);
+					    state->p_value, state->linenr);
 				patch->old_name = xstrdup(patch->def_name);
 				patch->new_name = xstrdup(patch->def_name);
 			}
 			if (!patch->is_delete && !patch->new_name)
 				die("git diff header lacks filename information "
-				    "(line %d)", linenr);
+				    "(line %d)", state->linenr);
 			patch->is_toplevel_relative = 1;
 			*hdrsize = git_hdr_len;
 			return offset;
@@ -1611,7 +1611,7 @@ static int find_header(struct apply_state *state,
 		/* Ok, we'll consider it a patch */
 		parse_traditional_patch(state, line, line+len, patch);
 		*hdrsize = len + nextlen;
-		linenr += 2;
+		state->linenr += 2;
 		return offset;
 	}
 	return -1;
@@ -1646,7 +1646,7 @@ static void check_whitespace(struct apply_state *state,
 {
 	unsigned result = ws_check(line + 1, len - 1, ws_rule);
 
-	record_ws_error(state, result, line + 1, len - 2, linenr);
+	record_ws_error(state, result, line + 1, len - 2, state->linenr);
 }
 
 /*
@@ -1679,11 +1679,11 @@ static int parse_fragment(struct apply_state *state,
 	/* Parse the thing.. */
 	line += len;
 	size -= len;
-	linenr++;
+	state->linenr++;
 	added = deleted = 0;
 	for (offset = len;
 	     0 < size;
-	     offset += len, size -= len, line += len, linenr++) {
+	     offset += len, size -= len, line += len, state->linenr++) {
 		if (!oldlines && !newlines)
 			break;
 		len = linelen(line, size);
@@ -1782,10 +1782,10 @@ static int parse_single_patch(struct apply_state *state,
 		int len;
 
 		fragment = xcalloc(1, sizeof(*fragment));
-		fragment->linenr = linenr;
+		fragment->linenr = state->linenr;
 		len = parse_fragment(state, line, size, patch, fragment);
 		if (len <= 0)
-			die(_("corrupt patch at line %d"), linenr);
+			die(_("corrupt patch at line %d"), state->linenr);
 		fragment->patch = line;
 		fragment->size = len;
 		oldlines += fragment->oldlines;
@@ -1871,7 +1871,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)
@@ -1913,13 +1914,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++;
@@ -1973,11 +1974,14 @@ 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;
 }
 
-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
@@ -1998,15 +2002,15 @@ static int parse_binary(char *buffer, unsigned long size, struct patch *patch)
 	int status;
 	int used, used_1;
 
-	forward = parse_binary_hunk(&buffer, &size, &status, &used);
+	forward = parse_binary_hunk(state, &buffer, &size, &status, &used);
 	if (!forward && !status)
 		/* there has to be one hunk (forward hunk) */
-		return error(_("unrecognized binary patch at line %d"), linenr-1);
+		return error(_("unrecognized binary patch at line %d"), state->linenr-1);
 	if (status)
 		/* otherwise we already gave an error message */
 		return status;
 
-	reverse = parse_binary_hunk(&buffer, &size, &status, &used_1);
+	reverse = parse_binary_hunk(state, &buffer, &size, &status, &used_1);
 	if (reverse)
 		used += used_1;
 	else if (status) {
@@ -2121,8 +2125,8 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
 		if (llen == sizeof(git_binary) - 1 &&
 		    !memcmp(git_binary, buffer + hd, llen)) {
 			int used;
-			linenr++;
-			used = parse_binary(buffer + hd + llen,
+			state->linenr++;
+			used = parse_binary(state, buffer + hd + llen,
 					    size - hd - llen, patch);
 			if (used)
 				patchsize = used + llen;
@@ -2140,7 +2144,7 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
 				int len = strlen(binhdr[i]);
 				if (len < size - hd &&
 				    !memcmp(binhdr[i], buffer + hd, len)) {
-					linenr++;
+					state->linenr++;
 					patch->is_binary = 1;
 					patchsize = llen;
 					break;
@@ -2154,7 +2158,7 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si
 		 */
 		if ((state->apply || state->check) &&
 		    (!patch->is_binary && !metadata_changes(patch)))
-			die(_("patch with only garbage at line %d"), linenr);
+			die(_("patch with only garbage at line %d"), state->linenr);
 	}
 
 	return offset + hdrsize + patchsize;
@@ -4746,6 +4750,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 	state.squelch_whitespace_errors = 5;
 	state.ws_error_action = warn_on_ws_error;
 	state.ws_ignore_action = ignore_ws_none;
+	state.linenr = 1;
 	strbuf_init(&state.root, 0);
 
 	git_apply_config();
-- 
2.8.0.rc1.49.gca61272

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

* [RFC/PATCH 44/48] builtin/apply: move 'fn_table' global into 'struct apply_state'
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (42 preceding siblings ...)
  2016-03-09 17:49 ` [RFC/PATCH 43/48] builtin/apply: move 'linenr' global " Christian Couder
@ 2016-03-09 17:49 ` Christian Couder
  2016-03-09 17:49 ` [RFC/PATCH 45/48] builtin/apply: move 'symlink_changes' " Christian Couder
                   ` (5 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:49 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 3808d60..8fda1bd 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -94,6 +94,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;
@@ -292,13 +298,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;
@@ -3231,14 +3230,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;
 
@@ -3270,7 +3269,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;
 
@@ -3280,7 +3279,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;
 	}
 
@@ -3289,12 +3288,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
@@ -3302,7 +3301,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;
@@ -3323,7 +3322,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;
 
@@ -3331,7 +3332,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;
 
@@ -3400,7 +3401,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);
@@ -3596,7 +3597,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)
@@ -3630,7 +3631,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);
@@ -3876,7 +3877,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
@@ -3954,7 +3955,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,
@@ -4592,7 +4593,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.0.rc1.49.gca61272

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

* [RFC/PATCH 45/48] builtin/apply: move 'symlink_changes' global into 'struct apply_state'
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (43 preceding siblings ...)
  2016-03-09 17:49 ` [RFC/PATCH 44/48] builtin/apply: move 'fn_table' " Christian Couder
@ 2016-03-09 17:49 ` Christian Couder
  2016-03-09 17:49 ` [RFC/PATCH 46/48] builtin/apply: move 'state' init into init_apply_state() Christian Couder
                   ` (4 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:49 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 8fda1bd..66ebc9b 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;
@@ -100,6 +114,8 @@ struct apply_state {
 	 */
 	struct string_list fn_table;
 
+	struct string_list symlink_changes;
+
 	int p_value;
 	int p_value_known;
 	unsigned int p_context;
@@ -3726,52 +3742,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);
 	}
 }
 
@@ -3785,7 +3791,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)
@@ -3954,7 +3960,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.0.rc1.49.gca61272

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

* [RFC/PATCH 46/48] builtin/apply: move 'state' init into init_apply_state()
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (44 preceding siblings ...)
  2016-03-09 17:49 ` [RFC/PATCH 45/48] builtin/apply: move 'symlink_changes' " Christian Couder
@ 2016-03-09 17:49 ` Christian Couder
  2016-03-09 17:49 ` [RFC/PATCH 47/48] builtin/apply: move 'state' check into check_apply_state() Christian Couder
                   ` (3 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:49 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 66ebc9b..1676ce1 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4667,6 +4667,29 @@ static int option_parse_directory(const struct option *opt,
 	return 0;
 }
 
+static void init_apply_state(struct apply_state *state, const char *prefix_)
+{
+	memset(state, 0, sizeof(*state));
+	state->prefix = prefix_;
+	state->prefix_length = state->prefix ? strlen(state->prefix) : 0;
+	state->apply = 1;
+	state->newfd = -1;
+	state->line_termination = '\n';
+	state->p_value = 1;
+	state->p_context = UINT_MAX;
+	state->squelch_whitespace_errors = 5;
+	state->ws_error_action = warn_on_ws_error;
+	state->ws_ignore_action = ignore_ws_none;
+	state->linenr = 1;
+	strbuf_init(&state->root, 0);
+
+	git_apply_config();
+	if (apply_default_whitespace)
+		parse_whitespace_option(state, apply_default_whitespace);
+	if (apply_default_ignorewhitespace)
+		parse_ignorewhitespace_option(state, apply_default_ignorewhitespace);
+}
+
 int cmd_apply(int argc, const char **argv, const char *prefix_)
 {
 	int i;
@@ -4746,25 +4769,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)
 		OPT_END()
 	};
 
-	memset(&state, 0, sizeof(state));
-	state.prefix = prefix_;
-	state.prefix_length = state.prefix ? strlen(state.prefix) : 0;
-	state.apply = 1;
-	state.newfd = -1;
-	state.line_termination = '\n';
-	state.p_value = 1;
-	state.p_context = UINT_MAX;
-	state.squelch_whitespace_errors = 5;
-	state.ws_error_action = warn_on_ws_error;
-	state.ws_ignore_action = ignore_ws_none;
-	state.linenr = 1;
-	strbuf_init(&state.root, 0);
-
-	git_apply_config();
-	if (apply_default_whitespace)
-		parse_whitespace_option(&state, apply_default_whitespace);
-	if (apply_default_ignorewhitespace)
-		parse_ignorewhitespace_option(&state, apply_default_ignorewhitespace);
+	init_apply_state(&state, prefix_);
 
 	argc = parse_options(argc, argv, state.prefix, builtin_apply_options,
 			apply_usage, 0);
-- 
2.8.0.rc1.49.gca61272

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

* [RFC/PATCH 47/48] builtin/apply: move 'state' check into check_apply_state()
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (45 preceding siblings ...)
  2016-03-09 17:49 ` [RFC/PATCH 46/48] builtin/apply: move 'state' init into init_apply_state() Christian Couder
@ 2016-03-09 17:49 ` Christian Couder
  2016-03-09 17:49 ` [RFC/PATCH 48/48] builtin/apply: move applying patches into apply_all_patches() Christian Couder
                   ` (2 subsequent siblings)
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:49 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 1676ce1..3f76469 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4690,11 +4690,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;
@@ -4774,28 +4801,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.0.rc1.49.gca61272

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

* [RFC/PATCH 48/48] builtin/apply: move applying patches into apply_all_patches()
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (46 preceding siblings ...)
  2016-03-09 17:49 ` [RFC/PATCH 47/48] builtin/apply: move 'state' check into check_apply_state() Christian Couder
@ 2016-03-09 17:49 ` Christian Couder
  2016-03-09 18:14 ` [RFC/PATCH 00/48] Libifying git apply Junio C Hamano
  2016-03-10  9:26 ` Duy Nguyen
  49 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-09 17:49 UTC (permalink / raw)
  To: git
  Cc: Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Stefan Beller,
	Matthieu Moy, Christian Couder

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

diff --git a/builtin/apply.c b/builtin/apply.c
index 3f76469..9611b1a 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -4718,13 +4718,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, &state->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[] = {
@@ -4803,61 +4869,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, &state.lock_file, COMMIT_LOCK))
-			die(_("Unable to write new index file"));
-	}
-
-	return !!errs;
+	return apply_all_patches(&state, argc, argv, options);
 }
-- 
2.8.0.rc1.49.gca61272

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

* Re: [RFC/PATCH 00/48] Libifying git apply
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (47 preceding siblings ...)
  2016-03-09 17:49 ` [RFC/PATCH 48/48] builtin/apply: move applying patches into apply_all_patches() Christian Couder
@ 2016-03-09 18:14 ` Junio C Hamano
  2016-03-11 15:57   ` Christian Couder
  2016-03-10  9:26 ` Duy Nguyen
  49 siblings, 1 reply; 58+ messages in thread
From: Junio C Hamano @ 2016-03-09 18:14 UTC (permalink / raw)
  To: Christian Couder
  Cc: git, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

> One point I'd especially welcome feedback about is the fact that there
> are many boolean options that are using OPT_BOOL(...), so they use an
> int. And there are a few others that are using OPT_BIT(...), so they
> use just a bit. I wonder if it is worth it to try to be consistent,
> and maybe also to try to save some memory.
>
> Related to this, some of the variables for these options have not been
> moved into the "apply_state" structure, because they are not global to
> the file, but maybe for consistency they should be.

These might be worthy clean-ups but that is only if they are done
after we make sure conversion proper is done faithfully to the
original, i.e. without introducing unnecessary bugs.  I'd advise
against doing them before the libification is done.

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

* Re: [RFC/PATCH 04/48] builtin/apply: extract line_by_line_fuzzy_match() from match_fragment()
  2016-03-09 17:48 ` [RFC/PATCH 04/48] builtin/apply: extract line_by_line_fuzzy_match() from match_fragment() Christian Couder
@ 2016-03-09 22:55   ` Stefan Beller
  2016-03-10  8:36     ` Christian Couder
  0 siblings, 1 reply; 58+ messages in thread
From: Stefan Beller @ 2016-03-09 22:55 UTC (permalink / raw)
  To: Christian Couder
  Cc: git, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Matthieu Moy,
	Christian Couder

On Wed, Mar 9, 2016 at 9:48 AM, Christian Couder
<christian.couder@gmail.com> wrote:

Some words in the commit message would be nice here as this is one of
the patches,
which isn't "obviously" a good thing to to. This comment also applies
to "builtin/apply:
introduce 'struct apply_state' to start libifying" where you lay out
the plan for the next
~40 patches.

I have only skimmed quickly over the other patches.


> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
> ---
>  builtin/apply.c | 125 ++++++++++++++++++++++++++++++++------------------------
>  1 file changed, 71 insertions(+), 54 deletions(-)
>
> diff --git a/builtin/apply.c b/builtin/apply.c
> index c99c859..7d7a8ab 100644
> --- a/builtin/apply.c
> +++ b/builtin/apply.c
> @@ -2244,6 +2244,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,
> @@ -2253,7 +2321,7 @@ static int match_fragment(struct image *img,
>                           int match_beginning, int match_end)
>  {
>         int i;
> -       char *fixed_buf, *buf, *orig, *target;
> +       char *fixed_buf, *orig, *target;
>         struct strbuf fixed;
>         size_t fixed_len, postlen;
>         int preimage_limit;
> @@ -2314,6 +2382,7 @@ static int match_fragment(struct image *img,
>                  * There must be one non-blank context line that match
>                  * a line before the end of img.
>                  */
> +               char *buf;
>                 char *buf_end;
>
>                 buf = preimage->buf;
> @@ -2334,59 +2403,7 @@ static int match_fragment(struct image *img,
>          * 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;
> +               return line_by_line_fuzzy_match(img, preimage, postimage, try, try_lno, preimage_limit);
>         }
>
>         if (ws_error_action != correct_ws_error)
> --
> 2.8.0.rc1.49.gca61272
>

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

* Re: [RFC/PATCH 01/48] builtin/apply: avoid parameter shadowing 'p_value' global
  2016-03-09 17:48 ` [RFC/PATCH 01/48] builtin/apply: avoid parameter shadowing 'p_value' global Christian Couder
@ 2016-03-09 23:27   ` Junio C Hamano
  2016-03-10  0:54     ` Duy Nguyen
  0 siblings, 1 reply; 58+ messages in thread
From: Junio C Hamano @ 2016-03-09 23:27 UTC (permalink / raw)
  To: Christian Couder
  Cc: git, Jeff King, Ævar Arnfjörð Bjarmason,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

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

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

To be honest, I have to say that I do not like this change from
readability's point of view.  Once the global p_value becomes a
field in a "apply state" structure, functions will get a pointer to
the struct as a parameter, reference to it would be spelled as
"astate->p_value", while a local variable or a parameter would be
"p_value" and there is no shadowing issue.

Also, you wouldn't be able to catch a misconversion in this patch
only from the patch text, if the conversion is done this way, would
you?  The patch may change the parameter p_value to p_v, and change
two references to p_value also to p_v, but it may leave another
reference to p_value that originally referred to the parameter
as-is, making it refer to the global, which would be a new bug
introduced by this conversion patch, but that remaining reference
would not show up in the patch.

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

* Re: [RFC/PATCH 01/48] builtin/apply: avoid parameter shadowing 'p_value' global
  2016-03-09 23:27   ` Junio C Hamano
@ 2016-03-10  0:54     ` Duy Nguyen
  2016-03-10 10:45       ` Christian Couder
  0 siblings, 1 reply; 58+ messages in thread
From: Duy Nguyen @ 2016-03-10  0:54 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: Christian Couder, Git Mailing List, Jeff King,
	Ævar Arnfjörð,
	Karsten Blees, Johannes Schindelin, Stefan Beller, Matthieu Moy,
	Christian Couder

On Thu, Mar 10, 2016 at 6:27 AM, Junio C Hamano <gitster@pobox.com> wrote:
> Christian Couder <christian.couder@gmail.com> writes:
>
>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
>> ---
>>  builtin/apply.c | 24 ++++++++++++------------
>>  1 file changed, 12 insertions(+), 12 deletions(-)
>
> To be honest, I have to say that I do not like this change from
> readability's point of view.  Once the global p_value becomes a
> field in a "apply state" structure, functions will get a pointer to
> the struct as a parameter, reference to it would be spelled as
> "astate->p_value", while a local variable or a parameter would be
> "p_value" and there is no shadowing issue.

We could revert these local var rename patches at the end of the
conversion, I think.

> Also, you wouldn't be able to catch a misconversion in this patch
> only from the patch text, if the conversion is done this way, would
> you?  The patch may change the parameter p_value to p_v, and change
> two references to p_value also to p_v, but it may leave another
> reference to p_value that originally referred to the parameter
> as-is, making it refer to the global, which would be a new bug
> introduced by this conversion patch, but that remaining reference
> would not show up in the patch.

Yeah, I wish we have a semantic parser that can help with this sort of
refactoring (can sparse do that?). Once we get AST tree, it should be
easy to identify variable scope. Without it, maybe we can still rely
on the compiler by renaming _both_ global and local vars to something
else. That forces us (both patch writer and reviewers) to catch and
examine every reference.
-- 
Duy

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

* Re: [RFC/PATCH 04/48] builtin/apply: extract line_by_line_fuzzy_match() from match_fragment()
  2016-03-09 22:55   ` Stefan Beller
@ 2016-03-10  8:36     ` Christian Couder
  0 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-10  8:36 UTC (permalink / raw)
  To: Stefan Beller
  Cc: git, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Nguyen Thai Ngoc Duy, Johannes Schindelin, Matthieu Moy,
	Christian Couder

On Wed, Mar 9, 2016 at 11:55 PM, Stefan Beller <sbeller@google.com> wrote:
> On Wed, Mar 9, 2016 at 9:48 AM, Christian Couder
> <christian.couder@gmail.com> wrote:
>
> Some words in the commit message would be nice here as this is one of
> the patches,
> which isn't "obviously" a good thing to to. This comment also applies
> to "builtin/apply:
> introduce 'struct apply_state' to start libifying" where you lay out
> the plan for the next
> ~40 patches.

Ok, I will try to explain better at least those 2 patches.

Thanks,
Christian.

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

* Re: [RFC/PATCH 00/48] Libifying git apply
  2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
                   ` (48 preceding siblings ...)
  2016-03-09 18:14 ` [RFC/PATCH 00/48] Libifying git apply Junio C Hamano
@ 2016-03-10  9:26 ` Duy Nguyen
  2016-03-11 17:34   ` Christian Couder
  49 siblings, 1 reply; 58+ messages in thread
From: Duy Nguyen @ 2016-03-10  9:26 UTC (permalink / raw)
  To: Christian Couder
  Cc: Git Mailing List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Johannes Schindelin, Stefan Beller, Matthieu Moy,
	Christian Couder

On Thu, Mar 10, 2016 at 12:48 AM, Christian Couder
<christian.couder@gmail.com> wrote:
> This is a patch series about libifying "git apply" functionality, to
> be able to use this functionality in "git am" without spawning new
> processes. This should make "git am" and "git rebase" significantly
> faster.
>
> This has been discussed in the following thread:
>
> http://thread.gmane.org/gmane.comp.version-control.git/287236/
>
> This RFC patch series for now just gets rid of the global variables
> and refactors the code around a bit.
>
> As suggested by Junio the global variables in builtin/apply.c are just
> thrown into a single "apply_state" structure that is passed around the
> callchain. A new parameter called "state" that is a pointer to the
> "apply_state" structure comes at the beginning of the helper functions
> that need it.
>
> Before I make further changes to handle erroneous input and make the
> libified functions not die() and properly clean things up, I'd be
> happy to get some feedback.

I didn't review individual patches. Instead I redid the whole thing
(in a slightly different way) and compared my end result to yours. In
general it looks good but..

1) I think you should focus the series on moving global vars to struct
apply_state only. You can save code move patches for the later phase.
Easier to review.

2) Given that there are only four local variables shadowing global
ones, p_value, linenr and options, I think it's ok to drop 1/48 and
2/48 and keep the good old names. You just need to mention about them
and what function they are declared in in the relevant "move global
..." patch so the reviewer is aware of it.

3) Moving lock_file to struct apply_state, then putting the whole
struct on stack, could be problematic. I believe there's a global
linked list keeping references of all lock_file variables until the
end of time, so we can't destroy lock_file/apply_state until we are
certain it's safe to do so. We could simply leave lock_file as a
global var for now (with a note in struct apply_state so we don't
forget). We can always do fancy stuff like malloc() later on if we
need to.

4) I noticed on the interdiff that there are translatable strings in
this file but not marked as such. You're going to spend lots of time
reading this file and are in a very good positioin to determine if a
string should be translated (i.e. wrap _() around it) or not. So maybe
you can have a look at this in the next series too. It's absolutely ok
if you say "no" here.

5) Minor detail, but we can rename prefix_ to prefix in
init_apply_state(). There trailing underscore was there in cmd_apply
because there's the global "prefix", but it's gone now.

Looking forward to seeing you double, triple git-rebase's speed.
-- 
Duy

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

* Re: [RFC/PATCH 01/48] builtin/apply: avoid parameter shadowing 'p_value' global
  2016-03-10  0:54     ` Duy Nguyen
@ 2016-03-10 10:45       ` Christian Couder
  0 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-10 10:45 UTC (permalink / raw)
  To: Duy Nguyen
  Cc: Junio C Hamano, Git Mailing List, Jeff King,
	Ævar Arnfjörð,
	Karsten Blees, Johannes Schindelin, Stefan Beller, Matthieu Moy,
	Christian Couder

On Thu, Mar 10, 2016 at 1:54 AM, Duy Nguyen <pclouds@gmail.com> wrote:
> On Thu, Mar 10, 2016 at 6:27 AM, Junio C Hamano <gitster@pobox.com> wrote:
>> Christian Couder <christian.couder@gmail.com> writes:
>>
>>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
>>> ---
>>>  builtin/apply.c | 24 ++++++++++++------------
>>>  1 file changed, 12 insertions(+), 12 deletions(-)
>>
>> To be honest, I have to say that I do not like this change from
>> readability's point of view.  Once the global p_value becomes a
>> field in a "apply state" structure, functions will get a pointer to
>> the struct as a parameter, reference to it would be spelled as
>> "astate->p_value", while a local variable or a parameter would be
>> "p_value" and there is no shadowing issue.
>
> We could revert these local var rename patches at the end of the
> conversion, I think.
>
>> Also, you wouldn't be able to catch a misconversion in this patch
>> only from the patch text, if the conversion is done this way, would
>> you?  The patch may change the parameter p_value to p_v, and change
>> two references to p_value also to p_v, but it may leave another
>> reference to p_value that originally referred to the parameter
>> as-is, making it refer to the global, which would be a new bug
>> introduced by this conversion patch, but that remaining reference
>> would not show up in the patch.
>
> Yeah, I wish we have a semantic parser that can help with this sort of
> refactoring (can sparse do that?). Once we get AST tree, it should be
> easy to identify variable scope. Without it, maybe we can still rely
> on the compiler by renaming _both_ global and local vars to something
> else. That forces us (both patch writer and reviewers) to catch and
> examine every reference.

I agree that the change in the patch is not very nice.
I can do one of the following:

1) do as Junio suggests, just discard this change and wait until
p_value is moved into apply_state for the problem to be solved; a
small downside of this is that if you compile with -Wshadow after each
change you will get some warnings until p_value is moved into
apply_state; I could move the patch to move p_value at the beginning
of the series but it is a bit involved and easier to review if it is
done towards the end,

2) revert these local var rename patches at the end of the conversion,
as Duy suggests

3) instead of renaming the local "p_value" variable, rename the global
"p_value" to maybe "state_p_value", so that when I move it into
apply_state I just need to s/state_p_value/state->p_value/.

I'd rather do 3) than 1) or 2) but I am also ok with 1) and 2).

Thanks,
Christian.

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

* Re: [RFC/PATCH 00/48] Libifying git apply
  2016-03-09 18:14 ` [RFC/PATCH 00/48] Libifying git apply Junio C Hamano
@ 2016-03-11 15:57   ` Christian Couder
  0 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-11 15:57 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Jeff King, Ævar Arnfjörð,
	Karsten Blees, Nguyen Thai Ngoc Duy, Johannes Schindelin,
	Stefan Beller, Matthieu Moy, Christian Couder

On Wed, Mar 9, 2016 at 7:14 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Christian Couder <christian.couder@gmail.com> writes:
>
>> One point I'd especially welcome feedback about is the fact that there
>> are many boolean options that are using OPT_BOOL(...), so they use an
>> int. And there are a few others that are using OPT_BIT(...), so they
>> use just a bit. I wonder if it is worth it to try to be consistent,
>> and maybe also to try to save some memory.
>>
>> Related to this, some of the variables for these options have not been
>> moved into the "apply_state" structure, because they are not global to
>> the file, but maybe for consistency they should be.
>
> These might be worthy clean-ups but that is only if they are done
> after we make sure conversion proper is done faithfully to the
> original, i.e. without introducing unnecessary bugs.  I'd advise
> against doing them before the libification is done.

Ok, I will try to avoid those kind of clean-ups.

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

* Re: [RFC/PATCH 00/48] Libifying git apply
  2016-03-10  9:26 ` Duy Nguyen
@ 2016-03-11 17:34   ` Christian Couder
  0 siblings, 0 replies; 58+ messages in thread
From: Christian Couder @ 2016-03-11 17:34 UTC (permalink / raw)
  To: Duy Nguyen
  Cc: Git Mailing List, Junio C Hamano, Jeff King,
	Ævar Arnfjörð Bjarmason, Karsten Blees,
	Johannes Schindelin, Stefan Beller, Matthieu Moy,
	Christian Couder

On Thu, Mar 10, 2016 at 10:26 AM, Duy Nguyen <pclouds@gmail.com> wrote:
> On Thu, Mar 10, 2016 at 12:48 AM, Christian Couder
> <christian.couder@gmail.com> wrote:
>> This is a patch series about libifying "git apply" functionality, to
>> be able to use this functionality in "git am" without spawning new
>> processes. This should make "git am" and "git rebase" significantly
>> faster.
>>
>> This has been discussed in the following thread:
>>
>> http://thread.gmane.org/gmane.comp.version-control.git/287236/
>>
>> This RFC patch series for now just gets rid of the global variables
>> and refactors the code around a bit.
>>
>> As suggested by Junio the global variables in builtin/apply.c are just
>> thrown into a single "apply_state" structure that is passed around the
>> callchain. A new parameter called "state" that is a pointer to the
>> "apply_state" structure comes at the beginning of the helper functions
>> that need it.
>>
>> Before I make further changes to handle erroneous input and make the
>> libified functions not die() and properly clean things up, I'd be
>> happy to get some feedback.
>
> I didn't review individual patches. Instead I redid the whole thing
> (in a slightly different way) and compared my end result to yours. In
> general it looks good but..
>
> 1) I think you should focus the series on moving global vars to struct
> apply_state only. You can save code move patches for the later phase.
> Easier to review.

I am not sure what you mean by "the later phase", but I tend to agree
and that's mostly what I did.
Most of the code move are at the end of the series so after the global
variable related changes.
There are a few exceptions because in a few cases it was difficult to
understand the code without a refactoring.
In such cases I think the refactoring can also actually help the reviewer.

> 2) Given that there are only four local variables shadowing global
> ones, p_value, linenr and options, I think it's ok to drop 1/48 and
> 2/48 and keep the good old names. You just need to mention about them
> and what function they are declared in in the relevant "move global
> ..." patch so the reviewer is aware of it.

About 1/48 (builtin/apply: avoid parameter shadowing 'p_value') this
is discussed in its own thread.
In 2/48 (builtin/apply: avoid parameter shadowing 'linenr'), I change
the "good old name" only twice in a small function, and I don't think
keeping the old name is a big deal there.

> 3) Moving lock_file to struct apply_state, then putting the whole
> struct on stack, could be problematic. I believe there's a global
> linked list keeping references of all lock_file variables until the
> end of time, so we can't destroy lock_file/apply_state until we are
> certain it's safe to do so. We could simply leave lock_file as a
> global var for now (with a note in struct apply_state so we don't
> forget). We can always do fancy stuff like malloc() later on if we
> need to.

Thanks for this information. Though I prefer to do the fancy stuff
right away, that is something like:

diff --git a/builtin/apply.c b/builtin/apply.c
index 4f57bce..6029869 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;
@@ -4517,8 +4523,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)

@@ -4566,8 +4570,10 @@ static int apply_patch(struct apply_state *state,
                state->apply = 0;

        state->update_index = state->check_index && state->apply;
-       if (state->update_index && newfd < 0)
-               newfd = hold_locked_index(&lock_file, 1);
+       if (state->update_index && newfd < 0) {
+               state->lock_file = xcalloc(1, sizeof(struct lock_file));
+               newfd = hold_locked_index(state->lock_file, 1);
+       }

        if (state->check_index) {
                if (read_cache() < 0)
@@ -4780,7 +4786,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"));
        }

It makes it possible to remove the 'newfd' variable as it is only used
to check if hold_locked_index() has already been called and now
"state->lock_file == NULL" can be used to do that.

By the way I wonder if I should also change "static struct lock_file
lock" in build_fake_ancestor().

> 4) I noticed on the interdiff that there are translatable strings in
> this file but not marked as such. You're going to spend lots of time
> reading this file and are in a very good positioin to determine if a
> string should be translated (i.e. wrap _() around it) or not. So maybe
> you can have a look at this in the next series too. It's absolutely ok
> if you say "no" here.

I will see if I can do that towards the end of the series, but this is
not at all on my radar for now.

> 5) Minor detail, but we can rename prefix_ to prefix in
> init_apply_state(). There trailing underscore was there in cmd_apply
> because there's the global "prefix", but it's gone now.

Yeah, I think this is worth doing, so it is done in my current series:

https://github.com/chriscool/git/commits/libify-apply21

> Looking forward to seeing you double, triple git-rebase's speed.

Thanks for your reviews and comments,
Christian.

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

end of thread, other threads:[~2016-03-11 17:35 UTC | newest]

Thread overview: 58+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-03-09 17:48 [RFC/PATCH 00/48] Libifying git apply Christian Couder
2016-03-09 17:48 ` [RFC/PATCH 01/48] builtin/apply: avoid parameter shadowing 'p_value' global Christian Couder
2016-03-09 23:27   ` Junio C Hamano
2016-03-10  0:54     ` Duy Nguyen
2016-03-10 10:45       ` Christian Couder
2016-03-09 17:48 ` [RFC/PATCH 02/48] builtin/apply: avoid parameter shadowing 'linenr' global Christian Couder
2016-03-09 17:48 ` [RFC/PATCH 03/48] builtin/apply: avoid local variable shadowing 'len' parameter Christian Couder
2016-03-09 17:48 ` [RFC/PATCH 04/48] builtin/apply: extract line_by_line_fuzzy_match() from match_fragment() Christian Couder
2016-03-09 22:55   ` Stefan Beller
2016-03-10  8:36     ` Christian Couder
2016-03-09 17:48 ` [RFC/PATCH 05/48] builtin/apply: move 'options' variable into cmd_apply() Christian Couder
2016-03-09 17:48 ` [RFC/PATCH 06/48] builtin/apply: introduce 'struct apply_state' to start libifying Christian Couder
2016-03-09 17:48 ` [RFC/PATCH 07/48] builtin/apply: move 'newfd' global into 'struct apply_state' Christian Couder
2016-03-09 17:48 ` [RFC/PATCH 08/48] builtin/apply: move 'unidiff_zero' " Christian Couder
2016-03-09 17:48 ` [RFC/PATCH 09/48] builtin/apply: move 'check' " Christian Couder
2016-03-09 17:48 ` [RFC/PATCH 10/48] builtin/apply: move 'check_index' " Christian Couder
2016-03-09 17:48 ` [RFC/PATCH 11/48] builtin/apply: move 'apply_in_reverse' " Christian Couder
2016-03-09 17:48 ` [RFC/PATCH 12/48] builtin/apply: move 'apply_with_reject' " Christian Couder
2016-03-09 17:48 ` [RFC/PATCH 13/48] builtin/apply: move 'apply_verbosely' " Christian Couder
2016-03-09 17:48 ` [RFC/PATCH 14/48] builtin/apply: move 'update_index' " Christian Couder
2016-03-09 17:48 ` [RFC/PATCH 15/48] builtin/apply: move 'allow_overlap' " Christian Couder
2016-03-09 17:48 ` [RFC/PATCH 16/48] builtin/apply: move 'cached' " Christian Couder
2016-03-09 17:48 ` [RFC/PATCH 17/48] builtin/apply: move 'diffstat' " Christian Couder
2016-03-09 17:48 ` [RFC/PATCH 18/48] builtin/apply: move 'numstat' " Christian Couder
2016-03-09 17:48 ` [RFC/PATCH 19/48] builtin/apply: move 'summary' " Christian Couder
2016-03-09 17:48 ` [RFC/PATCH 20/48] builtin/apply: move 'threeway' " Christian Couder
2016-03-09 17:48 ` [RFC/PATCH 21/48] builtin/apply: move 'no-add' " Christian Couder
2016-03-09 17:48 ` [RFC/PATCH 22/48] builtin/apply: move 'unsafe_paths' " Christian Couder
2016-03-09 17:48 ` [RFC/PATCH 23/48] builtin/apply: move 'line_termination' " Christian Couder
2016-03-09 17:48 ` [RFC/PATCH 24/48] builtin/apply: move 'fake_ancestor' " Christian Couder
2016-03-09 17:48 ` [RFC/PATCH 25/48] builtin/apply: move 'p_context' " Christian Couder
2016-03-09 17:48 ` [RFC/PATCH 26/48] builtin/apply: move 'apply' " Christian Couder
2016-03-09 17:48 ` [RFC/PATCH 27/48] builtin/apply: move 'read_stdin' global into cmd_apply() Christian Couder
2016-03-09 17:48 ` [RFC/PATCH 28/48] builtin/apply: move 'lock_file' global into 'struct apply_state' Christian Couder
2016-03-09 17:48 ` [RFC/PATCH 29/48] builtin/apply: move 'patch_input_file' " Christian Couder
2016-03-09 17:48 ` [RFC/PATCH 30/48] builtin/apply: move 'limit_by_name' " Christian Couder
2016-03-09 17:48 ` [RFC/PATCH 31/48] builtin/apply: move 'has_include' " Christian Couder
2016-03-09 17:49 ` [RFC/PATCH 32/48] builtin/apply: move 'p_value' " Christian Couder
2016-03-09 17:49 ` [RFC/PATCH 33/48] builtin/apply: move 'p_value_known' " Christian Couder
2016-03-09 17:49 ` [RFC/PATCH 34/48] builtin/apply: move 'root' " Christian Couder
2016-03-09 17:49 ` [RFC/PATCH 35/48] builtin/apply: move 'whitespace_error' " Christian Couder
2016-03-09 17:49 ` [RFC/PATCH 36/48] builtin/apply: move 'whitespace_option' " Christian Couder
2016-03-09 17:49 ` [RFC/PATCH 37/48] builtin/apply: remove whitespace_option arg from set_default_whitespace_mode() Christian Couder
2016-03-09 17:49 ` [RFC/PATCH 38/48] builtin/apply: move 'squelch_whitespace_errors' into 'struct apply_state' Christian Couder
2016-03-09 17:49 ` [RFC/PATCH 39/48] builtin/apply: move 'applied_after_fixing_ws' " Christian Couder
2016-03-09 17:49 ` [RFC/PATCH 40/48] builtin/apply: move 'ws_error_action' " Christian Couder
2016-03-09 17:49 ` [RFC/PATCH 41/48] builtin/apply: move 'ws_ignore_action' " Christian Couder
2016-03-09 17:49 ` [RFC/PATCH 42/48] builtin/apply: move 'max_change' and 'max_len' " Christian Couder
2016-03-09 17:49 ` [RFC/PATCH 43/48] builtin/apply: move 'linenr' global " Christian Couder
2016-03-09 17:49 ` [RFC/PATCH 44/48] builtin/apply: move 'fn_table' " Christian Couder
2016-03-09 17:49 ` [RFC/PATCH 45/48] builtin/apply: move 'symlink_changes' " Christian Couder
2016-03-09 17:49 ` [RFC/PATCH 46/48] builtin/apply: move 'state' init into init_apply_state() Christian Couder
2016-03-09 17:49 ` [RFC/PATCH 47/48] builtin/apply: move 'state' check into check_apply_state() Christian Couder
2016-03-09 17:49 ` [RFC/PATCH 48/48] builtin/apply: move applying patches into apply_all_patches() Christian Couder
2016-03-09 18:14 ` [RFC/PATCH 00/48] Libifying git apply Junio C Hamano
2016-03-11 15:57   ` Christian Couder
2016-03-10  9:26 ` Duy Nguyen
2016-03-11 17:34   ` Christian Couder

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