Back in edf2e37 (git-apply: work from subdirectory. - 2005-11-25),
git-apply is made to work from a subdir of a worktree. When applying a
git patch this way, only paths in the subdir are patched, the rest is
filtered out. To apply without filtering, the user has to move back to
toplevel. Add --whole to make it more convenient to do so without moving
cwd around.

Signed-off-by: Nguyễn Thái Ngọc Duy <pclo...@gmail.com>
---
 Documentation/git-apply.txt |  8 +++++++-
 builtin/apply.c             |  4 +++-
 t/t4111-apply-subdir.sh     | 32 ++++++++++++++++++++++++++++++++
 3 files changed, 42 insertions(+), 2 deletions(-)

diff --git a/Documentation/git-apply.txt b/Documentation/git-apply.txt
index 8ddb207..47cea57 100644
--- a/Documentation/git-apply.txt
+++ b/Documentation/git-apply.txt
@@ -13,7 +13,7 @@ SYNOPSIS
          [--apply] [--no-add] [--build-fake-ancestor=<file>] [-R | --reverse]
          [--allow-binary-replacement | --binary] [--reject] [-z]
          [-p<n>] [-C<n>] [--inaccurate-eof] [--recount] [--cached]
-         [--ignore-space-change | --ignore-whitespace]
+         [--ignore-space-change | --ignore-whitespace] [--whole]
          [--whitespace=(nowarn|warn|fix|error|error-all)]
          [--exclude=<path>] [--include=<path>] [--directory=<root>]
          [--verbose] [--unsafe-paths] [<patch>...]
@@ -154,6 +154,12 @@ discouraged.
        flag was the way to do so.  Currently we always allow binary
        patch application, so this is a no-op.
 
+--whole::
+       Normally when running inside a subdirectory of a working area,
+       patched files outside current directory is filtered out. This option
+       makes `git apply` to apply them all. All paths are still subject
+       to `--exclude` and `--include` fitlering if present.
+
 --exclude=<path-pattern>::
        Don't apply changes to files matching the given path pattern. This can
        be useful when importing patchsets, where you want to exclude certain
diff --git a/builtin/apply.c b/builtin/apply.c
index 42c610e..01e1d5e 100644
--- a/builtin/apply.c
+++ b/builtin/apply.c
@@ -80,6 +80,7 @@ static const char *patch_input_file;
 static struct strbuf root = STRBUF_INIT;
 static int read_stdin = 1;
 static int options;
+static int apply_whole;
 
 static void parse_whitespace_option(const char *option)
 {
@@ -1956,7 +1957,7 @@ static int use_patch(struct patch *p)
        int i;
 
        /* Paths outside are not touched regardless of "--include" */
-       if (0 < prefix_length) {
+       if (!apply_whole && 0 < prefix_length) {
                int pathlen = strlen(pathname);
                if (pathlen <= prefix_length ||
                    memcmp(prefix, pathname, prefix_length))
@@ -4565,6 +4566,7 @@ int cmd_apply(int argc, const char **argv, const char 
*prefix_)
                OPT_BIT(0, "recount", &options,
                        N_("do not trust the line counts in the hunk headers"),
                        RECOUNT),
+               OPT_BOOL(0, "whole", &apply_whole, N_("apply whole patch")),
                { OPTION_CALLBACK, 0, "directory", NULL, N_("root"),
                        N_("prepend <root> to all filenames"),
                        0, option_parse_directory },
diff --git a/t/t4111-apply-subdir.sh b/t/t4111-apply-subdir.sh
index 1618a6d..e5cd019 100755
--- a/t/t4111-apply-subdir.sh
+++ b/t/t4111-apply-subdir.sh
@@ -153,4 +153,36 @@ test_expect_success 'apply --cached from subdir of .git 
dir' '
        test_cmp expected.subdir actual.subdir
 '
 
+test_expect_success 'setup a git patch' '
+       cat >gitpatch <<-\EOF &&
+       diff --git a/file b/file
+       --- a/file
+       +++ b/file
+       @@ -1 +1,2 @@
+        1
+       +2
+       EOF
+       gitpatch="$(pwd)/gitpatch"
+'
+
+test_expect_success 'apply a git patch from subdir of toplevel' '
+       reset_subdir other preimage &&
+       (
+               cd sub/dir &&
+               git apply "$gitpatch"
+       ) &&
+       test_cmp preimage sub/dir/file &&
+       test_cmp preimage file
+'
+
+test_expect_success 'apply the whole git patch from subdir of toplevel' '
+       reset_subdir other preimage &&
+       (
+               cd sub/dir &&
+               git apply --whole "$gitpatch"
+       ) &&
+       test_cmp preimage sub/dir/file &&
+       test_cmp postimage file
+'
+
 test_done
-- 
2.8.0.rc0.210.gd302cd2

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to