#!/bin/sh # # Copyright (c) 2005 Junio C Hamano # global test_description := ''git apply boundary tests '' source ./test-lib.sh global L := '"c d e f g h i j k l m n o p q r s t u v w x'" test_expect_success setup ' for i in b '"$L"' y do echo $i done >victim && cat victim >original && git update-index --add victim && # add to the head for i in a b '"$L"' y do echo $i done >victim && cat victim >add-a-expect && git diff victim >add-a-patch.with && git diff --unified=0 >add-a-patch.without && # insert at line two for i in b a '"$L"' y do echo $i done >victim && cat victim >insert-a-expect && git diff victim >insert-a-patch.with && git diff --unified=0 >insert-a-patch.without && # modify at the head for i in a '"$L"' y do echo $i done >victim && cat victim >mod-a-expect && git diff victim >mod-a-patch.with && git diff --unified=0 >mod-a-patch.without && # remove from the head for i in '"$L"' y do echo $i done >victim && cat victim >del-a-expect && git diff victim >del-a-patch.with && git diff --unified=0 >del-a-patch.without && # add to the tail for i in b '"$L"' y z do echo $i done >victim && cat victim >add-z-expect && git diff victim >add-z-patch.with && git diff --unified=0 >add-z-patch.without && # modify at the tail for i in b '"$L"' z do echo $i done >victim && cat victim >mod-z-expect && git diff victim >mod-z-patch.with && git diff --unified=0 >mod-z-patch.without && # remove from the tail for i in b '"$L"' do echo $i done >victim && cat victim >del-z-expect && git diff victim >del-z-patch.with && git diff --unified=0 >del-z-patch.without # done ' for with in [with without] { matchstr $with { with { global u := '' } without { global u := ''--unidiff-zero '' } } for kind in [add-a add-z insert-a mod-a mod-z del-a del-z] { test_expect_success "apply $kind-patch $with context" ' cat original >victim && git update-index victim && git apply --index '"$u$kind-patch.$with"' && test_cmp '"$kind"'-expect victim ' } } for kind in [add-a add-z insert-a mod-a mod-z del-a del-z] { rm -f $kind-ng.without sed -e "s/^diff --git /diff /" \ -e '/^index /d' \ <$kind-patch.without >$kind-ng.without test_expect_success "apply non-git $kind-patch without context" ' cat original >victim && git update-index victim && git apply --unidiff-zero --index '"$kind-ng.without"' && test_cmp '"$kind"'-expect victim ' } test_expect_success 'two lines' ' >file && git add file && echo aaa >file && git diff >patch && git add file && echo bbb >file && git add file && test_must_fail git apply --check patch ' test_expect_success 'apply patch with 3 context lines matching at end' ' { echo a; echo b; echo c; echo d; } >file && git add file && echo e >>file && git diff >patch && >file && test_must_fail git apply patch ' test_done (CommandList children: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:test_description) op: Equal rhs: {(SQ <"git apply boundary tests\n"> <"\n">)} spids: [13] ) ] spids: [13] ) (C {(.)} {(./test-lib.sh)}) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:L) op: Equal rhs: {(DQ ("c d e f g h i j k l m n o p q r s t u v w x"))} spids: [24] ) ] spids: [24] ) (C {(test_expect_success)} {(setup)} {(SQ <"\n"> <"\tfor i in b ">) (DQ ($ VSub_Name "$L")) (SQ <" y\n"> <"\tdo\n"> <"\t\techo $i\n"> <"\tdone >victim &&\n"> <"\tcat victim >original &&\n"> <"\tgit update-index --add victim &&\n"> <"\n"> <"\t# add to the head\n"> <"\tfor i in a b "> ) (DQ ($ VSub_Name "$L")) (SQ <" y\n"> <"\tdo\n"> <"\t\techo $i\n"> <"\tdone >victim &&\n"> <"\tcat victim >add-a-expect &&\n"> <"\tgit diff victim >add-a-patch.with &&\n"> <"\tgit diff --unified=0 >add-a-patch.without &&\n"> <"\n"> <"\t# insert at line two\n"> <"\tfor i in b a "> ) (DQ ($ VSub_Name "$L")) (SQ <" y\n"> <"\tdo\n"> <"\t\techo $i\n"> <"\tdone >victim &&\n"> <"\tcat victim >insert-a-expect &&\n"> <"\tgit diff victim >insert-a-patch.with &&\n"> <"\tgit diff --unified=0 >insert-a-patch.without &&\n"> <"\n"> <"\t# modify at the head\n"> <"\tfor i in a "> ) (DQ ($ VSub_Name "$L")) (SQ <" y\n"> <"\tdo\n"> <"\t\techo $i\n"> <"\tdone >victim &&\n"> <"\tcat victim >mod-a-expect &&\n"> <"\tgit diff victim >mod-a-patch.with &&\n"> <"\tgit diff --unified=0 >mod-a-patch.without &&\n"> <"\n"> <"\t# remove from the head\n"> <"\tfor i in "> ) (DQ ($ VSub_Name "$L")) (SQ <" y\n"> <"\tdo\n"> <"\t\techo $i\n"> <"\tdone >victim &&\n"> <"\tcat victim >del-a-expect &&\n"> <"\tgit diff victim >del-a-patch.with &&\n"> <"\tgit diff --unified=0 >del-a-patch.without &&\n"> <"\n"> <"\t# add to the tail\n"> <"\tfor i in b "> ) (DQ ($ VSub_Name "$L")) (SQ <" y z\n"> <"\tdo\n"> <"\t\techo $i\n"> <"\tdone >victim &&\n"> <"\tcat victim >add-z-expect &&\n"> <"\tgit diff victim >add-z-patch.with &&\n"> <"\tgit diff --unified=0 >add-z-patch.without &&\n"> <"\n"> <"\t# modify at the tail\n"> <"\tfor i in b "> ) (DQ ($ VSub_Name "$L")) (SQ <" z\n"> <"\tdo\n"> <"\t\techo $i\n"> <"\tdone >victim &&\n"> <"\tcat victim >mod-z-expect &&\n"> <"\tgit diff victim >mod-z-patch.with &&\n"> <"\tgit diff --unified=0 >mod-z-patch.without &&\n"> <"\n"> <"\t# remove from the tail\n"> <"\tfor i in b "> ) (DQ ($ VSub_Name "$L")) (SQ <"\n"> <"\tdo\n"> <"\t\techo $i\n"> <"\tdone >victim &&\n"> <"\tcat victim >del-z-expect &&\n"> <"\tgit diff victim >del-z-patch.with &&\n"> <"\tgit diff --unified=0 >del-z-patch.without\n"> <"\n"> <"\t# done\n"> ) } ) (ForEach iter_name: with iter_words: [{(with)} {(without)}] do_arg_iter: False body: (DoGroup children: [ (Case to_match: {(DQ ($ VSub_Name "$with"))} arms: [ (case_arm pat_list: [{(with)}] action: [ (Assignment keyword: Assign_None pairs: [(assign_pair lhs:(LhsName name:u) op:Equal rhs:{(SQ )} spids:[183])] spids: [183] ) ] spids: [180 181 185 -1] ) (case_arm pat_list: [{(without)}] action: [ (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:u) op: Equal rhs: {(SQ <"--unidiff-zero ">)} spids: [191] ) ] spids: [191] ) ] spids: [188 189 196 -1] ) ] spids: [171 177 199] ) (ForEach iter_name: kind iter_words: [{(add-a)} {(add-z)} {(insert-a)} {(mod-a)} {(mod-z)} {(del-a)} {(del-z)}] do_arg_iter: False body: (DoGroup children: [ (C {(test_expect_success)} { (DQ ("apply ") ($ VSub_Name "$kind") ("-patch ") ($ VSub_Name "$with") (" context") ) } { (SQ <"\n"> <"\t\t\tcat original >victim &&\n"> <"\t\t\tgit update-index victim &&\n"> <"\t\t\tgit apply --index "> ) (DQ ($ VSub_Name "$u") ($ VSub_Name "$kind") (-patch.) ($ VSub_Name "$with")) (SQ <" &&\n"> <"\t\t\ttest_cmp ">) (DQ ($ VSub_Name "$kind")) (SQ <"-expect victim\n"> <"\t\t">) } ) ] spids: [223 261] ) spids: [207 -1] ) ] spids: [168 263] ) spids: [163 -1] ) (ForEach iter_name: kind iter_words: [{(add-a)} {(add-z)} {(insert-a)} {(mod-a)} {(mod-z)} {(del-a)} {(del-z)}] do_arg_iter: False body: (DoGroup children: [ (C {(rm)} {(-f)} {($ VSub_Name "$kind") (-ng.without)}) (SimpleCommand words: [{(sed)} {(-e)} {(DQ ("s/^diff --git /diff /"))} {(-e)} {(SQ <"/^index /d">)}] redirects: [ (Redir op_id: Redir_Less fd: -1 arg_word: {($ VSub_Name "$kind") (-patch.without)} spids: [315] ) (Redir op_id: Redir_Great fd: -1 arg_word: {($ VSub_Name "$kind") (-ng.without)} spids: [319] ) ] ) (C {(test_expect_success)} {(DQ ("apply non-git ") ($ VSub_Name "$kind") ("-patch without context"))} { (SQ <"\n"> <"\t\tcat original >victim &&\n"> <"\t\tgit update-index victim &&\n"> <"\t\tgit apply --unidiff-zero --index "> ) (DQ ($ VSub_Name "$kind") (-ng.without)) (SQ <" &&\n"> <"\t\ttest_cmp ">) (DQ ($ VSub_Name "$kind")) (SQ <"-expect victim\n"> <"\t">) } ) ] spids: [286 354] ) spids: [271 -1] ) (C {(test_expect_success)} {(SQ <"two lines">)} { (SQ <"\n"> <"\n"> <"\t>file &&\n"> <"\tgit add file &&\n"> <"\techo aaa >file &&\n"> <"\tgit diff >patch &&\n"> <"\tgit add file &&\n"> <"\techo bbb >file &&\n"> <"\tgit add file &&\n"> <"\ttest_must_fail git apply --check patch\n"> <"\n"> ) } ) (C {(test_expect_success)} {(SQ <"apply patch with 3 context lines matching at end">)} { (SQ <"\n"> <"\t{ echo a; echo b; echo c; echo d; } >file &&\n"> <"\tgit add file &&\n"> <"\techo e >>file &&\n"> <"\tgit diff >patch &&\n"> <"\t>file &&\n"> <"\ttest_must_fail git apply patch\n"> ) } ) (C {(test_done)}) ] )