blob: 42d552211915754e14ddb79d869fc9ba8f517fce [file] [log] [blame]
Nguyễn Thái Ngọc Duy4df3ec62019-04-25 16:45:55 +07001#!/bin/sh
2
3test_description='git restore --patch'
4
Rubén Justo2f64da02024-03-14 19:08:58 +01005TEST_PASSES_SANITIZE_LEAK=true
Nguyễn Thái Ngọc Duy4df3ec62019-04-25 16:45:55 +07006. ./lib-patch-mode.sh
7
Ghanshyam Thakkar7abc1862024-02-13 05:35:30 +05308test_expect_success 'setup' '
Nguyễn Thái Ngọc Duy4df3ec62019-04-25 16:45:55 +07009 mkdir dir &&
10 echo parent >dir/foo &&
11 echo dummy >bar &&
12 git add bar dir/foo &&
13 git commit -m initial &&
14 test_tick &&
15 test_commit second dir/foo head &&
16 set_and_save_state bar bar_work bar_index &&
17 save_head
18'
19
Ghanshyam Thakkar7abc1862024-02-13 05:35:30 +053020test_expect_success 'restore -p without pathspec is fine' '
Nguyễn Thái Ngọc Duy4df3ec62019-04-25 16:45:55 +070021 echo q >cmd &&
22 git restore -p <cmd
23'
24
25# note: bar sorts before dir/foo, so the first 'n' is always to skip 'bar'
26
Ghanshyam Thakkar7abc1862024-02-13 05:35:30 +053027test_expect_success 'saying "n" does nothing' '
Nguyễn Thái Ngọc Duy4df3ec62019-04-25 16:45:55 +070028 set_and_save_state dir/foo work head &&
29 test_write_lines n n | git restore -p &&
30 verify_saved_state bar &&
31 verify_saved_state dir/foo
32'
33
Ghanshyam Thakkar7abc1862024-02-13 05:35:30 +053034test_expect_success 'git restore -p' '
Nguyễn Thái Ngọc Duy4df3ec62019-04-25 16:45:55 +070035 set_and_save_state dir/foo work head &&
36 test_write_lines n y | git restore -p &&
37 verify_saved_state bar &&
38 verify_state dir/foo head head
39'
40
Ghanshyam Thakkar7abc1862024-02-13 05:35:30 +053041test_expect_success 'git restore -p with staged changes' '
Nguyễn Thái Ngọc Duy4df3ec62019-04-25 16:45:55 +070042 set_state dir/foo work index &&
43 test_write_lines n y | git restore -p &&
44 verify_saved_state bar &&
45 verify_state dir/foo index index
46'
47
Ghanshyam Thakkar5a8ed3f2024-02-13 05:35:29 +053048for opt in "HEAD" "@"
49do
Ghanshyam Thakkar7abc1862024-02-13 05:35:30 +053050 test_expect_success "git restore -p --source=$opt" '
Ghanshyam Thakkar5a8ed3f2024-02-13 05:35:29 +053051 set_state dir/foo work index &&
52 # the third n is to get out in case it mistakenly does not apply
53 test_write_lines n y n | git restore -p --source=$opt >output &&
54 verify_saved_state bar &&
55 verify_state dir/foo head index &&
56 test_grep "Discard" output
57 '
58done
Nguyễn Thái Ngọc Duy4df3ec62019-04-25 16:45:55 +070059
Ghanshyam Thakkar7abc1862024-02-13 05:35:30 +053060test_expect_success 'git restore -p --source=HEAD^' '
Nguyễn Thái Ngọc Duy4df3ec62019-04-25 16:45:55 +070061 set_state dir/foo work index &&
62 # the third n is to get out in case it mistakenly does not apply
63 test_write_lines n y n | git restore -p --source=HEAD^ &&
64 verify_saved_state bar &&
65 verify_state dir/foo parent index
66'
67
Ghanshyam Thakkar7abc1862024-02-13 05:35:30 +053068test_expect_success 'git restore -p --source=HEAD^...' '
Denton Liu5602b502020-10-07 00:56:15 -070069 set_state dir/foo work index &&
70 # the third n is to get out in case it mistakenly does not apply
71 test_write_lines n y n | git restore -p --source=HEAD^... &&
72 verify_saved_state bar &&
73 verify_state dir/foo parent index
74'
75
Ghanshyam Thakkar7abc1862024-02-13 05:35:30 +053076test_expect_success 'git restore -p handles deletion' '
Nguyễn Thái Ngọc Duy4df3ec62019-04-25 16:45:55 +070077 set_state dir/foo work index &&
78 rm dir/foo &&
79 test_write_lines n y | git restore -p &&
80 verify_saved_state bar &&
81 verify_state dir/foo index index
82'
83
84# 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
Ghanshyam Thakkar7abc1862024-02-13 05:35:30 +053089test_expect_success 'path limiting works: dir' '
Nguyễn Thái Ngọc Duy4df3ec62019-04-25 16:45:55 +070090 set_state dir/foo work head &&
91 test_write_lines y n | git restore -p dir &&
92 verify_saved_state bar &&
93 verify_state dir/foo head head
94'
95
Ghanshyam Thakkar7abc1862024-02-13 05:35:30 +053096test_expect_success 'path limiting works: -- dir' '
Nguyễn Thái Ngọc Duy4df3ec62019-04-25 16:45:55 +070097 set_state dir/foo work head &&
98 test_write_lines y n | git restore -p -- dir &&
99 verify_saved_state bar &&
100 verify_state dir/foo head head
101'
102
Ghanshyam Thakkar7abc1862024-02-13 05:35:30 +0530103test_expect_success 'path limiting works: HEAD^ -- dir' '
Nguyễn Thái Ngọc Duy4df3ec62019-04-25 16:45:55 +0700104 set_state dir/foo work head &&
105 # the third n is to get out in case it mistakenly does not apply
106 test_write_lines y n n | git restore -p --source=HEAD^ -- dir &&
107 verify_saved_state bar &&
108 verify_state dir/foo parent head
109'
110
Ghanshyam Thakkar7abc1862024-02-13 05:35:30 +0530111test_expect_success 'path limiting works: foo inside dir' '
Nguyễn Thái Ngọc Duy4df3ec62019-04-25 16:45:55 +0700112 set_state dir/foo work head &&
113 # the third n is to get out in case it mistakenly does not apply
114 test_write_lines y n n | (cd dir && git restore -p foo) &&
115 verify_saved_state bar &&
116 verify_state dir/foo head head
117'
118
Ghanshyam Thakkar7abc1862024-02-13 05:35:30 +0530119test_expect_success 'none of this moved HEAD' '
Nguyễn Thái Ngọc Duy4df3ec62019-04-25 16:45:55 +0700120 verify_saved_head
121'
122
123test_done