#!/bin/sh global test_description := ''patching from inconvenient places'' source ./test-lib.sh test_expect_success 'setup' ' cat >patch <<-\EOF && diff file.orig file --- a/file.orig +++ b/file @@ -1 +1,2 @@ 1 +2 EOF patch="$(pwd)/patch" && echo 1 >preimage && printf "%s\n" 1 2 >postimage && echo 3 >other && test_tick && git commit --allow-empty -m basis ' test_expect_success 'setup: subdir' ' reset_subdir() { git reset && mkdir -p sub/dir/b && mkdir -p objects && cp "$1" file && cp "$1" objects/file && cp "$1" sub/dir/file && cp "$1" sub/dir/b/file && git add file sub/dir/file sub/dir/b/file objects/file && cp "$2" file && cp "$2" sub/dir/file && cp "$2" sub/dir/b/file && cp "$2" objects/file && test_might_fail git update-index --refresh -q } ' test_expect_success 'apply from subdir of toplevel' ' cp postimage expected && reset_subdir other preimage && ( cd sub/dir && git apply "$patch" ) && test_cmp expected sub/dir/file ' test_expect_success 'apply --cached from subdir of toplevel' ' cp postimage expected && cp other expected.working && reset_subdir preimage other && ( cd sub/dir && git apply --cached "$patch" ) && git show :sub/dir/file >actual && test_cmp expected actual && test_cmp expected.working sub/dir/file ' test_expect_success 'apply --index from subdir of toplevel' ' cp postimage expected && reset_subdir preimage other && ( cd sub/dir && test_must_fail git apply --index "$patch" ) && reset_subdir other preimage && ( cd sub/dir && test_must_fail git apply --index "$patch" ) && reset_subdir preimage preimage && ( cd sub/dir && git apply --index "$patch" ) && git show :sub/dir/file >actual && test_cmp expected actual && test_cmp expected sub/dir/file ' test_expect_success 'apply half-broken patch from subdir of toplevel' ' ( cd sub/dir && test_must_fail git apply <<-EOF --- sub/dir/file +++ sub/dir/file @@ -1,0 +1,0 @@ --- file_in_root +++ file_in_root @@ -1,0 +1,0 @@ EOF ) ' test_expect_success 'apply from .git dir' ' cp postimage expected && cp preimage .git/file && cp preimage .git/objects/file && ( cd .git && git apply "$patch" ) && test_cmp expected .git/file ' test_expect_success 'apply from subdir of .git dir' ' cp postimage expected && cp preimage .git/file && cp preimage .git/objects/file && ( cd .git/objects && git apply "$patch" ) && test_cmp expected .git/objects/file ' test_expect_success 'apply --cached from .git dir' ' cp postimage expected && cp other expected.working && cp other .git/file && reset_subdir preimage other && ( cd .git && git apply --cached "$patch" ) && git show :file >actual && test_cmp expected actual && test_cmp expected.working file && test_cmp expected.working .git/file ' test_expect_success 'apply --cached from subdir of .git dir' ' cp postimage expected && cp preimage expected.subdir && cp other .git/file && cp other .git/objects/file && reset_subdir preimage other && ( cd .git/objects && git apply --cached "$patch" ) && git show :file >actual && git show :objects/file >actual.subdir && test_cmp expected actual && test_cmp expected.subdir actual.subdir ' test_done (CommandList children: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:test_description) op: Equal rhs: {(SQ <"patching from inconvenient places">)} spids: [4] ) ] spids: [4] ) (C {(.)} {(./test-lib.sh)}) (C {(test_expect_success)} {(SQ )} { (SQ <"\n"> <"\tcat >patch <<-\\EOF &&\n"> <"\tdiff file.orig file\n"> <"\t--- a/file.orig\n"> <"\t+++ b/file\n"> <"\t@@ -1 +1,2 @@\n"> <"\t 1\n"> <"\t+2\n"> <"\tEOF\n"> <"\tpatch=\"$(pwd)/patch\" &&\n"> <"\n"> <"\techo 1 >preimage &&\n"> <"\tprintf \"%s\\n\" 1 2 >postimage &&\n"> <"\techo 3 >other &&\n"> <"\n"> <"\ttest_tick &&\n"> <"\tgit commit --allow-empty -m basis\n"> ) } ) (C {(test_expect_success)} {(SQ <"setup: subdir">)} { (SQ <"\n"> <"\treset_subdir() {\n"> <"\t\tgit reset &&\n"> <"\t\tmkdir -p sub/dir/b &&\n"> <"\t\tmkdir -p objects &&\n"> <"\t\tcp \"$1\" file &&\n"> <"\t\tcp \"$1\" objects/file &&\n"> <"\t\tcp \"$1\" sub/dir/file &&\n"> <"\t\tcp \"$1\" sub/dir/b/file &&\n"> <"\t\tgit add file sub/dir/file sub/dir/b/file objects/file &&\n"> <"\t\tcp \"$2\" file &&\n"> <"\t\tcp \"$2\" sub/dir/file &&\n"> <"\t\tcp \"$2\" sub/dir/b/file &&\n"> <"\t\tcp \"$2\" objects/file &&\n"> <"\t\ttest_might_fail git update-index --refresh -q\n"> <"\t}\n"> ) } ) (C {(test_expect_success)} {(SQ <"apply from subdir of toplevel">)} { (SQ <"\n"> <"\tcp postimage expected &&\n"> <"\treset_subdir other preimage &&\n"> <"\t(\n"> <"\t\tcd sub/dir &&\n"> <"\t\tgit apply \"$patch\"\n"> <"\t) &&\n"> <"\ttest_cmp expected sub/dir/file\n"> ) } ) (C {(test_expect_success)} {(SQ <"apply --cached from subdir of toplevel">)} { (SQ <"\n"> <"\tcp postimage expected &&\n"> <"\tcp other expected.working &&\n"> <"\treset_subdir preimage other &&\n"> <"\t(\n"> <"\t\tcd sub/dir &&\n"> <"\t\tgit apply --cached \"$patch\"\n"> <"\t) &&\n"> <"\tgit show :sub/dir/file >actual &&\n"> <"\ttest_cmp expected actual &&\n"> <"\ttest_cmp expected.working sub/dir/file\n"> ) } ) (C {(test_expect_success)} {(SQ <"apply --index from subdir of toplevel">)} { (SQ <"\n"> <"\tcp postimage expected &&\n"> <"\treset_subdir preimage other &&\n"> <"\t(\n"> <"\t\tcd sub/dir &&\n"> <"\t\ttest_must_fail git apply --index \"$patch\"\n"> <"\t) &&\n"> <"\treset_subdir other preimage &&\n"> <"\t(\n"> <"\t\tcd sub/dir &&\n"> <"\t\ttest_must_fail git apply --index \"$patch\"\n"> <"\t) &&\n"> <"\treset_subdir preimage preimage &&\n"> <"\t(\n"> <"\t\tcd sub/dir &&\n"> <"\t\tgit apply --index \"$patch\"\n"> <"\t) &&\n"> <"\tgit show :sub/dir/file >actual &&\n"> <"\ttest_cmp expected actual &&\n"> <"\ttest_cmp expected sub/dir/file\n"> ) } ) (C {(test_expect_success)} {(SQ <"apply half-broken patch from subdir of toplevel">)} { (SQ <"\n"> <"\t(\n"> <"\t\tcd sub/dir &&\n"> <"\t\ttest_must_fail git apply <<-EOF\n"> <"\t\t--- sub/dir/file\n"> <"\t\t+++ sub/dir/file\n"> <"\t\t@@ -1,0 +1,0 @@\n"> <"\t\t--- file_in_root\n"> <"\t\t+++ file_in_root\n"> <"\t\t@@ -1,0 +1,0 @@\n"> <"\t\tEOF\n"> <"\t)\n"> ) } ) (C {(test_expect_success)} {(SQ <"apply from .git dir">)} { (SQ <"\n"> <"\tcp postimage expected &&\n"> <"\tcp preimage .git/file &&\n"> <"\tcp preimage .git/objects/file &&\n"> <"\t(\n"> <"\t\tcd .git &&\n"> <"\t\tgit apply \"$patch\"\n"> <"\t) &&\n"> <"\ttest_cmp expected .git/file\n"> ) } ) (C {(test_expect_success)} {(SQ <"apply from subdir of .git dir">)} { (SQ <"\n"> <"\tcp postimage expected &&\n"> <"\tcp preimage .git/file &&\n"> <"\tcp preimage .git/objects/file &&\n"> <"\t(\n"> <"\t\tcd .git/objects &&\n"> <"\t\tgit apply \"$patch\"\n"> <"\t) &&\n"> <"\ttest_cmp expected .git/objects/file\n"> ) } ) (C {(test_expect_success)} {(SQ <"apply --cached from .git dir">)} { (SQ <"\n"> <"\tcp postimage expected &&\n"> <"\tcp other expected.working &&\n"> <"\tcp other .git/file &&\n"> <"\treset_subdir preimage other &&\n"> <"\t(\n"> <"\t\tcd .git &&\n"> <"\t\tgit apply --cached \"$patch\"\n"> <"\t) &&\n"> <"\tgit show :file >actual &&\n"> <"\ttest_cmp expected actual &&\n"> <"\ttest_cmp expected.working file &&\n"> <"\ttest_cmp expected.working .git/file\n"> ) } ) (C {(test_expect_success)} {(SQ <"apply --cached from subdir of .git dir">)} { (SQ <"\n"> <"\tcp postimage expected &&\n"> <"\tcp preimage expected.subdir &&\n"> <"\tcp other .git/file &&\n"> <"\tcp other .git/objects/file &&\n"> <"\treset_subdir preimage other &&\n"> <"\t(\n"> <"\t\tcd .git/objects &&\n"> <"\t\tgit apply --cached \"$patch\"\n"> <"\t) &&\n"> <"\tgit show :file >actual &&\n"> <"\tgit show :objects/file >actual.subdir &&\n"> <"\ttest_cmp expected actual &&\n"> <"\ttest_cmp expected.subdir actual.subdir\n"> ) } ) (C {(test_done)}) ] )