{"thread":{"id":"42127","subject":"[PATCH 01/83] builtin/apply: make gitdiff_verify_name() return void","startedAt":"2016-04-24T13:33:00Z","lastAt":"2016-05-08T20:11:41Z","messageCount":156,"participants":["Christian Couder","Ramsay Jones","Duy Nguyen","Eric Sunshine","Johannes Schindelin","Junio C Hamano","Stefan Beller"],"isPatch":true,"patchVersion":1,"patchTotal":83},"messages":[{"id":"284247","messageId":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":null,"subject":"[PATCH 00/83] libify apply and use lib in am","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:00Z","receivedAt":"2016-04-24T13:33:00Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"This 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\nThis has initially been discussed in the following thread:\n\nhttp://thread.gmane.org/gmane.comp.version-control.git/287236/\n\nA RFC patch series was sent previously that only got rid of the global\nvariables and refactored the code around a bit:\n\nhttp://thread.gmane.org/gmane.comp.version-control.git/288489/\n\nThis new patch series is built on top of that previous work, so\npatches 1/83 to 47/83 are from the previous RFC patch series and\npatches after that are new.\n\nSorry if this patch series is a bit long. I can split it into two or\nmore series if it is prefered.\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\nPerformance numbers:\n\n  - A few days ago Ævar did a huge many-hundred commit rebase on the\n    kernel with 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  - Some days ago I tested rebasing 13 commits in Booking.com's\n    monorepo on a Red Hat 6.5 server with split-index and\n    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\nNo tests on Windows have been performed, but it could be interesting\nto test on this platform.\n\nChristian Couder (83):\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: introduce 'struct apply_state' to start libifying\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 'read_stdin' global into cmd_apply()\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 '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' init into init_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: rename 'prefix_' parameter to 'prefix'\n  builtin/apply: move 'lock_file' global into 'struct apply_state'\n  builtin/apply: get rid of the 'newfd' global\n  builtin/apply: make apply_patch() return -1 instead of die()ing\n  builtin/apply: read_patch_file() return -1 instead of die()ing\n  builtin/apply: make find_header() return -1 instead of die()ing\n  builtin/apply: make parse_chunk() return a negative integer on error\n  builtin/apply: make parse_single_patch() return -1 on error\n  apply: move 'struct apply_state' to apply.h\n  builtin/apply: libify parse_whitespace_option()\n  builtin/apply: libify parse_ignorewhitespace_option()\n  builtin/apply: move init_apply_state() to apply.c\n  apply: libify init_apply_state()\n  builtin/apply: libify check_apply_state()\n  builtin/apply: move check_apply_state() to apply.c\n  builtin/apply: make apply_all_patches() return -1 on error\n  builtin/apply: make parse_traditional_patch() return -1 on error\n  builtin/apply: make gitdiff_verify_name() return -1 on error\n  builtin/apply: change die_on_unsafe_path() to check_unsafe_path()\n  builtin/apply: make build_fake_ancestor() return -1 on error\n  builtin/apply: make remove_file() return -1 on error\n  builtin/apply: make add_conflicted_stages_file() return -1 on error\n  builtin/apply: make add_index_file() return -1 on error\n  builtin/apply: make create_file() return -1 on error\n  builtin/apply: make write_out_one_result() return -1 on error\n  builtin/apply: make write_out_results() return -1 on error\n  builtin/apply: make try_create_file() return -1 on error\n  builtin/apply: make create_one_file() return -1 on error\n  builtin/apply: rename option parsing functions\n  apply: rename and move opt constants to apply.h\n  Move libified code from builtin/apply.c to apply.{c,h}\n  apply: make some parsing functions static again\n  run-command: make dup_devnull() non static\n  apply: roll back index in case of error\n  environment: add set_index_file()\n  builtin/am: use apply api in run_apply()\n\n Makefile               |    1 +\n apply.c                | 4796 ++++++++++++++++++++++++++++++++++++++++++++++++\n apply.h                |  149 ++\n builtin/am.c           |  103 +-\n builtin/apply.c        | 4665 +---------------------------------------------\n cache.h                |    1 +\n environment.c          |    5 +\n run-command.c          |    2 +-\n run-command.h          |    6 +\n t/t4012-diff-binary.sh |    4 +-\n t/t4254-am-corrupt.sh  |    2 +-\n 11 files changed, 5096 insertions(+), 4638 deletions(-)\n create mode 100644 apply.c\n create mode 100644 apply.h\n\n-- \n2.8.1.300.g5fed0c0\n"},{"id":"284246","messageId":"1461504863-15946-2-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 01/83] builtin/apply: make gitdiff_verify_name() return void","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:01Z","receivedAt":"2016-04-24T13:33:01Z","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\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.1.300.g5fed0c0\n"},{"id":"284248","messageId":"1461504863-15946-3-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 02/83] builtin/apply: avoid parameter shadowing 'p_value' global","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:02Z","receivedAt":"2016-04-24T13:33:02Z","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\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.1.300.g5fed0c0\n"},{"id":"284249","messageId":"1461504863-15946-4-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 03/83] builtin/apply: avoid parameter shadowing 'linenr' global","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:03Z","receivedAt":"2016-04-24T13:33:03Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 4 ++--\n 1 file changed, 2 insertions(+), 2 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex e133b38..7115dc2 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -1516,7 +1516,7 @@ static int find_header(const char *line, unsigned long size, int *hdrsize, struc\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(unsigned result, const char *line, int len, int l_nr)\n {\n \tchar *err;\n \n@@ -1530,7 +1530,7 @@ 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\tpatch_input_file, l_nr, err, len, line);\n \tfree(err);\n }\n \n-- \n2.8.1.300.g5fed0c0\n"},{"id":"284321","messageId":"1461504863-15946-5-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 04/83] builtin/apply: avoid local variable shadowing 'len' parameter","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:04Z","receivedAt":"2016-04-24T13:33:04Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-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 7115dc2..78849e4 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.1.300.g5fed0c0\n"},{"id":"284320","messageId":"1461504863-15946-6-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 05/83] builtin/apply: extract line_by_line_fuzzy_match() from match_fragment()","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:05Z","receivedAt":"2016-04-24T13:33:05Z","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\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 129 ++++++++++++++++++++++++++++++++------------------------\n 1 file changed, 73 insertions(+), 56 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 78849e4..02239d9 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@@ -2251,7 +2319,7 @@ static int match_fragment(struct image *img,\n \t\t\t  int match_beginning, int match_end)\n {\n \tint i;\n-\tchar *fixed_buf, *buf, *orig, *target;\n+\tchar *fixed_buf, *orig, *target;\n \tstruct strbuf fixed;\n \tsize_t fixed_len, postlen;\n \tint preimage_limit;\n@@ -2312,6 +2380,7 @@ static int match_fragment(struct image *img,\n \t\t * There must be one non-blank context line that match\n \t\t * a line before the end of img.\n \t\t */\n+\t\tchar *buf;\n \t\tchar *buf_end;\n \n \t\tbuf = preimage->buf;\n@@ -2331,61 +2400,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.1.300.g5fed0c0\n"},{"id":"284250","messageId":"1461504863-15946-7-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 06/83] builtin/apply: move 'options' variable into cmd_apply()","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:06Z","receivedAt":"2016-04-24T13:33:06Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-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 02239d9..8fd8dbc 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@@ -4518,6 +4517,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.1.300.g5fed0c0\n"},{"id":"284323","messageId":"1461504863-15946-8-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 07/83] builtin/apply: introduce 'struct apply_state' to start libifying","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:07Z","receivedAt":"2016-04-24T13:33:07Z","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\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 8fd8dbc..51e6af4 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@@ -749,7 +752,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@@ -762,17 +765,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@@ -859,7 +862,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@@ -867,8 +873,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@@ -1430,7 +1436,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@@ -1507,7 +1517,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\tlinenr += 2;\n \t\treturn offset;\n@@ -1914,21 +1924,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@@ -1945,16 +1955,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@@ -1981,17 +1991,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@@ -4369,7 +4379,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@@ -4386,14 +4399,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@@ -4518,6 +4531,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+\tstruct apply_state state;\n \n \tconst char *whitespace_option = NULL;\n \n@@ -4590,15 +4604,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@@ -4629,23 +4645,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.1.300.g5fed0c0\n"},{"id":"284251","messageId":"1461504863-15946-9-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 08/83] builtin/apply: move 'unidiff_zero' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:08Z","receivedAt":"2016-04-24T13:33:08Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 42 ++++++++++++++++++++++++------------------\n 1 file changed, 24 insertions(+), 18 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 51e6af4..ad81210 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -24,6 +24,8 @@\n struct apply_state {\n \tconst char *prefix;\n \tint prefix_length;\n+\n+\tint unidiff_zero;\n };\n \n /*\n@@ -37,7 +39,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@@ -2696,7 +2697,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@@ -2838,7 +2840,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@@ -2846,7 +2848,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@@ -3069,7 +3071,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@@ -3082,7 +3084,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@@ -3390,8 +3392,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@@ -3417,7 +3422,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@@ -3461,7 +3466,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@@ -3469,9 +3475,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@@ -3719,7 +3725,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@@ -3818,13 +3824,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@@ -3834,7 +3840,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@@ -4436,7 +4442,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@@ -4585,7 +4591,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.1.300.g5fed0c0\n"},{"id":"284322","messageId":"1461504863-15946-10-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 09/83] builtin/apply: move 'check' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:09Z","receivedAt":"2016-04-24T13:33:09Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 16 +++++++++-------\n 1 file changed, 9 insertions(+), 7 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex ad81210..6c628f6 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -25,12 +25,15 @@ struct apply_state {\n \tconst char *prefix;\n \tint prefix_length;\n \n+\t/*\n+\t *  --check turns on checking that the working tree matches the\n+\t *    files that are being modified, but doesn't apply the patch\n+\t */\n+\tint check;\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@@ -47,7 +50,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 +2055,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\"), linenr);\n \t}\n@@ -4441,7 +4443,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@@ -4561,7 +4563,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@@ -4634,7 +4636,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.1.300.g5fed0c0\n"},{"id":"284252","messageId":"1461504863-15946-11-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 10/83] builtin/apply: move 'check_index' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:10Z","receivedAt":"2016-04-24T13:33:10Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 69 +++++++++++++++++++++++++++++++++------------------------\n 1 file changed, 40 insertions(+), 29 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 6c628f6..3f8671c 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -30,6 +30,10 @@ struct apply_state {\n \t *    files that are being modified, but doesn't apply the patch\n \t */\n \tint check;\n+\n+\t/* --index updates the cache as well. */\n+\tint check_index;\n+\n \tint unidiff_zero;\n };\n \n@@ -37,14 +41,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@@ -3248,13 +3250,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@@ -3280,7 +3283,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@@ -3298,7 +3302,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@@ -3357,7 +3361,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@@ -3384,7 +3390,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@@ -3434,11 +3440,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@@ -3473,7 +3479,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@@ -3504,7 +3510,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@@ -3527,7 +3536,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@@ -3577,11 +3586,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@@ -3657,7 +3668,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@@ -3678,7 +3689,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@@ -3693,14 +3704,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@@ -3740,7 +3751,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@@ -3767,7 +3778,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@@ -3822,7 +3833,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@@ -4434,11 +4445,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@@ -4565,7 +4576,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@@ -4632,20 +4643,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.1.300.g5fed0c0\n"},{"id":"284317","messageId":"1461504863-15946-12-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 11/83] builtin/apply: move 'apply_in_reverse' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:11Z","receivedAt":"2016-04-24T13:33:11Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 51 ++++++++++++++++++++++++++++++++-------------------\n 1 file changed, 32 insertions(+), 19 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 3f8671c..755e0e3 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -25,6 +25,8 @@ struct apply_state {\n \tconst char *prefix;\n \tint prefix_length;\n \n+\tint apply_in_reverse;\n+\n \t/*\n \t *  --check turns on checking that the working tree matches the\n \t *    files that are being modified, but doesn't apply the patch\n@@ -53,7 +55,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@@ -1561,8 +1562,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@@ -1602,12 +1606,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@@ -1615,7 +1619,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@@ -1671,7 +1675,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@@ -1683,7 +1690,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 = 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\"), linenr);\n \t\tfragment->patch = line;\n@@ -2013,8 +2020,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@@ -2747,7 +2756,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@@ -2920,7 +2929,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@@ -2954,7 +2963,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@@ -2967,7 +2978,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@@ -3000,7 +3011,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@@ -3061,7 +3074,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@@ -3084,7 +3097,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@@ -4423,7 +4436,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@@ -4602,7 +4615,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.1.300.g5fed0c0\n"},{"id":"284256","messageId":"1461504863-15946-13-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 12/83] builtin/apply: move 'apply_with_reject' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:12Z","receivedAt":"2016-04-24T13:33:12Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-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 755e0e3..67560e4 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 \tint apply_in_reverse;\n+\tint apply_with_reject;\n \n \t/*\n \t *  --check turns on checking that the working tree matches the\n@@ -55,7 +56,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@@ -3103,7 +3103,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@@ -4469,11 +4469,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@@ -4619,7 +4619,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@@ -4649,7 +4649,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@@ -4658,7 +4658,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.1.300.g5fed0c0\n"},{"id":"284257","messageId":"1461504863-15946-14-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 13/83] builtin/apply: move 'apply_verbosely' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:13Z","receivedAt":"2016-04-24T13:33:13Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-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 67560e4..154679e 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -27,6 +27,7 @@ struct apply_state {\n \n \tint apply_in_reverse;\n \tint apply_with_reject;\n+\tint apply_verbosely;\n \n \t/*\n \t *  --check turns on checking that the working tree matches the\n@@ -56,7 +57,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@@ -2812,7 +2812,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@@ -2927,7 +2927,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@@ -2949,7 +2949,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@@ -3863,7 +3863,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@@ -4294,7 +4294,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@@ -4309,7 +4309,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@@ -4365,7 +4365,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@@ -4380,7 +4380,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@@ -4444,7 +4444,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@@ -4472,7 +4472,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@@ -4623,7 +4623,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@@ -4659,7 +4659,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.1.300.g5fed0c0\n"},{"id":"284314","messageId":"1461504863-15946-15-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 14/83] builtin/apply: move 'update_index' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:14Z","receivedAt":"2016-04-24T13:33:14Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 46 +++++++++++++++++++++++++++-------------------\n 1 file changed, 27 insertions(+), 19 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 154679e..b57be2c 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -39,6 +39,8 @@ struct apply_state {\n \tint check_index;\n \n \tint unidiff_zero;\n+\n+\tint update_index;\n };\n \n /*\n@@ -51,7 +53,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@@ -4097,9 +4098,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@@ -4110,14 +4111,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@@ -4227,13 +4232,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@@ -4254,7 +4260,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@@ -4266,22 +4272,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@@ -4289,9 +4297,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@@ -4378,7 +4386,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@@ -4458,8 +4466,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@@ -4723,7 +4731,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.1.300.g5fed0c0\n"},{"id":"284319","messageId":"1461504863-15946-16-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 15/83] builtin/apply: move 'allow_overlap' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:15Z","receivedAt":"2016-04-24T13:33:15Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-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 b57be2c..a5dff99 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -25,6 +25,7 @@ struct apply_state {\n \tconst char *prefix;\n \tint prefix_length;\n \n+\tint allow_overlap;\n \tint apply_in_reverse;\n \tint apply_with_reject;\n \tint apply_verbosely;\n@@ -58,7 +59,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@@ -2635,7 +2635,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@@ -2700,7 +2701,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@@ -2948,7 +2949,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@@ -4629,7 +4630,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.1.300.g5fed0c0\n"},{"id":"284318","messageId":"1461504863-15946-17-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 16/83] builtin/apply: move 'cached' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:16Z","receivedAt":"2016-04-24T13:33:16Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 37 +++++++++++++++++++++----------------\n 1 file changed, 21 insertions(+), 16 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex a5dff99..ba828df 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -30,6 +30,9 @@ struct apply_state {\n \tint apply_with_reject;\n \tint apply_verbosely;\n \n+\t/* --cached updates only the cache without ever touching the working tree. */\n+\tint cached;\n+\n \t/*\n \t *  --check turns on checking that the working tree matches the\n \t *    files that are being modified, but doesn't apply the patch\n@@ -48,13 +51,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@@ -3272,7 +3273,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@@ -3545,7 +3546,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@@ -3563,9 +3564,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@@ -3573,7 +3574,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@@ -3611,7 +3612,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@@ -4105,7 +4106,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@@ -4138,7 +4139,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@@ -4190,9 +4191,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@@ -4270,7 +4275,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@@ -4600,7 +4605,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@@ -4660,7 +4665,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@@ -4673,7 +4678,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.1.300.g5fed0c0\n"},{"id":"284253","messageId":"1461504863-15946-18-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 17/83] builtin/apply: move 'diffstat' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:17Z","receivedAt":"2016-04-24T13:33:17Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-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 ba828df..d90948a 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -33,6 +33,9 @@ struct apply_state {\n \t/* --cached updates only the cache without ever touching the working tree. */\n \tint cached;\n \n+\t/* --stat does just a diffstat, and doesn't actually apply */\n+\tint diffstat;\n+\n \t/*\n \t *  --check turns on checking that the working tree matches the\n \t *    files that are being modified, but doesn't apply the patch\n@@ -48,7 +51,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@@ -56,7 +58,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@@ -4496,7 +4497,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@@ -4593,7 +4594,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@@ -4674,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 && (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.1.300.g5fed0c0\n"},{"id":"284315","messageId":"1461504863-15946-19-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 18/83] builtin/apply: move 'numstat' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:18Z","receivedAt":"2016-04-24T13:33:18Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-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 d90948a..16d78f9 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -36,6 +36,9 @@ struct apply_state {\n \t/* --stat does just a diffstat, and doesn't actually apply */\n \tint diffstat;\n \n+\t/* --numstat does numeric diffstat, and doesn't actually apply */\n+\tint numstat;\n+\n \t/*\n \t *  --check turns on checking that the working tree matches the\n \t *    files that are being modified, but doesn't apply the patch\n@@ -51,14 +54,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@@ -4500,7 +4501,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@@ -4598,7 +4599,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@@ -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 && (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.1.300.g5fed0c0\n"},{"id":"284255","messageId":"1461504863-15946-20-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 19/83] builtin/apply: move 'summary' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:19Z","receivedAt":"2016-04-24T13:33:19Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-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 16d78f9..e488879 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -39,6 +39,8 @@ struct apply_state {\n \t/* --numstat does numeric diffstat, and doesn't actually apply */\n \tint numstat;\n \n+\tint summary;\n+\n \t/*\n \t *  --check turns on checking that the working tree matches the\n \t *    files that are being modified, but doesn't apply the patch\n@@ -60,7 +62,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@@ -4504,7 +4505,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@@ -4601,7 +4602,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@@ -4676,7 +4677,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.1.300.g5fed0c0\n"},{"id":"284316","messageId":"1461504863-15946-21-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 20/83] builtin/apply: move 'threeway' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:20Z","receivedAt":"2016-04-24T13:33:20Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 15 ++++++++-------\n 1 file changed, 8 insertions(+), 7 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex e488879..33a1f8f 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -41,6 +41,8 @@ struct apply_state {\n \n \tint summary;\n \n+\tint threeway;\n+\n \t/*\n \t *  --check turns on checking that the working tree matches the\n \t *    files that are being modified, but doesn't apply the patch\n@@ -64,7 +66,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@@ -3504,7 +3505,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@@ -3799,7 +3800,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@@ -4614,7 +4615,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@@ -4666,11 +4667,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.1.300.g5fed0c0\n"},{"id":"284254","messageId":"1461504863-15946-22-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 21/83] builtin/apply: move 'no-add' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:21Z","receivedAt":"2016-04-24T13:33:21Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-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 33a1f8f..506357c 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -43,6 +43,8 @@ struct apply_state {\n \n \tint threeway;\n \n+\tint no_add;\n+\n \t/*\n \t *  --check turns on checking that the working tree matches the\n \t *    files that are being modified, but doesn't apply the patch\n@@ -65,7 +67,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@@ -2796,7 +2797,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@@ -4595,7 +4596,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.1.300.g5fed0c0\n"},{"id":"284313","messageId":"1461504863-15946-23-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 22/83] builtin/apply: move 'unsafe_paths' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:22Z","receivedAt":"2016-04-24T13:33:22Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-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 506357c..c45e481 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -57,6 +57,8 @@ struct apply_state {\n \tint unidiff_zero;\n \n \tint update_index;\n+\n+\tint unsafe_paths;\n };\n \n /*\n@@ -67,7 +69,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@@ -3842,7 +3843,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@@ -4612,7 +4613,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@@ -4689,7 +4690,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.1.300.g5fed0c0\n"},{"id":"284259","messageId":"1461504863-15946-24-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 23/83] builtin/apply: move 'line_termination' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:23Z","receivedAt":"2016-04-24T13:33:23Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 13 ++++++++-----\n 1 file changed, 8 insertions(+), 5 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex c45e481..228595d 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -59,6 +59,8 @@ struct apply_state {\n \tint update_index;\n \n \tint unsafe_paths;\n+\n+\tint line_termination;\n };\n \n /*\n@@ -70,7 +72,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@@ -3993,7 +3994,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@@ -4002,7 +4004,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@@ -4506,7 +4508,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@@ -4622,7 +4624,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@@ -4659,6 +4661,7 @@ int cmd_apply(int argc, const char **argv, 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-- \n2.8.1.300.g5fed0c0\n"},{"id":"284258","messageId":"1461504863-15946-25-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 24/83] builtin/apply: move 'fake_ancestor' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:24Z","receivedAt":"2016-04-24T13:33:24Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"By 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\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 14 ++++++--------\n 1 file changed, 6 insertions(+), 8 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 228595d..f69f982 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -39,6 +39,8 @@ struct apply_state {\n \t/* --numstat does numeric diffstat, and doesn't actually apply */\n \tint numstat;\n \n+\tconst char *fake_ancestor;\n+\n \tint summary;\n \n \tint threeway;\n@@ -63,15 +65,11 @@ struct apply_state {\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@@ -4501,8 +4499,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@@ -4621,7 +4619,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@@ -4683,7 +4681,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.1.300.g5fed0c0\n"},{"id":"284263","messageId":"1461504863-15946-26-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 25/83] builtin/apply: move 'p_context' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:25Z","receivedAt":"2016-04-24T13:33:25Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 8 +++++---\n 1 file changed, 5 insertions(+), 3 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex f69f982..b6d2343 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -63,6 +63,8 @@ struct apply_state {\n \tint unsafe_paths;\n \n \tint line_termination;\n+\n+\tunsigned int p_context;\n };\n \n static int newfd = -1;\n@@ -70,7 +72,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@@ -2888,7 +2889,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@@ -4624,7 +4625,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@@ -4660,6 +4661,7 @@ int cmd_apply(int argc, const char **argv, 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-- \n2.8.1.300.g5fed0c0\n"},{"id":"284311","messageId":"1461504863-15946-27-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 26/83] builtin/apply: move 'apply' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:26Z","receivedAt":"2016-04-24T13:33:26Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-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 b6d2343..699cabf 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -25,6 +25,7 @@ struct apply_state {\n \tconst char *prefix;\n \tint prefix_length;\n \n+\tint apply;\n \tint allow_overlap;\n \tint apply_in_reverse;\n \tint apply_with_reject;\n@@ -71,7 +72,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@@ -142,10 +143,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@@ -2074,7 +2076,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\"), linenr);\n \t}\n@@ -2933,7 +2935,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@@ -4477,9 +4479,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@@ -4488,12 +4490,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@@ -4660,6 +4662,7 @@ int cmd_apply(int argc, const char **argv, 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@@ -4682,9 +4685,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@@ -4712,11 +4715,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@@ -4734,7 +4737,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.1.300.g5fed0c0\n"},{"id":"284309","messageId":"1461504863-15946-28-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 27/83] builtin/apply: move 'read_stdin' global into cmd_apply()","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:27Z","receivedAt":"2016-04-24T13:33:27Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-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 699cabf..be237d1 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -96,7 +96,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@@ -4586,6 +4585,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 \tstruct apply_state state;\n \n \tconst char *whitespace_option = NULL;\n-- \n2.8.1.300.g5fed0c0\n"},{"id":"284260","messageId":"1461504863-15946-29-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 28/83] builtin/apply: move 'patch_input_file' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:28Z","receivedAt":"2016-04-24T13:33:28Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 28 ++++++++++++++++++----------\n 1 file changed, 18 insertions(+), 10 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex be237d1..ab311c1 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -66,6 +66,8 @@ struct apply_state {\n \tint line_termination;\n \n \tunsigned int p_context;\n+\n+\tconst char *patch_input_file;\n };\n \n static int newfd = -1;\n@@ -94,7 +96,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@@ -1540,7 +1541,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 l_nr)\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 l_nr)\n {\n \tchar *err;\n \n@@ -1554,15 +1559,18 @@ static void record_ws_error(unsigned result, const char *line, int len, int l_nr\n \n \terr = whitespace_error_string(result);\n \tfprintf(stderr, \"%s:%d: %s.\\n%.*s\\n\",\n-\t\tpatch_input_file, l_nr, err, len, line);\n+\t\tstate->patch_input_file, l_nr, 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, linenr);\n+\trecord_ws_error(state, result, line + 1, len - 2, linenr);\n }\n \n /*\n@@ -1617,12 +1625,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@@ -1630,7 +1638,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@@ -2920,7 +2928,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@@ -4443,7 +4451,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.1.300.g5fed0c0\n"},{"id":"284308","messageId":"1461504863-15946-30-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 29/83] builtin/apply: move 'limit_by_name' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:29Z","receivedAt":"2016-04-24T13:33:29Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 23 ++++++++++++++---------\n 1 file changed, 14 insertions(+), 9 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex ab311c1..c8b9bf0 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -68,6 +68,8 @@ struct apply_state {\n \tunsigned int p_context;\n \n \tconst char *patch_input_file;\n+\n+\tstruct string_list limit_by_name;\n };\n \n static int newfd = -1;\n@@ -1974,13 +1976,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@@ -1998,8 +2001,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@@ -4537,14 +4540,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@@ -4599,10 +4604,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.1.300.g5fed0c0\n"},{"id":"284312","messageId":"1461504863-15946-31-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 30/83] builtin/apply: move 'has_include' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:30Z","receivedAt":"2016-04-24T13:33:30Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-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 c8b9bf0..0717cd2 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -70,6 +70,7 @@ struct apply_state {\n \tconst char *patch_input_file;\n \n \tstruct string_list limit_by_name;\n+\tint has_include;\n };\n \n static int newfd = -1;\n@@ -1976,7 +1977,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@@ -2012,7 +2012,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@@ -4550,7 +4550,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.1.300.g5fed0c0\n"},{"id":"284310","messageId":"1461504863-15946-32-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 31/83] builtin/apply: move 'p_value' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:31Z","receivedAt":"2016-04-24T13:33:31Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-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 0717cd2..979849c 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -65,6 +65,7 @@ struct apply_state {\n \n \tint line_termination;\n \n+\tint p_value;\n \tunsigned int p_context;\n \n \tconst char *patch_input_file;\n@@ -75,7 +76,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@@ -896,24 +896,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@@ -932,7 +932,9 @@ static void parse_traditional_patch(struct apply_state *state,\n \t\tdie(_(\"unable to find filename in patch at line %d\"), 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@@ -949,10 +951,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@@ -962,7 +968,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, 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@@ -975,81 +981,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@@ -1057,7 +1087,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@@ -1065,7 +1097,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@@ -1102,7 +1136,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@@ -1111,15 +1147,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@@ -1136,7 +1174,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@@ -1154,7 +1194,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@@ -1171,7 +1211,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@@ -1182,7 +1222,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@@ -1197,7 +1237,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@@ -1213,7 +1253,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@@ -1257,7 +1297,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@@ -1273,7 +1313,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@@ -1287,7 +1331,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@@ -1300,7 +1344,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, 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@@ -1330,7 +1374,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@@ -1500,7 +1544,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@@ -1509,8 +1553,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, 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@@ -4555,9 +4599,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@@ -4610,7 +4656,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@@ -4677,6 +4723,7 @@ int cmd_apply(int argc, const char **argv, 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-- \n2.8.1.300.g5fed0c0\n"},{"id":"284262","messageId":"1461504863-15946-33-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 32/83] builtin/apply: move 'p_value_known' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:32Z","receivedAt":"2016-04-24T13:33:32Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-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 979849c..fecdb66 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -66,6 +66,7 @@ struct apply_state {\n \tint line_termination;\n \n \tint p_value;\n+\tint p_value_known;\n \tunsigned int p_context;\n \n \tconst char *patch_input_file;\n@@ -76,8 +77,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@@ -890,14 +889,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@@ -4604,7 +4603,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.1.300.g5fed0c0\n"},{"id":"284261","messageId":"1461504863-15946-34-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 33/83] builtin/apply: move 'root' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:33Z","receivedAt":"2016-04-24T13:33:33Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-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 fecdb66..209a1b4 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -73,6 +73,8 @@ struct apply_state {\n \n \tstruct string_list limit_by_name;\n \tint has_include;\n+\n+\tstruct strbuf root;\n };\n \n static int newfd = -1;\n@@ -98,8 +100,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@@ -489,7 +489,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@@ -513,8 +516,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@@ -677,8 +680,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@@ -716,32 +723,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@@ -749,10 +763,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@@ -777,7 +791,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@@ -902,17 +916,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@@ -957,7 +971,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@@ -967,7 +981,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, 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@@ -1042,7 +1056,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@@ -1052,7 +1066,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@@ -1062,7 +1076,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@@ -1072,7 +1086,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@@ -1331,8 +1345,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@@ -4630,9 +4644,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@@ -4711,7 +4726,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@@ -4724,6 +4739,7 @@ int cmd_apply(int argc, const char **argv, 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-- \n2.8.1.300.g5fed0c0\n"},{"id":"284307","messageId":"1461504863-15946-35-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 34/83] builtin/apply: move 'whitespace_error' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:34Z","receivedAt":"2016-04-24T13:33:34Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 27 ++++++++++++++-------------\n 1 file changed, 14 insertions(+), 13 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 209a1b4..9c4f67f 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -75,6 +75,8 @@ struct apply_state {\n \tint has_include;\n \n \tstruct strbuf root;\n+\n+\tint whitespace_error;\n };\n \n static int newfd = -1;\n@@ -90,7 +92,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@@ -1612,9 +1613,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@@ -2872,7 +2873,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@@ -4545,7 +4546,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@@ -4797,11 +4798,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@@ -4810,18 +4811,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.1.300.g5fed0c0\n"},{"id":"284304","messageId":"1461504863-15946-36-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 35/83] builtin/apply: move 'whitespace_option' into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:35Z","receivedAt":"2016-04-24T13:33:35Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"This will enable further refactoring.\n\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 9c4f67f..64bc3e9 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -76,6 +76,7 @@ struct apply_state {\n \n \tstruct strbuf root;\n \n+\tconst char *whitespace_option;\n \tint whitespace_error;\n };\n \n@@ -4635,9 +4636,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@@ -4662,8 +4663,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@@ -4703,7 +4702,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@@ -4791,11 +4790,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.1.300.g5fed0c0\n"},{"id":"284265","messageId":"1461504863-15946-37-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 36/83] builtin/apply: remove whitespace_option arg from set_default_whitespace_mode()","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:36Z","receivedAt":"2016-04-24T13:33:36Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-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 64bc3e9..401e111 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -147,10 +147,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@@ -4790,11 +4789,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.1.300.g5fed0c0\n"},{"id":"284264","messageId":"1461504863-15946-38-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 37/83] builtin/apply: move 'squelch_whitespace_errors' into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:37Z","receivedAt":"2016-04-24T13:33:37Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-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 401e111..259f256 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -78,6 +78,7 @@ struct apply_state {\n \n \tconst char *whitespace_option;\n \tint whitespace_error;\n+\tint squelch_whitespace_errors;\n };\n \n static int newfd = -1;\n@@ -93,7 +94,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@@ -102,7 +102,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@@ -122,7 +122,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@@ -1614,8 +1614,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@@ -4636,9 +4636,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@@ -4738,11 +4737,12 @@ int cmd_apply(int argc, const char **argv, 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@@ -4797,10 +4797,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.1.300.g5fed0c0\n"},{"id":"284306","messageId":"1461504863-15946-39-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 38/83] builtin/apply: move 'applied_after_fixing_ws' into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:38Z","receivedAt":"2016-04-24T13:33:38Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-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 259f256..52602af 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -79,6 +79,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@@ -94,7 +95,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@@ -2878,7 +2878,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@@ -4811,11 +4811,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.1.300.g5fed0c0\n"},{"id":"284266","messageId":"1461504863-15946-40-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 39/83] builtin/apply: move 'ws_error_action' into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:39Z","receivedAt":"2016-04-24T13:33:39Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 62 +++++++++++++++++++++++++++++++--------------------------\n 1 file changed, 34 insertions(+), 28 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 52602af..7d91454 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@@ -80,6 +87,8 @@ struct apply_state {\n \tint whitespace_error;\n \tint squelch_whitespace_errors;\n \tint applied_after_fixing_ws;\n+\n+\tenum ws_error_action ws_error_action;\n };\n \n static int newfd = -1;\n@@ -89,12 +98,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@@ -105,28 +108,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@@ -150,7 +153,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@@ -1685,12 +1688,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@@ -1698,7 +1701,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@@ -2411,7 +2414,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@@ -2432,7 +2436,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@@ -2505,7 +2509,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@@ -2617,7 +2621,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@@ -2661,7 +2666,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@@ -2874,7 +2879,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@@ -2952,7 +2957,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@@ -2988,10 +2993,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@@ -3002,7 +3007,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@@ -4546,7 +4551,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@@ -4738,6 +4743,7 @@ int cmd_apply(int argc, const char **argv, 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@@ -4806,7 +4812,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.1.300.g5fed0c0\n"},{"id":"284300","messageId":"1461504863-15946-41-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 40/83] builtin/apply: move 'ws_ignore_action' into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:40Z","receivedAt":"2016-04-24T13:33:40Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-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 7d91454..fa07b33 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@@ -89,6 +95,7 @@ struct apply_state {\n \tint applied_after_fixing_ws;\n \n \tenum ws_error_action ws_error_action;\n+\tenum ws_ignore ws_ignore_action;\n };\n \n static int newfd = -1;\n@@ -98,13 +105,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@@ -135,16 +135,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@@ -2505,7 +2506,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@@ -4628,12 +4629,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@@ -4708,10 +4710,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@@ -4744,13 +4746,14 @@ int cmd_apply(int argc, const char **argv, 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 \targc = parse_options(argc, argv, state.prefix, builtin_apply_options,\n \t\t\tapply_usage, 0);\n-- \n2.8.1.300.g5fed0c0\n"},{"id":"284302","messageId":"1461504863-15946-42-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 41/83] builtin/apply: move 'max_change' and 'max_len' into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:41Z","receivedAt":"2016-04-24T13:33:41Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-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 fa07b33..bb58174 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -78,6 +78,14 @@ struct apply_state {\n \n \tint line_termination;\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 \tint p_value;\n \tint p_value_known;\n \tunsigned int p_context;\n@@ -158,13 +166,6 @@ static void set_default_whitespace_mode(struct apply_state *state)\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  * things are flags, where -1 means \"don't know yet\".\n@@ -2188,7 +2189,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@@ -2199,7 +2200,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@@ -2222,13 +2223,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@@ -4055,7 +4056,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@@ -4063,7 +4064,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@@ -4161,25 +4162,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@@ -4536,7 +4537,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@@ -4580,7 +4581,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.1.300.g5fed0c0\n"},{"id":"284299","messageId":"1461504863-15946-43-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 42/83] builtin/apply: move 'linenr' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:42Z","receivedAt":"2016-04-24T13:33:42Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 75 ++++++++++++++++++++++++++++++---------------------------\n 1 file changed, 40 insertions(+), 35 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex bb58174..f387340 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -86,6 +86,13 @@ struct apply_state {\n \tint max_change;\n \tint max_len;\n \n+\t/*\n+\t * Various \"current state\", notably line numbers and what\n+\t * file (and how) we're patching right now.. The \"is_xxxx\"\n+\t * things are flags, where -1 means \"don't know yet\".\n+\t */\n+\tint linenr;\n+\n \tint p_value;\n \tint p_value_known;\n \tunsigned int p_context;\n@@ -166,13 +173,6 @@ static void set_default_whitespace_mode(struct apply_state *state)\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 linenr = 1;\n-\n-/*\n  * This represents one \"hunk\" from a patch, starting with\n  * \"@@ -oldpos,oldlines +newpos,newlines @@\" marker.  The\n  * patch text is pointed at by patch, and its byte length\n@@ -948,7 +948,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\"), 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@@ -986,17 +986,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, 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\"), 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@@ -1359,8 +1359,8 @@ static int parse_git_header(struct apply_state *state,\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)(struct apply_state *, const char *, struct patch *);\n@@ -1531,7 +1531,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, 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@@ -1552,7 +1552,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    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@@ -1573,13 +1573,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, 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@@ -1601,7 +1601,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\tlinenr += 2;\n+\t\tstate->linenr += 2;\n \t\treturn offset;\n \t}\n \treturn -1;\n@@ -1636,7 +1636,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, linenr);\n+\trecord_ws_error(state, result, line + 1, len - 2, state->linenr);\n }\n \n /*\n@@ -1669,11 +1669,11 @@ static int parse_fragment(struct apply_state *state,\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@@ -1772,10 +1772,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 = 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\"), 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@@ -1861,7 +1861,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@@ -1903,13 +1904,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@@ -1963,7 +1964,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@@ -1972,7 +1973,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@@ -1993,15 +1997,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\"), 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@@ -2116,8 +2120,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\tlinenr++;\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@@ -2137,7 +2141,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\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@@ -2151,7 +2155,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\"), linenr);\n+\t\t\tdie(_(\"patch with only garbage at line %d\"), state->linenr);\n \t}\n \n \treturn offset + hdrsize + patchsize;\n@@ -4748,6 +4752,7 @@ int cmd_apply(int argc, const char **argv, 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.1.300.g5fed0c0\n"},{"id":"284305","messageId":"1461504863-15946-44-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 43/83] builtin/apply: move 'fn_table' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:43Z","receivedAt":"2016-04-24T13:33:43Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 45 +++++++++++++++++++++++----------------------\n 1 file changed, 23 insertions(+), 22 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex f387340..b4c2bd8 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -93,6 +93,12 @@ struct apply_state {\n \t */\n \tint linenr;\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 \tint p_value;\n \tint p_value_known;\n \tunsigned int p_context;\n@@ -291,13 +297,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@@ -3228,14 +3227,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@@ -3267,7 +3266,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@@ -3277,7 +3276,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@@ -3286,12 +3285,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@@ -3299,7 +3298,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@@ -3320,7 +3319,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@@ -3328,7 +3329,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@@ -3397,7 +3398,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@@ -3593,7 +3594,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@@ -3627,7 +3628,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@@ -3873,7 +3874,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@@ -3951,7 +3952,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@@ -4595,7 +4596,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-- \n2.8.1.300.g5fed0c0\n"},{"id":"284303","messageId":"1461504863-15946-45-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 44/83] builtin/apply: move 'symlink_changes' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:44Z","receivedAt":"2016-04-24T13:33:44Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-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 b4c2bd8..8508607 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@@ -99,6 +113,8 @@ struct apply_state {\n \t */\n \tstruct string_list fn_table;\n \n+\tstruct string_list symlink_changes;\n+\n \tint p_value;\n \tint p_value_known;\n \tunsigned int p_context;\n@@ -3723,52 +3739,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@@ -3782,7 +3788,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@@ -3951,7 +3957,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-- \n2.8.1.300.g5fed0c0\n"},{"id":"284301","messageId":"1461504863-15946-46-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 45/83] builtin/apply: move 'state' init into init_apply_state()","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:45Z","receivedAt":"2016-04-24T13:33:45Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 41 +++++++++++++++++++++++------------------\n 1 file changed, 23 insertions(+), 18 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 8508607..f2dffa9 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -4670,6 +4670,28 @@ 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+{\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_value = 1;\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+\tstate->linenr = 1;\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(state, apply_default_ignorewhitespace);\n+}\n+\n int cmd_apply(int argc, const char **argv, const char *prefix_)\n {\n \tint i;\n@@ -4749,24 +4771,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-\tstate.apply = 1;\n-\tstate.line_termination = '\\n';\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-\tstate.ws_ignore_action = ignore_ws_none;\n-\tstate.linenr = 1;\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(&state, 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-- \n2.8.1.300.g5fed0c0\n"},{"id":"284268","messageId":"1461504863-15946-47-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 46/83] builtin/apply: move 'state' check into check_apply_state()","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:46Z","receivedAt":"2016-04-24T13:33:46Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-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 f2dffa9..94bf120 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -4692,11 +4692,38 @@ static void init_apply_state(struct apply_state *state, const char *prefix_)\n \t\tparse_ignorewhitespace_option(state, apply_default_ignorewhitespace);\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@@ -4776,28 +4803,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.1.300.g5fed0c0\n"},{"id":"284269","messageId":"1461504863-15946-48-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 47/83] builtin/apply: move applying patches into apply_all_patches()","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:47Z","receivedAt":"2016-04-24T13:33:47Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 128 ++++++++++++++++++++++++++++++--------------------------\n 1 file changed, 69 insertions(+), 59 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 94bf120..787426f 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -4720,13 +4720,79 @@ 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 \tstruct apply_state state;\n \n \tstruct option builtin_apply_options[] = {\n@@ -4805,61 +4871,5 @@ 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-\n-\treturn !!errs;\n+\treturn apply_all_patches(&state, argc, argv, options);\n }\n-- \n2.8.1.300.g5fed0c0\n"},{"id":"284298","messageId":"1461504863-15946-49-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 48/83] builtin/apply: rename 'prefix_' parameter to 'prefix'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:48Z","receivedAt":"2016-04-24T13:33:48Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-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 787426f..6c0b153 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -4670,10 +4670,10 @@ 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, const char *prefix)\n {\n \tmemset(state, 0, sizeof(*state));\n-\tstate->prefix = prefix_;\n+\tstate->prefix = prefix;\n \tstate->prefix_length = state->prefix ? strlen(state->prefix) : 0;\n \tstate->apply = 1;\n \tstate->line_termination = '\\n';\n@@ -4789,7 +4789,7 @@ static int apply_all_patches(struct apply_state *state,\n \treturn !!errs;\n }\n \n-int cmd_apply(int argc, const char **argv, const char *prefix_)\n+int cmd_apply(int argc, const char **argv, const char *prefix)\n {\n \tint force_apply = 0;\n \tint options = 0;\n@@ -4864,7 +4864,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);\n \n \targc = parse_options(argc, argv, state.prefix, builtin_apply_options,\n \t\t\tapply_usage, 0);\n-- \n2.8.1.300.g5fed0c0\n"},{"id":"284270","messageId":"1461504863-15946-50-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 49/83] builtin/apply: move 'lock_file' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:49Z","receivedAt":"2016-04-24T13:33:49Z","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.\nSo let's make the 'lock_file' variable a pointer to a 'struct lock_file'\nand let's alloc the struct when needed.\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 16 +++++++++++-----\n 1 file changed, 11 insertions(+), 5 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 6c0b153..d26419a 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 \tint apply;\n \tint allow_overlap;\n \tint apply_in_reverse;\n@@ -4515,8 +4521,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@@ -4568,8 +4572,10 @@ 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(&lock_file, 1);\n+\tif (state->update_index && newfd < 0) {\n+\t\tstate->lock_file = xcalloc(1, sizeof(struct lock_file));\n+\t\tnewfd = hold_locked_index(state->lock_file, 1);\n+\t}\n \n \tif (state->check_index) {\n \t\tif (read_cache() < 0)\n@@ -4782,7 +4788,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-- \n2.8.1.300.g5fed0c0\n"},{"id":"284271","messageId":"1461504863-15946-51-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 50/83] builtin/apply: get rid of the 'newfd' global","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:50Z","receivedAt":"2016-04-24T13:33:50Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"The 'newfd' global is useless now as we can just test\nfor \"lock_file == NULL\" instead of \"newfd < 0\".\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 6 ++----\n 1 file changed, 2 insertions(+), 4 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex d26419a..84ff2da 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -141,8 +141,6 @@ struct apply_state {\n \tenum ws_ignore ws_ignore_action;\n };\n \n-static int newfd = -1;\n-\n static const char * const apply_usage[] = {\n \tN_(\"git apply [<options>] [<patch>...]\"),\n \tNULL\n@@ -4572,9 +4570,9 @@ 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+\tif (state->update_index && state->lock_file == NULL) {\n \t\tstate->lock_file = xcalloc(1, sizeof(struct lock_file));\n-\t\tnewfd = hold_locked_index(state->lock_file, 1);\n+\t\thold_locked_index(state->lock_file, 1);\n \t}\n \n \tif (state->check_index) {\n-- \n2.8.1.300.g5fed0c0\n"},{"id":"284267","messageId":"1461504863-15946-52-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 51/83] builtin/apply: make apply_patch() return -1 instead of die()ing","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:51Z","receivedAt":"2016-04-24T13:33:51Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify `git apply` functionality we have to signal errors\nto the caller instead of die()ing.\n\nAs a first step in this direction, let's make apply_patch() return\n-1 in case of errors instead of dying. For now its only caller\napply_all_patches() will exit(1) when apply_patch() return -1.\n\nIn a later patch, apply_all_patches() will return -1 too instead of\nexiting.\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 39 ++++++++++++++++++++++++++++-----------\n 1 file changed, 28 insertions(+), 11 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 84ff2da..bc209f5 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -4522,6 +4522,14 @@ static int write_out_results(struct apply_state *state, struct patch *list)\n #define INACCURATE_EOF\t(1<<0)\n #define RECOUNT\t\t(1<<1)\n \n+/*\n+ * Try to apply a patch.\n+ *\n+ * Returns:\n+ *  -1 if an error happened\n+ *   0 if the patch applied\n+ *   1 if the patch did not apply\n+ */\n static int apply_patch(struct apply_state *state,\n \t\t       int fd,\n \t\t       const char *filename,\n@@ -4564,7 +4572,7 @@ static int apply_patch(struct apply_state *state,\n \t}\n \n \tif (!list && !skipped_patch)\n-\t\tdie(_(\"unrecognized input\"));\n+\t\treturn error(_(\"unrecognized input\"));\n \n \tif (state->whitespace_error && (state->ws_error_action == die_on_ws_error))\n \t\tstate->apply = 0;\n@@ -4575,19 +4583,17 @@ static int apply_patch(struct apply_state *state,\n \t\thold_locked_index(state->lock_file, 1);\n \t}\n \n-\tif (state->check_index) {\n-\t\tif (read_cache() < 0)\n-\t\t\tdie(_(\"unable to read index file\"));\n-\t}\n+\tif (state->check_index && read_cache() < 0)\n+\t\treturn error(_(\"unable to read index file\"));\n \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+\t\treturn -1;\n \n \tif (state->apply && write_out_results(state, list)) {\n \t\tif (state->apply_with_reject)\n-\t\t\texit(1);\n+\t\t\treturn -1;\n \t\t/* with --3way, we still need to write the index out */\n \t\treturn 1;\n \t}\n@@ -4730,6 +4736,7 @@ static int apply_all_patches(struct apply_state *state,\n \t\t\t     int options)\n {\n \tint i;\n+\tint res;\n \tint errs = 0;\n \tint read_stdin = 1;\n \n@@ -4738,7 +4745,10 @@ static int apply_all_patches(struct apply_state *state,\n \t\tint fd;\n \n \t\tif (!strcmp(arg, \"-\")) {\n-\t\t\terrs |= apply_patch(state, 0, \"<stdin>\", options);\n+\t\t\tres = apply_patch(state, 0, \"<stdin>\", options);\n+\t\t\tif (res < 0)\n+\t\t\t\texit(1);\n+\t\t\terrs |= res;\n \t\t\tread_stdin = 0;\n \t\t\tcontinue;\n \t\t} else if (0 < state->prefix_length)\n@@ -4751,12 +4761,19 @@ static int apply_all_patches(struct apply_state *state,\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\tres = apply_patch(state, fd, arg, options);\n+\t\tif (res < 0)\n+\t\t\texit(1);\n+\t\terrs |= res;\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 (read_stdin) {\n+\t\tres = apply_patch(state, 0, \"<stdin>\", options);\n+\t\tif (res < 0)\n+\t\t\texit(1);\n+\t\terrs |= res;\n+\t}\n \n \tif (state->whitespace_error) {\n \t\tif (state->squelch_whitespace_errors &&\n-- \n2.8.1.300.g5fed0c0\n"},{"id":"284275","messageId":"1461504863-15946-53-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 52/83] builtin/apply: read_patch_file() return -1 instead of die()ing","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:52Z","receivedAt":"2016-04-24T13:33:52Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 8 +++++---\n 1 file changed, 5 insertions(+), 3 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex bc209f5..c0bb24c 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -454,10 +454,10 @@ static void say_patch_name(FILE *output, const char *fmt, struct patch *patch)\n \n #define SLOP (16)\n \n-static void read_patch_file(struct strbuf *sb, int fd)\n+static int read_patch_file(struct strbuf *sb, int fd)\n {\n \tif (strbuf_read(sb, fd, 0) < 0)\n-\t\tdie_errno(\"git apply: failed to read\");\n+\t\treturn error(\"git apply: failed to read: %s\", strerror(errno));\n \n \t/*\n \t * Make sure that we have some slop in the buffer\n@@ -466,6 +466,7 @@ static void read_patch_file(struct strbuf *sb, int fd)\n \t */\n \tstrbuf_grow(sb, SLOP);\n \tmemset(sb->buf + sb->len, 0, SLOP);\n+\treturn 0;\n }\n \n static unsigned long linelen(const char *buffer, unsigned long size)\n@@ -4541,7 +4542,8 @@ static int apply_patch(struct apply_state *state,\n \tint skipped_patch = 0;\n \n \tstate->patch_input_file = filename;\n-\tread_patch_file(&buf, fd);\n+\tif (read_patch_file(&buf, fd))\n+\t\treturn -1;\n \toffset = 0;\n \twhile (offset < buf.len) {\n \t\tstruct patch *patch;\n-- \n2.8.1.300.g5fed0c0\n"},{"id":"284272","messageId":"1461504863-15946-54-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 53/83] builtin/apply: make find_header() return -1 instead of die()ing","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:53Z","receivedAt":"2016-04-24T13:33:53Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To be compatible with the rest of the error handling in builtin/apply.c,\nfind_header() should return -1 instead of calling die().\n\nUnfortunately find_header() already returns -1 when no header is found,\nso let's make it return -2 instead in this case.\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c       | 33 ++++++++++++++++++++++-----------\n t/t4254-am-corrupt.sh |  2 +-\n 2 files changed, 23 insertions(+), 12 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex c0bb24c..825ffe9 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -1538,6 +1538,14 @@ static int parse_fragment_header(const char *line, int len, struct fragment *fra\n \treturn offset;\n }\n \n+/*\n+ * Find file diff header\n+ *\n+ * Returns:\n+ *  -1 in case of error\n+ *  -2 if no header was found\n+ *   the size of the header in bytes (called \"offset\") otherwise\n+ */\n static int find_header(struct apply_state *state,\n \t\t       const char *line,\n \t\t       unsigned long size,\n@@ -1571,8 +1579,8 @@ static int find_header(struct apply_state *state,\n \t\t\tstruct fragment dummy;\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\treturn error(_(\"patch fragment without header at line %d: %.*s\"),\n+\t\t\t\t     state->linenr, (int)len-1, line);\n \t\t}\n \n \t\tif (size < len + 6)\n@@ -1588,18 +1596,18 @@ static int find_header(struct apply_state *state,\n \t\t\t\tcontinue;\n \t\t\tif (!patch->old_name && !patch->new_name) {\n \t\t\t\tif (!patch->def_name)\n-\t\t\t\t\tdie(Q_(\"git diff header lacks filename information when removing \"\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\treturn error(Q_(\"git diff header lacks filename information when removing \"\n+\t\t\t\t\t\t\t\"%d leading pathname component (line %d)\",\n+\t\t\t\t\t\t\t\"git diff header lacks filename information when removing \"\n+\t\t\t\t\t\t\t\"%d leading pathname components (line %d)\",\n+\t\t\t\t\t\t\tstate->p_value),\n+\t\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\treturn error(\"git diff header lacks filename information \"\n+\t\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@@ -1624,7 +1632,7 @@ static int find_header(struct apply_state *state,\n \t\tstate->linenr += 2;\n \t\treturn offset;\n \t}\n-\treturn -1;\n+\treturn -2;\n }\n \n static void record_ws_error(struct apply_state *state,\n@@ -2115,6 +2123,9 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si\n \tint hdrsize, patchsize;\n \tint offset = find_header(state, buffer, size, &hdrsize, patch);\n \n+\tif (offset == -1)\n+\t\texit(1);\n+\n \tif (offset < 0)\n \t\treturn offset;\n \ndiff --git a/t/t4254-am-corrupt.sh b/t/t4254-am-corrupt.sh\nindex 85716dd..9bd7dd2 100755\n--- a/t/t4254-am-corrupt.sh\n+++ b/t/t4254-am-corrupt.sh\n@@ -29,7 +29,7 @@ test_expect_success 'try to apply corrupted patch' '\n '\n \n test_expect_success 'compare diagnostic; ensure file is still here' '\n-\techo \"fatal: git diff header lacks filename information (line 4)\" >expected &&\n+\techo \"error: git diff header lacks filename information (line 4)\" >expected &&\n \ttest_path_is_file f &&\n \ttest_cmp expected actual\n '\n-- \n2.8.1.300.g5fed0c0\n"},{"id":"284296","messageId":"1461504863-15946-55-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 54/83] builtin/apply: make parse_chunk() return a negative integer on error","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:54Z","receivedAt":"2016-04-24T13:33:54Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"This negative number can be -2 if no patch header has been found,\notherwise it is -1.\n\nAs parse_chunk() is called only by apply_patch() which already\nreturns -1 when an error happened, let's make it return -1 when\nparse_chunk() returns -1.\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 19 +++++++++++--------\n 1 file changed, 11 insertions(+), 8 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 825ffe9..e1d6c8b 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -2110,22 +2110,22 @@ static int use_patch(struct apply_state *state, struct patch *p)\n \treturn !state->has_include;\n }\n \n-\n /*\n  * Read the patch text in \"buffer\" that extends for \"size\" bytes; stop\n  * reading after seeing a single patch (i.e. changes to a single file).\n  * Create fragments (i.e. patch hunks) and hang them to the given patch.\n- * Return the number of bytes consumed, so that the caller can call us\n- * again for the next patch.\n+ *\n+ * Returns:\n+ *   -1 on error,\n+ *   -2 if no header was found,\n+ *   the number of bytes consumed otherwise,\n+ *     so that the caller can call us again for the next patch.\n  */\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(state, buffer, size, &hdrsize, patch);\n \n-\tif (offset == -1)\n-\t\texit(1);\n-\n \tif (offset < 0)\n \t\treturn offset;\n \n@@ -2185,8 +2185,9 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si\n \t\t * empty to us here.\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    (!patch->is_binary && !metadata_changes(patch))) {\n+\t\t\treturn error(_(\"patch with only garbage at line %d\"), state->linenr);\n+\t\t}\n \t}\n \n \treturn offset + hdrsize + patchsize;\n@@ -4566,6 +4567,8 @@ static int apply_patch(struct apply_state *state,\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\tif (nr == -1)\n+\t\t\t\treturn -1;\n \t\t\tbreak;\n \t\t}\n \t\tif (state->apply_in_reverse)\n-- \n2.8.1.300.g5fed0c0\n"},{"id":"284297","messageId":"1461504863-15946-56-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 55/83] builtin/apply: make parse_single_patch() return -1 on error","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:55Z","receivedAt":"2016-04-24T13:33:55Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c        | 17 +++++++++++++----\n t/t4012-diff-binary.sh |  4 ++--\n 2 files changed, 15 insertions(+), 6 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex e1d6c8b..de5c745 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -1785,6 +1785,10 @@ static int parse_fragment(struct apply_state *state,\n  *\n  * The (fragment->patch, fragment->size) pair points into the memory given\n  * by the caller, not a copy, when we return.\n+ *\n+ * Returns:\n+ *   -1 in case of error,\n+ *   the number of bytes in the patch otherwise.\n  */\n static int parse_single_patch(struct apply_state *state,\n \t\t\t      const char *line,\n@@ -1802,8 +1806,10 @@ static int parse_single_patch(struct apply_state *state,\n \t\tfragment = xcalloc(1, sizeof(*fragment));\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\tif (len <= 0) {\n+\t\t\tfree(fragment);\n+\t\t\treturn error(_(\"corrupt patch at line %d\"), state->linenr);\n+\t\t}\n \t\tfragment->patch = line;\n \t\tfragment->size = len;\n \t\toldlines += fragment->oldlines;\n@@ -1839,9 +1845,9 @@ static int parse_single_patch(struct apply_state *state,\n \t\tpatch->is_delete = 0;\n \n \tif (0 < patch->is_new && oldlines)\n-\t\tdie(_(\"new file %s depends on old contents\"), patch->new_name);\n+\t\treturn error(_(\"new file %s depends on old contents\"), patch->new_name);\n \tif (0 < patch->is_delete && newlines)\n-\t\tdie(_(\"deleted file %s still has contents\"), patch->old_name);\n+\t\treturn error(_(\"deleted file %s still has contents\"), patch->old_name);\n \tif (!patch->is_delete && !newlines && context)\n \t\tfprintf_ln(stderr,\n \t\t\t   _(\"** warning: \"\n@@ -2143,6 +2149,9 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si\n \t\t\t\t       size - offset - hdrsize,\n \t\t\t\t       patch);\n \n+\tif (patchsize < 0)\n+\t\treturn -1;\n+\n \tif (!patchsize) {\n \t\tstatic const char git_binary[] = \"GIT binary patch\\n\";\n \t\tint hd = hdrsize + offset;\ndiff --git a/t/t4012-diff-binary.sh b/t/t4012-diff-binary.sh\nindex 643d729..0a8af76 100755\n--- a/t/t4012-diff-binary.sh\n+++ b/t/t4012-diff-binary.sh\n@@ -68,7 +68,7 @@ test_expect_success C_LOCALE_OUTPUT 'apply detecting corrupt patch correctly' '\n \tsed -e \"s/-CIT/xCIT/\" <output >broken &&\n \ttest_must_fail git apply --stat --summary broken 2>detected &&\n \tdetected=$(cat detected) &&\n-\tdetected=$(expr \"$detected\" : \"fatal.*at line \\\\([0-9]*\\\\)\\$\") &&\n+\tdetected=$(expr \"$detected\" : \"error.*at line \\\\([0-9]*\\\\)\\$\") &&\n \tdetected=$(sed -ne \"${detected}p\" broken) &&\n \ttest \"$detected\" = xCIT\n '\n@@ -77,7 +77,7 @@ test_expect_success C_LOCALE_OUTPUT 'apply detecting corrupt patch correctly' '\n \tgit diff --binary | sed -e \"s/-CIT/xCIT/\" >broken &&\n \ttest_must_fail git apply --stat --summary broken 2>detected &&\n \tdetected=$(cat detected) &&\n-\tdetected=$(expr \"$detected\" : \"fatal.*at line \\\\([0-9]*\\\\)\\$\") &&\n+\tdetected=$(expr \"$detected\" : \"error.*at line \\\\([0-9]*\\\\)\\$\") &&\n \tdetected=$(sed -ne \"${detected}p\" broken) &&\n \ttest \"$detected\" = xCIT\n '\n-- \n2.8.1.300.g5fed0c0\n"},{"id":"284295","messageId":"1461504863-15946-57-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 56/83] apply: move 'struct apply_state' to apply.h","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:56Z","receivedAt":"2016-04-24T13:33:56Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n apply.h         | 124 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n builtin/apply.c | 121 +-----------------------------------------------------\n 2 files changed, 125 insertions(+), 120 deletions(-)\n create mode 100644 apply.h\n\ndiff --git a/apply.h b/apply.h\nnew file mode 100644\nindex 0000000..98aafc6\n--- /dev/null\n+++ b/apply.h\n@@ -0,0 +1,124 @@\n+#ifndef APPLY_H\n+#define APPLY_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+\n+enum ws_ignore {\n+\tignore_ws_none,\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+\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+\tint apply;\n+\tint allow_overlap;\n+\tint apply_in_reverse;\n+\tint apply_with_reject;\n+\tint apply_verbosely;\n+\n+\t/* --cached updates only the cache without ever touching the working tree. */\n+\tint cached;\n+\n+\t/* --stat does just a diffstat, and doesn't actually apply */\n+\tint diffstat;\n+\n+\t/* --numstat does numeric diffstat, and doesn't actually apply */\n+\tint numstat;\n+\n+\tconst char *fake_ancestor;\n+\n+\tint summary;\n+\n+\tint threeway;\n+\n+\tint no_add;\n+\n+\t/*\n+\t *  --check turns on checking that the working tree matches the\n+\t *    files that are being modified, but doesn't apply the patch\n+\t */\n+\tint check;\n+\n+\t/* --index updates the cache as well. */\n+\tint check_index;\n+\n+\tint unidiff_zero;\n+\n+\tint update_index;\n+\n+\tint unsafe_paths;\n+\n+\tint line_termination;\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/*\n+\t * Various \"current state\", notably line numbers and what\n+\t * file (and how) we're patching right now.. The \"is_xxxx\"\n+\t * things are flags, where -1 means \"don't know yet\".\n+\t */\n+\tint linenr;\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+\tstruct string_list symlink_changes;\n+\n+\tint p_value;\n+\tint p_value_known;\n+\tunsigned int p_context;\n+\n+\tconst char *patch_input_file;\n+\n+\tstruct string_list limit_by_name;\n+\tint has_include;\n+\n+\tstruct strbuf root;\n+\n+\tconst char *whitespace_option;\n+\tint whitespace_error;\n+\tint squelch_whitespace_errors;\n+\tint applied_after_fixing_ws;\n+\n+\tenum ws_error_action ws_error_action;\n+\tenum ws_ignore ws_ignore_action;\n+};\n+\n+#endif\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex de5c745..1c1ac7d 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -20,126 +20,7 @@\n #include \"xdiff-interface.h\"\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-\n-enum ws_ignore {\n-\tignore_ws_none,\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-\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-\tint apply;\n-\tint allow_overlap;\n-\tint apply_in_reverse;\n-\tint apply_with_reject;\n-\tint apply_verbosely;\n-\n-\t/* --cached updates only the cache without ever touching the working tree. */\n-\tint cached;\n-\n-\t/* --stat does just a diffstat, and doesn't actually apply */\n-\tint diffstat;\n-\n-\t/* --numstat does numeric diffstat, and doesn't actually apply */\n-\tint numstat;\n-\n-\tconst char *fake_ancestor;\n-\n-\tint summary;\n-\n-\tint threeway;\n-\n-\tint no_add;\n-\n-\t/*\n-\t *  --check turns on checking that the working tree matches the\n-\t *    files that are being modified, but doesn't apply the patch\n-\t */\n-\tint check;\n-\n-\t/* --index updates the cache as well. */\n-\tint check_index;\n-\n-\tint unidiff_zero;\n-\n-\tint update_index;\n-\n-\tint unsafe_paths;\n-\n-\tint line_termination;\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/*\n-\t * Various \"current state\", notably line numbers and what\n-\t * file (and how) we're patching right now.. The \"is_xxxx\"\n-\t * things are flags, where -1 means \"don't know yet\".\n-\t */\n-\tint linenr;\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-\tstruct string_list symlink_changes;\n-\n-\tint p_value;\n-\tint p_value_known;\n-\tunsigned int p_context;\n-\n-\tconst char *patch_input_file;\n-\n-\tstruct string_list limit_by_name;\n-\tint has_include;\n-\n-\tstruct strbuf root;\n-\n-\tconst char *whitespace_option;\n-\tint whitespace_error;\n-\tint squelch_whitespace_errors;\n-\tint applied_after_fixing_ws;\n-\n-\tenum ws_error_action ws_error_action;\n-\tenum ws_ignore ws_ignore_action;\n-};\n+#include \"apply.h\"\n \n static const char * const apply_usage[] = {\n \tN_(\"git apply [<options>] [<patch>...]\"),\n-- \n2.8.1.300.g5fed0c0\n"},{"id":"284294","messageId":"1461504863-15946-58-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 57/83] builtin/apply: libify parse_whitespace_option()","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:57Z","receivedAt":"2016-04-24T13:33:57Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 23 ++++++++++++-----------\n 1 file changed, 12 insertions(+), 11 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 1c1ac7d..8d96f70 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -27,34 +27,34 @@ static const char * const apply_usage[] = {\n \tNULL\n };\n \n-static void parse_whitespace_option(struct apply_state *state, const char *option)\n+static int parse_whitespace_option(struct apply_state *state, const char *option)\n {\n \tif (!option) {\n \t\tstate->ws_error_action = warn_on_ws_error;\n-\t\treturn;\n+\t\treturn 0;\n \t}\n \tif (!strcmp(option, \"warn\")) {\n \t\tstate->ws_error_action = warn_on_ws_error;\n-\t\treturn;\n+\t\treturn 0;\n \t}\n \tif (!strcmp(option, \"nowarn\")) {\n \t\tstate->ws_error_action = nowarn_ws_error;\n-\t\treturn;\n+\t\treturn 0;\n \t}\n \tif (!strcmp(option, \"error\")) {\n \t\tstate->ws_error_action = die_on_ws_error;\n-\t\treturn;\n+\t\treturn 0;\n \t}\n \tif (!strcmp(option, \"error-all\")) {\n \t\tstate->ws_error_action = die_on_ws_error;\n \t\tstate->squelch_whitespace_errors = 0;\n-\t\treturn;\n+\t\treturn 0;\n \t}\n \tif (!strcmp(option, \"strip\") || !strcmp(option, \"fix\")) {\n \t\tstate->ws_error_action = correct_ws_error;\n-\t\treturn;\n+\t\treturn 0;\n \t}\n-\tdie(_(\"unrecognized whitespace option '%s'\"), option);\n+\treturn error(_(\"unrecognized whitespace option '%s'\"), option);\n }\n \n static void parse_ignorewhitespace_option(struct apply_state *state,\n@@ -4572,7 +4572,8 @@ static int option_parse_whitespace(const struct option *opt,\n {\n \tstruct apply_state *state = opt->value;\n \tstate->whitespace_option = arg;\n-\tparse_whitespace_option(state, arg);\n+\tif (parse_whitespace_option(state, arg))\n+\t\texit(1);\n \treturn 0;\n }\n \n@@ -4602,8 +4603,8 @@ static void init_apply_state(struct apply_state *state, const char *prefix)\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_whitespace && parse_whitespace_option(state, apply_default_whitespace))\n+\t\texit(1);\n \tif (apply_default_ignorewhitespace)\n \t\tparse_ignorewhitespace_option(state, apply_default_ignorewhitespace);\n }\n-- \n2.8.1.300.g5fed0c0\n"},{"id":"284278","messageId":"1461504863-15946-59-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 58/83] builtin/apply: libify parse_ignorewhitespace_option()","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:58Z","receivedAt":"2016-04-24T13:33:58Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-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 8d96f70..2f89922 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -57,20 +57,20 @@ static int parse_whitespace_option(struct apply_state *state, const char *option\n \treturn error(_(\"unrecognized whitespace option '%s'\"), option);\n }\n \n-static void parse_ignorewhitespace_option(struct apply_state *state,\n-\t\t\t\t\t  const char *option)\n+static int 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\tstate->ws_ignore_action = ignore_ws_none;\n-\t\treturn;\n+\t\treturn 0;\n \t}\n \tif (!strcmp(option, \"change\")) {\n \t\tstate->ws_ignore_action = ignore_ws_change;\n-\t\treturn;\n+\t\treturn 0;\n \t}\n-\tdie(_(\"unrecognized whitespace ignore option '%s'\"), option);\n+\treturn error(_(\"unrecognized whitespace ignore option '%s'\"), option);\n }\n \n static void set_default_whitespace_mode(struct apply_state *state)\n@@ -4605,8 +4605,8 @@ static void init_apply_state(struct apply_state *state, const char *prefix)\n \tgit_apply_config();\n \tif (apply_default_whitespace && parse_whitespace_option(state, apply_default_whitespace))\n \t\texit(1);\n-\tif (apply_default_ignorewhitespace)\n-\t\tparse_ignorewhitespace_option(state, apply_default_ignorewhitespace);\n+\tif (apply_default_ignorewhitespace && parse_ignorewhitespace_option(state, apply_default_ignorewhitespace))\n+\t\texit(1);\n }\n \n static void check_apply_state(struct apply_state *state, int force_apply)\n-- \n2.8.1.300.g5fed0c0\n"},{"id":"284276","messageId":"1461504863-15946-60-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 59/83] builtin/apply: move init_apply_state() to apply.c","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:33:59Z","receivedAt":"2016-04-24T13:33:59Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n Makefile        |  1 +\n apply.c         | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n apply.h         |  8 ++++++\n builtin/apply.c | 75 -----------------------------------------------------\n 4 files changed, 89 insertions(+), 75 deletions(-)\n create mode 100644 apply.c\n\ndiff --git a/Makefile b/Makefile\nindex a83e322..a8616fc 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -684,6 +684,7 @@ LIB_OBJS += abspath.o\n LIB_OBJS += advice.o\n LIB_OBJS += alias.o\n LIB_OBJS += alloc.o\n+LIB_OBJS += apply.o\n LIB_OBJS += archive.o\n LIB_OBJS += archive-tar.o\n LIB_OBJS += archive-zip.o\ndiff --git a/apply.c b/apply.c\nnew file mode 100644\nindex 0000000..9c5f258\n--- /dev/null\n+++ b/apply.c\n@@ -0,0 +1,80 @@\n+#include \"cache.h\"\n+#include \"apply.h\"\n+\n+\n+\n+static void git_apply_config(void)\n+{\n+\tgit_config_get_string_const(\"apply.whitespace\", &apply_default_whitespace);\n+\tgit_config_get_string_const(\"apply.ignorewhitespace\", &apply_default_ignorewhitespace);\n+\tgit_config(git_default_config, NULL);\n+}\n+\n+int parse_whitespace_option(struct apply_state *state, const char *option)\n+{\n+\tif (!option) {\n+\t\tstate->ws_error_action = warn_on_ws_error;\n+\t\treturn 0;\n+\t}\n+\tif (!strcmp(option, \"warn\")) {\n+\t\tstate->ws_error_action = warn_on_ws_error;\n+\t\treturn 0;\n+\t}\n+\tif (!strcmp(option, \"nowarn\")) {\n+\t\tstate->ws_error_action = nowarn_ws_error;\n+\t\treturn 0;\n+\t}\n+\tif (!strcmp(option, \"error\")) {\n+\t\tstate->ws_error_action = die_on_ws_error;\n+\t\treturn 0;\n+\t}\n+\tif (!strcmp(option, \"error-all\")) {\n+\t\tstate->ws_error_action = die_on_ws_error;\n+\t\tstate->squelch_whitespace_errors = 0;\n+\t\treturn 0;\n+\t}\n+\tif (!strcmp(option, \"strip\") || !strcmp(option, \"fix\")) {\n+\t\tstate->ws_error_action = correct_ws_error;\n+\t\treturn 0;\n+\t}\n+\treturn error(_(\"unrecognized whitespace option '%s'\"), option);\n+}\n+\n+int parse_ignorewhitespace_option(struct apply_state *state,\n+\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\tstate->ws_ignore_action = ignore_ws_none;\n+\t\treturn 0;\n+\t}\n+\tif (!strcmp(option, \"change\")) {\n+\t\tstate->ws_ignore_action = ignore_ws_change;\n+\t\treturn 0;\n+\t}\n+\treturn error(_(\"unrecognized whitespace ignore option '%s'\"), option);\n+}\n+\n+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+\tstate->apply = 1;\n+\tstate->line_termination = '\\n';\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+\tstate->ws_ignore_action = ignore_ws_none;\n+\tstate->linenr = 1;\n+\tstrbuf_init(&state->root, 0);\n+\n+\tgit_apply_config();\n+\tif (apply_default_whitespace && parse_whitespace_option(state, apply_default_whitespace))\n+\t\texit(1);\n+\tif (apply_default_ignorewhitespace && parse_ignorewhitespace_option(state, apply_default_ignorewhitespace))\n+\t\texit(1);\n+}\n+\ndiff --git a/apply.h b/apply.h\nindex 98aafc6..70ab658 100644\n--- a/apply.h\n+++ b/apply.h\n@@ -121,4 +121,12 @@ struct apply_state {\n \tenum ws_ignore ws_ignore_action;\n };\n \n+extern int parse_whitespace_option(struct apply_state *state,\n+\t\t\t\t   const char *option);\n+extern int parse_ignorewhitespace_option(struct apply_state *state,\n+\t\t\t\t\t const char *option);\n+\n+extern void init_apply_state(struct apply_state *state, const char *prefix);\n+\n+\n #endif\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 2f89922..1d958fa 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -27,52 +27,6 @@ static const char * const apply_usage[] = {\n \tNULL\n };\n \n-static int parse_whitespace_option(struct apply_state *state, const char *option)\n-{\n-\tif (!option) {\n-\t\tstate->ws_error_action = warn_on_ws_error;\n-\t\treturn 0;\n-\t}\n-\tif (!strcmp(option, \"warn\")) {\n-\t\tstate->ws_error_action = warn_on_ws_error;\n-\t\treturn 0;\n-\t}\n-\tif (!strcmp(option, \"nowarn\")) {\n-\t\tstate->ws_error_action = nowarn_ws_error;\n-\t\treturn 0;\n-\t}\n-\tif (!strcmp(option, \"error\")) {\n-\t\tstate->ws_error_action = die_on_ws_error;\n-\t\treturn 0;\n-\t}\n-\tif (!strcmp(option, \"error-all\")) {\n-\t\tstate->ws_error_action = die_on_ws_error;\n-\t\tstate->squelch_whitespace_errors = 0;\n-\t\treturn 0;\n-\t}\n-\tif (!strcmp(option, \"strip\") || !strcmp(option, \"fix\")) {\n-\t\tstate->ws_error_action = correct_ws_error;\n-\t\treturn 0;\n-\t}\n-\treturn error(_(\"unrecognized whitespace option '%s'\"), option);\n-}\n-\n-static int 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\tstate->ws_ignore_action = ignore_ws_none;\n-\t\treturn 0;\n-\t}\n-\tif (!strcmp(option, \"change\")) {\n-\t\tstate->ws_ignore_action = ignore_ws_change;\n-\t\treturn 0;\n-\t}\n-\treturn error(_(\"unrecognized whitespace ignore option '%s'\"), option);\n-}\n-\n static void set_default_whitespace_mode(struct apply_state *state)\n {\n \tif (!state->whitespace_option && !apply_default_whitespace)\n@@ -4522,13 +4476,6 @@ static int apply_patch(struct apply_state *state,\n \treturn 0;\n }\n \n-static void git_apply_config(void)\n-{\n-\tgit_config_get_string_const(\"apply.whitespace\", &apply_default_whitespace);\n-\tgit_config_get_string_const(\"apply.ignorewhitespace\", &apply_default_ignorewhitespace);\n-\tgit_config(git_default_config, NULL);\n-}\n-\n static int option_parse_exclude(const struct option *opt,\n \t\t\t\tconst char *arg, int unset)\n {\n@@ -4587,28 +4534,6 @@ 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-{\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_value = 1;\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-\tstate->linenr = 1;\n-\tstrbuf_init(&state->root, 0);\n-\n-\tgit_apply_config();\n-\tif (apply_default_whitespace && parse_whitespace_option(state, apply_default_whitespace))\n-\t\texit(1);\n-\tif (apply_default_ignorewhitespace && parse_ignorewhitespace_option(state, apply_default_ignorewhitespace))\n-\t\texit(1);\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-- \n2.8.1.300.g5fed0c0\n"},{"id":"284273","messageId":"1461504863-15946-61-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 60/83] apply: libify init_apply_state()","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:34:00Z","receivedAt":"2016-04-24T13:34:00Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n apply.c         | 7 ++++---\n apply.h         | 2 +-\n builtin/apply.c | 3 ++-\n 3 files changed, 7 insertions(+), 5 deletions(-)\n\ndiff --git a/apply.c b/apply.c\nindex 9c5f258..11bec48 100644\n--- a/apply.c\n+++ b/apply.c\n@@ -56,7 +56,7 @@ int parse_ignorewhitespace_option(struct apply_state *state,\n \treturn error(_(\"unrecognized whitespace ignore option '%s'\"), option);\n }\n \n-void init_apply_state(struct apply_state *state, const char *prefix)\n+int init_apply_state(struct apply_state *state, const char *prefix)\n {\n \tmemset(state, 0, sizeof(*state));\n \tstate->prefix = prefix;\n@@ -73,8 +73,9 @@ void init_apply_state(struct apply_state *state, const char *prefix)\n \n \tgit_apply_config();\n \tif (apply_default_whitespace && parse_whitespace_option(state, apply_default_whitespace))\n-\t\texit(1);\n+\t\treturn -1;\n \tif (apply_default_ignorewhitespace && parse_ignorewhitespace_option(state, apply_default_ignorewhitespace))\n-\t\texit(1);\n+\t\treturn -1;\n+\treturn 0;\n }\n \ndiff --git a/apply.h b/apply.h\nindex 70ab658..021e9e3 100644\n--- a/apply.h\n+++ b/apply.h\n@@ -126,7 +126,7 @@ extern int parse_whitespace_option(struct apply_state *state,\n extern int parse_ignorewhitespace_option(struct apply_state *state,\n \t\t\t\t\t const char *option);\n \n-extern void init_apply_state(struct apply_state *state, const char *prefix);\n+extern int init_apply_state(struct apply_state *state, const char *prefix);\n \n \n #endif\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 1d958fa..e3ee199 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -4717,7 +4717,8 @@ 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+\tif (init_apply_state(&state, prefix))\n+\t\texit(1);\n \n \targc = parse_options(argc, argv, state.prefix, builtin_apply_options,\n \t\t\tapply_usage, 0);\n-- \n2.8.1.300.g5fed0c0\n"},{"id":"284277","messageId":"1461504863-15946-62-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 61/83] builtin/apply: libify check_apply_state()","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:34:01Z","receivedAt":"2016-04-24T13:34:01Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 16 +++++++++-------\n 1 file changed, 9 insertions(+), 7 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex e3ee199..7576ec5 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -4534,17 +4534,17 @@ static int option_parse_directory(const struct option *opt,\n \treturn 0;\n }\n \n-static void check_apply_state(struct apply_state *state, int force_apply)\n+static int 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+\t\treturn error(\"--reject and --3way cannot be used together.\");\n \tif (state->cached && state->threeway)\n-\t\tdie(\"--cached and --3way cannot be used together.\");\n+\t\treturn error(\"--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\t\treturn error(_(\"--3way outside a repository\"));\n \t\tstate->check_index = 1;\n \t}\n \tif (state->apply_with_reject)\n@@ -4552,14 +4552,15 @@ static void check_apply_state(struct apply_state *state, int force_apply)\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+\t\treturn error(_(\"--index outside a repository\"));\n \tif (state->cached) {\n \t\tif (is_not_gitdir)\n-\t\t\tdie(_(\"--cached outside a repository\"));\n+\t\t\treturn error(_(\"--cached outside a repository\"));\n \t\tstate->check_index = 1;\n \t}\n \tif (state->check_index)\n \t\tstate->unsafe_paths = 0;\n+\treturn 0;\n }\n \n static int apply_all_patches(struct apply_state *state,\n@@ -4723,7 +4724,8 @@ 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-\tcheck_apply_state(&state, force_apply);\n+\tif (check_apply_state(&state, force_apply))\n+\t\texit(1);\n \n \treturn apply_all_patches(&state, argc, argv, options);\n }\n-- \n2.8.1.300.g5fed0c0\n"},{"id":"284279","messageId":"1461504863-15946-63-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 62/83] builtin/apply: move check_apply_state() to apply.c","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:34:02Z","receivedAt":"2016-04-24T13:34:02Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n apply.c         | 29 +++++++++++++++++++++++++++++\n apply.h         |  2 +-\n builtin/apply.c | 29 -----------------------------\n 3 files changed, 30 insertions(+), 30 deletions(-)\n\ndiff --git a/apply.c b/apply.c\nindex 11bec48..b29cc64 100644\n--- a/apply.c\n+++ b/apply.c\n@@ -79,3 +79,32 @@ int init_apply_state(struct apply_state *state, const char *prefix)\n \treturn 0;\n }\n \n+int 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\treturn error(\"--reject and --3way cannot be used together.\");\n+\tif (state->cached && state->threeway)\n+\t\treturn error(\"--cached and --3way cannot be used together.\");\n+\tif (state->threeway) {\n+\t\tif (is_not_gitdir)\n+\t\t\treturn error(_(\"--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\treturn error(_(\"--index outside a repository\"));\n+\tif (state->cached) {\n+\t\tif (is_not_gitdir)\n+\t\t\treturn error(_(\"--cached outside a repository\"));\n+\t\tstate->check_index = 1;\n+\t}\n+\tif (state->check_index)\n+\t\tstate->unsafe_paths = 0;\n+\treturn 0;\n+}\n+\ndiff --git a/apply.h b/apply.h\nindex 021e9e3..35d4d15 100644\n--- a/apply.h\n+++ b/apply.h\n@@ -127,6 +127,6 @@ extern int parse_ignorewhitespace_option(struct apply_state *state,\n \t\t\t\t\t const char *option);\n \n extern int init_apply_state(struct apply_state *state, const char *prefix);\n-\n+extern int check_apply_state(struct apply_state *state, int force_apply);\n \n #endif\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 7576ec5..eab5ae1 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -4534,35 +4534,6 @@ static int option_parse_directory(const struct option *opt,\n \treturn 0;\n }\n \n-static int 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\treturn error(\"--reject and --3way cannot be used together.\");\n-\tif (state->cached && state->threeway)\n-\t\treturn error(\"--cached and --3way cannot be used together.\");\n-\tif (state->threeway) {\n-\t\tif (is_not_gitdir)\n-\t\t\treturn error(_(\"--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\treturn error(_(\"--index outside a repository\"));\n-\tif (state->cached) {\n-\t\tif (is_not_gitdir)\n-\t\t\treturn error(_(\"--cached outside a repository\"));\n-\t\tstate->check_index = 1;\n-\t}\n-\tif (state->check_index)\n-\t\tstate->unsafe_paths = 0;\n-\treturn 0;\n-}\n-\n static int apply_all_patches(struct apply_state *state,\n \t\t\t     int argc,\n \t\t\t     const char **argv,\n-- \n2.8.1.300.g5fed0c0\n"},{"id":"284291","messageId":"1461504863-15946-64-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 63/83] builtin/apply: make apply_all_patches() return -1 on error","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:34:03Z","receivedAt":"2016-04-24T13:34:03Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 23 +++++++++++++----------\n 1 file changed, 13 insertions(+), 10 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex eab5ae1..73247c7 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -4551,7 +4551,7 @@ static int apply_all_patches(struct apply_state *state,\n \t\tif (!strcmp(arg, \"-\")) {\n \t\t\tres = apply_patch(state, 0, \"<stdin>\", options);\n \t\t\tif (res < 0)\n-\t\t\t\texit(1);\n+\t\t\t\treturn -1;\n \t\t\terrs |= res;\n \t\t\tread_stdin = 0;\n \t\t\tcontinue;\n@@ -4562,12 +4562,12 @@ static int apply_all_patches(struct apply_state *state,\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\t\treturn error(_(\"can't open patch '%s': %s\"), arg, strerror(errno));\n \t\tread_stdin = 0;\n \t\tset_default_whitespace_mode(state);\n \t\tres = apply_patch(state, fd, arg, options);\n \t\tif (res < 0)\n-\t\t\texit(1);\n+\t\t\treturn -1;\n \t\terrs |= res;\n \t\tclose(fd);\n \t}\n@@ -4575,7 +4575,7 @@ static int apply_all_patches(struct apply_state *state,\n \tif (read_stdin) {\n \t\tres = apply_patch(state, 0, \"<stdin>\", options);\n \t\tif (res < 0)\n-\t\t\texit(1);\n+\t\t\treturn -1;\n \t\terrs |= res;\n \t}\n \n@@ -4590,10 +4590,10 @@ static int apply_all_patches(struct apply_state *state,\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\t\treturn error(Q_(\"%d line adds whitespace errors.\",\n+\t\t\t\t\t\"%d lines add whitespace errors.\",\n+\t\t\t\t\tstate->whitespace_error),\n+\t\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@@ -4608,7 +4608,7 @@ static int apply_all_patches(struct apply_state *state,\n \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\t\treturn error(_(\"Unable to write new index file\"));\n \t}\n \n \treturn !!errs;\n@@ -4698,5 +4698,8 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \tif (check_apply_state(&state, force_apply))\n \t\texit(1);\n \n-\treturn apply_all_patches(&state, argc, argv, options);\n+\tif (apply_all_patches(&state, argc, argv, options))\n+\t\texit(1);\n+\n+\treturn 0;\n }\n-- \n2.8.1.300.g5fed0c0\n"},{"id":"284274","messageId":"1461504863-15946-65-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 64/83] builtin/apply: make parse_traditional_patch() return -1 on error","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:34:04Z","receivedAt":"2016-04-24T13:34:04Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 15 +++++++++------\n 1 file changed, 9 insertions(+), 6 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 73247c7..6b8ba2a 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -755,10 +755,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(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+static int 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@@ -803,7 +803,9 @@ 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\treturn error(_(\"unable to find filename in patch at line %d\"), state->linenr);\n+\n+\treturn 0;\n }\n \n static int gitdiff_hdrend(struct apply_state *state,\n@@ -1462,7 +1464,8 @@ static int find_header(struct apply_state *state,\n \t\t\tcontinue;\n \n \t\t/* Ok, we'll consider it a patch */\n-\t\tparse_traditional_patch(state, line, line+len, patch);\n+\t\tif (parse_traditional_patch(state, line, line+len, patch))\n+\t\t\treturn -1;\n \t\t*hdrsize = len + nextlen;\n \t\tstate->linenr += 2;\n \t\treturn offset;\n-- \n2.8.1.300.g5fed0c0\n"},{"id":"284293","messageId":"1461504863-15946-66-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 65/83] builtin/apply: make gitdiff_verify_name() return -1 on error","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:34:05Z","receivedAt":"2016-04-24T13:34:05Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 52 ++++++++++++++++++++++++++++++----------------------\n 1 file changed, 30 insertions(+), 22 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 6b8ba2a..268356b 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -812,7 +812,7 @@ 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+\treturn 1;\n }\n \n /*\n@@ -827,54 +827,56 @@ static int gitdiff_hdrend(struct apply_state *state,\n #define DIFF_OLD_NAME 0\n #define DIFF_NEW_NAME 1\n \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+static int gitdiff_verify_name(struct apply_state *state,\n+\t\t\t       const char *line,\n+\t\t\t       int isnull,\n+\t\t\t       char **name,\n+\t\t\t       int side)\n {\n \tif (!*name && !isnull) {\n \t\t*name = find_name(state, line, NULL, state->p_value, TERM_TAB);\n-\t\treturn;\n+\t\treturn 0;\n \t}\n \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    *name, state->linenr);\n+\t\t\treturn error(_(\"git apply: bad git-diff - expected /dev/null, got %s on line %d\"),\n+\t\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\tif (!another || memcmp(another, *name, len + 1)) {\n+\t\t\tfree(another);\n+\t\t\treturn error((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}\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\treturn error(_(\"git apply: bad git-diff - expected /dev/null on line %d\"), state->linenr);\n \t}\n+\n+\treturn 0;\n }\n \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(state, line,\n-\t\t\t    patch->is_new, &patch->old_name,\n-\t\t\t    DIFF_OLD_NAME);\n-\treturn 0;\n+\treturn gitdiff_verify_name(state, line,\n+\t\t\t\t   patch->is_new, &patch->old_name,\n+\t\t\t\t   DIFF_OLD_NAME);\n }\n \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(state, line,\n-\t\t\t    patch->is_delete, &patch->new_name,\n-\t\t\t    DIFF_NEW_NAME);\n-\treturn 0;\n+\treturn gitdiff_verify_name(state, line,\n+\t\t\t\t   patch->is_delete, &patch->new_name,\n+\t\t\t\t   DIFF_NEW_NAME);\n }\n \n static int gitdiff_oldmode(struct apply_state *state,\n@@ -1016,7 +1018,7 @@ 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+\treturn 1;\n }\n \n /*\n@@ -1248,9 +1250,13 @@ static int parse_git_header(struct apply_state *state,\n \t\tfor (i = 0; i < ARRAY_SIZE(optable); i++) {\n \t\t\tconst struct opentry *p = optable + i;\n \t\t\tint oplen = strlen(p->str);\n+\t\t\tint res;\n \t\t\tif (len < oplen || memcmp(p->str, line, oplen))\n \t\t\t\tcontinue;\n-\t\t\tif (p->fn(state, line + oplen, patch) < 0)\n+\t\t\tres = p->fn(state, line + oplen, patch);\n+\t\t\tif (res < 0)\n+\t\t\t\treturn -1;\n+\t\t\tif (res > 0)\n \t\t\t\treturn offset;\n \t\t\tbreak;\n \t\t}\n@@ -1429,6 +1435,8 @@ static int find_header(struct apply_state *state,\n \t\t */\n \t\tif (!memcmp(\"diff --git \", line, 11)) {\n \t\t\tint git_hdr_len = parse_git_header(state, line, len, size, patch);\n+\t\t\tif (git_hdr_len < 0)\n+\t\t\t\treturn -1;\n \t\t\tif (git_hdr_len <= len)\n \t\t\t\tcontinue;\n \t\t\tif (!patch->old_name && !patch->new_name) {\n-- \n2.8.1.300.g5fed0c0\n"},{"id":"284292","messageId":"1461504863-15946-67-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 66/83] builtin/apply: change die_on_unsafe_path() to check_unsafe_path()","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:34:06Z","receivedAt":"2016-04-24T13:34:06Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-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 268356b..8b7a8e0 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -3699,7 +3699,7 @@ static int path_is_beyond_symlink(struct apply_state *state, const char *name_)\n \treturn ret;\n }\n \n-static void die_on_unsafe_path(struct patch *patch)\n+static int check_unsafe_path(struct patch *patch)\n {\n \tconst char *old_name = NULL;\n \tconst char *new_name = NULL;\n@@ -3711,9 +3711,10 @@ static void die_on_unsafe_path(struct patch *patch)\n \t\tnew_name = patch->new_name;\n \n \tif (old_name && !verify_path(old_name))\n-\t\tdie(_(\"invalid path '%s'\"), old_name);\n+\t\treturn error(_(\"invalid path '%s'\"), old_name);\n \tif (new_name && !verify_path(new_name))\n-\t\tdie(_(\"invalid path '%s'\"), new_name);\n+\t\treturn error(_(\"invalid path '%s'\"), new_name);\n+\treturn 0;\n }\n \n /*\n@@ -3803,8 +3804,8 @@ static int check_patch(struct apply_state *state, struct patch *patch)\n \t\t}\n \t}\n \n-\tif (!state->unsafe_paths)\n-\t\tdie_on_unsafe_path(patch);\n+\tif (!state->unsafe_paths && check_unsafe_path(patch))\n+\t\treturn -1;\n \n \t/*\n \t * An attempt to read from or delete a path that is beyond a\n-- \n2.8.1.300.g5fed0c0\n"},{"id":"284282","messageId":"1461504863-15946-68-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 67/83] builtin/apply: make build_fake_ancestor() return -1 on error","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:34:07Z","receivedAt":"2016-04-24T13:34:07Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 28 ++++++++++++++++------------\n 1 file changed, 16 insertions(+), 12 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 8b7a8e0..69bb94c 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -3891,7 +3891,7 @@ static int preimage_sha1_in_gitlink_patch(struct patch *p, unsigned char sha1[20\n }\n \n /* Build an index that contains the just the files needed for a 3way merge */\n-static void build_fake_ancestor(struct patch *list, const char *filename)\n+static int build_fake_ancestor(struct patch *list, const char *filename)\n {\n \tstruct patch *patch;\n \tstruct index_state result = { NULL };\n@@ -3913,31 +3913,34 @@ static void build_fake_ancestor(struct patch *list, const char *filename)\n \t\t\tif (!preimage_sha1_in_gitlink_patch(patch, sha1))\n \t\t\t\t; /* ok, the textual part looks sane */\n \t\t\telse\n-\t\t\t\tdie(\"sha1 information is lacking or useless for submodule %s\",\n-\t\t\t\t    name);\n+\t\t\t\treturn error(\"sha1 information is lacking or \"\n+\t\t\t\t\t     \"useless for submodule %s\", name);\n \t\t} else if (!get_sha1_blob(patch->old_sha1_prefix, sha1)) {\n \t\t\t; /* ok */\n \t\t} else if (!patch->lines_added && !patch->lines_deleted) {\n \t\t\t/* mode-only change: update the current */\n \t\t\tif (get_current_sha1(patch->old_name, sha1))\n-\t\t\t\tdie(\"mode change for %s, which is not \"\n-\t\t\t\t    \"in current HEAD\", name);\n+\t\t\t\treturn error(\"mode change for %s, which is not \"\n+\t\t\t\t\t     \"in current HEAD\", name);\n \t\t} else\n-\t\t\tdie(\"sha1 information is lacking or useless \"\n-\t\t\t    \"(%s).\", name);\n+\t\t\treturn error(\"sha1 information is lacking or useless \"\n+\t\t\t\t     \"(%s).\", name);\n \n \t\tce = make_cache_entry(patch->old_mode, sha1, name, 0, 0);\n \t\tif (!ce)\n-\t\t\tdie(_(\"make_cache_entry failed for path '%s'\"), name);\n+\t\t\treturn error(_(\"make_cache_entry failed for path '%s'\"),\n+\t\t\t\t     name);\n \t\tif (add_index_entry(&result, ce, ADD_CACHE_OK_TO_ADD))\n-\t\t\tdie (\"Could not add %s to temporary index\", name);\n+\t\t\treturn error(\"Could not add %s to temporary index\",\n+\t\t\t\t     name);\n \t}\n \n \thold_lock_file_for_update(&lock, filename, LOCK_DIE_ON_ERROR);\n \tif (write_locked_index(&result, &lock, COMMIT_LOCK))\n-\t\tdie (\"Could not write temporary index to %s\", filename);\n+\t\treturn error(\"Could not write temporary index to %s\", filename);\n \n \tdiscard_index(&result);\n+\treturn 0;\n }\n \n static void stat_patch_list(struct apply_state *state, struct patch *patch)\n@@ -4470,8 +4473,9 @@ static int apply_patch(struct apply_state *state,\n \t\treturn 1;\n \t}\n \n-\tif (state->fake_ancestor)\n-\t\tbuild_fake_ancestor(list, state->fake_ancestor);\n+\tif (state->fake_ancestor &&\n+\t    build_fake_ancestor(list, state->fake_ancestor))\n+\t\treturn -1;\n \n \tif (state->diffstat)\n \t\tstat_patch_list(state, list);\n-- \n2.8.1.300.g5fed0c0\n"},{"id":"284281","messageId":"1461504863-15946-69-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 68/83] builtin/apply: make remove_file() return -1 on error","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:34:08Z","receivedAt":"2016-04-24T13:34:08Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 17 +++++++++++------\n 1 file changed, 11 insertions(+), 6 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 69bb94c..89118c1 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -4071,17 +4071,18 @@ static void patch_stats(struct apply_state *state, struct patch *patch)\n \t}\n }\n \n-static void remove_file(struct apply_state *state, struct patch *patch, int rmdir_empty)\n+static int remove_file(struct apply_state *state, struct patch *patch, int rmdir_empty)\n {\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\t\treturn error(_(\"unable to remove %s from index\"), patch->old_name);\n \t}\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 \t}\n+\treturn 0;\n }\n \n static void add_index_file(struct apply_state *state,\n@@ -4260,8 +4261,10 @@ static void write_out_one_result(struct apply_state *state,\n \t\t\t\t int phase)\n {\n \tif (patch->is_delete > 0) {\n-\t\tif (phase == 0)\n-\t\t\tremove_file(state, patch, 1);\n+\t\tif (phase == 0) {\n+\t\t\tif (remove_file(state, patch, 1))\n+\t\t\t\texit(1);\n+\t\t}\n \t\treturn;\n \t}\n \tif (patch->is_new > 0 || patch->is_copy) {\n@@ -4273,8 +4276,10 @@ static void write_out_one_result(struct apply_state *state,\n \t * Rename or modification boils down to the same\n \t * thing: remove the old, write the new\n \t */\n-\tif (phase == 0)\n-\t\tremove_file(state, patch, patch->is_rename);\n+\tif (phase == 0) {\n+\t\tif (remove_file(state, patch, patch->is_rename))\n+\t\t\texit(1);\n+\t}\n \tif (phase == 1)\n \t\tcreate_file(state, patch);\n }\n-- \n2.8.1.300.g5fed0c0\n"},{"id":"284287","messageId":"1461504863-15946-70-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 69/83] builtin/apply: make add_conflicted_stages_file() return -1 on error","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:34:09Z","receivedAt":"2016-04-24T13:34:09Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 16 ++++++++++------\n 1 file changed, 10 insertions(+), 6 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 89118c1..5a5be49 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -4210,7 +4210,7 @@ static void create_one_file(struct apply_state *state,\n \tdie_errno(_(\"unable to write file '%s' mode %o\"), path, mode);\n }\n \n-static void add_conflicted_stages_file(struct apply_state *state,\n+static int add_conflicted_stages_file(struct apply_state *state,\n \t\t\t\t       struct patch *patch)\n {\n \tint stage, namelen;\n@@ -4218,7 +4218,7 @@ static void add_conflicted_stages_file(struct apply_state *state,\n \tstruct cache_entry *ce;\n \n \tif (!state->update_index)\n-\t\treturn;\n+\t\treturn 0;\n \tnamelen = strlen(patch->new_name);\n \tce_size = cache_entry_size(namelen);\n \tmode = patch->new_mode ? patch->new_mode : (S_IFREG | 0644);\n@@ -4234,8 +4234,11 @@ static void add_conflicted_stages_file(struct apply_state *state,\n \t\tce->ce_namelen = namelen;\n \t\thashcpy(ce->sha1, patch->threeway_stage[stage - 1].hash);\n \t\tif (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)\n-\t\t\tdie(_(\"unable to add cache entry for %s\"), patch->new_name);\n+\t\t\treturn error(_(\"unable to add cache entry for %s\"),\n+\t\t\t\t     patch->new_name);\n \t}\n+\n+\treturn 0;\n }\n \n static void create_file(struct apply_state *state, struct patch *patch)\n@@ -4249,9 +4252,10 @@ static void create_file(struct apply_state *state, struct patch *patch)\n \t\tmode = S_IFREG | 0644;\n \tcreate_one_file(state, path, mode, buf, size);\n \n-\tif (patch->conflicted_threeway)\n-\t\tadd_conflicted_stages_file(state, patch);\n-\telse\n+\tif (patch->conflicted_threeway) {\n+\t\tif (add_conflicted_stages_file(state, patch))\n+\t\t\texit(1);\n+\t} else\n \t\tadd_index_file(state, path, mode, buf, size);\n }\n \n-- \n2.8.1.300.g5fed0c0\n"},{"id":"284280","messageId":"1461504863-15946-71-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 70/83] builtin/apply: make add_index_file() return -1 on error","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:34:10Z","receivedAt":"2016-04-24T13:34:10Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 48 +++++++++++++++++++++++++++++++-----------------\n 1 file changed, 31 insertions(+), 17 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 5a5be49..e021fad 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -4085,11 +4085,11 @@ static int remove_file(struct apply_state *state, struct patch *patch, int rmdir\n \treturn 0;\n }\n \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+static int 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@@ -4097,7 +4097,7 @@ static void add_index_file(struct apply_state *state,\n \tunsigned ce_size = cache_entry_size(namelen);\n \n \tif (!state->update_index)\n-\t\treturn;\n+\t\treturn 0;\n \n \tce = xcalloc(1, ce_size);\n \tmemcpy(ce->name, path, namelen);\n@@ -4108,20 +4108,32 @@ static void add_index_file(struct apply_state *state,\n \t\tconst char *s;\n \n \t\tif (!skip_prefix(buf, \"Subproject commit \", &s) ||\n-\t\t    get_sha1_hex(s, ce->sha1))\n-\t\t\tdie(_(\"corrupt patch for submodule %s\"), path);\n+\t\t    get_sha1_hex(s, ce->sha1)) {\n+\t\t\tfree(ce);\n+\t\t\treturn error(_(\"corrupt patch for submodule %s\"), path);\n+\t\t}\n \t} else {\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+\t\t\tif (lstat(path, &st) < 0) {\n+\t\t\t\tfree(ce);\n+\t\t\t\treturn error(_(\"unable to stat newly \"\n+\t\t\t\t\t       \"created file '%s': %s\"),\n+\t\t\t\t\t     path, strerror(errno));\n+\t\t\t}\n \t\t\tfill_stat_cache_info(ce, &st);\n \t\t}\n-\t\tif (write_sha1_file(buf, size, blob_type, ce->sha1) < 0)\n-\t\t\tdie(_(\"unable to create backing store for newly created file %s\"), path);\n+\t\tif (write_sha1_file(buf, size, blob_type, ce->sha1) < 0) {\n+\t\t\tfree(ce);\n+\t\t\treturn error(_(\"unable to create backing store \"\n+\t\t\t\t       \"for newly created file %s\"), path);\n+\t\t}\n+\t}\n+\tif (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0) {\n+\t\tfree(ce);\n+\t\treturn error(_(\"unable to add cache entry for %s\"), path);\n \t}\n-\tif (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)\n-\t\tdie(_(\"unable to add cache entry for %s\"), path);\n+\n+\treturn 0;\n }\n \n static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)\n@@ -4255,8 +4267,10 @@ static void create_file(struct apply_state *state, struct patch *patch)\n \tif (patch->conflicted_threeway) {\n \t\tif (add_conflicted_stages_file(state, patch))\n \t\t\texit(1);\n-\t} else\n-\t\tadd_index_file(state, path, mode, buf, size);\n+\t} else {\n+\t\tif (add_index_file(state, path, mode, buf, size))\n+\t\t\texit(1);\n+\t}\n }\n \n /* phase zero is to remove, phase one is to create */\n-- \n2.8.1.300.g5fed0c0\n"},{"id":"284286","messageId":"1461504863-15946-72-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 71/83] builtin/apply: make create_file() return -1 on error","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:34:11Z","receivedAt":"2016-04-24T13:34:11Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 25 +++++++++++++------------\n 1 file changed, 13 insertions(+), 12 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex e021fad..ebac9ee 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -4253,7 +4253,7 @@ static int add_conflicted_stages_file(struct apply_state *state,\n \treturn 0;\n }\n \n-static void create_file(struct apply_state *state, struct patch *patch)\n+static int create_file(struct apply_state *state, struct patch *patch)\n {\n \tchar *path = patch->new_name;\n \tunsigned mode = patch->new_mode;\n@@ -4264,13 +4264,10 @@ static void create_file(struct apply_state *state, struct patch *patch)\n \t\tmode = S_IFREG | 0644;\n \tcreate_one_file(state, path, mode, buf, size);\n \n-\tif (patch->conflicted_threeway) {\n-\t\tif (add_conflicted_stages_file(state, patch))\n-\t\t\texit(1);\n-\t} else {\n-\t\tif (add_index_file(state, path, mode, buf, size))\n-\t\t\texit(1);\n-\t}\n+\tif (patch->conflicted_threeway)\n+\t\treturn add_conflicted_stages_file(state, patch);\n+\telse\n+\t\treturn add_index_file(state, path, mode, buf, size);\n }\n \n /* phase zero is to remove, phase one is to create */\n@@ -4286,8 +4283,10 @@ static void write_out_one_result(struct apply_state *state,\n \t\treturn;\n \t}\n \tif (patch->is_new > 0 || patch->is_copy) {\n-\t\tif (phase == 1)\n-\t\t\tcreate_file(state, patch);\n+\t\tif (phase == 1) {\n+\t\t\tif (create_file(state, patch))\n+\t\t\t\texit(1);\n+\t\t}\n \t\treturn;\n \t}\n \t/*\n@@ -4298,8 +4297,10 @@ static void write_out_one_result(struct apply_state *state,\n \t\tif (remove_file(state, patch, patch->is_rename))\n \t\t\texit(1);\n \t}\n-\tif (phase == 1)\n-\t\tcreate_file(state, patch);\n+\tif (phase == 1) {\n+\t\tif (create_file(state, patch))\n+\t\t\texit(1);\n+\t}\n }\n \n static int write_out_one_reject(struct apply_state *state, struct patch *patch)\n-- \n2.8.1.300.g5fed0c0\n"},{"id":"284283","messageId":"1461504863-15946-73-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 72/83] builtin/apply: make write_out_one_result() return -1 on error","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:34:12Z","receivedAt":"2016-04-24T13:34:12Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 38 ++++++++++++++++----------------------\n 1 file changed, 16 insertions(+), 22 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex ebac9ee..dae89e2 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -4271,36 +4271,29 @@ static int create_file(struct apply_state *state, struct patch *patch)\n }\n \n /* phase zero is to remove, phase one is to create */\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+static int write_out_one_result(struct apply_state *state,\n+\t\t\t\tstruct patch *patch,\n+\t\t\t\tint phase)\n {\n \tif (patch->is_delete > 0) {\n-\t\tif (phase == 0) {\n-\t\t\tif (remove_file(state, patch, 1))\n-\t\t\t\texit(1);\n-\t\t}\n-\t\treturn;\n+\t\tif (phase == 0)\n+\t\t\treturn remove_file(state, patch, 1);\n+\t\treturn 0;\n \t}\n \tif (patch->is_new > 0 || patch->is_copy) {\n-\t\tif (phase == 1) {\n-\t\t\tif (create_file(state, patch))\n-\t\t\t\texit(1);\n-\t\t}\n-\t\treturn;\n+\t\tif (phase == 1)\n+\t\t\treturn create_file(state, patch);\n+\t\treturn 0;\n \t}\n \t/*\n \t * Rename or modification boils down to the same\n \t * thing: remove the old, write the new\n \t */\n-\tif (phase == 0) {\n-\t\tif (remove_file(state, patch, patch->is_rename))\n-\t\t\texit(1);\n-\t}\n-\tif (phase == 1) {\n-\t\tif (create_file(state, patch))\n-\t\t\texit(1);\n-\t}\n+\tif (phase == 0)\n+\t\treturn remove_file(state, patch, patch->is_rename);\n+\tif (phase == 1)\n+\t\treturn create_file(state, patch);\n+\treturn 0;\n }\n \n static int write_out_one_reject(struct apply_state *state, struct patch *patch)\n@@ -4387,7 +4380,8 @@ 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(state, l, phase);\n+\t\t\t\tif (write_out_one_result(state, l, phase))\n+\t\t\t\t\texit(1);\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-- \n2.8.1.300.g5fed0c0\n"},{"id":"284285","messageId":"1461504863-15946-74-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 73/83] builtin/apply: make write_out_results() return -1 on error","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:34:13Z","receivedAt":"2016-04-24T13:34:13Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 21 ++++++++++++++++-----\n 1 file changed, 16 insertions(+), 5 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex dae89e2..49ef4c9 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -4367,6 +4367,12 @@ static int write_out_one_reject(struct apply_state *state, struct patch *patch)\n \treturn -1;\n }\n \n+/*\n+ * Returns:\n+ *  -1 if an error happened\n+ *   0 if the patch applied cleanly\n+ *   1 if the patch did not apply cleanly\n+ */\n static int write_out_results(struct apply_state *state, struct patch *list)\n {\n \tint phase;\n@@ -4381,7 +4387,7 @@ static int write_out_results(struct apply_state *state, struct patch *list)\n \t\t\t\terrs = 1;\n \t\t\telse {\n \t\t\t\tif (write_out_one_result(state, l, phase))\n-\t\t\t\t\texit(1);\n+\t\t\t\t\treturn -1;\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@@ -4484,11 +4490,16 @@ static int apply_patch(struct apply_state *state,\n \t    !state->apply_with_reject)\n \t\treturn -1;\n \n-\tif (state->apply && write_out_results(state, list)) {\n-\t\tif (state->apply_with_reject)\n+\tif (state->apply) {\n+\t\tint res = write_out_results(state, list);\n+\t\tif (res < 0)\n \t\t\treturn -1;\n-\t\t/* with --3way, we still need to write the index out */\n-\t\treturn 1;\n+\t\tif (res > 0) {\n+\t\t\tif (state->apply_with_reject)\n+\t\t\t\treturn -1;\n+\t\t\t/* with --3way, we still need to write the index out */\n+\t\t\treturn 1;\n+\t\t}\n \t}\n \n \tif (state->fake_ancestor &&\n-- \n2.8.1.300.g5fed0c0\n"},{"id":"284290","messageId":"1461504863-15946-75-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 74/83] builtin/apply: make try_create_file() return -1 on error","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:34:14Z","receivedAt":"2016-04-24T13:34:14Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 38 ++++++++++++++++++++++++++++++--------\n 1 file changed, 30 insertions(+), 8 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 49ef4c9..32c38f0 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -4136,6 +4136,12 @@ static int add_index_file(struct apply_state *state,\n \treturn 0;\n }\n \n+/*\n+ * Returns:\n+ *  -1 if an unrecoverable error happened\n+ *   0 if everything went well\n+ *   1 if a recoverable error happened\n+ */\n static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)\n {\n \tint fd;\n@@ -4145,28 +4151,32 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,\n \t\tstruct stat st;\n \t\tif (!lstat(path, &st) && S_ISDIR(st.st_mode))\n \t\t\treturn 0;\n-\t\treturn mkdir(path, 0777);\n+\t\treturn !!mkdir(path, 0777);\n \t}\n \n \tif (has_symlinks && S_ISLNK(mode))\n \t\t/* Although buf:size is counted string, it also is NUL\n \t\t * terminated.\n \t\t */\n-\t\treturn symlink(buf, path);\n+\t\treturn !!symlink(buf, path);\n \n \tfd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666);\n \tif (fd < 0)\n-\t\treturn -1;\n+\t\treturn 1;\n \n \tif (convert_to_working_tree(path, buf, size, &nbuf)) {\n \t\tsize = nbuf.len;\n \t\tbuf  = nbuf.buf;\n \t}\n-\twrite_or_die(fd, buf, size);\n+\n+\tif (!write_or_whine_pipe(fd, buf, size, path)) {\n+\t\tstrbuf_release(&nbuf);\n+\t\treturn -1;\n+\t}\n \tstrbuf_release(&nbuf);\n \n \tif (close(fd) < 0)\n-\t\tdie_errno(_(\"closing file '%s'\"), path);\n+\t\treturn error(_(\"closing file '%s': %s\"), path, strerror(errno));\n \treturn 0;\n }\n \n@@ -4181,16 +4191,25 @@ static void create_one_file(struct apply_state *state,\n \t\t\t    const char *buf,\n \t\t\t    unsigned long size)\n {\n+\tint res;\n+\n \tif (state->cached)\n \t\treturn;\n-\tif (!try_create_file(path, mode, buf, size))\n+\n+\tres = try_create_file(path, mode, buf, size);\n+\tif (!res)\n \t\treturn;\n+\tif (res < 0)\n+\t\texit(1);\n \n \tif (errno == ENOENT) {\n \t\tif (safe_create_leading_directories(path))\n \t\t\treturn;\n-\t\tif (!try_create_file(path, mode, buf, size))\n+\t\tres = try_create_file(path, mode, buf, size);\n+\t\tif (!res)\n \t\t\treturn;\n+\t\tif (res < 0)\n+\t\t\texit(1);\n \t}\n \n \tif (errno == EEXIST || errno == EACCES) {\n@@ -4208,12 +4227,15 @@ static void create_one_file(struct apply_state *state,\n \t\tfor (;;) {\n \t\t\tchar newpath[PATH_MAX];\n \t\t\tmksnpath(newpath, sizeof(newpath), \"%s~%u\", path, nr);\n-\t\t\tif (!try_create_file(newpath, mode, buf, size)) {\n+\t\t\tres = try_create_file(newpath, mode, buf, size);\n+\t\t\tif (!res) {\n \t\t\t\tif (!rename(newpath, path))\n \t\t\t\t\treturn;\n \t\t\t\tunlink_or_warn(newpath);\n \t\t\t\tbreak;\n \t\t\t}\n+\t\t\tif (res < 0)\n+\t\t\t\texit(1);\n \t\t\tif (errno != EEXIST)\n \t\t\t\tbreak;\n \t\t\t++nr;\n-- \n2.8.1.300.g5fed0c0\n"},{"id":"284288","messageId":"1461504863-15946-76-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 75/83] builtin/apply: make create_one_file() return -1 on error","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:34:15Z","receivedAt":"2016-04-24T13:34:15Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 36 +++++++++++++++++++++---------------\n 1 file changed, 21 insertions(+), 15 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 32c38f0..5bd5154 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -4184,32 +4184,36 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,\n  * We optimistically assume that the directories exist,\n  * which is true 99% of the time anyway. If they don't,\n  * we create them and try again.\n+ *\n+ * Returns:\n+ *   -1 on error\n+ *   0 otherwise\n  */\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+static int 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 \tint res;\n \n \tif (state->cached)\n-\t\treturn;\n+\t\treturn 0;\n \n \tres = try_create_file(path, mode, buf, size);\n \tif (!res)\n-\t\treturn;\n+\t\treturn 0;\n \tif (res < 0)\n-\t\texit(1);\n+\t\treturn -1;\n \n \tif (errno == ENOENT) {\n \t\tif (safe_create_leading_directories(path))\n-\t\t\treturn;\n+\t\t\treturn 0;\n \t\tres = try_create_file(path, mode, buf, size);\n \t\tif (!res)\n-\t\t\treturn;\n+\t\t\treturn 0;\n \t\tif (res < 0)\n-\t\t\texit(1);\n+\t\t\treturn -1;\n \t}\n \n \tif (errno == EEXIST || errno == EACCES) {\n@@ -4230,18 +4234,19 @@ static void create_one_file(struct apply_state *state,\n \t\t\tres = try_create_file(newpath, mode, buf, size);\n \t\t\tif (!res) {\n \t\t\t\tif (!rename(newpath, path))\n-\t\t\t\t\treturn;\n+\t\t\t\t\treturn 0;\n \t\t\t\tunlink_or_warn(newpath);\n \t\t\t\tbreak;\n \t\t\t}\n \t\t\tif (res < 0)\n-\t\t\t\texit(1);\n+\t\t\t\treturn -1;\n \t\t\tif (errno != EEXIST)\n \t\t\t\tbreak;\n \t\t\t++nr;\n \t\t}\n \t}\n-\tdie_errno(_(\"unable to write file '%s' mode %o\"), path, mode);\n+\treturn error(_(\"unable to write file '%s' mode %o: %s\"),\n+\t\t     path, mode, strerror(errno));\n }\n \n static int add_conflicted_stages_file(struct apply_state *state,\n@@ -4284,7 +4289,8 @@ static int create_file(struct apply_state *state, struct patch *patch)\n \n \tif (!mode)\n \t\tmode = S_IFREG | 0644;\n-\tcreate_one_file(state, path, mode, buf, size);\n+\tif (create_one_file(state, path, mode, buf, size))\n+\t\treturn -1;\n \n \tif (patch->conflicted_threeway)\n \t\treturn add_conflicted_stages_file(state, patch);\n-- \n2.8.1.300.g5fed0c0\n"},{"id":"284289","messageId":"1461504863-15946-77-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 76/83] builtin/apply: rename option parsing functions","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:34:16Z","receivedAt":"2016-04-24T13:34:16Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"As these functions are going to be part of the libified\napply api, let's give them a name that is more specific\nto the apply api.\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 40 ++++++++++++++++++++--------------------\n 1 file changed, 20 insertions(+), 20 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 5bd5154..5910c5c 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -4549,16 +4549,16 @@ static int apply_patch(struct apply_state *state,\n \treturn 0;\n }\n \n-static int option_parse_exclude(const struct option *opt,\n-\t\t\t\tconst char *arg, int unset)\n+static int apply_option_parse_exclude(const struct option *opt,\n+\t\t\t\t      const char *arg, int unset)\n {\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+static int apply_option_parse_include(const struct option *opt,\n+\t\t\t\t      const char *arg, int unset)\n {\n \tstruct apply_state *state = opt->value;\n \tadd_name_limit(state, arg, 0);\n@@ -4566,9 +4566,9 @@ static int option_parse_include(const struct option *opt,\n \treturn 0;\n }\n \n-static int option_parse_p(const struct option *opt,\n-\t\t\t  const char *arg,\n-\t\t\t  int unset)\n+static int apply_option_parse_p(const struct option *opt,\n+\t\t\t\tconst char *arg,\n+\t\t\t\tint unset)\n {\n \tstruct apply_state *state = opt->value;\n \tstate->p_value = atoi(arg);\n@@ -4576,8 +4576,8 @@ static int option_parse_p(const struct option *opt,\n \treturn 0;\n }\n \n-static int option_parse_space_change(const struct option *opt,\n-\t\t\t\t     const char *arg, int unset)\n+static int apply_option_parse_space_change(const struct option *opt,\n+\t\t\t\t\t   const char *arg, int unset)\n {\n \tstruct apply_state *state = opt->value;\n \tif (unset)\n@@ -4587,8 +4587,8 @@ static int option_parse_space_change(const struct option *opt,\n \treturn 0;\n }\n \n-static int option_parse_whitespace(const struct option *opt,\n-\t\t\t\t   const char *arg, int unset)\n+static int apply_option_parse_whitespace(const struct option *opt,\n+\t\t\t\t\t const char *arg, int unset)\n {\n \tstruct apply_state *state = opt->value;\n \tstate->whitespace_option = arg;\n@@ -4597,8 +4597,8 @@ static int option_parse_whitespace(const struct option *opt,\n \treturn 0;\n }\n \n-static int option_parse_directory(const struct option *opt,\n-\t\t\t\t  const char *arg, int unset)\n+static int apply_option_parse_directory(const struct option *opt,\n+\t\t\t\t\tconst char *arg, int unset)\n {\n \tstruct apply_state *state = opt->value;\n \tstrbuf_reset(&state->root);\n@@ -4696,13 +4696,13 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\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-\t\t\t0, option_parse_exclude },\n+\t\t\t0, apply_option_parse_exclude },\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\t0, apply_option_parse_include },\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\t\t0, apply_option_parse_p },\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@@ -4734,13 +4734,13 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t\t\t\tN_(\"ensure at least <n> lines of context match\")),\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\t0, apply_option_parse_whitespace },\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\tPARSE_OPT_NOARG, apply_option_parse_space_change },\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\t\tPARSE_OPT_NOARG, apply_option_parse_space_change },\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@@ -4758,7 +4758,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \t\t\tRECOUNT),\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\t\t0, apply_option_parse_directory },\n \t\tOPT_END()\n \t};\n \n-- \n2.8.1.300.g5fed0c0\n"},{"id":"284284","messageId":"1461504863-15946-78-git-send-email-chriscool@tuxfamily.org","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"[PATCH 77/83] apply: rename and move opt constants to apply.h","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T13:34:17Z","receivedAt":"2016-04-24T13:34:17Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n apply.h         |  3 +++\n builtin/apply.c | 11 ++++-------\n 2 files changed, 7 insertions(+), 7 deletions(-)\n\ndiff --git a/apply.h b/apply.h\nindex 35d4d15..3df0128 100644\n--- a/apply.h\n+++ b/apply.h\n@@ -129,4 +129,7 @@ extern int parse_ignorewhitespace_option(struct apply_state *state,\n extern int init_apply_state(struct apply_state *state, const char *prefix);\n extern int check_apply_state(struct apply_state *state, int force_apply);\n \n+#define APPLY_OPT_INACCURATE_EOF\t(1<<0)\n+#define APPLY_OPT_RECOUNT\t\t(1<<1)\n+\n #endif\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 5910c5c..1c53c7e 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -4443,9 +4443,6 @@ static int write_out_results(struct apply_state *state, struct patch *list)\n \treturn errs;\n }\n \n-#define INACCURATE_EOF\t(1<<0)\n-#define RECOUNT\t\t(1<<1)\n-\n /*\n  * Try to apply a patch.\n  *\n@@ -4473,8 +4470,8 @@ static int apply_patch(struct apply_state *state,\n \t\tint nr;\n \n \t\tpatch = xcalloc(1, sizeof(*patch));\n-\t\tpatch->inaccurate_eof = !!(options & INACCURATE_EOF);\n-\t\tpatch->recount =  !!(options & RECOUNT);\n+\t\tpatch->inaccurate_eof = !!(options & APPLY_OPT_INACCURATE_EOF);\n+\t\tpatch->recount =  !!(options & APPLY_OPT_RECOUNT);\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@@ -4752,10 +4749,10 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\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+\t\t\tAPPLY_OPT_INACCURATE_EOF),\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\tAPPLY_OPT_RECOUNT),\n \t\t{ OPTION_CALLBACK, 0, \"directory\", &state, N_(\"root\"),\n \t\t\tN_(\"prepend <root> to all filenames\"),\n \t\t\t0, apply_option_parse_directory },\n-- \n2.8.1.300.g5fed0c0\n"},{"id":"284339","messageId":"571CE4D5.8010403@ramsayjones.plus.com","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"Re: [PATCH 00/83] libify apply and use lib in am","fromName":"Ramsay Jones","fromEmail":"ramsay@ramsayjones.plus.com","sentAt":"2016-04-24T15:23:01Z","receivedAt":"2016-04-24T15:23:01Z","isPatch":true,"sender":{"key":"ramsay@ramsayjones.plus.com","avatar":"https://avatars.githubusercontent.com/u/33702710?v=4"},"body":"\n\nOn 24/04/16 14:33, Christian Couder wrote:\n> This is a patch series about libifying `git apply` functionality, and\n> using this libified functionality in `git am`, so that no 'git apply'\n> process is spawn anymore. This makes `git am` significantly faster, so\n> `git rebase`, when it uses the am backend, is also significantly\n> faster.\n\nI just tried to git-am these patches, but patch #78 did not make it\nto the list.\n\n(Also, patches #59 and #62 both issue a 'new blank line at EOF' warning).\n\nATB,\nRamsay Jones\n"},{"id":"284340","messageId":"CAP8UFD0dEYPSku8w=7ehJFFgtNFO8EbUc4RDn+KRdrYG3-=Rxg@mail.gmail.com","threadId":"42127","inReplyTo":"571CE4D5.8010403@ramsayjones.plus.com","subject":"Re: [PATCH 00/83] libify apply and use lib in am","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T16:27:45Z","receivedAt":"2016-04-24T16:27:45Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Sun, Apr 24, 2016 at 5:23 PM, Ramsay Jones\n<ramsay@ramsayjones.plus.com> wrote:\n>\n>\n> On 24/04/16 14:33, Christian Couder wrote:\n>> This is a patch series about libifying `git apply` functionality, and\n>> using this libified functionality in `git am`, so that no 'git apply'\n>> process is spawn anymore. This makes `git am` significantly faster, so\n>> `git rebase`, when it uses the am backend, is also significantly\n>> faster.\n>\n> I just tried to git-am these patches, but patch #78 did not make it\n> to the list.\n\nThat's strange because gmail tells me it has been sent, and it made it\nto chriscool@tuxfamily.org.\n\nI use send-email and smtp.gmail.com. Just after sending patch #78 the\nconnection to smtp.gmail.com was closed with:\n\n4.7.0 Try again later, closing connection. (MAIL) j6sm6717101wjb.29 - gsmtp\n\nThen I had to wait a few minutes before I could send the last patches.\n\n> (Also, patches #59 and #62 both issue a 'new blank line at EOF' warning).\n\nOk, I will take a look at that.\n\nThanks,\nChristian.\n"},{"id":"284344","messageId":"CAP8UFD0rKNAoZgCPQkfo_qd7b_V4rOtLqBvQ-XQoeT6OnZidGw@mail.gmail.com","threadId":"42127","inReplyTo":"CAP8UFD0dEYPSku8w=7ehJFFgtNFO8EbUc4RDn+KRdrYG3-=Rxg@mail.gmail.com","subject":"Re: [PATCH 00/83] libify apply and use lib in am","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-24T16:56:27Z","receivedAt":"2016-04-24T16:56:27Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Sun, Apr 24, 2016 at 6:27 PM, Christian Couder\n<christian.couder@gmail.com> wrote:\n> On Sun, Apr 24, 2016 at 5:23 PM, Ramsay Jones\n> <ramsay@ramsayjones.plus.com> wrote:\n>>\n>>\n>> On 24/04/16 14:33, Christian Couder wrote:\n>>> This is a patch series about libifying `git apply` functionality, and\n>>> using this libified functionality in `git am`, so that no 'git apply'\n>>> process is spawn anymore. This makes `git am` significantly faster, so\n>>> `git rebase`, when it uses the am backend, is also significantly\n>>> faster.\n>>\n>> I just tried to git-am these patches, but patch #78 did not make it\n>> to the list.\n>\n> That's strange because gmail tells me it has been sent, and it made it\n> to chriscool@tuxfamily.org.\n\nInstead of waiting for the patch to appear on the list, you might want\nto use branch libify-apply-use-in-am25 in my GitHub repo:\n\nhttps://github.com/chriscool/git/commits/libify-apply-use-in-am25\n"},{"id":"284346","messageId":"571D059B.2010908@ramsayjones.plus.com","threadId":"42127","inReplyTo":"CAP8UFD0rKNAoZgCPQkfo_qd7b_V4rOtLqBvQ-XQoeT6OnZidGw@mail.gmail.com","subject":"Re: [PATCH 00/83] libify apply and use lib in am","fromName":"Ramsay Jones","fromEmail":"ramsay@ramsayjones.plus.com","sentAt":"2016-04-24T17:42:51Z","receivedAt":"2016-04-24T17:42:51Z","isPatch":true,"sender":{"key":"ramsay@ramsayjones.plus.com","avatar":"https://avatars.githubusercontent.com/u/33702710?v=4"},"body":"\n\nOn 24/04/16 17:56, Christian Couder wrote:\n> On Sun, Apr 24, 2016 at 6:27 PM, Christian Couder\n> <christian.couder@gmail.com> wrote:\n>> On Sun, Apr 24, 2016 at 5:23 PM, Ramsay Jones\n>> <ramsay@ramsayjones.plus.com> wrote:\n>>>\n>>>\n>>> On 24/04/16 14:33, Christian Couder wrote:\n>>>> This is a patch series about libifying `git apply` functionality, and\n>>>> using this libified functionality in `git am`, so that no 'git apply'\n>>>> process is spawn anymore. This makes `git am` significantly faster, so\n>>>> `git rebase`, when it uses the am backend, is also significantly\n>>>> faster.\n>>>\n>>> I just tried to git-am these patches, but patch #78 did not make it\n>>> to the list.\n>>\n>> That's strange because gmail tells me it has been sent, and it made it\n>> to chriscool@tuxfamily.org.\n> \n> Instead of waiting for the patch to appear on the list, you might want\n> to use branch libify-apply-use-in-am25 in my GitHub repo:\n> \n> https://github.com/chriscool/git/commits/libify-apply-use-in-am25\n\nHmm, that branch doesn't correspond directly to the patches you sent\nout (there are 86 commits, some marked with draft. I think commit d13d2ac\ncorresponds kinda to patch #83, but .... ).\n\nI think I'll wait to see the patches as you intend them to be seen. ;-)\n\nATB,\nRamsay Jones\n"},{"id":"284370","messageId":"CACsJy8Bu+fsWP-dmckbSCRvonpqtCyvwGZWHCvjtfSWKY3A6QQ@mail.gmail.com","threadId":"42127","inReplyTo":"571D059B.2010908@ramsayjones.plus.com","subject":"Re: [PATCH 00/83] libify apply and use lib in am","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2016-04-25T00:14:42Z","receivedAt":"2016-04-25T00:14:42Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Mon, Apr 25, 2016 at 12:42 AM, Ramsay Jones\n<ramsay@ramsayjones.plus.com> wrote:\n>\n>\n> On 24/04/16 17:56, Christian Couder wrote:\n>> On Sun, Apr 24, 2016 at 6:27 PM, Christian Couder\n>> <christian.couder@gmail.com> wrote:\n>>> On Sun, Apr 24, 2016 at 5:23 PM, Ramsay Jones\n>>> <ramsay@ramsayjones.plus.com> wrote:\n>>>>\n>>>>\n>>>> On 24/04/16 14:33, Christian Couder wrote:\n>>>>> This is a patch series about libifying `git apply` functionality, and\n>>>>> using this libified functionality in `git am`, so that no 'git apply'\n>>>>> process is spawn anymore. This makes `git am` significantly faster, so\n>>>>> `git rebase`, when it uses the am backend, is also significantly\n>>>>> faster.\n>>>>\n>>>> I just tried to git-am these patches, but patch #78 did not make it\n>>>> to the list.\n>>>\n>>> That's strange because gmail tells me it has been sent, and it made it\n>>> to chriscool@tuxfamily.org.\n\n#78 moves 4000k lines around and ends up ~260k in size, I think it may\nhave hit vger limit.\n\n>> Instead of waiting for the patch to appear on the list, you might want\n>> to use branch libify-apply-use-in-am25 in my GitHub repo:\n>>\n>> https://github.com/chriscool/git/commits/libify-apply-use-in-am25\n>\n> Hmm, that branch doesn't correspond directly to the patches you sent\n> out (there are 86 commits, some marked with draft. I think commit d13d2ac\n> corresponds kinda to patch #83, but .... ).\n>\n> I think I'll wait to see the patches as you intend them to be seen. ;-)\n\nI git-am'd the series then compared with the rebased version of\nlibify-apply-use-in-am25 on master. 33198a1 (i.e.\nlibify-apply-use-in-am25^) matches what was sent in content (didn't\ncompare commit messages).\n-- \nDuy\n"},{"id":"284375","messageId":"CAPig+cTx6jGwS12c8j_O+kT-f06o3E_yxrWZXmeU-XQ7fUY_9w@mail.gmail.com","threadId":"42127","inReplyTo":"1461504863-15946-46-git-send-email-chriscool@tuxfamily.org","subject":"Re: [PATCH 45/83] builtin/apply: move 'state' init into init_apply_state()","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2016-04-25T07:32:15Z","receivedAt":"2016-04-25T07:32:15Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Sun, Apr 24, 2016 at 9:33 AM, Christian Couder\n<christian.couder@gmail.com> wrote:\n> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n> ---\n> diff --git a/builtin/apply.c b/builtin/apply.c\n> @@ -4670,6 +4670,28 @@ static int option_parse_directory(const struct option *opt,\n> +static void init_apply_state(struct apply_state *state, const char *prefix_)\n> +{\n> +       memset(state, 0, sizeof(*state));\n> +       state->prefix = prefix_;\n> +       state->prefix_length = state->prefix ? strlen(state->prefix) : 0;\n> +       state->apply = 1;\n> +       state->line_termination = '\\n';\n> +       state->p_value = 1;\n> +       state->p_context = UINT_MAX;\n> +       state->squelch_whitespace_errors = 5;\n> +       state->ws_error_action = warn_on_ws_error;\n> +       state->ws_ignore_action = ignore_ws_none;\n> +       state->linenr = 1;\n> +       strbuf_init(&state->root, 0);\n> +\n> +       git_apply_config();\n> +       if (apply_default_whitespace)\n> +               parse_whitespace_option(state, apply_default_whitespace);\n> +       if (apply_default_ignorewhitespace)\n> +               parse_ignorewhitespace_option(state, apply_default_ignorewhitespace);\n> +}\n\nMinor:\n\nIf factoring out this code from cmd_apply() into init_apply_state()\nwas done as a preparatory patch before introduction of 'apply_state',\nthen each new 'state->foo=...' line would already be at its final\nlocation when added by its respective patch.\n\nDoing so would also provide an obvious opportunity to name the\n'prefix' argument to init_apply_state() \"prefix\" rather than the odd\n\"prefix_\".\n\n>  int cmd_apply(int argc, const char **argv, const char *prefix_)\n>  {\n>         int i;\n> @@ -4749,24 +4771,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)\n>                 OPT_END()\n>         };\n>\n> -       memset(&state, 0, sizeof(state));\n> -       state.prefix = prefix_;\n> -       state.prefix_length = state.prefix ? strlen(state.prefix) : 0;\n> -       state.apply = 1;\n> -       state.line_termination = '\\n';\n> -       state.p_value = 1;\n> -       state.p_context = UINT_MAX;\n> -       state.squelch_whitespace_errors = 5;\n> -       state.ws_error_action = warn_on_ws_error;\n> -       state.ws_ignore_action = ignore_ws_none;\n> -       state.linenr = 1;\n> -       strbuf_init(&state.root, 0);\n> -\n> -       git_apply_config();\n> -       if (apply_default_whitespace)\n> -               parse_whitespace_option(&state, apply_default_whitespace);\n> -       if (apply_default_ignorewhitespace)\n> -               parse_ignorewhitespace_option(&state, apply_default_ignorewhitespace);\n> +       init_apply_state(&state, prefix_);\n>\n>         argc = parse_options(argc, argv, state.prefix, builtin_apply_options,\n>                         apply_usage, 0);\n> --\n> 2.8.1.300.g5fed0c0\n"},{"id":"284378","messageId":"CAPig+cRSe8oOjo2h6SuJQyD+he_Q7zHfF4TivZ0amhAu4HLQ+g@mail.gmail.com","threadId":"42127","inReplyTo":"1461504863-15946-50-git-send-email-chriscool@tuxfamily.org","subject":"Re: [PATCH 49/83] builtin/apply: move 'lock_file' global into 'struct apply_state'","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2016-04-25T07:50:39Z","receivedAt":"2016-04-25T07:50:39Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Sun, Apr 24, 2016 at 9:33 AM, Christian Couder\n<christian.couder@gmail.com> wrote:\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.\n\nBy talking about \"the stack\" here, I suppose you mean that your\ninitial idea was to move the global lock_file into cmd_apply() or\nsomething?\n\n> So let's make the 'lock_file' variable a pointer to a 'struct lock_file'\n> and let's alloc the struct when needed.\n>\n> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n> ---\n> diff --git a/builtin/apply.c b/builtin/apply.c\n> @@ -52,6 +52,12 @@ struct apply_state {\n> +       /*\n> +        * Since lockfile.c keeps a linked list of all created\n> +        * lock_file structures, it isn't safe to free(lock_file).\n> +        */\n> +       struct lock_file *lock_file;\n\nIs there ever a time when lock_file is removed from the list (such as\nupon successful write of the index), in which case it would be safe to\nfree() this?\n\n> @@ -4515,8 +4521,6 @@ static int write_out_results(struct apply_state *state, struct patch *list)\n>         return errs;\n>  }\n>\n> -static struct lock_file lock_file;\n\nDoes the static lock_file in build_fake_ancestor() deserve the same\nsort of treatment? (I haven't traced the code enough to answer this.)\n\n>  #define INACCURATE_EOF (1<<0)\n>  #define RECOUNT                (1<<1)\n"},{"id":"284383","messageId":"CACsJy8AAQJhX+b1UUhVxC_vV_4r=XjDSbtDDd5MLkgwKri8dEg@mail.gmail.com","threadId":"42127","inReplyTo":"1461504863-15946-1-git-send-email-chriscool@tuxfamily.org","subject":"Re: [PATCH 00/83] libify apply and use lib in am","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2016-04-25T09:02:31Z","receivedAt":"2016-04-25T09:02:31Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Sun, Apr 24, 2016 at 8:33 PM, Christian Couder\n<christian.couder@gmail.com> wrote:\n> Sorry if this patch series is a bit long. I can split it into two or\n> more series if it is prefered.\n\nI suspect you deliberately made the series long just to show off how\ngood new git-rebase is ;-)\n\n> Performance numbers:\n>\n>   - A few days ago Ævar did a huge many-hundred commit rebase on the\n>     kernel with untracked cache.\n>\n> command: git rebase --onto 1993b17 52bef0c 29dde7c\n>\n> Vanilla \"next\" without split index:                1m54.953s\n> Vanilla \"next\" with split index:                   1m22.476s\n> This series on top of \"next\" without split index:  1m12.034s\n> This series on top of \"next\" with split index:     0m15.678s\n\nI was a bit puzzled why split-index helped so much. It shouldn't have\nin my opinion unless you paired it with index-helper, to cut down both\nread and write time. So I ran some tests. Long story short, I think we\ncan achieve this gain (and a little more) even without split-index.\n\nI ran my measurement patch [1] with and without your series used this\nseries as rebase material. Without the series, the picture is not so\nsurprising. We run git-apply 80+ times, each consists of this sequence\n\nread index\nwrite index (cache tree updates only)\nread index again\noptionally initialize name hash (when new entries are added, I guess)\nread packed-refs\nwrite index\n\nWith this series, we run a single git-apply which does\n\nread index (and sharedindex too if in split-index mode)\ninitialize name hash\nwrite index 80+ times\n\nThis explains why I guessed it wrong: when you write only, not read\nback, of course index-helper is not required. And with split-index you\nonly write as many entries as you touch (which is usually a small\nnumber compared to worktree's size), so writing 80+ times with\nsplit-index is a lot faster than write 80+ times with whole index.\n\nBut why write so many times when nobody reads it? We only need to\nwrite before git-apply exits, either after successfully applying the\nwhole series, or after it stops at conflicts, and maybe even at die()\nand SIGINT. Yes if git-apply segfaults, then the index update is lost,\nbut in such a case, it's usually a good idea to restart fresh anyway.\nWhen you only write index once (or twice) it won't matter if\nsplit-index is used.\n\n[1] http://article.gmane.org/gmane.comp.version-control.git/292001\n-- \nDuy\n"},{"id":"284384","messageId":"CAP8UFD2PwqcFPnBGexehKKSog=kkGa_C749QokQE-R3wt0yLqQ@mail.gmail.com","threadId":"42127","inReplyTo":"CACsJy8Bu+fsWP-dmckbSCRvonpqtCyvwGZWHCvjtfSWKY3A6QQ@mail.gmail.com","subject":"Re: [PATCH 00/83] libify apply and use lib in am","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-25T09:15:38Z","receivedAt":"2016-04-25T09:15:38Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Mon, Apr 25, 2016 at 2:14 AM, Duy Nguyen <pclouds@gmail.com> wrote:\n> On Mon, Apr 25, 2016 at 12:42 AM, Ramsay Jones\n> <ramsay@ramsayjones.plus.com> wrote:\n>>\n>>\n>> On 24/04/16 17:56, Christian Couder wrote:\n>>> On Sun, Apr 24, 2016 at 6:27 PM, Christian Couder\n>>> <christian.couder@gmail.com> wrote:\n>>>> On Sun, Apr 24, 2016 at 5:23 PM, Ramsay Jones\n>>>> <ramsay@ramsayjones.plus.com> wrote:\n>>>>>\n>>>>> I just tried to git-am these patches, but patch #78 did not make it\n>>>>> to the list.\n>>>>\n>>>> That's strange because gmail tells me it has been sent, and it made it\n>>>> to chriscool@tuxfamily.org.\n>\n> #78 moves 4000k lines around and ends up ~260k in size, I think it may\n> have hit vger limit.\n\nYeah probably. Thanks for looking at this.\n\nI think I will have to split it into smaller patches in a v2.\n\n>>> Instead of waiting for the patch to appear on the list, you might want\n>>> to use branch libify-apply-use-in-am25 in my GitHub repo:\n>>>\n>>> https://github.com/chriscool/git/commits/libify-apply-use-in-am25\n>>\n>> Hmm, that branch doesn't correspond directly to the patches you sent\n>> out (there are 86 commits, some marked with draft. I think commit d13d2ac\n>> corresponds kinda to patch #83, but .... ).\n>>\n>> I think I'll wait to see the patches as you intend them to be seen. ;-)\n>\n> I git-am'd the series then compared with the rebased version of\n> libify-apply-use-in-am25 on master. 33198a1 (i.e.\n> libify-apply-use-in-am25^) matches what was sent in content (didn't\n> compare commit messages).\n\nThanks for checking.\n"},{"id":"284387","messageId":"CAP8UFD05Cpsms8Tf_VmMeemoerfMm0E8fUqL3o0n_9d1hu7VMg@mail.gmail.com","threadId":"42127","inReplyTo":"CACsJy8AAQJhX+b1UUhVxC_vV_4r=XjDSbtDDd5MLkgwKri8dEg@mail.gmail.com","subject":"Re: [PATCH 00/83] libify apply and use lib in am","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-25T09:57:44Z","receivedAt":"2016-04-25T09:57:44Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Mon, Apr 25, 2016 at 11:02 AM, Duy Nguyen <pclouds@gmail.com> wrote:\n> On Sun, Apr 24, 2016 at 8:33 PM, Christian Couder\n> <christian.couder@gmail.com> wrote:\n>> Sorry if this patch series is a bit long. I can split it into two or\n>> more series if it is prefered.\n>\n> I suspect you deliberately made the series long just to show off how\n> good new git-rebase is ;-)\n\nYeah, and `git am` too :-)\n\n>> Performance numbers:\n>>\n>>   - A few days ago Ævar did a huge many-hundred commit rebase on the\n>>     kernel with untracked cache.\n>>\n>> command: git rebase --onto 1993b17 52bef0c 29dde7c\n>>\n>> Vanilla \"next\" without split index:                1m54.953s\n>> Vanilla \"next\" with split index:                   1m22.476s\n>> This series on top of \"next\" without split index:  1m12.034s\n>> This series on top of \"next\" with split index:     0m15.678s\n>\n> I was a bit puzzled why split-index helped so much. It shouldn't have\n> in my opinion unless you paired it with index-helper, to cut down both\n> read and write time. So I ran some tests. Long story short, I think we\n> can achieve this gain (and a little more) even without split-index.\n\nYeah, perhaps. For now though Ævar and myself would be happy to just\nhave a config option for split-index :-)\n\nThe other performance numbers I mentioned show that now the `git am`\npart of a 13 commit long rebase is reduced from 58% to 19% of the\nwhole rebase time. So further improvements in speeding up `git am`\ncould only make such a rebase at most 19% faster.\n\n> I ran my measurement patch [1] with and without your series used this\n> series as rebase material. Without the series, the picture is not so\n> surprising. We run git-apply 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> This explains why I guessed it wrong: when you write only, not read\n> back, of course index-helper is not required. And with split-index you\n> only write as many entries as you touch (which is usually a small\n> number compared to worktree's size), so writing 80+ times with\n> split-index is a lot faster than write 80+ times with whole index.\n\nYeah, I tried to explain in the cover letter and in the last patch why\nthis series enables split-index to give great results, but I think you\nare much better at explaining it.\n\n> But why write so many times when nobody reads it? We only need to\n> write before git-apply exits,\n\nYou mean `git am` here I think.\n\n> either after successfully applying the\n> whole series, or after it stops at conflicts, and maybe even at die()\n> and SIGINT. Yes if git-apply segfaults,\n\nHere too.\n\n> then the index update is lost,\n> but in such a case, it's usually a good idea to restart fresh anyway.\n> When you only write index once (or twice) it won't matter if\n> split-index is used.\n\nYeah I agree, but it would need further work, that can be done after\nthis series is merged.\nAnd I am not sure if the potential gains on a typical rebase would be worth it.\n\nThanks,\nChristian.\n"},{"id":"284388","messageId":"CACsJy8DfcEeOwoN0e401gwuBG-HskY7-k1RYb58+vrEjm8P5CQ@mail.gmail.com","threadId":"42127","inReplyTo":"CAP8UFD05Cpsms8Tf_VmMeemoerfMm0E8fUqL3o0n_9d1hu7VMg@mail.gmail.com","subject":"Re: [PATCH 00/83] libify apply and use lib in am","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2016-04-25T10:11:10Z","receivedAt":"2016-04-25T10:11:10Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Mon, Apr 25, 2016 at 4:57 PM, Christian Couder\n<christian.couder@gmail.com> wrote:\n>> But why write so many times when nobody reads it? We only need to\n>> write before git-apply exits,\n>\n> You mean `git am` here I think.\n>\n>> either after successfully applying the\n>> whole series, or after it stops at conflicts, and maybe even at die()\n>> and SIGINT. Yes if git-apply segfaults,\n>\n> Here too.\n\nYep it's git-am. I didn't read the series, I simply ran and misread\nthe traces a bit.\n\n>> then the index update is lost,\n>> but in such a case, it's usually a good idea to restart fresh anyway.\n>> When you only write index once (or twice) it won't matter if\n>> split-index is used.\n>\n> Yeah I agree, but it would need further work, that can be done after\n> this series is merged.\n\nSure.\n\n> And I am not sure if the potential gains on a typical rebase would be worth it.\n\nI didn't point it out, but in pathological cases where your patch\nseries touches a lot of (or even every) files in the worktree, the\ngain from split-index lowers and could even disappear. I don't know\nhow often that can happen in real life though.\n\nAlso, if you start to use split-index often, please note that I\nhaven't addressed the sharedindex.* pruning part (it's labeled\n\"experimental\" for a reason), you may have to un-split the index and\nrm $GIT_DIR/sharedindex.* manually from time to time to keep disk\nusage down.\n-- \nDuy\n"},{"id":"284397","messageId":"CACsJy8B7g8WgWbr4M4LHFyBshN2gi30pL1bCcZcTPB7qtmnWdQ@mail.gmail.com","threadId":"42127","inReplyTo":"1461504863-15946-54-git-send-email-chriscool@tuxfamily.org","subject":"Re: [PATCH 53/83] builtin/apply: make find_header() return -1 instead of die()ing","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2016-04-25T13:18:55Z","receivedAt":"2016-04-25T13:18:55Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Sun, Apr 24, 2016 at 8:33 PM, Christian Couder\n<christian.couder@gmail.com> wrote:\n> To be compatible with the rest of the error handling in builtin/apply.c,\n> find_header() should return -1 instead of calling die().\n>\n> Unfortunately find_header() already returns -1 when no header is found,\n> so let's make it return -2 instead in this case.\n\nI don't think this is a good way to go. Too many magic numbers. I\ndon't have a better option though. Maybe returning names instead of\nnumbers would help a bit.\n\n>  test_expect_success 'compare diagnostic; ensure file is still here' '\n> -       echo \"fatal: git diff header lacks filename information (line 4)\" >expected &&\n> +       echo \"error: git diff header lacks filename information (line 4)\" >expected &&\n>         test_path_is_file f &&\n>         test_cmp expected actual\n\nNot your fault. But this test_cmp should be test_i18ncmp. I don't know\nif anybody has still been testing with gettext poison though. May\nprobably not worth changing.\n-- \nDuy\n"},{"id":"284398","messageId":"CACsJy8AVVNOPfuAqSKKP-V2hTxHM-iYagXXa3j88GsEt3t7Ckw@mail.gmail.com","threadId":"42127","inReplyTo":"1461504863-15946-62-git-send-email-chriscool@tuxfamily.org","subject":"Re: [PATCH 61/83] builtin/apply: libify check_apply_state()","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2016-04-25T13:26:37Z","receivedAt":"2016-04-25T13:26:37Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Sun, Apr 24, 2016 at 8:34 PM, Christian Couder\n<christian.couder@gmail.com> wrote:\n> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n> ---\n>  builtin/apply.c | 16 +++++++++-------\n>  1 file changed, 9 insertions(+), 7 deletions(-)\n>\n> diff --git a/builtin/apply.c b/builtin/apply.c\n> index e3ee199..7576ec5 100644\n> --- a/builtin/apply.c\n> +++ b/builtin/apply.c\n> @@ -4534,17 +4534,17 @@ static int option_parse_directory(const struct option *opt,\n>         return 0;\n>  }\n>\n> -static void check_apply_state(struct apply_state *state, int force_apply)\n> +static int check_apply_state(struct apply_state *state, int force_apply)\n>  {\n>         int is_not_gitdir = !startup_info->have_repository;\n>\n>         if (state->apply_with_reject && state->threeway)\n> -               die(\"--reject and --3way cannot be used together.\");\n> +               return error(\"--reject and --3way cannot be used together.\");\n>         if (state->cached && state->threeway)\n> -               die(\"--cached and --3way cannot be used together.\");\n> +               return error(\"--cached and --3way cannot be used together.\");\n\nSweet opportunity to _() these messages since it clear shows in this\npatch that other messages of the same category are _()'d in this\nfunction. Could be done as a separate patch. But I'm also ok if you\nsay \"no\".\n\n>         if (state->threeway) {\n>                 if (is_not_gitdir)\n> -                       die(_(\"--3way outside a repository\"));\n> +                       return error(_(\"--3way outside a repository\"));\n>                 state->check_index = 1;\n>         }\n>         if (state->apply_with_reject)\n> @@ -4552,14 +4552,15 @@ static void check_apply_state(struct apply_state *state, int force_apply)\n>         if (!force_apply && (state->diffstat || state->numstat || state->summary || state->check || state->fake_ancestor))\n>                 state->apply = 0;\n>         if (state->check_index && is_not_gitdir)\n> -               die(_(\"--index outside a repository\"));\n> +               return error(_(\"--index outside a repository\"));\n>         if (state->cached) {\n>                 if (is_not_gitdir)\n> -                       die(_(\"--cached outside a repository\"));\n> +                       return error(_(\"--cached outside a repository\"));\n>                 state->check_index = 1;\n>         }\n>         if (state->check_index)\n>                 state->unsafe_paths = 0;\n> +       return 0;\n>  }\n-- \nDuy\n"},{"id":"284399","messageId":"CACsJy8CRiAhHqYDrJifhfarqzarEnuuJm4rw18zxnC9xaBwKNw@mail.gmail.com","threadId":"42127","inReplyTo":"1461504863-15946-64-git-send-email-chriscool@tuxfamily.org","subject":"Re: [PATCH 63/83] builtin/apply: make apply_all_patches() return -1 on error","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2016-04-25T13:30:55Z","receivedAt":"2016-04-25T13:30:55Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Sun, Apr 24, 2016 at 8:34 PM, Christian Couder\n<christian.couder@gmail.com> wrote:\n>         if (state->update_index) {\n>                 if (write_locked_index(&the_index, state->lock_file, COMMIT_LOCK))\n> -                       die(_(\"Unable to write new index file\"));\n> +                       return error(_(\"Unable to write new index file\"));\n>         }\n>\n>         return !!errs;\n> @@ -4698,5 +4698,8 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n>         if (check_apply_state(&state, force_apply))\n>                 exit(1);\n>\n> -       return apply_all_patches(&state, argc, argv, options);\n> +       if (apply_all_patches(&state, argc, argv, options))\n> +               exit(1);\n\nAbusing exit() too much? This is cmd_apply(). A return should be\nenough and you can do the !! trick that is used in\napply_all_patches(), shown just a little bit above.\n\n> +\n> +       return 0;\n>  }\n> --\n> 2.8.1.300.g5fed0c0\n>\n\n\n\n-- \nDuy\n"},{"id":"284400","messageId":"CACsJy8ATSuijr9beSuJ7X51hsmFVCs7TKEYd3puoLPMtVjKZ9g@mail.gmail.com","threadId":"42127","inReplyTo":"1461504863-15946-66-git-send-email-chriscool@tuxfamily.org","subject":"Re: [PATCH 65/83] builtin/apply: make gitdiff_verify_name() return -1 on error","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2016-04-25T13:36:56Z","receivedAt":"2016-04-25T13:36:56Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Sun, Apr 24, 2016 at 8:34 PM, Christian Couder\n<christian.couder@gmail.com> wrote:\n> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n> ---\n>  builtin/apply.c | 52 ++++++++++++++++++++++++++++++----------------------\n>  1 file changed, 30 insertions(+), 22 deletions(-)\n>\n> diff --git a/builtin/apply.c b/builtin/apply.c\n> index 6b8ba2a..268356b 100644\n> --- a/builtin/apply.c\n> +++ b/builtin/apply.c\n> @@ -812,7 +812,7 @@ static int gitdiff_hdrend(struct apply_state *state,\n>                           const char *line,\n>                           struct patch *patch)\n>  {\n> -       return -1;\n> +       return 1;\n>  }\n\nThese are in a function group that will be called as p->fn() in\nparse_git_header(). Is it possible to isolate them in a separate\npatch? This patch can follow after, which covers only\ngitdiff_verify_name() returning -1 and its call sites.\n\n>  static int gitdiff_oldmode(struct apply_state *state,\n> @@ -1016,7 +1018,7 @@ static int gitdiff_unrecognized(struct apply_state *state,\n>                                 const char *line,\n>                                 struct patch *patch)\n>  {\n> -       return -1;\n> +       return 1;\n>  }\n>\n>  /*\n> @@ -1248,9 +1250,13 @@ static int parse_git_header(struct apply_state *state,\n>                 for (i = 0; i < ARRAY_SIZE(optable); i++) {\n>                         const struct opentry *p = optable + i;\n>                         int oplen = strlen(p->str);\n> +                       int res;\n>                         if (len < oplen || memcmp(p->str, line, oplen))\n>                                 continue;\n> -                       if (p->fn(state, line + oplen, patch) < 0)\n> +                       res = p->fn(state, line + oplen, patch);\n> +                       if (res < 0)\n> +                               return -1;\n> +                       if (res > 0)\n>                                 return offset;\n>                         break;\n>                 }\n-- \nDuy\n"},{"id":"284401","messageId":"CACsJy8Cb20L=y_=J6S5=rngvH1n0aWvtf5eJrXP-df0aoiJyrQ@mail.gmail.com","threadId":"42127","inReplyTo":"1461504863-15946-79-git-send-email-chriscool@tuxfamily.org","subject":"Re: [PATCH 78/83] Move libified code from builtin/apply.c to apply.{c,h}","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2016-04-25T13:46:48Z","receivedAt":"2016-04-25T13:46:48Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Sun, Apr 24, 2016 at 8:34 PM, Christian Couder\n<christian.couder@gmail.com> wrote:\n> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n> ---\n>  apply.c         | 4678 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-\n>  apply.h         |   19 +\n>  builtin/apply.c | 4677 ------------------------------------------------------\n>  3 files changed, 4695 insertions(+), 4679 deletions(-)\n\nMaybe if you can rearrange functions in the right order in\nbuiltin/apply.c first and move everything in one patch (i.e. apply.c\nis created here), rename detection kicks in realizes apply.c is a\nrename of (old) builtin/apply.c and only shows the diff of (new)\nbuiltin/apply.c which is about 90 lines... I haven't tested if this is\ntrue though.\n-- \nDuy\n"},{"id":"284412","messageId":"alpine.DEB.2.20.1604251708370.2896@virtualbox","threadId":"42127","inReplyTo":"CAP8UFD0rKNAoZgCPQkfo_qd7b_V4rOtLqBvQ-XQoeT6OnZidGw@mail.gmail.com","subject":"Re: [PATCH 00/83] libify apply and use lib in am","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2016-04-25T15:09:51Z","receivedAt":"2016-04-25T15:09:51Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Chris,\n\nOn Sun, 24 Apr 2016, Christian Couder wrote:\n\n> On Sun, Apr 24, 2016 at 6:27 PM, Christian Couder\n> <christian.couder@gmail.com> wrote:\n> > On Sun, Apr 24, 2016 at 5:23 PM, Ramsay Jones\n> > <ramsay@ramsayjones.plus.com> wrote:\n> >>\n> >>\n> >> On 24/04/16 14:33, Christian Couder wrote:\n> >>> This is a patch series about libifying `git apply` functionality, and\n> >>> using this libified functionality in `git am`, so that no 'git apply'\n> >>> process is spawn anymore. This makes `git am` significantly faster, so\n> >>> `git rebase`, when it uses the am backend, is also significantly\n> >>> faster.\n> >>\n> >> I just tried to git-am these patches, but patch #78 did not make it\n> >> to the list.\n> >\n> > That's strange because gmail tells me it has been sent, and it made it\n> > to chriscool@tuxfamily.org.\n> \n> Instead of waiting for the patch to appear on the list, you might want\n> to use branch libify-apply-use-in-am25 in my GitHub repo:\n> \n> https://github.com/chriscool/git/commits/libify-apply-use-in-am25\n\nThanks for this. In particular with longer patch series, I find mail-based\npatch submissions *really* cumbersome, not only on the submitter's side\nbut also on the consumers' side.\n\nI wonder, however, why you use numbers in the branch name to version\nthings? I thought Git allowed for more advanced versioning than that...\n\n:-)\n\nCiao,\nDscho\n"},{"id":"284458","messageId":"xmqqzishlgj1.fsf@gitster.mtv.corp.google.com","threadId":"42127","inReplyTo":"CAPig+cRSe8oOjo2h6SuJQyD+he_Q7zHfF4TivZ0amhAu4HLQ+g@mail.gmail.com","subject":"Re: [PATCH 49/83] builtin/apply: move 'lock_file' global into 'struct apply_state'","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-04-25T17:55:30Z","receivedAt":"2016-04-25T17:55:30Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Eric Sunshine <sunshine@sunshineco.com> writes:\n\n>> +       /*\n>> +        * Since lockfile.c keeps a linked list of all created\n>> +        * lock_file structures, it isn't safe to free(lock_file).\n>> +        */\n>> +       struct lock_file *lock_file;\n>\n> Is there ever a time when lock_file is removed from the list (such as\n> upon successful write of the index), in which case it would be safe to\n> free() this?\n\nI do not think you need to think about \"free\"ing.\n\nEven if the libified version of the apply internal can be called\nmultiple times to process multiple patch inputs, there is no need to\nrun multiple instances of it yet.  And a lockfile, after the caller\nfinishes interacting with one file using it by calling commit or\nrollback, can be reused to interact with another file.\n\nSo the cleanest would be to:\n\n * make the caller of apply API responsible for preparing a static\n   or (allocating and leaking) lockfile instance,\n\n * make it pass a pointer to the lockfile to the apply API so that\n   it can store it in apply_state, and\n\n * have the caller use apply API feeding one patch at a time in a\n   loop, allowing the same lockfile instance used for each\n   \"invocation\".\n\nI sounded as if I were advocating non-reentrant implementation in\nthe introductory paragraph, but that is not the intention.  If you\nwant to do N threads, you would prepare N lockfile instances, give\nthem to N apply API instances to be stored in N apply_state\ninstances, and you would have N parallel applications, if you wanted\nto.\n"},{"id":"284467","messageId":"CAGZ79kYi0seMF12+Y4VxHBJxTh9wo4LUw0A50PYRvZEBvj6SBA@mail.gmail.com","threadId":"42127","inReplyTo":"1461504863-15946-6-git-send-email-chriscool@tuxfamily.org","subject":"Re: [PATCH 05/83] builtin/apply: extract line_by_line_fuzzy_match() from match_fragment()","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-04-25T18:50:20Z","receivedAt":"2016-04-25T18:50:20Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"> @@ -2251,7 +2319,7 @@ static int match_fragment(struct image *img,\n>                           int match_beginning, int match_end)\n>  {\n>         int i;\n> -       char *fixed_buf, *buf, *orig, *target;\n> +       char *fixed_buf, *orig, *target;\n>         struct strbuf fixed;\n>         size_t fixed_len, postlen;\n>         int preimage_limit;\n> @@ -2312,6 +2380,7 @@ static int match_fragment(struct image *img,\n>                  * There must be one non-blank context line that match\n>                  * a line before the end of img.\n>                  */\n> +               char *buf;\n\npatches 1-4 looking good, with no comment from me. Here is the first spot to\ncomment on.\n\nIt's not clear why we need to declare buf here? Oh wait it is. It's just\nmoved from the start of the function. But why do it in this patch?\nIt seems unrelated to the general intent of the patch. No need to reroll\nfor this nit alone, it just took me a while to figure out it was an unrelated\nthing.\n\n\n>                 char *buf_end;\n>\n>                 buf = preimage->buf;\n"},{"id":"284468","messageId":"CAGZ79kYUr9wQ--898OeyqVw_upqxfVNRsOveUAPSn=PtXZ4xBQ@mail.gmail.com","threadId":"42127","inReplyTo":"1461504863-15946-10-git-send-email-chriscool@tuxfamily.org","subject":"Re: [PATCH 09/83] builtin/apply: move 'check' global into 'struct apply_state'","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-04-25T18:57:47Z","receivedAt":"2016-04-25T18:57:47Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"On Sun, Apr 24, 2016 at 6:33 AM, Christian Couder\n<christian.couder@gmail.com> wrote:\n> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n> ---\n>  builtin/apply.c | 16 +++++++++-------\n>  1 file changed, 9 insertions(+), 7 deletions(-)\n>\n> diff --git a/builtin/apply.c b/builtin/apply.c\n> index ad81210..6c628f6 100644\n> --- a/builtin/apply.c\n> +++ b/builtin/apply.c\n> @@ -25,12 +25,15 @@ struct apply_state {\n>         const char *prefix;\n>         int prefix_length;\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\nThis is true, but at this part of the file/code we rather want to know what\n`check` does, instead of what the command line option --check does.\n(They are 2 different things, though one leading to the other one?) How about:\n\n    /*\n     * Only check the files to be modified, but do not modify the files.\n     */\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\nOh I see it was moved from here. Not sure if we want to rename\ncomments along the way or just keep it in this series.\n"},{"id":"284492","messageId":"CAGZ79kaCqfwgwngcqG5W0fe=SNOsp7nqtvWw=-xhZ60FBPpg+w@mail.gmail.com","threadId":"42127","inReplyTo":"1461504863-15946-19-git-send-email-chriscool@tuxfamily.org","subject":"Re: [PATCH 18/83] builtin/apply: move 'numstat' global into 'struct apply_state'","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-04-25T21:40:15Z","receivedAt":"2016-04-25T21:40:15Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"On Sun, Apr 24, 2016 at 6:33 AM, Christian Couder\n<christian.couder@gmail.com> wrote:\n> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n> ---\n>  builtin/apply.c | 11 ++++++-----\n>  1 file changed, 6 insertions(+), 5 deletions(-)\n>\n> diff --git a/builtin/apply.c b/builtin/apply.c\n> index d90948a..16d78f9 100644\n> --- a/builtin/apply.c\n> +++ b/builtin/apply.c\n> @@ -36,6 +36,9 @@ struct apply_state {\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>         /*\n>          *  --check turns on checking that the working tree matches the\n>          *    files that are being modified, but doesn't apply the patch\n> @@ -51,14 +54,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> @@ -4500,7 +4501,7 @@ static int apply_patch(struct apply_state *state,\n>         if (state->diffstat)\n>                 stat_patch_list(list);\n>\n> -       if (numstat)\n> +       if (state->numstat)\n>                 numstat_patch_list(list);\n>\n>         if (summary)\n> @@ -4598,7 +4599,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)\n>                         N_(\"instead of applying the patch, output diffstat for the input\")),\n>                 OPT_NOOP_NOARG(0, \"allow-binary-replacement\"),\n>                 OPT_NOOP_NOARG(0, \"binary\"),\n> -               OPT_BOOL(0, \"numstat\", &numstat,\n> +               OPT_BOOL(0, \"numstat\", &state.numstat,\n>                         N_(\"show number of added and deleted lines in decimal notation\")),\n>                 OPT_BOOL(0, \"summary\", &summary,\n>                         N_(\"instead of applying the patch, output a summary for the input\")),\n> @@ -4675,7 +4676,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)\n>         }\n>         if (state.apply_with_reject)\n>                 apply = state.apply_verbosely = 1;\n> -       if (!force_apply && (state.diffstat || numstat || summary || state.check || fake_ancestor))\n> +       if (!force_apply && (state.diffstat || state.numstat || summary || state.check || fake_ancestor))\n\nMental note: This patch is just doing a mechanical conversion, so it\nis fine to check for many \"state.FOOs\" here.\n\nHowever later we may want to move this out to a static oneliner like:\n\n    static int really_apply(state *s) {\n      return s->diffstat || s->numstat || ...;\n    }\n\n(with a better name obviously)\n\n\n>                 apply = 0;\n>         if (state.check_index && is_not_gitdir)\n>                 die(_(\"--index outside a repository\"));\n> --\n> 2.8.1.300.g5fed0c0\n>\n> --\n> To unsubscribe from this list: send the line \"unsubscribe git\" in\n> the body of a message to majordomo@vger.kernel.org\n> More majordomo info at  http://vger.kernel.org/majordomo-info.html\n"},{"id":"284494","messageId":"CAGZ79kaaATDzOHFOmCAANcZmrF_5JGAUYW3Ub1WJ7vSLODVP_g@mail.gmail.com","threadId":"42127","inReplyTo":"1461504863-15946-34-git-send-email-chriscool@tuxfamily.org","subject":"Re: [PATCH 33/83] builtin/apply: move 'root' global into 'struct apply_state'","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-04-25T21:50:09Z","receivedAt":"2016-04-25T21:50:09Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"On Sun, Apr 24, 2016 at 6:33 AM, Christian Couder\n<christian.couder@gmail.com> wrote:\n> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n> ---\n>  builtin/apply.c | 82 ++++++++++++++++++++++++++++++++++-----------------------\n>  1 file changed, 49 insertions(+), 33 deletions(-)\n>\n> diff --git a/builtin/apply.c b/builtin/apply.c\n> index fecdb66..209a1b4 100644\n> --- a/builtin/apply.c\n> +++ b/builtin/apply.c\n> @@ -73,6 +73,8 @@ struct apply_state {\n>\n>         struct string_list limit_by_name;\n>         int has_include;\n> +\n> +       struct strbuf root;\n>  };\n>\n>  static int newfd = -1;\n> @@ -98,8 +100,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>         if (!option) {\n> @@ -489,7 +489,10 @@ static char *squash_slash(char *name)\n>         return 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> +                          const char *line,\n> +                          const char *def,\n> +                          int p_value)\n>  {\n>         struct strbuf name = STRBUF_INIT;\n>         char *cp;\n> @@ -513,8 +516,8 @@ static char *find_name_gnu(const char *line, const char *def, int p_value)\n>         }\n>\n>         strbuf_remove(&name, 0, cp - name.buf);\n> -       if (root.len)\n> -               strbuf_insert(&name, 0, root.buf, root.len);\n> +       if (state->root.len)\n> +               strbuf_insert(&name, 0, state->root.buf, state->root.len);\n>         return squash_slash(strbuf_detach(&name, NULL));\n>  }\n>\n> @@ -677,8 +680,12 @@ static size_t diff_timestamp_len(const char *line, size_t len)\n>         return line + len - end;\n>  }\n>\n> -static char *find_name_common(const char *line, const char *def,\n> -                             int p_value, const char *end, int terminate)\n> +static char *find_name_common(struct apply_state *state,\n> +                             const char *line,\n> +                             const char *def,\n> +                             int p_value,\n> +                             const char *end,\n> +                             int terminate)\n>  {\n>         int len;\n>         const char *start = NULL;\n> @@ -716,32 +723,39 @@ static char *find_name_common(const char *line, const char *def,\n>                         return squash_slash(xstrdup(def));\n>         }\n>\n> -       if (root.len) {\n> -               char *ret = xstrfmt(\"%s%.*s\", root.buf, len, start);\n> +       if (state->root.len) {\n> +               char *ret = xstrfmt(\"%s%.*s\", state->root.buf, len, start);\n>                 return squash_slash(ret);\n>         }\n>\n>         return 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> +                      const char *line,\n> +                      char *def,\n> +                      int p_value,\n> +                      int terminate)\n>  {\n>         if (*line == '\"') {\n> -               char *name = find_name_gnu(line, def, p_value);\n> +               char *name = find_name_gnu(state, line, def, p_value);\n>                 if (name)\n>                         return name;\n>         }\n>\n> -       return find_name_common(line, def, p_value, NULL, terminate);\n> +       return 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> +                                  const char *line,\n> +                                  char *def,\n> +                                  int p_value)\n>  {\n>         size_t len;\n>         size_t date_len;\n>\n>         if (*line == '\"') {\n> -               char *name = find_name_gnu(line, def, p_value);\n> +               char *name = find_name_gnu(state, line, def, p_value);\n>                 if (name)\n>                         return name;\n>         }\n> @@ -749,10 +763,10 @@ static char *find_name_traditional(const char *line, char *def, int p_value)\n>         len = strchrnul(line, '\\n') - line;\n>         date_len = diff_timestamp_len(line, len);\n>         if (!date_len)\n> -               return find_name_common(line, def, p_value, NULL, TERM_TAB);\n> +               return find_name_common(state, line, def, p_value, NULL, TERM_TAB);\n>         len -= date_len;\n>\n> -       return find_name_common(line, def, p_value, line + len, 0);\n> +       return find_name_common(state, line, def, p_value, line + len, 0);\n>  }\n>\n>  static int count_slashes(const char *cp)\n> @@ -777,7 +791,7 @@ static int guess_p_value(struct apply_state *state, const char *nameline)\n>\n>         if (is_dev_null(nameline))\n>                 return -1;\n> -       name = find_name_traditional(nameline, NULL, 0);\n> +       name = find_name_traditional(state, nameline, NULL, 0);\n>         if (!name)\n>                 return -1;\n>         cp = strchr(name, '/');\n> @@ -902,17 +916,17 @@ static void parse_traditional_patch(struct apply_state *state,\n>         if (is_dev_null(first)) {\n>                 patch->is_new = 1;\n>                 patch->is_delete = 0;\n> -               name = find_name_traditional(second, NULL, state->p_value);\n> +               name = find_name_traditional(state, second, NULL, state->p_value);\n>                 patch->new_name = name;\n>         } else if (is_dev_null(second)) {\n>                 patch->is_new = 0;\n>                 patch->is_delete = 1;\n> -               name = find_name_traditional(first, NULL, state->p_value);\n> +               name = find_name_traditional(state, first, NULL, state->p_value);\n>                 patch->old_name = name;\n>         } else {\n>                 char *first_name;\n> -               first_name = find_name_traditional(first, NULL, state->p_value);\n> -               name = find_name_traditional(second, first_name, state->p_value);\n> +               first_name = find_name_traditional(state, first, NULL, state->p_value);\n> +               name = find_name_traditional(state, second, first_name, state->p_value);\n>                 free(first_name);\n>                 if (has_epoch_timestamp(first)) {\n>                         patch->is_new = 1;\n> @@ -957,7 +971,7 @@ static void gitdiff_verify_name(struct apply_state *state,\n>                                 int side)\n>  {\n>         if (!*name && !isnull) {\n> -               *name = find_name(line, NULL, state->p_value, TERM_TAB);\n> +               *name = find_name(state, line, NULL, state->p_value, TERM_TAB);\n>                 return;\n>         }\n>\n> @@ -967,7 +981,7 @@ static void gitdiff_verify_name(struct apply_state *state,\n>                 if (isnull)\n>                         die(_(\"git apply: bad git-diff - expected /dev/null, got %s on line %d\"),\n>                             *name, linenr);\n> -               another = find_name(line, NULL, state->p_value, TERM_TAB);\n> +               another = find_name(state, line, NULL, state->p_value, TERM_TAB);\n>                 if (!another || memcmp(another, *name, len + 1))\n>                         die((side == DIFF_NEW_NAME) ?\n>                             _(\"git apply: bad git-diff - inconsistent new filename on line %d\") :\n> @@ -1042,7 +1056,7 @@ static int gitdiff_copysrc(struct apply_state *state,\n>  {\n>         patch->is_copy = 1;\n>         free(patch->old_name);\n> -       patch->old_name = find_name(line, NULL, state->p_value ? state->p_value - 1 : 0, 0);\n> +       patch->old_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);\n>         return 0;\n>  }\n>\n> @@ -1052,7 +1066,7 @@ static int gitdiff_copydst(struct apply_state *state,\n>  {\n>         patch->is_copy = 1;\n>         free(patch->new_name);\n> -       patch->new_name = find_name(line, NULL, state->p_value ? state->p_value - 1 : 0, 0);\n> +       patch->new_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);\n>         return 0;\n>  }\n>\n> @@ -1062,7 +1076,7 @@ static int gitdiff_renamesrc(struct apply_state *state,\n>  {\n>         patch->is_rename = 1;\n>         free(patch->old_name);\n> -       patch->old_name = find_name(line, NULL, state->p_value ? state->p_value - 1 : 0, 0);\n> +       patch->old_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);\n>         return 0;\n>  }\n>\n> @@ -1072,7 +1086,7 @@ static int gitdiff_renamedst(struct apply_state *state,\n>  {\n>         patch->is_rename = 1;\n>         free(patch->new_name);\n> -       patch->new_name = find_name(line, NULL, state->p_value ? state->p_value - 1 : 0, 0);\n> +       patch->new_name = find_name(state, line, NULL, state->p_value ? state->p_value - 1 : 0, 0);\n>         return 0;\n>  }\n>\n> @@ -1331,8 +1345,8 @@ static int parse_git_header(struct apply_state *state,\n>          * the default name from the header.\n>          */\n>         patch->def_name = git_header_name(state, line, len);\n> -       if (patch->def_name && root.len) {\n> -               char *s = xstrfmt(\"%s%s\", root.buf, patch->def_name);\n> +       if (patch->def_name && state->root.len) {\n> +               char *s = xstrfmt(\"%s%s\", state->root.buf, patch->def_name);\n>                 free(patch->def_name);\n>                 patch->def_name = s;\n>         }\n> @@ -4630,9 +4644,10 @@ static int option_parse_whitespace(const struct option *opt,\n>  static int option_parse_directory(const struct option *opt,\n>                                   const char *arg, int unset)\n>  {\n> -       strbuf_reset(&root);\n> -       strbuf_addstr(&root, arg);\n> -       strbuf_complete(&root, '/');\n> +       struct apply_state *state = opt->value;\n\nOr even\n\n    struct strbuf root = ((state*)opt->value)->root;\n\nand then keep the next lines as is?\n\n> +       strbuf_reset(&state->root);\n> +       strbuf_addstr(&state->root, arg);\n> +       strbuf_complete(&state->root, '/');\n>         return 0;\n>  }\n>\n> @@ -4711,7 +4726,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)\n>                 OPT_BIT(0, \"recount\", &options,\n>                         N_(\"do not trust the line counts in the hunk headers\"),\n>                         RECOUNT),\n> -               { OPTION_CALLBACK, 0, \"directory\", NULL, N_(\"root\"),\n> +               { OPTION_CALLBACK, 0, \"directory\", &state, N_(\"root\"),\n>                         N_(\"prepend <root> to all filenames\"),\n>                         0, option_parse_directory },\n>                 OPT_END()\n> @@ -4724,6 +4739,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)\n>         state.line_termination = '\\n';\n>         state.p_value = 1;\n>         state.p_context = UINT_MAX;\n> +       strbuf_init(&state.root, 0);\n\nEventually we want to have some sort of `init_apply_state` function or\na define which has all the values, I guess?\n\nCompare for example to sliding_window.h, where\nwe have\n\n    struct sliding_view {\n        ...\n        struct strbuf buf;\n    };\n\n    #define SLIDING_VIEW_INIT(..., STRBUF_INIT }\n\n>\n>         git_apply_config();\n>         if (apply_default_whitespace)\n> --\n> 2.8.1.300.g5fed0c0\n>\n> --\n> To unsubscribe from this list: send the line \"unsubscribe git\" in\n> the body of a message to majordomo@vger.kernel.org\n> More majordomo info at  http://vger.kernel.org/majordomo-info.html\n"},{"id":"284496","messageId":"CAGZ79kaanAfW7wX-qP5Tog3B9gL_38aJEdMfHy8T5MXj2K9AgA@mail.gmail.com","threadId":"42127","inReplyTo":"CAGZ79kaaATDzOHFOmCAANcZmrF_5JGAUYW3Ub1WJ7vSLODVP_g@mail.gmail.com","subject":"Re: [PATCH 33/83] builtin/apply: move 'root' global into 'struct apply_state'","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-04-25T21:54:37Z","receivedAt":"2016-04-25T21:54:37Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":">\n> Eventually we want to have some sort of `init_apply_state` function or\n> a define which has all the values, I guess?\n>\n> Compare for example to sliding_window.h, where\n> we have\n>\n>     struct sliding_view {\n>         ...\n>         struct strbuf buf;\n>     };\n>\n>     #define SLIDING_VIEW_INIT(..., STRBUF_INIT }\n\nNevermind, just read patch\n\"builtin/apply: move 'state' init into init_apply_state()\"\n"},{"id":"284497","messageId":"CAGZ79kZ4UprHxtmO-rgiOo=od0U2nraq6m6wYcm_g87bsrL-Wg@mail.gmail.com","threadId":"42127","inReplyTo":"1461504863-15946-48-git-send-email-chriscool@tuxfamily.org","subject":"Re: [PATCH 47/83] builtin/apply: move applying patches into apply_all_patches()","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-04-25T22:00:44Z","receivedAt":"2016-04-25T22:00:44Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"On Sun, Apr 24, 2016 at 6:33 AM, Christian Couder\n<christian.couder@gmail.com> wrote:\n> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n\nUp to this patch, have a\nReviewed-by: Stefan Beller <sbeller@google.com>\nin case you want to split the series in here (as indicated in the\ncover letter, this was the last\npatch rerolled, the next patches are new and may need more discussion).\n\nI had some nits, but they cleared up in later patches.\n\nThanks,\nStefan\n\n> ---\n>  builtin/apply.c | 128 ++++++++++++++++++++++++++++++--------------------------\n>  1 file changed, 69 insertions(+), 59 deletions(-)\n>\n> diff --git a/builtin/apply.c b/builtin/apply.c\n> index 94bf120..787426f 100644\n> --- a/builtin/apply.c\n> +++ b/builtin/apply.c\n> @@ -4720,13 +4720,79 @@ static void check_apply_state(struct apply_state *state, int force_apply)\n>                 state->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> +                            int argc,\n> +                            const char **argv,\n> +                            int options)\n>  {\n>         int i;\n>         int errs = 0;\n> +       int read_stdin = 1;\n> +\n> +       for (i = 0; i < argc; i++) {\n> +               const char *arg = argv[i];\n> +               int fd;\n> +\n> +               if (!strcmp(arg, \"-\")) {\n> +                       errs |= apply_patch(state, 0, \"<stdin>\", options);\n> +                       read_stdin = 0;\n> +                       continue;\n> +               } else if (0 < state->prefix_length)\n> +                       arg = prefix_filename(state->prefix,\n> +                                             state->prefix_length,\n> +                                             arg);\n> +\n> +               fd = open(arg, O_RDONLY);\n> +               if (fd < 0)\n> +                       die_errno(_(\"can't open patch '%s'\"), arg);\n> +               read_stdin = 0;\n> +               set_default_whitespace_mode(state);\n> +               errs |= apply_patch(state, fd, arg, options);\n> +               close(fd);\n> +       }\n> +       set_default_whitespace_mode(state);\n> +       if (read_stdin)\n> +               errs |= apply_patch(state, 0, \"<stdin>\", options);\n> +\n> +       if (state->whitespace_error) {\n> +               if (state->squelch_whitespace_errors &&\n> +                   state->squelch_whitespace_errors < state->whitespace_error) {\n> +                       int squelched =\n> +                               state->whitespace_error - state->squelch_whitespace_errors;\n> +                       warning(Q_(\"squelched %d whitespace error\",\n> +                                  \"squelched %d whitespace errors\",\n> +                                  squelched),\n> +                               squelched);\n> +               }\n> +               if (state->ws_error_action == die_on_ws_error)\n> +                       die(Q_(\"%d line adds whitespace errors.\",\n> +                              \"%d lines add whitespace errors.\",\n> +                              state->whitespace_error),\n> +                           state->whitespace_error);\n> +               if (state->applied_after_fixing_ws && state->apply)\n> +                       warning(\"%d line%s applied after\"\n> +                               \" fixing whitespace errors.\",\n> +                               state->applied_after_fixing_ws,\n> +                               state->applied_after_fixing_ws == 1 ? \"\" : \"s\");\n> +               else if (state->whitespace_error)\n> +                       warning(Q_(\"%d line adds whitespace errors.\",\n> +                                  \"%d lines add whitespace errors.\",\n> +                                  state->whitespace_error),\n> +                               state->whitespace_error);\n> +       }\n> +\n> +       if (state->update_index) {\n> +               if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))\n> +                       die(_(\"Unable to write new index file\"));\n> +       }\n> +\n> +       return !!errs;\n> +}\n> +\n> +int cmd_apply(int argc, const char **argv, const char *prefix_)\n> +{\n>         int force_apply = 0;\n>         int options = 0;\n> -       int read_stdin = 1;\n>         struct apply_state state;\n>\n>         struct option builtin_apply_options[] = {\n> @@ -4805,61 +4871,5 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)\n>\n>         check_apply_state(&state, force_apply);\n>\n> -       for (i = 0; i < argc; i++) {\n> -               const char *arg = argv[i];\n> -               int fd;\n> -\n> -               if (!strcmp(arg, \"-\")) {\n> -                       errs |= apply_patch(&state, 0, \"<stdin>\", options);\n> -                       read_stdin = 0;\n> -                       continue;\n> -               } else if (0 < state.prefix_length)\n> -                       arg = prefix_filename(state.prefix,\n> -                                             state.prefix_length,\n> -                                             arg);\n> -\n> -               fd = open(arg, O_RDONLY);\n> -               if (fd < 0)\n> -                       die_errno(_(\"can't open patch '%s'\"), arg);\n> -               read_stdin = 0;\n> -               set_default_whitespace_mode(&state);\n> -               errs |= apply_patch(&state, fd, arg, options);\n> -               close(fd);\n> -       }\n> -       set_default_whitespace_mode(&state);\n> -       if (read_stdin)\n> -               errs |= apply_patch(&state, 0, \"<stdin>\", options);\n> -       if (state.whitespace_error) {\n> -               if (state.squelch_whitespace_errors &&\n> -                   state.squelch_whitespace_errors < state.whitespace_error) {\n> -                       int squelched =\n> -                               state.whitespace_error - state.squelch_whitespace_errors;\n> -                       warning(Q_(\"squelched %d whitespace error\",\n> -                                  \"squelched %d whitespace errors\",\n> -                                  squelched),\n> -                               squelched);\n> -               }\n> -               if (state.ws_error_action == die_on_ws_error)\n> -                       die(Q_(\"%d line adds whitespace errors.\",\n> -                              \"%d lines add whitespace errors.\",\n> -                              state.whitespace_error),\n> -                           state.whitespace_error);\n> -               if (state.applied_after_fixing_ws && state.apply)\n> -                       warning(\"%d line%s applied after\"\n> -                               \" fixing whitespace errors.\",\n> -                               state.applied_after_fixing_ws,\n> -                               state.applied_after_fixing_ws == 1 ? \"\" : \"s\");\n> -               else if (state.whitespace_error)\n> -                       warning(Q_(\"%d line adds whitespace errors.\",\n> -                                  \"%d lines add whitespace errors.\",\n> -                                  state.whitespace_error),\n> -                               state.whitespace_error);\n> -       }\n> -\n> -       if (state.update_index) {\n> -               if (write_locked_index(&the_index, &lock_file, COMMIT_LOCK))\n> -                       die(_(\"Unable to write new index file\"));\n> -       }\n> -\n> -       return !!errs;\n> +       return apply_all_patches(&state, argc, argv, options);\n>  }\n> --\n> 2.8.1.300.g5fed0c0\n>\n"},{"id":"284506","messageId":"CAPig+cQTx3wyk0E-ZBaBYbjsXRqHPg1f6snZKGjWkrTq=fb6Vg@mail.gmail.com","threadId":"42127","inReplyTo":"1461504863-15946-52-git-send-email-chriscool@tuxfamily.org","subject":"Re: [PATCH 51/83] builtin/apply: make apply_patch() return -1 instead of die()ing","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2016-04-26T01:20:11Z","receivedAt":"2016-04-26T01:20:11Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Sun, Apr 24, 2016 at 9:33 AM, Christian Couder\n<christian.couder@gmail.com> wrote:\n> To libify `git apply` functionality we have to signal errors\n> to the caller instead of die()ing.\n>\n> As a first step in this direction, let's make apply_patch() return\n> -1 in case of errors instead of dying. For now its only caller\n> apply_all_patches() will exit(1) when apply_patch() return -1.\n>\n> In a later patch, apply_all_patches() will return -1 too instead of\n> exiting.\n>\n> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n> ---\n> diff --git a/builtin/apply.c b/builtin/apply.c\n> @@ -4522,6 +4522,14 @@ static int write_out_results(struct apply_state *state, struct patch *list)\n>  static int apply_patch(struct apply_state *state,\n>                        int fd,\n>                        const char *filename,\n> @@ -4564,7 +4572,7 @@ static int apply_patch(struct apply_state *state,\n>         }\n>\n>         if (!list && !skipped_patch)\n> -               die(_(\"unrecognized input\"));\n> +               return error(_(\"unrecognized input\"));\n>\n>         if (state->whitespace_error && (state->ws_error_action == die_on_ws_error))\n>                 state->apply = 0;\n> @@ -4575,19 +4583,17 @@ static int apply_patch(struct apply_state *state,\n>                 hold_locked_index(state->lock_file, 1);\n>         }\n>\n> -       if (state->check_index) {\n> -               if (read_cache() < 0)\n> -                       die(_(\"unable to read index file\"));\n> -       }\n> +       if (state->check_index && read_cache() < 0)\n> +               return error(_(\"unable to read index file\"));\n>\n>         if ((state->check || state->apply) &&\n>             check_patch_list(state, list) < 0 &&\n>             !state->apply_with_reject)\n> -               exit(1);\n> +               return -1;\n>\n>         if (state->apply && write_out_results(state, list)) {\n>                 if (state->apply_with_reject)\n> -                       exit(1);\n> +                       return -1;\n>                 /* with --3way, we still need to write the index out */\n>                 return 1;\n>         }\n\nAre these new 'returns' leaking 'list', 'buf', and 'fn_table' which\notherwise get released at the end of the function?\n"},{"id":"284532","messageId":"CAP8UFD228joDLyomvae7rVs02TuTbmEHerwWvH=VR+FE-YFLRA@mail.gmail.com","threadId":"42127","inReplyTo":"CAGZ79kYi0seMF12+Y4VxHBJxTh9wo4LUw0A50PYRvZEBvj6SBA@mail.gmail.com","subject":"Re: [PATCH 05/83] builtin/apply: extract line_by_line_fuzzy_match() from match_fragment()","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-26T16:15:05Z","receivedAt":"2016-04-26T16:15:05Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Mon, Apr 25, 2016 at 8:50 PM, Stefan Beller <sbeller@google.com> wrote:\n>> @@ -2251,7 +2319,7 @@ static int match_fragment(struct image *img,\n>>                           int match_beginning, int match_end)\n>>  {\n>>         int i;\n>> -       char *fixed_buf, *buf, *orig, *target;\n>> +       char *fixed_buf, *orig, *target;\n>>         struct strbuf fixed;\n>>         size_t fixed_len, postlen;\n>>         int preimage_limit;\n>> @@ -2312,6 +2380,7 @@ static int match_fragment(struct image *img,\n>>                  * There must be one non-blank context line that match\n>>                  * a line before the end of img.\n>>                  */\n>> +               char *buf;\n>\n> patches 1-4 looking good, with no comment from me. Here is the first spot to\n> comment on.\n>\n> It's not clear why we need to declare buf here? Oh wait it is. It's just\n> moved from the start of the function. But why do it in this patch?\n> It seems unrelated to the general intent of the patch. No need to reroll\n> for this nit alone, it just took me a while to figure out it was an unrelated\n> thing.\n\nYeah, I agree it's a bit unrelated. But rather than add another patch\nto an already long series just for this, I added the following to the\ncommit message:\n\nWhile at it, let's reduce the scope of \"char *buf\" in match_fragment().\n"},{"id":"284534","messageId":"CAP8UFD2rD5qo7TBPFdP5BifOGuWHAtZA2BNGDqnY_feoCfmKeQ@mail.gmail.com","threadId":"42127","inReplyTo":"CAGZ79kYUr9wQ--898OeyqVw_upqxfVNRsOveUAPSn=PtXZ4xBQ@mail.gmail.com","subject":"Re: [PATCH 09/83] builtin/apply: move 'check' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-26T16:26:31Z","receivedAt":"2016-04-26T16:26:31Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Mon, Apr 25, 2016 at 8:57 PM, Stefan Beller <sbeller@google.com> wrote:\n> On Sun, Apr 24, 2016 at 6:33 AM, Christian Couder\n> <christian.couder@gmail.com> wrote:\n>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n>> ---\n>>  builtin/apply.c | 16 +++++++++-------\n>>  1 file changed, 9 insertions(+), 7 deletions(-)\n>>\n>> diff --git a/builtin/apply.c b/builtin/apply.c\n>> index ad81210..6c628f6 100644\n>> --- a/builtin/apply.c\n>> +++ b/builtin/apply.c\n>> @@ -25,12 +25,15 @@ struct apply_state {\n>>         const char *prefix;\n>>         int prefix_length;\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> This is true, but at this part of the file/code we rather want to know what\n> `check` does, instead of what the command line option --check does.\n> (They are 2 different things, though one leading to the other one?) How about:\n>\n>     /*\n>      * Only check the files to be modified, but do not modify the files.\n>      */\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>\n> Oh I see it was moved from here. Not sure if we want to rename\n> comments along the way or just keep it in this series.\n\nI kept the existing comments when they were still relevant.\nIt could be a cleanup to change them to something like what you\nsuggest, but as it is not important for this series which is already\nlong, I prefer to leave it for now.\n"},{"id":"284535","messageId":"CAGZ79kauQU6B2EUcHyWck+ZP4G2OUAKrCkW3-PfsNZdijyF-TA@mail.gmail.com","threadId":"42127","inReplyTo":"CAP8UFD2rD5qo7TBPFdP5BifOGuWHAtZA2BNGDqnY_feoCfmKeQ@mail.gmail.com","subject":"Re: [PATCH 09/83] builtin/apply: move 'check' global into 'struct apply_state'","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-04-26T16:31:17Z","receivedAt":"2016-04-26T16:31:17Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"On Tue, Apr 26, 2016 at 9:26 AM, Christian Couder\n<christian.couder@gmail.com> wrote:\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>> Oh I see it was moved from here. Not sure if we want to rename\n>> comments along the way or just keep it in this series.\n>\n> I kept the existing comments when they were still relevant.\n> It could be a cleanup to change them to something like what you\n> suggest, but as it is not important for this series which is already\n> long, I prefer to leave it for now.\n\nYeah that was my conclusion as well after some thought.\n\nThanks,\nStefan\n"},{"id":"284538","messageId":"CAP8UFD09oT=FtP2sig9htR=C_36jWiH=t4nSD7VrWh_gkKbkDg@mail.gmail.com","threadId":"42127","inReplyTo":"CAGZ79kaCqfwgwngcqG5W0fe=SNOsp7nqtvWw=-xhZ60FBPpg+w@mail.gmail.com","subject":"Re: [PATCH 18/83] builtin/apply: move 'numstat' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-26T16:35:52Z","receivedAt":"2016-04-26T16:35:52Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Mon, Apr 25, 2016 at 11:40 PM, Stefan Beller <sbeller@google.com> wrote:\n> On Sun, Apr 24, 2016 at 6:33 AM, Christian Couder\n> <christian.couder@gmail.com> wrote:\n>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n>> ---\n>>  builtin/apply.c | 11 ++++++-----\n>>  1 file changed, 6 insertions(+), 5 deletions(-)\n>>\n>> diff --git a/builtin/apply.c b/builtin/apply.c\n>> index d90948a..16d78f9 100644\n>> --- a/builtin/apply.c\n>> +++ b/builtin/apply.c\n>> @@ -36,6 +36,9 @@ struct apply_state {\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>>         /*\n>>          *  --check turns on checking that the working tree matches the\n>>          *    files that are being modified, but doesn't apply the patch\n>> @@ -51,14 +54,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>> @@ -4500,7 +4501,7 @@ static int apply_patch(struct apply_state *state,\n>>         if (state->diffstat)\n>>                 stat_patch_list(list);\n>>\n>> -       if (numstat)\n>> +       if (state->numstat)\n>>                 numstat_patch_list(list);\n>>\n>>         if (summary)\n>> @@ -4598,7 +4599,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)\n>>                         N_(\"instead of applying the patch, output diffstat for the input\")),\n>>                 OPT_NOOP_NOARG(0, \"allow-binary-replacement\"),\n>>                 OPT_NOOP_NOARG(0, \"binary\"),\n>> -               OPT_BOOL(0, \"numstat\", &numstat,\n>> +               OPT_BOOL(0, \"numstat\", &state.numstat,\n>>                         N_(\"show number of added and deleted lines in decimal notation\")),\n>>                 OPT_BOOL(0, \"summary\", &summary,\n>>                         N_(\"instead of applying the patch, output a summary for the input\")),\n>> @@ -4675,7 +4676,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)\n>>         }\n>>         if (state.apply_with_reject)\n>>                 apply = state.apply_verbosely = 1;\n>> -       if (!force_apply && (state.diffstat || numstat || summary || state.check || fake_ancestor))\n>> +       if (!force_apply && (state.diffstat || state.numstat || summary || state.check || fake_ancestor))\n>\n> Mental note: This patch is just doing a mechanical conversion, so it\n> is fine to check for many \"state.FOOs\" here.\n>\n> However later we may want to move this out to a static oneliner like:\n>\n>     static int really_apply(state *s) {\n>       return s->diffstat || s->numstat || ...;\n>     }\n>\n> (with a better name obviously)\n\nYeah, this is another cleanup that could be done.\nI added it to a list and will try to take care of it later.\n"},{"id":"284581","messageId":"CAP8UFD0r-1yNwfxtU0jEK_QgCf4szCV1w_KWxqjBOCNDD42vdg@mail.gmail.com","threadId":"42127","inReplyTo":"CAGZ79kaaATDzOHFOmCAANcZmrF_5JGAUYW3Ub1WJ7vSLODVP_g@mail.gmail.com","subject":"Re: [PATCH 33/83] builtin/apply: move 'root' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-26T20:13:47Z","receivedAt":"2016-04-26T20:13:47Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Mon, Apr 25, 2016 at 11:50 PM, Stefan Beller <sbeller@google.com> wrote:\n> On Sun, Apr 24, 2016 at 6:33 AM, Christian Couder\n> <christian.couder@gmail.com> wrote:\n>\n>> @@ -4630,9 +4644,10 @@ static int option_parse_whitespace(const struct option *opt,\n>>  static int option_parse_directory(const struct option *opt,\n>>                                   const char *arg, int unset)\n>>  {\n>> -       strbuf_reset(&root);\n>> -       strbuf_addstr(&root, arg);\n>> -       strbuf_complete(&root, '/');\n>> +       struct apply_state *state = opt->value;\n>\n> Or even\n>\n>     struct strbuf root = ((state*)opt->value)->root;\n>\n> and then keep the next lines as is?\n\nI found it more coherent to have all the option_parse_*() functions do:\n\n    struct apply_state *state = opt->value;\n\nas their first instruction.\n"},{"id":"284582","messageId":"xmqqbn4wf7ft.fsf@gitster.mtv.corp.google.com","threadId":"42127","inReplyTo":"CAP8UFD228joDLyomvae7rVs02TuTbmEHerwWvH=VR+FE-YFLRA@mail.gmail.com","subject":"Re: [PATCH 05/83] builtin/apply: extract line_by_line_fuzzy_match() from match_fragment()","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-04-26T20:20:38Z","receivedAt":"2016-04-26T20:20:38Z","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>> It's not clear why we need to declare buf here? Oh wait it is. It's just\n>> moved from the start of the function. But why do it in this patch?\n>> It seems unrelated to the general intent of the patch. No need to reroll\n>> for this nit alone, it just took me a while to figure out it was an unrelated\n>> thing.\n>\n> Yeah, I agree it's a bit unrelated. But rather than add another patch\n> to an already long series just for this,...\n\nIsn't not doing the unrelated move at all a more sensible\nalternative, if that change does not deserve its own place in the\nseries after all?\n"},{"id":"284588","messageId":"xmqq7ffkf78f.fsf@gitster.mtv.corp.google.com","threadId":"42127","inReplyTo":"1461504863-15946-11-git-send-email-chriscool@tuxfamily.org","subject":"Re: [PATCH 10/83] builtin/apply: move 'check_index' global into 'struct apply_state'","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-04-26T20:25:04Z","receivedAt":"2016-04-26T20:25:04Z","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> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n> ---\n>  builtin/apply.c | 69 +++++++++++++++++++++++++++++++++------------------------\n>  1 file changed, 40 insertions(+), 29 deletions(-)\n>\n> diff --git a/builtin/apply.c b/builtin/apply.c\n> index 6c628f6..3f8671c 100644\n> --- a/builtin/apply.c\n> +++ b/builtin/apply.c\n> @@ -30,6 +30,10 @@ struct apply_state {\n>  \t *    files that are being modified, but doesn't apply the patch\n>  \t */\n>  \tint check;\n> +\n> +\t/* --index updates the cache as well. */\n> +\tint check_index;\n> +\n>  \tint unidiff_zero;\n>  };\n>  \n> @@ -37,14 +41,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\nI like the way this series moves only a few variables at a time to\nlimit the scope of each step.  I would have expected check-index and\ncached to touch pretty much the same codepaths (the latter would\ninvolve a subset of the codepaths involved for the former), but\ndoing them separately is fine.\n"},{"id":"284584","messageId":"xmqq37q8f748.fsf@gitster.mtv.corp.google.com","threadId":"42127","inReplyTo":"1461504863-15946-23-git-send-email-chriscool@tuxfamily.org","subject":"Re: [PATCH 22/83] builtin/apply: move 'unsafe_paths' global into 'struct apply_state'","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-04-26T20:27:35Z","receivedAt":"2016-04-26T20:27:35Z","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> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n> ---\n>  builtin/apply.c | 9 +++++----\n>  1 file changed, 5 insertions(+), 4 deletions(-)\n>\n> diff --git a/builtin/apply.c b/builtin/apply.c\n> index 506357c..c45e481 100644\n> --- a/builtin/apply.c\n> +++ b/builtin/apply.c\n> @@ -57,6 +57,8 @@ struct apply_state {\n>  \tint unidiff_zero;\n>  \n>  \tint update_index;\n> +\n> +\tint unsafe_paths;\n>  };\n\nHaving said \n\n    I like the way this series moves only a few variables at a time to\n    limit the scope of each step.\n\nit gets irritating to see all these unnecessary blank lines in the\nstructure definition added by each step, which would mean all of\nthese patches need to fix them in the next reroll.\n"},{"id":"284585","messageId":"xmqqy480dsi0.fsf@gitster.mtv.corp.google.com","threadId":"42127","inReplyTo":"1461504863-15946-28-git-send-email-chriscool@tuxfamily.org","subject":"Re: [PATCH 27/83] builtin/apply: move 'read_stdin' global into cmd_apply()","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-04-26T20:28:39Z","receivedAt":"2016-04-26T20:28:39Z","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> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n> ---\n>  builtin/apply.c | 2 +-\n>  1 file changed, 1 insertion(+), 1 deletion(-)\n\nMakes sense but do so immediately next to 06/83, \"options\" thing?\n\n> diff --git a/builtin/apply.c b/builtin/apply.c\n> index 699cabf..be237d1 100644\n> --- a/builtin/apply.c\n> +++ b/builtin/apply.c\n> @@ -96,7 +96,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> @@ -4586,6 +4585,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>  \tstruct apply_state state;\n>  \n>  \tconst char *whitespace_option = NULL;\n"},{"id":"284586","messageId":"xmqqtwiods5n.fsf@gitster.mtv.corp.google.com","threadId":"42127","inReplyTo":"1461504863-15946-40-git-send-email-chriscool@tuxfamily.org","subject":"Re: [PATCH 39/83] builtin/apply: move 'ws_error_action' into 'struct apply_state'","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-04-26T20:36:04Z","receivedAt":"2016-04-26T20:36:04Z","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> +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> @@ -80,6 +87,8 @@ struct apply_state {\n>  \tint whitespace_error;\n>  \tint squelch_whitespace_errors;\n>  \tint applied_after_fixing_ws;\n> +\n> +\tenum ws_error_action ws_error_action;\n>  };\n>  \n>  static int newfd = -1;\n> @@ -89,12 +98,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\nThis is a good example of a variable that needs initialization,\nwhich is turned into a field in apply_state that needs\ninitialization.  It is done here:\n\n> @@ -4738,6 +4743,7 @@ int cmd_apply(int argc, const char **argv, 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\nand we already have these random initial values described here.\n\nAs I do not expect that cmd_apply() which is a moral equivalent of\nmain() will stay to be the only one who wants to see a reasonably\ninitialized apply_state(), I think the patch that introduced the\nvery first version of \"struct apply_state\" should also introduce a\nhelper function to initialize it, i.e.\n\n\tstatic void init_apply_state(struct apply_state *s,\n        \t\t\t     const char *prefix)\n        {\n\t\tmemset(s, '\\0', sizeof(*s));\n                s->prefix = prefix;\n                s->prefix_length = s->prefix ? strlen(s->prefix) : 0;\n\t}\n\nin [PATCH 7/xx].\n"},{"id":"284587","messageId":"xmqqpotcdrxg.fsf@gitster.mtv.corp.google.com","threadId":"42127","inReplyTo":"xmqqtwiods5n.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH 39/83] builtin/apply: move 'ws_error_action' into 'struct apply_state'","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-04-26T20:40:59Z","receivedAt":"2016-04-26T20:40:59Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n> As I do not expect that cmd_apply() which is a moral equivalent of\n> main() will stay to be the only one who wants to see a reasonably\n> initialized apply_state(), I think the patch that introduced the\n> very first version of \"struct apply_state\" should also introduce a\n> helper function to initialize it, i.e.\n>\n> \tstatic void init_apply_state(struct apply_state *s,\n>         \t\t\t     const char *prefix)\n>         {\n> \t\tmemset(s, '\\0', sizeof(*s));\n>                 s->prefix = prefix;\n>                 s->prefix_length = s->prefix ? strlen(s->prefix) : 0;\n> \t}\n>\n> in [PATCH 7/xx].\n\nJust to avoid misunderstanding, I do not mean to say that the\ninit-apply-state helper that should have been introduced in 07/xx\nwould gain a new caller-supplied parameter ws_error_action.  This\nstep would have a patch to the function that does something like:\n\n static void init_apply_state(struct apply_state *s,\n                                 const char *prefix)\n {\n         memset(s, '\\0', sizeof(*s));\n         s->prefix = prefix;\n         s->prefix_length = s->prefix ? strlen(s->prefix) : 0;\n         ...\n+        s->ws_error_action = warn_on_ws_error;\n }\n\nwithout having the caller supply what error_action should the state\nbe initialized with.\n"},{"id":"284641","messageId":"CAP8UFD03d7DVnKDmzv0JbAv1j7=nPv=qCJgkKqNOt5JPL5+hMg@mail.gmail.com","threadId":"42127","inReplyTo":"CAPig+cTx6jGwS12c8j_O+kT-f06o3E_yxrWZXmeU-XQ7fUY_9w@mail.gmail.com","subject":"Re: [PATCH 45/83] builtin/apply: move 'state' init into init_apply_state()","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-27T12:00:33Z","receivedAt":"2016-04-27T12:00:33Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Mon, Apr 25, 2016 at 9:32 AM, Eric Sunshine <sunshine@sunshineco.com> wrote:\n> On Sun, Apr 24, 2016 at 9:33 AM, Christian Couder\n> <christian.couder@gmail.com> wrote:\n>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n>> ---\n>> diff --git a/builtin/apply.c b/builtin/apply.c\n>> @@ -4670,6 +4670,28 @@ static int option_parse_directory(const struct option *opt,\n>> +static void init_apply_state(struct apply_state *state, const char *prefix_)\n>> +{\n>> +       memset(state, 0, sizeof(*state));\n>> +       state->prefix = prefix_;\n>> +       state->prefix_length = state->prefix ? strlen(state->prefix) : 0;\n>> +       state->apply = 1;\n>> +       state->line_termination = '\\n';\n>> +       state->p_value = 1;\n>> +       state->p_context = UINT_MAX;\n>> +       state->squelch_whitespace_errors = 5;\n>> +       state->ws_error_action = warn_on_ws_error;\n>> +       state->ws_ignore_action = ignore_ws_none;\n>> +       state->linenr = 1;\n>> +       strbuf_init(&state->root, 0);\n>> +\n>> +       git_apply_config();\n>> +       if (apply_default_whitespace)\n>> +               parse_whitespace_option(state, apply_default_whitespace);\n>> +       if (apply_default_ignorewhitespace)\n>> +               parse_ignorewhitespace_option(state, apply_default_ignorewhitespace);\n>> +}\n>\n> Minor:\n>\n> If factoring out this code from cmd_apply() into init_apply_state()\n> was done as a preparatory patch before introduction of 'apply_state',\n> then each new 'state->foo=...' line would already be at its final\n> location when added by its respective patch.\n\nYeah I agree. So I did something like that.\nAnd by the way while doing it I saw that Junio also suggested a similar change.\n\nThe little difference with what you and Junio suggest is that the\npatch that creates init_apply_state() is just after the patch that\nintroduces 'struct apply_state'. I think it is a bit cleaner this way,\nas if I create init_apply_state() first, then I would have to change\nits arguments in the next patch that introduces 'struct apply_state'.\n\n> Doing so would also provide an obvious opportunity to name the\n> 'prefix' argument to init_apply_state() \"prefix\" rather than the odd\n> \"prefix_\".\n\nYeah, I did that too.\n\nThanks,\nChristian.\n"},{"id":"284642","messageId":"CAP8UFD0Xx4d=751yhaHpRKUgDQ1G2FkuCEziwdZpVMiJbjyaMg@mail.gmail.com","threadId":"42127","inReplyTo":"xmqq37q8f748.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH 22/83] builtin/apply: move 'unsafe_paths' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-27T12:14:40Z","receivedAt":"2016-04-27T12:14:40Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Tue, Apr 26, 2016 at 10:27 PM, Junio C Hamano <gitster@pobox.com> wrote:\n> Christian Couder <christian.couder@gmail.com> writes:\n>\n>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n>> ---\n>>  builtin/apply.c | 9 +++++----\n>>  1 file changed, 5 insertions(+), 4 deletions(-)\n>>\n>> diff --git a/builtin/apply.c b/builtin/apply.c\n>> index 506357c..c45e481 100644\n>> --- a/builtin/apply.c\n>> +++ b/builtin/apply.c\n>> @@ -57,6 +57,8 @@ struct apply_state {\n>>       int unidiff_zero;\n>>\n>>       int update_index;\n>> +\n>> +     int unsafe_paths;\n>>  };\n>\n> Having said\n>\n>     I like the way this series moves only a few variables at a time to\n>     limit the scope of each step.\n>\n> it gets irritating to see all these unnecessary blank lines in the\n> structure definition added by each step, which would mean all of\n> these patches need to fix them in the next reroll.\n\nThe reason I added some blank lines is that I moved comments that were\nall in one block at the top.\nI moved each comment near the related variables and sometimes a\ncomment is related to 2 variables, like in the extract below the\ncomment that starts with \"For \"diff-stat\" like behaviour,...\":\n\n------------------------------------------------------\n\n    /* --index updates the cache as well. */\n    int check_index;\n\n    int unidiff_zero;\n\n    int update_index;\n\n    int unsafe_paths;\n\n    int line_termination;\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    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\nIf I remove all the blank lines, I think it will make it harder to\nunderstand which comment belong to which variable(s).\n\nMaybe a compromise would be to just remove blank lines between the\nvariables that don't have any related comment like \"unidiff_zero\",\n\"update_index\", \"unsafe_paths\" and \"line_termination\" above.\n"},{"id":"284645","messageId":"CAP8UFD2kS8NihKQ4OtzOKNhjqBA-o-pNTA3CYpNGQ0ZfM8DWqA@mail.gmail.com","threadId":"42127","inReplyTo":"xmqqy480dsi0.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH 27/83] builtin/apply: move 'read_stdin' global into cmd_apply()","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-27T13:13:21Z","receivedAt":"2016-04-27T13:13:21Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Tue, Apr 26, 2016 at 10:28 PM, Junio C Hamano <gitster@pobox.com> wrote:\n> Christian Couder <christian.couder@gmail.com> writes:\n>\n>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n>> ---\n>>  builtin/apply.c | 2 +-\n>>  1 file changed, 1 insertion(+), 1 deletion(-)\n>\n> Makes sense but do so immediately next to 06/83, \"options\" thing?\n\nOk, in the next reroll this patch will be immediately next to 06/83.\n"},{"id":"284646","messageId":"CAP8UFD1nJXK1k2nv+i+Z-woUHFqcjf=u2_NdYyRxF7vTg26xgA@mail.gmail.com","threadId":"42127","inReplyTo":"xmqqtwiods5n.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH 39/83] builtin/apply: move 'ws_error_action' into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-27T13:23:26Z","receivedAt":"2016-04-27T13:23:26Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Tue, Apr 26, 2016 at 10:36 PM, Junio C Hamano <gitster@pobox.com> wrote:\n> Christian Couder <christian.couder@gmail.com> writes:\n>\n>> +enum ws_error_action {\n>> +     nowarn_ws_error,\n>> +     warn_on_ws_error,\n>> +     die_on_ws_error,\n>> +     correct_ws_error\n>> +};\n>> +\n>>  struct apply_state {\n>>       const char *prefix;\n>>       int prefix_length;\n>> @@ -80,6 +87,8 @@ struct apply_state {\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>>  };\n>>\n>>  static int newfd = -1;\n>> @@ -89,12 +98,6 @@ static const char * const apply_usage[] = {\n>>       NULL\n>>  };\n>>\n>> -static enum ws_error_action {\n>> -     nowarn_ws_error,\n>> -     warn_on_ws_error,\n>> -     die_on_ws_error,\n>> -     correct_ws_error\n>> -} ws_error_action = warn_on_ws_error;\n>\n> This is a good example of a variable that needs initialization,\n> which is turned into a field in apply_state that needs\n> initialization.  It is done here:\n>\n>> @@ -4738,6 +4743,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_)\n>>       state.p_value = 1;\n>>       state.p_context = UINT_MAX;\n>>       state.squelch_whitespace_errors = 5;\n>> +     state.ws_error_action = warn_on_ws_error;\n>>       strbuf_init(&state.root, 0);\n>\n> and we already have these random initial values described here.\n>\n> As I do not expect that cmd_apply() which is a moral equivalent of\n> main() will stay to be the only one who wants to see a reasonably\n> initialized apply_state(), I think the patch that introduced the\n> very first version of \"struct apply_state\" should also introduce a\n> helper function to initialize it, i.e.\n>\n>         static void init_apply_state(struct apply_state *s,\n>                                      const char *prefix)\n>         {\n>                 memset(s, '\\0', sizeof(*s));\n>                 s->prefix = prefix;\n>                 s->prefix_length = s->prefix ? strlen(s->prefix) : 0;\n>         }\n>\n> in [PATCH 7/xx].\n\nYeah, Eric also made nearly the same comment, so in the next reroll\nthe init_apply_state() is introduced in the patch immediately after\nthe patch that creates 'struct apply_state' (rather than much later in\nthe series). I prefer to not introduce it in the same patch as the\npatch that creates 'struct apply_state' is already quite big. (But if\nyou insist, yeah I will squash both patches together.)\n"},{"id":"284647","messageId":"CAP8UFD3SMRqX2=gNZteOe1A0+LT=nfG4HFg+Udtnobp1t+gBPA@mail.gmail.com","threadId":"42127","inReplyTo":"xmqqbn4wf7ft.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH 05/83] builtin/apply: extract line_by_line_fuzzy_match() from match_fragment()","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-27T13:31:49Z","receivedAt":"2016-04-27T13:31:49Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Tue, Apr 26, 2016 at 10:20 PM, Junio C Hamano <gitster@pobox.com> wrote:\n> Christian Couder <christian.couder@gmail.com> writes:\n>\n>>> It's not clear why we need to declare buf here? Oh wait it is. It's just\n>>> moved from the start of the function. But why do it in this patch?\n>>> It seems unrelated to the general intent of the patch. No need to reroll\n>>> for this nit alone, it just took me a while to figure out it was an unrelated\n>>> thing.\n>>\n>> Yeah, I agree it's a bit unrelated. But rather than add another patch\n>> to an already long series just for this,...\n>\n> Isn't not doing the unrelated move at all a more sensible\n> alternative, if that change does not deserve its own place in the\n> series after all?\n\nOk, I removed the unrelated move.\n"},{"id":"284655","messageId":"xmqqa8kfax8x.fsf@gitster.mtv.corp.google.com","threadId":"42127","inReplyTo":"CAP8UFD0Xx4d=751yhaHpRKUgDQ1G2FkuCEziwdZpVMiJbjyaMg@mail.gmail.com","subject":"Re: [PATCH 22/83] builtin/apply: move 'unsafe_paths' global into 'struct apply_state'","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-04-27T15:26:38Z","receivedAt":"2016-04-27T15:26:38Z","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> The reason I added some blank lines is that I moved comments that were\n> all in one block at the top.\n> I moved each comment near the related variables and sometimes a\n> comment is related to 2 variables, like in the extract below the\n> comment that starts with \"For \"diff-stat\" like behaviour,...\":\n\nDon't.\n\nIf you have four things there, and two are related, you would do\n\n    /* used for --distim option */\n    int distim_weight;\n    int distim_target;\n\n    int unrelated;\n\n    int another;\n\nAnd the first two are shown clearly to be related and unrelated to\nthe other ones.  That sound nice in theory, but think what you would\ndo when you want to comment further on one of the related ones.  You\nwould likely do this:\n\n    /* used for --distim option */\n    int distim_weight;\n   -int distim_target;\n   +int distim_target; /* which dosh to distim? */\n\n    int unrelated;\n\nFor the same reason, if you are to comment on one of the ungrouped\nthings, you would do this:\n\n\n   -int unrelated;\n   +int unrelated; /* this is not used for --distim */\n\n    int another;\n\nNotice that a blank between unrelated and another is not helping\nanything?  There is no reason to keep blanks in between each and\nevery unrelated things.\n\nSee how \"struct diff_options\" is done for a better example.  A blank\nline every once in a while that roups related things together is\ngood.  Blindly applying it to all groups of one defeats the benefit\nby making the result harder to read.\n"},{"id":"284658","messageId":"CAP8UFD0Q3USKhXMZrxTxQc0hr3kjBP517_KBqc7CS--4eTbJww@mail.gmail.com","threadId":"42127","inReplyTo":"CAGZ79kZ4UprHxtmO-rgiOo=od0U2nraq6m6wYcm_g87bsrL-Wg@mail.gmail.com","subject":"Re: [PATCH 47/83] builtin/apply: move applying patches into apply_all_patches()","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-27T15:51:33Z","receivedAt":"2016-04-27T15:51:33Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Tue, Apr 26, 2016 at 12:00 AM, Stefan Beller <sbeller@google.com> wrote:\n> On Sun, Apr 24, 2016 at 6:33 AM, Christian Couder\n> <christian.couder@gmail.com> wrote:\n>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n>\n> Up to this patch, have a\n> Reviewed-by: Stefan Beller <sbeller@google.com>\n> in case you want to split the series in here (as indicated in the\n> cover letter, this was the last\n> patch rerolled, the next patches are new and may need more discussion).\n>\n> I had some nits, but they cleared up in later patches.\n\nThanks for your review! I will add your Reviewed-by in the next reroll.\n"},{"id":"284665","messageId":"xmqqtwin9fus.fsf@gitster.mtv.corp.google.com","threadId":"42127","inReplyTo":"1461504863-15946-4-git-send-email-chriscool@tuxfamily.org","subject":"Re: [PATCH 03/83] builtin/apply: avoid parameter shadowing 'linenr' global","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-04-27T16:27:39Z","receivedAt":"2016-04-27T16:27:39Z","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> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n> ---\n\nI think 02/83 that renamed the global-to-be-moved-to-state to\nstate_p_value was brilliant, and this should follow suit; you would\nbe moving linenr into the state eventually in later steps, right?\n\n>  builtin/apply.c | 4 ++--\n>  1 file changed, 2 insertions(+), 2 deletions(-)\n>\n> diff --git a/builtin/apply.c b/builtin/apply.c\n> index e133b38..7115dc2 100644\n> --- a/builtin/apply.c\n> +++ b/builtin/apply.c\n> @@ -1516,7 +1516,7 @@ static int find_header(const char *line, unsigned long size, int *hdrsize, struc\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(unsigned result, const char *line, int len, int l_nr)\n>  {\n>  \tchar *err;\n>  \n> @@ -1530,7 +1530,7 @@ 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\tpatch_input_file, l_nr, err, len, line);\n>  \tfree(err);\n>  }\n"},{"id":"284696","messageId":"CAP8UFD09L3i0RjghK7O8xQOK-_PfKZesUC_piUukrBOMDOJxbg@mail.gmail.com","threadId":"42127","inReplyTo":"xmqqtwin9fus.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH 03/83] builtin/apply: avoid parameter shadowing 'linenr' global","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-27T17:07:39Z","receivedAt":"2016-04-27T17:07:39Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Wed, Apr 27, 2016 at 6:27 PM, Junio C Hamano <gitster@pobox.com> wrote:\n>\n> I think 02/83 that renamed the global-to-be-moved-to-state to\n> state_p_value was brilliant, and this should follow suit; you would\n> be moving linenr into the state eventually in later steps, right?\n\nYeah, ok, I will do the same thing to this patch as I did to 02/83.\n\nThanks.\n"},{"id":"284702","messageId":"CAPig+cRw7_6hbLZ7HfvCHwy7UcujnfOTToMSXaRbx30WTXvaQA@mail.gmail.com","threadId":"42127","inReplyTo":"1461504863-15946-54-git-send-email-chriscool@tuxfamily.org","subject":"Re: [PATCH 53/83] builtin/apply: make find_header() return -1 instead of die()ing","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2016-04-27T18:08:48Z","receivedAt":"2016-04-27T18:08:48Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Sun, Apr 24, 2016 at 9:33 AM, Christian Couder\n<christian.couder@gmail.com> wrote:\n> To be compatible with the rest of the error handling in builtin/apply.c,\n> find_header() should return -1 instead of calling die().\n>\n> Unfortunately find_header() already returns -1 when no header is found,\n> so let's make it return -2 instead in this case.\n>\n> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n> ---\n> diff --git a/builtin/apply.c b/builtin/apply.c\n> @@ -1588,18 +1596,18 @@ static int find_header(struct apply_state *state,\n>                                 continue;\n>                         if (!patch->old_name && !patch->new_name) {\n>                                 if (!patch->def_name)\n> -                                       die(Q_(\"git diff header lacks filename information when removing \"\n> -                                              \"%d leading pathname component (line %d)\",\n> -                                              \"git diff header lacks filename information when removing \"\n> -                                              \"%d leading pathname components (line %d)\",\n> -                                              state->p_value),\n> -                                           state->p_value, state->linenr);\n> +                                       return error(Q_(\"git diff header lacks filename information when removing \"\n> +                                                       \"%d leading pathname component (line %d)\",\n> +                                                       \"git diff header lacks filename information when removing \"\n> +                                                       \"%d leading pathname components (line %d)\",\n> +                                                       state->p_value),\n> +                                                    state->p_value, state->linenr);\n>                                 patch->old_name = xstrdup(patch->def_name);\n>                                 patch->new_name = xstrdup(patch->def_name);\n>                         }\n>                         if (!patch->is_delete && !patch->new_name)\n> -                               die(\"git diff header lacks filename information \"\n> -                                   \"(line %d)\", state->linenr);\n> +                               return error(\"git diff header lacks filename information \"\n> +                                            \"(line %d)\", state->linenr);\n\nI realize that the caller in this patch currently just die()'s, and I\nhaven't looked at subsequent patches yet, but is this new 'return'\ngoing to cause the caller to start leaking patch->old_name and\npatch->new_name which are xstrdup()'d just above?\n\n>                         patch->is_toplevel_relative = 1;\n>                         *hdrsize = git_hdr_len;\n>                         return offset;\n> @@ -2115,6 +2123,9 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si\n>         int hdrsize, patchsize;\n>         int offset = find_header(state, buffer, size, &hdrsize, patch);\n>\n> +       if (offset == -1)\n> +               exit(1);\n> +\n>         if (offset < 0)\n>                 return offset;\n"},{"id":"284703","messageId":"CAPig+cSdRL3hc81pXXFKAZWisGYPHcd47Z4BBhkeSWZ=5_FT9A@mail.gmail.com","threadId":"42127","inReplyTo":"CACsJy8B7g8WgWbr4M4LHFyBshN2gi30pL1bCcZcTPB7qtmnWdQ@mail.gmail.com","subject":"Re: [PATCH 53/83] builtin/apply: make find_header() return -1 instead of die()ing","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2016-04-27T18:10:41Z","receivedAt":"2016-04-27T18:10:41Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Mon, Apr 25, 2016 at 9:18 AM, Duy Nguyen <pclouds@gmail.com> wrote:\n> On Sun, Apr 24, 2016 at 8:33 PM, Christian Couder\n> <christian.couder@gmail.com> wrote:\n>> To be compatible with the rest of the error handling in builtin/apply.c,\n>> find_header() should return -1 instead of calling die().\n>>\n>> Unfortunately find_header() already returns -1 when no header is found,\n>> so let's make it return -2 instead in this case.\n>\n> I don't think this is a good way to go. Too many magic numbers. I\n> don't have a better option though. Maybe returning names instead of\n> numbers would help a bit.\n\nI suppose 'hdrsize' could signal this extra condition. For instance,\nalways return -1 on error, and set hdrsize to 0 for header not found\n(unless 0 is a valid size?), and -1 for any other error.\n\nBut perhaps that's getting too clever...\n"},{"id":"284837","messageId":"CAP8UFD1=w2D-5q9bWrYqzL3v1q7fYi9imy1UhG2OaW2vB=2ECA@mail.gmail.com","threadId":"42127","inReplyTo":"xmqqzishlgj1.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH 49/83] builtin/apply: move 'lock_file' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-28T16:30:53Z","receivedAt":"2016-04-28T16:30:53Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Mon, Apr 25, 2016 at 7:55 PM, Junio C Hamano <gitster@pobox.com> wrote:\n> Eric Sunshine <sunshine@sunshineco.com> writes:\n>\n>>> +       /*\n>>> +        * Since lockfile.c keeps a linked list of all created\n>>> +        * lock_file structures, it isn't safe to free(lock_file).\n>>> +        */\n>>> +       struct lock_file *lock_file;\n>>\n>> Is there ever a time when lock_file is removed from the list (such as\n>> upon successful write of the index), in which case it would be safe to\n>> free() this?\n>\n> I do not think you need to think about \"free\"ing.\n\nYeah, lockfile.h says:\n\n * * Automatic cruft removal. If the program exits after we lock a\n *   file but before the changes have been committed, we want to make\n *   sure that we remove the lockfile. This is done by remembering the\n *   lockfiles we have created in a linked list and setting up an\n *   `atexit(3)` handler and a signal handler that clean up the\n *   lockfiles. This mechanism ensures that outstanding lockfiles are\n *   cleaned up if the program exits (including when `die()` is\n *   called) or if the program is terminated by a signal.\n\nand:\n\n * The caller:\n *\n * * Allocates a `struct lock_file` either as a static variable or on\n *   the heap, initialized to zeros. Once you use the structure to\n *   call the `hold_lock_file_for_*()` family of functions, it belongs\n *   to the lockfile subsystem and its storage must remain valid\n *   throughout the life of the program (i.e. you cannot use an\n *   on-stack variable to hold this structure).\n\n> Even if the libified version of the apply internal can be called\n> multiple times to process multiple patch inputs, there is no need to\n> run multiple instances of it yet.  And a lockfile, after the caller\n> finishes interacting with one file using it by calling commit or\n> rollback, can be reused to interact with another file.\n>\n> So the cleanest would be to:\n>\n>  * make the caller of apply API responsible for preparing a static\n>    or (allocating and leaking) lockfile instance,\n\nOk.\n\n>  * make it pass a pointer to the lockfile to the apply API so that\n>    it can store it in apply_state, and\n\nOk, I will add a new \"struct lock_file *\" parameter to\ninit_apply_state(), for the caller of the apply API to do that.\nThough I think it should be ok for the caller to pass a NULL pointer\nand in this case have init_apply_state() allocate the lockfile file\ninstance.\n\n>  * have the caller use apply API feeding one patch at a time in a\n>    loop, allowing the same lockfile instance used for each\n>    \"invocation\".\n\nOk, the same lockfile instance will be used for each invocation.\n\n> I sounded as if I were advocating non-reentrant implementation in\n> the introductory paragraph, but that is not the intention.  If you\n> want to do N threads, you would prepare N lockfile instances, give\n> them to N apply API instances to be stored in N apply_state\n> instances, and you would have N parallel applications, if you wanted\n> to.\n\nThanks,\nChristian.\n"},{"id":"284872","messageId":"xmqq1t5p4he4.fsf@gitster.mtv.corp.google.com","threadId":"42127","inReplyTo":"CAP8UFD1=w2D-5q9bWrYqzL3v1q7fYi9imy1UhG2OaW2vB=2ECA@mail.gmail.com","subject":"Re: [PATCH 49/83] builtin/apply: move 'lock_file' global into 'struct apply_state'","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-04-28T20:17:55Z","receivedAt":"2016-04-28T20:17:55Z","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>> I do not think you need to think about \"free\"ing.\n>\n> Yeah, lockfile.h says:\n> ...\n> and:\n> ...\n\nYup, we are now on the same page.\n\n>> Even if the libified version of the apply internal can be called\n>> multiple times to process multiple patch inputs, there is no need to\n>> run multiple instances of it yet.  And a lockfile, after the caller\n>> finishes interacting with one file using it by calling commit or\n>> rollback, can be reused to interact with another file.\n\nlockfile.h says this about the above paragraph, which is a more\nimportant part ;-)\n\n * When finished writing, the caller can:\n * ...\n * Even after the lockfile is committed or rolled back, the\n * `lock_file` object must not be freed or altered by the caller.\n * However, it may be reused; just pass it to another call of\n * `hold_lock_file_for_update()`.\n"},{"id":"285047","messageId":"CAP8UFD1bDj2bnRSpo3Gp5B4Du6fNGSUfXv6dLL6Cs4KsJTw5Zg@mail.gmail.com","threadId":"42127","inReplyTo":"CAPig+cRSe8oOjo2h6SuJQyD+he_Q7zHfF4TivZ0amhAu4HLQ+g@mail.gmail.com","subject":"Re: [PATCH 49/83] builtin/apply: move 'lock_file' global into 'struct apply_state'","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-30T19:39:19Z","receivedAt":"2016-04-30T19:39:19Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Mon, Apr 25, 2016 at 9:50 AM, Eric Sunshine <sunshine@sunshineco.com> wrote:\n>> @@ -4515,8 +4521,6 @@ static int write_out_results(struct apply_state *state, struct patch *list)\n>>         return errs;\n>>  }\n>>\n>> -static struct lock_file lock_file;\n>\n> Does the static lock_file in build_fake_ancestor() deserve the same\n> sort of treatment? (I haven't traced the code enough to answer this.)\n\nMaybe yes we could do the same thing for this static lock_file, but\nthis can be done later, and it could be a bit involved, so I prefer to\nnot touch that for now.\n\nWe are using the lock_file like this in build_fake_ancestor():\n\n    hold_lock_file_for_update(&lock, filename, LOCK_DIE_ON_ERROR);\n    if (write_locked_index(&result, &lock, COMMIT_LOCK))\n        return error(\"Could not write temporary index to %s\", filename);\n\nso it looks like it is safe to call build_fake_ancestor() many times\nas long as it is not called by different threads.\n"},{"id":"285048","messageId":"CAP8UFD2SzSXWSH8uB2iJ8-QpkYhXDNGbSks4Ua3TLrVH0a7Acw@mail.gmail.com","threadId":"42127","inReplyTo":"CAPig+cQTx3wyk0E-ZBaBYbjsXRqHPg1f6snZKGjWkrTq=fb6Vg@mail.gmail.com","subject":"Re: [PATCH 51/83] builtin/apply: make apply_patch() return -1 instead of die()ing","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-04-30T19:41:07Z","receivedAt":"2016-04-30T19:41:07Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Tue, Apr 26, 2016 at 3:20 AM, Eric Sunshine <sunshine@sunshineco.com> wrote:\n> On Sun, Apr 24, 2016 at 9:33 AM, Christian Couder\n> <christian.couder@gmail.com> wrote:\n>> To libify `git apply` functionality we have to signal errors\n>> to the caller instead of die()ing.\n>>\n>> As a first step in this direction, let's make apply_patch() return\n>> -1 in case of errors instead of dying. For now its only caller\n>> apply_all_patches() will exit(1) when apply_patch() return -1.\n>>\n>> In a later patch, apply_all_patches() will return -1 too instead of\n>> exiting.\n>>\n>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n>> ---\n>> diff --git a/builtin/apply.c b/builtin/apply.c\n>> @@ -4522,6 +4522,14 @@ static int write_out_results(struct apply_state *state, struct patch *list)\n>>  static int apply_patch(struct apply_state *state,\n>>                        int fd,\n>>                        const char *filename,\n>> @@ -4564,7 +4572,7 @@ static int apply_patch(struct apply_state *state,\n>>         }\n>>\n>>         if (!list && !skipped_patch)\n>> -               die(_(\"unrecognized input\"));\n>> +               return error(_(\"unrecognized input\"));\n>>\n>>         if (state->whitespace_error && (state->ws_error_action == die_on_ws_error))\n>>                 state->apply = 0;\n>> @@ -4575,19 +4583,17 @@ static int apply_patch(struct apply_state *state,\n>>                 hold_locked_index(state->lock_file, 1);\n>>         }\n>>\n>> -       if (state->check_index) {\n>> -               if (read_cache() < 0)\n>> -                       die(_(\"unable to read index file\"));\n>> -       }\n>> +       if (state->check_index && read_cache() < 0)\n>> +               return error(_(\"unable to read index file\"));\n>>\n>>         if ((state->check || state->apply) &&\n>>             check_patch_list(state, list) < 0 &&\n>>             !state->apply_with_reject)\n>> -               exit(1);\n>> +               return -1;\n>>\n>>         if (state->apply && write_out_results(state, list)) {\n>>                 if (state->apply_with_reject)\n>> -                       exit(1);\n>> +                       return -1;\n>>                 /* with --3way, we still need to write the index out */\n>>                 return 1;\n>>         }\n>\n> Are these new 'returns' leaking 'list', 'buf', and 'fn_table' which\n> otherwise get released at the end of the function?\n\nYeah, you are right, I will fix that. Thanks.\n"},{"id":"285118","messageId":"CAP8UFD0Sv4T57Qt5HnWRisAir==Urs2yr=gzTEvGDvkCpdrNng@mail.gmail.com","threadId":"42127","inReplyTo":"CAPig+cRw7_6hbLZ7HfvCHwy7UcujnfOTToMSXaRbx30WTXvaQA@mail.gmail.com","subject":"Re: [PATCH 53/83] builtin/apply: make find_header() return -1 instead of die()ing","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-01T16:45:02Z","receivedAt":"2016-05-01T16:45:02Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Wed, Apr 27, 2016 at 8:08 PM, Eric Sunshine <sunshine@sunshineco.com> wrote:\n> On Sun, Apr 24, 2016 at 9:33 AM, Christian Couder\n> <christian.couder@gmail.com> wrote:\n>> To be compatible with the rest of the error handling in builtin/apply.c,\n>> find_header() should return -1 instead of calling die().\n>>\n>> Unfortunately find_header() already returns -1 when no header is found,\n>> so let's make it return -2 instead in this case.\n>>\n>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n>> ---\n>> diff --git a/builtin/apply.c b/builtin/apply.c\n>> @@ -1588,18 +1596,18 @@ static int find_header(struct apply_state *state,\n>>                                 continue;\n>>                         if (!patch->old_name && !patch->new_name) {\n>>                                 if (!patch->def_name)\n>> -                                       die(Q_(\"git diff header lacks filename information when removing \"\n>> -                                              \"%d leading pathname component (line %d)\",\n>> -                                              \"git diff header lacks filename information when removing \"\n>> -                                              \"%d leading pathname components (line %d)\",\n>> -                                              state->p_value),\n>> -                                           state->p_value, state->linenr);\n>> +                                       return error(Q_(\"git diff header lacks filename information when removing \"\n>> +                                                       \"%d leading pathname component (line %d)\",\n>> +                                                       \"git diff header lacks filename information when removing \"\n>> +                                                       \"%d leading pathname components (line %d)\",\n>> +                                                       state->p_value),\n>> +                                                    state->p_value, state->linenr);\n>>                                 patch->old_name = xstrdup(patch->def_name);\n>>                                 patch->new_name = xstrdup(patch->def_name);\n>>                         }\n>>                         if (!patch->is_delete && !patch->new_name)\n>> -                               die(\"git diff header lacks filename information \"\n>> -                                   \"(line %d)\", state->linenr);\n>> +                               return error(\"git diff header lacks filename information \"\n>> +                                            \"(line %d)\", state->linenr);\n>\n> I realize that the caller in this patch currently just die()'s, and I\n> haven't looked at subsequent patches yet, but is this new 'return'\n> going to cause the caller to start leaking patch->old_name and\n> patch->new_name which are xstrdup()'d just above?\n\nI think it is ok because find_header() is called only from\nparse_chunk() which is only called by apply_patch() and apply_patch()\ncalls \"free_patch(patch)\" when parse_chunk() returns a negative\ninteger.\nAnd free_patch() frees old_name and new_name.\n"},{"id":"285119","messageId":"CAP8UFD2AKOuvGesLHBEo5gkUYQ0N_QvLTmqBUXCFsWBn2JNsbg@mail.gmail.com","threadId":"42127","inReplyTo":"CAPig+cSdRL3hc81pXXFKAZWisGYPHcd47Z4BBhkeSWZ=5_FT9A@mail.gmail.com","subject":"Re: [PATCH 53/83] builtin/apply: make find_header() return -1 instead of die()ing","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-01T16:53:08Z","receivedAt":"2016-05-01T16:53:08Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Wed, Apr 27, 2016 at 8:10 PM, Eric Sunshine <sunshine@sunshineco.com> wrote:\n> On Mon, Apr 25, 2016 at 9:18 AM, Duy Nguyen <pclouds@gmail.com> wrote:\n>> On Sun, Apr 24, 2016 at 8:33 PM, Christian Couder\n>> <christian.couder@gmail.com> wrote:\n>>> To be compatible with the rest of the error handling in builtin/apply.c,\n>>> find_header() should return -1 instead of calling die().\n>>>\n>>> Unfortunately find_header() already returns -1 when no header is found,\n>>> so let's make it return -2 instead in this case.\n>>\n>> I don't think this is a good way to go. Too many magic numbers. I\n>> don't have a better option though. Maybe returning names instead of\n>> numbers would help a bit.\n>\n> I suppose 'hdrsize' could signal this extra condition. For instance,\n> always return -1 on error, and set hdrsize to 0 for header not found\n> (unless 0 is a valid size?), and -1 for any other error.\n>\n> But perhaps that's getting too clever...\n\nYeah, I don't think it would make reading the patch simpler.\n"},{"id":"285120","messageId":"CAP8UFD3CPwDR_P23MvmBVEb24NSd93foDaNion57D7Qazf377g@mail.gmail.com","threadId":"42127","inReplyTo":"CACsJy8AVVNOPfuAqSKKP-V2hTxHM-iYagXXa3j88GsEt3t7Ckw@mail.gmail.com","subject":"Re: [PATCH 61/83] builtin/apply: libify check_apply_state()","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-01T16:58:54Z","receivedAt":"2016-05-01T16:58:54Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Mon, Apr 25, 2016 at 3:26 PM, Duy Nguyen <pclouds@gmail.com> wrote:\n> On Sun, Apr 24, 2016 at 8:34 PM, Christian Couder\n> <christian.couder@gmail.com> wrote:\n>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n>> ---\n>>  builtin/apply.c | 16 +++++++++-------\n>>  1 file changed, 9 insertions(+), 7 deletions(-)\n>>\n>> diff --git a/builtin/apply.c b/builtin/apply.c\n>> index e3ee199..7576ec5 100644\n>> --- a/builtin/apply.c\n>> +++ b/builtin/apply.c\n>> @@ -4534,17 +4534,17 @@ static int option_parse_directory(const struct option *opt,\n>>         return 0;\n>>  }\n>>\n>> -static void check_apply_state(struct apply_state *state, int force_apply)\n>> +static int check_apply_state(struct apply_state *state, int force_apply)\n>>  {\n>>         int is_not_gitdir = !startup_info->have_repository;\n>>\n>>         if (state->apply_with_reject && state->threeway)\n>> -               die(\"--reject and --3way cannot be used together.\");\n>> +               return error(\"--reject and --3way cannot be used together.\");\n>>         if (state->cached && state->threeway)\n>> -               die(\"--cached and --3way cannot be used together.\");\n>> +               return error(\"--cached and --3way cannot be used together.\");\n>\n> Sweet opportunity to _() these messages since it clear shows in this\n> patch that other messages of the same category are _()'d in this\n> function. Could be done as a separate patch. But I'm also ok if you\n> say \"no\".\n\nI have added it to my todo list of things that should be done after this series.\n"},{"id":"285125","messageId":"CAP8UFD3c6YHi33kwu=yB60oRqv91GLDm7J4zb5nxcVi6XNJo9A@mail.gmail.com","threadId":"42127","inReplyTo":"CACsJy8CRiAhHqYDrJifhfarqzarEnuuJm4rw18zxnC9xaBwKNw@mail.gmail.com","subject":"Re: [PATCH 63/83] builtin/apply: make apply_all_patches() return -1 on error","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-01T18:32:31Z","receivedAt":"2016-05-01T18:32:31Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Mon, Apr 25, 2016 at 3:30 PM, Duy Nguyen <pclouds@gmail.com> wrote:\n> On Sun, Apr 24, 2016 at 8:34 PM, Christian Couder\n> <christian.couder@gmail.com> wrote:\n>>         if (state->update_index) {\n>>                 if (write_locked_index(&the_index, state->lock_file, COMMIT_LOCK))\n>> -                       die(_(\"Unable to write new index file\"));\n>> +                       return error(_(\"Unable to write new index file\"));\n>>         }\n>>\n>>         return !!errs;\n>> @@ -4698,5 +4698,8 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n>>         if (check_apply_state(&state, force_apply))\n>>                 exit(1);\n>>\n>> -       return apply_all_patches(&state, argc, argv, options);\n>> +       if (apply_all_patches(&state, argc, argv, options))\n>> +               exit(1);\n>\n> Abusing exit() too much? This is cmd_apply(). A return should be\n> enough and you can do the !! trick that is used in\n> apply_all_patches(), shown just a little bit above.\n\nOk, I will use:\n\n    return !!apply_all_patches(&state, argc, argv, options);\n\nThanks,\nChristian.\n"},{"id":"285129","messageId":"CAPig+cQbF_Xq2oB5ALQkj_Zdw8VR8kyVyinq_K24zGGK7Xundw@mail.gmail.com","threadId":"42127","inReplyTo":"1461504863-15946-55-git-send-email-chriscool@tuxfamily.org","subject":"Re: [PATCH 54/83] builtin/apply: make parse_chunk() return a negative integer on error","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2016-05-01T19:04:28Z","receivedAt":"2016-05-01T19:04:28Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Sun, Apr 24, 2016 at 9:33 AM, Christian Couder\n<christian.couder@gmail.com> wrote:\n> This negative number can be -2 if no patch header has been found,\n> otherwise it is -1.\n>\n> As parse_chunk() is called only by apply_patch() which already\n> returns -1 when an error happened, let's make it return -1 when\n> parse_chunk() returns -1.\n>\n> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n> ---\n> diff --git a/builtin/apply.c b/builtin/apply.c\n> @@ -4566,6 +4567,8 @@ static int apply_patch(struct apply_state *state,\n>                 nr = parse_chunk(state, buf.buf + offset, buf.len - offset, patch);\n>                 if (nr < 0) {\n>                         free_patch(patch);\n> +                       if (nr == -1)\n> +                               return -1;\n\nSame comment as 51/83 about this leaking 'list', 'buf', and 'fn_table'.\n\n>                         break;\n>                 }\n>                 if (state->apply_in_reverse)\n"},{"id":"285131","messageId":"CAP8UFD38YhMkqYLxLrSA4oi2D=3mjo+grJ00K-5LPxpH3rNBdA@mail.gmail.com","threadId":"42127","inReplyTo":"CACsJy8ATSuijr9beSuJ7X51hsmFVCs7TKEYd3puoLPMtVjKZ9g@mail.gmail.com","subject":"Re: [PATCH 65/83] builtin/apply: make gitdiff_verify_name() return -1 on error","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-01T19:31:12Z","receivedAt":"2016-05-01T19:31:12Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Mon, Apr 25, 2016 at 3:36 PM, Duy Nguyen <pclouds@gmail.com> wrote:\n> On Sun, Apr 24, 2016 at 8:34 PM, Christian Couder\n> <christian.couder@gmail.com> wrote:\n>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n>> ---\n>>  builtin/apply.c | 52 ++++++++++++++++++++++++++++++----------------------\n>>  1 file changed, 30 insertions(+), 22 deletions(-)\n>>\n>> diff --git a/builtin/apply.c b/builtin/apply.c\n>> index 6b8ba2a..268356b 100644\n>> --- a/builtin/apply.c\n>> +++ b/builtin/apply.c\n>> @@ -812,7 +812,7 @@ static int gitdiff_hdrend(struct apply_state *state,\n>>                           const char *line,\n>>                           struct patch *patch)\n>>  {\n>> -       return -1;\n>> +       return 1;\n>>  }\n>\n> These are in a function group that will be called as p->fn() in\n> parse_git_header(). Is it possible to isolate them in a separate\n> patch? This patch can follow after, which covers only\n> gitdiff_verify_name() returning -1 and its call sites.\n\nYeah, I had planned to do something like that when developing the\nseries but I forgot.\n\nThanks,\nChristian.\n"},{"id":"285132","messageId":"CAPig+cSTf+9GJ9r4VZ9A98vQyyzqdGQFbihw2aAtB9sHM4tFCQ@mail.gmail.com","threadId":"42127","inReplyTo":"1461504863-15946-56-git-send-email-chriscool@tuxfamily.org","subject":"Re: [PATCH 55/83] builtin/apply: make parse_single_patch() return -1 on error","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2016-05-01T19:32:28Z","receivedAt":"2016-05-01T19:32:28Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Sun, Apr 24, 2016 at 9:33 AM, Christian Couder\n<christian.couder@gmail.com> wrote:\n> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n> ---\n> diff --git a/builtin/apply.c b/builtin/apply.c\n> @@ -1802,8 +1806,10 @@ static int parse_single_patch(struct apply_state *state,\n>                 fragment = xcalloc(1, sizeof(*fragment));\n>                 fragment->linenr = state->linenr;\n>                 len = parse_fragment(state, line, size, patch, fragment);\n> -               if (len <= 0)\n> -                       die(_(\"corrupt patch at line %d\"), state->linenr);\n> +               if (len <= 0) {\n> +                       free(fragment);\n> +                       return error(_(\"corrupt patch at line %d\"), state->linenr);\n\nNo fragment leak here, okay.\n\n> +               }\n>                 fragment->patch = line;\n>                 fragment->size = len;\n>                 oldlines += fragment->oldlines;\n> @@ -1839,9 +1845,9 @@ static int parse_single_patch(struct apply_state *state,\n>                 patch->is_delete = 0;\n>\n>         if (0 < patch->is_new && oldlines)\n> -               die(_(\"new file %s depends on old contents\"), patch->new_name);\n> +               return error(_(\"new file %s depends on old contents\"), patch->new_name);\n>         if (0 < patch->is_delete && newlines)\n> -               die(_(\"deleted file %s still has contents\"), patch->old_name);\n> +               return error(_(\"deleted file %s still has contents\"), patch->old_name);\n\nThese two return -1 and don't manually release the fragments, which\nmight seem like a leak, however...\n\n>         if (!patch->is_delete && !newlines && context)\n>                 fprintf_ln(stderr,\n>                            _(\"** warning: \"\n> @@ -2143,6 +2149,9 @@ static int parse_chunk(struct apply_state *state, char *buffer, unsigned long si\n>                                        size - offset - hdrsize,\n>                                        patch);\n>\n> +       if (patchsize < 0)\n> +               return -1;\n\nThis forwards -1 to its caller, and the caller, apply_patch() does\nrelease the fragments when it sees -1, so there does not seem to be\nany (new) leak. Good.\n"},{"id":"285133","messageId":"CAPig+cTxZf=oMB=V5JoZNUO6eTtnabv7LinNo1LUeah6ofrbEA@mail.gmail.com","threadId":"42127","inReplyTo":"1461504863-15946-60-git-send-email-chriscool@tuxfamily.org","subject":"Re: [PATCH 59/83] builtin/apply: move init_apply_state() to apply.c","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2016-05-01T19:37:49Z","receivedAt":"2016-05-01T19:37:49Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Sun, Apr 24, 2016 at 9:33 AM, Christian Couder\n<christian.couder@gmail.com> wrote:\n> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n> ---\n> diff --git a/apply.c b/apply.c\n> @@ -0,0 +1,80 @@\n> +#include \"cache.h\"\n> +#include \"apply.h\"\n> +\n> +\n> +\n\nToo many blank lines?\n\n> +static void git_apply_config(void)\n> +{\n> +       git_config_get_string_const(\"apply.whitespace\", &apply_default_whitespace);\n> +       git_config_get_string_const(\"apply.ignorewhitespace\", &apply_default_ignorewhitespace);\n> +       git_config(git_default_config, NULL);\n> +}\n"},{"id":"285134","messageId":"CAP8UFD0U1eBGYsVdNovhAGJzc1L=svDpkunTmTGboKmxhFF5Ow@mail.gmail.com","threadId":"42127","inReplyTo":"CAPig+cQbF_Xq2oB5ALQkj_Zdw8VR8kyVyinq_K24zGGK7Xundw@mail.gmail.com","subject":"Re: [PATCH 54/83] builtin/apply: make parse_chunk() return a negative integer on error","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-01T19:51:56Z","receivedAt":"2016-05-01T19:51:56Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Sun, May 1, 2016 at 9:04 PM, Eric Sunshine <sunshine@sunshineco.com> wrote:\n> On Sun, Apr 24, 2016 at 9:33 AM, Christian Couder\n> <christian.couder@gmail.com> wrote:\n>> This negative number can be -2 if no patch header has been found,\n>> otherwise it is -1.\n>>\n>> As parse_chunk() is called only by apply_patch() which already\n>> returns -1 when an error happened, let's make it return -1 when\n>> parse_chunk() returns -1.\n>>\n>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n>> ---\n>> diff --git a/builtin/apply.c b/builtin/apply.c\n>> @@ -4566,6 +4567,8 @@ static int apply_patch(struct apply_state *state,\n>>                 nr = parse_chunk(state, buf.buf + offset, buf.len - offset, patch);\n>>                 if (nr < 0) {\n>>                         free_patch(patch);\n>> +                       if (nr == -1)\n>> +                               return -1;\n>\n> Same comment as 51/83 about this leaking 'list', 'buf', and 'fn_table'.\n\nOk, thanks!\n"},{"id":"285135","messageId":"CAP8UFD2T6naSMqK+yvBysw9-4hjN1yhUi33Cc73+ChFsistDBQ@mail.gmail.com","threadId":"42127","inReplyTo":"CAPig+cTxZf=oMB=V5JoZNUO6eTtnabv7LinNo1LUeah6ofrbEA@mail.gmail.com","subject":"Re: [PATCH 59/83] builtin/apply: move init_apply_state() to apply.c","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-01T20:19:31Z","receivedAt":"2016-05-01T20:19:31Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Sun, May 1, 2016 at 9:37 PM, Eric Sunshine <sunshine@sunshineco.com> wrote:\n> On Sun, Apr 24, 2016 at 9:33 AM, Christian Couder\n> <christian.couder@gmail.com> wrote:\n>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n>> ---\n>> diff --git a/apply.c b/apply.c\n>> @@ -0,0 +1,80 @@\n>> +#include \"cache.h\"\n>> +#include \"apply.h\"\n>> +\n>> +\n>> +\n>\n> Too many blank lines?\n\nOk, I will remove 2 of them.\n"},{"id":"285136","messageId":"CAPig+cSVCDNRup95ay=htiXiw0UCGs6boDuqovGnQJY8imbFhQ@mail.gmail.com","threadId":"42127","inReplyTo":"1461504863-15946-64-git-send-email-chriscool@tuxfamily.org","subject":"Re: [PATCH 63/83] builtin/apply: make apply_all_patches() return -1 on error","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2016-05-01T21:03:11Z","receivedAt":"2016-05-01T21:03:11Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Sun, Apr 24, 2016 at 9:34 AM, Christian Couder\n<christian.couder@gmail.com> wrote:\n> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n> ---\n> diff --git a/builtin/apply.c b/builtin/apply.c\n> @@ -4562,12 +4562,12 @@ static int apply_all_patches(struct apply_state *state,\n>\n>                 fd = open(arg, O_RDONLY);\n>                 if (fd < 0)\n> -                       die_errno(_(\"can't open patch '%s'\"), arg);\n> +                       return error(_(\"can't open patch '%s': %s\"), arg, strerror(errno));\n>                 read_stdin = 0;\n>                 set_default_whitespace_mode(state);\n>                 res = apply_patch(state, fd, arg, options);\n>                 if (res < 0)\n> -                       exit(1);\n> +                       return -1;\n\nThis leaks 'fd', doesn't it?\n\n>                 errs |= res;\n>                 close(fd);\n>         }\n> @@ -4590,10 +4590,10 @@ static int apply_all_patches(struct apply_state *state,\n>                                 squelched);\n>                 }\n>                 if (state->ws_error_action == die_on_ws_error)\n> -                       die(Q_(\"%d line adds whitespace errors.\",\n> -                              \"%d lines add whitespace errors.\",\n> -                              state->whitespace_error),\n> -                           state->whitespace_error);\n> +                       return error(Q_(\"%d line adds whitespace errors.\",\n> +                                       \"%d lines add whitespace errors.\",\n> +                                       state->whitespace_error),\n> +                                    state->whitespace_error);\n\nHow does this new 'return' relate to the logic below which updates the\nindex? Does the index need to be updated here now too?\n\n>                 if (state->applied_after_fixing_ws && state->apply)\n>                         warning(\"%d line%s applied after\"\n>                                 \" fixing whitespace errors.\",\n> @@ -4608,7 +4608,7 @@ static int apply_all_patches(struct apply_state *state,\n>\n>         if (state->update_index) {\n>                 if (write_locked_index(&the_index, state->lock_file, COMMIT_LOCK))\n> -                       die(_(\"Unable to write new index file\"));\n> +                       return error(_(\"Unable to write new index file\"));\n>         }\n>\n>         return !!errs;\n> @@ -4698,5 +4698,8 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n>         if (check_apply_state(&state, force_apply))\n>                 exit(1);\n>\n> -       return apply_all_patches(&state, argc, argv, options);\n> +       if (apply_all_patches(&state, argc, argv, options))\n> +               exit(1);\n> +\n> +       return 0;\n>  }\n"},{"id":"285145","messageId":"alpine.DEB.2.20.1605020907170.9313@virtualbox","threadId":"42127","inReplyTo":"CAPig+cSVCDNRup95ay=htiXiw0UCGs6boDuqovGnQJY8imbFhQ@mail.gmail.com","subject":"Re: [PATCH 63/83] builtin/apply: make apply_all_patches() return -1 on error","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2016-05-02T07:09:21Z","receivedAt":"2016-05-02T07:09:21Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Eric,\n\nOn Sun, 1 May 2016, Eric Sunshine wrote:\n\n> On Sun, Apr 24, 2016 at 9:34 AM, Christian Couder\n> <christian.couder@gmail.com> wrote:\n> > Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n> > ---\n> > diff --git a/builtin/apply.c b/builtin/apply.c\n> > @@ -4562,12 +4562,12 @@ static int apply_all_patches(struct apply_state *state,\n> >\n> >                 fd = open(arg, O_RDONLY);\n> >                 if (fd < 0)\n> > -                       die_errno(_(\"can't open patch '%s'\"), arg);\n> > +                       return error(_(\"can't open patch '%s': %s\"), arg, strerror(errno));\n> >                 read_stdin = 0;\n> >                 set_default_whitespace_mode(state);\n> >                 res = apply_patch(state, fd, arg, options);\n> >                 if (res < 0)\n> > -                       exit(1);\n> > +                       return -1;\n> \n> This leaks 'fd', doesn't it?\n\nIndeed it does. I thought that I had sent a mail about fd leakage, just\nresent it. Such a leakage prevented the test suite from passing after\napplying this patch series on Windows and patching up several obvious\ncompile problems.\n\nCiao,\nDscho\n"},{"id":"285148","messageId":"CAPig+cSMLCg49O3tf=g2qDi-TUz-xsdW-ac_Eze9g=XsCKSSJA@mail.gmail.com","threadId":"42127","inReplyTo":"1461504863-15946-68-git-send-email-chriscool@tuxfamily.org","subject":"Re: [PATCH 67/83] builtin/apply: make build_fake_ancestor() return -1 on error","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2016-05-02T07:32:06Z","receivedAt":"2016-05-02T07:32:06Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Sun, Apr 24, 2016 at 9:34 AM, Christian Couder\n<christian.couder@gmail.com> wrote:\n> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n> ---\n> diff --git a/builtin/apply.c b/builtin/apply.c\n> @@ -3913,31 +3913,34 @@ static void build_fake_ancestor(struct patch *list, const char *filename)\n>                 ce = make_cache_entry(patch->old_mode, sha1, name, 0, 0);\n>                 if (!ce)\n> -                       die(_(\"make_cache_entry failed for path '%s'\"), name);\n> +                       return error(_(\"make_cache_entry failed for path '%s'\"),\n> +                                    name);\n>                 if (add_index_entry(&result, ce, ADD_CACHE_OK_TO_ADD))\n> -                       die (\"Could not add %s to temporary index\", name);\n> +                       return error(\"Could not add %s to temporary index\",\n> +                                    name);\n\nWhat happens to 'ce' here? Does it leak?\n\n>         }\n>\n>         hold_lock_file_for_update(&lock, filename, LOCK_DIE_ON_ERROR);\n>         if (write_locked_index(&result, &lock, COMMIT_LOCK))\n> -               die (\"Could not write temporary index to %s\", filename);\n> +               return error(\"Could not write temporary index to %s\", filename);\n\nWhat happens with 'result'? Does it need to be discarded?\n\n>         discard_index(&result);\n> +       return 0;\n>  }\n>\n>  static void stat_patch_list(struct apply_state *state, struct patch *patch)\n> @@ -4470,8 +4473,9 @@ static int apply_patch(struct apply_state *state,\n>                 return 1;\n>         }\n>\n> -       if (state->fake_ancestor)\n> -               build_fake_ancestor(list, state->fake_ancestor);\n> +       if (state->fake_ancestor &&\n> +           build_fake_ancestor(list, state->fake_ancestor))\n> +               return -1;\n\nSame comment as earlier patches: Leaking 'list', 'buf', 'fn_table'.\n\n>         if (state->diffstat)\n>                 stat_patch_list(state, list);\n"},{"id":"285149","messageId":"CAPig+cRBgj29ugY0qYS9eB09CDKcJXeBKg3KqPaoZFSzCwftZw@mail.gmail.com","threadId":"42127","inReplyTo":"1461504863-15946-70-git-send-email-chriscool@tuxfamily.org","subject":"Re: [PATCH 69/83] builtin/apply: make add_conflicted_stages_file() return -1 on error","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2016-05-02T07:36:51Z","receivedAt":"2016-05-02T07:36:51Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Sun, Apr 24, 2016 at 9:34 AM, Christian Couder\n<christian.couder@gmail.com> wrote:\n> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n> ---\n> diff --git a/builtin/apply.c b/builtin/apply.c\n> @@ -4234,8 +4234,11 @@ static void add_conflicted_stages_file(struct apply_state *state,\n>                 ce->ce_namelen = namelen;\n>                 hashcpy(ce->sha1, patch->threeway_stage[stage - 1].hash);\n>                 if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)\n> -                       die(_(\"unable to add cache entry for %s\"), patch->new_name);\n> +                       return error(_(\"unable to add cache entry for %s\"),\n> +                                    patch->new_name);\n\nIs this leaking 'ce' (which is allocated a few lines above the shown context)?\n\n>         }\n> +\n> +       return 0;\n>  }\n"},{"id":"285169","messageId":"CAPig+cTpTYowkeO-cDd-87aAw_tRHvdhV4BaBveg25MvsDAd4w@mail.gmail.com","threadId":"42127","inReplyTo":"1461504863-15946-74-git-send-email-chriscool@tuxfamily.org","subject":"Re: [PATCH 73/83] builtin/apply: make write_out_results() return -1 on error","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2016-05-02T17:42:55Z","receivedAt":"2016-05-02T17:42:55Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Sun, Apr 24, 2016 at 9:34 AM, Christian Couder\n<christian.couder@gmail.com> wrote:\n> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n> ---\n> diff --git a/builtin/apply.c b/builtin/apply.c\n> @@ -4381,7 +4387,7 @@ static int write_out_results(struct apply_state *state, struct patch *list)\n>                                 errs = 1;\n>                         else {\n>                                 if (write_out_one_result(state, l, phase))\n> -                                       exit(1);\n> +                                       return -1;\n\nIsn't this leaking 'string_list cpath'?\n\n>                                 if (phase == 1) {\n>                                         if (write_out_one_reject(state, l))\n>                                                 errs = 1;\n> @@ -4484,11 +4490,16 @@ static int apply_patch(struct apply_state *state,\n>             !state->apply_with_reject)\n>                 return -1;\n>\n> -       if (state->apply && write_out_results(state, list)) {\n> -               if (state->apply_with_reject)\n> +       if (state->apply) {\n> +               int res = write_out_results(state, list);\n> +               if (res < 0)\n>                         return -1;\n\nMentioned previously: Leaking 'list', 'buf', 'fn_table'.\n\n> -               /* with --3way, we still need to write the index out */\n> -               return 1;\n> +               if (res > 0) {\n> +                       if (state->apply_with_reject)\n> +                               return -1;\n> +                       /* with --3way, we still need to write the index out */\n> +                       return 1;\n\nNot the fault of this patch, but this 'return' in the original code\nwas also leaking 'list', 'buf', 'fn_table', right?\n\n> +               }\n>         }\n>\n>         if (state->fake_ancestor &&\n"},{"id":"285175","messageId":"CAPig+cQsS7x2qb8SphaAgdfpCLBQ-z=joL3w8RZH0HC9xRG54g@mail.gmail.com","threadId":"42127","inReplyTo":"1461504863-15946-75-git-send-email-chriscool@tuxfamily.org","subject":"Re: [PATCH 74/83] builtin/apply: make try_create_file() return -1 on error","fromName":"Eric Sunshine","fromEmail":"sunshine@sunshineco.com","sentAt":"2016-05-02T18:01:13Z","receivedAt":"2016-05-02T18:01:13Z","isPatch":true,"sender":{"key":"sunshine@sunshineco.com","avatar":"https://avatars.githubusercontent.com/u/163641?v=4"},"body":"On Sun, Apr 24, 2016 at 9:34 AM, Christian Couder\n<christian.couder@gmail.com> wrote:\n> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n> ---\n> diff --git a/builtin/apply.c b/builtin/apply.c\n> @@ -4145,28 +4151,32 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,\n>         fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666);\n>         if (fd < 0)\n> -               return -1;\n> +               return 1;\n>\n>         if (convert_to_working_tree(path, buf, size, &nbuf)) {\n>                 size = nbuf.len;\n>                 buf  = nbuf.buf;\n>         }\n> -       write_or_die(fd, buf, size);\n> +\n> +       if (!write_or_whine_pipe(fd, buf, size, path)) {\n> +               strbuf_release(&nbuf);\n> +               return -1;\n\nThis is leaking 'fd'.\n\n> +       }\n>         strbuf_release(&nbuf);\n>\n>         if (close(fd) < 0)\n> -               die_errno(_(\"closing file '%s'\"), path);\n> +               return error(_(\"closing file '%s': %s\"), path, strerror(errno));\n>         return 0;\n>  }\n>\n> @@ -4208,12 +4227,15 @@ static void create_one_file(struct apply_state *state,\n>                 for (;;) {\n>                         char newpath[PATH_MAX];\n>                         mksnpath(newpath, sizeof(newpath), \"%s~%u\", path, nr);\n> -                       if (!try_create_file(newpath, mode, buf, size)) {\n> +                       res = try_create_file(newpath, mode, buf, size);\n> +                       if (!res) {\n>                                 if (!rename(newpath, path))\n>                                         return;\n>                                 unlink_or_warn(newpath);\n>                                 break;\n>                         }\n> +                       if (res < 0)\n> +                               exit(1);\n\nTwo issues:\n\nGetting the error case out of the way early (moving this 'if' just\nafter 'res=...') would make it easier to reason about the remaining\nlogic.\n\nIt's already difficult to understand what the below 'errno' check is\ntesting. try_create_file(), rename(), or unlink_or_warn()? Plopping\nthis new error handling conditional in front of it divorces the\n'errno' check even further from whatever it is testing.\n\n>                         if (errno != EEXIST)\n>                                 break;\n>                         ++nr;\n"},{"id":"285320","messageId":"CAP8UFD3NXo4vMBK9-3LChWcoSj7y_nKeea-RLyGmdy7kVvhabA@mail.gmail.com","threadId":"42127","inReplyTo":"CAPig+cSVCDNRup95ay=htiXiw0UCGs6boDuqovGnQJY8imbFhQ@mail.gmail.com","subject":"Re: [PATCH 63/83] builtin/apply: make apply_all_patches() return -1 on error","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-03T13:01:16Z","receivedAt":"2016-05-03T13:01:16Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Sun, May 1, 2016 at 11:03 PM, Eric Sunshine <sunshine@sunshineco.com> wrote:\n>> @@ -4590,10 +4590,10 @@ static int apply_all_patches(struct apply_state *state,\n>>                                 squelched);\n>>                 }\n>>                 if (state->ws_error_action == die_on_ws_error)\n>> -                       die(Q_(\"%d line adds whitespace errors.\",\n>> -                              \"%d lines add whitespace errors.\",\n>> -                              state->whitespace_error),\n>> -                           state->whitespace_error);\n>> +                       return error(Q_(\"%d line adds whitespace errors.\",\n>> +                                       \"%d lines add whitespace errors.\",\n>> +                                       state->whitespace_error),\n>> +                                    state->whitespace_error);\n>\n> How does this new 'return' relate to the logic below which updates the\n> index? Does the index need to be updated here now too?\n\nI think it is the right thing to do not to update the index when there\nare whitespaces and state->ws_error_action == die_on_ws_error.\n"},{"id":"285321","messageId":"CAP8UFD0FcxrmqXjJZpdkc0akY4ZDGGY+dEwDg-EGAFAXV_=e5w@mail.gmail.com","threadId":"42127","inReplyTo":"CAPig+cSMLCg49O3tf=g2qDi-TUz-xsdW-ac_Eze9g=XsCKSSJA@mail.gmail.com","subject":"Re: [PATCH 67/83] builtin/apply: make build_fake_ancestor() return -1 on error","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-03T13:52:37Z","receivedAt":"2016-05-03T13:52:37Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Mon, May 2, 2016 at 9:32 AM, Eric Sunshine <sunshine@sunshineco.com> wrote:\n> On Sun, Apr 24, 2016 at 9:34 AM, Christian Couder\n> <christian.couder@gmail.com> wrote:\n>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n>> ---\n>> diff --git a/builtin/apply.c b/builtin/apply.c\n>> @@ -3913,31 +3913,34 @@ static void build_fake_ancestor(struct patch *list, const char *filename)\n>>                 ce = make_cache_entry(patch->old_mode, sha1, name, 0, 0);\n>>                 if (!ce)\n>> -                       die(_(\"make_cache_entry failed for path '%s'\"), name);\n>> +                       return error(_(\"make_cache_entry failed for path '%s'\"),\n>> +                                    name);\n>>                 if (add_index_entry(&result, ce, ADD_CACHE_OK_TO_ADD))\n>> -                       die (\"Could not add %s to temporary index\", name);\n>> +                       return error(\"Could not add %s to temporary index\",\n>> +                                    name);\n>\n> What happens to 'ce' here? Does it leak?\n\nYeah, I think it leaks.\n\n>>         }\n>>\n>>         hold_lock_file_for_update(&lock, filename, LOCK_DIE_ON_ERROR);\n>>         if (write_locked_index(&result, &lock, COMMIT_LOCK))\n>> -               die (\"Could not write temporary index to %s\", filename);\n>> +               return error(\"Could not write temporary index to %s\", filename);\n>\n> What happens with 'result'? Does it need to be discarded?\n\nYeah, I think so.\n\n>>         discard_index(&result);\n>> +       return 0;\n>>  }\n>>\n>>  static void stat_patch_list(struct apply_state *state, struct patch *patch)\n>> @@ -4470,8 +4473,9 @@ static int apply_patch(struct apply_state *state,\n>>                 return 1;\n>>         }\n>>\n>> -       if (state->fake_ancestor)\n>> -               build_fake_ancestor(list, state->fake_ancestor);\n>> +       if (state->fake_ancestor &&\n>> +           build_fake_ancestor(list, state->fake_ancestor))\n>> +               return -1;\n>\n> Same comment as earlier patches: Leaking 'list', 'buf', 'fn_table'.\n\nOk, I will take care of these.\n\nThanks,\nChristian.\n"},{"id":"285322","messageId":"CAP8UFD3F+hXxypz+tw8fRBNS5fWwkaV71GmzDkNBUTZbJt2ENQ@mail.gmail.com","threadId":"42127","inReplyTo":"CAPig+cRBgj29ugY0qYS9eB09CDKcJXeBKg3KqPaoZFSzCwftZw@mail.gmail.com","subject":"Re: [PATCH 69/83] builtin/apply: make add_conflicted_stages_file() return -1 on error","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-03T14:33:57Z","receivedAt":"2016-05-03T14:33:57Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Mon, May 2, 2016 at 9:36 AM, Eric Sunshine <sunshine@sunshineco.com> wrote:\n> On Sun, Apr 24, 2016 at 9:34 AM, Christian Couder\n> <christian.couder@gmail.com> wrote:\n>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n>> ---\n>> diff --git a/builtin/apply.c b/builtin/apply.c\n>> @@ -4234,8 +4234,11 @@ static void add_conflicted_stages_file(struct apply_state *state,\n>>                 ce->ce_namelen = namelen;\n>>                 hashcpy(ce->sha1, patch->threeway_stage[stage - 1].hash);\n>>                 if (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0)\n>> -                       die(_(\"unable to add cache entry for %s\"), patch->new_name);\n>> +                       return error(_(\"unable to add cache entry for %s\"),\n>> +                                    patch->new_name);\n>\n> Is this leaking 'ce' (which is allocated a few lines above the shown context)?\n\nYes, thanks.\n"},{"id":"285323","messageId":"CAP8UFD1L4TgE-4mzB3bCShgYe9CBFXC+yzi7sKCjHcwBWWJ=AA@mail.gmail.com","threadId":"42127","inReplyTo":"CAPig+cTpTYowkeO-cDd-87aAw_tRHvdhV4BaBveg25MvsDAd4w@mail.gmail.com","subject":"Re: [PATCH 73/83] builtin/apply: make write_out_results() return -1 on error","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-03T14:55:23Z","receivedAt":"2016-05-03T14:55:23Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Mon, May 2, 2016 at 7:42 PM, Eric Sunshine <sunshine@sunshineco.com> wrote:\n> On Sun, Apr 24, 2016 at 9:34 AM, Christian Couder\n> <christian.couder@gmail.com> wrote:\n>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n>> ---\n>> diff --git a/builtin/apply.c b/builtin/apply.c\n>> @@ -4381,7 +4387,7 @@ static int write_out_results(struct apply_state *state, struct patch *list)\n>>                                 errs = 1;\n>>                         else {\n>>                                 if (write_out_one_result(state, l, phase))\n>> -                                       exit(1);\n>> +                                       return -1;\n>\n> Isn't this leaking 'string_list cpath'?\n\nYes, it is. Will fix.\n\n>>                                 if (phase == 1) {\n>>                                         if (write_out_one_reject(state, l))\n>>                                                 errs = 1;\n>> @@ -4484,11 +4490,16 @@ static int apply_patch(struct apply_state *state,\n>>             !state->apply_with_reject)\n>>                 return -1;\n>>\n>> -       if (state->apply && write_out_results(state, list)) {\n>> -               if (state->apply_with_reject)\n>> +       if (state->apply) {\n>> +               int res = write_out_results(state, list);\n>> +               if (res < 0)\n>>                         return -1;\n>\n> Mentioned previously: Leaking 'list', 'buf', 'fn_table'.\n\nYeah, already fixed.\n\n>> -               /* with --3way, we still need to write the index out */\n>> -               return 1;\n>> +               if (res > 0) {\n>> +                       if (state->apply_with_reject)\n>> +                               return -1;\n>> +                       /* with --3way, we still need to write the index out */\n>> +                       return 1;\n>\n> Not the fault of this patch, but this 'return' in the original code\n> was also leaking 'list', 'buf', 'fn_table', right?\n\nYeah, already fixed.\n"},{"id":"285335","messageId":"CAP8UFD2Qc4h+PjZSXBFDoYBXVj7dkvf5j-aAuZs0-ZxbHQfuMA@mail.gmail.com","threadId":"42127","inReplyTo":"CAPig+cQsS7x2qb8SphaAgdfpCLBQ-z=joL3w8RZH0HC9xRG54g@mail.gmail.com","subject":"Re: [PATCH 74/83] builtin/apply: make try_create_file() return -1 on error","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-03T16:24:30Z","receivedAt":"2016-05-03T16:24:30Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Mon, May 2, 2016 at 8:01 PM, Eric Sunshine <sunshine@sunshineco.com> wrote:\n> On Sun, Apr 24, 2016 at 9:34 AM, Christian Couder\n> <christian.couder@gmail.com> wrote:\n>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n>> ---\n>> diff --git a/builtin/apply.c b/builtin/apply.c\n>> @@ -4145,28 +4151,32 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,\n>>         fd = open(path, O_CREAT | O_EXCL | O_WRONLY, (mode & 0100) ? 0777 : 0666);\n>>         if (fd < 0)\n>> -               return -1;\n>> +               return 1;\n>>\n>>         if (convert_to_working_tree(path, buf, size, &nbuf)) {\n>>                 size = nbuf.len;\n>>                 buf  = nbuf.buf;\n>>         }\n>> -       write_or_die(fd, buf, size);\n>> +\n>> +       if (!write_or_whine_pipe(fd, buf, size, path)) {\n>> +               strbuf_release(&nbuf);\n>> +               return -1;\n>\n> This is leaking 'fd'.\n\nOk, fixed.\n\n>> @@ -4208,12 +4227,15 @@ static void create_one_file(struct apply_state *state,\n>>                 for (;;) {\n>>                         char newpath[PATH_MAX];\n>>                         mksnpath(newpath, sizeof(newpath), \"%s~%u\", path, nr);\n>> -                       if (!try_create_file(newpath, mode, buf, size)) {\n>> +                       res = try_create_file(newpath, mode, buf, size);\n>> +                       if (!res) {\n>>                                 if (!rename(newpath, path))\n>>                                         return;\n>>                                 unlink_or_warn(newpath);\n>>                                 break;\n>>                         }\n>> +                       if (res < 0)\n>> +                               exit(1);\n>\n> Two issues:\n>\n> Getting the error case out of the way early (moving this 'if' just\n> after 'res=...') would make it easier to reason about the remaining\n> logic.\n\nOk, I moved up the error cases.\n\n> It's already difficult to understand what the below 'errno' check is\n> testing. try_create_file(), rename(), or unlink_or_warn()? Plopping\n> this new error handling conditional in front of it divorces the\n> 'errno' check even further from whatever it is testing.\n\nYeah, but I don't see what I could do about that.\n"},{"id":"285452","messageId":"CAP8UFD3o_u48rRHP7ZOWnVH7X0O5wwM-Gk-VpH5PP_kFx19ZpQ@mail.gmail.com","threadId":"42127","inReplyTo":"CACsJy8Cb20L=y_=J6S5=rngvH1n0aWvtf5eJrXP-df0aoiJyrQ@mail.gmail.com","subject":"Re: [PATCH 78/83] Move libified code from builtin/apply.c to apply.{c,h}","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-04T10:39:07Z","receivedAt":"2016-05-04T10:39:07Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Mon, Apr 25, 2016 at 3:46 PM, Duy Nguyen <pclouds@gmail.com> wrote:\n> On Sun, Apr 24, 2016 at 8:34 PM, Christian Couder\n> <christian.couder@gmail.com> wrote:\n>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n>> ---\n>>  apply.c         | 4678 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-\n>>  apply.h         |   19 +\n>>  builtin/apply.c | 4677 ------------------------------------------------------\n>>  3 files changed, 4695 insertions(+), 4679 deletions(-)\n>\n> Maybe if you can rearrange functions in the right order in\n> builtin/apply.c first and move everything in one patch (i.e. apply.c\n> is created here), rename detection kicks in realizes apply.c is a\n> rename of (old) builtin/apply.c and only shows the diff of (new)\n> builtin/apply.c which is about 90 lines... I haven't tested if this is\n> true though.\n\nRight now I get:\n\n> git log --summary -M -C -B -1 20f1d27\ncommit 20f1d274609f5dde2eaaa279e7ee79fd5ef9c849\nAuthor: Christian Couder <chriscool@tuxfamily.org>\nDate:   Fri Apr 22 20:55:46 2016 +0200\n\n    Move libified code from builtin/apply.c to apply.{c,h}\n\n    Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n\n copy builtin/apply.c => apply.c (96%)\n rewrite builtin/apply.c (96%)\n\nAnd using format-patch:\n\n> git format-patch -M -C -B -1  -o ../../patches/test-libify-apply-use-in-am/ 20f1d27\n../../patches/test-libify-apply-use-in-am/0001-Move-libified-code-from-builtin-apply.c-to-apply.-c-.patch\n> wc ../../patches/test-libify-apply-use-in-am/0001-Move-libified-code-from-builtin-apply.c-to-apply.-c-.patch\n  5264  23426 147133\n../../patches/test-libify-apply-use-in-am/0001-Move-libified-code-from-builtin-apply.c-to-apply.-c-.patch\n\nThe previous patch was:\n\n> wc ../../patches/libify-apply-use-in-am25/0078-Move-libified-code-from-builtin-apply.c-to-apply.-c-.patch\n  9436  41887 254435\n../../patches/libify-apply-use-in-am25/0078-Move-libified-code-from-builtin-apply.c-to-apply.-c-.patch\n\nSo it is better, though not really as small as I would like.\n\nI am also not so sure that moving everything in one patch would make\nthings much smaller.\nBy the way at one point I have to create apply.h from some of the\ncontent of builtin/apply.c (like the struct apply_state) and I don't\nknow if you are talking about that too.\n\nIf you want you can try using the latest version of the series:\n\nhttps://github.com/chriscool/git/commits/libify-apply-use-in-am39\n\nRight now I think I will just use -M -C -B with format-patch for v2.\n\nThanks,\nChristian.\n"},{"id":"285459","messageId":"CACsJy8AtEv9HHOJUDVKmO=qLkWctvS2zoGdo03jtQwyy2kKzRQ@mail.gmail.com","threadId":"42127","inReplyTo":"CAP8UFD3o_u48rRHP7ZOWnVH7X0O5wwM-Gk-VpH5PP_kFx19ZpQ@mail.gmail.com","subject":"Re: [PATCH 78/83] Move libified code from builtin/apply.c to apply.{c,h}","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2016-05-04T11:32:43Z","receivedAt":"2016-05-04T11:32:43Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Wed, May 4, 2016 at 5:39 PM, Christian Couder\n<christian.couder@gmail.com> wrote:\n> On Mon, Apr 25, 2016 at 3:46 PM, Duy Nguyen <pclouds@gmail.com> wrote:\n>> On Sun, Apr 24, 2016 at 8:34 PM, Christian Couder\n>> <christian.couder@gmail.com> wrote:\n>>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n>>> ---\n>>>  apply.c         | 4678 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-\n>>>  apply.h         |   19 +\n>>>  builtin/apply.c | 4677 ------------------------------------------------------\n>>>  3 files changed, 4695 insertions(+), 4679 deletions(-)\n>>\n>> Maybe if you can rearrange functions in the right order in\n>> builtin/apply.c first and move everything in one patch (i.e. apply.c\n>> is created here), rename detection kicks in realizes apply.c is a\n>> rename of (old) builtin/apply.c and only shows the diff of (new)\n>> builtin/apply.c which is about 90 lines... I haven't tested if this is\n>> true though.\n>\n> Right now I get:\n>\n>> git log --summary -M -C -B -1 20f1d27\n> commit 20f1d274609f5dde2eaaa279e7ee79fd5ef9c849\n> Author: Christian Couder <chriscool@tuxfamily.org>\n> Date:   Fri Apr 22 20:55:46 2016 +0200\n>\n>     Move libified code from builtin/apply.c to apply.{c,h}\n>\n>     Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n>\n>  copy builtin/apply.c => apply.c (96%)\n>  rewrite builtin/apply.c (96%)\n\nAh.. I forgot about -B to break rename pairs! This looks good. I\nreally need to go back to diff rename hints series, so we can annotate\nthis commit and don't have to use -B every time.\n\n> And using format-patch:\n>\n>> git format-patch -M -C -B -1  -o ../../patches/test-libify-apply-use-in-am/ 20f1d27\n> ../../patches/test-libify-apply-use-in-am/0001-Move-libified-code-from-builtin-apply.c-to-apply.-c-.patch\n>> wc ../../patches/test-libify-apply-use-in-am/0001-Move-libified-code-from-builtin-apply.c-to-apply.-c-.patch\n>   5264  23426 147133\n\nIt's a lot of lines, but if you look at diff stat, it's 244 insertions\nand 4906 deletions. I _think_ deletions are something reviewers don't\nhave to pay much attention to (trusting that git moves code\ncorrectly), so only 244 lines to review are really nice. But I may be\nwrong.\n-- \nDuy\n"},{"id":"285555","messageId":"CAP8UFD0Ku_q56JMvyh9UncJ2D+QsOcB4Svyr5ZWj+KqAaHORMw@mail.gmail.com","threadId":"42127","inReplyTo":"CACsJy8AtEv9HHOJUDVKmO=qLkWctvS2zoGdo03jtQwyy2kKzRQ@mail.gmail.com","subject":"Re: [PATCH 78/83] Move libified code from builtin/apply.c to apply.{c,h}","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-05T08:50:08Z","receivedAt":"2016-05-05T08:50:08Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Wed, May 4, 2016 at 1:32 PM, Duy Nguyen <pclouds@gmail.com> wrote:\n> On Wed, May 4, 2016 at 5:39 PM, Christian Couder\n> <christian.couder@gmail.com> wrote:\n>>\n>> Right now I get:\n>>\n>>> git log --summary -M -C -B -1 20f1d27\n>> commit 20f1d274609f5dde2eaaa279e7ee79fd5ef9c849\n>> Author: Christian Couder <chriscool@tuxfamily.org>\n>> Date:   Fri Apr 22 20:55:46 2016 +0200\n>>\n>>     Move libified code from builtin/apply.c to apply.{c,h}\n>>\n>>     Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n>>\n>>  copy builtin/apply.c => apply.c (96%)\n>>  rewrite builtin/apply.c (96%)\n>\n> Ah.. I forgot about -B to break rename pairs! This looks good. I\n> really need to go back to diff rename hints series, so we can annotate\n> this commit and don't have to use -B every time.\n>\n>> And using format-patch:\n>>\n>>> git format-patch -M -C -B -1  -o ../../patches/test-libify-apply-use-in-am/ 20f1d27\n>> ../../patches/test-libify-apply-use-in-am/0001-Move-libified-code-from-builtin-apply.c-to-apply.-c-.patch\n>>> wc ../../patches/test-libify-apply-use-in-am/0001-Move-libified-code-from-builtin-apply.c-to-apply.-c-.patch\n>>   5264  23426 147133\n>\n> It's a lot of lines, but if you look at diff stat, it's 244 insertions\n> and 4906 deletions. I _think_ deletions are something reviewers don't\n> have to pay much attention to (trusting that git moves code\n> correctly), so only 244 lines to review are really nice. But I may be\n> wrong.\n\nYeah, you are right. I hadn't looked at the diff stat. Thanks.\n"},{"id":"285779","messageId":"CAP8UFD0ktQyk3SHFbq=fjK8++UArNmy+3p3CCgHaDBT5WRwU6Q@mail.gmail.com","threadId":"42127","inReplyTo":"CAP8UFD0Ku_q56JMvyh9UncJ2D+QsOcB4Svyr5ZWj+KqAaHORMw@mail.gmail.com","subject":"Re: [PATCH 78/83] Move libified code from builtin/apply.c to apply.{c,h}","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-06T20:18:54Z","receivedAt":"2016-05-06T20:18:54Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Thu, May 5, 2016 at 10:50 AM, Christian Couder\n<christian.couder@gmail.com> wrote:\n> On Wed, May 4, 2016 at 1:32 PM, Duy Nguyen <pclouds@gmail.com> wrote:\n>> On Wed, May 4, 2016 at 5:39 PM, Christian Couder\n>> <christian.couder@gmail.com> wrote:\n>>>\n>>> Right now I get:\n>>>\n>>>> git log --summary -M -C -B -1 20f1d27\n>>> commit 20f1d274609f5dde2eaaa279e7ee79fd5ef9c849\n>>> Author: Christian Couder <chriscool@tuxfamily.org>\n>>> Date:   Fri Apr 22 20:55:46 2016 +0200\n>>>\n>>>     Move libified code from builtin/apply.c to apply.{c,h}\n>>>\n>>>     Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n>>>\n>>>  copy builtin/apply.c => apply.c (96%)\n>>>  rewrite builtin/apply.c (96%)\n>>\n>> Ah.. I forgot about -B to break rename pairs! This looks good. I\n>> really need to go back to diff rename hints series, so we can annotate\n>> this commit and don't have to use -B every time.\n>>\n>>> And using format-patch:\n>>>\n>>>> git format-patch -M -C -B -1  -o ../../patches/test-libify-apply-use-in-am/ 20f1d27\n>>> ../../patches/test-libify-apply-use-in-am/0001-Move-libified-code-from-builtin-apply.c-to-apply.-c-.patch\n>>>> wc ../../patches/test-libify-apply-use-in-am/0001-Move-libified-code-from-builtin-apply.c-to-apply.-c-.patch\n>>>   5264  23426 147133\n\nBy the way does someone know how such a patch can be applied?\n\nI get the following:\n\n> git reset --keep 20f1d27~\n> git am ../../patches/test-libify-apply-use-in-am/0001-Move-libified-code-from-builtin-apply.c-to-apply.-c-.patch\nApplying: Move libified code from builtin/apply.c to apply.{c,h}\nerror: apply.c: already exists in index\nPatch failed at 0001 Move libified code from builtin/apply.c to apply.{c,h}\nThe copy of the patch that failed is found in:\n   /home/christian/git/git/.git/rebase-apply/patch\nWhen you have resolved this problem, run \"git am --continue\".\nIf you prefer to skip this patch, run \"git am --skip\" instead.\nTo restore the original branch and stop patching, run \"git am --abort\".\n\nWith git apply I get:\n\n> git am --abort\n> git apply -v ../../patches/test-libify-apply-use-in-am/0001-Move-libified-code-from-builtin-apply.c-to-apply.-c-.patch\nChecking patch builtin/apply.c => apply.c...\nerror: apply.c: already exists in working directory\nChecking patch apply.h...\nChecking patch builtin/apply.c...\n"},{"id":"285783","messageId":"xmqq4maa3nfm.fsf@gitster.mtv.corp.google.com","threadId":"42127","inReplyTo":"CAP8UFD0ktQyk3SHFbq=fjK8++UArNmy+3p3CCgHaDBT5WRwU6Q@mail.gmail.com","subject":"Re: [PATCH 78/83] Move libified code from builtin/apply.c to apply.{c,h}","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-05-06T21:07:41Z","receivedAt":"2016-05-06T21:07:41Z","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> By the way does someone know how such a patch can be applied?\n\nPerhaps this one?\n\n    http://thread.gmane.org/gmane.linux.kernel/1879635\n"},{"id":"285900","messageId":"CAP8UFD1UMJXkPx1zHMK4sJrueMeXE=5uT+oPp1D31+dsU55AFw@mail.gmail.com","threadId":"42127","inReplyTo":"xmqq4maa3nfm.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH 78/83] Move libified code from builtin/apply.c to apply.{c,h}","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-05-08T20:11:41Z","receivedAt":"2016-05-08T20:11:41Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Fri, May 6, 2016 at 11:07 PM, Junio C Hamano <gitster@pobox.com> wrote:\n> Christian Couder <christian.couder@gmail.com> writes:\n>\n>> By the way does someone know how such a patch can be applied?\n>\n> I think in mid Feb 2015 I sent out a message that says \"'diff -B -M'\n> gives a broken result, do not use it\".\n> I do not have time to dig the mail archive right now.\n>\n> Perhaps this one?\n>\n>     http://thread.gmane.org/gmane.linux.kernel/1879635\n\nI get something strange with only -B:\n\n$ git reset --keep 20f1d27~\n$ git format-patch -B -1  -o ../../patches/test-libify-apply-use-in-am/ 20f1d27\n../../patches/test-libify-apply-use-in-am/0001-Move-libified-code-from-builtin-apply.c-to-apply.-c-.patch\n$ wc ../../patches/test-libify-apply-use-in-am/0001-Move-libified-code-from-builtin-apply.c-to-apply.-c-.patch\n  5264  23426 147127\n../../patches/test-libify-apply-use-in-am/0001-Move-libified-code-from-builtin-apply.c-to-apply.-c-.patch\n\nI get a conflict with `git am`:\n\n$ git am --3way\n../../patches/test-libify-apply-use-in-am/0001-Move-libified-code-from-builtin-apply.c-to-apply.-c-.patch\nApplying: Move libified code from builtin/apply.c to apply.{c,h}\nUsing index info to reconstruct a base tree...\nFalling back to patching base and 3-way merge...\nAuto-merging apply.c\nCONFLICT (add/add): Merge conflict in apply.c\nerror: Failed to merge in the changes.\nPatch failed at 0001 Move libified code from builtin/apply.c to apply.{c,h}\nThe copy of the patch that failed is found in: .git/rebase-apply/patch\nWhen you have resolved this problem, run \"git am --continue\".\nIf you prefer to skip this patch, run \"git am --skip\" instead.\nTo restore the original branch and stop patching, run \"git am --abort\".\n\nBut it works using `git apply --3way`:\n\n$ git reset --hard HEAD\nHEAD is now at 557f659 apply: rename and move opt constants to apply.h\n$ git apply --3way\n../../patches/test-libify-apply-use-in-am/0001-Move-libified-code-from-builtin-apply.c-to-apply.-c-.patch\nFalling back to three-way merge...\nApplied patch to 'apply.c' cleanly.\n$ git am --continue\nApplying: Move libified code from builtin/apply.c to apply.{c,h}\n"}]}