{"thread":{"id":"42433","subject":"[PATCH v3 02/49] builtin/apply: avoid parameter shadowing 'p_value' global","startedAt":"2016-05-24T08:10:37Z","lastAt":"2016-06-03T09:42:13Z","messageCount":53,"participants":["Christian Couder","Junio C Hamano"],"isPatch":true,"patchVersion":3,"patchTotal":49},"messages":[{"id":"287350","messageId":"20160524081126.16973-1-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":null,"subject":"[PATCH v3 00/49] libify apply and use lib in am, part 1","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:10:37Z","receivedAt":"2016-05-24T08:10:37Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Goal\n~~~~\n\nThis is a patch series about libifying `git apply` functionality, and\nusing this libified functionality in `git am`, so that no 'git apply'\nprocess is spawn anymore. This makes `git am` significantly faster, so\n`git rebase`, when it uses the am backend, is also significantly\nfaster.\n\nPrevious discussions and patches series\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nThis has initially been discussed in the following thread:\n\n  http://thread.gmane.org/gmane.comp.version-control.git/287236/\n\nThen the following patch series were sent:\n\nRFC: http://thread.gmane.org/gmane.comp.version-control.git/288489/\nv1: http://thread.gmane.org/gmane.comp.version-control.git/292324/\nv2: http://thread.gmane.org/gmane.comp.version-control.git/294248/\n\nHighlevel view of the patches in the series\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nThis new patch series is built on top of the above previous work.\n\nIt contains only patches 01/94 to 50/94 from v2, as v2 contains too\nmany patches and it was decided to split it.\n\nThe changes since v2 are the following:\n\n    - Patch 48/94 (builtin/apply: rename 'prefix_' parameter to\n      'prefix') was squashed into 09/94 (builtin/apply: move 'state'\n      init into init_apply_state()), as suggested by Junio.\n\n    - Fields in 'struct apply_state' have been reorganized, as\n      suggested by Junio.\n\n    - clear_apply_state() has been added to free 'struct apply_state'\n      resources.\n\nGeneral comments\n~~~~~~~~~~~~~~~~\n\nSorry if this patch series is still long despite being splitted.\n\nI will send a diff between this version and the 50 first patches of v2\nsoon as a reply to this email.\n\nThe benefits are not just related to not creating new processes. When\n`git am` launched a `git apply` process, this new process had to read\nthe index from disk. Then after the `git apply`process had terminated,\n`git am` dropped its index and read the index from disk to get the\nindex that had been modified by the `git apply`process. This was\ninefficient and also prevented the split-index mechanism to provide\nmany performance benefits.\n\nUsing this series as rebase material, Duy explains it like this:\n\n > Without the series, the picture is not so surprising. We run git-apply\n > 80+ times, each consists of this sequence\n >\n > read index\n > write index (cache tree updates only)\n > read index again\n > optionally initialize name hash (when new entries are added, I guess)\n > read packed-refs\n > write index\n >\n > With this series, we run a single git-apply which does\n >\n > read index (and sharedindex too if in split-index mode)\n > initialize name hash\n > write index 80+ times\n\n(See: http://thread.gmane.org/gmane.comp.version-control.git/292324/focus=292460)\n\nLinks\n~~~~~\n\nThis patch series is available here:\n\nv3: https://github.com/chriscool/git/commits/libify-apply61\nv2: https://github.com/chriscool/git/commits/libify-apply-use-in-am54\nv1: https://github.com/chriscool/git/commits/libify-apply-use-in-am25 \n\nPerformance numbers\n~~~~~~~~~~~~~~~~~~~\n\nFor now only tests on Linux have been performed on v1 around mid\nApril. It could be interesting to test on other platforms especially\nWindows and perhaps OSX too.\n\n  - Ævar did a huge many-hundred commit rebase on the kernel with\n    untracked cache.\n\ncommand: git rebase --onto 1993b17 52bef0c 29dde7c\n\nVanilla \"next\" without split index:                1m54.953s\nVanilla \"next\" with split index:                   1m22.476s\nThis series on top of \"next\" without split index:  1m12.034s\nThis series on top of \"next\" with split index:     0m15.678s\n\nÆvar used his Debian laptop with SSD.\n\n  - I tested rebasing 13 commits in Booking.com's monorepo on a Red\n    Hat 6.5 server with split-index and GIT_TRACE_PERFORMANCE=1.\n\nWith Git v2.8.0, the rebase took 6.375888383 s, with the git am\ncommand launched by the rebase command taking 3.705677431 s.\n\nWith this series on top of next, the rebase took 3.044529494 s, with\nthe git am command launched by the rebase command taking 0.583521168\ns.\n\n\nChristian Couder (49):\n  builtin/apply: make gitdiff_verify_name() return void\n  builtin/apply: avoid parameter shadowing 'p_value' global\n  builtin/apply: avoid parameter shadowing 'linenr' global\n  builtin/apply: avoid local variable shadowing 'len' parameter\n  builtin/apply: extract line_by_line_fuzzy_match() from\n    match_fragment()\n  builtin/apply: move 'options' variable into cmd_apply()\n  builtin/apply: move 'read_stdin' global into cmd_apply()\n  builtin/apply: introduce 'struct apply_state' to start libifying\n  builtin/apply: move 'state' init into init_apply_state()\n  builtin/apply: move 'unidiff_zero' global into 'struct apply_state'\n  builtin/apply: move 'check' global into 'struct apply_state'\n  builtin/apply: move 'check_index' global into 'struct apply_state'\n  builtin/apply: move 'apply_in_reverse' global into 'struct\n    apply_state'\n  builtin/apply: move 'apply_with_reject' global into 'struct\n    apply_state'\n  builtin/apply: move 'apply_verbosely' global into 'struct apply_state'\n  builtin/apply: move 'update_index' global into 'struct apply_state'\n  builtin/apply: move 'allow_overlap' global into 'struct apply_state'\n  builtin/apply: move 'cached' global into 'struct apply_state'\n  builtin/apply: move 'diffstat' global into 'struct apply_state'\n  builtin/apply: move 'numstat' global into 'struct apply_state'\n  builtin/apply: move 'summary' global into 'struct apply_state'\n  builtin/apply: move 'threeway' global into 'struct apply_state'\n  builtin/apply: move 'no_add' global into 'struct apply_state'\n  builtin/apply: move 'unsafe_paths' global into 'struct apply_state'\n  builtin/apply: move 'line_termination' global into 'struct\n    apply_state'\n  builtin/apply: move 'fake_ancestor' global into 'struct apply_state'\n  builtin/apply: move 'p_context' global into 'struct apply_state'\n  builtin/apply: move 'apply' global into 'struct apply_state'\n  builtin/apply: move 'patch_input_file' global into 'struct\n    apply_state'\n  builtin/apply: move 'limit_by_name' global into 'struct apply_state'\n  builtin/apply: move 'has_include' global into 'struct apply_state'\n  builtin/apply: move 'p_value' global into 'struct apply_state'\n  builtin/apply: move 'p_value_known' global into 'struct apply_state'\n  builtin/apply: move 'root' global into 'struct apply_state'\n  builtin/apply: move 'whitespace_error' global into 'struct\n    apply_state'\n  builtin/apply: move 'whitespace_option' into 'struct apply_state'\n  builtin/apply: remove whitespace_option arg from\n    set_default_whitespace_mode()\n  builtin/apply: move 'squelch_whitespace_errors' into 'struct\n    apply_state'\n  builtin/apply: move 'applied_after_fixing_ws' into 'struct\n    apply_state'\n  builtin/apply: move 'ws_error_action' into 'struct apply_state'\n  builtin/apply: move 'ws_ignore_action' into 'struct apply_state'\n  builtin/apply: move 'max_change' and 'max_len' into 'struct\n    apply_state'\n  builtin/apply: move 'state_linenr' global into 'struct apply_state'\n  builtin/apply: move 'fn_table' global into 'struct apply_state'\n  builtin/apply: move 'symlink_changes' global into 'struct apply_state'\n  builtin/apply: move 'state' check into check_apply_state()\n  builtin/apply: move applying patches into apply_all_patches()\n  builtin/apply: move 'lock_file' global into 'struct apply_state'\n  builtin/apply: move 'newfd' global into 'struct apply_state'\n\n builtin/apply.c | 1432 +++++++++++++++++++++++++++++++------------------------\n 1 file changed, 821 insertions(+), 611 deletions(-)\n\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287349","messageId":"20160524081126.16973-2-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 01/49] builtin/apply: make gitdiff_verify_name() return void","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:10:38Z","receivedAt":"2016-05-24T08:10:38Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"As the value returned by gitdiff_verify_name() is put into the\nsame variable that is passed as a parameter to this function,\nit is simpler to pass the address of the variable and have\ngitdiff_verify_name() change the variable itself.\n\nThis also makes it possible to later have this function return\n-1 instead of die()ing in case of error.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 26 +++++++++++++-------------\n 1 file changed, 13 insertions(+), 13 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 8e4da2e..fe5aebd 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -925,43 +925,43 @@ static int gitdiff_hdrend(const char *line, struct patch *patch)\n #define DIFF_OLD_NAME 0\n #define DIFF_NEW_NAME 1\n \n-static char *gitdiff_verify_name(const char *line, int isnull, char *orig_name, int side)\n+static void gitdiff_verify_name(const char *line, int isnull, char **name, int side)\n {\n-\tif (!orig_name && !isnull)\n-\t\treturn find_name(line, NULL, p_value, TERM_TAB);\n+\tif (!*name && !isnull) {\n+\t\t*name = find_name(line, NULL, p_value, TERM_TAB);\n+\t\treturn;\n+\t}\n \n-\tif (orig_name) {\n-\t\tint len = strlen(orig_name);\n+\tif (*name) {\n+\t\tint len = strlen(*name);\n \t\tchar *another;\n \t\tif (isnull)\n \t\t\tdie(_(\"git apply: bad git-diff - expected /dev/null, got %s on line %d\"),\n-\t\t\t    orig_name, linenr);\n+\t\t\t    *name, linenr);\n \t\tanother = find_name(line, NULL, p_value, TERM_TAB);\n-\t\tif (!another || memcmp(another, orig_name, len + 1))\n+\t\tif (!another || memcmp(another, *name, len + 1))\n \t\t\tdie((side == DIFF_NEW_NAME) ?\n \t\t\t    _(\"git apply: bad git-diff - inconsistent new filename on line %d\") :\n \t\t\t    _(\"git apply: bad git-diff - inconsistent old filename on line %d\"), linenr);\n \t\tfree(another);\n-\t\treturn orig_name;\n \t} else {\n \t\t/* expect \"/dev/null\" */\n \t\tif (memcmp(\"/dev/null\", line, 9) || line[9] != '\\n')\n \t\t\tdie(_(\"git apply: bad git-diff - expected /dev/null on line %d\"), linenr);\n-\t\treturn NULL;\n \t}\n }\n \n static int gitdiff_oldname(const char *line, struct patch *patch)\n {\n-\tpatch->old_name = gitdiff_verify_name(line, patch->is_new, patch->old_name,\n-\t\t\t\t\t      DIFF_OLD_NAME);\n+\tgitdiff_verify_name(line, patch->is_new, &patch->old_name,\n+\t\t\t    DIFF_OLD_NAME);\n \treturn 0;\n }\n \n static int gitdiff_newname(const char *line, struct patch *patch)\n {\n-\tpatch->new_name = gitdiff_verify_name(line, patch->is_delete, patch->new_name,\n-\t\t\t\t\t      DIFF_NEW_NAME);\n+\tgitdiff_verify_name(line, patch->is_delete, &patch->new_name,\n+\t\t\t    DIFF_NEW_NAME);\n \treturn 0;\n }\n \n-- \n2.8.3.443.gaeee61e\n"},{"id":"287347","messageId":"20160524081126.16973-3-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 02/49] builtin/apply: avoid parameter shadowing 'p_value' global","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:10:39Z","receivedAt":"2016-05-24T08:10:39Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Let's just rename the global 'state_p_value' as it will become\n'state->p_value' in a following patch.\n\nThis also avoid errors when compiling with -Wshadow and makes\nit safer to later move global variables into a \"state\" struct.\n\nHelped-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 34 +++++++++++++++++-----------------\n 1 file changed, 17 insertions(+), 17 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex fe5aebd..e133b38 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -35,7 +35,7 @@ static int prefix_length = -1;\n static int newfd = -1;\n \n static int unidiff_zero;\n-static int p_value = 1;\n+static int state_p_value = 1;\n static int p_value_known;\n static int check_index;\n static int update_index;\n@@ -872,24 +872,24 @@ static void parse_traditional_patch(const char *first, const char *second, struc\n \t\tq = guess_p_value(second);\n \t\tif (p < 0) p = q;\n \t\tif (0 <= p && p == q) {\n-\t\t\tp_value = p;\n+\t\t\tstate_p_value = p;\n \t\t\tp_value_known = 1;\n \t\t}\n \t}\n \tif (is_dev_null(first)) {\n \t\tpatch->is_new = 1;\n \t\tpatch->is_delete = 0;\n-\t\tname = find_name_traditional(second, NULL, p_value);\n+\t\tname = find_name_traditional(second, NULL, state_p_value);\n \t\tpatch->new_name = name;\n \t} else if (is_dev_null(second)) {\n \t\tpatch->is_new = 0;\n \t\tpatch->is_delete = 1;\n-\t\tname = find_name_traditional(first, NULL, p_value);\n+\t\tname = find_name_traditional(first, NULL, state_p_value);\n \t\tpatch->old_name = name;\n \t} else {\n \t\tchar *first_name;\n-\t\tfirst_name = find_name_traditional(first, NULL, p_value);\n-\t\tname = find_name_traditional(second, first_name, p_value);\n+\t\tfirst_name = find_name_traditional(first, NULL, state_p_value);\n+\t\tname = find_name_traditional(second, first_name, state_p_value);\n \t\tfree(first_name);\n \t\tif (has_epoch_timestamp(first)) {\n \t\t\tpatch->is_new = 1;\n@@ -928,7 +928,7 @@ static int gitdiff_hdrend(const char *line, struct patch *patch)\n static void gitdiff_verify_name(const char *line, int isnull, char **name, int side)\n {\n \tif (!*name && !isnull) {\n-\t\t*name = find_name(line, NULL, p_value, TERM_TAB);\n+\t\t*name = find_name(line, NULL, state_p_value, TERM_TAB);\n \t\treturn;\n \t}\n \n@@ -938,7 +938,7 @@ static void gitdiff_verify_name(const char *line, int isnull, char **name, int s\n \t\tif (isnull)\n \t\t\tdie(_(\"git apply: bad git-diff - expected /dev/null, got %s on line %d\"),\n \t\t\t    *name, linenr);\n-\t\tanother = find_name(line, NULL, p_value, TERM_TAB);\n+\t\tanother = find_name(line, NULL, state_p_value, TERM_TAB);\n \t\tif (!another || memcmp(another, *name, len + 1))\n \t\t\tdie((side == DIFF_NEW_NAME) ?\n \t\t\t    _(\"git apply: bad git-diff - inconsistent new filename on line %d\") :\n@@ -997,7 +997,7 @@ static int gitdiff_copysrc(const char *line, struct patch *patch)\n {\n \tpatch->is_copy = 1;\n \tfree(patch->old_name);\n-\tpatch->old_name = find_name(line, NULL, p_value ? p_value - 1 : 0, 0);\n+\tpatch->old_name = find_name(line, NULL, state_p_value ? state_p_value - 1 : 0, 0);\n \treturn 0;\n }\n \n@@ -1005,7 +1005,7 @@ static int gitdiff_copydst(const char *line, struct patch *patch)\n {\n \tpatch->is_copy = 1;\n \tfree(patch->new_name);\n-\tpatch->new_name = find_name(line, NULL, p_value ? p_value - 1 : 0, 0);\n+\tpatch->new_name = find_name(line, NULL, state_p_value ? state_p_value - 1 : 0, 0);\n \treturn 0;\n }\n \n@@ -1013,7 +1013,7 @@ static int gitdiff_renamesrc(const char *line, struct patch *patch)\n {\n \tpatch->is_rename = 1;\n \tfree(patch->old_name);\n-\tpatch->old_name = find_name(line, NULL, p_value ? p_value - 1 : 0, 0);\n+\tpatch->old_name = find_name(line, NULL, state_p_value ? state_p_value - 1 : 0, 0);\n \treturn 0;\n }\n \n@@ -1021,7 +1021,7 @@ static int gitdiff_renamedst(const char *line, struct patch *patch)\n {\n \tpatch->is_rename = 1;\n \tfree(patch->new_name);\n-\tpatch->new_name = find_name(line, NULL, p_value ? p_value - 1 : 0, 0);\n+\tpatch->new_name = find_name(line, NULL, state_p_value ? state_p_value - 1 : 0, 0);\n \treturn 0;\n }\n \n@@ -1092,10 +1092,10 @@ static const char *skip_tree_prefix(const char *line, int llen)\n \tint nslash;\n \tint i;\n \n-\tif (!p_value)\n+\tif (!state_p_value)\n \t\treturn (llen && line[0] == '/') ? NULL : line;\n \n-\tnslash = p_value;\n+\tnslash = state_p_value;\n \tfor (i = 0; i < llen; i++) {\n \t\tint ch = line[i];\n \t\tif (ch == '/' && --nslash <= 0)\n@@ -1481,8 +1481,8 @@ static int find_header(const char *line, unsigned long size, int *hdrsize, struc\n \t\t\t\t\t       \"%d leading pathname component (line %d)\",\n \t\t\t\t\t       \"git diff header lacks filename information when removing \"\n \t\t\t\t\t       \"%d leading pathname components (line %d)\",\n-\t\t\t\t\t       p_value),\n-\t\t\t\t\t    p_value, linenr);\n+\t\t\t\t\t       state_p_value),\n+\t\t\t\t\t    state_p_value, linenr);\n \t\t\t\tpatch->old_name = xstrdup(patch->def_name);\n \t\t\t\tpatch->new_name = xstrdup(patch->def_name);\n \t\t\t}\n@@ -4461,7 +4461,7 @@ static int option_parse_include(const struct option *opt,\n static int option_parse_p(const struct option *opt,\n \t\t\t  const char *arg, int unset)\n {\n-\tp_value = atoi(arg);\n+\tstate_p_value = atoi(arg);\n \tp_value_known = 1;\n \treturn 0;\n }\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287396","messageId":"20160524081126.16973-4-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 03/49] builtin/apply: avoid parameter shadowing 'linenr' global","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:10:40Z","receivedAt":"2016-05-24T08:10:40Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Let's just rename the global 'state_linenr' as it will become\n'state->linenr' in a following patch.\n\nThis also avoid errors when compiling with -Wshadow and makes\nit safer to later move global variables into a \"state\" struct.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 48 ++++++++++++++++++++++++------------------------\n 1 file changed, 24 insertions(+), 24 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex e133b38..705a9c8 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -144,7 +144,7 @@ static int max_change, max_len;\n  * file (and how) we're patching right now.. The \"is_xxxx\"\n  * things are flags, where -1 means \"don't know yet\".\n  */\n-static int linenr = 1;\n+static int state_linenr = 1;\n \n /*\n  * This represents one \"hunk\" from a patch, starting with\n@@ -905,7 +905,7 @@ static void parse_traditional_patch(const char *first, const char *second, struc\n \t\t}\n \t}\n \tif (!name)\n-\t\tdie(_(\"unable to find filename in patch at line %d\"), linenr);\n+\t\tdie(_(\"unable to find filename in patch at line %d\"), state_linenr);\n }\n \n static int gitdiff_hdrend(const char *line, struct patch *patch)\n@@ -937,17 +937,17 @@ static void gitdiff_verify_name(const char *line, int isnull, char **name, int s\n \t\tchar *another;\n \t\tif (isnull)\n \t\t\tdie(_(\"git apply: bad git-diff - expected /dev/null, got %s on line %d\"),\n-\t\t\t    *name, linenr);\n+\t\t\t    *name, state_linenr);\n \t\tanother = find_name(line, NULL, state_p_value, TERM_TAB);\n \t\tif (!another || memcmp(another, *name, len + 1))\n \t\t\tdie((side == DIFF_NEW_NAME) ?\n \t\t\t    _(\"git apply: bad git-diff - inconsistent new filename on line %d\") :\n-\t\t\t    _(\"git apply: bad git-diff - inconsistent old filename on line %d\"), linenr);\n+\t\t\t    _(\"git apply: bad git-diff - inconsistent old filename on line %d\"), state_linenr);\n \t\tfree(another);\n \t} else {\n \t\t/* expect \"/dev/null\" */\n \t\tif (memcmp(\"/dev/null\", line, 9) || line[9] != '\\n')\n-\t\t\tdie(_(\"git apply: bad git-diff - expected /dev/null on line %d\"), linenr);\n+\t\t\tdie(_(\"git apply: bad git-diff - expected /dev/null on line %d\"), state_linenr);\n \t}\n }\n \n@@ -1272,8 +1272,8 @@ static int parse_git_header(const char *line, int len, unsigned int size, struct\n \n \tline += len;\n \tsize -= len;\n-\tlinenr++;\n-\tfor (offset = len ; size > 0 ; offset += len, size -= len, line += len, linenr++) {\n+\tstate_linenr++;\n+\tfor (offset = len ; size > 0 ; offset += len, size -= len, line += len, state_linenr++) {\n \t\tstatic const struct opentry {\n \t\t\tconst char *str;\n \t\t\tint (*fn)(const char *, struct patch *);\n@@ -1440,7 +1440,7 @@ static int find_header(const char *line, unsigned long size, int *hdrsize, struc\n \tpatch->is_new = patch->is_delete = -1;\n \tpatch->old_mode = patch->new_mode = 0;\n \tpatch->old_name = patch->new_name = NULL;\n-\tfor (offset = 0; size > 0; offset += len, size -= len, line += len, linenr++) {\n+\tfor (offset = 0; size > 0; offset += len, size -= len, line += len, state_linenr++) {\n \t\tunsigned long nextlen;\n \n \t\tlen = linelen(line, size);\n@@ -1461,7 +1461,7 @@ static int find_header(const char *line, unsigned long size, int *hdrsize, struc\n \t\t\tif (parse_fragment_header(line, len, &dummy) < 0)\n \t\t\t\tcontinue;\n \t\t\tdie(_(\"patch fragment without header at line %d: %.*s\"),\n-\t\t\t    linenr, (int)len-1, line);\n+\t\t\t    state_linenr, (int)len-1, line);\n \t\t}\n \n \t\tif (size < len + 6)\n@@ -1482,13 +1482,13 @@ static int find_header(const char *line, unsigned long size, int *hdrsize, struc\n \t\t\t\t\t       \"git diff header lacks filename information when removing \"\n \t\t\t\t\t       \"%d leading pathname components (line %d)\",\n \t\t\t\t\t       state_p_value),\n-\t\t\t\t\t    state_p_value, linenr);\n+\t\t\t\t\t    state_p_value, state_linenr);\n \t\t\t\tpatch->old_name = xstrdup(patch->def_name);\n \t\t\t\tpatch->new_name = xstrdup(patch->def_name);\n \t\t\t}\n \t\t\tif (!patch->is_delete && !patch->new_name)\n \t\t\t\tdie(\"git diff header lacks filename information \"\n-\t\t\t\t    \"(line %d)\", linenr);\n+\t\t\t\t    \"(line %d)\", state_linenr);\n \t\t\tpatch->is_toplevel_relative = 1;\n \t\t\t*hdrsize = git_hdr_len;\n \t\t\treturn offset;\n@@ -1510,7 +1510,7 @@ static int find_header(const char *line, unsigned long size, int *hdrsize, struc\n \t\t/* Ok, we'll consider it a patch */\n \t\tparse_traditional_patch(line, line+len, patch);\n \t\t*hdrsize = len + nextlen;\n-\t\tlinenr += 2;\n+\t\tstate_linenr += 2;\n \t\treturn offset;\n \t}\n \treturn -1;\n@@ -1538,7 +1538,7 @@ static void check_whitespace(const char *line, int len, unsigned ws_rule)\n {\n \tunsigned result = ws_check(line + 1, len - 1, ws_rule);\n \n-\trecord_ws_error(result, line + 1, len - 2, linenr);\n+\trecord_ws_error(result, line + 1, len - 2, state_linenr);\n }\n \n /*\n@@ -1568,11 +1568,11 @@ static int parse_fragment(const char *line, unsigned long size,\n \t/* Parse the thing.. */\n \tline += len;\n \tsize -= len;\n-\tlinenr++;\n+\tstate_linenr++;\n \tadded = deleted = 0;\n \tfor (offset = len;\n \t     0 < size;\n-\t     offset += len, size -= len, line += len, linenr++) {\n+\t     offset += len, size -= len, line += len, state_linenr++) {\n \t\tif (!oldlines && !newlines)\n \t\t\tbreak;\n \t\tlen = linelen(line, size);\n@@ -1668,10 +1668,10 @@ static int parse_single_patch(const char *line, unsigned long size, struct patch\n \t\tint len;\n \n \t\tfragment = xcalloc(1, sizeof(*fragment));\n-\t\tfragment->linenr = linenr;\n+\t\tfragment->linenr = state_linenr;\n \t\tlen = parse_fragment(line, size, patch, fragment);\n \t\tif (len <= 0)\n-\t\t\tdie(_(\"corrupt patch at line %d\"), linenr);\n+\t\t\tdie(_(\"corrupt patch at line %d\"), state_linenr);\n \t\tfragment->patch = line;\n \t\tfragment->size = len;\n \t\toldlines += fragment->oldlines;\n@@ -1799,13 +1799,13 @@ static struct fragment *parse_binary_hunk(char **buf_p,\n \telse\n \t\treturn NULL;\n \n-\tlinenr++;\n+\tstate_linenr++;\n \tbuffer += llen;\n \twhile (1) {\n \t\tint byte_length, max_byte_length, newsize;\n \t\tllen = linelen(buffer, size);\n \t\tused += llen;\n-\t\tlinenr++;\n+\t\tstate_linenr++;\n \t\tif (llen == 1) {\n \t\t\t/* consume the blank line */\n \t\t\tbuffer++;\n@@ -1859,7 +1859,7 @@ static struct fragment *parse_binary_hunk(char **buf_p,\n \tfree(data);\n \t*status_p = -1;\n \terror(_(\"corrupt binary patch at line %d: %.*s\"),\n-\t      linenr-1, llen-1, buffer);\n+\t      state_linenr-1, llen-1, buffer);\n \treturn NULL;\n }\n \n@@ -1892,7 +1892,7 @@ static int parse_binary(char *buffer, unsigned long size, struct patch *patch)\n \tforward = parse_binary_hunk(&buffer, &size, &status, &used);\n \tif (!forward && !status)\n \t\t/* there has to be one hunk (forward hunk) */\n-\t\treturn error(_(\"unrecognized binary patch at line %d\"), linenr-1);\n+\t\treturn error(_(\"unrecognized binary patch at line %d\"), state_linenr-1);\n \tif (status)\n \t\t/* otherwise we already gave an error message */\n \t\treturn status;\n@@ -2010,7 +2010,7 @@ static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)\n \t\tif (llen == sizeof(git_binary) - 1 &&\n \t\t    !memcmp(git_binary, buffer + hd, llen)) {\n \t\t\tint used;\n-\t\t\tlinenr++;\n+\t\t\tstate_linenr++;\n \t\t\tused = parse_binary(buffer + hd + llen,\n \t\t\t\t\t    size - hd - llen, patch);\n \t\t\tif (used < 0)\n@@ -2031,7 +2031,7 @@ static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)\n \t\t\t\tint len = strlen(binhdr[i]);\n \t\t\t\tif (len < size - hd &&\n \t\t\t\t    !memcmp(binhdr[i], buffer + hd, len)) {\n-\t\t\t\t\tlinenr++;\n+\t\t\t\t\tstate_linenr++;\n \t\t\t\t\tpatch->is_binary = 1;\n \t\t\t\t\tpatchsize = llen;\n \t\t\t\t\tbreak;\n@@ -2045,7 +2045,7 @@ static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)\n \t\t */\n \t\tif ((apply || check) &&\n \t\t    (!patch->is_binary && !metadata_changes(patch)))\n-\t\t\tdie(_(\"patch with only garbage at line %d\"), linenr);\n+\t\t\tdie(_(\"patch with only garbage at line %d\"), state_linenr);\n \t}\n \n \treturn offset + hdrsize + patchsize;\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287395","messageId":"20160524081126.16973-5-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 04/49] builtin/apply: avoid local variable shadowing 'len' parameter","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:10:41Z","receivedAt":"2016-05-24T08:10:41Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"This is just a cleanup to avoid errors when compiling with -Wshadow and\nto make it safer to later move global variables into a \"state\" struct.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 20 ++++++++++----------\n 1 file changed, 10 insertions(+), 10 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 705a9c8..bb8bf7f 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -2194,17 +2194,17 @@ static void update_pre_post_images(struct image *preimage,\n \tfixed = preimage->buf;\n \n \tfor (i = reduced = ctx = 0; i < postimage->nr; i++) {\n-\t\tsize_t len = postimage->line[i].len;\n+\t\tsize_t l_len = postimage->line[i].len;\n \t\tif (!(postimage->line[i].flag & LINE_COMMON)) {\n \t\t\t/* an added line -- no counterparts in preimage */\n-\t\t\tmemmove(new, old, len);\n-\t\t\told += len;\n-\t\t\tnew += len;\n+\t\t\tmemmove(new, old, l_len);\n+\t\t\told += l_len;\n+\t\t\tnew += l_len;\n \t\t\tcontinue;\n \t\t}\n \n \t\t/* a common context -- skip it in the original postimage */\n-\t\told += len;\n+\t\told += l_len;\n \n \t\t/* and find the corresponding one in the fixed preimage */\n \t\twhile (ctx < preimage->nr &&\n@@ -2223,11 +2223,11 @@ static void update_pre_post_images(struct image *preimage,\n \t\t}\n \n \t\t/* and copy it in, while fixing the line length */\n-\t\tlen = preimage->line[ctx].len;\n-\t\tmemcpy(new, fixed, len);\n-\t\tnew += len;\n-\t\tfixed += len;\n-\t\tpostimage->line[i].len = len;\n+\t\tl_len = preimage->line[ctx].len;\n+\t\tmemcpy(new, fixed, l_len);\n+\t\tnew += l_len;\n+\t\tfixed += l_len;\n+\t\tpostimage->line[i].len = l_len;\n \t\tctx++;\n \t}\n \n-- \n2.8.3.443.gaeee61e\n"},{"id":"287397","messageId":"20160524081126.16973-6-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 05/49] builtin/apply: extract line_by_line_fuzzy_match() from match_fragment()","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:10:42Z","receivedAt":"2016-05-24T08:10:42Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"The match_fragment() function is very big and contains a big special case\nalgorithm that does line by line fuzzy matching. So let's extract this\nalgorithm in a separate line_by_line_fuzzy_match() function.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 126 +++++++++++++++++++++++++++++++-------------------------\n 1 file changed, 71 insertions(+), 55 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex bb8bf7f..7bab466 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -2242,6 +2242,74 @@ static void update_pre_post_images(struct image *preimage,\n \tpostimage->nr -= reduced;\n }\n \n+static int line_by_line_fuzzy_match(struct image *img,\n+\t\t\t\t    struct image *preimage,\n+\t\t\t\t    struct image *postimage,\n+\t\t\t\t    unsigned long try,\n+\t\t\t\t    int try_lno,\n+\t\t\t\t    int preimage_limit)\n+{\n+\tint i;\n+\tsize_t imgoff = 0;\n+\tsize_t preoff = 0;\n+\tsize_t postlen = postimage->len;\n+\tsize_t extra_chars;\n+\tchar *buf;\n+\tchar *preimage_eof;\n+\tchar *preimage_end;\n+\tstruct strbuf fixed;\n+\tchar *fixed_buf;\n+\tsize_t fixed_len;\n+\n+\tfor (i = 0; i < preimage_limit; i++) {\n+\t\tsize_t prelen = preimage->line[i].len;\n+\t\tsize_t imglen = img->line[try_lno+i].len;\n+\n+\t\tif (!fuzzy_matchlines(img->buf + try + imgoff, imglen,\n+\t\t\t\t      preimage->buf + preoff, prelen))\n+\t\t\treturn 0;\n+\t\tif (preimage->line[i].flag & LINE_COMMON)\n+\t\t\tpostlen += imglen - prelen;\n+\t\timgoff += imglen;\n+\t\tpreoff += prelen;\n+\t}\n+\n+\t/*\n+\t * Ok, the preimage matches with whitespace fuzz.\n+\t *\n+\t * imgoff now holds the true length of the target that\n+\t * matches the preimage before the end of the file.\n+\t *\n+\t * Count the number of characters in the preimage that fall\n+\t * beyond the end of the file and make sure that all of them\n+\t * are whitespace characters. (This can only happen if\n+\t * we are removing blank lines at the end of the file.)\n+\t */\n+\tbuf = preimage_eof = preimage->buf + preoff;\n+\tfor ( ; i < preimage->nr; i++)\n+\t\tpreoff += preimage->line[i].len;\n+\tpreimage_end = preimage->buf + preoff;\n+\tfor ( ; buf < preimage_end; buf++)\n+\t\tif (!isspace(*buf))\n+\t\t\treturn 0;\n+\n+\t/*\n+\t * Update the preimage and the common postimage context\n+\t * lines to use the same whitespace as the target.\n+\t * If whitespace is missing in the target (i.e.\n+\t * if the preimage extends beyond the end of the file),\n+\t * use the whitespace from the preimage.\n+\t */\n+\textra_chars = preimage_end - preimage_eof;\n+\tstrbuf_init(&fixed, imgoff + extra_chars);\n+\tstrbuf_add(&fixed, img->buf + try, imgoff);\n+\tstrbuf_add(&fixed, preimage_eof, extra_chars);\n+\tfixed_buf = strbuf_detach(&fixed, &fixed_len);\n+\tupdate_pre_post_images(preimage, postimage,\n+\t\t\t       fixed_buf, fixed_len, postlen);\n+\treturn 1;\n+}\n+\n static int match_fragment(struct image *img,\n \t\t\t  struct image *preimage,\n \t\t\t  struct image *postimage,\n@@ -2331,61 +2399,9 @@ static int match_fragment(struct image *img,\n \t * fuzzy matching. We collect all the line length information because\n \t * we need it to adjust whitespace if we match.\n \t */\n-\tif (ws_ignore_action == ignore_ws_change) {\n-\t\tsize_t imgoff = 0;\n-\t\tsize_t preoff = 0;\n-\t\tsize_t postlen = postimage->len;\n-\t\tsize_t extra_chars;\n-\t\tchar *preimage_eof;\n-\t\tchar *preimage_end;\n-\t\tfor (i = 0; i < preimage_limit; i++) {\n-\t\t\tsize_t prelen = preimage->line[i].len;\n-\t\t\tsize_t imglen = img->line[try_lno+i].len;\n-\n-\t\t\tif (!fuzzy_matchlines(img->buf + try + imgoff, imglen,\n-\t\t\t\t\t      preimage->buf + preoff, prelen))\n-\t\t\t\treturn 0;\n-\t\t\tif (preimage->line[i].flag & LINE_COMMON)\n-\t\t\t\tpostlen += imglen - prelen;\n-\t\t\timgoff += imglen;\n-\t\t\tpreoff += prelen;\n-\t\t}\n-\n-\t\t/*\n-\t\t * Ok, the preimage matches with whitespace fuzz.\n-\t\t *\n-\t\t * imgoff now holds the true length of the target that\n-\t\t * matches the preimage before the end of the file.\n-\t\t *\n-\t\t * Count the number of characters in the preimage that fall\n-\t\t * beyond the end of the file and make sure that all of them\n-\t\t * are whitespace characters. (This can only happen if\n-\t\t * we are removing blank lines at the end of the file.)\n-\t\t */\n-\t\tbuf = preimage_eof = preimage->buf + preoff;\n-\t\tfor ( ; i < preimage->nr; i++)\n-\t\t\tpreoff += preimage->line[i].len;\n-\t\tpreimage_end = preimage->buf + preoff;\n-\t\tfor ( ; buf < preimage_end; buf++)\n-\t\t\tif (!isspace(*buf))\n-\t\t\t\treturn 0;\n-\n-\t\t/*\n-\t\t * Update the preimage and the common postimage context\n-\t\t * lines to use the same whitespace as the target.\n-\t\t * If whitespace is missing in the target (i.e.\n-\t\t * if the preimage extends beyond the end of the file),\n-\t\t * use the whitespace from the preimage.\n-\t\t */\n-\t\textra_chars = preimage_end - preimage_eof;\n-\t\tstrbuf_init(&fixed, imgoff + extra_chars);\n-\t\tstrbuf_add(&fixed, img->buf + try, imgoff);\n-\t\tstrbuf_add(&fixed, preimage_eof, extra_chars);\n-\t\tfixed_buf = strbuf_detach(&fixed, &fixed_len);\n-\t\tupdate_pre_post_images(preimage, postimage,\n-\t\t\t\tfixed_buf, fixed_len, postlen);\n-\t\treturn 1;\n-\t}\n+\tif (ws_ignore_action == ignore_ws_change)\n+\t\treturn line_by_line_fuzzy_match(img, preimage, postimage,\n+\t\t\t\t\t\ttry, try_lno, preimage_limit);\n \n \tif (ws_error_action != correct_ws_error)\n \t\treturn 0;\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287348","messageId":"20160524081126.16973-7-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 06/49] builtin/apply: move 'options' variable into cmd_apply()","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:10:43Z","receivedAt":"2016-05-24T08:10:43Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"The 'options' variable doesn't need to be static and global to the\nfile. It can be local to cmd_apply(), so let's move it there.\n\nThis will make it easier to libify the apply functionality.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 7bab466..5a1d65a 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -79,7 +79,6 @@ static enum ws_ignore {\n static const char *patch_input_file;\n static struct strbuf root = STRBUF_INIT;\n static int read_stdin = 1;\n-static int options;\n \n static void parse_whitespace_option(const char *option)\n {\n@@ -4517,6 +4516,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)\n \tint errs = 0;\n \tint is_not_gitdir = !startup_info->have_repository;\n \tint force_apply = 0;\n+\tint options = 0;\n \n \tconst char *whitespace_option = NULL;\n \n-- \n2.8.3.443.gaeee61e\n"},{"id":"287352","messageId":"20160524081126.16973-8-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 07/49] builtin/apply: move 'read_stdin' global into cmd_apply()","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:10:44Z","receivedAt":"2016-05-24T08:10:44Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"The 'read_stdin' variable doesn't need to be static and global to the\nfile. It can be local to cmd_apply(), so let's move it there.\n\nThis will make it easier to libify the apply functionality.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 2 +-\n 1 file changed, 1 insertion(+), 1 deletion(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 5a1d65a..c911e4e 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -78,7 +78,6 @@ static enum ws_ignore {\n \n static const char *patch_input_file;\n static struct strbuf root = STRBUF_INIT;\n-static int read_stdin = 1;\n \n static void parse_whitespace_option(const char *option)\n {\n@@ -4517,6 +4516,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)\n \tint is_not_gitdir = !startup_info->have_repository;\n \tint force_apply = 0;\n \tint options = 0;\n+\tint read_stdin = 1;\n \n \tconst char *whitespace_option = NULL;\n \n-- \n2.8.3.443.gaeee61e\n"},{"id":"287351","messageId":"20160524081126.16973-9-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 08/49] builtin/apply: introduce 'struct apply_state' to start libifying","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:10:45Z","receivedAt":"2016-05-24T08:10:45Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Currently commands that want to use the apply functionality have to launch\na \"git apply\" process which can be bad for performance.\n\nLet's start libifying the apply functionality and to do that we first need\nto get rid of the global variables in \"builtin/apply.c\".\n\nThis patch introduces \"struct apply_state\" into which all the previously\nglobal variables will be moved. A new parameter called \"state\" that is a\npointer to the \"apply_state\" structure will come at the beginning of the\nhelper functions that need it and will be passed around the call chain.\n\nTo start let's move the \"prefix\" and \"prefix_length\" global variables into\n\"struct apply_state\".\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 94 ++++++++++++++++++++++++++++++++++-----------------------\n 1 file changed, 56 insertions(+), 38 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex c911e4e..ae068e7 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -21,6 +21,11 @@\n #include \"ll-merge.h\"\n #include \"rerere.h\"\n \n+struct apply_state {\n+\tconst char *prefix;\n+\tint prefix_length;\n+};\n+\n /*\n  *  --check turns on checking that the working tree matches the\n  *    files that are being modified, but doesn't apply the patch\n@@ -30,8 +35,6 @@\n  *  --index updates the cache as well.\n  *  --cached updates only the cache without ever touching the working tree.\n  */\n-static const char *prefix;\n-static int prefix_length = -1;\n static int newfd = -1;\n \n static int unidiff_zero;\n@@ -748,7 +751,7 @@ static int count_slashes(const char *cp)\n  * Given the string after \"--- \" or \"+++ \", guess the appropriate\n  * p_value for the given patch.\n  */\n-static int guess_p_value(const char *nameline)\n+static int guess_p_value(struct apply_state *state, const char *nameline)\n {\n \tchar *name, *cp;\n \tint val = -1;\n@@ -761,17 +764,17 @@ static int guess_p_value(const char *nameline)\n \tcp = strchr(name, '/');\n \tif (!cp)\n \t\tval = 0;\n-\telse if (prefix) {\n+\telse if (state->prefix) {\n \t\t/*\n \t\t * Does it begin with \"a/$our-prefix\" and such?  Then this is\n \t\t * very likely to apply to our directory.\n \t\t */\n-\t\tif (!strncmp(name, prefix, prefix_length))\n-\t\t\tval = count_slashes(prefix);\n+\t\tif (!strncmp(name, state->prefix, state->prefix_length))\n+\t\t\tval = count_slashes(state->prefix);\n \t\telse {\n \t\t\tcp++;\n-\t\t\tif (!strncmp(cp, prefix, prefix_length))\n-\t\t\t\tval = count_slashes(prefix) + 1;\n+\t\t\tif (!strncmp(cp, state->prefix, state->prefix_length))\n+\t\t\t\tval = count_slashes(state->prefix) + 1;\n \t\t}\n \t}\n \tfree(name);\n@@ -858,7 +861,10 @@ static int has_epoch_timestamp(const char *nameline)\n  * files, we can happily check the index for a match, but for creating a\n  * new file we should try to match whatever \"patch\" does. I have no idea.\n  */\n-static void parse_traditional_patch(const char *first, const char *second, struct patch *patch)\n+static void parse_traditional_patch(struct apply_state *state,\n+\t\t\t\t    const char *first,\n+\t\t\t\t    const char *second,\n+\t\t\t\t    struct patch *patch)\n {\n \tchar *name;\n \n@@ -866,8 +872,8 @@ static void parse_traditional_patch(const char *first, const char *second, struc\n \tsecond += 4;\t/* skip \"+++ \" */\n \tif (!p_value_known) {\n \t\tint p, q;\n-\t\tp = guess_p_value(first);\n-\t\tq = guess_p_value(second);\n+\t\tp = guess_p_value(state, first);\n+\t\tq = guess_p_value(state, second);\n \t\tif (p < 0) p = q;\n \t\tif (0 <= p && p == q) {\n \t\t\tstate_p_value = p;\n@@ -1429,7 +1435,11 @@ static int parse_fragment_header(const char *line, int len, struct fragment *fra\n \treturn offset;\n }\n \n-static int find_header(const char *line, unsigned long size, int *hdrsize, struct patch *patch)\n+static int find_header(struct apply_state *state,\n+\t\t       const char *line,\n+\t\t       unsigned long size,\n+\t\t       int *hdrsize,\n+\t\t       struct patch *patch)\n {\n \tunsigned long offset, len;\n \n@@ -1506,7 +1516,7 @@ static int find_header(const char *line, unsigned long size, int *hdrsize, struc\n \t\t\tcontinue;\n \n \t\t/* Ok, we'll consider it a patch */\n-\t\tparse_traditional_patch(line, line+len, patch);\n+\t\tparse_traditional_patch(state, line, line+len, patch);\n \t\t*hdrsize = len + nextlen;\n \t\tstate_linenr += 2;\n \t\treturn offset;\n@@ -1913,21 +1923,21 @@ static int parse_binary(char *buffer, unsigned long size, struct patch *patch)\n \treturn used;\n }\n \n-static void prefix_one(char **name)\n+static void prefix_one(struct apply_state *state, char **name)\n {\n \tchar *old_name = *name;\n \tif (!old_name)\n \t\treturn;\n-\t*name = xstrdup(prefix_filename(prefix, prefix_length, *name));\n+\t*name = xstrdup(prefix_filename(state->prefix, state->prefix_length, *name));\n \tfree(old_name);\n }\n \n-static void prefix_patch(struct patch *p)\n+static void prefix_patch(struct apply_state *state, struct patch *p)\n {\n-\tif (!prefix || p->is_toplevel_relative)\n+\tif (!state->prefix || p->is_toplevel_relative)\n \t\treturn;\n-\tprefix_one(&p->new_name);\n-\tprefix_one(&p->old_name);\n+\tprefix_one(state, &p->new_name);\n+\tprefix_one(state, &p->old_name);\n }\n \n /*\n@@ -1944,16 +1954,16 @@ static void add_name_limit(const char *name, int exclude)\n \tit->util = exclude ? NULL : (void *) 1;\n }\n \n-static int use_patch(struct patch *p)\n+static int use_patch(struct apply_state *state, struct patch *p)\n {\n \tconst char *pathname = p->new_name ? p->new_name : p->old_name;\n \tint i;\n \n \t/* Paths outside are not touched regardless of \"--include\" */\n-\tif (0 < prefix_length) {\n+\tif (0 < state->prefix_length) {\n \t\tint pathlen = strlen(pathname);\n-\t\tif (pathlen <= prefix_length ||\n-\t\t    memcmp(prefix, pathname, prefix_length))\n+\t\tif (pathlen <= state->prefix_length ||\n+\t\t    memcmp(state->prefix, pathname, state->prefix_length))\n \t\t\treturn 0;\n \t}\n \n@@ -1980,17 +1990,17 @@ static int use_patch(struct patch *p)\n  * Return the number of bytes consumed, so that the caller can call us\n  * again for the next patch.\n  */\n-static int parse_chunk(char *buffer, unsigned long size, struct patch *patch)\n+static int parse_chunk(struct apply_state *state, char *buffer, unsigned long size, struct patch *patch)\n {\n \tint hdrsize, patchsize;\n-\tint offset = find_header(buffer, size, &hdrsize, patch);\n+\tint offset = find_header(state, buffer, size, &hdrsize, patch);\n \n \tif (offset < 0)\n \t\treturn offset;\n \n-\tprefix_patch(patch);\n+\tprefix_patch(state, patch);\n \n-\tif (!use_patch(patch))\n+\tif (!use_patch(state, patch))\n \t\tpatch->ws_rule = 0;\n \telse\n \t\tpatch->ws_rule = whitespace_rule(patch->new_name\n@@ -4367,7 +4377,10 @@ static struct lock_file lock_file;\n #define INACCURATE_EOF\t(1<<0)\n #define RECOUNT\t\t(1<<1)\n \n-static int apply_patch(int fd, const char *filename, int options)\n+static int apply_patch(struct apply_state *state,\n+\t\t       int fd,\n+\t\t       const char *filename,\n+\t\t       int options)\n {\n \tsize_t offset;\n \tstruct strbuf buf = STRBUF_INIT; /* owns the patch text */\n@@ -4384,14 +4397,14 @@ static int apply_patch(int fd, const char *filename, int options)\n \t\tpatch = xcalloc(1, sizeof(*patch));\n \t\tpatch->inaccurate_eof = !!(options & INACCURATE_EOF);\n \t\tpatch->recount =  !!(options & RECOUNT);\n-\t\tnr = parse_chunk(buf.buf + offset, buf.len - offset, patch);\n+\t\tnr = parse_chunk(state, buf.buf + offset, buf.len - offset, patch);\n \t\tif (nr < 0) {\n \t\t\tfree_patch(patch);\n \t\t\tbreak;\n \t\t}\n \t\tif (apply_in_reverse)\n \t\t\treverse_patches(patch);\n-\t\tif (use_patch(patch)) {\n+\t\tif (use_patch(state, patch)) {\n \t\t\tpatch_stats(patch);\n \t\t\t*listp = patch;\n \t\t\tlistp = &patch->next;\n@@ -4517,6 +4530,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)\n \tint force_apply = 0;\n \tint options = 0;\n \tint read_stdin = 1;\n+\tstruct apply_state state;\n \n \tconst char *whitespace_option = NULL;\n \n@@ -4589,15 +4603,17 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)\n \t\tOPT_END()\n \t};\n \n-\tprefix = prefix_;\n-\tprefix_length = prefix ? strlen(prefix) : 0;\n+\tmemset(&state, 0, sizeof(state));\n+\tstate.prefix = prefix_;\n+\tstate.prefix_length = state.prefix ? strlen(state.prefix) : 0;\n+\n \tgit_apply_config();\n \tif (apply_default_whitespace)\n \t\tparse_whitespace_option(apply_default_whitespace);\n \tif (apply_default_ignorewhitespace)\n \t\tparse_ignorewhitespace_option(apply_default_ignorewhitespace);\n \n-\targc = parse_options(argc, argv, prefix, builtin_apply_options,\n+\targc = parse_options(argc, argv, state.prefix, builtin_apply_options,\n \t\t\tapply_usage, 0);\n \n \tif (apply_with_reject && threeway)\n@@ -4628,23 +4644,25 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)\n \t\tint fd;\n \n \t\tif (!strcmp(arg, \"-\")) {\n-\t\t\terrs |= apply_patch(0, \"<stdin>\", options);\n+\t\t\terrs |= apply_patch(&state, 0, \"<stdin>\", options);\n \t\t\tread_stdin = 0;\n \t\t\tcontinue;\n-\t\t} else if (0 < prefix_length)\n-\t\t\targ = prefix_filename(prefix, prefix_length, arg);\n+\t\t} else if (0 < state.prefix_length)\n+\t\t\targ = prefix_filename(state.prefix,\n+\t\t\t\t\t      state.prefix_length,\n+\t\t\t\t\t      arg);\n \n \t\tfd = open(arg, O_RDONLY);\n \t\tif (fd < 0)\n \t\t\tdie_errno(_(\"can't open patch '%s'\"), arg);\n \t\tread_stdin = 0;\n \t\tset_default_whitespace_mode(whitespace_option);\n-\t\terrs |= apply_patch(fd, arg, options);\n+\t\terrs |= apply_patch(&state, fd, arg, options);\n \t\tclose(fd);\n \t}\n \tset_default_whitespace_mode(whitespace_option);\n \tif (read_stdin)\n-\t\terrs |= apply_patch(0, \"<stdin>\", options);\n+\t\terrs |= apply_patch(&state, 0, \"<stdin>\", options);\n \tif (whitespace_error) {\n \t\tif (squelch_whitespace_errors &&\n \t\t    squelch_whitespace_errors < whitespace_error) {\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287394","messageId":"20160524081126.16973-10-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 09/49] builtin/apply: move 'state' init into init_apply_state()","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:10:46Z","receivedAt":"2016-05-24T08:10:46Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"When the apply functionality will be libified, the 'struct apply_state'\nwill be used by different pieces of code.\n\nTo properly initialize a 'struct apply_state', let's provide a nice\nand easy to use init_apply_state() function.\n\nLet's also provide clear_apply_state() to release memory used by\n'struct apply_state' members, so that a 'struct apply_state' instance\ncan be easily reused without leaking memory.\n\nNote that clear_apply_state() does nothing for now, but it will later.\n\nWhile at it, let's rename 'prefix_' parameter to 'prefix'.\n\nHelped-by: Eric Sunshine <sunshine@sunshineco.com>\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 32 ++++++++++++++++++++++----------\n 1 file changed, 22 insertions(+), 10 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex ae068e7..52b5d3e 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -4522,7 +4522,25 @@ static int option_parse_directory(const struct option *opt,\n \treturn 0;\n }\n \n-int cmd_apply(int argc, const char **argv, const char *prefix_)\n+static void init_apply_state(struct apply_state *state, const char *prefix)\n+{\n+\tmemset(state, 0, sizeof(*state));\n+\tstate->prefix = prefix;\n+\tstate->prefix_length = state->prefix ? strlen(state->prefix) : 0;\n+\n+\tgit_apply_config();\n+\tif (apply_default_whitespace)\n+\t\tparse_whitespace_option(apply_default_whitespace);\n+\tif (apply_default_ignorewhitespace)\n+\t\tparse_ignorewhitespace_option(apply_default_ignorewhitespace);\n+}\n+\n+static void clear_apply_state(struct apply_state *state)\n+{\n+\t/* empty for now */\n+}\n+\n+int cmd_apply(int argc, const char **argv, const char *prefix)\n {\n \tint i;\n \tint errs = 0;\n@@ -4603,15 +4621,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)\n \t\tOPT_END()\n \t};\n \n-\tmemset(&state, 0, sizeof(state));\n-\tstate.prefix = prefix_;\n-\tstate.prefix_length = state.prefix ? strlen(state.prefix) : 0;\n-\n-\tgit_apply_config();\n-\tif (apply_default_whitespace)\n-\t\tparse_whitespace_option(apply_default_whitespace);\n-\tif (apply_default_ignorewhitespace)\n-\t\tparse_ignorewhitespace_option(apply_default_ignorewhitespace);\n+\tinit_apply_state(&state, prefix);\n \n \targc = parse_options(argc, argv, state.prefix, builtin_apply_options,\n \t\t\tapply_usage, 0);\n@@ -4695,5 +4705,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)\n \t\t\tdie(_(\"Unable to write new index file\"));\n \t}\n \n+\tclear_apply_state(&state);\n+\n \treturn !!errs;\n }\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287391","messageId":"20160524081126.16973-11-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 10/49] builtin/apply: move 'unidiff_zero' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:10:47Z","receivedAt":"2016-05-24T08:10:47Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify the apply functionality the 'unidiff_zero' variable should\nnot be static and global to the file. Let's move it into\n'struct apply_state'.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 43 +++++++++++++++++++++++++------------------\n 1 file changed, 25 insertions(+), 18 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 52b5d3e..6c36898 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -24,6 +24,9 @@\n struct apply_state {\n \tconst char *prefix;\n \tint prefix_length;\n+\n+\t/* These boolean parameters control how the apply is done */\n+\tint unidiff_zero;\n };\n \n /*\n@@ -37,7 +40,6 @@ struct apply_state {\n  */\n static int newfd = -1;\n \n-static int unidiff_zero;\n static int state_p_value = 1;\n static int p_value_known;\n static int check_index;\n@@ -2694,7 +2696,8 @@ static void update_image(struct image *img,\n  * postimage) for the hunk.  Find lines that match \"preimage\" in \"img\" and\n  * replace the part of \"img\" with \"postimage\" text.\n  */\n-static int apply_one_fragment(struct image *img, struct fragment *frag,\n+static int apply_one_fragment(struct apply_state *state,\n+\t\t\t      struct image *img, struct fragment *frag,\n \t\t\t      int inaccurate_eof, unsigned ws_rule,\n \t\t\t      int nth_fragment)\n {\n@@ -2836,7 +2839,7 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,\n \t * without leading context must match at the beginning.\n \t */\n \tmatch_beginning = (!frag->oldpos ||\n-\t\t\t   (frag->oldpos == 1 && !unidiff_zero));\n+\t\t\t   (frag->oldpos == 1 && !state->unidiff_zero));\n \n \t/*\n \t * A hunk without trailing lines must match at the end.\n@@ -2844,7 +2847,7 @@ static int apply_one_fragment(struct image *img, struct fragment *frag,\n \t * from the lack of trailing lines if the patch was generated\n \t * with unidiff without any context.\n \t */\n-\tmatch_end = !unidiff_zero && !trailing;\n+\tmatch_end = !state->unidiff_zero && !trailing;\n \n \tpos = frag->newpos ? (frag->newpos - 1) : 0;\n \tpreimage.buf = oldlines;\n@@ -3067,7 +3070,7 @@ static int apply_binary(struct image *img, struct patch *patch)\n \treturn 0;\n }\n \n-static int apply_fragments(struct image *img, struct patch *patch)\n+static int apply_fragments(struct apply_state *state, struct image *img, struct patch *patch)\n {\n \tstruct fragment *frag = patch->fragments;\n \tconst char *name = patch->old_name ? patch->old_name : patch->new_name;\n@@ -3080,7 +3083,7 @@ static int apply_fragments(struct image *img, struct patch *patch)\n \n \twhile (frag) {\n \t\tnth++;\n-\t\tif (apply_one_fragment(img, frag, inaccurate_eof, ws_rule, nth)) {\n+\t\tif (apply_one_fragment(state, img, frag, inaccurate_eof, ws_rule, nth)) {\n \t\t\terror(_(\"patch failed: %s:%ld\"), name, frag->oldpos);\n \t\t\tif (!apply_with_reject)\n \t\t\t\treturn -1;\n@@ -3388,8 +3391,11 @@ static int load_current(struct image *image, struct patch *patch)\n \treturn 0;\n }\n \n-static int try_threeway(struct image *image, struct patch *patch,\n-\t\t\tstruct stat *st, const struct cache_entry *ce)\n+static int try_threeway(struct apply_state *state,\n+\t\t\tstruct image *image,\n+\t\t\tstruct patch *patch,\n+\t\t\tstruct stat *st,\n+\t\t\tconst struct cache_entry *ce)\n {\n \tunsigned char pre_sha1[20], post_sha1[20], our_sha1[20];\n \tstruct strbuf buf = STRBUF_INIT;\n@@ -3415,7 +3421,7 @@ static int try_threeway(struct image *image, struct patch *patch,\n \timg = strbuf_detach(&buf, &len);\n \tprepare_image(&tmp_image, img, len, 1);\n \t/* Apply the patch to get the post image */\n-\tif (apply_fragments(&tmp_image, patch) < 0) {\n+\tif (apply_fragments(state, &tmp_image, patch) < 0) {\n \t\tclear_image(&tmp_image);\n \t\treturn -1;\n \t}\n@@ -3459,7 +3465,8 @@ static int try_threeway(struct image *image, struct patch *patch,\n \treturn 0;\n }\n \n-static int apply_data(struct patch *patch, struct stat *st, const struct cache_entry *ce)\n+static int apply_data(struct apply_state *state, struct patch *patch,\n+\t\t      struct stat *st, const struct cache_entry *ce)\n {\n \tstruct image image;\n \n@@ -3467,9 +3474,9 @@ static int apply_data(struct patch *patch, struct stat *st, const struct cache_e\n \t\treturn -1;\n \n \tif (patch->direct_to_threeway ||\n-\t    apply_fragments(&image, patch) < 0) {\n+\t    apply_fragments(state, &image, patch) < 0) {\n \t\t/* Note: with --reject, apply_fragments() returns 0 */\n-\t\tif (!threeway || try_threeway(&image, patch, st, ce) < 0)\n+\t\tif (!threeway || try_threeway(state, &image, patch, st, ce) < 0)\n \t\t\treturn -1;\n \t}\n \tpatch->result = image.buf;\n@@ -3717,7 +3724,7 @@ static void die_on_unsafe_path(struct patch *patch)\n  * Check and apply the patch in-core; leave the result in patch->result\n  * for the caller to write it out to the final destination.\n  */\n-static int check_patch(struct patch *patch)\n+static int check_patch(struct apply_state *state, struct patch *patch)\n {\n \tstruct stat st;\n \tconst char *old_name = patch->old_name;\n@@ -3816,13 +3823,13 @@ static int check_patch(struct patch *patch)\n \t\treturn error(_(\"affected file '%s' is beyond a symbolic link\"),\n \t\t\t     patch->new_name);\n \n-\tif (apply_data(patch, &st, ce) < 0)\n+\tif (apply_data(state, patch, &st, ce) < 0)\n \t\treturn error(_(\"%s: patch does not apply\"), name);\n \tpatch->rejected = 0;\n \treturn 0;\n }\n \n-static int check_patch_list(struct patch *patch)\n+static int check_patch_list(struct apply_state *state, struct patch *patch)\n {\n \tint err = 0;\n \n@@ -3832,7 +3839,7 @@ static int check_patch_list(struct patch *patch)\n \t\tif (apply_verbosely)\n \t\t\tsay_patch_name(stderr,\n \t\t\t\t       _(\"Checking patch %s...\"), patch);\n-\t\terr |= check_patch(patch);\n+\t\terr |= check_patch(state, patch);\n \t\tpatch = patch->next;\n \t}\n \treturn err;\n@@ -4434,7 +4441,7 @@ static int apply_patch(struct apply_state *state,\n \t}\n \n \tif ((check || apply) &&\n-\t    check_patch_list(list) < 0 &&\n+\t    check_patch_list(state, list) < 0 &&\n \t    !apply_with_reject)\n \t\texit(1);\n \n@@ -4602,7 +4609,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t\t\tPARSE_OPT_NOARG, option_parse_space_change },\n \t\tOPT_BOOL('R', \"reverse\", &apply_in_reverse,\n \t\t\tN_(\"apply the patch in reverse\")),\n-\t\tOPT_BOOL(0, \"unidiff-zero\", &unidiff_zero,\n+\t\tOPT_BOOL(0, \"unidiff-zero\", &state.unidiff_zero,\n \t\t\tN_(\"don't expect at least one line of context\")),\n \t\tOPT_BOOL(0, \"reject\", &apply_with_reject,\n \t\t\tN_(\"leave the rejected hunks in corresponding *.rej files\")),\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287353","messageId":"20160524081126.16973-12-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 11/49] builtin/apply: move 'check' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:10:48Z","receivedAt":"2016-05-24T08:10:48Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify the apply functionality the 'check' variable should\nnot be static and global to the file. Let's move it into\n'struct apply_state'.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 14 +++++++-------\n 1 file changed, 7 insertions(+), 7 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 6c36898..55a5541 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -25,13 +25,14 @@ struct apply_state {\n \tconst char *prefix;\n \tint prefix_length;\n \n+\t/* These control what gets looked at and modified */\n+\tint check; /* preimage must match working tree, don't actually apply */\n+\n \t/* These boolean parameters control how the apply is done */\n \tint unidiff_zero;\n };\n \n /*\n- *  --check turns on checking that the working tree matches the\n- *    files that are being modified, but doesn't apply the patch\n  *  --stat does just a diffstat, and doesn't actually apply\n  *  --numstat does numeric diffstat, and doesn't actually apply\n  *  --index-info shows the old and new index info for paths if available.\n@@ -48,7 +49,6 @@ static int cached;\n static int diffstat;\n static int numstat;\n static int summary;\n-static int check;\n static int apply = 1;\n static int apply_in_reverse;\n static int apply_with_reject;\n@@ -2053,7 +2053,7 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si\n \t\t * without metadata change.  A binary patch appears\n \t\t * empty to us here.\n \t\t */\n-\t\tif ((apply || check) &&\n+\t\tif ((apply || state->check) &&\n \t\t    (!patch->is_binary && !metadata_changes(patch)))\n \t\t\tdie(_(\"patch with only garbage at line %d\"), state_linenr);\n \t}\n@@ -4440,7 +4440,7 @@ static int apply_patch(struct apply_state *state,\n \t\t\tdie(_(\"unable to read index file\"));\n \t}\n \n-\tif ((check || apply) &&\n+\tif ((state->check || apply) &&\n \t    check_patch_list(state, list) < 0 &&\n \t    !apply_with_reject)\n \t\texit(1);\n@@ -4579,7 +4579,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t\t\tN_(\"show number of added and deleted lines in decimal notation\")),\n \t\tOPT_BOOL(0, \"summary\", &summary,\n \t\t\tN_(\"instead of applying the patch, output a summary for the input\")),\n-\t\tOPT_BOOL(0, \"check\", &check,\n+\t\tOPT_BOOL(0, \"check\", &state.check,\n \t\t\tN_(\"instead of applying the patch, see if the patch is applicable\")),\n \t\tOPT_BOOL(0, \"index\", &check_index,\n \t\t\tN_(\"make sure the patch is applicable to the current index\")),\n@@ -4644,7 +4644,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t}\n \tif (apply_with_reject)\n \t\tapply = apply_verbosely = 1;\n-\tif (!force_apply && (diffstat || numstat || summary || check || fake_ancestor))\n+\tif (!force_apply && (diffstat || numstat || summary || state.check || fake_ancestor))\n \t\tapply = 0;\n \tif (check_index && is_not_gitdir)\n \t\tdie(_(\"--index outside a repository\"));\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287355","messageId":"20160524081126.16973-13-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 12/49] builtin/apply: move 'check_index' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:10:49Z","receivedAt":"2016-05-24T08:10:49Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify the apply functionality the 'check_index' variable should\nnot be static and global to the file. Let's move it into\n'struct apply_state'.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 66 ++++++++++++++++++++++++++++++++-------------------------\n 1 file changed, 37 insertions(+), 29 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 55a5541..769383c 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -27,6 +27,7 @@ struct apply_state {\n \n \t/* These control what gets looked at and modified */\n \tint check; /* preimage must match working tree, don't actually apply */\n+\tint check_index; /* preimage must match the indexed version */\n \n \t/* These boolean parameters control how the apply is done */\n \tint unidiff_zero;\n@@ -36,14 +37,12 @@ struct apply_state {\n  *  --stat does just a diffstat, and doesn't actually apply\n  *  --numstat does numeric diffstat, and doesn't actually apply\n  *  --index-info shows the old and new index info for paths if available.\n- *  --index updates the cache as well.\n  *  --cached updates only the cache without ever touching the working tree.\n  */\n static int newfd = -1;\n \n static int state_p_value = 1;\n static int p_value_known;\n-static int check_index;\n static int update_index;\n static int cached;\n static int diffstat;\n@@ -3245,13 +3244,14 @@ static int verify_index_match(const struct cache_entry *ce, struct stat *st)\n \n #define SUBMODULE_PATCH_WITHOUT_INDEX 1\n \n-static int load_patch_target(struct strbuf *buf,\n+static int load_patch_target(struct apply_state *state,\n+\t\t\t     struct strbuf *buf,\n \t\t\t     const struct cache_entry *ce,\n \t\t\t     struct stat *st,\n \t\t\t     const char *name,\n \t\t\t     unsigned expected_mode)\n {\n-\tif (cached || check_index) {\n+\tif (cached || state->check_index) {\n \t\tif (read_file_or_gitlink(ce, buf))\n \t\t\treturn error(_(\"read of %s failed\"), name);\n \t} else if (name) {\n@@ -3277,7 +3277,8 @@ static int load_patch_target(struct strbuf *buf,\n  * applying a non-git patch that incrementally updates the tree,\n  * we read from the result of a previous diff.\n  */\n-static int load_preimage(struct image *image,\n+static int load_preimage(struct apply_state *state,\n+\t\t\t struct image *image,\n \t\t\t struct patch *patch, struct stat *st,\n \t\t\t const struct cache_entry *ce)\n {\n@@ -3295,7 +3296,7 @@ static int load_preimage(struct image *image,\n \t\t/* We have a patched copy in memory; use that. */\n \t\tstrbuf_add(&buf, previous->result, previous->resultsize);\n \t} else {\n-\t\tstatus = load_patch_target(&buf, ce, st,\n+\t\tstatus = load_patch_target(state, &buf, ce, st,\n \t\t\t\t\t   patch->old_name, patch->old_mode);\n \t\tif (status < 0)\n \t\t\treturn status;\n@@ -3354,7 +3355,9 @@ static int three_way_merge(struct image *image,\n  * the current contents of the new_name.  In no cases other than that\n  * this function will be called.\n  */\n-static int load_current(struct image *image, struct patch *patch)\n+static int load_current(struct apply_state *state,\n+\t\t\tstruct image *image,\n+\t\t\tstruct patch *patch)\n {\n \tstruct strbuf buf = STRBUF_INIT;\n \tint status, pos;\n@@ -3381,7 +3384,7 @@ static int load_current(struct image *image, struct patch *patch)\n \tif (verify_index_match(ce, &st))\n \t\treturn error(_(\"%s: does not match index\"), name);\n \n-\tstatus = load_patch_target(&buf, ce, &st, name, mode);\n+\tstatus = load_patch_target(state, &buf, ce, &st, name, mode);\n \tif (status < 0)\n \t\treturn status;\n \telse if (status)\n@@ -3431,11 +3434,11 @@ static int try_threeway(struct apply_state *state,\n \n \t/* our_sha1[] is ours */\n \tif (patch->is_new) {\n-\t\tif (load_current(&tmp_image, patch))\n+\t\tif (load_current(state, &tmp_image, patch))\n \t\t\treturn error(\"cannot read the current contents of '%s'\",\n \t\t\t\t     patch->new_name);\n \t} else {\n-\t\tif (load_preimage(&tmp_image, patch, st, ce))\n+\t\tif (load_preimage(state, &tmp_image, patch, st, ce))\n \t\t\treturn error(\"cannot read the current contents of '%s'\",\n \t\t\t\t     patch->old_name);\n \t}\n@@ -3470,7 +3473,7 @@ static int apply_data(struct apply_state *state, struct patch *patch,\n {\n \tstruct image image;\n \n-\tif (load_preimage(&image, patch, st, ce) < 0)\n+\tif (load_preimage(state, &image, patch, st, ce) < 0)\n \t\treturn -1;\n \n \tif (patch->direct_to_threeway ||\n@@ -3501,7 +3504,10 @@ static int apply_data(struct apply_state *state, struct patch *patch,\n  * check_patch() separately makes sure (and errors out otherwise) that\n  * the path the patch creates does not exist in the current tree.\n  */\n-static int check_preimage(struct patch *patch, struct cache_entry **ce, struct stat *st)\n+static int check_preimage(struct apply_state *state,\n+\t\t\t  struct patch *patch,\n+\t\t\t  struct cache_entry **ce,\n+\t\t\t  struct stat *st)\n {\n \tconst char *old_name = patch->old_name;\n \tstruct patch *previous = NULL;\n@@ -3524,7 +3530,7 @@ static int check_preimage(struct patch *patch, struct cache_entry **ce, struct s\n \t\t\treturn error(_(\"%s: %s\"), old_name, strerror(errno));\n \t}\n \n-\tif (check_index && !previous) {\n+\tif (state->check_index && !previous) {\n \t\tint pos = cache_name_pos(old_name, strlen(old_name));\n \t\tif (pos < 0) {\n \t\t\tif (patch->is_new < 0)\n@@ -3574,11 +3580,13 @@ static int check_preimage(struct patch *patch, struct cache_entry **ce, struct s\n #define EXISTS_IN_INDEX 1\n #define EXISTS_IN_WORKTREE 2\n \n-static int check_to_create(const char *new_name, int ok_if_exists)\n+static int check_to_create(struct apply_state *state,\n+\t\t\t   const char *new_name,\n+\t\t\t   int ok_if_exists)\n {\n \tstruct stat nst;\n \n-\tif (check_index &&\n+\tif (state->check_index &&\n \t    cache_name_pos(new_name, strlen(new_name)) >= 0 &&\n \t    !ok_if_exists)\n \t\treturn EXISTS_IN_INDEX;\n@@ -3654,7 +3662,7 @@ static void prepare_symlink_changes(struct patch *patch)\n \t}\n }\n \n-static int path_is_beyond_symlink_1(struct strbuf *name)\n+static int path_is_beyond_symlink_1(struct apply_state *state, struct strbuf *name)\n {\n \tdo {\n \t\tunsigned int change;\n@@ -3675,7 +3683,7 @@ static int path_is_beyond_symlink_1(struct strbuf *name)\n \t\t\tcontinue;\n \n \t\t/* otherwise, check the preimage */\n-\t\tif (check_index) {\n+\t\tif (state->check_index) {\n \t\t\tstruct cache_entry *ce;\n \n \t\t\tce = cache_file_exists(name->buf, name->len, ignore_case);\n@@ -3690,14 +3698,14 @@ static int path_is_beyond_symlink_1(struct strbuf *name)\n \treturn 0;\n }\n \n-static int path_is_beyond_symlink(const char *name_)\n+static int path_is_beyond_symlink(struct apply_state *state, const char *name_)\n {\n \tint ret;\n \tstruct strbuf name = STRBUF_INIT;\n \n \tassert(*name_ != '\\0');\n \tstrbuf_addstr(&name, name_);\n-\tret = path_is_beyond_symlink_1(&name);\n+\tret = path_is_beyond_symlink_1(state, &name);\n \tstrbuf_release(&name);\n \n \treturn ret;\n@@ -3737,7 +3745,7 @@ static int check_patch(struct apply_state *state, struct patch *patch)\n \n \tpatch->rejected = 1; /* we will drop this after we succeed */\n \n-\tstatus = check_preimage(patch, &ce, &st);\n+\tstatus = check_preimage(state, patch, &ce, &st);\n \tif (status)\n \t\treturn status;\n \told_name = patch->old_name;\n@@ -3764,7 +3772,7 @@ static int check_patch(struct apply_state *state, struct patch *patch)\n \n \tif (new_name &&\n \t    ((0 < patch->is_new) || patch->is_rename || patch->is_copy)) {\n-\t\tint err = check_to_create(new_name, ok_if_exists);\n+\t\tint err = check_to_create(state, new_name, ok_if_exists);\n \n \t\tif (err && threeway) {\n \t\t\tpatch->direct_to_threeway = 1;\n@@ -3819,7 +3827,7 @@ static int check_patch(struct apply_state *state, struct patch *patch)\n \t * is not deposited to a path that is beyond a symbolic link\n \t * here.\n \t */\n-\tif (!patch->is_delete && path_is_beyond_symlink(patch->new_name))\n+\tif (!patch->is_delete && path_is_beyond_symlink(state, patch->new_name))\n \t\treturn error(_(\"affected file '%s' is beyond a symbolic link\"),\n \t\t\t     patch->new_name);\n \n@@ -4431,11 +4439,11 @@ static int apply_patch(struct apply_state *state,\n \tif (whitespace_error && (ws_error_action == die_on_ws_error))\n \t\tapply = 0;\n \n-\tupdate_index = check_index && apply;\n+\tupdate_index = state->check_index && apply;\n \tif (update_index && newfd < 0)\n \t\tnewfd = hold_locked_index(&lock_file, 1);\n \n-\tif (check_index) {\n+\tif (state->check_index) {\n \t\tif (read_cache() < 0)\n \t\t\tdie(_(\"unable to read index file\"));\n \t}\n@@ -4581,7 +4589,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t\t\tN_(\"instead of applying the patch, output a summary for the input\")),\n \t\tOPT_BOOL(0, \"check\", &state.check,\n \t\t\tN_(\"instead of applying the patch, see if the patch is applicable\")),\n-\t\tOPT_BOOL(0, \"index\", &check_index,\n+\t\tOPT_BOOL(0, \"index\", &state.check_index,\n \t\t\tN_(\"make sure the patch is applicable to the current index\")),\n \t\tOPT_BOOL(0, \"cached\", &cached,\n \t\t\tN_(\"apply a patch without touching the working tree\")),\n@@ -4640,20 +4648,20 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \tif (threeway) {\n \t\tif (is_not_gitdir)\n \t\t\tdie(_(\"--3way outside a repository\"));\n-\t\tcheck_index = 1;\n+\t\tstate.check_index = 1;\n \t}\n \tif (apply_with_reject)\n \t\tapply = apply_verbosely = 1;\n \tif (!force_apply && (diffstat || numstat || summary || state.check || fake_ancestor))\n \t\tapply = 0;\n-\tif (check_index && is_not_gitdir)\n+\tif (state.check_index && is_not_gitdir)\n \t\tdie(_(\"--index outside a repository\"));\n \tif (cached) {\n \t\tif (is_not_gitdir)\n \t\t\tdie(_(\"--cached outside a repository\"));\n-\t\tcheck_index = 1;\n+\t\tstate.check_index = 1;\n \t}\n-\tif (check_index)\n+\tif (state.check_index)\n \t\tunsafe_paths = 0;\n \n \tfor (i = 0; i < argc; i++) {\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287354","messageId":"20160524081126.16973-14-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 13/49] builtin/apply: move 'apply_in_reverse' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:10:50Z","receivedAt":"2016-05-24T08:10:50Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify the apply functionality the 'apply_in_reverse' variable should\nnot be static and global to the file. Let's move it into\n'struct apply_state'.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 50 +++++++++++++++++++++++++++++++-------------------\n 1 file changed, 31 insertions(+), 19 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 769383c..796d990 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -30,6 +30,7 @@ struct apply_state {\n \tint check_index; /* preimage must match the indexed version */\n \n \t/* These boolean parameters control how the apply is done */\n+\tint apply_in_reverse;\n \tint unidiff_zero;\n };\n \n@@ -49,7 +50,6 @@ static int diffstat;\n static int numstat;\n static int summary;\n static int apply = 1;\n-static int apply_in_reverse;\n static int apply_with_reject;\n static int apply_verbosely;\n static int allow_overlap;\n@@ -1556,8 +1556,11 @@ static void check_whitespace(const char *line, int len, unsigned ws_rule)\n  * between a \"---\" that is part of a patch, and a \"---\" that starts\n  * the next patch is to look at the line counts..\n  */\n-static int parse_fragment(const char *line, unsigned long size,\n-\t\t\t  struct patch *patch, struct fragment *fragment)\n+static int parse_fragment(struct apply_state *state,\n+\t\t\t  const char *line,\n+\t\t\t  unsigned long size,\n+\t\t\t  struct patch *patch,\n+\t\t\t  struct fragment *fragment)\n {\n \tint added, deleted;\n \tint len = linelen(line, size), offset;\n@@ -1597,12 +1600,12 @@ static int parse_fragment(const char *line, unsigned long size,\n \t\t\tif (!deleted && !added)\n \t\t\t\tleading++;\n \t\t\ttrailing++;\n-\t\t\tif (!apply_in_reverse &&\n+\t\t\tif (!state->apply_in_reverse &&\n \t\t\t    ws_error_action == correct_ws_error)\n \t\t\t\tcheck_whitespace(line, len, patch->ws_rule);\n \t\t\tbreak;\n \t\tcase '-':\n-\t\t\tif (apply_in_reverse &&\n+\t\t\tif (state->apply_in_reverse &&\n \t\t\t    ws_error_action != nowarn_ws_error)\n \t\t\t\tcheck_whitespace(line, len, patch->ws_rule);\n \t\t\tdeleted++;\n@@ -1610,7 +1613,7 @@ static int parse_fragment(const char *line, unsigned long size,\n \t\t\ttrailing = 0;\n \t\t\tbreak;\n \t\tcase '+':\n-\t\t\tif (!apply_in_reverse &&\n+\t\t\tif (!state->apply_in_reverse &&\n \t\t\t    ws_error_action != nowarn_ws_error)\n \t\t\t\tcheck_whitespace(line, len, patch->ws_rule);\n \t\t\tadded++;\n@@ -1666,7 +1669,10 @@ static int parse_fragment(const char *line, unsigned long size,\n  * The (fragment->patch, fragment->size) pair points into the memory given\n  * by the caller, not a copy, when we return.\n  */\n-static int parse_single_patch(const char *line, unsigned long size, struct patch *patch)\n+static int parse_single_patch(struct apply_state *state,\n+\t\t\t      const char *line,\n+\t\t\t      unsigned long size,\n+\t\t\t      struct patch *patch)\n {\n \tunsigned long offset = 0;\n \tunsigned long oldlines = 0, newlines = 0, context = 0;\n@@ -1678,7 +1684,7 @@ static int parse_single_patch(const char *line, unsigned long size, struct patch\n \n \t\tfragment = xcalloc(1, sizeof(*fragment));\n \t\tfragment->linenr = state_linenr;\n-\t\tlen = parse_fragment(line, size, patch, fragment);\n+\t\tlen = parse_fragment(state, line, size, patch, fragment);\n \t\tif (len <= 0)\n \t\t\tdie(_(\"corrupt patch at line %d\"), state_linenr);\n \t\tfragment->patch = line;\n@@ -2008,8 +2014,10 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si\n \t\t\t\t\t\t ? patch->new_name\n \t\t\t\t\t\t : patch->old_name);\n \n-\tpatchsize = parse_single_patch(buffer + offset + hdrsize,\n-\t\t\t\t       size - offset - hdrsize, patch);\n+\tpatchsize = parse_single_patch(state,\n+\t\t\t\t       buffer + offset + hdrsize,\n+\t\t\t\t       size - offset - hdrsize,\n+\t\t\t\t       patch);\n \n \tif (!patchsize) {\n \t\tstatic const char git_binary[] = \"GIT binary patch\\n\";\n@@ -2741,7 +2749,7 @@ static int apply_one_fragment(struct apply_state *state,\n \t\tif (len < size && patch[len] == '\\\\')\n \t\t\tplen--;\n \t\tfirst = *patch;\n-\t\tif (apply_in_reverse) {\n+\t\tif (state->apply_in_reverse) {\n \t\t\tif (first == '-')\n \t\t\t\tfirst = '+';\n \t\t\telse if (first == '+')\n@@ -2914,7 +2922,7 @@ static int apply_one_fragment(struct apply_state *state,\n \n \t\tif (apply_verbosely && applied_pos != pos) {\n \t\t\tint offset = applied_pos - pos;\n-\t\t\tif (apply_in_reverse)\n+\t\t\tif (state->apply_in_reverse)\n \t\t\t\toffset = 0 - offset;\n \t\t\tfprintf_ln(stderr,\n \t\t\t\t   Q_(\"Hunk #%d succeeded at %d (offset %d line).\",\n@@ -2948,7 +2956,9 @@ out:\n \treturn (applied_pos < 0);\n }\n \n-static int apply_binary_fragment(struct image *img, struct patch *patch)\n+static int apply_binary_fragment(struct apply_state *state,\n+\t\t\t\t struct image *img,\n+\t\t\t\t struct patch *patch)\n {\n \tstruct fragment *fragment = patch->fragments;\n \tunsigned long len;\n@@ -2961,7 +2971,7 @@ static int apply_binary_fragment(struct image *img, struct patch *patch)\n \t\t\t     patch->old_name);\n \n \t/* Binary patch is irreversible without the optional second hunk */\n-\tif (apply_in_reverse) {\n+\tif (state->apply_in_reverse) {\n \t\tif (!fragment->next)\n \t\t\treturn error(\"cannot reverse-apply a binary patch \"\n \t\t\t\t     \"without the reverse hunk to '%s'\",\n@@ -2994,7 +3004,9 @@ static int apply_binary_fragment(struct image *img, struct patch *patch)\n  * but the preimage prepared by the caller in \"img\" is freed here\n  * or in the helper function apply_binary_fragment() this calls.\n  */\n-static int apply_binary(struct image *img, struct patch *patch)\n+static int apply_binary(struct apply_state *state,\n+\t\t\tstruct image *img,\n+\t\t\tstruct patch *patch)\n {\n \tconst char *name = patch->old_name ? patch->old_name : patch->new_name;\n \tunsigned char sha1[20];\n@@ -3055,7 +3067,7 @@ static int apply_binary(struct image *img, struct patch *patch)\n \t\t * apply the patch data to it, which is stored\n \t\t * in the patch->fragments->{patch,size}.\n \t\t */\n-\t\tif (apply_binary_fragment(img, patch))\n+\t\tif (apply_binary_fragment(state, img, patch))\n \t\t\treturn error(_(\"binary patch does not apply to '%s'\"),\n \t\t\t\t     name);\n \n@@ -3078,7 +3090,7 @@ static int apply_fragments(struct apply_state *state, struct image *img, struct\n \tint nth = 0;\n \n \tif (patch->is_binary)\n-\t\treturn apply_binary(img, patch);\n+\t\treturn apply_binary(state, img, patch);\n \n \twhile (frag) {\n \t\tnth++;\n@@ -4417,7 +4429,7 @@ static int apply_patch(struct apply_state *state,\n \t\t\tfree_patch(patch);\n \t\t\tbreak;\n \t\t}\n-\t\tif (apply_in_reverse)\n+\t\tif (state->apply_in_reverse)\n \t\t\treverse_patches(patch);\n \t\tif (use_patch(state, patch)) {\n \t\t\tpatch_stats(patch);\n@@ -4615,7 +4627,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t\t{ OPTION_CALLBACK, 0, \"ignore-whitespace\", NULL, NULL,\n \t\t\tN_(\"ignore changes in whitespace when finding context\"),\n \t\t\tPARSE_OPT_NOARG, option_parse_space_change },\n-\t\tOPT_BOOL('R', \"reverse\", &apply_in_reverse,\n+\t\tOPT_BOOL('R', \"reverse\", &state.apply_in_reverse,\n \t\t\tN_(\"apply the patch in reverse\")),\n \t\tOPT_BOOL(0, \"unidiff-zero\", &state.unidiff_zero,\n \t\t\tN_(\"don't expect at least one line of context\")),\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287384","messageId":"20160524081126.16973-15-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 14/49] builtin/apply: move 'apply_with_reject' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:10:51Z","receivedAt":"2016-05-24T08:10:51Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify the apply functionality the 'apply_with_reject' variable should\nnot be static and global to the file. Let's move it into\n'struct apply_state'.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 14 +++++++-------\n 1 file changed, 7 insertions(+), 7 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 796d990..8692e2f 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -31,6 +31,7 @@ struct apply_state {\n \n \t/* These boolean parameters control how the apply is done */\n \tint apply_in_reverse;\n+\tint apply_with_reject;\n \tint unidiff_zero;\n };\n \n@@ -50,7 +51,6 @@ static int diffstat;\n static int numstat;\n static int summary;\n static int apply = 1;\n-static int apply_with_reject;\n static int apply_verbosely;\n static int allow_overlap;\n static int no_add;\n@@ -3096,7 +3096,7 @@ static int apply_fragments(struct apply_state *state, struct image *img, struct\n \t\tnth++;\n \t\tif (apply_one_fragment(state, img, frag, inaccurate_eof, ws_rule, nth)) {\n \t\t\terror(_(\"patch failed: %s:%ld\"), name, frag->oldpos);\n-\t\t\tif (!apply_with_reject)\n+\t\t\tif (!state->apply_with_reject)\n \t\t\t\treturn -1;\n \t\t\tfrag->rejected = 1;\n \t\t}\n@@ -4462,11 +4462,11 @@ static int apply_patch(struct apply_state *state,\n \n \tif ((state->check || apply) &&\n \t    check_patch_list(state, list) < 0 &&\n-\t    !apply_with_reject)\n+\t    !state->apply_with_reject)\n \t\texit(1);\n \n \tif (apply && write_out_results(list)) {\n-\t\tif (apply_with_reject)\n+\t\tif (state->apply_with_reject)\n \t\t\texit(1);\n \t\t/* with --3way, we still need to write the index out */\n \t\treturn 1;\n@@ -4631,7 +4631,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t\t\tN_(\"apply the patch in reverse\")),\n \t\tOPT_BOOL(0, \"unidiff-zero\", &state.unidiff_zero,\n \t\t\tN_(\"don't expect at least one line of context\")),\n-\t\tOPT_BOOL(0, \"reject\", &apply_with_reject,\n+\t\tOPT_BOOL(0, \"reject\", &state.apply_with_reject,\n \t\t\tN_(\"leave the rejected hunks in corresponding *.rej files\")),\n \t\tOPT_BOOL(0, \"allow-overlap\", &allow_overlap,\n \t\t\tN_(\"allow overlapping hunks\")),\n@@ -4653,7 +4653,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \targc = parse_options(argc, argv, state.prefix, builtin_apply_options,\n \t\t\tapply_usage, 0);\n \n-\tif (apply_with_reject && threeway)\n+\tif (state.apply_with_reject && threeway)\n \t\tdie(\"--reject and --3way cannot be used together.\");\n \tif (cached && threeway)\n \t\tdie(\"--cached and --3way cannot be used together.\");\n@@ -4662,7 +4662,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t\t\tdie(_(\"--3way outside a repository\"));\n \t\tstate.check_index = 1;\n \t}\n-\tif (apply_with_reject)\n+\tif (state.apply_with_reject)\n \t\tapply = apply_verbosely = 1;\n \tif (!force_apply && (diffstat || numstat || summary || state.check || fake_ancestor))\n \t\tapply = 0;\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287387","messageId":"20160524081126.16973-16-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 15/49] builtin/apply: move 'apply_verbosely' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:10:52Z","receivedAt":"2016-05-24T08:10:52Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify the apply functionality the 'apply_verbosely' variable should\nnot be static and global to the file. Let's move it into\n'struct apply_state'.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 26 +++++++++++++-------------\n 1 file changed, 13 insertions(+), 13 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 8692e2f..07dc89d 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -32,6 +32,7 @@ struct apply_state {\n \t/* These boolean parameters control how the apply is done */\n \tint apply_in_reverse;\n \tint apply_with_reject;\n+\tint apply_verbosely;\n \tint unidiff_zero;\n };\n \n@@ -51,7 +52,6 @@ static int diffstat;\n static int numstat;\n static int summary;\n static int apply = 1;\n-static int apply_verbosely;\n static int allow_overlap;\n static int no_add;\n static int threeway;\n@@ -2805,7 +2805,7 @@ static int apply_one_fragment(struct apply_state *state,\n \t\t\t/* Ignore it, we already handled it */\n \t\t\tbreak;\n \t\tdefault:\n-\t\t\tif (apply_verbosely)\n+\t\t\tif (state->apply_verbosely)\n \t\t\t\terror(_(\"invalid start of line: '%c'\"), first);\n \t\t\tapplied_pos = -1;\n \t\t\tgoto out;\n@@ -2920,7 +2920,7 @@ static int apply_one_fragment(struct apply_state *state,\n \t\t\t\tapply = 0;\n \t\t}\n \n-\t\tif (apply_verbosely && applied_pos != pos) {\n+\t\tif (state->apply_verbosely && applied_pos != pos) {\n \t\t\tint offset = applied_pos - pos;\n \t\t\tif (state->apply_in_reverse)\n \t\t\t\toffset = 0 - offset;\n@@ -2942,7 +2942,7 @@ static int apply_one_fragment(struct apply_state *state,\n \t\t\t\t   leading, trailing, applied_pos+1);\n \t\tupdate_image(img, applied_pos, &preimage, &postimage);\n \t} else {\n-\t\tif (apply_verbosely)\n+\t\tif (state->apply_verbosely)\n \t\t\terror(_(\"while searching for:\\n%.*s\"),\n \t\t\t      (int)(old - oldlines), oldlines);\n \t}\n@@ -3856,7 +3856,7 @@ static int check_patch_list(struct apply_state *state, struct patch *patch)\n \tprepare_symlink_changes(patch);\n \tprepare_fn_table(patch);\n \twhile (patch) {\n-\t\tif (apply_verbosely)\n+\t\tif (state->apply_verbosely)\n \t\t\tsay_patch_name(stderr,\n \t\t\t\t       _(\"Checking patch %s...\"), patch);\n \t\terr |= check_patch(state, patch);\n@@ -4287,7 +4287,7 @@ static void write_out_one_result(struct patch *patch, int phase)\n \t\tcreate_file(patch);\n }\n \n-static int write_out_one_reject(struct patch *patch)\n+static int write_out_one_reject(struct apply_state *state, struct patch *patch)\n {\n \tFILE *rej;\n \tchar namebuf[PATH_MAX];\n@@ -4302,7 +4302,7 @@ static int write_out_one_reject(struct patch *patch)\n \t}\n \n \tif (!cnt) {\n-\t\tif (apply_verbosely)\n+\t\tif (state->apply_verbosely)\n \t\t\tsay_patch_name(stderr,\n \t\t\t\t       _(\"Applied patch %s cleanly.\"), patch);\n \t\treturn 0;\n@@ -4358,7 +4358,7 @@ static int write_out_one_reject(struct patch *patch)\n \treturn -1;\n }\n \n-static int write_out_results(struct patch *list)\n+static int write_out_results(struct apply_state *state, struct patch *list)\n {\n \tint phase;\n \tint errs = 0;\n@@ -4373,7 +4373,7 @@ static int write_out_results(struct patch *list)\n \t\t\telse {\n \t\t\t\twrite_out_one_result(l, phase);\n \t\t\t\tif (phase == 1) {\n-\t\t\t\t\tif (write_out_one_reject(l))\n+\t\t\t\t\tif (write_out_one_reject(state, l))\n \t\t\t\t\t\terrs = 1;\n \t\t\t\t\tif (l->conflicted_threeway) {\n \t\t\t\t\t\tstring_list_append(&cpath, l->new_name);\n@@ -4437,7 +4437,7 @@ static int apply_patch(struct apply_state *state,\n \t\t\tlistp = &patch->next;\n \t\t}\n \t\telse {\n-\t\t\tif (apply_verbosely)\n+\t\t\tif (state->apply_verbosely)\n \t\t\t\tsay_patch_name(stderr, _(\"Skipped patch '%s'.\"), patch);\n \t\t\tfree_patch(patch);\n \t\t\tskipped_patch++;\n@@ -4465,7 +4465,7 @@ static int apply_patch(struct apply_state *state,\n \t    !state->apply_with_reject)\n \t\texit(1);\n \n-\tif (apply && write_out_results(list)) {\n+\tif (apply && write_out_results(state, list)) {\n \t\tif (state->apply_with_reject)\n \t\t\texit(1);\n \t\t/* with --3way, we still need to write the index out */\n@@ -4635,7 +4635,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t\t\tN_(\"leave the rejected hunks in corresponding *.rej files\")),\n \t\tOPT_BOOL(0, \"allow-overlap\", &allow_overlap,\n \t\t\tN_(\"allow overlapping hunks\")),\n-\t\tOPT__VERBOSE(&apply_verbosely, N_(\"be verbose\")),\n+\t\tOPT__VERBOSE(&state.apply_verbosely, N_(\"be verbose\")),\n \t\tOPT_BIT(0, \"inaccurate-eof\", &options,\n \t\t\tN_(\"tolerate incorrectly detected missing new-line at the end of file\"),\n \t\t\tINACCURATE_EOF),\n@@ -4663,7 +4663,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t\tstate.check_index = 1;\n \t}\n \tif (state.apply_with_reject)\n-\t\tapply = apply_verbosely = 1;\n+\t\tapply = state.apply_verbosely = 1;\n \tif (!force_apply && (diffstat || numstat || summary || state.check || fake_ancestor))\n \t\tapply = 0;\n \tif (state.check_index && is_not_gitdir)\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287385","messageId":"20160524081126.16973-17-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 16/49] builtin/apply: move 'update_index' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:10:53Z","receivedAt":"2016-05-24T08:10:53Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify the apply functionality the 'update_index' variable should\nnot be static and global to the file. Let's move it into\n'struct apply_state'.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 45 ++++++++++++++++++++++++++-------------------\n 1 file changed, 26 insertions(+), 19 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 07dc89d..7b5abb9 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -28,6 +28,7 @@ struct apply_state {\n \t/* These control what gets looked at and modified */\n \tint check; /* preimage must match working tree, don't actually apply */\n \tint check_index; /* preimage must match the indexed version */\n+\tint update_index; /* check_index && apply */\n \n \t/* These boolean parameters control how the apply is done */\n \tint apply_in_reverse;\n@@ -46,7 +47,6 @@ static int newfd = -1;\n \n static int state_p_value = 1;\n static int p_value_known;\n-static int update_index;\n static int cached;\n static int diffstat;\n static int numstat;\n@@ -4090,9 +4090,9 @@ static void patch_stats(struct patch *patch)\n \t}\n }\n \n-static void remove_file(struct patch *patch, int rmdir_empty)\n+static void remove_file(struct apply_state *state, struct patch *patch, int rmdir_empty)\n {\n-\tif (update_index) {\n+\tif (state->update_index) {\n \t\tif (remove_file_from_cache(patch->old_name) < 0)\n \t\t\tdie(_(\"unable to remove %s from index\"), patch->old_name);\n \t}\n@@ -4103,14 +4103,18 @@ static void remove_file(struct patch *patch, int rmdir_empty)\n \t}\n }\n \n-static void add_index_file(const char *path, unsigned mode, void *buf, unsigned long size)\n+static void add_index_file(struct apply_state *state,\n+\t\t\t   const char *path,\n+\t\t\t   unsigned mode,\n+\t\t\t   void *buf,\n+\t\t\t   unsigned long size)\n {\n \tstruct stat st;\n \tstruct cache_entry *ce;\n \tint namelen = strlen(path);\n \tunsigned ce_size = cache_entry_size(namelen);\n \n-\tif (!update_index)\n+\tif (!state->update_index)\n \t\treturn;\n \n \tce = xcalloc(1, ce_size);\n@@ -4220,13 +4224,14 @@ static void create_one_file(char *path, unsigned mode, const char *buf, unsigned\n \tdie_errno(_(\"unable to write file '%s' mode %o\"), path, mode);\n }\n \n-static void add_conflicted_stages_file(struct patch *patch)\n+static void add_conflicted_stages_file(struct apply_state *state,\n+\t\t\t\t       struct patch *patch)\n {\n \tint stage, namelen;\n \tunsigned ce_size, mode;\n \tstruct cache_entry *ce;\n \n-\tif (!update_index)\n+\tif (!state->update_index)\n \t\treturn;\n \tnamelen = strlen(patch->new_name);\n \tce_size = cache_entry_size(namelen);\n@@ -4247,7 +4252,7 @@ static void add_conflicted_stages_file(struct patch *patch)\n \t}\n }\n \n-static void create_file(struct patch *patch)\n+static void create_file(struct apply_state *state, struct patch *patch)\n {\n \tchar *path = patch->new_name;\n \tunsigned mode = patch->new_mode;\n@@ -4259,22 +4264,24 @@ static void create_file(struct patch *patch)\n \tcreate_one_file(path, mode, buf, size);\n \n \tif (patch->conflicted_threeway)\n-\t\tadd_conflicted_stages_file(patch);\n+\t\tadd_conflicted_stages_file(state, patch);\n \telse\n-\t\tadd_index_file(path, mode, buf, size);\n+\t\tadd_index_file(state, path, mode, buf, size);\n }\n \n /* phase zero is to remove, phase one is to create */\n-static void write_out_one_result(struct patch *patch, int phase)\n+static void write_out_one_result(struct apply_state *state,\n+\t\t\t\t struct patch *patch,\n+\t\t\t\t int phase)\n {\n \tif (patch->is_delete > 0) {\n \t\tif (phase == 0)\n-\t\t\tremove_file(patch, 1);\n+\t\t\tremove_file(state, patch, 1);\n \t\treturn;\n \t}\n \tif (patch->is_new > 0 || patch->is_copy) {\n \t\tif (phase == 1)\n-\t\t\tcreate_file(patch);\n+\t\t\tcreate_file(state, patch);\n \t\treturn;\n \t}\n \t/*\n@@ -4282,9 +4289,9 @@ static void write_out_one_result(struct patch *patch, int phase)\n \t * thing: remove the old, write the new\n \t */\n \tif (phase == 0)\n-\t\tremove_file(patch, patch->is_rename);\n+\t\tremove_file(state, patch, patch->is_rename);\n \tif (phase == 1)\n-\t\tcreate_file(patch);\n+\t\tcreate_file(state, patch);\n }\n \n static int write_out_one_reject(struct apply_state *state, struct patch *patch)\n@@ -4371,7 +4378,7 @@ static int write_out_results(struct apply_state *state, struct patch *list)\n \t\t\tif (l->rejected)\n \t\t\t\terrs = 1;\n \t\t\telse {\n-\t\t\t\twrite_out_one_result(l, phase);\n+\t\t\t\twrite_out_one_result(state, l, phase);\n \t\t\t\tif (phase == 1) {\n \t\t\t\t\tif (write_out_one_reject(state, l))\n \t\t\t\t\t\terrs = 1;\n@@ -4451,8 +4458,8 @@ static int apply_patch(struct apply_state *state,\n \tif (whitespace_error && (ws_error_action == die_on_ws_error))\n \t\tapply = 0;\n \n-\tupdate_index = state->check_index && apply;\n-\tif (update_index && newfd < 0)\n+\tstate->update_index = state->check_index && apply;\n+\tif (state->update_index && newfd < 0)\n \t\tnewfd = hold_locked_index(&lock_file, 1);\n \n \tif (state->check_index) {\n@@ -4727,7 +4734,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t\t\t\twhitespace_error);\n \t}\n \n-\tif (update_index) {\n+\tif (state.update_index) {\n \t\tif (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))\n \t\t\tdie(_(\"Unable to write new index file\"));\n \t}\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287393","messageId":"20160524081126.16973-18-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 17/49] builtin/apply: move 'allow_overlap' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:10:54Z","receivedAt":"2016-05-24T08:10:54Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify the apply functionality the 'allow_overlap' variable should\nnot be static and global to the file. Let's move it into\n'struct apply_state'.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 11 ++++++-----\n 1 file changed, 6 insertions(+), 5 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 7b5abb9..ad3bec4 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -31,6 +31,7 @@ struct apply_state {\n \tint update_index; /* check_index && apply */\n \n \t/* These boolean parameters control how the apply is done */\n+\tint allow_overlap;\n \tint apply_in_reverse;\n \tint apply_with_reject;\n \tint apply_verbosely;\n@@ -52,7 +53,6 @@ static int diffstat;\n static int numstat;\n static int summary;\n static int apply = 1;\n-static int allow_overlap;\n static int no_add;\n static int threeway;\n static int unsafe_paths;\n@@ -2627,7 +2627,8 @@ static void remove_last_line(struct image *img)\n  * apply at applied_pos (counts in line numbers) in \"img\".\n  * Update \"img\" to remove \"preimage\" and replace it with \"postimage\".\n  */\n-static void update_image(struct image *img,\n+static void update_image(struct apply_state *state,\n+\t\t\t struct image *img,\n \t\t\t int applied_pos,\n \t\t\t struct image *preimage,\n \t\t\t struct image *postimage)\n@@ -2692,7 +2693,7 @@ static void update_image(struct image *img,\n \tmemcpy(img->line + applied_pos,\n \t       postimage->line,\n \t       postimage->nr * sizeof(*img->line));\n-\tif (!allow_overlap)\n+\tif (!state->allow_overlap)\n \t\tfor (i = 0; i < postimage->nr; i++)\n \t\t\timg->line[applied_pos + i].flag |= LINE_PATCHED;\n \timg->nr = nr;\n@@ -2940,7 +2941,7 @@ static int apply_one_fragment(struct apply_state *state,\n \t\t\tfprintf_ln(stderr, _(\"Context reduced to (%ld/%ld)\"\n \t\t\t\t\t     \" to apply fragment at %d\"),\n \t\t\t\t   leading, trailing, applied_pos+1);\n-\t\tupdate_image(img, applied_pos, &preimage, &postimage);\n+\t\tupdate_image(state, img, applied_pos, &preimage, &postimage);\n \t} else {\n \t\tif (state->apply_verbosely)\n \t\t\terror(_(\"while searching for:\\n%.*s\"),\n@@ -4640,7 +4641,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t\t\tN_(\"don't expect at least one line of context\")),\n \t\tOPT_BOOL(0, \"reject\", &state.apply_with_reject,\n \t\t\tN_(\"leave the rejected hunks in corresponding *.rej files\")),\n-\t\tOPT_BOOL(0, \"allow-overlap\", &allow_overlap,\n+\t\tOPT_BOOL(0, \"allow-overlap\", &state.allow_overlap,\n \t\t\tN_(\"allow overlapping hunks\")),\n \t\tOPT__VERBOSE(&state.apply_verbosely, N_(\"be verbose\")),\n \t\tOPT_BIT(0, \"inaccurate-eof\", &options,\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287389","messageId":"20160524081126.16973-19-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 18/49] builtin/apply: move 'cached' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:10:55Z","receivedAt":"2016-05-24T08:10:55Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify the apply functionality the 'cached' variable should\nnot be static and global to the file. Let's move it into\n'struct apply_state'.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 35 +++++++++++++++++++----------------\n 1 file changed, 19 insertions(+), 16 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex ad3bec4..9cba460 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -26,6 +26,7 @@ struct apply_state {\n \tint prefix_length;\n \n \t/* These control what gets looked at and modified */\n+\tint cached; /* apply to the index only */\n \tint check; /* preimage must match working tree, don't actually apply */\n \tint check_index; /* preimage must match the indexed version */\n \tint update_index; /* check_index && apply */\n@@ -42,13 +43,11 @@ struct apply_state {\n  *  --stat does just a diffstat, and doesn't actually apply\n  *  --numstat does numeric diffstat, and doesn't actually apply\n  *  --index-info shows the old and new index info for paths if available.\n- *  --cached updates only the cache without ever touching the working tree.\n  */\n static int newfd = -1;\n \n static int state_p_value = 1;\n static int p_value_known;\n-static int cached;\n static int diffstat;\n static int numstat;\n static int summary;\n@@ -3264,7 +3263,7 @@ static int load_patch_target(struct apply_state *state,\n \t\t\t     const char *name,\n \t\t\t     unsigned expected_mode)\n {\n-\tif (cached || state->check_index) {\n+\tif (state->cached || state->check_index) {\n \t\tif (read_file_or_gitlink(ce, buf))\n \t\t\treturn error(_(\"read of %s failed\"), name);\n \t} else if (name) {\n@@ -3537,7 +3536,7 @@ static int check_preimage(struct apply_state *state,\n \t\treturn error(_(\"path %s has been renamed/deleted\"), old_name);\n \tif (previous) {\n \t\tst_mode = previous->new_mode;\n-\t} else if (!cached) {\n+\t} else if (!state->cached) {\n \t\tstat_ret = lstat(old_name, st);\n \t\tif (stat_ret && errno != ENOENT)\n \t\t\treturn error(_(\"%s: %s\"), old_name, strerror(errno));\n@@ -3555,9 +3554,9 @@ static int check_preimage(struct apply_state *state,\n \t\t\tif (checkout_target(&the_index, *ce, st))\n \t\t\t\treturn -1;\n \t\t}\n-\t\tif (!cached && verify_index_match(*ce, st))\n+\t\tif (!state->cached && verify_index_match(*ce, st))\n \t\t\treturn error(_(\"%s: does not match index\"), old_name);\n-\t\tif (cached)\n+\t\tif (state->cached)\n \t\t\tst_mode = (*ce)->ce_mode;\n \t} else if (stat_ret < 0) {\n \t\tif (patch->is_new < 0)\n@@ -3565,7 +3564,7 @@ static int check_preimage(struct apply_state *state,\n \t\treturn error(_(\"%s: %s\"), old_name, strerror(errno));\n \t}\n \n-\tif (!cached && !previous)\n+\tif (!state->cached && !previous)\n \t\tst_mode = ce_mode_from_stat(*ce, st->st_mode);\n \n \tif (patch->is_new < 0)\n@@ -3603,7 +3602,7 @@ static int check_to_create(struct apply_state *state,\n \t    cache_name_pos(new_name, strlen(new_name)) >= 0 &&\n \t    !ok_if_exists)\n \t\treturn EXISTS_IN_INDEX;\n-\tif (cached)\n+\tif (state->cached)\n \t\treturn 0;\n \n \tif (!lstat(new_name, &nst)) {\n@@ -4097,7 +4096,7 @@ static void remove_file(struct apply_state *state, struct patch *patch, int rmdi\n \t\tif (remove_file_from_cache(patch->old_name) < 0)\n \t\t\tdie(_(\"unable to remove %s from index\"), patch->old_name);\n \t}\n-\tif (!cached) {\n+\tif (!state->cached) {\n \t\tif (!remove_or_warn(patch->old_mode, patch->old_name) && rmdir_empty) {\n \t\t\tremove_path(patch->old_name);\n \t\t}\n@@ -4130,7 +4129,7 @@ static void add_index_file(struct apply_state *state,\n \t\t    get_sha1_hex(s, ce->sha1))\n \t\t\tdie(_(\"corrupt patch for submodule %s\"), path);\n \t} else {\n-\t\tif (!cached) {\n+\t\tif (!state->cached) {\n \t\t\tif (lstat(path, &st) < 0)\n \t\t\t\tdie_errno(_(\"unable to stat newly created file '%s'\"),\n \t\t\t\t\t  path);\n@@ -4182,9 +4181,13 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,\n  * which is true 99% of the time anyway. If they don't,\n  * we create them and try again.\n  */\n-static void create_one_file(char *path, unsigned mode, const char *buf, unsigned long size)\n+static void create_one_file(struct apply_state *state,\n+\t\t\t    char *path,\n+\t\t\t    unsigned mode,\n+\t\t\t    const char *buf,\n+\t\t\t    unsigned long size)\n {\n-\tif (cached)\n+\tif (state->cached)\n \t\treturn;\n \tif (!try_create_file(path, mode, buf, size))\n \t\treturn;\n@@ -4262,7 +4265,7 @@ static void create_file(struct apply_state *state, struct patch *patch)\n \n \tif (!mode)\n \t\tmode = S_IFREG | 0644;\n-\tcreate_one_file(path, mode, buf, size);\n+\tcreate_one_file(state, path, mode, buf, size);\n \n \tif (patch->conflicted_threeway)\n \t\tadd_conflicted_stages_file(state, patch);\n@@ -4611,7 +4614,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t\t\tN_(\"instead of applying the patch, see if the patch is applicable\")),\n \t\tOPT_BOOL(0, \"index\", &state.check_index,\n \t\t\tN_(\"make sure the patch is applicable to the current index\")),\n-\t\tOPT_BOOL(0, \"cached\", &cached,\n+\t\tOPT_BOOL(0, \"cached\", &state.cached,\n \t\t\tN_(\"apply a patch without touching the working tree\")),\n \t\tOPT_BOOL(0, \"unsafe-paths\", &unsafe_paths,\n \t\t\tN_(\"accept a patch that touches outside the working area\")),\n@@ -4663,7 +4666,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \n \tif (state.apply_with_reject && threeway)\n \t\tdie(\"--reject and --3way cannot be used together.\");\n-\tif (cached && threeway)\n+\tif (state.cached && threeway)\n \t\tdie(\"--cached and --3way cannot be used together.\");\n \tif (threeway) {\n \t\tif (is_not_gitdir)\n@@ -4676,7 +4679,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t\tapply = 0;\n \tif (state.check_index && is_not_gitdir)\n \t\tdie(_(\"--index outside a repository\"));\n-\tif (cached) {\n+\tif (state.cached) {\n \t\tif (is_not_gitdir)\n \t\t\tdie(_(\"--cached outside a repository\"));\n \t\tstate.check_index = 1;\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287390","messageId":"20160524081126.16973-20-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 19/49] builtin/apply: move 'diffstat' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:10:56Z","receivedAt":"2016-05-24T08:10:56Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify the apply functionality the 'diffstat' variable should\nnot be static and global to the file. Let's move it into\n'struct apply_state'.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 11 ++++++-----\n 1 file changed, 6 insertions(+), 5 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 9cba460..d940125 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -31,6 +31,9 @@ struct apply_state {\n \tint check_index; /* preimage must match the indexed version */\n \tint update_index; /* check_index && apply */\n \n+\t/* These control cosmetic aspect of the output */\n+\tint diffstat; /* just show a diffstat, and don't actually apply */\n+\n \t/* These boolean parameters control how the apply is done */\n \tint allow_overlap;\n \tint apply_in_reverse;\n@@ -40,7 +43,6 @@ struct apply_state {\n };\n \n /*\n- *  --stat does just a diffstat, and doesn't actually apply\n  *  --numstat does numeric diffstat, and doesn't actually apply\n  *  --index-info shows the old and new index info for paths if available.\n  */\n@@ -48,7 +50,6 @@ static int newfd = -1;\n \n static int state_p_value = 1;\n static int p_value_known;\n-static int diffstat;\n static int numstat;\n static int summary;\n static int apply = 1;\n@@ -4486,7 +4487,7 @@ static int apply_patch(struct apply_state *state,\n \tif (fake_ancestor)\n \t\tbuild_fake_ancestor(list, fake_ancestor);\n \n-\tif (diffstat)\n+\tif (state->diffstat)\n \t\tstat_patch_list(list);\n \n \tif (numstat)\n@@ -4602,7 +4603,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t\t\t0, option_parse_p },\n \t\tOPT_BOOL(0, \"no-add\", &no_add,\n \t\t\tN_(\"ignore additions made by the patch\")),\n-\t\tOPT_BOOL(0, \"stat\", &diffstat,\n+\t\tOPT_BOOL(0, \"stat\", &state.diffstat,\n \t\t\tN_(\"instead of applying the patch, output diffstat for the input\")),\n \t\tOPT_NOOP_NOARG(0, \"allow-binary-replacement\"),\n \t\tOPT_NOOP_NOARG(0, \"binary\"),\n@@ -4675,7 +4676,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t}\n \tif (state.apply_with_reject)\n \t\tapply = state.apply_verbosely = 1;\n-\tif (!force_apply && (diffstat || numstat || summary || state.check || fake_ancestor))\n+\tif (!force_apply && (state.diffstat || numstat || summary || state.check || fake_ancestor))\n \t\tapply = 0;\n \tif (state.check_index && is_not_gitdir)\n \t\tdie(_(\"--index outside a repository\"));\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287392","messageId":"20160524081126.16973-21-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 20/49] builtin/apply: move 'numstat' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:10:57Z","receivedAt":"2016-05-24T08:10:57Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify the apply functionality the 'numstat' variable should\nnot be static and global to the file. Let's move it into\n'struct apply_state'.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 9 ++++-----\n 1 file changed, 4 insertions(+), 5 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex d940125..47a45a7 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -33,6 +33,7 @@ struct apply_state {\n \n \t/* These control cosmetic aspect of the output */\n \tint diffstat; /* just show a diffstat, and don't actually apply */\n+\tint numstat; /* just show a numeric diffstat, and don't actually apply */\n \n \t/* These boolean parameters control how the apply is done */\n \tint allow_overlap;\n@@ -43,14 +44,12 @@ struct apply_state {\n };\n \n /*\n- *  --numstat does numeric diffstat, and doesn't actually apply\n  *  --index-info shows the old and new index info for paths if available.\n  */\n static int newfd = -1;\n \n static int state_p_value = 1;\n static int p_value_known;\n-static int numstat;\n static int summary;\n static int apply = 1;\n static int no_add;\n@@ -4490,7 +4489,7 @@ static int apply_patch(struct apply_state *state,\n \tif (state->diffstat)\n \t\tstat_patch_list(list);\n \n-\tif (numstat)\n+\tif (state->numstat)\n \t\tnumstat_patch_list(list);\n \n \tif (summary)\n@@ -4607,7 +4606,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t\t\tN_(\"instead of applying the patch, output diffstat for the input\")),\n \t\tOPT_NOOP_NOARG(0, \"allow-binary-replacement\"),\n \t\tOPT_NOOP_NOARG(0, \"binary\"),\n-\t\tOPT_BOOL(0, \"numstat\", &numstat,\n+\t\tOPT_BOOL(0, \"numstat\", &state.numstat,\n \t\t\tN_(\"show number of added and deleted lines in decimal notation\")),\n \t\tOPT_BOOL(0, \"summary\", &summary,\n \t\t\tN_(\"instead of applying the patch, output a summary for the input\")),\n@@ -4676,7 +4675,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t}\n \tif (state.apply_with_reject)\n \t\tapply = state.apply_verbosely = 1;\n-\tif (!force_apply && (state.diffstat || numstat || summary || state.check || fake_ancestor))\n+\tif (!force_apply && (state.diffstat || state.numstat || summary || state.check || fake_ancestor))\n \t\tapply = 0;\n \tif (state.check_index && is_not_gitdir)\n \t\tdie(_(\"--index outside a repository\"));\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287388","messageId":"20160524081126.16973-22-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 21/49] builtin/apply: move 'summary' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:10:58Z","receivedAt":"2016-05-24T08:10:58Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify the apply functionality the 'summary' variable should\nnot be static and global to the file. Let's move it into\n'struct apply_state'.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 8 ++++----\n 1 file changed, 4 insertions(+), 4 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 47a45a7..f174a42 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -34,6 +34,7 @@ struct apply_state {\n \t/* These control cosmetic aspect of the output */\n \tint diffstat; /* just show a diffstat, and don't actually apply */\n \tint numstat; /* just show a numeric diffstat, and don't actually apply */\n+\tint summary; /* just report creation, deletion, etc, and don't actually apply */\n \n \t/* These boolean parameters control how the apply is done */\n \tint allow_overlap;\n@@ -50,7 +51,6 @@ static int newfd = -1;\n \n static int state_p_value = 1;\n static int p_value_known;\n-static int summary;\n static int apply = 1;\n static int no_add;\n static int threeway;\n@@ -4492,7 +4492,7 @@ static int apply_patch(struct apply_state *state,\n \tif (state->numstat)\n \t\tnumstat_patch_list(list);\n \n-\tif (summary)\n+\tif (state->summary)\n \t\tsummary_patch_list(list);\n \n \tfree_patch_list(list);\n@@ -4608,7 +4608,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t\tOPT_NOOP_NOARG(0, \"binary\"),\n \t\tOPT_BOOL(0, \"numstat\", &state.numstat,\n \t\t\tN_(\"show number of added and deleted lines in decimal notation\")),\n-\t\tOPT_BOOL(0, \"summary\", &summary,\n+\t\tOPT_BOOL(0, \"summary\", &state.summary,\n \t\t\tN_(\"instead of applying the patch, output a summary for the input\")),\n \t\tOPT_BOOL(0, \"check\", &state.check,\n \t\t\tN_(\"instead of applying the patch, see if the patch is applicable\")),\n@@ -4675,7 +4675,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t}\n \tif (state.apply_with_reject)\n \t\tapply = state.apply_verbosely = 1;\n-\tif (!force_apply && (state.diffstat || state.numstat || summary || state.check || fake_ancestor))\n+\tif (!force_apply && (state.diffstat || state.numstat || state.summary || state.check || fake_ancestor))\n \t\tapply = 0;\n \tif (state.check_index && is_not_gitdir)\n \t\tdie(_(\"--index outside a repository\"));\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287383","messageId":"20160524081126.16973-23-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 22/49] builtin/apply: move 'threeway' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:10:59Z","receivedAt":"2016-05-24T08:10:59Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify the apply functionality the 'threeway' variable should\nnot be static and global to the file. Let's move it into\n'struct apply_state'.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 14 +++++++-------\n 1 file changed, 7 insertions(+), 7 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex f174a42..d00017b 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -41,6 +41,7 @@ struct apply_state {\n \tint apply_in_reverse;\n \tint apply_with_reject;\n \tint apply_verbosely;\n+\tint threeway;\n \tint unidiff_zero;\n };\n \n@@ -53,7 +54,6 @@ static int state_p_value = 1;\n static int p_value_known;\n static int apply = 1;\n static int no_add;\n-static int threeway;\n static int unsafe_paths;\n static const char *fake_ancestor;\n static int line_termination = '\\n';\n@@ -3491,7 +3491,7 @@ static int apply_data(struct apply_state *state, struct patch *patch,\n \tif (patch->direct_to_threeway ||\n \t    apply_fragments(state, &image, patch) < 0) {\n \t\t/* Note: with --reject, apply_fragments() returns 0 */\n-\t\tif (!threeway || try_threeway(state, &image, patch, st, ce) < 0)\n+\t\tif (!state->threeway || try_threeway(state, &image, patch, st, ce) < 0)\n \t\t\treturn -1;\n \t}\n \tpatch->result = image.buf;\n@@ -3786,7 +3786,7 @@ static int check_patch(struct apply_state *state, struct patch *patch)\n \t    ((0 < patch->is_new) || patch->is_rename || patch->is_copy)) {\n \t\tint err = check_to_create(state, new_name, ok_if_exists);\n \n-\t\tif (err && threeway) {\n+\t\tif (err && state->threeway) {\n \t\t\tpatch->direct_to_threeway = 1;\n \t\t} else switch (err) {\n \t\tcase 0:\n@@ -4620,7 +4620,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t\t\tN_(\"accept a patch that touches outside the working area\")),\n \t\tOPT_BOOL(0, \"apply\", &force_apply,\n \t\t\tN_(\"also apply the patch (use with --stat/--summary/--check)\")),\n-\t\tOPT_BOOL('3', \"3way\", &threeway,\n+\t\tOPT_BOOL('3', \"3way\", &state.threeway,\n \t\t\t N_( \"attempt three-way merge if a patch does not apply\")),\n \t\tOPT_FILENAME(0, \"build-fake-ancestor\", &fake_ancestor,\n \t\t\tN_(\"build a temporary index based on embedded index information\")),\n@@ -4664,11 +4664,11 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \targc = parse_options(argc, argv, state.prefix, builtin_apply_options,\n \t\t\tapply_usage, 0);\n \n-\tif (state.apply_with_reject && threeway)\n+\tif (state.apply_with_reject && state.threeway)\n \t\tdie(\"--reject and --3way cannot be used together.\");\n-\tif (state.cached && threeway)\n+\tif (state.cached && state.threeway)\n \t\tdie(\"--cached and --3way cannot be used together.\");\n-\tif (threeway) {\n+\tif (state.threeway) {\n \t\tif (is_not_gitdir)\n \t\t\tdie(_(\"--3way outside a repository\"));\n \t\tstate.check_index = 1;\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287386","messageId":"20160524081126.16973-24-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 23/49] builtin/apply: move 'no_add' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:11:00Z","receivedAt":"2016-05-24T08:11:00Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify the apply functionality the 'no_add' variable should\nnot be static and global to the file. Let's move it into\n'struct apply_state'.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 6 +++---\n 1 file changed, 3 insertions(+), 3 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex d00017b..604e7bf 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -41,6 +41,7 @@ struct apply_state {\n \tint apply_in_reverse;\n \tint apply_with_reject;\n \tint apply_verbosely;\n+\tint no_add;\n \tint threeway;\n \tint unidiff_zero;\n };\n@@ -53,7 +54,6 @@ static int newfd = -1;\n static int state_p_value = 1;\n static int p_value_known;\n static int apply = 1;\n-static int no_add;\n static int unsafe_paths;\n static const char *fake_ancestor;\n static int line_termination = '\\n';\n@@ -2782,7 +2782,7 @@ static int apply_one_fragment(struct apply_state *state,\n \t\t/* Fall-through for ' ' */\n \t\tcase '+':\n \t\t\t/* --no-add does not add new lines */\n-\t\t\tif (first == '+' && no_add)\n+\t\t\tif (first == '+' && state->no_add)\n \t\t\t\tbreak;\n \n \t\t\tstart = newlines.len;\n@@ -4600,7 +4600,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t\t{ OPTION_CALLBACK, 'p', NULL, NULL, N_(\"num\"),\n \t\t\tN_(\"remove <num> leading slashes from traditional diff paths\"),\n \t\t\t0, option_parse_p },\n-\t\tOPT_BOOL(0, \"no-add\", &no_add,\n+\t\tOPT_BOOL(0, \"no-add\", &state.no_add,\n \t\t\tN_(\"ignore additions made by the patch\")),\n \t\tOPT_BOOL(0, \"stat\", &state.diffstat,\n \t\t\tN_(\"instead of applying the patch, output diffstat for the input\")),\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287380","messageId":"20160524081126.16973-25-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 24/49] builtin/apply: move 'unsafe_paths' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:11:01Z","receivedAt":"2016-05-24T08:11:01Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify the apply functionality the 'unsafe_paths' variable should\nnot be static and global to the file. Let's move it into\n'struct apply_state'.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 8 ++++----\n 1 file changed, 4 insertions(+), 4 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 604e7bf..4ef83c1 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -44,6 +44,7 @@ struct apply_state {\n \tint no_add;\n \tint threeway;\n \tint unidiff_zero;\n+\tint unsafe_paths;\n };\n \n /*\n@@ -54,7 +55,6 @@ static int newfd = -1;\n static int state_p_value = 1;\n static int p_value_known;\n static int apply = 1;\n-static int unsafe_paths;\n static const char *fake_ancestor;\n static int line_termination = '\\n';\n static unsigned int p_context = UINT_MAX;\n@@ -3827,7 +3827,7 @@ static int check_patch(struct apply_state *state, struct patch *patch)\n \t\t}\n \t}\n \n-\tif (!unsafe_paths)\n+\tif (!state->unsafe_paths)\n \t\tdie_on_unsafe_path(patch);\n \n \t/*\n@@ -4616,7 +4616,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t\t\tN_(\"make sure the patch is applicable to the current index\")),\n \t\tOPT_BOOL(0, \"cached\", &state.cached,\n \t\t\tN_(\"apply a patch without touching the working tree\")),\n-\t\tOPT_BOOL(0, \"unsafe-paths\", &unsafe_paths,\n+\t\tOPT_BOOL(0, \"unsafe-paths\", &state.unsafe_paths,\n \t\t\tN_(\"accept a patch that touches outside the working area\")),\n \t\tOPT_BOOL(0, \"apply\", &force_apply,\n \t\t\tN_(\"also apply the patch (use with --stat/--summary/--check)\")),\n@@ -4685,7 +4685,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t\tstate.check_index = 1;\n \t}\n \tif (state.check_index)\n-\t\tunsafe_paths = 0;\n+\t\tstate.unsafe_paths = 0;\n \n \tfor (i = 0; i < argc; i++) {\n \t\tconst char *arg = argv[i];\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287358","messageId":"20160524081126.16973-26-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 25/49] builtin/apply: move 'line_termination' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:11:02Z","receivedAt":"2016-05-24T08:11:02Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify the apply functionality the 'line_termination' variable should\nnot be static and global to the file. Let's move it into\n'struct apply_state'.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 14 +++++++++-----\n 1 file changed, 9 insertions(+), 5 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 4ef83c1..95cd60a 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -45,6 +45,9 @@ struct apply_state {\n \tint threeway;\n \tint unidiff_zero;\n \tint unsafe_paths;\n+\n+\t/* Other non boolean parameters */\n+\tint line_termination;\n };\n \n /*\n@@ -56,7 +59,6 @@ static int state_p_value = 1;\n static int p_value_known;\n static int apply = 1;\n static const char *fake_ancestor;\n-static int line_termination = '\\n';\n static unsigned int p_context = UINT_MAX;\n static const char * const apply_usage[] = {\n \tN_(\"git apply [<options>] [<patch>...]\"),\n@@ -3977,7 +3979,8 @@ static void stat_patch_list(struct patch *patch)\n \tprint_stat_summary(stdout, files, adds, dels);\n }\n \n-static void numstat_patch_list(struct patch *patch)\n+static void numstat_patch_list(struct apply_state *state,\n+\t\t\t       struct patch *patch)\n {\n \tfor ( ; patch; patch = patch->next) {\n \t\tconst char *name;\n@@ -3986,7 +3989,7 @@ static void numstat_patch_list(struct patch *patch)\n \t\t\tprintf(\"-\\t-\\t\");\n \t\telse\n \t\t\tprintf(\"%d\\t%d\\t\", patch->lines_added, patch->lines_deleted);\n-\t\twrite_name_quoted(name, stdout, line_termination);\n+\t\twrite_name_quoted(name, stdout, state->line_termination);\n \t}\n }\n \n@@ -4490,7 +4493,7 @@ static int apply_patch(struct apply_state *state,\n \t\tstat_patch_list(list);\n \n \tif (state->numstat)\n-\t\tnumstat_patch_list(list);\n+\t\tnumstat_patch_list(state, list);\n \n \tif (state->summary)\n \t\tsummary_patch_list(list);\n@@ -4565,6 +4568,7 @@ static void init_apply_state(struct apply_state *state, const char *prefix)\n \tmemset(state, 0, sizeof(*state));\n \tstate->prefix = prefix;\n \tstate->prefix_length = state->prefix ? strlen(state->prefix) : 0;\n+\tstate->line_termination = '\\n';\n \n \tgit_apply_config();\n \tif (apply_default_whitespace)\n@@ -4625,7 +4629,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t\tOPT_FILENAME(0, \"build-fake-ancestor\", &fake_ancestor,\n \t\t\tN_(\"build a temporary index based on embedded index information\")),\n \t\t/* Think twice before adding \"--nul\" synonym to this */\n-\t\tOPT_SET_INT('z', NULL, &line_termination,\n+\t\tOPT_SET_INT('z', NULL, &state.line_termination,\n \t\t\tN_(\"paths are separated with NUL character\"), '\\0'),\n \t\tOPT_INTEGER('C', NULL, &p_context,\n \t\t\t\tN_(\"ensure at least <n> lines of context match\")),\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287374","messageId":"20160524081126.16973-27-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 26/49] builtin/apply: move 'fake_ancestor' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:11:03Z","receivedAt":"2016-05-24T08:11:03Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify the apply functionality the 'fake_ancestor' variable should\nnot be static and global to the file. Let's move it into\n'struct apply_state'.\n\nBy the way remove a comment about '--index-info' that was renamed\n'--build-fake-ancestor' in commit 26b28007689d27a921ea90e5a29fc8eb74b0d297\n(apply: get rid of --index-info in favor of --build-fake-ancestor,\nSep 17 2007).\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 13 +++++--------\n 1 file changed, 5 insertions(+), 8 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 95cd60a..59b0f1b 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -47,18 +47,15 @@ struct apply_state {\n \tint unsafe_paths;\n \n \t/* Other non boolean parameters */\n+\tconst char *fake_ancestor;\n \tint line_termination;\n };\n \n-/*\n- *  --index-info shows the old and new index info for paths if available.\n- */\n static int newfd = -1;\n \n static int state_p_value = 1;\n static int p_value_known;\n static int apply = 1;\n-static const char *fake_ancestor;\n static unsigned int p_context = UINT_MAX;\n static const char * const apply_usage[] = {\n \tN_(\"git apply [<options>] [<patch>...]\"),\n@@ -4486,8 +4483,8 @@ static int apply_patch(struct apply_state *state,\n \t\treturn 1;\n \t}\n \n-\tif (fake_ancestor)\n-\t\tbuild_fake_ancestor(list, fake_ancestor);\n+\tif (state->fake_ancestor)\n+\t\tbuild_fake_ancestor(list, state->fake_ancestor);\n \n \tif (state->diffstat)\n \t\tstat_patch_list(list);\n@@ -4626,7 +4623,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t\t\tN_(\"also apply the patch (use with --stat/--summary/--check)\")),\n \t\tOPT_BOOL('3', \"3way\", &state.threeway,\n \t\t\t N_( \"attempt three-way merge if a patch does not apply\")),\n-\t\tOPT_FILENAME(0, \"build-fake-ancestor\", &fake_ancestor,\n+\t\tOPT_FILENAME(0, \"build-fake-ancestor\", &state.fake_ancestor,\n \t\t\tN_(\"build a temporary index based on embedded index information\")),\n \t\t/* Think twice before adding \"--nul\" synonym to this */\n \t\tOPT_SET_INT('z', NULL, &state.line_termination,\n@@ -4679,7 +4676,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t}\n \tif (state.apply_with_reject)\n \t\tapply = state.apply_verbosely = 1;\n-\tif (!force_apply && (state.diffstat || state.numstat || state.summary || state.check || fake_ancestor))\n+\tif (!force_apply && (state.diffstat || state.numstat || state.summary || state.check || state.fake_ancestor))\n \t\tapply = 0;\n \tif (state.check_index && is_not_gitdir)\n \t\tdie(_(\"--index outside a repository\"));\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287378","messageId":"20160524081126.16973-28-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 27/49] builtin/apply: move 'p_context' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:11:04Z","receivedAt":"2016-05-24T08:11:04Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify the apply functionality the 'p_context' variable should\nnot be static and global to the file. Let's move it into\n'struct apply_state'.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 7 ++++---\n 1 file changed, 4 insertions(+), 3 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 59b0f1b..3c9f052 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -49,6 +49,7 @@ struct apply_state {\n \t/* Other non boolean parameters */\n \tconst char *fake_ancestor;\n \tint line_termination;\n+\tunsigned int p_context;\n };\n \n static int newfd = -1;\n@@ -56,7 +57,6 @@ static int newfd = -1;\n static int state_p_value = 1;\n static int p_value_known;\n static int apply = 1;\n-static unsigned int p_context = UINT_MAX;\n static const char * const apply_usage[] = {\n \tN_(\"git apply [<options>] [<patch>...]\"),\n \tNULL\n@@ -2872,7 +2872,7 @@ static int apply_one_fragment(struct apply_state *state,\n \t\t\tbreak;\n \n \t\t/* Am I at my context limits? */\n-\t\tif ((leading <= p_context) && (trailing <= p_context))\n+\t\tif ((leading <= state->p_context) && (trailing <= state->p_context))\n \t\t\tbreak;\n \t\tif (match_beginning || match_end) {\n \t\t\tmatch_beginning = match_end = 0;\n@@ -4566,6 +4566,7 @@ static void init_apply_state(struct apply_state *state, const char *prefix)\n \tstate->prefix = prefix;\n \tstate->prefix_length = state->prefix ? strlen(state->prefix) : 0;\n \tstate->line_termination = '\\n';\n+\tstate->p_context = UINT_MAX;\n \n \tgit_apply_config();\n \tif (apply_default_whitespace)\n@@ -4628,7 +4629,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t\t/* Think twice before adding \"--nul\" synonym to this */\n \t\tOPT_SET_INT('z', NULL, &state.line_termination,\n \t\t\tN_(\"paths are separated with NUL character\"), '\\0'),\n-\t\tOPT_INTEGER('C', NULL, &p_context,\n+\t\tOPT_INTEGER('C', NULL, &state.p_context,\n \t\t\t\tN_(\"ensure at least <n> lines of context match\")),\n \t\t{ OPTION_CALLBACK, 0, \"whitespace\", &whitespace_option, N_(\"action\"),\n \t\t\tN_(\"detect new or modified lines that have whitespace errors\"),\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287381","messageId":"20160524081126.16973-29-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 28/49] builtin/apply: move 'apply' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:11:05Z","receivedAt":"2016-05-24T08:11:05Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify the apply functionality the 'apply' variable should\nnot be static and global to the file. Let's move it into\n'struct apply_state'.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 31 +++++++++++++++++--------------\n 1 file changed, 17 insertions(+), 14 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 3c9f052..c0c18ce 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -26,6 +26,7 @@ struct apply_state {\n \tint prefix_length;\n \n \t/* These control what gets looked at and modified */\n+\tint apply; /* this is not a dry-run */\n \tint cached; /* apply to the index only */\n \tint check; /* preimage must match working tree, don't actually apply */\n \tint check_index; /* preimage must match the indexed version */\n@@ -56,7 +57,7 @@ static int newfd = -1;\n \n static int state_p_value = 1;\n static int p_value_known;\n-static int apply = 1;\n+\n static const char * const apply_usage[] = {\n \tN_(\"git apply [<options>] [<patch>...]\"),\n \tNULL\n@@ -126,10 +127,11 @@ static void parse_ignorewhitespace_option(const char *option)\n \tdie(_(\"unrecognized whitespace ignore option '%s'\"), option);\n }\n \n-static void set_default_whitespace_mode(const char *whitespace_option)\n+static void set_default_whitespace_mode(struct apply_state *state,\n+\t\t\t\t\tconst char *whitespace_option)\n {\n \tif (!whitespace_option && !apply_default_whitespace)\n-\t\tws_error_action = (apply ? warn_on_ws_error : nowarn_ws_error);\n+\t\tws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error);\n }\n \n /*\n@@ -2058,7 +2060,7 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si\n \t\t * without metadata change.  A binary patch appears\n \t\t * empty to us here.\n \t\t */\n-\t\tif ((apply || state->check) &&\n+\t\tif ((state->apply || state->check) &&\n \t\t    (!patch->is_binary && !metadata_changes(patch)))\n \t\t\tdie(_(\"patch with only garbage at line %d\"), state_linenr);\n \t}\n@@ -2916,7 +2918,7 @@ static int apply_one_fragment(struct apply_state *state,\n \t\t\t * apply_data->apply_fragments->apply_one_fragment\n \t\t\t */\n \t\t\tif (ws_error_action == die_on_ws_error)\n-\t\t\t\tapply = 0;\n+\t\t\t\tstate->apply = 0;\n \t\t}\n \n \t\tif (state->apply_verbosely && applied_pos != pos) {\n@@ -4460,9 +4462,9 @@ static int apply_patch(struct apply_state *state,\n \t\tdie(_(\"unrecognized input\"));\n \n \tif (whitespace_error && (ws_error_action == die_on_ws_error))\n-\t\tapply = 0;\n+\t\tstate->apply = 0;\n \n-\tstate->update_index = state->check_index && apply;\n+\tstate->update_index = state->check_index && state->apply;\n \tif (state->update_index && newfd < 0)\n \t\tnewfd = hold_locked_index(&lock_file, 1);\n \n@@ -4471,12 +4473,12 @@ static int apply_patch(struct apply_state *state,\n \t\t\tdie(_(\"unable to read index file\"));\n \t}\n \n-\tif ((state->check || apply) &&\n+\tif ((state->check || state->apply) &&\n \t    check_patch_list(state, list) < 0 &&\n \t    !state->apply_with_reject)\n \t\texit(1);\n \n-\tif (apply && write_out_results(state, list)) {\n+\tif (state->apply && write_out_results(state, list)) {\n \t\tif (state->apply_with_reject)\n \t\t\texit(1);\n \t\t/* with --3way, we still need to write the index out */\n@@ -4565,6 +4567,7 @@ static void init_apply_state(struct apply_state *state, const char *prefix)\n \tmemset(state, 0, sizeof(*state));\n \tstate->prefix = prefix;\n \tstate->prefix_length = state->prefix ? strlen(state->prefix) : 0;\n+\tstate->apply = 1;\n \tstate->line_termination = '\\n';\n \tstate->p_context = UINT_MAX;\n \n@@ -4676,9 +4679,9 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t\tstate.check_index = 1;\n \t}\n \tif (state.apply_with_reject)\n-\t\tapply = state.apply_verbosely = 1;\n+\t\tstate.apply = state.apply_verbosely = 1;\n \tif (!force_apply && (state.diffstat || state.numstat || state.summary || state.check || state.fake_ancestor))\n-\t\tapply = 0;\n+\t\tstate.apply = 0;\n \tif (state.check_index && is_not_gitdir)\n \t\tdie(_(\"--index outside a repository\"));\n \tif (state.cached) {\n@@ -4706,11 +4709,11 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t\tif (fd < 0)\n \t\t\tdie_errno(_(\"can't open patch '%s'\"), arg);\n \t\tread_stdin = 0;\n-\t\tset_default_whitespace_mode(whitespace_option);\n+\t\tset_default_whitespace_mode(&state, whitespace_option);\n \t\terrs |= apply_patch(&state, fd, arg, options);\n \t\tclose(fd);\n \t}\n-\tset_default_whitespace_mode(whitespace_option);\n+\tset_default_whitespace_mode(&state, whitespace_option);\n \tif (read_stdin)\n \t\terrs |= apply_patch(&state, 0, \"<stdin>\", options);\n \tif (whitespace_error) {\n@@ -4728,7 +4731,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t\t\t       \"%d lines add whitespace errors.\",\n \t\t\t       whitespace_error),\n \t\t\t    whitespace_error);\n-\t\tif (applied_after_fixing_ws && apply)\n+\t\tif (applied_after_fixing_ws && state.apply)\n \t\t\twarning(\"%d line%s applied after\"\n \t\t\t\t\" fixing whitespace errors.\",\n \t\t\t\tapplied_after_fixing_ws,\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287382","messageId":"20160524081126.16973-30-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 29/49] builtin/apply: move 'patch_input_file' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:11:06Z","receivedAt":"2016-05-24T08:11:06Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify the apply functionality the 'patch_input_file' variable should\nnot be static and global to the file. Let's move it into\n'struct apply_state'.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 27 +++++++++++++++++----------\n 1 file changed, 17 insertions(+), 10 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex c0c18ce..fa90028 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -49,6 +49,7 @@ struct apply_state {\n \n \t/* Other non boolean parameters */\n \tconst char *fake_ancestor;\n+\tconst char *patch_input_file;\n \tint line_termination;\n \tunsigned int p_context;\n };\n@@ -79,7 +80,6 @@ static enum ws_ignore {\n } ws_ignore_action = ignore_ws_none;\n \n \n-static const char *patch_input_file;\n static struct strbuf root = STRBUF_INIT;\n \n static void parse_whitespace_option(const char *option)\n@@ -1525,7 +1525,11 @@ static int find_header(struct apply_state *state,\n \treturn -1;\n }\n \n-static void record_ws_error(unsigned result, const char *line, int len, int linenr)\n+static void record_ws_error(struct apply_state *state,\n+\t\t\t    unsigned result,\n+\t\t\t    const char *line,\n+\t\t\t    int len,\n+\t\t\t    int linenr)\n {\n \tchar *err;\n \n@@ -1539,15 +1543,18 @@ static void record_ws_error(unsigned result, const char *line, int len, int line\n \n \terr = whitespace_error_string(result);\n \tfprintf(stderr, \"%s:%d: %s.\\n%.*s\\n\",\n-\t\tpatch_input_file, linenr, err, len, line);\n+\t\tstate->patch_input_file, linenr, err, len, line);\n \tfree(err);\n }\n \n-static void check_whitespace(const char *line, int len, unsigned ws_rule)\n+static void check_whitespace(struct apply_state *state,\n+\t\t\t     const char *line,\n+\t\t\t     int len,\n+\t\t\t     unsigned ws_rule)\n {\n \tunsigned result = ws_check(line + 1, len - 1, ws_rule);\n \n-\trecord_ws_error(result, line + 1, len - 2, state_linenr);\n+\trecord_ws_error(state, result, line + 1, len - 2, state_linenr);\n }\n \n /*\n@@ -1602,12 +1609,12 @@ static int parse_fragment(struct apply_state *state,\n \t\t\ttrailing++;\n \t\t\tif (!state->apply_in_reverse &&\n \t\t\t    ws_error_action == correct_ws_error)\n-\t\t\t\tcheck_whitespace(line, len, patch->ws_rule);\n+\t\t\t\tcheck_whitespace(state, line, len, patch->ws_rule);\n \t\t\tbreak;\n \t\tcase '-':\n \t\t\tif (state->apply_in_reverse &&\n \t\t\t    ws_error_action != nowarn_ws_error)\n-\t\t\t\tcheck_whitespace(line, len, patch->ws_rule);\n+\t\t\t\tcheck_whitespace(state, line, len, patch->ws_rule);\n \t\t\tdeleted++;\n \t\t\toldlines--;\n \t\t\ttrailing = 0;\n@@ -1615,7 +1622,7 @@ static int parse_fragment(struct apply_state *state,\n \t\tcase '+':\n \t\t\tif (!state->apply_in_reverse &&\n \t\t\t    ws_error_action != nowarn_ws_error)\n-\t\t\t\tcheck_whitespace(line, len, patch->ws_rule);\n+\t\t\t\tcheck_whitespace(state, line, len, patch->ws_rule);\n \t\t\tadded++;\n \t\t\tnewlines--;\n \t\t\ttrailing = 0;\n@@ -2904,7 +2911,7 @@ static int apply_one_fragment(struct apply_state *state,\n \t\t    preimage.nr + applied_pos >= img->nr &&\n \t\t    (ws_rule & WS_BLANK_AT_EOF) &&\n \t\t    ws_error_action != nowarn_ws_error) {\n-\t\t\trecord_ws_error(WS_BLANK_AT_EOF, \"+\", 1,\n+\t\t\trecord_ws_error(state, WS_BLANK_AT_EOF, \"+\", 1,\n \t\t\t\t\tfound_new_blank_lines_at_end);\n \t\t\tif (ws_error_action == correct_ws_error) {\n \t\t\t\twhile (new_blank_lines_at_end--)\n@@ -4427,7 +4434,7 @@ static int apply_patch(struct apply_state *state,\n \tstruct patch *list = NULL, **listp = &list;\n \tint skipped_patch = 0;\n \n-\tpatch_input_file = filename;\n+\tstate->patch_input_file = filename;\n \tread_patch_file(&buf, fd);\n \toffset = 0;\n \twhile (offset < buf.len) {\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287377","messageId":"20160524081126.16973-31-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 30/49] builtin/apply: move 'limit_by_name' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:11:07Z","receivedAt":"2016-05-24T08:11:07Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify the apply functionality the 'limit_by_name' variable should\nnot be static and global to the file. Let's move it into\n'struct apply_state'.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 26 ++++++++++++++++----------\n 1 file changed, 16 insertions(+), 10 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex fa90028..0e3de17 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -52,6 +52,9 @@ struct apply_state {\n \tconst char *patch_input_file;\n \tint line_termination;\n \tunsigned int p_context;\n+\n+\t/* Exclude and include path parameters */\n+\tstruct string_list limit_by_name;\n };\n \n static int newfd = -1;\n@@ -1958,13 +1961,14 @@ static void prefix_patch(struct apply_state *state, struct patch *p)\n  * include/exclude\n  */\n \n-static struct string_list limit_by_name;\n static int has_include;\n-static void add_name_limit(const char *name, int exclude)\n+static void add_name_limit(struct apply_state *state,\n+\t\t\t   const char *name,\n+\t\t\t   int exclude)\n {\n \tstruct string_list_item *it;\n \n-\tit = string_list_append(&limit_by_name, name);\n+\tit = string_list_append(&state->limit_by_name, name);\n \tit->util = exclude ? NULL : (void *) 1;\n }\n \n@@ -1982,8 +1986,8 @@ static int use_patch(struct apply_state *state, struct patch *p)\n \t}\n \n \t/* See if it matches any of exclude/include rule */\n-\tfor (i = 0; i < limit_by_name.nr; i++) {\n-\t\tstruct string_list_item *it = &limit_by_name.items[i];\n+\tfor (i = 0; i < state->limit_by_name.nr; i++) {\n+\t\tstruct string_list_item *it = &state->limit_by_name.items[i];\n \t\tif (!wildmatch(it->string, pathname, 0, NULL))\n \t\t\treturn (it->util != NULL);\n \t}\n@@ -4520,14 +4524,16 @@ static void git_apply_config(void)\n static int option_parse_exclude(const struct option *opt,\n \t\t\t\tconst char *arg, int unset)\n {\n-\tadd_name_limit(arg, 1);\n+\tstruct apply_state *state = opt->value;\n+\tadd_name_limit(state, arg, 1);\n \treturn 0;\n }\n \n static int option_parse_include(const struct option *opt,\n \t\t\t\tconst char *arg, int unset)\n {\n-\tadd_name_limit(arg, 0);\n+\tstruct apply_state *state = opt->value;\n+\tadd_name_limit(state, arg, 0);\n \thas_include = 1;\n \treturn 0;\n }\n@@ -4587,7 +4593,7 @@ static void init_apply_state(struct apply_state *state, const char *prefix)\n \n static void clear_apply_state(struct apply_state *state)\n {\n-\t/* empty for now */\n+\tstring_list_clear(&state->limit_by_name, 0);\n }\n \n int cmd_apply(int argc, const char **argv, const char *prefix)\n@@ -4603,10 +4609,10 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \tconst char *whitespace_option = NULL;\n \n \tstruct option builtin_apply_options[] = {\n-\t\t{ OPTION_CALLBACK, 0, \"exclude\", NULL, N_(\"path\"),\n+\t\t{ OPTION_CALLBACK, 0, \"exclude\", &state, N_(\"path\"),\n \t\t\tN_(\"don't apply changes matching the given path\"),\n \t\t\t0, option_parse_exclude },\n-\t\t{ OPTION_CALLBACK, 0, \"include\", NULL, N_(\"path\"),\n+\t\t{ OPTION_CALLBACK, 0, \"include\", &state, N_(\"path\"),\n \t\t\tN_(\"apply changes matching the given path\"),\n \t\t\t0, option_parse_include },\n \t\t{ OPTION_CALLBACK, 'p', NULL, NULL, N_(\"num\"),\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287376","messageId":"20160524081126.16973-32-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 31/49] builtin/apply: move 'has_include' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:11:08Z","receivedAt":"2016-05-24T08:11:08Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify the apply functionality the 'has_include' variable should\nnot be static and global to the file. Let's move it into\n'struct apply_state'.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 6 +++---\n 1 file changed, 3 insertions(+), 3 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 0e3de17..ebbc711 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -55,6 +55,7 @@ struct apply_state {\n \n \t/* Exclude and include path parameters */\n \tstruct string_list limit_by_name;\n+\tint has_include;\n };\n \n static int newfd = -1;\n@@ -1961,7 +1962,6 @@ static void prefix_patch(struct apply_state *state, struct patch *p)\n  * include/exclude\n  */\n \n-static int has_include;\n static void add_name_limit(struct apply_state *state,\n \t\t\t   const char *name,\n \t\t\t   int exclude)\n@@ -1997,7 +1997,7 @@ static int use_patch(struct apply_state *state, struct patch *p)\n \t * not used.  Otherwise, we saw bunch of exclude rules (or none)\n \t * and such a path is used.\n \t */\n-\treturn !has_include;\n+\treturn !state->has_include;\n }\n \n \n@@ -4534,7 +4534,7 @@ static int option_parse_include(const struct option *opt,\n {\n \tstruct apply_state *state = opt->value;\n \tadd_name_limit(state, arg, 0);\n-\thas_include = 1;\n+\tstate->has_include = 1;\n \treturn 0;\n }\n \n-- \n2.8.3.443.gaeee61e\n"},{"id":"287373","messageId":"20160524081126.16973-33-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 32/49] builtin/apply: move 'p_value' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:11:09Z","receivedAt":"2016-05-24T08:11:09Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify the apply functionality the 'p_value' variable should\nnot be static and global to the file. Let's move it into\n'struct apply_state'.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 151 +++++++++++++++++++++++++++++++++++++-------------------\n 1 file changed, 99 insertions(+), 52 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex ebbc711..843fafd 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -51,6 +51,7 @@ struct apply_state {\n \tconst char *fake_ancestor;\n \tconst char *patch_input_file;\n \tint line_termination;\n+\tint p_value;\n \tunsigned int p_context;\n \n \t/* Exclude and include path parameters */\n@@ -60,7 +61,6 @@ struct apply_state {\n \n static int newfd = -1;\n \n-static int state_p_value = 1;\n static int p_value_known;\n \n static const char * const apply_usage[] = {\n@@ -881,24 +881,24 @@ static void parse_traditional_patch(struct apply_state *state,\n \t\tq = guess_p_value(state, second);\n \t\tif (p < 0) p = q;\n \t\tif (0 <= p && p == q) {\n-\t\t\tstate_p_value = p;\n+\t\t\tstate->p_value = p;\n \t\t\tp_value_known = 1;\n \t\t}\n \t}\n \tif (is_dev_null(first)) {\n \t\tpatch->is_new = 1;\n \t\tpatch->is_delete = 0;\n-\t\tname = find_name_traditional(second, NULL, state_p_value);\n+\t\tname = find_name_traditional(second, NULL, state->p_value);\n \t\tpatch->new_name = name;\n \t} else if (is_dev_null(second)) {\n \t\tpatch->is_new = 0;\n \t\tpatch->is_delete = 1;\n-\t\tname = find_name_traditional(first, NULL, state_p_value);\n+\t\tname = find_name_traditional(first, NULL, state->p_value);\n \t\tpatch->old_name = name;\n \t} else {\n \t\tchar *first_name;\n-\t\tfirst_name = find_name_traditional(first, NULL, state_p_value);\n-\t\tname = find_name_traditional(second, first_name, state_p_value);\n+\t\tfirst_name = find_name_traditional(first, NULL, state->p_value);\n+\t\tname = find_name_traditional(second, first_name, state->p_value);\n \t\tfree(first_name);\n \t\tif (has_epoch_timestamp(first)) {\n \t\t\tpatch->is_new = 1;\n@@ -917,7 +917,9 @@ static void parse_traditional_patch(struct apply_state *state,\n \t\tdie(_(\"unable to find filename in patch at line %d\"), state_linenr);\n }\n \n-static int gitdiff_hdrend(const char *line, struct patch *patch)\n+static int gitdiff_hdrend(struct apply_state *state,\n+\t\t\t  const char *line,\n+\t\t\t  struct patch *patch)\n {\n \treturn -1;\n }\n@@ -934,10 +936,14 @@ static int gitdiff_hdrend(const char *line, struct patch *patch)\n #define DIFF_OLD_NAME 0\n #define DIFF_NEW_NAME 1\n \n-static void gitdiff_verify_name(const char *line, int isnull, char **name, int side)\n+static void gitdiff_verify_name(struct apply_state *state,\n+\t\t\t\tconst char *line,\n+\t\t\t\tint isnull,\n+\t\t\t\tchar **name,\n+\t\t\t\tint side)\n {\n \tif (!*name && !isnull) {\n-\t\t*name = find_name(line, NULL, state_p_value, TERM_TAB);\n+\t\t*name = find_name(line, NULL, state->p_value, TERM_TAB);\n \t\treturn;\n \t}\n \n@@ -947,7 +953,7 @@ static void gitdiff_verify_name(const char *line, int isnull, char **name, int s\n \t\tif (isnull)\n \t\t\tdie(_(\"git apply: bad git-diff - expected /dev/null, got %s on line %d\"),\n \t\t\t    *name, state_linenr);\n-\t\tanother = find_name(line, NULL, state_p_value, TERM_TAB);\n+\t\tanother = find_name(line, NULL, state->p_value, TERM_TAB);\n \t\tif (!another || memcmp(another, *name, len + 1))\n \t\t\tdie((side == DIFF_NEW_NAME) ?\n \t\t\t    _(\"git apply: bad git-diff - inconsistent new filename on line %d\") :\n@@ -960,81 +966,105 @@ static void gitdiff_verify_name(const char *line, int isnull, char **name, int s\n \t}\n }\n \n-static int gitdiff_oldname(const char *line, struct patch *patch)\n+static int gitdiff_oldname(struct apply_state *state,\n+\t\t\t   const char *line,\n+\t\t\t   struct patch *patch)\n {\n-\tgitdiff_verify_name(line, patch->is_new, &patch->old_name,\n+\tgitdiff_verify_name(state, line,\n+\t\t\t    patch->is_new, &patch->old_name,\n \t\t\t    DIFF_OLD_NAME);\n \treturn 0;\n }\n \n-static int gitdiff_newname(const char *line, struct patch *patch)\n+static int gitdiff_newname(struct apply_state *state,\n+\t\t\t   const char *line,\n+\t\t\t   struct patch *patch)\n {\n-\tgitdiff_verify_name(line, patch->is_delete, &patch->new_name,\n+\tgitdiff_verify_name(state, line,\n+\t\t\t    patch->is_delete, &patch->new_name,\n \t\t\t    DIFF_NEW_NAME);\n \treturn 0;\n }\n \n-static int gitdiff_oldmode(const char *line, struct patch *patch)\n+static int gitdiff_oldmode(struct apply_state *state,\n+\t\t\t   const char *line,\n+\t\t\t   struct patch *patch)\n {\n \tpatch->old_mode = strtoul(line, NULL, 8);\n \treturn 0;\n }\n \n-static int gitdiff_newmode(const char *line, struct patch *patch)\n+static int gitdiff_newmode(struct apply_state *state,\n+\t\t\t   const char *line,\n+\t\t\t   struct patch *patch)\n {\n \tpatch->new_mode = strtoul(line, NULL, 8);\n \treturn 0;\n }\n \n-static int gitdiff_delete(const char *line, struct patch *patch)\n+static int gitdiff_delete(struct apply_state *state,\n+\t\t\t  const char *line,\n+\t\t\t  struct patch *patch)\n {\n \tpatch->is_delete = 1;\n \tfree(patch->old_name);\n \tpatch->old_name = xstrdup_or_null(patch->def_name);\n-\treturn gitdiff_oldmode(line, patch);\n+\treturn gitdiff_oldmode(state, line, patch);\n }\n \n-static int gitdiff_newfile(const char *line, struct patch *patch)\n+static int gitdiff_newfile(struct apply_state *state,\n+\t\t\t   const char *line,\n+\t\t\t   struct patch *patch)\n {\n \tpatch->is_new = 1;\n \tfree(patch->new_name);\n \tpatch->new_name = xstrdup_or_null(patch->def_name);\n-\treturn gitdiff_newmode(line, patch);\n+\treturn gitdiff_newmode(state, line, patch);\n }\n \n-static int gitdiff_copysrc(const char *line, struct patch *patch)\n+static int gitdiff_copysrc(struct apply_state *state,\n+\t\t\t   const char *line,\n+\t\t\t   struct patch *patch)\n {\n \tpatch->is_copy = 1;\n \tfree(patch->old_name);\n-\tpatch->old_name = find_name(line, NULL, state_p_value ? state_p_value - 1 : 0, 0);\n+\tpatch->old_name = find_name(line, NULL, state->p_value ? state->p_value - 1 : 0, 0);\n \treturn 0;\n }\n \n-static int gitdiff_copydst(const char *line, struct patch *patch)\n+static int gitdiff_copydst(struct apply_state *state,\n+\t\t\t   const char *line,\n+\t\t\t   struct patch *patch)\n {\n \tpatch->is_copy = 1;\n \tfree(patch->new_name);\n-\tpatch->new_name = find_name(line, NULL, state_p_value ? state_p_value - 1 : 0, 0);\n+\tpatch->new_name = find_name(line, NULL, state->p_value ? state->p_value - 1 : 0, 0);\n \treturn 0;\n }\n \n-static int gitdiff_renamesrc(const char *line, struct patch *patch)\n+static int gitdiff_renamesrc(struct apply_state *state,\n+\t\t\t     const char *line,\n+\t\t\t     struct patch *patch)\n {\n \tpatch->is_rename = 1;\n \tfree(patch->old_name);\n-\tpatch->old_name = find_name(line, NULL, state_p_value ? state_p_value - 1 : 0, 0);\n+\tpatch->old_name = find_name(line, NULL, state->p_value ? state->p_value - 1 : 0, 0);\n \treturn 0;\n }\n \n-static int gitdiff_renamedst(const char *line, struct patch *patch)\n+static int gitdiff_renamedst(struct apply_state *state,\n+\t\t\t     const char *line,\n+\t\t\t     struct patch *patch)\n {\n \tpatch->is_rename = 1;\n \tfree(patch->new_name);\n-\tpatch->new_name = find_name(line, NULL, state_p_value ? state_p_value - 1 : 0, 0);\n+\tpatch->new_name = find_name(line, NULL, state->p_value ? state->p_value - 1 : 0, 0);\n \treturn 0;\n }\n \n-static int gitdiff_similarity(const char *line, struct patch *patch)\n+static int gitdiff_similarity(struct apply_state *state,\n+\t\t\t      const char *line,\n+\t\t\t      struct patch *patch)\n {\n \tunsigned long val = strtoul(line, NULL, 10);\n \tif (val <= 100)\n@@ -1042,7 +1072,9 @@ static int gitdiff_similarity(const char *line, struct patch *patch)\n \treturn 0;\n }\n \n-static int gitdiff_dissimilarity(const char *line, struct patch *patch)\n+static int gitdiff_dissimilarity(struct apply_state *state,\n+\t\t\t\t const char *line,\n+\t\t\t\t struct patch *patch)\n {\n \tunsigned long val = strtoul(line, NULL, 10);\n \tif (val <= 100)\n@@ -1050,7 +1082,9 @@ static int gitdiff_dissimilarity(const char *line, struct patch *patch)\n \treturn 0;\n }\n \n-static int gitdiff_index(const char *line, struct patch *patch)\n+static int gitdiff_index(struct apply_state *state,\n+\t\t\t const char *line,\n+\t\t\t struct patch *patch)\n {\n \t/*\n \t * index line is N hexadecimal, \"..\", N hexadecimal,\n@@ -1087,7 +1121,9 @@ static int gitdiff_index(const char *line, struct patch *patch)\n  * This is normal for a diff that doesn't change anything: we'll fall through\n  * into the next diff. Tell the parser to break out.\n  */\n-static int gitdiff_unrecognized(const char *line, struct patch *patch)\n+static int gitdiff_unrecognized(struct apply_state *state,\n+\t\t\t\tconst char *line,\n+\t\t\t\tstruct patch *patch)\n {\n \treturn -1;\n }\n@@ -1096,15 +1132,17 @@ static int gitdiff_unrecognized(const char *line, struct patch *patch)\n  * Skip p_value leading components from \"line\"; as we do not accept\n  * absolute paths, return NULL in that case.\n  */\n-static const char *skip_tree_prefix(const char *line, int llen)\n+static const char *skip_tree_prefix(struct apply_state *state,\n+\t\t\t\t    const char *line,\n+\t\t\t\t    int llen)\n {\n \tint nslash;\n \tint i;\n \n-\tif (!state_p_value)\n+\tif (!state->p_value)\n \t\treturn (llen && line[0] == '/') ? NULL : line;\n \n-\tnslash = state_p_value;\n+\tnslash = state->p_value;\n \tfor (i = 0; i < llen; i++) {\n \t\tint ch = line[i];\n \t\tif (ch == '/' && --nslash <= 0)\n@@ -1121,7 +1159,9 @@ static const char *skip_tree_prefix(const char *line, int llen)\n  * creation or deletion of an empty file.  In any of these cases,\n  * both sides are the same name under a/ and b/ respectively.\n  */\n-static char *git_header_name(const char *line, int llen)\n+static char *git_header_name(struct apply_state *state,\n+\t\t\t     const char *line,\n+\t\t\t     int llen)\n {\n \tconst char *name;\n \tconst char *second = NULL;\n@@ -1139,7 +1179,7 @@ static char *git_header_name(const char *line, int llen)\n \t\t\tgoto free_and_fail1;\n \n \t\t/* strip the a/b prefix including trailing slash */\n-\t\tcp = skip_tree_prefix(first.buf, first.len);\n+\t\tcp = skip_tree_prefix(state, first.buf, first.len);\n \t\tif (!cp)\n \t\t\tgoto free_and_fail1;\n \t\tstrbuf_remove(&first, 0, cp - first.buf);\n@@ -1156,7 +1196,7 @@ static char *git_header_name(const char *line, int llen)\n \t\tif (*second == '\"') {\n \t\t\tif (unquote_c_style(&sp, second, NULL))\n \t\t\t\tgoto free_and_fail1;\n-\t\t\tcp = skip_tree_prefix(sp.buf, sp.len);\n+\t\t\tcp = skip_tree_prefix(state, sp.buf, sp.len);\n \t\t\tif (!cp)\n \t\t\t\tgoto free_and_fail1;\n \t\t\t/* They must match, otherwise ignore */\n@@ -1167,7 +1207,7 @@ static char *git_header_name(const char *line, int llen)\n \t\t}\n \n \t\t/* unquoted second */\n-\t\tcp = skip_tree_prefix(second, line + llen - second);\n+\t\tcp = skip_tree_prefix(state, second, line + llen - second);\n \t\tif (!cp)\n \t\t\tgoto free_and_fail1;\n \t\tif (line + llen - cp != first.len ||\n@@ -1182,7 +1222,7 @@ static char *git_header_name(const char *line, int llen)\n \t}\n \n \t/* unquoted first name */\n-\tname = skip_tree_prefix(line, llen);\n+\tname = skip_tree_prefix(state, line, llen);\n \tif (!name)\n \t\treturn NULL;\n \n@@ -1198,7 +1238,7 @@ static char *git_header_name(const char *line, int llen)\n \t\t\tif (unquote_c_style(&sp, second, NULL))\n \t\t\t\tgoto free_and_fail2;\n \n-\t\t\tnp = skip_tree_prefix(sp.buf, sp.len);\n+\t\t\tnp = skip_tree_prefix(state, sp.buf, sp.len);\n \t\t\tif (!np)\n \t\t\t\tgoto free_and_fail2;\n \n@@ -1242,7 +1282,7 @@ static char *git_header_name(const char *line, int llen)\n \t\t\t */\n \t\t\tif (!name[len + 1])\n \t\t\t\treturn NULL; /* no postimage name */\n-\t\t\tsecond = skip_tree_prefix(name + len + 1,\n+\t\t\tsecond = skip_tree_prefix(state, name + len + 1,\n \t\t\t\t\t\t  line_len - (len + 1));\n \t\t\tif (!second)\n \t\t\t\treturn NULL;\n@@ -1258,7 +1298,11 @@ static char *git_header_name(const char *line, int llen)\n }\n \n /* Verify that we recognize the lines following a git header */\n-static int parse_git_header(const char *line, int len, unsigned int size, struct patch *patch)\n+static int parse_git_header(struct apply_state *state,\n+\t\t\t    const char *line,\n+\t\t\t    int len,\n+\t\t\t    unsigned int size,\n+\t\t\t    struct patch *patch)\n {\n \tunsigned long offset;\n \n@@ -1272,7 +1316,7 @@ static int parse_git_header(const char *line, int len, unsigned int size, struct\n \t * or removing or adding empty files), so we get\n \t * the default name from the header.\n \t */\n-\tpatch->def_name = git_header_name(line, len);\n+\tpatch->def_name = git_header_name(state, line, len);\n \tif (patch->def_name && root.len) {\n \t\tchar *s = xstrfmt(\"%s%s\", root.buf, patch->def_name);\n \t\tfree(patch->def_name);\n@@ -1285,7 +1329,7 @@ static int parse_git_header(const char *line, int len, unsigned int size, struct\n \tfor (offset = len ; size > 0 ; offset += len, size -= len, line += len, state_linenr++) {\n \t\tstatic const struct opentry {\n \t\t\tconst char *str;\n-\t\t\tint (*fn)(const char *, struct patch *);\n+\t\t\tint (*fn)(struct apply_state *, const char *, struct patch *);\n \t\t} optable[] = {\n \t\t\t{ \"@@ -\", gitdiff_hdrend },\n \t\t\t{ \"--- \", gitdiff_oldname },\n@@ -1315,7 +1359,7 @@ static int parse_git_header(const char *line, int len, unsigned int size, struct\n \t\t\tint oplen = strlen(p->str);\n \t\t\tif (len < oplen || memcmp(p->str, line, oplen))\n \t\t\t\tcontinue;\n-\t\t\tif (p->fn(line + oplen, patch) < 0)\n+\t\t\tif (p->fn(state, line + oplen, patch) < 0)\n \t\t\t\treturn offset;\n \t\t\tbreak;\n \t\t}\n@@ -1485,7 +1529,7 @@ static int find_header(struct apply_state *state,\n \t\t * or mode change, so we handle that specially\n \t\t */\n \t\tif (!memcmp(\"diff --git \", line, 11)) {\n-\t\t\tint git_hdr_len = parse_git_header(line, len, size, patch);\n+\t\t\tint git_hdr_len = parse_git_header(state, line, len, size, patch);\n \t\t\tif (git_hdr_len <= len)\n \t\t\t\tcontinue;\n \t\t\tif (!patch->old_name && !patch->new_name) {\n@@ -1494,8 +1538,8 @@ static int find_header(struct apply_state *state,\n \t\t\t\t\t       \"%d leading pathname component (line %d)\",\n \t\t\t\t\t       \"git diff header lacks filename information when removing \"\n \t\t\t\t\t       \"%d leading pathname components (line %d)\",\n-\t\t\t\t\t       state_p_value),\n-\t\t\t\t\t    state_p_value, state_linenr);\n+\t\t\t\t\t       state->p_value),\n+\t\t\t\t\t    state->p_value, state_linenr);\n \t\t\t\tpatch->old_name = xstrdup(patch->def_name);\n \t\t\t\tpatch->new_name = xstrdup(patch->def_name);\n \t\t\t}\n@@ -4539,9 +4583,11 @@ static int option_parse_include(const struct option *opt,\n }\n \n static int option_parse_p(const struct option *opt,\n-\t\t\t  const char *arg, int unset)\n+\t\t\t  const char *arg,\n+\t\t\t  int unset)\n {\n-\tstate_p_value = atoi(arg);\n+\tstruct apply_state *state = opt->value;\n+\tstate->p_value = atoi(arg);\n \tp_value_known = 1;\n \treturn 0;\n }\n@@ -4582,6 +4628,7 @@ static void init_apply_state(struct apply_state *state, const char *prefix)\n \tstate->prefix_length = state->prefix ? strlen(state->prefix) : 0;\n \tstate->apply = 1;\n \tstate->line_termination = '\\n';\n+\tstate->p_value = 1;\n \tstate->p_context = UINT_MAX;\n \n \tgit_apply_config();\n@@ -4615,7 +4662,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t\t{ OPTION_CALLBACK, 0, \"include\", &state, N_(\"path\"),\n \t\t\tN_(\"apply changes matching the given path\"),\n \t\t\t0, option_parse_include },\n-\t\t{ OPTION_CALLBACK, 'p', NULL, NULL, N_(\"num\"),\n+\t\t{ OPTION_CALLBACK, 'p', NULL, &state, N_(\"num\"),\n \t\t\tN_(\"remove <num> leading slashes from traditional diff paths\"),\n \t\t\t0, option_parse_p },\n \t\tOPT_BOOL(0, \"no-add\", &state.no_add,\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287356","messageId":"20160524081126.16973-34-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 33/49] builtin/apply: move 'p_value_known' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:11:10Z","receivedAt":"2016-05-24T08:11:10Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify the apply functionality the 'p_value_known' variable should\nnot be static and global to the file. Let's move it into\n'struct apply_state'.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 9 ++++-----\n 1 file changed, 4 insertions(+), 5 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 843fafd..e1b68d4 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -52,6 +52,7 @@ struct apply_state {\n \tconst char *patch_input_file;\n \tint line_termination;\n \tint p_value;\n+\tint p_value_known;\n \tunsigned int p_context;\n \n \t/* Exclude and include path parameters */\n@@ -61,8 +62,6 @@ struct apply_state {\n \n static int newfd = -1;\n \n-static int p_value_known;\n-\n static const char * const apply_usage[] = {\n \tN_(\"git apply [<options>] [<patch>...]\"),\n \tNULL\n@@ -875,14 +874,14 @@ static void parse_traditional_patch(struct apply_state *state,\n \n \tfirst += 4;\t/* skip \"--- \" */\n \tsecond += 4;\t/* skip \"+++ \" */\n-\tif (!p_value_known) {\n+\tif (!state->p_value_known) {\n \t\tint p, q;\n \t\tp = guess_p_value(state, first);\n \t\tq = guess_p_value(state, second);\n \t\tif (p < 0) p = q;\n \t\tif (0 <= p && p == q) {\n \t\t\tstate->p_value = p;\n-\t\t\tp_value_known = 1;\n+\t\t\tstate->p_value_known = 1;\n \t\t}\n \t}\n \tif (is_dev_null(first)) {\n@@ -4588,7 +4587,7 @@ static int option_parse_p(const struct option *opt,\n {\n \tstruct apply_state *state = opt->value;\n \tstate->p_value = atoi(arg);\n-\tp_value_known = 1;\n+\tstate->p_value_known = 1;\n \treturn 0;\n }\n \n-- \n2.8.3.443.gaeee61e\n"},{"id":"287375","messageId":"20160524081126.16973-35-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 34/49] builtin/apply: move 'root' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:11:11Z","receivedAt":"2016-05-24T08:11:11Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify the apply functionality the 'root' variable should\nnot be static and global to the file. Let's move it into\n'struct apply_state'.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 82 ++++++++++++++++++++++++++++++++++-----------------------\n 1 file changed, 49 insertions(+), 33 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex e1b68d4..5dc1d89 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -51,6 +51,7 @@ struct apply_state {\n \tconst char *fake_ancestor;\n \tconst char *patch_input_file;\n \tint line_termination;\n+\tstruct strbuf root;\n \tint p_value;\n \tint p_value_known;\n \tunsigned int p_context;\n@@ -83,8 +84,6 @@ static enum ws_ignore {\n } ws_ignore_action = ignore_ws_none;\n \n \n-static struct strbuf root = STRBUF_INIT;\n-\n static void parse_whitespace_option(const char *option)\n {\n \tif (!option) {\n@@ -474,7 +473,10 @@ static char *squash_slash(char *name)\n \treturn name;\n }\n \n-static char *find_name_gnu(const char *line, const char *def, int p_value)\n+static char *find_name_gnu(struct apply_state *state,\n+\t\t\t   const char *line,\n+\t\t\t   const char *def,\n+\t\t\t   int p_value)\n {\n \tstruct strbuf name = STRBUF_INIT;\n \tchar *cp;\n@@ -498,8 +500,8 @@ static char *find_name_gnu(const char *line, const char *def, int p_value)\n \t}\n \n \tstrbuf_remove(&name, 0, cp - name.buf);\n-\tif (root.len)\n-\t\tstrbuf_insert(&name, 0, root.buf, root.len);\n+\tif (state->root.len)\n+\t\tstrbuf_insert(&name, 0, state->root.buf, state->root.len);\n \treturn squash_slash(strbuf_detach(&name, NULL));\n }\n \n@@ -662,8 +664,12 @@ static size_t diff_timestamp_len(const char *line, size_t len)\n \treturn line + len - end;\n }\n \n-static char *find_name_common(const char *line, const char *def,\n-\t\t\t      int p_value, const char *end, int terminate)\n+static char *find_name_common(struct apply_state *state,\n+\t\t\t      const char *line,\n+\t\t\t      const char *def,\n+\t\t\t      int p_value,\n+\t\t\t      const char *end,\n+\t\t\t      int terminate)\n {\n \tint len;\n \tconst char *start = NULL;\n@@ -701,32 +707,39 @@ static char *find_name_common(const char *line, const char *def,\n \t\t\treturn squash_slash(xstrdup(def));\n \t}\n \n-\tif (root.len) {\n-\t\tchar *ret = xstrfmt(\"%s%.*s\", root.buf, len, start);\n+\tif (state->root.len) {\n+\t\tchar *ret = xstrfmt(\"%s%.*s\", state->root.buf, len, start);\n \t\treturn squash_slash(ret);\n \t}\n \n \treturn squash_slash(xmemdupz(start, len));\n }\n \n-static char *find_name(const char *line, char *def, int p_value, int terminate)\n+static char *find_name(struct apply_state *state,\n+\t\t       const char *line,\n+\t\t       char *def,\n+\t\t       int p_value,\n+\t\t       int terminate)\n {\n \tif (*line == '\"') {\n-\t\tchar *name = find_name_gnu(line, def, p_value);\n+\t\tchar *name = find_name_gnu(state, line, def, p_value);\n \t\tif (name)\n \t\t\treturn name;\n \t}\n \n-\treturn find_name_common(line, def, p_value, NULL, terminate);\n+\treturn find_name_common(state, line, def, p_value, NULL, terminate);\n }\n \n-static char *find_name_traditional(const char *line, char *def, int p_value)\n+static char *find_name_traditional(struct apply_state *state,\n+\t\t\t\t   const char *line,\n+\t\t\t\t   char *def,\n+\t\t\t\t   int p_value)\n {\n \tsize_t len;\n \tsize_t date_len;\n \n \tif (*line == '\"') {\n-\t\tchar *name = find_name_gnu(line, def, p_value);\n+\t\tchar *name = find_name_gnu(state, line, def, p_value);\n \t\tif (name)\n \t\t\treturn name;\n \t}\n@@ -734,10 +747,10 @@ static char *find_name_traditional(const char *line, char *def, int p_value)\n \tlen = strchrnul(line, '\\n') - line;\n \tdate_len = diff_timestamp_len(line, len);\n \tif (!date_len)\n-\t\treturn find_name_common(line, def, p_value, NULL, TERM_TAB);\n+\t\treturn find_name_common(state, line, def, p_value, NULL, TERM_TAB);\n \tlen -= date_len;\n \n-\treturn find_name_common(line, def, p_value, line + len, 0);\n+\treturn find_name_common(state, line, def, p_value, line + len, 0);\n }\n \n static int count_slashes(const char *cp)\n@@ -762,7 +775,7 @@ static int guess_p_value(struct apply_state *state, const char *nameline)\n \n \tif (is_dev_null(nameline))\n \t\treturn -1;\n-\tname = find_name_traditional(nameline, NULL, 0);\n+\tname = find_name_traditional(state, nameline, NULL, 0);\n \tif (!name)\n \t\treturn -1;\n \tcp = strchr(name, '/');\n@@ -887,17 +900,17 @@ static void parse_traditional_patch(struct apply_state *state,\n \tif (is_dev_null(first)) {\n \t\tpatch->is_new = 1;\n \t\tpatch->is_delete = 0;\n-\t\tname = find_name_traditional(second, NULL, state->p_value);\n+\t\tname = find_name_traditional(state, second, NULL, state->p_value);\n \t\tpatch->new_name = name;\n \t} else if (is_dev_null(second)) {\n \t\tpatch->is_new = 0;\n \t\tpatch->is_delete = 1;\n-\t\tname = find_name_traditional(first, NULL, state->p_value);\n+\t\tname = find_name_traditional(state, first, NULL, state->p_value);\n \t\tpatch->old_name = name;\n \t} else {\n \t\tchar *first_name;\n-\t\tfirst_name = find_name_traditional(first, NULL, state->p_value);\n-\t\tname = find_name_traditional(second, first_name, state->p_value);\n+\t\tfirst_name = find_name_traditional(state, first, NULL, state->p_value);\n+\t\tname = find_name_traditional(state, second, first_name, state->p_value);\n \t\tfree(first_name);\n \t\tif (has_epoch_timestamp(first)) {\n \t\t\tpatch->is_new = 1;\n@@ -942,7 +955,7 @@ static void gitdiff_verify_name(struct apply_state *state,\n \t\t\t\tint side)\n {\n \tif (!*name && !isnull) {\n-\t\t*name = find_name(line, NULL, state->p_value, TERM_TAB);\n+\t\t*name = find_name(state, line, NULL, state->p_value, TERM_TAB);\n \t\treturn;\n \t}\n \n@@ -952,7 +965,7 @@ static void gitdiff_verify_name(struct apply_state *state,\n \t\tif (isnull)\n \t\t\tdie(_(\"git apply: bad git-diff - expected /dev/null, got %s on line %d\"),\n \t\t\t    *name, state_linenr);\n-\t\tanother = find_name(line, NULL, state->p_value, TERM_TAB);\n+\t\tanother = find_name(state, line, NULL, state->p_value, TERM_TAB);\n \t\tif (!another || memcmp(another, *name, len + 1))\n \t\t\tdie((side == DIFF_NEW_NAME) ?\n \t\t\t    _(\"git apply: bad git-diff - inconsistent new filename on line %d\") :\n@@ -1027,7 +1040,7 @@ static int gitdiff_copysrc(struct apply_state *state,\n {\n \tpatch->is_copy = 1;\n \tfree(patch->old_name);\n-\tpatch->old_name = find_name(line, NULL, state->p_value ? state->p_value - 1 : 0, 0);\n+\tpatch->old_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);\n \treturn 0;\n }\n \n@@ -1037,7 +1050,7 @@ static int gitdiff_copydst(struct apply_state *state,\n {\n \tpatch->is_copy = 1;\n \tfree(patch->new_name);\n-\tpatch->new_name = find_name(line, NULL, state->p_value ? state->p_value - 1 : 0, 0);\n+\tpatch->new_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);\n \treturn 0;\n }\n \n@@ -1047,7 +1060,7 @@ static int gitdiff_renamesrc(struct apply_state *state,\n {\n \tpatch->is_rename = 1;\n \tfree(patch->old_name);\n-\tpatch->old_name = find_name(line, NULL, state->p_value ? state->p_value - 1 : 0, 0);\n+\tpatch->old_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);\n \treturn 0;\n }\n \n@@ -1057,7 +1070,7 @@ static int gitdiff_renamedst(struct apply_state *state,\n {\n \tpatch->is_rename = 1;\n \tfree(patch->new_name);\n-\tpatch->new_name = find_name(line, NULL, state->p_value ? state->p_value - 1 : 0, 0);\n+\tpatch->new_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);\n \treturn 0;\n }\n \n@@ -1316,8 +1329,8 @@ static int parse_git_header(struct apply_state *state,\n \t * the default name from the header.\n \t */\n \tpatch->def_name = git_header_name(state, line, len);\n-\tif (patch->def_name && root.len) {\n-\t\tchar *s = xstrfmt(\"%s%s\", root.buf, patch->def_name);\n+\tif (patch->def_name && state->root.len) {\n+\t\tchar *s = xstrfmt(\"%s%s\", state->root.buf, patch->def_name);\n \t\tfree(patch->def_name);\n \t\tpatch->def_name = s;\n \t}\n@@ -4614,9 +4627,10 @@ static int option_parse_whitespace(const struct option *opt,\n static int option_parse_directory(const struct option *opt,\n \t\t\t\t  const char *arg, int unset)\n {\n-\tstrbuf_reset(&root);\n-\tstrbuf_addstr(&root, arg);\n-\tstrbuf_complete(&root, '/');\n+\tstruct apply_state *state = opt->value;\n+\tstrbuf_reset(&state->root);\n+\tstrbuf_addstr(&state->root, arg);\n+\tstrbuf_complete(&state->root, '/');\n \treturn 0;\n }\n \n@@ -4629,6 +4643,7 @@ static void init_apply_state(struct apply_state *state, const char *prefix)\n \tstate->line_termination = '\\n';\n \tstate->p_value = 1;\n \tstate->p_context = UINT_MAX;\n+\tstrbuf_init(&state->root, 0);\n \n \tgit_apply_config();\n \tif (apply_default_whitespace)\n@@ -4640,6 +4655,7 @@ static void init_apply_state(struct apply_state *state, const char *prefix)\n static void clear_apply_state(struct apply_state *state)\n {\n \tstring_list_clear(&state->limit_by_name, 0);\n+\tstrbuf_release(&state->root);\n }\n \n int cmd_apply(int argc, const char **argv, const char *prefix)\n@@ -4717,7 +4733,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t\tOPT_BIT(0, \"recount\", &options,\n \t\t\tN_(\"do not trust the line counts in the hunk headers\"),\n \t\t\tRECOUNT),\n-\t\t{ OPTION_CALLBACK, 0, \"directory\", NULL, N_(\"root\"),\n+\t\t{ OPTION_CALLBACK, 0, \"directory\", &state, N_(\"root\"),\n \t\t\tN_(\"prepend <root> to all filenames\"),\n \t\t\t0, option_parse_directory },\n \t\tOPT_END()\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287361","messageId":"20160524081126.16973-36-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 35/49] builtin/apply: move 'whitespace_error' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:11:12Z","receivedAt":"2016-05-24T08:11:12Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify the apply functionality the 'whitespace_error' variable should\nnot be static and global to the file. Let's move it into\n'struct apply_state'.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 28 +++++++++++++++-------------\n 1 file changed, 15 insertions(+), 13 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 5dc1d89..44717b2 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -59,6 +59,9 @@ struct apply_state {\n \t/* Exclude and include path parameters */\n \tstruct string_list limit_by_name;\n \tint has_include;\n+\n+\t/* These control whitespace errors */\n+\tint whitespace_error;\n };\n \n static int newfd = -1;\n@@ -74,7 +77,6 @@ static enum ws_error_action {\n \tdie_on_ws_error,\n \tcorrect_ws_error\n } ws_error_action = warn_on_ws_error;\n-static int whitespace_error;\n static int squelch_whitespace_errors = 5;\n static int applied_after_fixing_ws;\n \n@@ -1596,9 +1598,9 @@ static void record_ws_error(struct apply_state *state,\n \tif (!result)\n \t\treturn;\n \n-\twhitespace_error++;\n+\tstate->whitespace_error++;\n \tif (squelch_whitespace_errors &&\n-\t    squelch_whitespace_errors < whitespace_error)\n+\t    squelch_whitespace_errors < state->whitespace_error)\n \t\treturn;\n \n \terr = whitespace_error_string(result);\n@@ -2855,7 +2857,7 @@ static int apply_one_fragment(struct apply_state *state,\n \n \t\t\tstart = newlines.len;\n \t\t\tif (first != '+' ||\n-\t\t\t    !whitespace_error ||\n+\t\t\t    !state->whitespace_error ||\n \t\t\t    ws_error_action != correct_ws_error) {\n \t\t\t\tstrbuf_add(&newlines, patch + 1, plen);\n \t\t\t}\n@@ -4528,7 +4530,7 @@ static int apply_patch(struct apply_state *state,\n \tif (!list && !skipped_patch)\n \t\tdie(_(\"unrecognized input\"));\n \n-\tif (whitespace_error && (ws_error_action == die_on_ws_error))\n+\tif (state->whitespace_error && (ws_error_action == die_on_ws_error))\n \t\tstate->apply = 0;\n \n \tstate->update_index = state->check_index && state->apply;\n@@ -4791,11 +4793,11 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \tset_default_whitespace_mode(&state, whitespace_option);\n \tif (read_stdin)\n \t\terrs |= apply_patch(&state, 0, \"<stdin>\", options);\n-\tif (whitespace_error) {\n+\tif (state.whitespace_error) {\n \t\tif (squelch_whitespace_errors &&\n-\t\t    squelch_whitespace_errors < whitespace_error) {\n+\t\t    squelch_whitespace_errors < state.whitespace_error) {\n \t\t\tint squelched =\n-\t\t\t\twhitespace_error - squelch_whitespace_errors;\n+\t\t\t\tstate.whitespace_error - squelch_whitespace_errors;\n \t\t\twarning(Q_(\"squelched %d whitespace error\",\n \t\t\t\t   \"squelched %d whitespace errors\",\n \t\t\t\t   squelched),\n@@ -4804,18 +4806,18 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t\tif (ws_error_action == die_on_ws_error)\n \t\t\tdie(Q_(\"%d line adds whitespace errors.\",\n \t\t\t       \"%d lines add whitespace errors.\",\n-\t\t\t       whitespace_error),\n-\t\t\t    whitespace_error);\n+\t\t\t       state.whitespace_error),\n+\t\t\t    state.whitespace_error);\n \t\tif (applied_after_fixing_ws && state.apply)\n \t\t\twarning(\"%d line%s applied after\"\n \t\t\t\t\" fixing whitespace errors.\",\n \t\t\t\tapplied_after_fixing_ws,\n \t\t\t\tapplied_after_fixing_ws == 1 ? \"\" : \"s\");\n-\t\telse if (whitespace_error)\n+\t\telse if (state.whitespace_error)\n \t\t\twarning(Q_(\"%d line adds whitespace errors.\",\n \t\t\t\t   \"%d lines add whitespace errors.\",\n-\t\t\t\t   whitespace_error),\n-\t\t\t\twhitespace_error);\n+\t\t\t\t   state.whitespace_error),\n+\t\t\t\tstate.whitespace_error);\n \t}\n \n \tif (state.update_index) {\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287372","messageId":"20160524081126.16973-37-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 36/49] builtin/apply: move 'whitespace_option' into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:11:13Z","receivedAt":"2016-05-24T08:11:13Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"This will enable further refactoring, and it is more coherent and\nsimpler if all the option_parse_*() functions are passed a\n'struct apply_state' instance in opt->value.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 13 ++++++-------\n 1 file changed, 6 insertions(+), 7 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 44717b2..78205f8 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -61,6 +61,7 @@ struct apply_state {\n \tint has_include;\n \n \t/* These control whitespace errors */\n+\tconst char *whitespace_option;\n \tint whitespace_error;\n };\n \n@@ -4619,9 +4620,9 @@ static int option_parse_space_change(const struct option *opt,\n static int option_parse_whitespace(const struct option *opt,\n \t\t\t\t   const char *arg, int unset)\n {\n-\tconst char **whitespace_option = opt->value;\n+\tstruct apply_state *state = opt->value;\n \n-\t*whitespace_option = arg;\n+\tstate->whitespace_option = arg;\n \tparse_whitespace_option(arg);\n \treturn 0;\n }\n@@ -4670,8 +4671,6 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \tint read_stdin = 1;\n \tstruct apply_state state;\n \n-\tconst char *whitespace_option = NULL;\n-\n \tstruct option builtin_apply_options[] = {\n \t\t{ OPTION_CALLBACK, 0, \"exclude\", &state, N_(\"path\"),\n \t\t\tN_(\"don't apply changes matching the given path\"),\n@@ -4711,7 +4710,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t\t\tN_(\"paths are separated with NUL character\"), '\\0'),\n \t\tOPT_INTEGER('C', NULL, &state.p_context,\n \t\t\t\tN_(\"ensure at least <n> lines of context match\")),\n-\t\t{ OPTION_CALLBACK, 0, \"whitespace\", &whitespace_option, N_(\"action\"),\n+\t\t{ OPTION_CALLBACK, 0, \"whitespace\", &state, N_(\"action\"),\n \t\t\tN_(\"detect new or modified lines that have whitespace errors\"),\n \t\t\t0, option_parse_whitespace },\n \t\t{ OPTION_CALLBACK, 0, \"ignore-space-change\", NULL, NULL,\n@@ -4786,11 +4785,11 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t\tif (fd < 0)\n \t\t\tdie_errno(_(\"can't open patch '%s'\"), arg);\n \t\tread_stdin = 0;\n-\t\tset_default_whitespace_mode(&state, whitespace_option);\n+\t\tset_default_whitespace_mode(&state, state.whitespace_option);\n \t\terrs |= apply_patch(&state, fd, arg, options);\n \t\tclose(fd);\n \t}\n-\tset_default_whitespace_mode(&state, whitespace_option);\n+\tset_default_whitespace_mode(&state, state.whitespace_option);\n \tif (read_stdin)\n \t\terrs |= apply_patch(&state, 0, \"<stdin>\", options);\n \tif (state.whitespace_error) {\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287359","messageId":"20160524081126.16973-38-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 37/49] builtin/apply: remove whitespace_option arg from set_default_whitespace_mode()","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:11:14Z","receivedAt":"2016-05-24T08:11:14Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"A previous change has move the whitespace_option variable from cmd_apply\ninto 'struct apply_state', so that we can now avoid passing it separately\nto set_default_whitespace_mode().\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 9 ++++-----\n 1 file changed, 4 insertions(+), 5 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 78205f8..523ed74 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -132,10 +132,9 @@ static void parse_ignorewhitespace_option(const char *option)\n \tdie(_(\"unrecognized whitespace ignore option '%s'\"), option);\n }\n \n-static void set_default_whitespace_mode(struct apply_state *state,\n-\t\t\t\t\tconst char *whitespace_option)\n+static void set_default_whitespace_mode(struct apply_state *state)\n {\n-\tif (!whitespace_option && !apply_default_whitespace)\n+\tif (!state->whitespace_option && !apply_default_whitespace)\n \t\tws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error);\n }\n \n@@ -4785,11 +4784,11 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t\tif (fd < 0)\n \t\t\tdie_errno(_(\"can't open patch '%s'\"), arg);\n \t\tread_stdin = 0;\n-\t\tset_default_whitespace_mode(&state, state.whitespace_option);\n+\t\tset_default_whitespace_mode(&state);\n \t\terrs |= apply_patch(&state, fd, arg, options);\n \t\tclose(fd);\n \t}\n-\tset_default_whitespace_mode(&state, state.whitespace_option);\n+\tset_default_whitespace_mode(&state);\n \tif (read_stdin)\n \t\terrs |= apply_patch(&state, 0, \"<stdin>\", options);\n \tif (state.whitespace_error) {\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287366","messageId":"20160524081126.16973-39-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 38/49] builtin/apply: move 'squelch_whitespace_errors' into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:11:15Z","receivedAt":"2016-05-24T08:11:15Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify the apply functionality the 'squelch_whitespace_errors' variable should\nnot be static and global to the file. Let's move it into\n'struct apply_state'.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 22 +++++++++++-----------\n 1 file changed, 11 insertions(+), 11 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 523ed74..619b8fb 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -63,6 +63,7 @@ struct apply_state {\n \t/* These control whitespace errors */\n \tconst char *whitespace_option;\n \tint whitespace_error;\n+\tint squelch_whitespace_errors;\n };\n \n static int newfd = -1;\n@@ -78,7 +79,6 @@ static enum ws_error_action {\n \tdie_on_ws_error,\n \tcorrect_ws_error\n } ws_error_action = warn_on_ws_error;\n-static int squelch_whitespace_errors = 5;\n static int applied_after_fixing_ws;\n \n static enum ws_ignore {\n@@ -87,7 +87,7 @@ static enum ws_ignore {\n } ws_ignore_action = ignore_ws_none;\n \n \n-static void parse_whitespace_option(const char *option)\n+static void parse_whitespace_option(struct apply_state *state, const char *option)\n {\n \tif (!option) {\n \t\tws_error_action = warn_on_ws_error;\n@@ -107,7 +107,7 @@ static void parse_whitespace_option(const char *option)\n \t}\n \tif (!strcmp(option, \"error-all\")) {\n \t\tws_error_action = die_on_ws_error;\n-\t\tsquelch_whitespace_errors = 0;\n+\t\tstate->squelch_whitespace_errors = 0;\n \t\treturn;\n \t}\n \tif (!strcmp(option, \"strip\") || !strcmp(option, \"fix\")) {\n@@ -1599,8 +1599,8 @@ static void record_ws_error(struct apply_state *state,\n \t\treturn;\n \n \tstate->whitespace_error++;\n-\tif (squelch_whitespace_errors &&\n-\t    squelch_whitespace_errors < state->whitespace_error)\n+\tif (state->squelch_whitespace_errors &&\n+\t    state->squelch_whitespace_errors < state->whitespace_error)\n \t\treturn;\n \n \terr = whitespace_error_string(result);\n@@ -4620,9 +4620,8 @@ static int option_parse_whitespace(const struct option *opt,\n \t\t\t\t   const char *arg, int unset)\n {\n \tstruct apply_state *state = opt->value;\n-\n \tstate->whitespace_option = arg;\n-\tparse_whitespace_option(arg);\n+\tparse_whitespace_option(state, arg);\n \treturn 0;\n }\n \n@@ -4645,11 +4644,12 @@ static void init_apply_state(struct apply_state *state, const char *prefix)\n \tstate->line_termination = '\\n';\n \tstate->p_value = 1;\n \tstate->p_context = UINT_MAX;\n+\tstate->squelch_whitespace_errors = 5;\n \tstrbuf_init(&state->root, 0);\n \n \tgit_apply_config();\n \tif (apply_default_whitespace)\n-\t\tparse_whitespace_option(apply_default_whitespace);\n+\t\tparse_whitespace_option(state, apply_default_whitespace);\n \tif (apply_default_ignorewhitespace)\n \t\tparse_ignorewhitespace_option(apply_default_ignorewhitespace);\n }\n@@ -4792,10 +4792,10 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \tif (read_stdin)\n \t\terrs |= apply_patch(&state, 0, \"<stdin>\", options);\n \tif (state.whitespace_error) {\n-\t\tif (squelch_whitespace_errors &&\n-\t\t    squelch_whitespace_errors < state.whitespace_error) {\n+\t\tif (state.squelch_whitespace_errors &&\n+\t\t    state.squelch_whitespace_errors < state.whitespace_error) {\n \t\t\tint squelched =\n-\t\t\t\tstate.whitespace_error - squelch_whitespace_errors;\n+\t\t\t\tstate.whitespace_error - state.squelch_whitespace_errors;\n \t\t\twarning(Q_(\"squelched %d whitespace error\",\n \t\t\t\t   \"squelched %d whitespace errors\",\n \t\t\t\t   squelched),\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287360","messageId":"20160524081126.16973-40-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 39/49] builtin/apply: move 'applied_after_fixing_ws' into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:11:16Z","receivedAt":"2016-05-24T08:11:16Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify the apply functionality the 'applied_after_fixing_ws' variable should\nnot be static and global to the file. Let's move it into\n'struct apply_state'.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 10 +++++-----\n 1 file changed, 5 insertions(+), 5 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 619b8fb..91b6283 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -64,6 +64,7 @@ struct apply_state {\n \tconst char *whitespace_option;\n \tint whitespace_error;\n \tint squelch_whitespace_errors;\n+\tint applied_after_fixing_ws;\n };\n \n static int newfd = -1;\n@@ -79,7 +80,6 @@ static enum ws_error_action {\n \tdie_on_ws_error,\n \tcorrect_ws_error\n } ws_error_action = warn_on_ws_error;\n-static int applied_after_fixing_ws;\n \n static enum ws_ignore {\n \tignore_ws_none,\n@@ -2862,7 +2862,7 @@ static int apply_one_fragment(struct apply_state *state,\n \t\t\t\tstrbuf_add(&newlines, patch + 1, plen);\n \t\t\t}\n \t\t\telse {\n-\t\t\t\tws_fix_copy(&newlines, patch + 1, plen, ws_rule, &applied_after_fixing_ws);\n+\t\t\t\tws_fix_copy(&newlines, patch + 1, plen, ws_rule, &state->applied_after_fixing_ws);\n \t\t\t}\n \t\t\tadd_line_info(&postimage, newlines.buf + start, newlines.len - start,\n \t\t\t\t      (first == '+' ? 0 : LINE_COMMON));\n@@ -4806,11 +4806,11 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t\t\t       \"%d lines add whitespace errors.\",\n \t\t\t       state.whitespace_error),\n \t\t\t    state.whitespace_error);\n-\t\tif (applied_after_fixing_ws && state.apply)\n+\t\tif (state.applied_after_fixing_ws && state.apply)\n \t\t\twarning(\"%d line%s applied after\"\n \t\t\t\t\" fixing whitespace errors.\",\n-\t\t\t\tapplied_after_fixing_ws,\n-\t\t\t\tapplied_after_fixing_ws == 1 ? \"\" : \"s\");\n+\t\t\t\tstate.applied_after_fixing_ws,\n+\t\t\t\tstate.applied_after_fixing_ws == 1 ? \"\" : \"s\");\n \t\telse if (state.whitespace_error)\n \t\t\twarning(Q_(\"%d line adds whitespace errors.\",\n \t\t\t\t   \"%d lines add whitespace errors.\",\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287365","messageId":"20160524081126.16973-41-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 40/49] builtin/apply: move 'ws_error_action' into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:11:17Z","receivedAt":"2016-05-24T08:11:17Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify the apply functionality the 'ws_error_action' variable should\nnot be static and global to the file. Let's move it into\n'struct apply_state'.\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 61 +++++++++++++++++++++++++++++++--------------------------\n 1 file changed, 33 insertions(+), 28 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 91b6283..61d809a 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -21,6 +21,13 @@\n #include \"ll-merge.h\"\n #include \"rerere.h\"\n \n+enum ws_error_action {\n+\tnowarn_ws_error,\n+\twarn_on_ws_error,\n+\tdie_on_ws_error,\n+\tcorrect_ws_error\n+};\n+\n struct apply_state {\n \tconst char *prefix;\n \tint prefix_length;\n@@ -61,6 +68,7 @@ struct apply_state {\n \tint has_include;\n \n \t/* These control whitespace errors */\n+\tenum ws_error_action ws_error_action;\n \tconst char *whitespace_option;\n \tint whitespace_error;\n \tint squelch_whitespace_errors;\n@@ -74,12 +82,6 @@ static const char * const apply_usage[] = {\n \tNULL\n };\n \n-static enum ws_error_action {\n-\tnowarn_ws_error,\n-\twarn_on_ws_error,\n-\tdie_on_ws_error,\n-\tcorrect_ws_error\n-} ws_error_action = warn_on_ws_error;\n \n static enum ws_ignore {\n \tignore_ws_none,\n@@ -90,28 +92,28 @@ static enum ws_ignore {\n static void parse_whitespace_option(struct apply_state *state, const char *option)\n {\n \tif (!option) {\n-\t\tws_error_action = warn_on_ws_error;\n+\t\tstate->ws_error_action = warn_on_ws_error;\n \t\treturn;\n \t}\n \tif (!strcmp(option, \"warn\")) {\n-\t\tws_error_action = warn_on_ws_error;\n+\t\tstate->ws_error_action = warn_on_ws_error;\n \t\treturn;\n \t}\n \tif (!strcmp(option, \"nowarn\")) {\n-\t\tws_error_action = nowarn_ws_error;\n+\t\tstate->ws_error_action = nowarn_ws_error;\n \t\treturn;\n \t}\n \tif (!strcmp(option, \"error\")) {\n-\t\tws_error_action = die_on_ws_error;\n+\t\tstate->ws_error_action = die_on_ws_error;\n \t\treturn;\n \t}\n \tif (!strcmp(option, \"error-all\")) {\n-\t\tws_error_action = die_on_ws_error;\n+\t\tstate->ws_error_action = die_on_ws_error;\n \t\tstate->squelch_whitespace_errors = 0;\n \t\treturn;\n \t}\n \tif (!strcmp(option, \"strip\") || !strcmp(option, \"fix\")) {\n-\t\tws_error_action = correct_ws_error;\n+\t\tstate->ws_error_action = correct_ws_error;\n \t\treturn;\n \t}\n \tdie(_(\"unrecognized whitespace option '%s'\"), option);\n@@ -135,7 +137,7 @@ static void parse_ignorewhitespace_option(const char *option)\n static void set_default_whitespace_mode(struct apply_state *state)\n {\n \tif (!state->whitespace_option && !apply_default_whitespace)\n-\t\tws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error);\n+\t\tstate->ws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error);\n }\n \n /*\n@@ -1670,12 +1672,12 @@ static int parse_fragment(struct apply_state *state,\n \t\t\t\tleading++;\n \t\t\ttrailing++;\n \t\t\tif (!state->apply_in_reverse &&\n-\t\t\t    ws_error_action == correct_ws_error)\n+\t\t\t    state->ws_error_action == correct_ws_error)\n \t\t\t\tcheck_whitespace(state, line, len, patch->ws_rule);\n \t\t\tbreak;\n \t\tcase '-':\n \t\t\tif (state->apply_in_reverse &&\n-\t\t\t    ws_error_action != nowarn_ws_error)\n+\t\t\t    state->ws_error_action != nowarn_ws_error)\n \t\t\t\tcheck_whitespace(state, line, len, patch->ws_rule);\n \t\t\tdeleted++;\n \t\t\toldlines--;\n@@ -1683,7 +1685,7 @@ static int parse_fragment(struct apply_state *state,\n \t\t\tbreak;\n \t\tcase '+':\n \t\t\tif (!state->apply_in_reverse &&\n-\t\t\t    ws_error_action != nowarn_ws_error)\n+\t\t\t    state->ws_error_action != nowarn_ws_error)\n \t\t\t\tcheck_whitespace(state, line, len, patch->ws_rule);\n \t\t\tadded++;\n \t\t\tnewlines--;\n@@ -2396,7 +2398,8 @@ static int line_by_line_fuzzy_match(struct image *img,\n \treturn 1;\n }\n \n-static int match_fragment(struct image *img,\n+static int match_fragment(struct apply_state *state,\n+\t\t\t  struct image *img,\n \t\t\t  struct image *preimage,\n \t\t\t  struct image *postimage,\n \t\t\t  unsigned long try,\n@@ -2417,7 +2420,7 @@ static int match_fragment(struct image *img,\n \t\tpreimage_limit = preimage->nr;\n \t\tif (match_end && (preimage->nr + try_lno != img->nr))\n \t\t\treturn 0;\n-\t} else if (ws_error_action == correct_ws_error &&\n+\t} else if (state->ws_error_action == correct_ws_error &&\n \t\t   (ws_rule & WS_BLANK_AT_EOF)) {\n \t\t/*\n \t\t * This hunk extends beyond the end of img, and we are\n@@ -2489,7 +2492,7 @@ static int match_fragment(struct image *img,\n \t\treturn line_by_line_fuzzy_match(img, preimage, postimage,\n \t\t\t\t\t\ttry, try_lno, preimage_limit);\n \n-\tif (ws_error_action != correct_ws_error)\n+\tif (state->ws_error_action != correct_ws_error)\n \t\treturn 0;\n \n \t/*\n@@ -2601,7 +2604,8 @@ static int match_fragment(struct image *img,\n \treturn 0;\n }\n \n-static int find_pos(struct image *img,\n+static int find_pos(struct apply_state *state,\n+\t\t    struct image *img,\n \t\t    struct image *preimage,\n \t\t    struct image *postimage,\n \t\t    int line,\n@@ -2645,7 +2649,7 @@ static int find_pos(struct image *img,\n \ttry_lno = line;\n \n \tfor (i = 0; ; i++) {\n-\t\tif (match_fragment(img, preimage, postimage,\n+\t\tif (match_fragment(state, img, preimage, postimage,\n \t\t\t\t   try, try_lno, ws_rule,\n \t\t\t\t   match_beginning, match_end))\n \t\t\treturn try_lno;\n@@ -2858,7 +2862,7 @@ static int apply_one_fragment(struct apply_state *state,\n \t\t\tstart = newlines.len;\n \t\t\tif (first != '+' ||\n \t\t\t    !state->whitespace_error ||\n-\t\t\t    ws_error_action != correct_ws_error) {\n+\t\t\t    state->ws_error_action != correct_ws_error) {\n \t\t\t\tstrbuf_add(&newlines, patch + 1, plen);\n \t\t\t}\n \t\t\telse {\n@@ -2936,7 +2940,7 @@ static int apply_one_fragment(struct apply_state *state,\n \n \tfor (;;) {\n \n-\t\tapplied_pos = find_pos(img, &preimage, &postimage, pos,\n+\t\tapplied_pos = find_pos(state, img, &preimage, &postimage, pos,\n \t\t\t\t       ws_rule, match_beginning, match_end);\n \n \t\tif (applied_pos >= 0)\n@@ -2972,10 +2976,10 @@ static int apply_one_fragment(struct apply_state *state,\n \t\tif (new_blank_lines_at_end &&\n \t\t    preimage.nr + applied_pos >= img->nr &&\n \t\t    (ws_rule & WS_BLANK_AT_EOF) &&\n-\t\t    ws_error_action != nowarn_ws_error) {\n+\t\t    state->ws_error_action != nowarn_ws_error) {\n \t\t\trecord_ws_error(state, WS_BLANK_AT_EOF, \"+\", 1,\n \t\t\t\t\tfound_new_blank_lines_at_end);\n-\t\t\tif (ws_error_action == correct_ws_error) {\n+\t\t\tif (state->ws_error_action == correct_ws_error) {\n \t\t\t\twhile (new_blank_lines_at_end--)\n \t\t\t\t\tremove_last_line(&postimage);\n \t\t\t}\n@@ -2986,7 +2990,7 @@ static int apply_one_fragment(struct apply_state *state,\n \t\t\t * apply_patch->check_patch_list->check_patch->\n \t\t\t * apply_data->apply_fragments->apply_one_fragment\n \t\t\t */\n-\t\t\tif (ws_error_action == die_on_ws_error)\n+\t\t\tif (state->ws_error_action == die_on_ws_error)\n \t\t\t\tstate->apply = 0;\n \t\t}\n \n@@ -4530,7 +4534,7 @@ static int apply_patch(struct apply_state *state,\n \tif (!list && !skipped_patch)\n \t\tdie(_(\"unrecognized input\"));\n \n-\tif (state->whitespace_error && (ws_error_action == die_on_ws_error))\n+\tif (state->whitespace_error && (state->ws_error_action == die_on_ws_error))\n \t\tstate->apply = 0;\n \n \tstate->update_index = state->check_index && state->apply;\n@@ -4645,6 +4649,7 @@ static void init_apply_state(struct apply_state *state, const char *prefix)\n \tstate->p_value = 1;\n \tstate->p_context = UINT_MAX;\n \tstate->squelch_whitespace_errors = 5;\n+\tstate->ws_error_action = warn_on_ws_error;\n \tstrbuf_init(&state->root, 0);\n \n \tgit_apply_config();\n@@ -4801,7 +4806,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t\t\t\t   squelched),\n \t\t\t\tsquelched);\n \t\t}\n-\t\tif (ws_error_action == die_on_ws_error)\n+\t\tif (state.ws_error_action == die_on_ws_error)\n \t\t\tdie(Q_(\"%d line adds whitespace errors.\",\n \t\t\t       \"%d lines add whitespace errors.\",\n \t\t\t       state.whitespace_error),\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287357","messageId":"20160524081126.16973-42-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 41/49] builtin/apply: move 'ws_ignore_action' into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:11:18Z","receivedAt":"2016-05-24T08:11:18Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify the apply functionality the 'ws_ignore_action' variable should\nnot be static and global to the file. Let's move it into\n'struct apply_state'.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 37 ++++++++++++++++++++-----------------\n 1 file changed, 20 insertions(+), 17 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 61d809a..e5bc9cc 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -28,6 +28,12 @@ enum ws_error_action {\n \tcorrect_ws_error\n };\n \n+\n+enum ws_ignore {\n+\tignore_ws_none,\n+\tignore_ws_change\n+};\n+\n struct apply_state {\n \tconst char *prefix;\n \tint prefix_length;\n@@ -69,6 +75,7 @@ struct apply_state {\n \n \t/* These control whitespace errors */\n \tenum ws_error_action ws_error_action;\n+\tenum ws_ignore ws_ignore_action;\n \tconst char *whitespace_option;\n \tint whitespace_error;\n \tint squelch_whitespace_errors;\n@@ -82,13 +89,6 @@ static const char * const apply_usage[] = {\n \tNULL\n };\n \n-\n-static enum ws_ignore {\n-\tignore_ws_none,\n-\tignore_ws_change\n-} ws_ignore_action = ignore_ws_none;\n-\n-\n static void parse_whitespace_option(struct apply_state *state, const char *option)\n {\n \tif (!option) {\n@@ -119,16 +119,17 @@ static void parse_whitespace_option(struct apply_state *state, const char *optio\n \tdie(_(\"unrecognized whitespace option '%s'\"), option);\n }\n \n-static void parse_ignorewhitespace_option(const char *option)\n+static void parse_ignorewhitespace_option(struct apply_state *state,\n+\t\t\t\t\t  const char *option)\n {\n \tif (!option || !strcmp(option, \"no\") ||\n \t    !strcmp(option, \"false\") || !strcmp(option, \"never\") ||\n \t    !strcmp(option, \"none\")) {\n-\t\tws_ignore_action = ignore_ws_none;\n+\t\tstate->ws_ignore_action = ignore_ws_none;\n \t\treturn;\n \t}\n \tif (!strcmp(option, \"change\")) {\n-\t\tws_ignore_action = ignore_ws_change;\n+\t\tstate->ws_ignore_action = ignore_ws_change;\n \t\treturn;\n \t}\n \tdie(_(\"unrecognized whitespace ignore option '%s'\"), option);\n@@ -2488,7 +2489,7 @@ static int match_fragment(struct apply_state *state,\n \t * fuzzy matching. We collect all the line length information because\n \t * we need it to adjust whitespace if we match.\n \t */\n-\tif (ws_ignore_action == ignore_ws_change)\n+\tif (state->ws_ignore_action == ignore_ws_change)\n \t\treturn line_by_line_fuzzy_match(img, preimage, postimage,\n \t\t\t\t\t\ttry, try_lno, preimage_limit);\n \n@@ -4611,12 +4612,13 @@ static int option_parse_p(const struct option *opt,\n }\n \n static int option_parse_space_change(const struct option *opt,\n-\t\t\t  const char *arg, int unset)\n+\t\t\t\t     const char *arg, int unset)\n {\n+\tstruct apply_state *state = opt->value;\n \tif (unset)\n-\t\tws_ignore_action = ignore_ws_none;\n+\t\tstate->ws_ignore_action = ignore_ws_none;\n \telse\n-\t\tws_ignore_action = ignore_ws_change;\n+\t\tstate->ws_ignore_action = ignore_ws_change;\n \treturn 0;\n }\n \n@@ -4650,13 +4652,14 @@ static void init_apply_state(struct apply_state *state, const char *prefix)\n \tstate->p_context = UINT_MAX;\n \tstate->squelch_whitespace_errors = 5;\n \tstate->ws_error_action = warn_on_ws_error;\n+\tstate->ws_ignore_action = ignore_ws_none;\n \tstrbuf_init(&state->root, 0);\n \n \tgit_apply_config();\n \tif (apply_default_whitespace)\n \t\tparse_whitespace_option(state, apply_default_whitespace);\n \tif (apply_default_ignorewhitespace)\n-\t\tparse_ignorewhitespace_option(apply_default_ignorewhitespace);\n+\t\tparse_ignorewhitespace_option(state, apply_default_ignorewhitespace);\n }\n \n static void clear_apply_state(struct apply_state *state)\n@@ -4717,10 +4720,10 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t\t{ OPTION_CALLBACK, 0, \"whitespace\", &state, N_(\"action\"),\n \t\t\tN_(\"detect new or modified lines that have whitespace errors\"),\n \t\t\t0, option_parse_whitespace },\n-\t\t{ OPTION_CALLBACK, 0, \"ignore-space-change\", NULL, NULL,\n+\t\t{ OPTION_CALLBACK, 0, \"ignore-space-change\", &state, NULL,\n \t\t\tN_(\"ignore changes in whitespace when finding context\"),\n \t\t\tPARSE_OPT_NOARG, option_parse_space_change },\n-\t\t{ OPTION_CALLBACK, 0, \"ignore-whitespace\", NULL, NULL,\n+\t\t{ OPTION_CALLBACK, 0, \"ignore-whitespace\", &state, NULL,\n \t\t\tN_(\"ignore changes in whitespace when finding context\"),\n \t\t\tPARSE_OPT_NOARG, option_parse_space_change },\n \t\tOPT_BOOL('R', \"reverse\", &state.apply_in_reverse,\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287367","messageId":"20160524081126.16973-43-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 42/49] builtin/apply: move 'max_change' and 'max_len' into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:11:19Z","receivedAt":"2016-05-24T08:11:19Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify the apply functionality the 'max_change' and 'max_len'\nvariables should not be static and global to the file. Let's move\nthem into 'struct apply_state'.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 49 +++++++++++++++++++++++++------------------------\n 1 file changed, 25 insertions(+), 24 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex e5bc9cc..9e7d181 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -73,6 +73,14 @@ struct apply_state {\n \tstruct string_list limit_by_name;\n \tint has_include;\n \n+\t/*\n+\t * For \"diff-stat\" like behaviour, we keep track of the biggest change\n+\t * we've seen, and the longest filename. That allows us to do simple\n+\t * scaling.\n+\t */\n+\tint max_change;\n+\tint max_len;\n+\n \t/* These control whitespace errors */\n \tenum ws_error_action ws_error_action;\n \tenum ws_ignore ws_ignore_action;\n@@ -141,13 +149,6 @@ static void set_default_whitespace_mode(struct apply_state *state)\n \t\tstate->ws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error);\n }\n \n-/*\n- * For \"diff-stat\" like behaviour, we keep track of the biggest change\n- * we've seen, and the longest filename. That allows us to do simple\n- * scaling.\n- */\n-static int max_change, max_len;\n-\n /*\n  * Various \"current state\", notably line numbers and what\n  * file (and how) we're patching right now.. The \"is_xxxx\"\n@@ -2172,7 +2173,7 @@ static const char pluses[] =\n static const char minuses[]=\n \"----------------------------------------------------------------------\";\n \n-static void show_stats(struct patch *patch)\n+static void show_stats(struct apply_state *state, struct patch *patch)\n {\n \tstruct strbuf qname = STRBUF_INIT;\n \tchar *cp = patch->new_name ? patch->new_name : patch->old_name;\n@@ -2183,7 +2184,7 @@ static void show_stats(struct patch *patch)\n \t/*\n \t * \"scale\" the filename\n \t */\n-\tmax = max_len;\n+\tmax = state->max_len;\n \tif (max > 50)\n \t\tmax = 50;\n \n@@ -2206,13 +2207,13 @@ static void show_stats(struct patch *patch)\n \t/*\n \t * scale the add/delete\n \t */\n-\tmax = max + max_change > 70 ? 70 - max : max_change;\n+\tmax = max + state->max_change > 70 ? 70 - max : state->max_change;\n \tadd = patch->lines_added;\n \tdel = patch->lines_deleted;\n \n-\tif (max_change > 0) {\n-\t\tint total = ((add + del) * max + max_change / 2) / max_change;\n-\t\tadd = (add * max + max_change / 2) / max_change;\n+\tif (state->max_change > 0) {\n+\t\tint total = ((add + del) * max + state->max_change / 2) / state->max_change;\n+\t\tadd = (add * max + state->max_change / 2) / state->max_change;\n \t\tdel = total - add;\n \t}\n \tprintf(\"%5d %.*s%.*s\\n\", patch->lines_added + patch->lines_deleted,\n@@ -4038,7 +4039,7 @@ static void build_fake_ancestor(struct patch *list, const char *filename)\n \tdiscard_index(&result);\n }\n \n-static void stat_patch_list(struct patch *patch)\n+static void stat_patch_list(struct apply_state *state, struct patch *patch)\n {\n \tint files, adds, dels;\n \n@@ -4046,7 +4047,7 @@ static void stat_patch_list(struct patch *patch)\n \t\tfiles++;\n \t\tadds += patch->lines_added;\n \t\tdels += patch->lines_deleted;\n-\t\tshow_stats(patch);\n+\t\tshow_stats(state, patch);\n \t}\n \n \tprint_stat_summary(stdout, files, adds, dels);\n@@ -4144,25 +4145,25 @@ static void summary_patch_list(struct patch *patch)\n \t}\n }\n \n-static void patch_stats(struct patch *patch)\n+static void patch_stats(struct apply_state *state, struct patch *patch)\n {\n \tint lines = patch->lines_added + patch->lines_deleted;\n \n-\tif (lines > max_change)\n-\t\tmax_change = lines;\n+\tif (lines > state->max_change)\n+\t\tstate->max_change = lines;\n \tif (patch->old_name) {\n \t\tint len = quote_c_style(patch->old_name, NULL, NULL, 0);\n \t\tif (!len)\n \t\t\tlen = strlen(patch->old_name);\n-\t\tif (len > max_len)\n-\t\t\tmax_len = len;\n+\t\tif (len > state->max_len)\n+\t\t\tstate->max_len = len;\n \t}\n \tif (patch->new_name) {\n \t\tint len = quote_c_style(patch->new_name, NULL, NULL, 0);\n \t\tif (!len)\n \t\t\tlen = strlen(patch->new_name);\n-\t\tif (len > max_len)\n-\t\t\tmax_len = len;\n+\t\tif (len > state->max_len)\n+\t\t\tstate->max_len = len;\n \t}\n }\n \n@@ -4519,7 +4520,7 @@ static int apply_patch(struct apply_state *state,\n \t\tif (state->apply_in_reverse)\n \t\t\treverse_patches(patch);\n \t\tif (use_patch(state, patch)) {\n-\t\t\tpatch_stats(patch);\n+\t\t\tpatch_stats(state, patch);\n \t\t\t*listp = patch;\n \t\t\tlistp = &patch->next;\n \t\t}\n@@ -4563,7 +4564,7 @@ static int apply_patch(struct apply_state *state,\n \t\tbuild_fake_ancestor(list, state->fake_ancestor);\n \n \tif (state->diffstat)\n-\t\tstat_patch_list(list);\n+\t\tstat_patch_list(state, list);\n \n \tif (state->numstat)\n \t\tnumstat_patch_list(state, list);\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287370","messageId":"20160524081126.16973-44-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 43/49] builtin/apply: move 'state_linenr' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:11:20Z","receivedAt":"2016-05-24T08:11:20Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify the apply functionality the 'state_linenr' variable should\nnot be static and global to the file. Let's move it into\n'struct apply_state'.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 71 +++++++++++++++++++++++++++++----------------------------\n 1 file changed, 36 insertions(+), 35 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 9e7d181..dd56a8e 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -73,6 +73,9 @@ struct apply_state {\n \tstruct string_list limit_by_name;\n \tint has_include;\n \n+\t/* Various \"current state\" */\n+\tint linenr; /* current line number */\n+\n \t/*\n \t * For \"diff-stat\" like behaviour, we keep track of the biggest change\n \t * we've seen, and the longest filename. That allows us to do simple\n@@ -149,13 +152,6 @@ static void set_default_whitespace_mode(struct apply_state *state)\n \t\tstate->ws_error_action = (state->apply ? warn_on_ws_error : nowarn_ws_error);\n }\n \n-/*\n- * Various \"current state\", notably line numbers and what\n- * file (and how) we're patching right now.. The \"is_xxxx\"\n- * things are flags, where -1 means \"don't know yet\".\n- */\n-static int state_linenr = 1;\n-\n /*\n  * This represents one \"hunk\" from a patch, starting with\n  * \"@@ -oldpos,oldlines +newpos,newlines @@\" marker.  The\n@@ -932,7 +928,7 @@ static void parse_traditional_patch(struct apply_state *state,\n \t\t}\n \t}\n \tif (!name)\n-\t\tdie(_(\"unable to find filename in patch at line %d\"), state_linenr);\n+\t\tdie(_(\"unable to find filename in patch at line %d\"), state->linenr);\n }\n \n static int gitdiff_hdrend(struct apply_state *state,\n@@ -970,17 +966,17 @@ static void gitdiff_verify_name(struct apply_state *state,\n \t\tchar *another;\n \t\tif (isnull)\n \t\t\tdie(_(\"git apply: bad git-diff - expected /dev/null, got %s on line %d\"),\n-\t\t\t    *name, state_linenr);\n+\t\t\t    *name, state->linenr);\n \t\tanother = find_name(state, line, NULL, state->p_value, TERM_TAB);\n \t\tif (!another || memcmp(another, *name, len + 1))\n \t\t\tdie((side == DIFF_NEW_NAME) ?\n \t\t\t    _(\"git apply: bad git-diff - inconsistent new filename on line %d\") :\n-\t\t\t    _(\"git apply: bad git-diff - inconsistent old filename on line %d\"), state_linenr);\n+\t\t\t    _(\"git apply: bad git-diff - inconsistent old filename on line %d\"), state->linenr);\n \t\tfree(another);\n \t} else {\n \t\t/* expect \"/dev/null\" */\n \t\tif (memcmp(\"/dev/null\", line, 9) || line[9] != '\\n')\n-\t\t\tdie(_(\"git apply: bad git-diff - expected /dev/null on line %d\"), state_linenr);\n+\t\t\tdie(_(\"git apply: bad git-diff - expected /dev/null on line %d\"), state->linenr);\n \t}\n }\n \n@@ -1343,8 +1339,8 @@ static int parse_git_header(struct apply_state *state,\n \n \tline += len;\n \tsize -= len;\n-\tstate_linenr++;\n-\tfor (offset = len ; size > 0 ; offset += len, size -= len, line += len, state_linenr++) {\n+\tstate->linenr++;\n+\tfor (offset = len ; size > 0 ; offset += len, size -= len, line += len, state->linenr++) {\n \t\tstatic const struct opentry {\n \t\t\tconst char *str;\n \t\t\tint (*fn)(struct apply_state *, const char *, struct patch *);\n@@ -1515,7 +1511,7 @@ static int find_header(struct apply_state *state,\n \tpatch->is_new = patch->is_delete = -1;\n \tpatch->old_mode = patch->new_mode = 0;\n \tpatch->old_name = patch->new_name = NULL;\n-\tfor (offset = 0; size > 0; offset += len, size -= len, line += len, state_linenr++) {\n+\tfor (offset = 0; size > 0; offset += len, size -= len, line += len, state->linenr++) {\n \t\tunsigned long nextlen;\n \n \t\tlen = linelen(line, size);\n@@ -1536,7 +1532,7 @@ static int find_header(struct apply_state *state,\n \t\t\tif (parse_fragment_header(line, len, &dummy) < 0)\n \t\t\t\tcontinue;\n \t\t\tdie(_(\"patch fragment without header at line %d: %.*s\"),\n-\t\t\t    state_linenr, (int)len-1, line);\n+\t\t\t    state->linenr, (int)len-1, line);\n \t\t}\n \n \t\tif (size < len + 6)\n@@ -1557,13 +1553,13 @@ static int find_header(struct apply_state *state,\n \t\t\t\t\t       \"git diff header lacks filename information when removing \"\n \t\t\t\t\t       \"%d leading pathname components (line %d)\",\n \t\t\t\t\t       state->p_value),\n-\t\t\t\t\t    state->p_value, state_linenr);\n+\t\t\t\t\t    state->p_value, state->linenr);\n \t\t\t\tpatch->old_name = xstrdup(patch->def_name);\n \t\t\t\tpatch->new_name = xstrdup(patch->def_name);\n \t\t\t}\n \t\t\tif (!patch->is_delete && !patch->new_name)\n \t\t\t\tdie(\"git diff header lacks filename information \"\n-\t\t\t\t    \"(line %d)\", state_linenr);\n+\t\t\t\t    \"(line %d)\", state->linenr);\n \t\t\tpatch->is_toplevel_relative = 1;\n \t\t\t*hdrsize = git_hdr_len;\n \t\t\treturn offset;\n@@ -1585,7 +1581,7 @@ static int find_header(struct apply_state *state,\n \t\t/* Ok, we'll consider it a patch */\n \t\tparse_traditional_patch(state, line, line+len, patch);\n \t\t*hdrsize = len + nextlen;\n-\t\tstate_linenr += 2;\n+\t\tstate->linenr += 2;\n \t\treturn offset;\n \t}\n \treturn -1;\n@@ -1620,7 +1616,7 @@ static void check_whitespace(struct apply_state *state,\n {\n \tunsigned result = ws_check(line + 1, len - 1, ws_rule);\n \n-\trecord_ws_error(state, result, line + 1, len - 2, state_linenr);\n+\trecord_ws_error(state, result, line + 1, len - 2, state->linenr);\n }\n \n /*\n@@ -1653,11 +1649,11 @@ static int parse_fragment(struct apply_state *state,\n \t/* Parse the thing.. */\n \tline += len;\n \tsize -= len;\n-\tstate_linenr++;\n+\tstate->linenr++;\n \tadded = deleted = 0;\n \tfor (offset = len;\n \t     0 < size;\n-\t     offset += len, size -= len, line += len, state_linenr++) {\n+\t     offset += len, size -= len, line += len, state->linenr++) {\n \t\tif (!oldlines && !newlines)\n \t\t\tbreak;\n \t\tlen = linelen(line, size);\n@@ -1756,10 +1752,10 @@ static int parse_single_patch(struct apply_state *state,\n \t\tint len;\n \n \t\tfragment = xcalloc(1, sizeof(*fragment));\n-\t\tfragment->linenr = state_linenr;\n+\t\tfragment->linenr = state->linenr;\n \t\tlen = parse_fragment(state, line, size, patch, fragment);\n \t\tif (len <= 0)\n-\t\t\tdie(_(\"corrupt patch at line %d\"), state_linenr);\n+\t\t\tdie(_(\"corrupt patch at line %d\"), state->linenr);\n \t\tfragment->patch = line;\n \t\tfragment->size = len;\n \t\toldlines += fragment->oldlines;\n@@ -1845,7 +1841,8 @@ static char *inflate_it(const void *data, unsigned long size,\n  * points at an allocated memory that the caller must free, so\n  * it is marked as \"->free_patch = 1\".\n  */\n-static struct fragment *parse_binary_hunk(char **buf_p,\n+static struct fragment *parse_binary_hunk(struct apply_state *state,\n+\t\t\t\t\t  char **buf_p,\n \t\t\t\t\t  unsigned long *sz_p,\n \t\t\t\t\t  int *status_p,\n \t\t\t\t\t  int *used_p)\n@@ -1887,13 +1884,13 @@ static struct fragment *parse_binary_hunk(char **buf_p,\n \telse\n \t\treturn NULL;\n \n-\tstate_linenr++;\n+\tstate->linenr++;\n \tbuffer += llen;\n \twhile (1) {\n \t\tint byte_length, max_byte_length, newsize;\n \t\tllen = linelen(buffer, size);\n \t\tused += llen;\n-\t\tstate_linenr++;\n+\t\tstate->linenr++;\n \t\tif (llen == 1) {\n \t\t\t/* consume the blank line */\n \t\t\tbuffer++;\n@@ -1947,7 +1944,7 @@ static struct fragment *parse_binary_hunk(char **buf_p,\n \tfree(data);\n \t*status_p = -1;\n \terror(_(\"corrupt binary patch at line %d: %.*s\"),\n-\t      state_linenr-1, llen-1, buffer);\n+\t      state->linenr-1, llen-1, buffer);\n \treturn NULL;\n }\n \n@@ -1956,7 +1953,10 @@ static struct fragment *parse_binary_hunk(char **buf_p,\n  *   -1 in case of error,\n  *   the length of the parsed binary patch otherwise\n  */\n-static int parse_binary(char *buffer, unsigned long size, struct patch *patch)\n+static int parse_binary(struct apply_state *state,\n+\t\t\tchar *buffer,\n+\t\t\tunsigned long size,\n+\t\t\tstruct patch *patch)\n {\n \t/*\n \t * We have read \"GIT binary patch\\n\"; what follows is a line\n@@ -1977,15 +1977,15 @@ static int parse_binary(char *buffer, unsigned long size, struct patch *patch)\n \tint status;\n \tint used, used_1;\n \n-\tforward = parse_binary_hunk(&buffer, &size, &status, &used);\n+\tforward = parse_binary_hunk(state, &buffer, &size, &status, &used);\n \tif (!forward && !status)\n \t\t/* there has to be one hunk (forward hunk) */\n-\t\treturn error(_(\"unrecognized binary patch at line %d\"), state_linenr-1);\n+\t\treturn error(_(\"unrecognized binary patch at line %d\"), state->linenr-1);\n \tif (status)\n \t\t/* otherwise we already gave an error message */\n \t\treturn status;\n \n-\treverse = parse_binary_hunk(&buffer, &size, &status, &used_1);\n+\treverse = parse_binary_hunk(state, &buffer, &size, &status, &used_1);\n \tif (reverse)\n \t\tused += used_1;\n \telse if (status) {\n@@ -2100,8 +2100,8 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si\n \t\tif (llen == sizeof(git_binary) - 1 &&\n \t\t    !memcmp(git_binary, buffer + hd, llen)) {\n \t\t\tint used;\n-\t\t\tstate_linenr++;\n-\t\t\tused = parse_binary(buffer + hd + llen,\n+\t\t\tstate->linenr++;\n+\t\t\tused = parse_binary(state, buffer + hd + llen,\n \t\t\t\t\t    size - hd - llen, patch);\n \t\t\tif (used < 0)\n \t\t\t\treturn -1;\n@@ -2121,7 +2121,7 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si\n \t\t\t\tint len = strlen(binhdr[i]);\n \t\t\t\tif (len < size - hd &&\n \t\t\t\t    !memcmp(binhdr[i], buffer + hd, len)) {\n-\t\t\t\t\tstate_linenr++;\n+\t\t\t\t\tstate->linenr++;\n \t\t\t\t\tpatch->is_binary = 1;\n \t\t\t\t\tpatchsize = llen;\n \t\t\t\t\tbreak;\n@@ -2135,7 +2135,7 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si\n \t\t */\n \t\tif ((state->apply || state->check) &&\n \t\t    (!patch->is_binary && !metadata_changes(patch)))\n-\t\t\tdie(_(\"patch with only garbage at line %d\"), state_linenr);\n+\t\t\tdie(_(\"patch with only garbage at line %d\"), state->linenr);\n \t}\n \n \treturn offset + hdrsize + patchsize;\n@@ -4654,6 +4654,7 @@ static void init_apply_state(struct apply_state *state, const char *prefix)\n \tstate->squelch_whitespace_errors = 5;\n \tstate->ws_error_action = warn_on_ws_error;\n \tstate->ws_ignore_action = ignore_ws_none;\n+\tstate->linenr = 1;\n \tstrbuf_init(&state->root, 0);\n \n \tgit_apply_config();\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287364","messageId":"20160524081126.16973-45-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 44/49] builtin/apply: move 'fn_table' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:11:21Z","receivedAt":"2016-05-24T08:11:21Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify the apply functionality the 'fn_table' variable should\nnot be static and global to the file. Let's move it into\n'struct apply_state'.\n\nAs fn_table is cleared at the end of apply_patch(), it is not\nnecessary to clear it in clear_apply_state().\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 47 +++++++++++++++++++++++++----------------------\n 1 file changed, 25 insertions(+), 22 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex dd56a8e..47622be 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -84,6 +84,12 @@ struct apply_state {\n \tint max_change;\n \tint max_len;\n \n+\t/*\n+\t * Records filenames that have been touched, in order to handle\n+\t * the case where more than one patches touch the same file.\n+\t */\n+\tstruct string_list fn_table;\n+\n \t/* These control whitespace errors */\n \tenum ws_error_action ws_error_action;\n \tenum ws_ignore ws_ignore_action;\n@@ -271,13 +277,6 @@ struct image {\n \tstruct line *line;\n };\n \n-/*\n- * Records filenames that have been touched, in order to handle\n- * the case where more than one patches touch the same file.\n- */\n-\n-static struct string_list fn_table;\n-\n static uint32_t hash_line(const char *cp, size_t len)\n {\n \tsize_t i;\n@@ -3207,14 +3206,14 @@ static int read_file_or_gitlink(const struct cache_entry *ce, struct strbuf *buf\n \treturn read_blob_object(buf, ce->sha1, ce->ce_mode);\n }\n \n-static struct patch *in_fn_table(const char *name)\n+static struct patch *in_fn_table(struct apply_state *state, const char *name)\n {\n \tstruct string_list_item *item;\n \n \tif (name == NULL)\n \t\treturn NULL;\n \n-\titem = string_list_lookup(&fn_table, name);\n+\titem = string_list_lookup(&state->fn_table, name);\n \tif (item != NULL)\n \t\treturn (struct patch *)item->util;\n \n@@ -3246,7 +3245,7 @@ static int was_deleted(struct patch *patch)\n \treturn patch == PATH_WAS_DELETED;\n }\n \n-static void add_to_fn_table(struct patch *patch)\n+static void add_to_fn_table(struct apply_state *state, struct patch *patch)\n {\n \tstruct string_list_item *item;\n \n@@ -3256,7 +3255,7 @@ static void add_to_fn_table(struct patch *patch)\n \t * file creations and copies\n \t */\n \tif (patch->new_name != NULL) {\n-\t\titem = string_list_insert(&fn_table, patch->new_name);\n+\t\titem = string_list_insert(&state->fn_table, patch->new_name);\n \t\titem->util = patch;\n \t}\n \n@@ -3265,12 +3264,12 @@ static void add_to_fn_table(struct patch *patch)\n \t * later chunks shouldn't patch old names\n \t */\n \tif ((patch->new_name == NULL) || (patch->is_rename)) {\n-\t\titem = string_list_insert(&fn_table, patch->old_name);\n+\t\titem = string_list_insert(&state->fn_table, patch->old_name);\n \t\titem->util = PATH_WAS_DELETED;\n \t}\n }\n \n-static void prepare_fn_table(struct patch *patch)\n+static void prepare_fn_table(struct apply_state *state, struct patch *patch)\n {\n \t/*\n \t * store information about incoming file deletion\n@@ -3278,7 +3277,7 @@ static void prepare_fn_table(struct patch *patch)\n \twhile (patch) {\n \t\tif ((patch->new_name == NULL) || (patch->is_rename)) {\n \t\t\tstruct string_list_item *item;\n-\t\t\titem = string_list_insert(&fn_table, patch->old_name);\n+\t\t\titem = string_list_insert(&state->fn_table, patch->old_name);\n \t\t\titem->util = PATH_TO_BE_DELETED;\n \t\t}\n \t\tpatch = patch->next;\n@@ -3299,7 +3298,9 @@ static int checkout_target(struct index_state *istate,\n \treturn 0;\n }\n \n-static struct patch *previous_patch(struct patch *patch, int *gone)\n+static struct patch *previous_patch(struct apply_state *state,\n+\t\t\t\t    struct patch *patch,\n+\t\t\t\t    int *gone)\n {\n \tstruct patch *previous;\n \n@@ -3307,7 +3308,7 @@ static struct patch *previous_patch(struct patch *patch, int *gone)\n \tif (patch->is_copy || patch->is_rename)\n \t\treturn NULL; /* \"git\" patches do not depend on the order */\n \n-\tprevious = in_fn_table(patch->old_name);\n+\tprevious = in_fn_table(state, patch->old_name);\n \tif (!previous)\n \t\treturn NULL;\n \n@@ -3376,7 +3377,7 @@ static int load_preimage(struct apply_state *state,\n \tstruct patch *previous;\n \tint status;\n \n-\tprevious = previous_patch(patch, &status);\n+\tprevious = previous_patch(state, patch, &status);\n \tif (status)\n \t\treturn error(_(\"path %s has been renamed/deleted\"),\n \t\t\t     patch->old_name);\n@@ -3572,7 +3573,7 @@ static int apply_data(struct apply_state *state, struct patch *patch,\n \t}\n \tpatch->result = image.buf;\n \tpatch->resultsize = image.len;\n-\tadd_to_fn_table(patch);\n+\tadd_to_fn_table(state, patch);\n \tfree(image.line_allocated);\n \n \tif (0 < patch->is_delete && patch->resultsize)\n@@ -3606,7 +3607,7 @@ static int check_preimage(struct apply_state *state,\n \t\treturn 0;\n \n \tassert(patch->is_new <= 0);\n-\tprevious = previous_patch(patch, &status);\n+\tprevious = previous_patch(state, patch, &status);\n \n \tif (status)\n \t\treturn error(_(\"path %s has been renamed/deleted\"), old_name);\n@@ -3852,7 +3853,7 @@ static int check_patch(struct apply_state *state, struct patch *patch)\n \t * B and rename from A to B is handled the same way by asking\n \t * was_deleted().\n \t */\n-\tif ((tpatch = in_fn_table(new_name)) &&\n+\tif ((tpatch = in_fn_table(state, new_name)) &&\n \t    (was_deleted(tpatch) || to_be_deleted(tpatch)))\n \t\tok_if_exists = 1;\n \telse\n@@ -3930,7 +3931,7 @@ static int check_patch_list(struct apply_state *state, struct patch *patch)\n \tint err = 0;\n \n \tprepare_symlink_changes(patch);\n-\tprepare_fn_table(patch);\n+\tprepare_fn_table(state, patch);\n \twhile (patch) {\n \t\tif (state->apply_verbosely)\n \t\t\tsay_patch_name(stderr,\n@@ -4574,7 +4575,7 @@ static int apply_patch(struct apply_state *state,\n \n \tfree_patch_list(list);\n \tstrbuf_release(&buf);\n-\tstring_list_clear(&fn_table, 0);\n+\tstring_list_clear(&state->fn_table, 0);\n \treturn 0;\n }\n \n@@ -4668,6 +4669,8 @@ static void clear_apply_state(struct apply_state *state)\n {\n \tstring_list_clear(&state->limit_by_name, 0);\n \tstrbuf_release(&state->root);\n+\n+\t/* &state->fn_table is cleared at the end of apply_patch() */\n }\n \n int cmd_apply(int argc, const char **argv, const char *prefix)\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287362","messageId":"20160524081126.16973-46-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 45/49] builtin/apply: move 'symlink_changes' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:11:22Z","receivedAt":"2016-05-24T08:11:22Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify the apply functionality the 'symlink_changes' variable should\nnot be static and global to the file. Let's move it into\n'struct apply_state'.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 50 ++++++++++++++++++++++++++++----------------------\n 1 file changed, 28 insertions(+), 22 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 47622be..980bb34 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -34,6 +34,20 @@ enum ws_ignore {\n \tignore_ws_change\n };\n \n+/*\n+ * We need to keep track of how symlinks in the preimage are\n+ * manipulated by the patches.  A patch to add a/b/c where a/b\n+ * is a symlink should not be allowed to affect the directory\n+ * the symlink points at, but if the same patch removes a/b,\n+ * it is perfectly fine, as the patch removes a/b to make room\n+ * to create a directory a/b so that a/b/c can be created.\n+ *\n+ * See also \"struct string_list symlink_changes\" in \"struct\n+ * apply_state\".\n+ */\n+#define SYMLINK_GOES_AWAY 01\n+#define SYMLINK_IN_RESULT 02\n+\n struct apply_state {\n \tconst char *prefix;\n \tint prefix_length;\n@@ -75,6 +89,7 @@ struct apply_state {\n \n \t/* Various \"current state\" */\n \tint linenr; /* current line number */\n+\tstruct string_list symlink_changes; /* we have to track symlinks */\n \n \t/*\n \t * For \"diff-stat\" like behaviour, we keep track of the biggest change\n@@ -3702,52 +3717,42 @@ static int check_to_create(struct apply_state *state,\n \treturn 0;\n }\n \n-/*\n- * We need to keep track of how symlinks in the preimage are\n- * manipulated by the patches.  A patch to add a/b/c where a/b\n- * is a symlink should not be allowed to affect the directory\n- * the symlink points at, but if the same patch removes a/b,\n- * it is perfectly fine, as the patch removes a/b to make room\n- * to create a directory a/b so that a/b/c can be created.\n- */\n-static struct string_list symlink_changes;\n-#define SYMLINK_GOES_AWAY 01\n-#define SYMLINK_IN_RESULT 02\n-\n-static uintptr_t register_symlink_changes(const char *path, uintptr_t what)\n+static uintptr_t register_symlink_changes(struct apply_state *state,\n+\t\t\t\t\t  const char *path,\n+\t\t\t\t\t  uintptr_t what)\n {\n \tstruct string_list_item *ent;\n \n-\tent = string_list_lookup(&symlink_changes, path);\n+\tent = string_list_lookup(&state->symlink_changes, path);\n \tif (!ent) {\n-\t\tent = string_list_insert(&symlink_changes, path);\n+\t\tent = string_list_insert(&state->symlink_changes, path);\n \t\tent->util = (void *)0;\n \t}\n \tent->util = (void *)(what | ((uintptr_t)ent->util));\n \treturn (uintptr_t)ent->util;\n }\n \n-static uintptr_t check_symlink_changes(const char *path)\n+static uintptr_t check_symlink_changes(struct apply_state *state, const char *path)\n {\n \tstruct string_list_item *ent;\n \n-\tent = string_list_lookup(&symlink_changes, path);\n+\tent = string_list_lookup(&state->symlink_changes, path);\n \tif (!ent)\n \t\treturn 0;\n \treturn (uintptr_t)ent->util;\n }\n \n-static void prepare_symlink_changes(struct patch *patch)\n+static void prepare_symlink_changes(struct apply_state *state, struct patch *patch)\n {\n \tfor ( ; patch; patch = patch->next) {\n \t\tif ((patch->old_name && S_ISLNK(patch->old_mode)) &&\n \t\t    (patch->is_rename || patch->is_delete))\n \t\t\t/* the symlink at patch->old_name is removed */\n-\t\t\tregister_symlink_changes(patch->old_name, SYMLINK_GOES_AWAY);\n+\t\t\tregister_symlink_changes(state, patch->old_name, SYMLINK_GOES_AWAY);\n \n \t\tif (patch->new_name && S_ISLNK(patch->new_mode))\n \t\t\t/* the symlink at patch->new_name is created or remains */\n-\t\t\tregister_symlink_changes(patch->new_name, SYMLINK_IN_RESULT);\n+\t\t\tregister_symlink_changes(state, patch->new_name, SYMLINK_IN_RESULT);\n \t}\n }\n \n@@ -3761,7 +3766,7 @@ static int path_is_beyond_symlink_1(struct apply_state *state, struct strbuf *na\n \t\tif (!name->len)\n \t\t\tbreak;\n \t\tname->buf[name->len] = '\\0';\n-\t\tchange = check_symlink_changes(name->buf);\n+\t\tchange = check_symlink_changes(state, name->buf);\n \t\tif (change & SYMLINK_IN_RESULT)\n \t\t\treturn 1;\n \t\tif (change & SYMLINK_GOES_AWAY)\n@@ -3930,7 +3935,7 @@ static int check_patch_list(struct apply_state *state, struct patch *patch)\n {\n \tint err = 0;\n \n-\tprepare_symlink_changes(patch);\n+\tprepare_symlink_changes(state, patch);\n \tprepare_fn_table(state, patch);\n \twhile (patch) {\n \t\tif (state->apply_verbosely)\n@@ -4668,6 +4673,7 @@ static void init_apply_state(struct apply_state *state, const char *prefix)\n static void clear_apply_state(struct apply_state *state)\n {\n \tstring_list_clear(&state->limit_by_name, 0);\n+\tstring_list_clear(&state->symlink_changes, 0);\n \tstrbuf_release(&state->root);\n \n \t/* &state->fn_table is cleared at the end of apply_patch() */\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287371","messageId":"20160524081126.16973-47-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 46/49] builtin/apply: move 'state' check into check_apply_state()","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:11:23Z","receivedAt":"2016-05-24T08:11:23Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify the apply functionality we should provide a function\nto check that the values in a 'struct apply_state' instance are\ncoherent. Let's move the code to do that into a new\ncheck_apply_state() function.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 52 +++++++++++++++++++++++++++++-----------------------\n 1 file changed, 29 insertions(+), 23 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 980bb34..8095026 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -4679,11 +4679,38 @@ static void clear_apply_state(struct apply_state *state)\n \t/* &state->fn_table is cleared at the end of apply_patch() */\n }\n \n+static void check_apply_state(struct apply_state *state, int force_apply)\n+{\n+\tint is_not_gitdir = !startup_info->have_repository;\n+\n+\tif (state->apply_with_reject && state->threeway)\n+\t\tdie(\"--reject and --3way cannot be used together.\");\n+\tif (state->cached && state->threeway)\n+\t\tdie(\"--cached and --3way cannot be used together.\");\n+\tif (state->threeway) {\n+\t\tif (is_not_gitdir)\n+\t\t\tdie(_(\"--3way outside a repository\"));\n+\t\tstate->check_index = 1;\n+\t}\n+\tif (state->apply_with_reject)\n+\t\tstate->apply = state->apply_verbosely = 1;\n+\tif (!force_apply && (state->diffstat || state->numstat || state->summary || state->check || state->fake_ancestor))\n+\t\tstate->apply = 0;\n+\tif (state->check_index && is_not_gitdir)\n+\t\tdie(_(\"--index outside a repository\"));\n+\tif (state->cached) {\n+\t\tif (is_not_gitdir)\n+\t\t\tdie(_(\"--cached outside a repository\"));\n+\t\tstate->check_index = 1;\n+\t}\n+\tif (state->check_index)\n+\t\tstate->unsafe_paths = 0;\n+}\n+\n int cmd_apply(int argc, const char **argv, const char *prefix)\n {\n \tint i;\n \tint errs = 0;\n-\tint is_not_gitdir = !startup_info->have_repository;\n \tint force_apply = 0;\n \tint options = 0;\n \tint read_stdin = 1;\n@@ -4763,28 +4790,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \targc = parse_options(argc, argv, state.prefix, builtin_apply_options,\n \t\t\tapply_usage, 0);\n \n-\tif (state.apply_with_reject && state.threeway)\n-\t\tdie(\"--reject and --3way cannot be used together.\");\n-\tif (state.cached && state.threeway)\n-\t\tdie(\"--cached and --3way cannot be used together.\");\n-\tif (state.threeway) {\n-\t\tif (is_not_gitdir)\n-\t\t\tdie(_(\"--3way outside a repository\"));\n-\t\tstate.check_index = 1;\n-\t}\n-\tif (state.apply_with_reject)\n-\t\tstate.apply = state.apply_verbosely = 1;\n-\tif (!force_apply && (state.diffstat || state.numstat || state.summary || state.check || state.fake_ancestor))\n-\t\tstate.apply = 0;\n-\tif (state.check_index && is_not_gitdir)\n-\t\tdie(_(\"--index outside a repository\"));\n-\tif (state.cached) {\n-\t\tif (is_not_gitdir)\n-\t\t\tdie(_(\"--cached outside a repository\"));\n-\t\tstate.check_index = 1;\n-\t}\n-\tif (state.check_index)\n-\t\tstate.unsafe_paths = 0;\n+\tcheck_apply_state(&state, force_apply);\n \n \tfor (i = 0; i < argc; i++) {\n \t\tconst char *arg = argv[i];\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287363","messageId":"20160524081126.16973-48-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 47/49] builtin/apply: move applying patches into apply_all_patches()","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:11:24Z","receivedAt":"2016-05-24T08:11:24Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify the apply functionality we should provide a function to\napply many patches. Let's move the code to do that into a new\napply_all_patches() function.\n\nReviewed-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 129 +++++++++++++++++++++++++++++++-------------------------\n 1 file changed, 71 insertions(+), 58 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 8095026..5027f1b 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -4707,13 +4707,80 @@ static void check_apply_state(struct apply_state *state, int force_apply)\n \t\tstate->unsafe_paths = 0;\n }\n \n-int cmd_apply(int argc, const char **argv, const char *prefix)\n+static int apply_all_patches(struct apply_state *state,\n+\t\t\t     int argc,\n+\t\t\t     const char **argv,\n+\t\t\t     int options)\n {\n \tint i;\n \tint errs = 0;\n+\tint read_stdin = 1;\n+\n+\tfor (i = 0; i < argc; i++) {\n+\t\tconst char *arg = argv[i];\n+\t\tint fd;\n+\n+\t\tif (!strcmp(arg, \"-\")) {\n+\t\t\terrs |= apply_patch(state, 0, \"<stdin>\", options);\n+\t\t\tread_stdin = 0;\n+\t\t\tcontinue;\n+\t\t} else if (0 < state->prefix_length)\n+\t\t\targ = prefix_filename(state->prefix,\n+\t\t\t\t\t      state->prefix_length,\n+\t\t\t\t\t      arg);\n+\n+\t\tfd = open(arg, O_RDONLY);\n+\t\tif (fd < 0)\n+\t\t\tdie_errno(_(\"can't open patch '%s'\"), arg);\n+\t\tread_stdin = 0;\n+\t\tset_default_whitespace_mode(state);\n+\t\terrs |= apply_patch(state, fd, arg, options);\n+\t\tclose(fd);\n+\t}\n+\tset_default_whitespace_mode(state);\n+\tif (read_stdin)\n+\t\terrs |= apply_patch(state, 0, \"<stdin>\", options);\n+\n+\tif (state->whitespace_error) {\n+\t\tif (state->squelch_whitespace_errors &&\n+\t\t    state->squelch_whitespace_errors < state->whitespace_error) {\n+\t\t\tint squelched =\n+\t\t\t\tstate->whitespace_error - state->squelch_whitespace_errors;\n+\t\t\twarning(Q_(\"squelched %d whitespace error\",\n+\t\t\t\t   \"squelched %d whitespace errors\",\n+\t\t\t\t   squelched),\n+\t\t\t\tsquelched);\n+\t\t}\n+\t\tif (state->ws_error_action == die_on_ws_error)\n+\t\t\tdie(Q_(\"%d line adds whitespace errors.\",\n+\t\t\t       \"%d lines add whitespace errors.\",\n+\t\t\t       state->whitespace_error),\n+\t\t\t    state->whitespace_error);\n+\t\tif (state->applied_after_fixing_ws && state->apply)\n+\t\t\twarning(\"%d line%s applied after\"\n+\t\t\t\t\" fixing whitespace errors.\",\n+\t\t\t\tstate->applied_after_fixing_ws,\n+\t\t\t\tstate->applied_after_fixing_ws == 1 ? \"\" : \"s\");\n+\t\telse if (state->whitespace_error)\n+\t\t\twarning(Q_(\"%d line adds whitespace errors.\",\n+\t\t\t\t   \"%d lines add whitespace errors.\",\n+\t\t\t\t   state->whitespace_error),\n+\t\t\t\tstate->whitespace_error);\n+\t}\n+\n+\tif (state->update_index) {\n+\t\tif (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))\n+\t\t\tdie(_(\"Unable to write new index file\"));\n+\t}\n+\n+\treturn !!errs;\n+}\n+\n+int cmd_apply(int argc, const char **argv, const char *prefix)\n+{\n \tint force_apply = 0;\n \tint options = 0;\n-\tint read_stdin = 1;\n+\tint ret;\n \tstruct apply_state state;\n \n \tstruct option builtin_apply_options[] = {\n@@ -4792,63 +4859,9 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \n \tcheck_apply_state(&state, force_apply);\n \n-\tfor (i = 0; i < argc; i++) {\n-\t\tconst char *arg = argv[i];\n-\t\tint fd;\n-\n-\t\tif (!strcmp(arg, \"-\")) {\n-\t\t\terrs |= apply_patch(&state, 0, \"<stdin>\", options);\n-\t\t\tread_stdin = 0;\n-\t\t\tcontinue;\n-\t\t} else if (0 < state.prefix_length)\n-\t\t\targ = prefix_filename(state.prefix,\n-\t\t\t\t\t      state.prefix_length,\n-\t\t\t\t\t      arg);\n-\n-\t\tfd = open(arg, O_RDONLY);\n-\t\tif (fd < 0)\n-\t\t\tdie_errno(_(\"can't open patch '%s'\"), arg);\n-\t\tread_stdin = 0;\n-\t\tset_default_whitespace_mode(&state);\n-\t\terrs |= apply_patch(&state, fd, arg, options);\n-\t\tclose(fd);\n-\t}\n-\tset_default_whitespace_mode(&state);\n-\tif (read_stdin)\n-\t\terrs |= apply_patch(&state, 0, \"<stdin>\", options);\n-\tif (state.whitespace_error) {\n-\t\tif (state.squelch_whitespace_errors &&\n-\t\t    state.squelch_whitespace_errors < state.whitespace_error) {\n-\t\t\tint squelched =\n-\t\t\t\tstate.whitespace_error - state.squelch_whitespace_errors;\n-\t\t\twarning(Q_(\"squelched %d whitespace error\",\n-\t\t\t\t   \"squelched %d whitespace errors\",\n-\t\t\t\t   squelched),\n-\t\t\t\tsquelched);\n-\t\t}\n-\t\tif (state.ws_error_action == die_on_ws_error)\n-\t\t\tdie(Q_(\"%d line adds whitespace errors.\",\n-\t\t\t       \"%d lines add whitespace errors.\",\n-\t\t\t       state.whitespace_error),\n-\t\t\t    state.whitespace_error);\n-\t\tif (state.applied_after_fixing_ws && state.apply)\n-\t\t\twarning(\"%d line%s applied after\"\n-\t\t\t\t\" fixing whitespace errors.\",\n-\t\t\t\tstate.applied_after_fixing_ws,\n-\t\t\t\tstate.applied_after_fixing_ws == 1 ? \"\" : \"s\");\n-\t\telse if (state.whitespace_error)\n-\t\t\twarning(Q_(\"%d line adds whitespace errors.\",\n-\t\t\t\t   \"%d lines add whitespace errors.\",\n-\t\t\t\t   state.whitespace_error),\n-\t\t\t\tstate.whitespace_error);\n-\t}\n-\n-\tif (state.update_index) {\n-\t\tif (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))\n-\t\t\tdie(_(\"Unable to write new index file\"));\n-\t}\n+\tret = apply_all_patches(&state, argc, argv, options);\n \n \tclear_apply_state(&state);\n \n-\treturn !!errs;\n+\treturn ret;\n }\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287368","messageId":"20160524081126.16973-49-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 48/49] builtin/apply: move 'lock_file' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:11:25Z","receivedAt":"2016-05-24T08:11:25Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"We cannot have a 'struct lock_file' allocated on the stack, as lockfile.c\nkeeps a linked list of all created lock_file structures. So let's make the\n'lock_file' variable a pointer to a 'struct lock_file'\n\nAs the same instance of this struct can be reused, let's add an argument\nto init_apply_state(), so that the caller can supply the same instance to\ndifferent calls. And let's alloc an instance in init_apply_state(), if the\ncaller doesn't want to supply one.\n\nHelped-by: Eric Sunshine <sunshine@sunshineco.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 19 +++++++++++++------\n 1 file changed, 13 insertions(+), 6 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 5027f1b..5d46b7c 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -52,6 +52,12 @@ struct apply_state {\n \tconst char *prefix;\n \tint prefix_length;\n \n+\t/*\n+\t * Since lockfile.c keeps a linked list of all created\n+\t * lock_file structures, it isn't safe to free(lock_file).\n+\t */\n+\tstruct lock_file *lock_file;\n+\n \t/* These control what gets looked at and modified */\n \tint apply; /* this is not a dry-run */\n \tint cached; /* apply to the index only */\n@@ -4493,8 +4499,6 @@ static int write_out_results(struct apply_state *state, struct patch *list)\n \treturn errs;\n }\n \n-static struct lock_file lock_file;\n-\n #define INACCURATE_EOF\t(1<<0)\n #define RECOUNT\t\t(1<<1)\n \n@@ -4547,7 +4551,7 @@ static int apply_patch(struct apply_state *state,\n \n \tstate->update_index = state->check_index && state->apply;\n \tif (state->update_index && newfd < 0)\n-\t\tnewfd = hold_locked_index(&lock_file, 1);\n+\t\tnewfd = hold_locked_index(state->lock_file, 1);\n \n \tif (state->check_index) {\n \t\tif (read_cache() < 0)\n@@ -4648,11 +4652,14 @@ static int option_parse_directory(const struct option *opt,\n \treturn 0;\n }\n \n-static void init_apply_state(struct apply_state *state, const char *prefix)\n+static void init_apply_state(struct apply_state *state,\n+\t\t\t     const char *prefix,\n+\t\t\t     struct lock_file *lock_file)\n {\n \tmemset(state, 0, sizeof(*state));\n \tstate->prefix = prefix;\n \tstate->prefix_length = state->prefix ? strlen(state->prefix) : 0;\n+\tstate->lock_file = lock_file ? lock_file : xcalloc(1, sizeof(*lock_file));\n \tstate->apply = 1;\n \tstate->line_termination = '\\n';\n \tstate->p_value = 1;\n@@ -4769,7 +4776,7 @@ static int apply_all_patches(struct apply_state *state,\n \t}\n \n \tif (state->update_index) {\n-\t\tif (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))\n+\t\tif (write_locked_index(&the_index, state->lock_file, COMMIT_LOCK))\n \t\t\tdie(_(\"Unable to write new index file\"));\n \t}\n \n@@ -4852,7 +4859,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t\tOPT_END()\n \t};\n \n-\tinit_apply_state(&state, prefix);\n+\tinit_apply_state(&state, prefix, NULL);\n \n \targc = parse_options(argc, argv, state.prefix, builtin_apply_options,\n \t\t\tapply_usage, 0);\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287369","messageId":"20160524081126.16973-50-chriscool@tuxfamily.org","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"[PATCH v3 49/49] builtin/apply: move 'newfd' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:11:26Z","receivedAt":"2016-05-24T08:11:26Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify the apply functionality the 'newfd' variable should\nnot be static and global to the file. Let's move it into\n'struct apply_state'.\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 9 +++++----\n 1 file changed, 5 insertions(+), 4 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 5d46b7c..c1c5592 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -57,6 +57,7 @@ struct apply_state {\n \t * lock_file structures, it isn't safe to free(lock_file).\n \t */\n \tstruct lock_file *lock_file;\n+\tint newfd;\n \n \t/* These control what gets looked at and modified */\n \tint apply; /* this is not a dry-run */\n@@ -120,8 +121,6 @@ struct apply_state {\n \tint applied_after_fixing_ws;\n };\n \n-static int newfd = -1;\n-\n static const char * const apply_usage[] = {\n \tN_(\"git apply [<options>] [<patch>...]\"),\n \tNULL\n@@ -4550,8 +4549,8 @@ static int apply_patch(struct apply_state *state,\n \t\tstate->apply = 0;\n \n \tstate->update_index = state->check_index && state->apply;\n-\tif (state->update_index && newfd < 0)\n-\t\tnewfd = hold_locked_index(state->lock_file, 1);\n+\tif (state->update_index && state->newfd < 0)\n+\t\tstate->newfd = hold_locked_index(state->lock_file, 1);\n \n \tif (state->check_index) {\n \t\tif (read_cache() < 0)\n@@ -4660,6 +4659,7 @@ static void init_apply_state(struct apply_state *state,\n \tstate->prefix = prefix;\n \tstate->prefix_length = state->prefix ? strlen(state->prefix) : 0;\n \tstate->lock_file = lock_file ? lock_file : xcalloc(1, sizeof(*lock_file));\n+\tstate->newfd = -1;\n \tstate->apply = 1;\n \tstate->line_termination = '\\n';\n \tstate->p_value = 1;\n@@ -4778,6 +4778,7 @@ static int apply_all_patches(struct apply_state *state,\n \tif (state->update_index) {\n \t\tif (write_locked_index(&the_index, state->lock_file, COMMIT_LOCK))\n \t\t\tdie(_(\"Unable to write new index file\"));\n+\t\tstate->newfd = -1;\n \t}\n \n \treturn !!errs;\n-- \n2.8.3.443.gaeee61e\n"},{"id":"287401","messageId":"CAP8UFD0oU4BM=k=+9ijz86ZH8o63i3EVxqL-XMZaZKdyD7_x-w@mail.gmail.com","threadId":"42433","inReplyTo":"20160524081126.16973-1-chriscool@tuxfamily.org","subject":"Re: [PATCH v3 00/49] libify apply and use lib in am, part 1","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-24T08:59:58Z","receivedAt":"2016-05-24T08:59:58Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Tue, May 24, 2016 at 10:10 AM, Christian Couder\n<christian.couder@gmail.com> wrote:\n>\n> I will send a diff between this version and the 50 first patches of v2\n> soon as a reply to this email.\n\nHere is the diff:\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex ec55768..c1c5592 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -59,44 +59,44 @@ struct apply_state {\n     struct lock_file *lock_file;\n     int newfd;\n\n-    int apply;\n+    /* These control what gets looked at and modified */\n+    int apply; /* this is not a dry-run */\n+    int cached; /* apply to the index only */\n+    int check; /* preimage must match working tree, don't actually apply */\n+    int check_index; /* preimage must match the indexed version */\n+    int update_index; /* check_index && apply */\n+\n+    /* These control cosmetic aspect of the output */\n+    int diffstat; /* just show a diffstat, and don't actually apply */\n+    int numstat; /* just show a numeric diffstat, and don't actually apply */\n+    int summary; /* just report creation, deletion, etc, and don't\nactually apply */\n+\n+    /* These boolean parameters control how the apply is done */\n     int allow_overlap;\n     int apply_in_reverse;\n     int apply_with_reject;\n     int apply_verbosely;\n-\n-    /* --cached updates only the cache without ever touching the\nworking tree. */\n-    int cached;\n-\n-    /* --stat does just a diffstat, and doesn't actually apply */\n-    int diffstat;\n-\n-    /* --numstat does numeric diffstat, and doesn't actually apply */\n-    int numstat;\n-\n-    int summary;\n-    int threeway;\n     int no_add;\n+    int threeway;\n+    int unidiff_zero;\n+    int unsafe_paths;\n+\n+    /* Other non boolean parameters */\n     const char *fake_ancestor;\n     const char *patch_input_file;\n-    struct string_list limit_by_name;\n-    int has_include;\n+    int line_termination;\n     struct strbuf root;\n-    struct string_list symlink_changes;\n-\n-    /*\n-     *  --check turns on checking that the working tree matches the\n-     *    files that are being modified, but doesn't apply the patch\n-     */\n-    int check;\n+    int p_value;\n+    int p_value_known;\n+    unsigned int p_context;\n\n-    /* --index updates the cache as well. */\n-    int check_index;\n+    /* Exclude and include path parameters */\n+    struct string_list limit_by_name;\n+    int has_include;\n\n-    int unidiff_zero;\n-    int update_index;\n-    int unsafe_paths;\n-    int line_termination;\n+    /* Various \"current state\" */\n+    int linenr; /* current line number */\n+    struct string_list symlink_changes; /* we have to track symlinks */\n\n     /*\n      * For \"diff-stat\" like behaviour, we keep track of the biggest change\n@@ -106,30 +106,19 @@ struct apply_state {\n     int max_change;\n     int max_len;\n\n-    /*\n-     * Various \"current state\", notably line numbers and what\n-     * file (and how) we're patching right now.. The \"is_xxxx\"\n-     * things are flags, where -1 means \"don't know yet\".\n-     */\n-    int linenr;\n-\n     /*\n      * Records filenames that have been touched, in order to handle\n      * the case where more than one patches touch the same file.\n      */\n     struct string_list fn_table;\n-\n-    int p_value;\n-    int p_value_known;\n-    unsigned int p_context;\n\n+    /* These control whitespace errors */\n+    enum ws_error_action ws_error_action;\n+    enum ws_ignore ws_ignore_action;\n     const char *whitespace_option;\n     int whitespace_error;\n     int squelch_whitespace_errors;\n     int applied_after_fixing_ws;\n-\n-    enum ws_error_action ws_error_action;\n-    enum ws_ignore ws_ignore_action;\n };\n\n static const char * const apply_usage[] = {\n@@ -4688,6 +4677,15 @@ static void init_apply_state(struct apply_state *state,\n         parse_ignorewhitespace_option(state, apply_default_ignorewhitespace);\n }\n\n+static void clear_apply_state(struct apply_state *state)\n+{\n+    string_list_clear(&state->limit_by_name, 0);\n+    string_list_clear(&state->symlink_changes, 0);\n+    strbuf_release(&state->root);\n+\n+    /* &state->fn_table is cleared at the end of apply_patch() */\n+}\n+\n static void check_apply_state(struct apply_state *state, int force_apply)\n {\n     int is_not_gitdir = !startup_info->have_repository;\n@@ -4790,6 +4788,7 @@ int cmd_apply(int argc, const char **argv, const\nchar *prefix)\n {\n     int force_apply = 0;\n     int options = 0;\n+    int ret;\n     struct apply_state state;\n\n     struct option builtin_apply_options[] = {\n@@ -4868,5 +4867,9 @@ int cmd_apply(int argc, const char **argv, const\nchar *prefix)\n\n     check_apply_state(&state, force_apply);\n\n-    return apply_all_patches(&state, argc, argv, options);\n+    ret = apply_all_patches(&state, argc, argv, options);\n+\n+    clear_apply_state(&state);\n+\n+    return ret;\n }\n"},{"id":"288057","messageId":"xmqq8tyozuul.fsf@gitster.mtv.corp.google.com","threadId":"42433","inReplyTo":"20160524081126.16973-49-chriscool@tuxfamily.org","subject":"Re: [PATCH v3 48/49] builtin/apply: move 'lock_file' global into 'struct apply_state'","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-06-01T17:23:30Z","receivedAt":"2016-06-01T17:23:30Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Christian Couder <christian.couder@gmail.com> writes:\n\n> We cannot have a 'struct lock_file' allocated on the stack, as lockfile.c\n> keeps a linked list of all created lock_file structures. So let's make the\n> 'lock_file' variable a pointer to a 'struct lock_file'\n\nGood.\n\n> As the same instance of this struct can be reused, let's add an argument\n> to init_apply_state(), so that the caller can supply the same instance to\n> different calls.\n\nGood.\n\n> And let's alloc an instance in init_apply_state(), if the\n> caller doesn't want to supply one.\n\nThis is questionable.\n\n> -static struct lock_file lock_file;\n\nI'd rather leave this as-is, and pass the pointer to it to\ninit_apply_state().  For the purpose of \"cmd_apply()\" which is the\nfirst user of the \"(semi-)libified apply API\", that reads a single\npatch and applies it before exiting, that is sufficient.\n\nAs to the \"if the caller does not want to supply one, we will\nallocate one that will definitely be leaked\", I am mildly opposed.\n\nBy making it the responsibility of the caller, whenever a new caller\nof the libified apply API is written, those who write it is _forced_\nto think about not leaking the lockfile structure, which is a good\nthing.\n\nOther than that, all 49 patches look sensible to me.\n\nThanks for working on this.\n"},{"id":"288215","messageId":"CAP8UFD37RJ_O-5Ob7KhF_d288=Ab0o4hVJDrtBoNPJ1VPmLosg@mail.gmail.com","threadId":"42433","inReplyTo":"xmqq8tyozuul.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH v3 48/49] builtin/apply: move 'lock_file' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-06-03T09:42:13Z","receivedAt":"2016-06-03T09:42:13Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Wed, Jun 1, 2016 at 7:23 PM, Junio C Hamano <gitster@pobox.com> wrote:\n> Christian Couder <christian.couder@gmail.com> writes:\n>\n>> We cannot have a 'struct lock_file' allocated on the stack, as lockfile.c\n>> keeps a linked list of all created lock_file structures. So let's make the\n>> 'lock_file' variable a pointer to a 'struct lock_file'\n>\n> Good.\n>\n>> As the same instance of this struct can be reused, let's add an argument\n>> to init_apply_state(), so that the caller can supply the same instance to\n>> different calls.\n>\n> Good.\n>\n>> And let's alloc an instance in init_apply_state(), if the\n>> caller doesn't want to supply one.\n>\n> This is questionable.\n>\n>> -static struct lock_file lock_file;\n>\n> I'd rather leave this as-is, and pass the pointer to it to\n> init_apply_state().  For the purpose of \"cmd_apply()\" which is the\n> first user of the \"(semi-)libified apply API\", that reads a single\n> patch and applies it before exiting, that is sufficient.\n\nOk, I will leave this as-is.\n\n> As to the \"if the caller does not want to supply one, we will\n> allocate one that will definitely be leaked\", I am mildly opposed.\n>\n> By making it the responsibility of the caller, whenever a new caller\n> of the libified apply API is written, those who write it is _forced_\n> to think about not leaking the lockfile structure, which is a good\n> thing.\n\nOk, I will remove the allocation in case the caller pass NULL.\n\n> Other than that, all 49 patches look sensible to me.\n\nOk, I think I will resend only this patch (48/49) with the changes you\nsuggest, and maybe the next one 49/49 to avoid spamming the list.\n\n> Thanks for working on this.\n\nThanks,\nChristian.\n"}]}