blob: 47aeb0b1674c01fd35e3aef759756348026a04bb [file] [log] [blame]
Thomas Rast4f353652009-08-15 13:48:30 +02001#!/bin/sh
2
3test_description='git checkout --patch'
4
5. ./lib-patch-mode.sh
6
Ævar Arnfjörð Bjarmasonf2f7b6a2010-08-13 20:40:06 +00007test_expect_success PERL 'setup' '
Thomas Rast4f353652009-08-15 13:48:30 +02008 mkdir dir &&
9 echo parent > dir/foo &&
10 echo dummy > bar &&
11 git add bar dir/foo &&
12 git commit -m initial &&
13 test_tick &&
14 test_commit second dir/foo head &&
15 set_and_save_state bar bar_work bar_index &&
16 save_head
17'
18
19# note: bar sorts before dir/foo, so the first 'n' is always to skip 'bar'
20
Ævar Arnfjörð Bjarmasonf2f7b6a2010-08-13 20:40:06 +000021test_expect_success PERL 'saying "n" does nothing' '
Thomas Rast4f353652009-08-15 13:48:30 +020022 set_and_save_state dir/foo work head &&
Eric Sunshine0590ff22018-07-01 20:23:42 -040023 test_write_lines n n | git checkout -p &&
Thomas Rast4f353652009-08-15 13:48:30 +020024 verify_saved_state bar &&
25 verify_saved_state dir/foo
26'
27
Ævar Arnfjörð Bjarmasonf2f7b6a2010-08-13 20:40:06 +000028test_expect_success PERL 'git checkout -p' '
Eric Sunshine0590ff22018-07-01 20:23:42 -040029 test_write_lines n y | git checkout -p &&
Thomas Rast4f353652009-08-15 13:48:30 +020030 verify_saved_state bar &&
31 verify_state dir/foo head head
32'
33
Ævar Arnfjörð Bjarmasonf2f7b6a2010-08-13 20:40:06 +000034test_expect_success PERL 'git checkout -p with staged changes' '
Jonathan Niedera48fcd82010-10-30 20:46:54 -050035 set_state dir/foo work index &&
Eric Sunshine0590ff22018-07-01 20:23:42 -040036 test_write_lines n y | git checkout -p &&
Thomas Rast4f353652009-08-15 13:48:30 +020037 verify_saved_state bar &&
38 verify_state dir/foo index index
39'
40
Ævar Arnfjörð Bjarmasonf2f7b6a2010-08-13 20:40:06 +000041test_expect_success PERL 'git checkout -p HEAD with NO staged changes: abort' '
Thomas Rast4f353652009-08-15 13:48:30 +020042 set_and_save_state dir/foo work head &&
Eric Sunshine0590ff22018-07-01 20:23:42 -040043 test_write_lines n y n | git checkout -p HEAD &&
Thomas Rast4f353652009-08-15 13:48:30 +020044 verify_saved_state bar &&
45 verify_saved_state dir/foo
46'
47
Ævar Arnfjörð Bjarmasonf2f7b6a2010-08-13 20:40:06 +000048test_expect_success PERL 'git checkout -p HEAD with NO staged changes: apply' '
Eric Sunshine0590ff22018-07-01 20:23:42 -040049 test_write_lines n y y | git checkout -p HEAD &&
Thomas Rast4f353652009-08-15 13:48:30 +020050 verify_saved_state bar &&
51 verify_state dir/foo head head
52'
53
Ævar Arnfjörð Bjarmasonf2f7b6a2010-08-13 20:40:06 +000054test_expect_success PERL 'git checkout -p HEAD with change already staged' '
Jonathan Niedera8146152010-09-07 03:22:53 -050055 set_state dir/foo index index &&
Thomas Rast4f353652009-08-15 13:48:30 +020056 # the third n is to get out in case it mistakenly does not apply
Eric Sunshine0590ff22018-07-01 20:23:42 -040057 test_write_lines n y n | git checkout -p HEAD &&
Thomas Rast4f353652009-08-15 13:48:30 +020058 verify_saved_state bar &&
59 verify_state dir/foo head head
60'
61
Ævar Arnfjörð Bjarmasonf2f7b6a2010-08-13 20:40:06 +000062test_expect_success PERL 'git checkout -p HEAD^' '
Thomas Rast4f353652009-08-15 13:48:30 +020063 # the third n is to get out in case it mistakenly does not apply
Eric Sunshine0590ff22018-07-01 20:23:42 -040064 test_write_lines n y n | git checkout -p HEAD^ &&
Thomas Rast4f353652009-08-15 13:48:30 +020065 verify_saved_state bar &&
66 verify_state dir/foo parent parent
67'
68
Ævar Arnfjörð Bjarmasonf2f7b6a2010-08-13 20:40:06 +000069test_expect_success PERL 'git checkout -p handles deletion' '
Jeff Kinge1327ed2010-02-22 20:05:44 -050070 set_state dir/foo work index &&
71 rm dir/foo &&
Eric Sunshine0590ff22018-07-01 20:23:42 -040072 test_write_lines n y | git checkout -p &&
Jeff Kinge1327ed2010-02-22 20:05:44 -050073 verify_saved_state bar &&
74 verify_state dir/foo index index
75'
76
Thomas Rast4f353652009-08-15 13:48:30 +020077# The idea in the rest is that bar sorts first, so we always say 'y'
78# first and if the path limiter fails it'll apply to bar instead of
79# dir/foo. There's always an extra 'n' to reject edits to dir/foo in
80# the failure case (and thus get out of the loop).
81
Ævar Arnfjörð Bjarmasonf2f7b6a2010-08-13 20:40:06 +000082test_expect_success PERL 'path limiting works: dir' '
Thomas Rast4f353652009-08-15 13:48:30 +020083 set_state dir/foo work head &&
Eric Sunshine0590ff22018-07-01 20:23:42 -040084 test_write_lines y n | git checkout -p dir &&
Thomas Rast4f353652009-08-15 13:48:30 +020085 verify_saved_state bar &&
86 verify_state dir/foo head head
87'
88
Ævar Arnfjörð Bjarmasonf2f7b6a2010-08-13 20:40:06 +000089test_expect_success PERL 'path limiting works: -- dir' '
Thomas Rast4f353652009-08-15 13:48:30 +020090 set_state dir/foo work head &&
Eric Sunshine0590ff22018-07-01 20:23:42 -040091 test_write_lines y n | git checkout -p -- dir &&
Thomas Rast4f353652009-08-15 13:48:30 +020092 verify_saved_state bar &&
93 verify_state dir/foo head head
94'
95
Ævar Arnfjörð Bjarmasonf2f7b6a2010-08-13 20:40:06 +000096test_expect_success PERL 'path limiting works: HEAD^ -- dir' '
Thomas Rast4f353652009-08-15 13:48:30 +020097 # the third n is to get out in case it mistakenly does not apply
Eric Sunshine0590ff22018-07-01 20:23:42 -040098 test_write_lines y n n | git checkout -p HEAD^ -- dir &&
Thomas Rast4f353652009-08-15 13:48:30 +020099 verify_saved_state bar &&
100 verify_state dir/foo parent parent
101'
102
Ævar Arnfjörð Bjarmasonf2f7b6a2010-08-13 20:40:06 +0000103test_expect_success PERL 'path limiting works: foo inside dir' '
Thomas Rast4f353652009-08-15 13:48:30 +0200104 set_state dir/foo work head &&
105 # the third n is to get out in case it mistakenly does not apply
Eric Sunshine0590ff22018-07-01 20:23:42 -0400106 test_write_lines y n n | (cd dir && git checkout -p foo) &&
Thomas Rast4f353652009-08-15 13:48:30 +0200107 verify_saved_state bar &&
108 verify_state dir/foo head head
109'
110
Ævar Arnfjörð Bjarmasonf2f7b6a2010-08-13 20:40:06 +0000111test_expect_success PERL 'none of this moved HEAD' '
Thomas Rast4f353652009-08-15 13:48:30 +0200112 verify_saved_head
113'
114
115test_done