blob: 747eb5563eca8f1df4374f179a08019221ba1fac [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
Johannes Schindelined922dc2021-11-30 14:14:14 +00007test_expect_success '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
Johannes Schindelined922dc2021-11-30 14:14:14 +000021test_expect_success '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
Johannes Schindelined922dc2021-11-30 14:14:14 +000028test_expect_success '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
Johannes Schindelined922dc2021-11-30 14:14:14 +000034test_expect_success '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
Johannes Schindelined922dc2021-11-30 14:14:14 +000041test_expect_success '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
Johannes Schindelined922dc2021-11-30 14:14:14 +000048test_expect_success '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
Johannes Schindelined922dc2021-11-30 14:14:14 +000054test_expect_success '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
Johannes Schindelined922dc2021-11-30 14:14:14 +000062test_expect_success 'git checkout -p HEAD^...' '
Denton Liu5602b502020-10-07 00:56:15 -070063 # the third n is to get out in case it mistakenly does not apply
64 test_write_lines n y n | git checkout -p HEAD^... &&
65 verify_saved_state bar &&
66 verify_state dir/foo parent parent
67'
68
Johannes Schindelined922dc2021-11-30 14:14:14 +000069test_expect_success 'git checkout -p HEAD^' '
Thomas Rast4f353652009-08-15 13:48:30 +020070 # the third n is to get out in case it mistakenly does not apply
Eric Sunshine0590ff22018-07-01 20:23:42 -040071 test_write_lines n y n | git checkout -p HEAD^ &&
Thomas Rast4f353652009-08-15 13:48:30 +020072 verify_saved_state bar &&
73 verify_state dir/foo parent parent
74'
75
Johannes Schindelined922dc2021-11-30 14:14:14 +000076test_expect_success 'git checkout -p handles deletion' '
Jeff Kinge1327ed2010-02-22 20:05:44 -050077 set_state dir/foo work index &&
78 rm dir/foo &&
Eric Sunshine0590ff22018-07-01 20:23:42 -040079 test_write_lines n y | git checkout -p &&
Jeff Kinge1327ed2010-02-22 20:05:44 -050080 verify_saved_state bar &&
81 verify_state dir/foo index index
82'
83
Thomas Rast4f353652009-08-15 13:48:30 +020084# The idea in the rest is that bar sorts first, so we always say 'y'
85# first and if the path limiter fails it'll apply to bar instead of
86# dir/foo. There's always an extra 'n' to reject edits to dir/foo in
87# the failure case (and thus get out of the loop).
88
Johannes Schindelined922dc2021-11-30 14:14:14 +000089test_expect_success '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
Johannes Schindelined922dc2021-11-30 14:14:14 +000096test_expect_success 'path limiting works: -- dir' '
Thomas Rast4f353652009-08-15 13:48:30 +020097 set_state dir/foo work head &&
Eric Sunshine0590ff22018-07-01 20:23:42 -040098 test_write_lines y n | git checkout -p -- dir &&
Thomas Rast4f353652009-08-15 13:48:30 +020099 verify_saved_state bar &&
100 verify_state dir/foo head head
101'
102
Johannes Schindelined922dc2021-11-30 14:14:14 +0000103test_expect_success 'path limiting works: HEAD^ -- dir' '
Thomas Rast4f353652009-08-15 13:48:30 +0200104 # the third n is to get out in case it mistakenly does not apply
Eric Sunshine0590ff22018-07-01 20:23:42 -0400105 test_write_lines y n n | git checkout -p HEAD^ -- dir &&
Thomas Rast4f353652009-08-15 13:48:30 +0200106 verify_saved_state bar &&
107 verify_state dir/foo parent parent
108'
109
Johannes Schindelined922dc2021-11-30 14:14:14 +0000110test_expect_success 'path limiting works: foo inside dir' '
Thomas Rast4f353652009-08-15 13:48:30 +0200111 set_state dir/foo work head &&
112 # the third n is to get out in case it mistakenly does not apply
Eric Sunshine0590ff22018-07-01 20:23:42 -0400113 test_write_lines y n n | (cd dir && git checkout -p foo) &&
Thomas Rast4f353652009-08-15 13:48:30 +0200114 verify_saved_state bar &&
115 verify_state dir/foo head head
116'
117
Johannes Schindelined922dc2021-11-30 14:14:14 +0000118test_expect_success 'none of this moved HEAD' '
Thomas Rast4f353652009-08-15 13:48:30 +0200119 verify_saved_head
120'
121
Johannes Schindelined922dc2021-11-30 14:14:14 +0000122test_expect_success 'empty tree can be handled' '
Johannes Schindelin5c29f192020-12-19 14:55:59 +0000123 test_when_finished "git reset --hard" &&
124 git checkout -p $(test_oid empty_tree) --
125'
126
Thomas Rast4f353652009-08-15 13:48:30 +0200127test_done