{"thread":{"id":"44003","subject":"[PATCH v14 00/41] libify apply and use lib in am, part 2","startedAt":"2016-09-04T20:18:51Z","lastAt":"2016-09-07T19:51:57Z","messageCount":44,"participants":["Christian Couder","Stefan Beller","Junio C Hamano"],"isPatch":true,"patchVersion":14,"patchTotal":41},"messages":[{"id":"301034","messageId":"20160904201833.21676-1-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":null,"subject":"[PATCH v14 00/41] libify apply and use lib in am, part 2","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:17:52Z","receivedAt":"2016-09-04T20:18:51Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Goal\n~~~~\n\nThis is a patch series about libifying `git apply` functionality, and\nusing this libified functionality in `git am`, so that no 'git apply'\nprocess is spawn anymore. This makes `git am` significantly faster, so\n`git rebase`, when it uses the am backend, is also significantly\nfaster.\n\nPrevious discussions and patches series\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nThis has initially been discussed in the following thread:\n\n  http://thread.gmane.org/gmane.comp.version-control.git/287236/\n\nThen the following patch series were sent:\n\nRFC: http://thread.gmane.org/gmane.comp.version-control.git/288489/\nv1: http://thread.gmane.org/gmane.comp.version-control.git/292324/\nv2: http://thread.gmane.org/gmane.comp.version-control.git/294248/\nv3: http://thread.gmane.org/gmane.comp.version-control.git/295429/\nv4: http://thread.gmane.org/gmane.comp.version-control.git/296350/\nv5: http://thread.gmane.org/gmane.comp.version-control.git/296490/\nv6: http://thread.gmane.org/gmane.comp.version-control.git/297024/\nv7: http://thread.gmane.org/gmane.comp.version-control.git/297193/\nv8: https://public-inbox.org/git/20160627182429.31550-1-chriscool%40tuxfamily.org/\nv9: https://public-inbox.org/git/20160730172509.22939-1-chriscool%40tuxfamily.org/\nv10: https://public-inbox.org/git/20160808210337.5038-1-chriscool%40tuxfamily.org/\nv11: https://public-inbox.org/git/20160811085229.19017-1-chriscool%40tuxfamily.org/\nv12: https://public-inbox.org/git/20160811184501.384-1-chriscool@tuxfamily.org/\nv13: https://public-inbox.org/git/20160827184547.4365-1-chriscool@tuxfamily.org/\n\nHighlevel view of the patches in the series\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nThis is \"part 2\" of the full patch series. I am resending everything\nthat is not already in master to give people another chance to review\neverything.\n\n  - Patch 01/41 to 27/41 were in v10, but not in v11, v12, and v13.\n\nThey libify the apply functions by making them return error codes\ninstead of die()ing or exit()ing and by making sure they release all\nthe resources they use.\n\nThere is only one change in patch 26/41 where I now use error_errno()\nright away instead of first using error() and then converting the\nerror() into error_errno() in patch 32/41.\n\n  - Patches 28/41 to 31/41 were in v12 and v13.\n\nThey finish libifying the apply functionality that was in\nbuiltin/apply.c and move it into apply.{c,h}, but the libified\nfunctionality is not yet used in `git am`.\n\nIn patch 29/41 I added some comments in apply.h above the definition\nof APPLY_OPT_INACCURATE_EOF and APPLY_OPT_RECOUNT, as suggested by\nStefan. This is the only change compared to v13.\n\n  - Patch 32/41 was in v10, v12 and v13.\n\nIt replaces some calls to error() with calls to error_errno().\n\nOne hunk has been removed because of the changes in 26/41.\n\n  - Patches 33/41 to 37/41 were in v10, v12 and v13.\n\nThey implement a way to make the libified apply code silent by\nchanging the bool `apply_verbosely` into a tristate enum called\n`apply_verbosity`, that can be one of `verbosity_verbose`,\n`verbosity_normal` or `verbosity_silent`.\n\nThe only changes since v13 are in 37/41. The name of the first\nargument to mute_routine() is changed from \"bla\" to \"msg\" as suggested\nby Ramsey, and the commit message is improved as suggested by Stefan.\n\n  - Patch 38/41 was in v10, v12 and v13, and hasn't changed.\n\nIt refactors `git apply` option parsing to make it possible for `git\nam` to easily pass some command line options to the libified applied\ncode.\n\n  - Patch 39/41 was new in v13, and hasn't changed.\n\nIt is a refactoring to prepare for patch 40/41.\n\n  - Patch 40/41 was in v12 and v13, and hasn't changed.\n\nIt adds a \"const char *index_file\" into \"struct apply_state\", to make\nit possible to use a special index file instead of the default one.\n\n  - Patch 41/41 was in v10, v12 and v13.\n\nThis patch makes `git am` use the libified functionality.\n\nThe only change compared to v13 is that \"through\" instead of \"thru\" is\nused in an error message.\n\nGeneral comments\n~~~~~~~~~~~~~~~~\n\nNow this patch series is 41 patches long to give yet another\npossibility for all the patches to be reviewed.\n\nI took another look at all these patches myself and didn't find\nanything other than the changes I made in 26/41 to improve.\n\nI will send a diff between this version and v13 as a reply to this\nemail.\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\nBy the way, current work is ongoing to make it possible to use\nsplit-index more easily by adding a config variable, see:\n\nhttps://public-inbox.org/git/20160711172254.13439-1-chriscool%40tuxfamily.org/\n\nUsing an earlier version of this series as rebase material, Duy\nexplained split-index benefits along with this patch series like this:\n\n > Without the series, the picture is not so surprising. We run git-apply\n > 80+ times, each consists of this sequence\n >\n > read index\n > write index (cache tree updates only)\n > read index again\n > optionally initialize name hash (when new entries are added, I guess)\n > read packed-refs\n > write index\n >\n > With this series, we run a single git-apply which does\n >\n > read index (and sharedindex too if in split-index mode)\n > initialize name hash\n > write index 80+ times\n\n(See: http://thread.gmane.org/gmane.comp.version-control.git/292324/focus=292460)\n\nLinks\n~~~~~\n\nThis patch series is available here:\n\nhttps://github.com/chriscool/git/commits/libify-apply-use-in-am\n\nThe previous versions are available there:\n\nv1: https://github.com/chriscool/git/commits/libify-apply-use-in-am25 \nv2: https://github.com/chriscool/git/commits/libify-apply-use-in-am54\nv6: https://github.com/chriscool/git/commits/libify-apply-use-in-am65\nv7: https://github.com/chriscool/git/commits/libify-apply-use-in-am75\nv8: https://github.com/chriscool/git/commits/libify-apply-use-in-am97\nv9: https://github.com/chriscool/git/commits/libify-apply-use-in-am106\nv10: https://github.com/chriscool/git/commits/libify-apply-use-in-am114\nv11: https://github.com/chriscool/git/commits/libify-apply-use-in-am116\nv12: https://github.com/chriscool/git/commits/libify-apply-use-in-am118\nv13: https://github.com/chriscool/git/commits/libify-apply-use-in-am120\n\nPerformance numbers\n~~~~~~~~~~~~~~~~~~~\n\nNumbers are only available for tests that have been performed on Linux\nusing a very early version of this series, though Johannes Sixt\nreported great improvements on Windows. It could be interesting to get\ndetailed numbers on other platforms like Windows and OSX.\n\n  - Around mid April Æ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  - Around mid April 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\n\nChristian Couder (41):\n  apply: make some names more specific\n  apply: move 'struct apply_state' to apply.h\n  builtin/apply: make apply_patch() return -1 or -128 instead of\n    die()ing\n  builtin/apply: read_patch_file() return -1 instead of die()ing\n  builtin/apply: make find_header() return -128 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  builtin/apply: make parse_whitespace_option() return -1 instead of\n    die()ing\n  builtin/apply: make parse_ignorewhitespace_option() return -1 instead\n    of die()ing\n  builtin/apply: move init_apply_state() to apply.c\n  apply: make init_apply_state() return -1 instead of exit()ing\n  builtin/apply: make check_apply_state() return -1 instead of die()ing\n  builtin/apply: move check_apply_state() to apply.c\n  builtin/apply: make apply_all_patches() return 128 or 1 on error\n  builtin/apply: make parse_traditional_patch() return -1 on error\n  builtin/apply: make gitdiff_*() return 1 at end of header\n  builtin/apply: make gitdiff_*() 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  apply: use error_errno() where possible\n  apply: make it possible to silently apply\n  apply: don't print on stdout in verbosity_silent mode\n  usage: add set_warn_routine()\n  usage: add get_error_routine() and get_warn_routine()\n  apply: change error_routine when silent\n  apply: refactor `git apply` option parsing\n  apply: pass apply state to build_fake_ancestor()\n  apply: learn to use a different index file\n  builtin/am: use apply API in run_apply()\n\n Makefile               |    1 +\n apply.c                | 4988 ++++++++++++++++++++++++++++++++++++++++++++++++\n apply.h                |  137 ++\n builtin/am.c           |   65 +-\n builtin/apply.c        | 4873 +---------------------------------------------\n git-compat-util.h      |    3 +\n t/t4012-diff-binary.sh |    4 +-\n t/t4254-am-corrupt.sh  |    2 +-\n usage.c                |   15 +\n 9 files changed, 5205 insertions(+), 4883 deletions(-)\n create mode 100644 apply.c\n create mode 100644 apply.h\n\n-- \n2.10.0.41.g9df52c3\n\n"},{"id":"301035","messageId":"20160904201833.21676-2-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"[PATCH v14 01/41] apply: make some names more specific","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:17:53Z","receivedAt":"2016-09-04T20:18:54Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To prepare for some structs and constants being moved from\nbuiltin/apply.c to apply.h, we should give them some more\nspecific names to avoid possible name collisions in the global\nnamespace.\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 20 ++++++++++----------\n 1 file changed, 10 insertions(+), 10 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 1a488f9..ab8f0bd 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -21,7 +21,7 @@\n #include \"ll-merge.h\"\n #include \"rerere.h\"\n \n-enum ws_error_action {\n+enum apply_ws_error_action {\n \tnowarn_ws_error,\n \twarn_on_ws_error,\n \tdie_on_ws_error,\n@@ -29,7 +29,7 @@ enum ws_error_action {\n };\n \n \n-enum ws_ignore {\n+enum apply_ws_ignore {\n \tignore_ws_none,\n \tignore_ws_change\n };\n@@ -45,8 +45,8 @@ enum ws_ignore {\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+#define APPLY_SYMLINK_GOES_AWAY 01\n+#define APPLY_SYMLINK_IN_RESULT 02\n \n struct apply_state {\n \tconst char *prefix;\n@@ -110,8 +110,8 @@ struct apply_state {\n \tstruct string_list fn_table;\n \n \t/* These control whitespace errors */\n-\tenum ws_error_action ws_error_action;\n-\tenum ws_ignore ws_ignore_action;\n+\tenum apply_ws_error_action ws_error_action;\n+\tenum apply_ws_ignore ws_ignore_action;\n \tconst char *whitespace_option;\n \tint whitespace_error;\n \tint squelch_whitespace_errors;\n@@ -3750,11 +3750,11 @@ static void prepare_symlink_changes(struct apply_state *state, struct patch *pat\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(state, patch->old_name, SYMLINK_GOES_AWAY);\n+\t\t\tregister_symlink_changes(state, patch->old_name, APPLY_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(state, patch->new_name, SYMLINK_IN_RESULT);\n+\t\t\tregister_symlink_changes(state, patch->new_name, APPLY_SYMLINK_IN_RESULT);\n \t}\n }\n \n@@ -3769,9 +3769,9 @@ static int path_is_beyond_symlink_1(struct apply_state *state, struct strbuf *na\n \t\t\tbreak;\n \t\tname->buf[name->len] = '\\0';\n \t\tchange = check_symlink_changes(state, name->buf);\n-\t\tif (change & SYMLINK_IN_RESULT)\n+\t\tif (change & APPLY_SYMLINK_IN_RESULT)\n \t\t\treturn 1;\n-\t\tif (change & SYMLINK_GOES_AWAY)\n+\t\tif (change & APPLY_SYMLINK_GOES_AWAY)\n \t\t\t/*\n \t\t\t * This cannot be \"return 0\", because we may\n \t\t\t * see a new one created at a higher level.\n-- \n2.10.0.41.g9df52c3\n\n"},{"id":"301036","messageId":"20160904201833.21676-3-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"[PATCH v14 02/41] apply: move 'struct apply_state' to apply.h","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:17:54Z","receivedAt":"2016-09-04T20:18:59Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify `git apply` functionality we must make 'struct apply_state'\nusable outside \"builtin/apply.c\".\n\nLet's do that by creating a new \"apply.h\" and moving\n'struct apply_state' there.\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n apply.h         | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n builtin/apply.c |  98 +-----------------------------------------------------\n 2 files changed, 101 insertions(+), 97 deletions(-)\n create mode 100644 apply.h\n\ndiff --git a/apply.h b/apply.h\nnew file mode 100644\nindex 0000000..7493a40\n--- /dev/null\n+++ b/apply.h\n@@ -0,0 +1,100 @@\n+#ifndef APPLY_H\n+#define APPLY_H\n+\n+enum apply_ws_error_action {\n+\tnowarn_ws_error,\n+\twarn_on_ws_error,\n+\tdie_on_ws_error,\n+\tcorrect_ws_error\n+};\n+\n+enum apply_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 APPLY_SYMLINK_GOES_AWAY 01\n+#define APPLY_SYMLINK_IN_RESULT 02\n+\n+struct apply_state {\n+\tconst char *prefix;\n+\tint prefix_length;\n+\n+\t/* These are lock_file related */\n+\tstruct lock_file *lock_file;\n+\tint newfd;\n+\n+\t/* These control what gets looked at and modified */\n+\tint apply; /* this is not a dry-run */\n+\tint cached; /* apply to the index only */\n+\tint check; /* preimage must match working tree, don't actually apply */\n+\tint check_index; /* preimage must match the indexed version */\n+\tint update_index; /* check_index && apply */\n+\n+\t/* These control cosmetic aspect of the output */\n+\tint diffstat; /* just show a diffstat, and don't actually apply */\n+\tint numstat; /* just show a numeric diffstat, and don't actually apply */\n+\tint summary; /* just report creation, deletion, etc, and don't actually apply */\n+\n+\t/* These boolean parameters control how the apply is done */\n+\tint allow_overlap;\n+\tint apply_in_reverse;\n+\tint apply_with_reject;\n+\tint apply_verbosely;\n+\tint no_add;\n+\tint threeway;\n+\tint unidiff_zero;\n+\tint unsafe_paths;\n+\n+\t/* Other non boolean parameters */\n+\tconst char *fake_ancestor;\n+\tconst char *patch_input_file;\n+\tint line_termination;\n+\tstruct strbuf root;\n+\tint p_value;\n+\tint p_value_known;\n+\tunsigned int p_context;\n+\n+\t/* Exclude and include path parameters */\n+\tstruct string_list limit_by_name;\n+\tint has_include;\n+\n+\t/* Various \"current state\" */\n+\tint linenr; /* current line number */\n+\tstruct string_list symlink_changes; /* we have to track symlinks */\n+\n+\t/*\n+\t * For \"diff-stat\" like behaviour, we keep track of the biggest change\n+\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 * Records filenames that have been touched, in order to handle\n+\t * the case where more than one patches touch the same file.\n+\t */\n+\tstruct string_list fn_table;\n+\n+\t/* These control whitespace errors */\n+\tenum apply_ws_error_action ws_error_action;\n+\tenum apply_ws_ignore ws_ignore_action;\n+\tconst char *whitespace_option;\n+\tint whitespace_error;\n+\tint squelch_whitespace_errors;\n+\tint applied_after_fixing_ws;\n+};\n+\n+#endif\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex ab8f0bd..ed923ca 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -20,103 +20,7 @@\n #include \"xdiff-interface.h\"\n #include \"ll-merge.h\"\n #include \"rerere.h\"\n-\n-enum apply_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 apply_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 APPLY_SYMLINK_GOES_AWAY 01\n-#define APPLY_SYMLINK_IN_RESULT 02\n-\n-struct apply_state {\n-\tconst char *prefix;\n-\tint prefix_length;\n-\n-\t/* These are lock_file related */\n-\tstruct lock_file *lock_file;\n-\tint newfd;\n-\n-\t/* These control what gets looked at and modified */\n-\tint apply; /* this is not a dry-run */\n-\tint cached; /* apply to the index only */\n-\tint check; /* preimage must match working tree, don't actually apply */\n-\tint check_index; /* preimage must match the indexed version */\n-\tint update_index; /* check_index && apply */\n-\n-\t/* These control cosmetic aspect of the output */\n-\tint diffstat; /* just show a diffstat, and don't actually apply */\n-\tint numstat; /* just show a numeric diffstat, and don't actually apply */\n-\tint summary; /* just report creation, deletion, etc, and don't actually apply */\n-\n-\t/* These boolean parameters control how the apply is done */\n-\tint allow_overlap;\n-\tint apply_in_reverse;\n-\tint apply_with_reject;\n-\tint apply_verbosely;\n-\tint no_add;\n-\tint threeway;\n-\tint unidiff_zero;\n-\tint unsafe_paths;\n-\n-\t/* Other non boolean parameters */\n-\tconst char *fake_ancestor;\n-\tconst char *patch_input_file;\n-\tint line_termination;\n-\tstruct strbuf root;\n-\tint p_value;\n-\tint p_value_known;\n-\tunsigned int p_context;\n-\n-\t/* Exclude and include path parameters */\n-\tstruct string_list limit_by_name;\n-\tint has_include;\n-\n-\t/* Various \"current state\" */\n-\tint linenr; /* current line number */\n-\tstruct string_list symlink_changes; /* we have to track symlinks */\n-\n-\t/*\n-\t * For \"diff-stat\" like behaviour, we keep track of the biggest change\n-\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 * Records filenames that have been touched, in order to handle\n-\t * the case where more than one patches touch the same file.\n-\t */\n-\tstruct string_list fn_table;\n-\n-\t/* These control whitespace errors */\n-\tenum apply_ws_error_action ws_error_action;\n-\tenum apply_ws_ignore ws_ignore_action;\n-\tconst char *whitespace_option;\n-\tint whitespace_error;\n-\tint squelch_whitespace_errors;\n-\tint applied_after_fixing_ws;\n-};\n+#include \"apply.h\"\n \n static const char * const apply_usage[] = {\n \tN_(\"git apply [<options>] [<patch>...]\"),\n-- \n2.10.0.41.g9df52c3\n\n"},{"id":"301037","messageId":"20160904201833.21676-5-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"[PATCH v14 04/41] builtin/apply: read_patch_file() return -1 instead of die()ing","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:17:56Z","receivedAt":"2016-09-04T20:19:02Z","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 to the\ncaller instead of die()ing. Let's do that by returning -1 instead of\ndie()ing in read_patch_file().\n\nHelped-by: Stefan Beller <sbeller@google.com>\nSigned-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 435030a..dd7afee 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -335,10 +335,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_errno(\"git apply: failed to read\");\n \n \t/*\n \t * Make sure that we have some slop in the buffer\n@@ -347,6 +347,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@@ -4425,7 +4426,8 @@ static int apply_patch(struct apply_state *state,\n \tint res = 0;\n \n \tstate->patch_input_file = filename;\n-\tread_patch_file(&buf, fd);\n+\tif (read_patch_file(&buf, fd) < 0)\n+\t\treturn -128;\n \toffset = 0;\n \twhile (offset < buf.len) {\n \t\tstruct patch *patch;\n-- \n2.10.0.41.g9df52c3\n\n"},{"id":"301038","messageId":"20160904201833.21676-4-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"[PATCH v14 03/41] builtin/apply: make apply_patch() return -1 or -128 instead of die()ing","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:17:55Z","receivedAt":"2016-09-04T20:19:05Z","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 or -128 in case of errors instead of dying. For now its only\ncaller apply_all_patches() will exit(128) when apply_patch()\nreturn -128 and it will exit(1) when it returns -1.\n\nWe exit() with code 128 because that was what die() was doing\nand we want to keep the distinction between exiting with code 1\nand exiting with code 128.\n\nHelped-by: Eric Sunshine <sunshine@sunshineco.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 60 ++++++++++++++++++++++++++++++++++++++++++---------------\n 1 file changed, 45 insertions(+), 15 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex ed923ca..435030a 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -4404,6 +4404,15 @@ static struct lock_file lock_file;\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+ *  -128 if a bad error happened (like patch unreadable)\n+ *  -1 if patch did not apply and user cannot deal with it\n+ *   0 if the patch applied\n+ *   1 if the patch did not apply but user might fix it\n+ */\n static int apply_patch(struct apply_state *state,\n \t\t       int fd,\n \t\t       const char *filename,\n@@ -4413,6 +4422,7 @@ static int apply_patch(struct apply_state *state,\n \tstruct strbuf buf = STRBUF_INIT; /* owns the patch text */\n \tstruct patch *list = NULL, **listp = &list;\n \tint skipped_patch = 0;\n+\tint res = 0;\n \n \tstate->patch_input_file = filename;\n \tread_patch_file(&buf, fd);\n@@ -4445,8 +4455,11 @@ static int apply_patch(struct apply_state *state,\n \t\toffset += nr;\n \t}\n \n-\tif (!list && !skipped_patch)\n-\t\tdie(_(\"unrecognized input\"));\n+\tif (!list && !skipped_patch) {\n+\t\terror(_(\"unrecognized input\"));\n+\t\tres = -128;\n+\t\tgoto end;\n+\t}\n \n \tif (state->whitespace_error && (state->ws_error_action == die_on_ws_error))\n \t\tstate->apply = 0;\n@@ -4455,21 +4468,23 @@ static int apply_patch(struct apply_state *state,\n \tif (state->update_index && state->newfd < 0)\n \t\tstate->newfd = hold_locked_index(state->lock_file, 1);\n \n-\tif (state->check_index) {\n-\t\tif (read_cache() < 0)\n-\t\t\tdie(_(\"unable to read index file\"));\n+\tif (state->check_index && read_cache() < 0) {\n+\t\terror(_(\"unable to read index file\"));\n+\t\tres = -128;\n+\t\tgoto end;\n \t}\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    !state->apply_with_reject) {\n+\t\tres = -1;\n+\t\tgoto end;\n+\t}\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/* with --3way, we still need to write the index out */\n-\t\treturn 1;\n+\t\tres = state->apply_with_reject ? -1 : 1;\n+\t\tgoto end;\n \t}\n \n \tif (state->fake_ancestor)\n@@ -4484,10 +4499,11 @@ static int apply_patch(struct apply_state *state,\n \tif (state->summary)\n \t\tsummary_patch_list(list);\n \n+end:\n \tfree_patch_list(list);\n \tstrbuf_release(&buf);\n \tstring_list_clear(&state->fn_table, 0);\n-\treturn 0;\n+\treturn res;\n }\n \n static void git_apply_config(void)\n@@ -4628,6 +4644,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@@ -4636,7 +4653,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\tgoto end;\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@@ -4649,12 +4669,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\tgoto end;\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\tgoto end;\n+\t\terrs |= res;\n+\t}\n \n \tif (state->whitespace_error) {\n \t\tif (state->squelch_whitespace_errors &&\n@@ -4690,6 +4717,9 @@ static int apply_all_patches(struct apply_state *state,\n \t}\n \n \treturn !!errs;\n+\n+end:\n+\texit(res == -1 ? 1 : 128);\n }\n \n int cmd_apply(int argc, const char **argv, const char *prefix)\n-- \n2.10.0.41.g9df52c3\n\n"},{"id":"301039","messageId":"20160904201833.21676-13-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"[PATCH v14 12/41] builtin/apply: make check_apply_state() return -1 instead of die()ing","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:18:04Z","receivedAt":"2016-09-04T20:19:26Z","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 to the\ncaller instead of die()ing.\n\nTo do that in a compatible manner with the rest of the error handling\nin \"builtin/apply.c\", check_apply_state() should return -1 instead of\ncalling die().\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 61fd316..bb89e07 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -4551,17 +4551,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@@ -4569,16 +4569,18 @@ 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 \tif (!state->lock_file)\n-\t\tdie(\"BUG: state->lock_file should not be NULL\");\n+\t\treturn error(\"BUG: state->lock_file should not be NULL\");\n+\n+\treturn 0;\n }\n \n static int apply_all_patches(struct apply_state *state,\n@@ -4747,7 +4749,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(128);\n \n \tret = apply_all_patches(&state, argc, argv, options);\n \n-- \n2.10.0.41.g9df52c3\n\n"},{"id":"301040","messageId":"20160904201833.21676-12-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"[PATCH v14 11/41] apply: make init_apply_state() return -1 instead of exit()ing","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:18:03Z","receivedAt":"2016-09-04T20:19:30Z","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 to the\ncaller instead of exit()ing.\n\nTo do that in a compatible manner with the rest of the error handling\nin \"builtin/apply.c\", init_apply_state() should return -1 instead of\ncalling exit().\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n apply.c         | 11 ++++++-----\n apply.h         |  6 +++---\n builtin/apply.c |  3 ++-\n 3 files changed, 11 insertions(+), 9 deletions(-)\n\ndiff --git a/apply.c b/apply.c\nindex c858ca4..6e0e992 100644\n--- a/apply.c\n+++ b/apply.c\n@@ -55,9 +55,9 @@ 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,\n-\t\t      const char *prefix,\n-\t\t      struct lock_file *lock_file)\n+int init_apply_state(struct apply_state *state,\n+\t\t     const char *prefix,\n+\t\t     struct lock_file *lock_file)\n {\n \tmemset(state, 0, sizeof(*state));\n \tstate->prefix = prefix;\n@@ -79,9 +79,10 @@ void init_apply_state(struct apply_state *state,\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 \n void clear_apply_state(struct apply_state *state)\ndiff --git a/apply.h b/apply.h\nindex 08c0a25..e18a18a 100644\n--- a/apply.h\n+++ b/apply.h\n@@ -102,9 +102,9 @@ 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,\n-\t\t\t     const char *prefix,\n-\t\t\t     struct lock_file *lock_file);\n+extern int init_apply_state(struct apply_state *state,\n+\t\t\t    const char *prefix,\n+\t\t\t    struct lock_file *lock_file);\n extern void clear_apply_state(struct apply_state *state);\n \n #endif\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex bb6ff77..61fd316 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -4741,7 +4741,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, &lock_file);\n+\tif (init_apply_state(&state, prefix, &lock_file))\n+\t\texit(128);\n \n \targc = parse_options(argc, argv, state.prefix, builtin_apply_options,\n \t\t\tapply_usage, 0);\n-- \n2.10.0.41.g9df52c3\n\n"},{"id":"301041","messageId":"20160904201833.21676-15-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"[PATCH v14 14/41] builtin/apply: make apply_all_patches() return 128 or 1 on error","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:18:06Z","receivedAt":"2016-09-04T20:19:31Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To finish libifying the apply functionality, apply_all_patches() should not\ndie() or exit() in case of error, but return either 128 or 1, so that it\ngives the same exit code as when die() or exit(1) is called. This way\nscripts relying on the exit code don't need to be changed.\n\nWhile doing that we must take care that file descriptors are properly closed\nand, if needed, reset to a sensible value.\n\nAlso, according to the lockfile API, when finished with a lockfile, one\nshould either commit it or roll it back.\n\nThis is even more important now that the same lockfile can be passed\nto init_apply_state() many times to be reused by series of calls to\nthe apply lib functions.\n\nHelped-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nHelped-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>\nHelped-by: Eric Sunshine <sunshine@sunshineco.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 37 ++++++++++++++++++++++++++-----------\n 1 file changed, 26 insertions(+), 11 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 075ada4..5530ba1 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -4578,15 +4578,18 @@ static int apply_all_patches(struct apply_state *state,\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\tif (fd < 0) {\n+\t\t\terror(_(\"can't open patch '%s': %s\"), arg, strerror(errno));\n+\t\t\tres = -128;\n+\t\t\tgoto end;\n+\t\t}\n \t\tread_stdin = 0;\n \t\tset_default_whitespace_mode(state);\n \t\tres = apply_patch(state, fd, arg, options);\n+\t\tclose(fd);\n \t\tif (res < 0)\n \t\t\tgoto end;\n \t\terrs |= res;\n-\t\tclose(fd);\n \t}\n \tset_default_whitespace_mode(state);\n \tif (read_stdin) {\n@@ -4606,11 +4609,14 @@ static int apply_all_patches(struct apply_state *state,\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->ws_error_action == die_on_ws_error) {\n+\t\t\terror(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      state->whitespace_error);\n+\t\t\tres = -128;\n+\t\t\tgoto end;\n+\t\t}\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@@ -4624,15 +4630,24 @@ 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, state->lock_file, COMMIT_LOCK))\n-\t\t\tdie(_(\"Unable to write new index file\"));\n+\t\tres = write_locked_index(&the_index, state->lock_file, COMMIT_LOCK);\n+\t\tif (res) {\n+\t\t\terror(_(\"Unable to write new index file\"));\n+\t\t\tres = -128;\n+\t\t\tgoto end;\n+\t\t}\n \t\tstate->newfd = -1;\n \t}\n \n \treturn !!errs;\n \n end:\n-\texit(res == -1 ? 1 : 128);\n+\tif (state->newfd >= 0) {\n+\t\trollback_lock_file(state->lock_file);\n+\t\tstate->newfd = -1;\n+\t}\n+\n+\treturn (res == -1 ? 1 : 128);\n }\n \n int cmd_apply(int argc, const char **argv, const char *prefix)\n-- \n2.10.0.41.g9df52c3\n\n"},{"id":"301042","messageId":"20160904201833.21676-16-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"[PATCH v14 15/41] builtin/apply: make parse_traditional_patch() return -1 on error","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:18:07Z","receivedAt":"2016-09-04T20:19:32Z","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 to the\ncaller instead of die()ing.\n\nTo do that in a compatible manner with the rest of the error handling\nin \"builtin/apply.c\", parse_traditional_patch() should return -1\ninstead of calling die().\n\nSigned-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 5530ba1..f99498b 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@@ -1467,7 +1469,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 -128;\n \t\t*hdrsize = len + nextlen;\n \t\tstate->linenr += 2;\n \t\treturn offset;\n-- \n2.10.0.41.g9df52c3\n\n"},{"id":"301043","messageId":"20160904201833.21676-18-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"[PATCH v14 17/41] builtin/apply: make gitdiff_*() return -1 on error","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:18:09Z","receivedAt":"2016-09-04T20:19:33Z","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 to the\ncaller instead of die()ing.\n\nTo do that in a compatible manner with the rest of the error handling\nin \"builtin/apply.c\", gitdiff_*() functions should return -1 instead\nof calling die().\n\nA previous patch made it possible for gitdiff_*() functions to\nreturn -1 in case of error. Let's take advantage of that to\nmake gitdiff_verify_name() return -1 on error, and to have\ngitdiff_oldname() and gitdiff_newname() directly return\nwhat gitdiff_verify_name() returns.\n\nHelped-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 40 +++++++++++++++++++++-------------------\n 1 file changed, 21 insertions(+), 19 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex eb918e5..6b16173 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\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-- \n2.10.0.41.g9df52c3\n\n"},{"id":"301044","messageId":"20160904201833.21676-14-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"[PATCH v14 13/41] builtin/apply: move check_apply_state() to apply.c","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:18:05Z","receivedAt":"2016-09-04T20:19:35Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify `git apply` functionality we must make check_apply_state()\nusable outside \"builtin/apply.c\".\n\nLet's do that by moving it into \"apply.c\".\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n apply.c         | 32 ++++++++++++++++++++++++++++++++\n apply.h         |  1 +\n builtin/apply.c | 32 --------------------------------\n 3 files changed, 33 insertions(+), 32 deletions(-)\n\ndiff --git a/apply.c b/apply.c\nindex 6e0e992..2eac3e3 100644\n--- a/apply.c\n+++ b/apply.c\n@@ -93,3 +93,35 @@ void clear_apply_state(struct apply_state *state)\n \n \t/* &state->fn_table is cleared at the end of apply_patch() */\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+\tif (!state->lock_file)\n+\t\treturn error(\"BUG: state->lock_file should not be NULL\");\n+\n+\treturn 0;\n+}\ndiff --git a/apply.h b/apply.h\nindex e18a18a..53f09b5 100644\n--- a/apply.h\n+++ b/apply.h\n@@ -106,5 +106,6 @@ extern int init_apply_state(struct apply_state *state,\n \t\t\t    const char *prefix,\n \t\t\t    struct lock_file *lock_file);\n extern void clear_apply_state(struct apply_state *state);\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 bb89e07..075ada4 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -4551,38 +4551,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-\tif (!state->lock_file)\n-\t\treturn error(\"BUG: state->lock_file should not be NULL\");\n-\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.10.0.41.g9df52c3\n\n"},{"id":"301045","messageId":"20160904201833.21676-20-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"[PATCH v14 19/41] builtin/apply: make build_fake_ancestor() return -1 on error","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:18:11Z","receivedAt":"2016-09-04T20:19:36Z","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 to the\ncaller instead of die()ing.\n\nTo do that in a compatible manner with the rest of the error handling\nin \"builtin/apply.c\", build_fake_ancestor() should return -1 instead\nof calling die().\n\nHelped-by: Eric Sunshine <sunshine@sunshineco.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 41 ++++++++++++++++++++++++++---------------\n 1 file changed, 26 insertions(+), 15 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 166e94d..575981b 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -3900,11 +3900,12 @@ 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 \tstatic struct lock_file lock;\n+\tint res;\n \n \t/* Once we start supporting the reverse patch, it may be\n \t * worth showing the new sha1 prefix, but until then...\n@@ -3922,31 +3923,38 @@ 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\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(_(\"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\tfree(ce);\n+\t\t\treturn error(\"Could not add %s to temporary index\",\n+\t\t\t\t     name);\n+\t\t}\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-\n+\tres = write_locked_index(&result, &lock, COMMIT_LOCK);\n \tdiscard_index(&result);\n+\n+\tif (res)\n+\t\treturn error(\"Could not write temporary index to %s\", filename);\n+\n+\treturn 0;\n }\n \n static void stat_patch_list(struct apply_state *state, struct patch *patch)\n@@ -4495,8 +4503,11 @@ static int apply_patch(struct apply_state *state,\n \t\tgoto end;\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\tres = -128;\n+\t\tgoto end;\n+\t}\n \n \tif (state->diffstat)\n \t\tstat_patch_list(state, list);\n-- \n2.10.0.41.g9df52c3\n\n"},{"id":"301046","messageId":"20160904201833.21676-21-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"[PATCH v14 20/41] builtin/apply: make remove_file() return -1 on error","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:18:12Z","receivedAt":"2016-09-04T20:19:54Z","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 to the\ncaller instead of die()ing.\n\nTo do that in a compatible manner with the rest of the error handling\nin \"builtin/apply.c\", remove_file() should return -1 instead of\ncalling die().\n\nSigned-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 575981b..27fb6e2 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -4085,17 +4085,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@@ -4274,8 +4275,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(128);\n+\t\t}\n \t\treturn;\n \t}\n \tif (patch->is_new > 0 || patch->is_copy) {\n@@ -4287,8 +4290,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(128);\n+\t}\n \tif (phase == 1)\n \t\tcreate_file(state, patch);\n }\n-- \n2.10.0.41.g9df52c3\n\n"},{"id":"301047","messageId":"20160904201833.21676-17-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"[PATCH v14 16/41] builtin/apply: make gitdiff_*() return 1 at end of header","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:18:08Z","receivedAt":"2016-09-04T20:19:56Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"The gitdiff_*() functions that are called as p->fn() in parse_git_header()\nshould return 1 instead of -1 in case of end of header or unrecognized\ninput, as these are not real errors. It just instructs the parser to break\nout.\n\nThis makes it possible for gitdiff_*() functions to return -1 in case of a\nreal error. This will be done in a following patch.\n\nHelped-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 12 +++++++++---\n 1 file changed, 9 insertions(+), 3 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex f99498b..eb918e5 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@@ -1016,7 +1016,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 +1248,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@@ -1430,6 +1434,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 -128;\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.10.0.41.g9df52c3\n\n"},{"id":"301048","messageId":"20160904201833.21676-24-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"[PATCH v14 23/41] builtin/apply: make create_file() return -1 on error","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:18:15Z","receivedAt":"2016-09-04T20:19:57Z","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 to the\ncaller instead of exit()ing.\n\nTo do that in a compatible manner with the rest of the error handling\nin \"builtin/apply.c\", create_file() should just return what\nadd_conflicted_stages_file() and add_index_file() are returning\ninstead of calling exit().\n\nSigned-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 a646900..fdfeab0 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -4269,7 +4269,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@@ -4280,13 +4280,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(128);\n-\t} else {\n-\t\tif (add_index_file(state, path, mode, buf, size))\n-\t\t\texit(128);\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@@ -4302,8 +4299,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(128);\n+\t\t}\n \t\treturn;\n \t}\n \t/*\n@@ -4314,8 +4313,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(128);\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(128);\n+\t}\n }\n \n static int write_out_one_reject(struct apply_state *state, struct patch *patch)\n-- \n2.10.0.41.g9df52c3\n\n"},{"id":"301049","messageId":"20160904201833.21676-23-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"[PATCH v14 22/41] builtin/apply: make add_index_file() return -1 on error","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:18:14Z","receivedAt":"2016-09-04T20:19:59Z","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 to the\ncaller instead of die()ing.\n\nTo do that in a compatible manner with the rest of the error handling\nin \"builtin/apply.c\", add_index_file() should return -1 instead of\ncalling die().\n\nSigned-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 ad0b875..a646900 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -4099,11 +4099,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@@ -4111,7 +4111,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@@ -4122,20 +4122,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\tdie(_(\"unable to add cache entry for %s\"), path);\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+\n+\treturn 0;\n }\n \n static int try_create_file(const char *path, unsigned int mode, const char *buf, unsigned long size)\n@@ -4271,8 +4283,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(128);\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(128);\n+\t}\n }\n \n /* phase zero is to remove, phase one is to create */\n-- \n2.10.0.41.g9df52c3\n\n"},{"id":"301050","messageId":"20160904201833.21676-19-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"[PATCH v14 18/41] builtin/apply: change die_on_unsafe_path() to check_unsafe_path()","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:18:10Z","receivedAt":"2016-09-04T20:20:00Z","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 to the\ncaller instead of die()ing.\n\nTo do that in a compatible manner with the rest of the error handling\nin \"builtin/apply.c\", die_on_unsafe_path() should return a negative\ninteger instead of calling die(), so while doing that let's change\nits name to check_unsafe_path().\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 32 +++++++++++++++++++++-----------\n 1 file changed, 21 insertions(+), 11 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 6b16173..166e94d 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -3704,7 +3704,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@@ -3716,9 +3716,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@@ -3808,8 +3809,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 -128;\n \n \t/*\n \t * An attempt to read from or delete a path that is beyond a\n@@ -3837,10 +3838,14 @@ static int check_patch_list(struct apply_state *state, struct patch *patch)\n \tprepare_symlink_changes(state, patch);\n \tprepare_fn_table(state, patch);\n \twhile (patch) {\n+\t\tint res;\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+\t\tres = check_patch(state, patch);\n+\t\tif (res == -128)\n+\t\t\treturn -128;\n+\t\terr |= res;\n \t\tpatch = patch->next;\n \t}\n \treturn err;\n@@ -4472,11 +4477,16 @@ static int apply_patch(struct apply_state *state,\n \t\tgoto end;\n \t}\n \n-\tif ((state->check || state->apply) &&\n-\t    check_patch_list(state, list) < 0 &&\n-\t    !state->apply_with_reject) {\n-\t\tres = -1;\n-\t\tgoto end;\n+\tif (state->check || state->apply) {\n+\t\tint r = check_patch_list(state, list);\n+\t\tif (r == -128) {\n+\t\t\tres = -128;\n+\t\t\tgoto end;\n+\t\t}\n+\t\tif (r < 0 && !state->apply_with_reject) {\n+\t\t\tres = -1;\n+\t\t\tgoto end;\n+\t\t}\n \t}\n \n \tif (state->apply && write_out_results(state, list)) {\n-- \n2.10.0.41.g9df52c3\n\n"},{"id":"301051","messageId":"20160904201833.21676-25-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"[PATCH v14 24/41] builtin/apply: make write_out_one_result() return -1 on error","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:18:16Z","receivedAt":"2016-09-04T20:20:01Z","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 to the\ncaller instead of exit()ing.\n\nTo do that in a compatible manner with the rest of the error handling\nin \"builtin/apply.c\", write_out_one_result() should just return what\nremove_file() and create_file() are returning instead of calling\nexit().\n\nSigned-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 fdfeab0..003acec 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -4287,36 +4287,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(128);\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(128);\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(128);\n-\t}\n-\tif (phase == 1) {\n-\t\tif (create_file(state, patch))\n-\t\t\texit(128);\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@@ -4403,7 +4396,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(128);\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.10.0.41.g9df52c3\n\n"},{"id":"301052","messageId":"20160904201833.21676-22-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"[PATCH v14 21/41] builtin/apply: make add_conflicted_stages_file() return -1 on error","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:18:13Z","receivedAt":"2016-09-04T20:20:02Z","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 to the\ncaller instead of die()ing.\n\nTo do that in a compatible manner with the rest of the error handling\nin \"builtin/apply.c\", add_conflicted_stages_file() should return -1\ninstead of calling die().\n\nHelped-by: Eric Sunshine <sunshine@sunshineco.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 20 +++++++++++++-------\n 1 file changed, 13 insertions(+), 7 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 27fb6e2..ad0b875 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -4224,7 +4224,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@@ -4232,7 +4232,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@@ -4247,9 +4247,14 @@ static void add_conflicted_stages_file(struct apply_state *state,\n \t\tce->ce_flags = create_ce_flags(stage);\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\tif (add_cache_entry(ce, ADD_CACHE_OK_TO_ADD) < 0) {\n+\t\t\tfree(ce);\n+\t\t\treturn error(_(\"unable to add cache entry for %s\"),\n+\t\t\t\t     patch->new_name);\n+\t\t}\n \t}\n+\n+\treturn 0;\n }\n \n static void create_file(struct apply_state *state, struct patch *patch)\n@@ -4263,9 +4268,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(128);\n+\t} else\n \t\tadd_index_file(state, path, mode, buf, size);\n }\n \n-- \n2.10.0.41.g9df52c3\n\n"},{"id":"301053","messageId":"20160904201833.21676-26-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"[PATCH v14 25/41] builtin/apply: make write_out_results() return -1 on error","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:18:17Z","receivedAt":"2016-09-04T20:20:03Z","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 to the\ncaller instead of exit()ing.\n\nTo do that in a compatible manner with the rest of the error handling\nin \"builtin/apply.c\", write_out_results() should return -1 instead of\ncalling exit().\n\nHelped-by: Eric Sunshine <sunshine@sunshineco.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 27 +++++++++++++++++++++------\n 1 file changed, 21 insertions(+), 6 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 003acec..c787ead 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -4383,6 +4383,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@@ -4396,8 +4402,10 @@ 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\tif (write_out_one_result(state, l, phase))\n-\t\t\t\t\texit(128);\n+\t\t\t\tif (write_out_one_result(state, l, phase)) {\n+\t\t\t\t\tstring_list_clear(&cpath, 0);\n+\t\t\t\t\treturn -1;\n+\t\t\t\t}\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@@ -4517,10 +4525,17 @@ static int apply_patch(struct apply_state *state,\n \t\t}\n \t}\n \n-\tif (state->apply && write_out_results(state, list)) {\n-\t\t/* with --3way, we still need to write the index out */\n-\t\tres = state->apply_with_reject ? -1 : 1;\n-\t\tgoto end;\n+\tif (state->apply) {\n+\t\tint write_res = write_out_results(state, list);\n+\t\tif (write_res < 0) {\n+\t\t\tres = -128;\n+\t\t\tgoto end;\n+\t\t}\n+\t\tif (write_res > 0) {\n+\t\t\t/* with --3way, we still need to write the index out */\n+\t\t\tres = state->apply_with_reject ? -1 : 1;\n+\t\t\tgoto end;\n+\t\t}\n \t}\n \n \tif (state->fake_ancestor &&\n-- \n2.10.0.41.g9df52c3\n\n"},{"id":"301054","messageId":"20160904201833.21676-30-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"[PATCH v14 29/41] apply: rename and move opt constants to apply.h","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:18:21Z","receivedAt":"2016-09-04T20:20:04Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"The constants for the \"inaccurate-eof\" and the \"recount\" options will\nbe used in both \"apply.c\" and \"builtin/apply.c\", so they need to go\ninto \"apply.h\", and therefore they need a name that is more specific\nto the API they belong to.\n\nHelped-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n apply.h         |  7 +++++++\n builtin/apply.c | 11 ++++-------\n 2 files changed, 11 insertions(+), 7 deletions(-)\n\ndiff --git a/apply.h b/apply.h\nindex 53f09b5..48abd8e 100644\n--- a/apply.h\n+++ b/apply.h\n@@ -108,4 +108,11 @@ extern int init_apply_state(struct apply_state *state,\n extern void clear_apply_state(struct apply_state *state);\n extern int check_apply_state(struct apply_state *state, int force_apply);\n \n+/*\n+ * Some aspects of the apply behavior are controlled by the following\n+ * bits in the \"options\" parameter passed to apply_all_patches().\n+ */\n+#define APPLY_OPT_INACCURATE_EOF\t(1<<0) /* accept inaccurate eof */\n+#define APPLY_OPT_RECOUNT\t\t(1<<1) /* accept inaccurate line count */\n+\n #endif\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 1f56303..da31af2 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -4463,9 +4463,6 @@ static int write_out_results(struct apply_state *state, struct patch *list)\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 /*\n  * Try to apply a patch.\n  *\n@@ -4495,8 +4492,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@@ -4811,10 +4808,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.10.0.41.g9df52c3\n\n"},{"id":"301055","messageId":"20160904201833.21676-28-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"[PATCH v14 27/41] builtin/apply: make create_one_file() return -1 on error","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:18:19Z","receivedAt":"2016-09-04T20:20:05Z","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 to the\ncaller instead of exit()ing.\n\nTo do that in a compatible manner with the rest of the error handling\nin \"builtin/apply.c\", create_one_file() should return -1 instead of\ncalling exit().\n\nSigned-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 3145e03..5cd7fd8 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -4198,32 +4198,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 < 0)\n-\t\texit(128);\n+\t\treturn -1;\n \tif (!res)\n-\t\treturn;\n+\t\treturn 0;\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 < 0)\n-\t\t\texit(128);\n+\t\t\treturn -1;\n \t\tif (!res)\n-\t\t\treturn;\n+\t\t\treturn 0;\n \t}\n \n \tif (errno == EEXIST || errno == EACCES) {\n@@ -4243,10 +4247,10 @@ static void create_one_file(struct apply_state *state,\n \t\t\tmksnpath(newpath, sizeof(newpath), \"%s~%u\", path, nr);\n \t\t\tres = try_create_file(newpath, mode, buf, size);\n \t\t\tif (res < 0)\n-\t\t\t\texit(128);\n+\t\t\t\treturn -1;\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@@ -4255,7 +4259,8 @@ static void create_one_file(struct apply_state *state,\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_errno(_(\"unable to write file '%s' mode %o\"),\n+\t\t\t   path, mode);\n }\n \n static int add_conflicted_stages_file(struct apply_state *state,\n@@ -4300,7 +4305,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.10.0.41.g9df52c3\n\n"},{"id":"301056","messageId":"20160904201833.21676-32-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"[PATCH v14 31/41] apply: make some parsing functions static again","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:18:23Z","receivedAt":"2016-09-04T20:20:07Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Some parsing functions that were used in both \"apply.c\" and\n\"builtin/apply.c\" are now only used in the former, so they\ncan be made static to \"apply.c\".\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n apply.c | 6 +++---\n apply.h | 5 -----\n 2 files changed, 3 insertions(+), 8 deletions(-)\n\ndiff --git a/apply.c b/apply.c\nindex f640224..a4dfc64 100644\n--- a/apply.c\n+++ b/apply.c\n@@ -27,7 +27,7 @@ static void git_apply_config(void)\n \tgit_config(git_default_config, NULL);\n }\n \n-int 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@@ -57,8 +57,8 @@ int parse_whitespace_option(struct apply_state *state, const char *option)\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+static int parse_ignorewhitespace_option(struct apply_state *state,\n+\t\t\t\t\t\t const char *option)\n {\n \tif (!option || !strcmp(option, \"no\") ||\n \t    !strcmp(option, \"false\") || !strcmp(option, \"never\") ||\ndiff --git a/apply.h b/apply.h\nindex 28cbe6c..51b983b 100644\n--- a/apply.h\n+++ b/apply.h\n@@ -97,11 +97,6 @@ struct apply_state {\n \tint applied_after_fixing_ws;\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 int apply_option_parse_exclude(const struct option *opt,\n \t\t\t\t      const char *arg, int unset);\n extern int apply_option_parse_include(const struct option *opt,\n-- \n2.10.0.41.g9df52c3\n\n"},{"id":"301057","messageId":"20160904201833.21676-33-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"[PATCH v14 32/41] apply: use error_errno() where possible","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:18:24Z","receivedAt":"2016-09-04T20:20:10Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To avoid possible mistakes and to uniformly show the errno\nrelated messages, let's use error_errno() where possible.\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n apply.c | 16 ++++++++--------\n 1 file changed, 8 insertions(+), 8 deletions(-)\n\ndiff --git a/apply.c b/apply.c\nindex a4dfc64..41a33d3 100644\n--- a/apply.c\n+++ b/apply.c\n@@ -3497,7 +3497,7 @@ static int load_current(struct apply_state *state,\n \tce = active_cache[pos];\n \tif (lstat(name, &st)) {\n \t\tif (errno != ENOENT)\n-\t\t\treturn error(_(\"%s: %s\"), name, strerror(errno));\n+\t\t\treturn error_errno(\"%s\", name);\n \t\tif (checkout_target(&the_index, ce, &st))\n \t\t\treturn -1;\n \t}\n@@ -3647,7 +3647,7 @@ static int check_preimage(struct apply_state *state,\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+\t\t\treturn error_errno(\"%s\", old_name);\n \t}\n \n \tif (state->check_index && !previous) {\n@@ -3669,7 +3669,7 @@ static int check_preimage(struct apply_state *state,\n \t} else if (stat_ret < 0) {\n \t\tif (patch->is_new < 0)\n \t\t\tgoto is_new;\n-\t\treturn error(_(\"%s: %s\"), old_name, strerror(errno));\n+\t\treturn error_errno(\"%s\", old_name);\n \t}\n \n \tif (!state->cached && !previous)\n@@ -3728,7 +3728,7 @@ static int check_to_create(struct apply_state *state,\n \n \t\treturn EXISTS_IN_WORKTREE;\n \t} else if ((errno != ENOENT) && (errno != ENOTDIR)) {\n-\t\treturn error(\"%s: %s\", new_name, strerror(errno));\n+\t\treturn error_errno(\"%s\", new_name);\n \t}\n \treturn 0;\n }\n@@ -4247,9 +4247,9 @@ static int add_index_file(struct apply_state *state,\n \t\tif (!state->cached) {\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\treturn error_errno(_(\"unable to stat newly \"\n+\t\t\t\t\t\t     \"created file '%s'\"),\n+\t\t\t\t\t\t   path);\n \t\t\t}\n \t\t\tfill_stat_cache_info(ce, &st);\n \t\t}\n@@ -4503,7 +4503,7 @@ static int write_out_one_reject(struct apply_state *state, struct patch *patch)\n \n \trej = fopen(namebuf, \"w\");\n \tif (!rej)\n-\t\treturn error(_(\"cannot open %s: %s\"), namebuf, strerror(errno));\n+\t\treturn error_errno(_(\"cannot open %s\"), namebuf);\n \n \t/* Normal git tools never deal with .rej, so do not pretend\n \t * this is a git patch by saying --git or giving extended\n-- \n2.10.0.41.g9df52c3\n\n"},{"id":"301058","messageId":"20160904201833.21676-29-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"[PATCH v14 28/41] builtin/apply: rename option parsing functions","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:18:20Z","receivedAt":"2016-09-04T20:20:11Z","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 5cd7fd8..1f56303 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -4588,16 +4588,16 @@ static int apply_patch(struct apply_state *state,\n \treturn res;\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@@ -4605,9 +4605,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@@ -4615,8 +4615,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@@ -4626,8 +4626,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@@ -4636,8 +4636,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@@ -4755,13 +4755,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@@ -4793,13 +4793,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@@ -4817,7 +4817,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.10.0.41.g9df52c3\n\n"},{"id":"301059","messageId":"20160904201833.21676-34-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"[PATCH v14 33/41] apply: make it possible to silently apply","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:18:25Z","receivedAt":"2016-09-04T20:20:12Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"This changes 'int apply_verbosely' into 'enum apply_verbosity', and\nchanges the possible values of the variable from a bool to\na tristate.\n\nThe previous 'false' state is changed into 'verbosity_normal'.\nThe previous 'true' state is changed into 'verbosity_verbose'.\n\nThe new added state is 'verbosity_silent'. It should prevent\nanything to be printed on both stderr and stdout.\n\nThis is needed because `git am` wants to first call apply\nfunctionality silently, if it can then fall back on 3-way merge\nin case of error.\n\nPrinting on stdout, and calls to warning() or error() are not\ntaken care of in this patch, as that will be done in following\npatches.\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n apply.c         | 62 +++++++++++++++++++++++++++++++++++++--------------------\n apply.h         |  8 +++++++-\n builtin/apply.c |  2 +-\n 3 files changed, 48 insertions(+), 24 deletions(-)\n\ndiff --git a/apply.c b/apply.c\nindex 41a33d3..df85cbc 100644\n--- a/apply.c\n+++ b/apply.c\n@@ -125,8 +125,11 @@ int check_apply_state(struct apply_state *state, int force_apply)\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 (state->apply_with_reject) {\n+\t\tstate->apply = 1;\n+\t\tif (state->apply_verbosity == verbosity_normal)\n+\t\t\tstate->apply_verbosity = verbosity_verbose;\n+\t}\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@@ -1620,8 +1623,9 @@ static void record_ws_error(struct apply_state *state,\n \t\treturn;\n \n \terr = whitespace_error_string(result);\n-\tfprintf(stderr, \"%s:%d: %s.\\n%.*s\\n\",\n-\t\tstate->patch_input_file, linenr, err, len, line);\n+\tif (state->apply_verbosity > verbosity_silent)\n+\t\tfprintf(stderr, \"%s:%d: %s.\\n%.*s\\n\",\n+\t\t\tstate->patch_input_file, linenr, err, len, line);\n \tfree(err);\n }\n \n@@ -1816,7 +1820,7 @@ static int parse_single_patch(struct apply_state *state,\n \t\treturn error(_(\"new file %s depends on old contents\"), patch->new_name);\n \tif (0 < patch->is_delete && newlines)\n \t\treturn error(_(\"deleted file %s still has contents\"), patch->old_name);\n-\tif (!patch->is_delete && !newlines && context)\n+\tif (!patch->is_delete && !newlines && context && state->apply_verbosity > verbosity_silent)\n \t\tfprintf_ln(stderr,\n \t\t\t   _(\"** warning: \"\n \t\t\t     \"file %s becomes empty but is not deleted\"),\n@@ -2911,7 +2915,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 (state->apply_verbosely)\n+\t\t\tif (state->apply_verbosity > verbosity_normal)\n \t\t\t\terror(_(\"invalid start of line: '%c'\"), first);\n \t\t\tapplied_pos = -1;\n \t\t\tgoto out;\n@@ -3026,7 +3030,7 @@ static int apply_one_fragment(struct apply_state *state,\n \t\t\t\tstate->apply = 0;\n \t\t}\n \n-\t\tif (state->apply_verbosely && applied_pos != pos) {\n+\t\tif (state->apply_verbosity > verbosity_normal && 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@@ -3041,14 +3045,14 @@ static int apply_one_fragment(struct apply_state *state,\n \t\t * Warn if it was necessary to reduce the number\n \t\t * of context lines.\n \t\t */\n-\t\tif ((leading != frag->leading) ||\n-\t\t    (trailing != frag->trailing))\n+\t\tif ((leading != frag->leading ||\n+\t\t     trailing != frag->trailing) && state->apply_verbosity > verbosity_silent)\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(state, img, applied_pos, &preimage, &postimage);\n \t} else {\n-\t\tif (state->apply_verbosely)\n+\t\tif (state->apply_verbosity > verbosity_normal)\n \t\t\terror(_(\"while searching for:\\n%.*s\"),\n \t\t\t      (int)(old - oldlines), oldlines);\n \t}\n@@ -3539,7 +3543,8 @@ static int try_threeway(struct apply_state *state,\n \t\t read_blob_object(&buf, pre_sha1, patch->old_mode))\n \t\treturn error(\"repository lacks the necessary blob to fall back on 3-way merge.\");\n \n-\tfprintf(stderr, \"Falling back to three-way merge...\\n\");\n+\tif (state->apply_verbosity > verbosity_silent)\n+\t\tfprintf(stderr, \"Falling back to three-way merge...\\n\");\n \n \timg = strbuf_detach(&buf, &len);\n \tprepare_image(&tmp_image, img, len, 1);\n@@ -3569,7 +3574,9 @@ static int try_threeway(struct apply_state *state,\n \tstatus = three_way_merge(image, patch->new_name,\n \t\t\t\t pre_sha1, our_sha1, post_sha1);\n \tif (status < 0) {\n-\t\tfprintf(stderr, \"Failed to fall back on three-way merge...\\n\");\n+\t\tif (state->apply_verbosity > verbosity_silent)\n+\t\t\tfprintf(stderr,\n+\t\t\t\t\"Failed to fall back on three-way merge...\\n\");\n \t\treturn status;\n \t}\n \n@@ -3581,9 +3588,15 @@ static int try_threeway(struct apply_state *state,\n \t\t\thashcpy(patch->threeway_stage[0].hash, pre_sha1);\n \t\thashcpy(patch->threeway_stage[1].hash, our_sha1);\n \t\thashcpy(patch->threeway_stage[2].hash, post_sha1);\n-\t\tfprintf(stderr, \"Applied patch to '%s' with conflicts.\\n\", patch->new_name);\n+\t\tif (state->apply_verbosity > verbosity_silent)\n+\t\t\tfprintf(stderr,\n+\t\t\t\t\"Applied patch to '%s' with conflicts.\\n\",\n+\t\t\t\tpatch->new_name);\n \t} else {\n-\t\tfprintf(stderr, \"Applied patch to '%s' cleanly.\\n\", patch->new_name);\n+\t\tif (state->apply_verbosity > verbosity_silent)\n+\t\t\tfprintf(stderr,\n+\t\t\t\t\"Applied patch to '%s' cleanly.\\n\",\n+\t\t\t\tpatch->new_name);\n \t}\n \treturn 0;\n }\n@@ -3956,7 +3969,7 @@ static int check_patch_list(struct apply_state *state, struct patch *patch)\n \tprepare_fn_table(state, patch);\n \twhile (patch) {\n \t\tint res;\n-\t\tif (state->apply_verbosely)\n+\t\tif (state->apply_verbosity > verbosity_normal)\n \t\t\tsay_patch_name(stderr,\n \t\t\t\t       _(\"Checking patch %s...\"), patch);\n \t\tres = check_patch(state, patch);\n@@ -4472,7 +4485,7 @@ static int write_out_one_reject(struct apply_state *state, struct patch *patch)\n \t}\n \n \tif (!cnt) {\n-\t\tif (state->apply_verbosely)\n+\t\tif (state->apply_verbosity > verbosity_normal)\n \t\t\tsay_patch_name(stderr,\n \t\t\t\t       _(\"Applied patch %s cleanly.\"), patch);\n \t\treturn 0;\n@@ -4489,7 +4502,8 @@ static int write_out_one_reject(struct apply_state *state, struct patch *patch)\n \t\t\t    \"Applying patch %%s with %d rejects...\",\n \t\t\t    cnt),\n \t\t    cnt);\n-\tsay_patch_name(stderr, sb.buf, patch);\n+\tif (state->apply_verbosity > verbosity_silent)\n+\t\tsay_patch_name(stderr, sb.buf, patch);\n \tstrbuf_release(&sb);\n \n \tcnt = strlen(patch->new_name);\n@@ -4516,10 +4530,12 @@ static int write_out_one_reject(struct apply_state *state, struct patch *patch)\n \t     frag;\n \t     cnt++, frag = frag->next) {\n \t\tif (!frag->rejected) {\n-\t\t\tfprintf_ln(stderr, _(\"Hunk #%d applied cleanly.\"), cnt);\n+\t\t\tif (state->apply_verbosity > verbosity_silent)\n+\t\t\t\tfprintf_ln(stderr, _(\"Hunk #%d applied cleanly.\"), cnt);\n \t\t\tcontinue;\n \t\t}\n-\t\tfprintf_ln(stderr, _(\"Rejected hunk #%d.\"), cnt);\n+\t\tif (state->apply_verbosity > verbosity_silent)\n+\t\t\tfprintf_ln(stderr, _(\"Rejected hunk #%d.\"), cnt);\n \t\tfprintf(rej, \"%.*s\", frag->size, frag->patch);\n \t\tif (frag->patch[frag->size-1] != '\\n')\n \t\t\tfputc('\\n', rej);\n@@ -4568,8 +4584,10 @@ static int write_out_results(struct apply_state *state, struct patch *list)\n \t\tstruct string_list_item *item;\n \n \t\tstring_list_sort(&cpath);\n-\t\tfor_each_string_list_item(item, &cpath)\n-\t\t\tfprintf(stderr, \"U %s\\n\", item->string);\n+\t\tif (state->apply_verbosity > verbosity_silent) {\n+\t\t\tfor_each_string_list_item(item, &cpath)\n+\t\t\t\tfprintf(stderr, \"U %s\\n\", item->string);\n+\t\t}\n \t\tstring_list_clear(&cpath, 0);\n \n \t\trerere(0);\n@@ -4626,7 +4644,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 (state->apply_verbosely)\n+\t\t\tif (state->apply_verbosity > verbosity_normal)\n \t\t\t\tsay_patch_name(stderr, _(\"Skipped patch '%s'.\"), patch);\n \t\t\tfree_patch(patch);\n \t\t\tskipped_patch++;\ndiff --git a/apply.h b/apply.h\nindex 51b983b..f015403 100644\n--- a/apply.h\n+++ b/apply.h\n@@ -13,6 +13,12 @@ enum apply_ws_ignore {\n \tignore_ws_change\n };\n \n+enum apply_verbosity {\n+\tverbosity_silent = -1,\n+\tverbosity_normal = 0,\n+\tverbosity_verbose = 1\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@@ -51,13 +57,13 @@ struct apply_state {\n \tint allow_overlap;\n \tint apply_in_reverse;\n \tint apply_with_reject;\n-\tint apply_verbosely;\n \tint no_add;\n \tint threeway;\n \tint unidiff_zero;\n \tint unsafe_paths;\n \n \t/* Other non boolean parameters */\n+\tenum apply_verbosity apply_verbosity;\n \tconst char *fake_ancestor;\n \tconst char *patch_input_file;\n \tint line_termination;\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 9c66474..7338701 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -74,7 +74,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\", &state.allow_overlap,\n \t\t\tN_(\"allow overlapping hunks\")),\n-\t\tOPT__VERBOSE(&state.apply_verbosely, N_(\"be verbose\")),\n+\t\tOPT__VERBOSE(&state.apply_verbosity, 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\tAPPLY_OPT_INACCURATE_EOF),\n-- \n2.10.0.41.g9df52c3\n\n"},{"id":"301060","messageId":"20160904201833.21676-35-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"[PATCH v14 34/41] apply: don't print on stdout in verbosity_silent mode","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:18:26Z","receivedAt":"2016-09-04T20:20:15Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"When apply_verbosity is set to verbosity_silent nothing should be\nprinted on both stderr and stdout.\n\nTo avoid printing on stdout, we can just skip calling the following\nfunctions:\n\n\t- stat_patch_list(),\n\t- numstat_patch_list(),\n\t- summary_patch_list().\n\nIt is safe to do that because the above functions have no side\neffects other than printing:\n\n- stat_patch_list() only computes some local values and then call\nshow_stats() and print_stat_summary(), those two functions only\ncompute local values and call printing functions,\n- numstat_patch_list() also only computes local values and calls\nprinting functions,\n- summary_patch_list() calls show_file_mode_name(), printf(),\nshow_rename_copy(), show_mode_change() that are only printing.\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n apply.c | 6 +++---\n 1 file changed, 3 insertions(+), 3 deletions(-)\n\ndiff --git a/apply.c b/apply.c\nindex df85cbc..ddbb0a2 100644\n--- a/apply.c\n+++ b/apply.c\n@@ -4702,13 +4702,13 @@ static int apply_patch(struct apply_state *state,\n \t\tgoto end;\n \t}\n \n-\tif (state->diffstat)\n+\tif (state->diffstat && state->apply_verbosity > verbosity_silent)\n \t\tstat_patch_list(state, list);\n \n-\tif (state->numstat)\n+\tif (state->numstat && state->apply_verbosity > verbosity_silent)\n \t\tnumstat_patch_list(state, list);\n \n-\tif (state->summary)\n+\tif (state->summary && state->apply_verbosity > verbosity_silent)\n \t\tsummary_patch_list(list);\n \n end:\n-- \n2.10.0.41.g9df52c3\n\n"},{"id":"301061","messageId":"20160904201833.21676-27-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"[PATCH v14 26/41] builtin/apply: make try_create_file() return -1 on error","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:18:18Z","receivedAt":"2016-09-04T20:20:16Z","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 to the\ncaller instead of die()ing.\n\nTo do that in a compatible manner with the rest of the error handling\nin \"builtin/apply.c\", try_create_file() should return -1 in case of\nerror.\n\nUnfortunately try_create_file() currently returns -1 to signal a\nrecoverable error. To fix that, let's make it return 1 in case of\na recoverable error and -1 in case of an unrecoverable error.\n\nHelped-by: Eric Sunshine <sunshine@sunshineco.com>\nHelped-by: Jeff King <peff@peff.net>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 44 +++++++++++++++++++++++++++++++++-----------\n 1 file changed, 33 insertions(+), 11 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex c787ead..3145e03 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -4150,38 +4150,48 @@ 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+\tint fd, res;\n \tstruct strbuf nbuf = STRBUF_INIT;\n \n \tif (S_ISGITLINK(mode)) {\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+\tres = write_in_full(fd, buf, size) < 0;\n+\tif (res)\n+\t\terror_errno(_(\"failed to write to '%s'\"), path);\n \tstrbuf_release(&nbuf);\n \n-\tif (close(fd) < 0)\n-\t\tdie_errno(_(\"closing file '%s'\"), path);\n-\treturn 0;\n+\tif (close(fd) < 0 && !res)\n+\t\treturn error_errno(_(\"closing file '%s'\"), path);\n+\n+\treturn res ? -1 : 0;\n }\n \n /*\n@@ -4195,15 +4205,24 @@ 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 < 0)\n+\t\texit(128);\n+\tif (!res)\n \t\treturn;\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 < 0)\n+\t\t\texit(128);\n+\t\tif (!res)\n \t\t\treturn;\n \t}\n \n@@ -4222,7 +4241,10 @@ 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 < 0)\n+\t\t\t\texit(128);\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-- \n2.10.0.41.g9df52c3\n\n"},{"id":"301062","messageId":"20160904201833.21676-36-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"[PATCH v14 35/41] usage: add set_warn_routine()","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:18:27Z","receivedAt":"2016-09-04T20:20:17Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"There are already set_die_routine() and set_error_routine(),\nso let's add set_warn_routine() as this will be needed in a\nfollowing commit.\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n git-compat-util.h | 1 +\n usage.c           | 5 +++++\n 2 files changed, 6 insertions(+)\n\ndiff --git a/git-compat-util.h b/git-compat-util.h\nindex db89ba7..d189b4f 100644\n--- a/git-compat-util.h\n+++ b/git-compat-util.h\n@@ -440,6 +440,7 @@ static inline int const_error(void)\n \n extern void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params));\n extern void set_error_routine(void (*routine)(const char *err, va_list params));\n+extern void set_warn_routine(void (*routine)(const char *warn, va_list params));\n extern void set_die_is_recursing_routine(int (*routine)(void));\n extern void set_error_handle(FILE *);\n \ndiff --git a/usage.c b/usage.c\nindex 1dad03f..67e5526 100644\n--- a/usage.c\n+++ b/usage.c\n@@ -70,6 +70,11 @@ void set_error_routine(void (*routine)(const char *err, va_list params))\n \terror_routine = routine;\n }\n \n+void set_warn_routine(void (*routine)(const char *warn, va_list params))\n+{\n+\twarn_routine = routine;\n+}\n+\n void set_die_is_recursing_routine(int (*routine)(void))\n {\n \tdie_is_recursing = routine;\n-- \n2.10.0.41.g9df52c3\n\n"},{"id":"301063","messageId":"20160904201833.21676-42-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"[PATCH v14 41/41] builtin/am: use apply API in run_apply()","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:18:33Z","receivedAt":"2016-09-04T20:20:19Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"This replaces run_apply() implementation with a new one that\nuses the apply API that has been previously prepared in\napply.c and apply.h.\n\nThis shoud improve performance a lot in certain cases.\n\nAs the previous implementation was creating a new `git apply`\nprocess to apply each patch, it could be slow on systems like\nWindows where it is costly to create new processes.\n\nAlso the new `git apply` process had to read the index from\ndisk, and when the process was done the calling process\ndiscarded its own index and read back from disk the new\nindex that had been created by the `git apply` process.\n\nThis could be very inefficient with big repositories that\nhave big index files, especially when the system decided\nthat it was a good idea to run the `git apply` processes on\na different processor core.\n\nAlso eliminating index reads enables further performance\nimprovements by using:\n\n`git update-index --split-index`\n\nFor example here is a benchmark of a multi hundred commit\nrebase on the Linux kernel on a Debian laptop with SSD:\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(using branch \"next\" from mid April 2016.)\n\nBenchmarked-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/am.c | 65 ++++++++++++++++++++++++++++++++++++++++--------------------\n 1 file changed, 43 insertions(+), 22 deletions(-)\n\ndiff --git a/builtin/am.c b/builtin/am.c\nindex 739b34d..38d5d11 100644\n--- a/builtin/am.c\n+++ b/builtin/am.c\n@@ -28,6 +28,7 @@\n #include \"rerere.h\"\n #include \"prompt.h\"\n #include \"mailinfo.h\"\n+#include \"apply.h\"\n \n /**\n  * Returns 1 if the file is empty or does not exist, 0 otherwise.\n@@ -1522,39 +1523,59 @@ static int parse_mail_rebase(struct am_state *state, const char *mail)\n  */\n static int run_apply(const struct am_state *state, const char *index_file)\n {\n-\tstruct child_process cp = CHILD_PROCESS_INIT;\n-\n-\tcp.git_cmd = 1;\n-\n-\tif (index_file)\n-\t\targv_array_pushf(&cp.env_array, \"GIT_INDEX_FILE=%s\", index_file);\n+\tstruct argv_array apply_paths = ARGV_ARRAY_INIT;\n+\tstruct argv_array apply_opts = ARGV_ARRAY_INIT;\n+\tstruct apply_state apply_state;\n+\tint res, opts_left;\n+\tstatic struct lock_file lock_file;\n+\tint force_apply = 0;\n+\tint options = 0;\n+\n+\tif (init_apply_state(&apply_state, NULL, &lock_file))\n+\t\tdie(\"BUG: init_apply_state() failed\");\n+\n+\targv_array_push(&apply_opts, \"apply\");\n+\targv_array_pushv(&apply_opts, state->git_apply_opts.argv);\n+\n+\topts_left = apply_parse_options(apply_opts.argc, apply_opts.argv,\n+\t\t\t\t\t&apply_state, &force_apply, &options,\n+\t\t\t\t\tNULL);\n+\n+\tif (opts_left != 0)\n+\t\tdie(\"unknown option passed through to git apply\");\n+\n+\tif (index_file) {\n+\t\tapply_state.index_file = index_file;\n+\t\tapply_state.cached = 1;\n+\t} else\n+\t\tapply_state.check_index = 1;\n \n \t/*\n \t * If we are allowed to fall back on 3-way merge, don't give false\n \t * errors during the initial attempt.\n \t */\n-\tif (state->threeway && !index_file) {\n-\t\tcp.no_stdout = 1;\n-\t\tcp.no_stderr = 1;\n-\t}\n+\tif (state->threeway && !index_file)\n+\t\tapply_state.apply_verbosity = verbosity_silent;\n \n-\targv_array_push(&cp.args, \"apply\");\n+\tif (check_apply_state(&apply_state, force_apply))\n+\t\tdie(\"BUG: check_apply_state() failed\");\n \n-\targv_array_pushv(&cp.args, state->git_apply_opts.argv);\n+\targv_array_push(&apply_paths, am_path(state, \"patch\"));\n \n-\tif (index_file)\n-\t\targv_array_push(&cp.args, \"--cached\");\n-\telse\n-\t\targv_array_push(&cp.args, \"--index\");\n+\tres = apply_all_patches(&apply_state, apply_paths.argc, apply_paths.argv, options);\n \n-\targv_array_push(&cp.args, am_path(state, \"patch\"));\n+\targv_array_clear(&apply_paths);\n+\targv_array_clear(&apply_opts);\n+\tclear_apply_state(&apply_state);\n \n-\tif (run_command(&cp))\n-\t\treturn -1;\n+\tif (res)\n+\t\treturn res;\n \n-\t/* Reload index as git-apply will have modified it. */\n-\tdiscard_cache();\n-\tread_cache_from(index_file ? index_file : get_index_file());\n+\tif (index_file) {\n+\t\t/* Reload index as apply_all_patches() will have modified it. */\n+\t\tdiscard_cache();\n+\t\tread_cache_from(index_file);\n+\t}\n \n \treturn 0;\n }\n-- \n2.10.0.41.g9df52c3\n\n"},{"id":"301064","messageId":"20160904201833.21676-41-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"[PATCH v14 40/41] apply: learn to use a different index file","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:18:32Z","receivedAt":"2016-09-04T20:20:21Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Sometimes we want to apply in a different index file.\n\nBefore the apply functionality was libified it was possible to\nuse the GIT_INDEX_FILE environment variable, for this purpose.\n\nBut now, as the apply functionality has been libified, it should\nbe possible to do that in a libified way.\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n apply.c | 27 +++++++++++++++++++++------\n apply.h |  1 +\n 2 files changed, 22 insertions(+), 6 deletions(-)\n\ndiff --git a/apply.c b/apply.c\nindex f29387d..b1dd998 100644\n--- a/apply.c\n+++ b/apply.c\n@@ -3993,12 +3993,21 @@ static int check_patch_list(struct apply_state *state, struct patch *patch)\n \treturn err;\n }\n \n+static int read_apply_cache(struct apply_state *state)\n+{\n+\tif (state->index_file)\n+\t\treturn read_cache_from(state->index_file);\n+\telse\n+\t\treturn read_cache();\n+}\n+\n /* This function tries to read the sha1 from the current index */\n-static int get_current_sha1(const char *path, unsigned char *sha1)\n+static int get_current_sha1(struct apply_state *state, const char *path,\n+\t\t\t    unsigned char *sha1)\n {\n \tint pos;\n \n-\tif (read_cache() < 0)\n+\tif (read_apply_cache(state) < 0)\n \t\treturn -1;\n \tpos = cache_name_pos(path, strlen(path));\n \tif (pos < 0)\n@@ -4071,7 +4080,7 @@ static int build_fake_ancestor(struct apply_state *state, struct patch *list)\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\tif (get_current_sha1(state, patch->old_name, sha1))\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@@ -4675,10 +4684,16 @@ 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 && state->newfd < 0)\n-\t\tstate->newfd = hold_locked_index(state->lock_file, 1);\n+\tif (state->update_index && state->newfd < 0) {\n+\t\tif (state->index_file)\n+\t\t\tstate->newfd = hold_lock_file_for_update(state->lock_file,\n+\t\t\t\t\t\t\t\t state->index_file,\n+\t\t\t\t\t\t\t\t LOCK_DIE_ON_ERROR);\n+\t\telse\n+\t\t\tstate->newfd = hold_locked_index(state->lock_file, 1);\n+\t}\n \n-\tif (state->check_index && read_cache() < 0) {\n+\tif (state->check_index && read_apply_cache(state) < 0) {\n \t\terror(_(\"unable to read index file\"));\n \t\tres = -128;\n \t\tgoto end;\ndiff --git a/apply.h b/apply.h\nindex 9fec536..b3d6783 100644\n--- a/apply.h\n+++ b/apply.h\n@@ -63,6 +63,7 @@ struct apply_state {\n \tint unsafe_paths;\n \n \t/* Other non boolean parameters */\n+\tconst char *index_file;\n \tenum apply_verbosity apply_verbosity;\n \tconst char *fake_ancestor;\n \tconst char *patch_input_file;\n-- \n2.10.0.41.g9df52c3\n\n"},{"id":"301065","messageId":"20160904201833.21676-40-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"[PATCH v14 39/41] apply: pass apply state to build_fake_ancestor()","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:18:31Z","receivedAt":"2016-09-04T20:20:24Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify git apply functionality, we will need to read from a\ndifferent index file in get_current_sha1(). This index file will be\nstored in \"struct apply_state\", so let's pass the state to\nbuild_fake_ancestor() which will later pass it to get_current_sha1().\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n apply.c | 9 +++++----\n 1 file changed, 5 insertions(+), 4 deletions(-)\n\ndiff --git a/apply.c b/apply.c\nindex 2dad0db..f29387d 100644\n--- a/apply.c\n+++ b/apply.c\n@@ -4042,7 +4042,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 int build_fake_ancestor(struct patch *list, const char *filename)\n+static int build_fake_ancestor(struct apply_state *state, struct patch *list)\n {\n \tstruct patch *patch;\n \tstruct index_state result = { NULL };\n@@ -4089,12 +4089,13 @@ static int build_fake_ancestor(struct patch *list, const char *filename)\n \t\t}\n \t}\n \n-\thold_lock_file_for_update(&lock, filename, LOCK_DIE_ON_ERROR);\n+\thold_lock_file_for_update(&lock, state->fake_ancestor, LOCK_DIE_ON_ERROR);\n \tres = write_locked_index(&result, &lock, COMMIT_LOCK);\n \tdiscard_index(&result);\n \n \tif (res)\n-\t\treturn error(\"Could not write temporary index to %s\", filename);\n+\t\treturn error(\"Could not write temporary index to %s\",\n+\t\t\t     state->fake_ancestor);\n \n \treturn 0;\n }\n@@ -4709,7 +4710,7 @@ static int apply_patch(struct apply_state *state,\n \t}\n \n \tif (state->fake_ancestor &&\n-\t    build_fake_ancestor(list, state->fake_ancestor)) {\n+\t    build_fake_ancestor(state, list)) {\n \t\tres = -128;\n \t\tgoto end;\n \t}\n-- \n2.10.0.41.g9df52c3\n\n"},{"id":"301066","messageId":"20160904201833.21676-39-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"[PATCH v14 38/41] apply: refactor `git apply` option parsing","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:18:30Z","receivedAt":"2016-09-04T20:20:26Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Parsing `git apply` options can be useful to other commands that\nwant to call the libified apply functionality, because this way\nthey can easily pass some options from their own command line to\nthe libified apply functionality.\n\nThis will be used by `git am` in a following patch.\n\nTo make this possible, let's refactor the `git apply` option\nparsing code into a new libified apply_parse_options() function.\n\nDoing that makes it possible to remove some functions definitions\nfrom \"apply.h\" and make them static in \"apply.c\".\n\nHelped-by: Ramsay Jones <ramsay@ramsayjones.plus.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n apply.c         | 103 +++++++++++++++++++++++++++++++++++++++++++++++++-------\n apply.h         |  18 +++-------\n builtin/apply.c |  74 ++--------------------------------------\n 3 files changed, 97 insertions(+), 98 deletions(-)\n\ndiff --git a/apply.c b/apply.c\nindex 27baf0e..2dad0db 100644\n--- a/apply.c\n+++ b/apply.c\n@@ -4730,16 +4730,16 @@ static int apply_patch(struct apply_state *state,\n \treturn res;\n }\n \n-int apply_option_parse_exclude(const struct option *opt,\n-\t\t\t       const 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-int apply_option_parse_include(const struct option *opt,\n-\t\t\t       const 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@@ -4747,9 +4747,9 @@ int apply_option_parse_include(const struct option *opt,\n \treturn 0;\n }\n \n-int apply_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@@ -4757,8 +4757,8 @@ int apply_option_parse_p(const struct option *opt,\n \treturn 0;\n }\n \n-int apply_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@@ -4768,8 +4768,8 @@ int apply_option_parse_space_change(const struct option *opt,\n \treturn 0;\n }\n \n-int apply_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@@ -4778,8 +4778,8 @@ int apply_option_parse_whitespace(const struct option *opt,\n \treturn 0;\n }\n \n-int apply_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@@ -4893,3 +4893,80 @@ int apply_all_patches(struct apply_state *state,\n \t\treturn res;\n \treturn (res == -1 ? 1 : 128);\n }\n+\n+int apply_parse_options(int argc, const char **argv,\n+\t\t\tstruct apply_state *state,\n+\t\t\tint *force_apply, int *options,\n+\t\t\tconst char * const *apply_usage)\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+\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, 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, 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+\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\", &state->numstat,\n+\t\t\tN_(\"show number of added and deleted lines in decimal notation\")),\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+\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\", &state->cached,\n+\t\t\tN_(\"apply a patch without touching the working tree\")),\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+\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\", &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+\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\", state, N_(\"action\"),\n+\t\t\tN_(\"detect new or modified lines that have whitespace errors\"),\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, 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, 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+\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\", &state->allow_overlap,\n+\t\t\tN_(\"allow overlapping hunks\")),\n+\t\tOPT__VERBOSE(&state->apply_verbosity, 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\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\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+\t\tOPT_END()\n+\t};\n+\n+\treturn parse_options(argc, argv, state->prefix, builtin_apply_options, apply_usage, 0);\n+}\ndiff --git a/apply.h b/apply.h\nindex 902346b..9fec536 100644\n--- a/apply.h\n+++ b/apply.h\n@@ -111,20 +111,10 @@ struct apply_state {\n \tint applied_after_fixing_ws;\n };\n \n-extern int apply_option_parse_exclude(const struct option *opt,\n-\t\t\t\t      const char *arg, int unset);\n-extern int apply_option_parse_include(const struct option *opt,\n-\t\t\t\t      const char *arg, int unset);\n-extern int apply_option_parse_p(const struct option *opt,\n-\t\t\t\tconst char *arg,\n-\t\t\t\tint unset);\n-extern int apply_option_parse_whitespace(const struct option *opt,\n-\t\t\t\t\t const char *arg, int unset);\n-extern int apply_option_parse_directory(const struct option *opt,\n-\t\t\t\t\tconst char *arg, int unset);\n-extern int apply_option_parse_space_change(const struct option *opt,\n-\t\t\t\t\t   const char *arg, int unset);\n-\n+extern int apply_parse_options(int argc, const char **argv,\n+\t\t\t       struct apply_state *state,\n+\t\t\t       int *force_apply, int *options,\n+\t\t\t       const char * const *apply_usage);\n extern int init_apply_state(struct apply_state *state,\n \t\t\t    const char *prefix,\n \t\t\t    struct lock_file *lock_file);\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 7338701..81b9a61 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -18,80 +18,12 @@ int cmd_apply(int argc, const char **argv, const char *prefix)\n \tint ret;\n \tstruct apply_state state;\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-\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, 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, 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-\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\", &state.numstat,\n-\t\t\tN_(\"show number of added and deleted lines in decimal notation\")),\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-\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\", &state.cached,\n-\t\t\tN_(\"apply a patch without touching the working tree\")),\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-\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\", &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-\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\", &state, N_(\"action\"),\n-\t\t\tN_(\"detect new or modified lines that have whitespace errors\"),\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, 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, 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-\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\", &state.allow_overlap,\n-\t\t\tN_(\"allow overlapping hunks\")),\n-\t\tOPT__VERBOSE(&state.apply_verbosity, 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\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\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-\t\tOPT_END()\n-\t};\n-\n \tif (init_apply_state(&state, prefix, &lock_file))\n \t\texit(128);\n \n-\targc = parse_options(argc, argv, state.prefix, builtin_apply_options,\n-\t\t\tapply_usage, 0);\n+\targc = apply_parse_options(argc, argv,\n+\t\t\t\t   &state, &force_apply, &options,\n+\t\t\t\t   apply_usage);\n \n \tif (check_apply_state(&state, force_apply))\n \t\texit(128);\n-- \n2.10.0.41.g9df52c3\n\n"},{"id":"301067","messageId":"20160904201833.21676-38-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"[PATCH v14 37/41] apply: change error_routine when silent","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:18:29Z","receivedAt":"2016-09-04T20:20:27Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To avoid printing anything when applying with\n`state->apply_verbosity == verbosity_silent`, let's save the\nexisting warn and error routines before applying, and let's\nreplace them with a routine that does nothing.\n\nThen after applying, let's restore the saved routines.\n\nNote that, as we need to restore the saved routines in all\ncases, we cannot return early any more in apply_all_patches().\n\nHelped-by: Stefan Beller <sbeller@google.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n apply.c | 21 ++++++++++++++++++++-\n apply.h |  8 ++++++++\n 2 files changed, 28 insertions(+), 1 deletion(-)\n\ndiff --git a/apply.c b/apply.c\nindex ddbb0a2..27baf0e 100644\n--- a/apply.c\n+++ b/apply.c\n@@ -112,6 +112,11 @@ void clear_apply_state(struct apply_state *state)\n \t/* &state->fn_table is cleared at the end of apply_patch() */\n }\n \n+static void mute_routine(const char *msg, va_list params)\n+{\n+\t/* do nothing */\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@@ -144,6 +149,13 @@ int check_apply_state(struct apply_state *state, int force_apply)\n \tif (!state->lock_file)\n \t\treturn error(\"BUG: state->lock_file should not be NULL\");\n \n+\tif (state->apply_verbosity <= verbosity_silent) {\n+\t\tstate->saved_error_routine = get_error_routine();\n+\t\tstate->saved_warn_routine = get_warn_routine();\n+\t\tset_error_routine(mute_routine);\n+\t\tset_warn_routine(mute_routine);\n+\t}\n+\n \treturn 0;\n }\n \n@@ -4864,7 +4876,7 @@ int apply_all_patches(struct apply_state *state,\n \t\tstate->newfd = -1;\n \t}\n \n-\treturn !!errs;\n+\tres = !!errs;\n \n end:\n \tif (state->newfd >= 0) {\n@@ -4872,5 +4884,12 @@ int apply_all_patches(struct apply_state *state,\n \t\tstate->newfd = -1;\n \t}\n \n+\tif (state->apply_verbosity <= verbosity_silent) {\n+\t\tset_error_routine(state->saved_error_routine);\n+\t\tset_warn_routine(state->saved_warn_routine);\n+\t}\n+\n+\tif (res > -1)\n+\t\treturn res;\n \treturn (res == -1 ? 1 : 128);\n }\ndiff --git a/apply.h b/apply.h\nindex f015403..902346b 100644\n--- a/apply.h\n+++ b/apply.h\n@@ -94,6 +94,14 @@ struct apply_state {\n \t */\n \tstruct string_list fn_table;\n \n+\t/*\n+\t * This is to save reporting routines before using\n+\t * set_error_routine() or set_warn_routine() to install muting\n+\t * routines when in verbosity_silent mode.\n+\t */\n+\tvoid (*saved_error_routine)(const char *err, va_list params);\n+\tvoid (*saved_warn_routine)(const char *warn, va_list params);\n+\n \t/* These control whitespace errors */\n \tenum apply_ws_error_action ws_error_action;\n \tenum apply_ws_ignore ws_ignore_action;\n-- \n2.10.0.41.g9df52c3\n\n"},{"id":"301068","messageId":"20160904201833.21676-37-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"[PATCH v14 36/41] usage: add get_error_routine() and get_warn_routine()","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:18:28Z","receivedAt":"2016-09-04T20:20:30Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"Let's make it possible to get the current error_routine and warn_routine,\nso that we can store them before using set_error_routine() or\nset_warn_routine() to use new ones.\n\nThis way we will be able put back the original routines, when we are done\nwith using new ones.\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n git-compat-util.h |  2 ++\n usage.c           | 10 ++++++++++\n 2 files changed, 12 insertions(+)\n\ndiff --git a/git-compat-util.h b/git-compat-util.h\nindex d189b4f..bac8613 100644\n--- a/git-compat-util.h\n+++ b/git-compat-util.h\n@@ -440,7 +440,9 @@ static inline int const_error(void)\n \n extern void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params));\n extern void set_error_routine(void (*routine)(const char *err, va_list params));\n+extern void (*get_error_routine(void))(const char *err, va_list params);\n extern void set_warn_routine(void (*routine)(const char *warn, va_list params));\n+extern void (*get_warn_routine(void))(const char *warn, va_list params);\n extern void set_die_is_recursing_routine(int (*routine)(void));\n extern void set_error_handle(FILE *);\n \ndiff --git a/usage.c b/usage.c\nindex 67e5526..2fd3045 100644\n--- a/usage.c\n+++ b/usage.c\n@@ -70,11 +70,21 @@ void set_error_routine(void (*routine)(const char *err, va_list params))\n \terror_routine = routine;\n }\n \n+void (*get_error_routine(void))(const char *err, va_list params)\n+{\n+\treturn error_routine;\n+}\n+\n void set_warn_routine(void (*routine)(const char *warn, va_list params))\n {\n \twarn_routine = routine;\n }\n \n+void (*get_warn_routine(void))(const char *warn, va_list params)\n+{\n+\treturn warn_routine;\n+}\n+\n void set_die_is_recursing_routine(int (*routine)(void))\n {\n \tdie_is_recursing = routine;\n-- \n2.10.0.41.g9df52c3\n\n"},{"id":"301069","messageId":"20160904201833.21676-7-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"[PATCH v14 06/41] builtin/apply: make parse_chunk() return a negative integer on error","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:17:58Z","receivedAt":"2016-09-04T20:20:34Z","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 to the\ncaller instead of die()ing or exit()ing.\n\nTo do that in a compatible manner with the rest of the error handling\nin builtin/apply.c, parse_chunk() should return a negative integer\ninstead of calling die() or exit().\n\nAs parse_chunk() is called only by apply_patch() which already\nreturns either -1 or -128 when an error happened, let's make it also\nreturn -1 or -128.\n\nThis makes it compatible with what find_header() and parse_binary()\nalready return.\n\nHelped-by: Eric Sunshine <sunshine@sunshineco.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 22 ++++++++++++++--------\n 1 file changed, 14 insertions(+), 8 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 434ba0c..c07d142 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -1996,22 +1996,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 if no header was found or parse_binary() failed,\n+ *   -128 on another error,\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 == -128)\n-\t\texit(128);\n-\n \tif (offset < 0)\n \t\treturn offset;\n \n@@ -2071,8 +2071,10 @@ 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\terror(_(\"patch with only garbage at line %d\"), state->linenr);\n+\t\t\treturn -128;\n+\t\t}\n \t}\n \n \treturn offset + hdrsize + patchsize;\n@@ -4455,6 +4457,10 @@ 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 == -128) {\n+\t\t\t\tres = -128;\n+\t\t\t\tgoto end;\n+\t\t\t}\n \t\t\tbreak;\n \t\t}\n \t\tif (state->apply_in_reverse)\n-- \n2.10.0.41.g9df52c3\n\n"},{"id":"301070","messageId":"20160904201833.21676-11-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"[PATCH v14 10/41] builtin/apply: move init_apply_state() to apply.c","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:18:02Z","receivedAt":"2016-09-04T20:20:37Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"To libify `git apply` functionality we must make init_apply_state()\nusable outside \"builtin/apply.c\".\n\nLet's do that by moving it into a new \"apply.c\".\n\nHelped-by: Eric Sunshine <sunshine@sunshineco.com>\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n Makefile        |  1 +\n apply.c         | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n apply.h         | 10 ++++++\n builtin/apply.c | 91 -------------------------------------------------------\n 4 files changed, 105 insertions(+), 91 deletions(-)\n create mode 100644 apply.c\n\ndiff --git a/Makefile b/Makefile\nindex d96ecb7..c6ac236 100644\n--- a/Makefile\n+++ b/Makefile\n@@ -691,6 +691,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..c858ca4\n--- /dev/null\n+++ b/apply.c\n@@ -0,0 +1,94 @@\n+#include \"cache.h\"\n+#include \"lockfile.h\"\n+#include \"apply.h\"\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,\n+\t\t      const char *prefix,\n+\t\t      struct lock_file *lock_file)\n+{\n+\tmemset(state, 0, sizeof(*state));\n+\tstate->prefix = prefix;\n+\tstate->prefix_length = state->prefix ? strlen(state->prefix) : 0;\n+\tstate->lock_file = lock_file;\n+\tstate->newfd = -1;\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+\tstring_list_init(&state->fn_table, 0);\n+\tstring_list_init(&state->limit_by_name, 0);\n+\tstring_list_init(&state->symlink_changes, 0);\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+void clear_apply_state(struct apply_state *state)\n+{\n+\tstring_list_clear(&state->limit_by_name, 0);\n+\tstring_list_clear(&state->symlink_changes, 0);\n+\tstrbuf_release(&state->root);\n+\n+\t/* &state->fn_table is cleared at the end of apply_patch() */\n+}\ndiff --git a/apply.h b/apply.h\nindex 7493a40..08c0a25 100644\n--- a/apply.h\n+++ b/apply.h\n@@ -97,4 +97,14 @@ struct apply_state {\n \tint applied_after_fixing_ws;\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,\n+\t\t\t     const char *prefix,\n+\t\t\t     struct lock_file *lock_file);\n+extern void clear_apply_state(struct apply_state *state);\n+\n #endif\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex ecb1f7a..bb6ff77 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@@ -4539,13 +4493,6 @@ static int apply_patch(struct apply_state *state,\n \treturn res;\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@@ -4604,44 +4551,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,\n-\t\t\t     const char *prefix,\n-\t\t\t     struct lock_file *lock_file)\n-{\n-\tmemset(state, 0, sizeof(*state));\n-\tstate->prefix = prefix;\n-\tstate->prefix_length = state->prefix ? strlen(state->prefix) : 0;\n-\tstate->lock_file = lock_file;\n-\tstate->newfd = -1;\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-\tstring_list_init(&state->fn_table, 0);\n-\tstring_list_init(&state->limit_by_name, 0);\n-\tstring_list_init(&state->symlink_changes, 0);\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 clear_apply_state(struct apply_state *state)\n-{\n-\tstring_list_clear(&state->limit_by_name, 0);\n-\tstring_list_clear(&state->symlink_changes, 0);\n-\tstrbuf_release(&state->root);\n-\n-\t/* &state->fn_table is cleared at the end of apply_patch() */\n-}\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.10.0.41.g9df52c3\n\n"},{"id":"301071","messageId":"20160904201833.21676-10-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"[PATCH v14 09/41] builtin/apply: make parse_ignorewhitespace_option() return -1 instead of die()ing","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:18:01Z","receivedAt":"2016-09-04T20:20:52Z","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 to the\ncaller instead of die()ing.\n\nTo do that in a compatible manner with the rest of the error handling\nin \"builtin/apply.c\", parse_ignorewhitespace_option() should return\n-1 instead of calling die().\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c | 14 +++++++-------\n 1 file changed, 7 insertions(+), 7 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 06a76f2..ecb1f7a 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@@ -4629,8 +4629,8 @@ static void init_apply_state(struct apply_state *state,\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 clear_apply_state(struct apply_state *state)\n-- \n2.10.0.41.g9df52c3\n\n"},{"id":"301072","messageId":"20160904201833.21676-8-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"[PATCH v14 07/41] builtin/apply: make parse_single_patch() return -1 on error","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:17:59Z","receivedAt":"2016-09-04T20:21:15Z","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 to the\ncaller instead of die()ing.\n\nTo do that in a compatible manner with the rest of the error handling\nin builtin/apply.c, parse_single_patch() should return a negative\ninteger instead of calling die().\n\nLet's do that by using error() and let's adjust the related test\ncases accordingly.\n\nSigned-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 c07d142..10aaba7 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -1671,6 +1671,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@@ -1688,8 +1692,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@@ -1725,9 +1731,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@@ -2029,6 +2035,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 -128;\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.10.0.41.g9df52c3\n\n"},{"id":"301073","messageId":"20160904201833.21676-9-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"[PATCH v14 08/41] builtin/apply: make parse_whitespace_option() return -1 instead of die()ing","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:18:00Z","receivedAt":"2016-09-04T20:21:25Z","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 to the\ncaller instead of die()ing.\n\nTo do that in a compatible manner with the rest of the error handling\nin builtin/apply.c, parse_whitespace_option() should return -1 instead\nof calling die().\n\nSigned-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 10aaba7..06a76f2 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@@ -4589,7 +4589,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@@ -4626,8 +4627,8 @@ static void init_apply_state(struct apply_state *state,\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.10.0.41.g9df52c3\n\n"},{"id":"301074","messageId":"20160904201833.21676-6-chriscool@tuxfamily.org","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"[PATCH v14 05/41] builtin/apply: make find_header() return -128 instead of die()ing","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-09-04T20:17:57Z","receivedAt":"2016-09-04T20:21:41Z","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 to the\ncaller instead of die()ing.\n\nTo do that in a compatible manner with the rest of the error handling\nin builtin/apply.c, let's make find_header() return -128 instead of\ncalling die().\n\nWe could make it return -1, unfortunately find_header() already\nreturns -1 when no header is found.\n\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n builtin/apply.c       | 40 ++++++++++++++++++++++++++++------------\n t/t4254-am-corrupt.sh |  2 +-\n 2 files changed, 29 insertions(+), 13 deletions(-)\n\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex dd7afee..434ba0c 100644\n--- a/builtin/apply.c\n+++ b/builtin/apply.c\n@@ -1419,6 +1419,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 if no header was found\n+ *  -128 in case of error\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@@ -1452,8 +1460,9 @@ 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\terror(_(\"patch fragment without header at line %d: %.*s\"),\n+\t\t\t\t     state->linenr, (int)len-1, line);\n+\t\t\treturn -128;\n \t\t}\n \n \t\tif (size < len + 6)\n@@ -1468,19 +1477,23 @@ static int find_header(struct apply_state *state,\n \t\t\tif (git_hdr_len <= len)\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\tif (!patch->def_name) {\n+\t\t\t\t\terror(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\t\treturn -128;\n+\t\t\t\t}\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\tif (!patch->is_delete && !patch->new_name) {\n+\t\t\t\terror(\"git diff header lacks filename information \"\n+\t\t\t\t\t     \"(line %d)\", state->linenr);\n+\t\t\t\treturn -128;\n+\t\t\t}\n \t\t\tpatch->is_toplevel_relative = 1;\n \t\t\t*hdrsize = git_hdr_len;\n \t\t\treturn offset;\n@@ -1996,6 +2009,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 == -128)\n+\t\texit(128);\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.10.0.41.g9df52c3\n\n"},{"id":"301218","messageId":"CAGZ79kb5jOpMXhLe_7tGdCAq51p5F07=fv2pR7FohCZ53SFswA@mail.gmail.com","threadId":"44003","inReplyTo":"20160904201833.21676-31-chriscool@tuxfamily.org","subject":"Re: [PATCH v14 30/41] Move libified code from builtin/apply.c to apply.{c,h}","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-09-06T18:29:11Z","receivedAt":"2016-09-06T18:29:38Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"On Sun, Sep 4, 2016 at 1:18 PM, Christian Couder\n<christian.couder@gmail.com> wrote:\n> As most of the apply code in builtin/apply.c has been libified by a number of\n> previous commits, it can now be moved to apply.{c,h}, so that more code can\n> use it.\n>\n> Helped-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n> Helped-by: Ramsay Jones <ramsay@ramsayjones.plus.com>\n> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n> ---\n>  apply.c         | 4731 ++++++++++++++++++++++++++++++++++++++++++++++++++++++\n>  apply.h         |   19 +\n>  builtin/apply.c | 4733 +------------------------------------------------------\n>  3 files changed, 4751 insertions(+), 4732 deletions(-)\n\nSo I wanted to review this patch, so I wrote a patch to review this patch. ;)\nhttps://public-inbox.org/git/82367750-adea-6dee-198a-e39ac7a84b31@gmail.com/T/#t\n\n> +                       if (!state->apply_in_reverse &&\n> +                           state->ws_error_action != nowarn_ws_error)\n> +                               check_whitespace(state, line, len, patch->ws_rule);\n> +                       added++;\n> +                       newlines--;\n> +                       trailing = 0;\n> +                       break;\n> +\n> +               /*\n> +                * We allow \"\\ No newline at end of file\". Depending\n> +                 * on locale settings when the patch was produced we\n> +                 * don't know what this line looks like. The only\n> +                 * thing we do know is that it begins with \"\\ \".\n\nThe previous three lines are white space broken AFAICT. The seem to be\nwhite space broken in the original location as well, so no need for a reroll\njust for this. But in case you do a reroll, you may want to fix these\non the fly?\n\n> +\n> +int apply_option_parse_exclude(const struct option *opt,\n> +                              const char *arg, int unset)\n> +\n> +int apply_option_parse_include(const struct option *opt,\n> +                              const char *arg, int unset)\n> +int apply_option_parse_p(const struct option *opt,\n> +                        const char *arg,\n> +                        int unset)\n\nThese three functions seem slightly different, not just moved.\nOh you removed the static key word!\n\nApart from the one minor nit and the removal of the static keyword,\nthe rest is just moved code.\n\nThanks,\nStefan\n"},{"id":"301224","messageId":"CAGZ79kY0YfAJjy-2u4UJxHtd+02_ZcVg8n1yVD9oZydmW6q6cw@mail.gmail.com","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"Re: [PATCH v14 00/41] libify apply and use lib in am, part 2","fromName":"Stefan Beller","fromEmail":"sbeller@google.com","sentAt":"2016-09-06T18:54:35Z","receivedAt":"2016-09-06T18:55:03Z","isPatch":true,"sender":{"key":"stefanbeller@gmail.com","avatar":"https://avatars.githubusercontent.com/u/455868?v=4"},"body":"On Sun, Sep 4, 2016 at 1:17 PM, Christian Couder\n<christian.couder@gmail.com> wrote:\n> Goal\n> ~~~~\n>\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\nI reviewed this v14 and all patches look good to me.\n\nThanks,\nStefan\n"},{"id":"301319","messageId":"xmqqinu7lc9m.fsf@gitster.mtv.corp.google.com","threadId":"44003","inReplyTo":"20160904201833.21676-1-chriscool@tuxfamily.org","subject":"Re: [PATCH v14 00/41] libify apply and use lib in am, part 2","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-09-07T19:51:01Z","receivedAt":"2016-09-07T19:51:57Z","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> In patch 29/41 I added some comments in apply.h above the definition\n> of APPLY_OPT_INACCURATE_EOF and APPLY_OPT_RECOUNT, as suggested by\n> Stefan. This is the only change compared to v13.\n\nOK.\n\n\n>   - Patches 33/41 to 37/41 were in v10, v12 and v13.\n>\n> They implement a way to make the libified apply code silent by\n> changing the bool `apply_verbosely` into a tristate enum called\n> `apply_verbosity`, that can be one of `verbosity_verbose`,\n> `verbosity_normal` or `verbosity_silent`.\n\nThis is a reasonable approach.\n\n> The only changes since v13 are in 37/41. The name of the first\n> argument to mute_routine() is changed from \"bla\" to \"msg\" as suggested\n> by Ramsey, and the commit message is improved as suggested by Stefan.\n\n;-)\n\n>   - Patch 40/41 was in v12 and v13, and hasn't changed.\n>\n> It adds a \"const char *index_file\" into \"struct apply_state\", to make\n> it possible to use a special index file instead of the default one.\n\nLooks sensible.\n"}]}