{"thread":{"id":"43059","subject":"[PATCH v12 01/13] builtin/apply: rename option parsing functions","startedAt":"2016-08-11T18:46:45Z","lastAt":"2016-08-26T23:46:32Z","messageCount":15,"participants":["Christian Couder","Junio C Hamano"],"isPatch":true,"patchVersion":12,"patchTotal":13},"messages":[{"id":"293777","messageId":"20160811184501.384-2-chriscool@tuxfamily.org","threadId":"43059","inReplyTo":"20160811184501.384-1-chriscool@tuxfamily.org","subject":"[PATCH v12 01/13] builtin/apply: rename option parsing functions","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-08-11T18:44:49Z","receivedAt":"2016-08-11T18:46:45Z","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 ad403f8..429fe44 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.9.2.769.gc0f0333\n\n"},{"id":"293778","messageId":"20160811184501.384-1-chriscool@tuxfamily.org","threadId":"43059","inReplyTo":null,"subject":"[PATCH v12 00/13] libify apply and use lib in am, part 3","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-08-11T18:44:48Z","receivedAt":"2016-08-11T18:46:49Z","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/\n\nHighlevel view of the patches in the series\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\nThis is \"part 3\" of the full patch series. I am resending only the\nlast 13 patches of the full series as \"part 3\", because I don't want\nto resend the first 27 patches of v10 nearly as is. So \"part 2\" is\nmade of patch 01/40 from v11 and patches from 02/40 to 27/40 from v10.\nThe \"part 1\" is in \"master\" for some time now.\n\n  - Patch 01/13 was v1, v2, v6, v7, v8, v9, v10 and v11.\n\nSince v10 and v11 only the commit message has been changed. As\nsuggested by Stefan Naewe, 'api' as been replaced with 'API'.\n\n  - Patches 02/13 to 04/13 were in v1, v2, v6, v7, v8, v9 and v10.\n\nThey haven't changed since v10.\n\nAlong with 01/13 they finish libifying the apply functionality that\nwas in builtin/apply.c and move it into apply.{c,h}, but the libified\nfunctionality is not yet used in `git am`.\n\n  - Patch 05/13 was in v6, v7, v8, v9 and v10, and hasn't changed.\n\nIt replaces some calls to error() with calls to error_errno().\n\n  - Patches 06/13 to 10/13 were in v2, v6, v7, v8, v9 and v10.\n\nThey haven't changed since v10.\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\nThis is because \"git am\", since it was a shell script, has been\nsilencing the apply functionality by redirecting file descriptors to\n/dev/null, but this is not acceptable in C.\n\n  - Patch 11/13 was in v9 and v10, 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 as suggested by Junio.\n\n  - Patch 12/13 is new.\n\nIt replaces patch 33/40 in v10 (environment: add set_index_file())\nthat was a hack to make it possible for `git am` to use the libified\napply functionality on a different index file.\n\nNow for this purpose, in this new patch, we add\n\"const char *index_file\" into \"struct apply_state\", and when\n\"index_file\" is set, we use hold_lock_file_for_update(), passing it\n\"state->index_file\", instead of using hold_locked_index(), as it is\nnot possible to pass an index filename in hold_locked_index().\n\n  - Patch 13/13 was in v1, v2, v6, v7, v8, v9 and v10.\n\nThis patch makes `git am` use the libified functionality. It now uses\n\"index_file\" in \"struct apply_state\" added by 12/13 to pass the file\nwe want the index written into to the libified apply functionality.\n\n\nGeneral comments\n~~~~~~~~~~~~~~~~\n\nNow this patch series is shorter than previously. Hopefully also the\nearly part of this series until 05/13 or 11/13 will be ready soon to\nbe moved to next and master, and I may only need to resend the last 2\npatches if anything.\n\nI will send a diff between this version and v10 as a reply to this\nemail. (Note that in v11 only commit messages changed, so there is no\ndifference between v10 and v11.)\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\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 (13):\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: learn to use a different index file\n  builtin/am: use apply API in run_apply()\n\n apply.c           | 4861 ++++++++++++++++++++++++++++++++++++++++++++++++++++-\n apply.h           |   34 +-\n builtin/am.c      |   65 +-\n builtin/apply.c   | 4814 +---------------------------------------------------\n git-compat-util.h |    3 +\n usage.c           |   15 +\n 6 files changed, 4951 insertions(+), 4841 deletions(-)\n\n-- \n2.9.2.769.gc0f0333\n\n"},{"id":"293779","messageId":"20160811184501.384-3-chriscool@tuxfamily.org","threadId":"43059","inReplyTo":"20160811184501.384-1-chriscool@tuxfamily.org","subject":"[PATCH v12 02/13] apply: rename and move opt constants to apply.h","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-08-11T18:44:50Z","receivedAt":"2016-08-11T18:46:52Z","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\nSigned-off-by: Christian Couder <chriscool@tuxfamily.org>\n---\n apply.h         |  3 +++\n builtin/apply.c | 11 ++++-------\n 2 files changed, 7 insertions(+), 7 deletions(-)\n\ndiff --git a/apply.h b/apply.h\nindex 53f09b5..ca1dcee 100644\n--- a/apply.h\n+++ b/apply.h\n@@ -108,4 +108,7 @@ 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+#define APPLY_OPT_INACCURATE_EOF\t(1<<0)\n+#define APPLY_OPT_RECOUNT\t\t(1<<1)\n+\n #endif\ndiff --git a/builtin/apply.c b/builtin/apply.c\nindex 429fe44..9c396bb 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.9.2.769.gc0f0333\n\n"},{"id":"293780","messageId":"20160811184501.384-5-chriscool@tuxfamily.org","threadId":"43059","inReplyTo":"20160811184501.384-1-chriscool@tuxfamily.org","subject":"[PATCH v12 04/13] apply: make some parsing functions static again","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-08-11T18:44:52Z","receivedAt":"2016-08-11T18:47:10Z","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 7b96130..c0cb3f5 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 5ec022c..df44b51 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.9.2.769.gc0f0333\n\n"},{"id":"293781","messageId":"20160811184501.384-7-chriscool@tuxfamily.org","threadId":"43059","inReplyTo":"20160811184501.384-1-chriscool@tuxfamily.org","subject":"[PATCH v12 06/13] apply: make it possible to silently apply","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-08-11T18:44:54Z","receivedAt":"2016-08-11T18:47:13Z","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 df44b51..bd4eb6d 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.9.2.769.gc0f0333\n\n"},{"id":"293782","messageId":"20160811184501.384-6-chriscool@tuxfamily.org","threadId":"43059","inReplyTo":"20160811184501.384-1-chriscool@tuxfamily.org","subject":"[PATCH v12 05/13] apply: use error_errno() where possible","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-08-11T18:44:53Z","receivedAt":"2016-08-11T18:47:14Z","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 | 18 +++++++++---------\n 1 file changed, 9 insertions(+), 9 deletions(-)\n\ndiff --git a/apply.c b/apply.c\nindex c0cb3f5..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@@ -4306,7 +4306,7 @@ static int try_create_file(const char *path, unsigned int mode, const char *buf,\n \tstrbuf_release(&nbuf);\n \n \tif (close(fd) < 0 && !res)\n-\t\treturn error(_(\"closing file '%s': %s\"), path, strerror(errno));\n+\t\treturn error_errno(_(\"closing file '%s'\"), path);\n \n \treturn res ? -1 : 0;\n }\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.9.2.769.gc0f0333\n\n"},{"id":"293783","messageId":"20160811184501.384-8-chriscool@tuxfamily.org","threadId":"43059","inReplyTo":"20160811184501.384-1-chriscool@tuxfamily.org","subject":"[PATCH v12 07/13] apply: don't print on stdout in verbosity_silent mode","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-08-11T18:44:55Z","receivedAt":"2016-08-11T18:47: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.9.2.769.gc0f0333\n\n"},{"id":"293784","messageId":"20160811184501.384-10-chriscool@tuxfamily.org","threadId":"43059","inReplyTo":"20160811184501.384-1-chriscool@tuxfamily.org","subject":"[PATCH v12 09/13] usage: add get_error_routine() and get_warn_routine()","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-08-11T18:44:57Z","receivedAt":"2016-08-11T18:47:26Z","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 c7a51f8..de04df1 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.9.2.769.gc0f0333\n\n"},{"id":"293785","messageId":"20160811184501.384-12-chriscool@tuxfamily.org","threadId":"43059","inReplyTo":"20160811184501.384-1-chriscool@tuxfamily.org","subject":"[PATCH v12 11/13] apply: refactor `git apply` option parsing","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-08-11T18:44:59Z","receivedAt":"2016-08-11T18:47:29Z","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 bf81b70..2ec2a8a 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 5cde641..e2b89e8 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.9.2.769.gc0f0333\n\n"},{"id":"293786","messageId":"20160811184501.384-9-chriscool@tuxfamily.org","threadId":"43059","inReplyTo":"20160811184501.384-1-chriscool@tuxfamily.org","subject":"[PATCH v12 08/13] usage: add set_warn_routine()","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-08-11T18:44:56Z","receivedAt":"2016-08-11T18:47:30Z","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 590bfdd..c7a51f8 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.9.2.769.gc0f0333\n\n"},{"id":"293787","messageId":"20160811184501.384-11-chriscool@tuxfamily.org","threadId":"43059","inReplyTo":"20160811184501.384-1-chriscool@tuxfamily.org","subject":"[PATCH v12 10/13] apply: change error_routine when silent","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-08-11T18:44:58Z","receivedAt":"2016-08-11T18:47:31Z","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\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..bf81b70 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 *bla, 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 bd4eb6d..5cde641 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.9.2.769.gc0f0333\n\n"},{"id":"293788","messageId":"20160811184501.384-14-chriscool@tuxfamily.org","threadId":"43059","inReplyTo":"20160811184501.384-1-chriscool@tuxfamily.org","subject":"[PATCH v12 13/13] builtin/am: use apply API in run_apply()","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-08-11T18:45:01Z","receivedAt":"2016-08-11T18:47:34Z","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..0e5d384 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 thru 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.9.2.769.gc0f0333\n\n"},{"id":"293789","messageId":"20160811184501.384-13-chriscool@tuxfamily.org","threadId":"43059","inReplyTo":"20160811184501.384-1-chriscool@tuxfamily.org","subject":"[PATCH v12 12/13] apply: learn to use a different index file","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-08-11T18:45:00Z","receivedAt":"2016-08-11T18:47:35Z","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 | 10 ++++++++--\n apply.h |  1 +\n 2 files changed, 9 insertions(+), 2 deletions(-)\n\ndiff --git a/apply.c b/apply.c\nindex 2ec2a8a..7e561a4 100644\n--- a/apply.c\n+++ b/apply.c\n@@ -4674,8 +4674,14 @@ 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 \t\terror(_(\"unable to read index file\"));\ndiff --git a/apply.h b/apply.h\nindex e2b89e8..1ba4f8d 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.9.2.769.gc0f0333\n\n"},{"id":"297739","messageId":"xmqqvaz7ys9u.fsf@gitster.mtv.corp.google.com","threadId":"43059","inReplyTo":"20160811184501.384-13-chriscool@tuxfamily.org","subject":"Re: [PATCH v12 12/13] apply: learn to use a different index file","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-08-11T20:17:33Z","receivedAt":"2016-08-11T20:29:33Z","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> Sometimes we want to apply in a different index file.\n>\n> Before the apply functionality was libified it was possible to\n> use the GIT_INDEX_FILE environment variable, for this purpose.\n>\n> But now, as the apply functionality has been libified, it should\n> be possible to do that in a libified way.\n>\n> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n> ---\n>  apply.c | 10 ++++++++--\n>  apply.h |  1 +\n>  2 files changed, 9 insertions(+), 2 deletions(-)\n>\n> diff --git a/apply.c b/apply.c\n> index 2ec2a8a..7e561a4 100644\n> --- a/apply.c\n> +++ b/apply.c\n> @@ -4674,8 +4674,14 @@ 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>  \t\terror(_(\"unable to read index file\"));\n\nHere is a call to read_cache() that reads the default index file on\nthe filesystem into the default in-core index \"the_index\".\n\nShouldn't it be reading from state->index_file instead?\n\nIf we limit the review only to the context of your series, I think\n\n    fall_back_threeway()\n     -> build_fake_ancestor() -- uses index_path to use custom index\n     -> discard_cache()\n     -> read_cache_from(index_path) -- reads back the fake ancestor\n     -> write_index_as_tree() -- writes the fake_ancestor\n     -> run_apply(index_path)\n        -> apply_all_patches()\n           -> apply_patch()\n\nis the only codepath that uses a custom index file, so when the\ncontrol reaches this function with a custom index file, the in-core\nindex is already populated, making read_cache() a no-op, and that is\nthe only thing that makes the resulting code avoid triggering this\nbug, but as part of a general \"libified\" codepath, I think it should\nbe made to read from state->index_file using read_cache_from().\n\nI only noticed this call to read_cache(), but there may be others\nlurking nearby.\n"},{"id":"300320","messageId":"CAP8UFD0UxpRbbBeONETjMmdAeA64NoHXc+eAjasYQSCuc5=EyQ@mail.gmail.com","threadId":"43059","inReplyTo":"xmqqvaz7ys9u.fsf@gitster.mtv.corp.google.com","subject":"Re: [PATCH v12 12/13] apply: learn to use a different index file","fromName":"Christian Couder","fromEmail":"christian.couder@gmail.com","sentAt":"2016-08-26T23:46:24Z","receivedAt":"2016-08-26T23:46:32Z","isPatch":true,"sender":{"key":"christian.couder@gmail.com","avatar":"https://avatars.githubusercontent.com/u/208954?v=4"},"body":"On Thu, Aug 11, 2016 at 10:17 PM, Junio C Hamano <gitster@pobox.com> wrote:\n> Christian Couder <christian.couder@gmail.com> writes:\n>\n>> Sometimes we want to apply in a different index file.\n>>\n>> Before the apply functionality was libified it was possible to\n>> use the GIT_INDEX_FILE environment variable, for this purpose.\n>>\n>> But now, as the apply functionality has been libified, it should\n>> be possible to do that in a libified way.\n>>\n>> Signed-off-by: Christian Couder <chriscool@tuxfamily.org>\n>> ---\n>>  apply.c | 10 ++++++++--\n>>  apply.h |  1 +\n>>  2 files changed, 9 insertions(+), 2 deletions(-)\n>>\n>> diff --git a/apply.c b/apply.c\n>> index 2ec2a8a..7e561a4 100644\n>> --- a/apply.c\n>> +++ b/apply.c\n>> @@ -4674,8 +4674,14 @@ static int apply_patch(struct apply_state *state,\n>>               state->apply = 0;\n>>\n>>       state->update_index = state->check_index && state->apply;\n>> -     if (state->update_index && state->newfd < 0)\n>> -             state->newfd = hold_locked_index(state->lock_file, 1);\n>> +     if (state->update_index && state->newfd < 0) {\n>> +             if (state->index_file)\n>> +                     state->newfd = hold_lock_file_for_update(state->lock_file,\n>> +                                                              state->index_file,\n>> +                                                              LOCK_DIE_ON_ERROR);\n>> +             else\n>> +                     state->newfd = hold_locked_index(state->lock_file, 1);\n>> +     }\n>>\n>>       if (state->check_index && read_cache() < 0) {\n>>               error(_(\"unable to read index file\"));\n>\n> Here is a call to read_cache() that reads the default index file on\n> the filesystem into the default in-core index \"the_index\".\n>\n> Shouldn't it be reading from state->index_file instead?\n\nYes, it should.\n\n> If we limit the review only to the context of your series, I think\n>\n>     fall_back_threeway()\n>      -> build_fake_ancestor() -- uses index_path to use custom index\n>      -> discard_cache()\n>      -> read_cache_from(index_path) -- reads back the fake ancestor\n>      -> write_index_as_tree() -- writes the fake_ancestor\n>      -> run_apply(index_path)\n>         -> apply_all_patches()\n>            -> apply_patch()\n>\n> is the only codepath that uses a custom index file, so when the\n> control reaches this function with a custom index file, the in-core\n> index is already populated, making read_cache() a no-op, and that is\n> the only thing that makes the resulting code avoid triggering this\n> bug, but as part of a general \"libified\" codepath,\n\nYeah, I agree with this reasoning.\n\n> I think it should\n> be made to read from state->index_file using read_cache_from().\n\nYeah, I will change it.\n\n> I only noticed this call to read_cache(), but there may be others\n> lurking nearby.\n\nYeah, there is another one in get_current_sha1() which is only called\nby build_fake_ancestor() (from apply.c not from builtin/am.c as there\nis a function with this name in both files), but this function is\ncurrently called only when --build-fake-ancestor is passed which is\nnot the case in run_apply() in am.c. I will change it too.\n\nThanks,\nChristian.\n"}]}