summaryrefslogtreecommitdiff
path: root/t/t2500-untracked-overwriting.sh
blob: 5c0bf4d21fcbb2a55e2c41c184ccb4c188c88ca4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/bin/sh

test_description='Test handling of overwriting untracked files'

. ./test-lib.sh

test_setup_reset () {
	git init reset_$1 &&
	(
		cd reset_$1 &&
		test_commit init &&

		git branch stable &&
		git branch work &&

		git checkout work &&
		test_commit foo &&

		git checkout stable
	)
}

test_expect_success 'reset --hard will nuke untracked files/dirs' '
	test_setup_reset hard &&
	(
		cd reset_hard &&
		git ls-tree -r stable &&
		git log --all --name-status --oneline &&
		git ls-tree -r work &&

		mkdir foo.t &&
		echo precious >foo.t/file &&
		echo foo >expect &&

		git reset --hard work &&

		# check that untracked directory foo.t/ was nuked
		test_path_is_file foo.t &&
		test_cmp expect foo.t
	)
'

test_expect_success 'reset --merge will preserve untracked files/dirs' '
	test_setup_reset merge &&
	(
		cd reset_merge &&

		mkdir foo.t &&
		echo precious >foo.t/file &&
		cp foo.t/file expect &&

		test_must_fail git reset --merge work 2>error &&
		test_cmp expect foo.t/file &&
		grep "Updating .foo.t. would lose untracked files" error
	)
'

test_expect_success 'reset --keep will preserve untracked files/dirs' '
	test_setup_reset keep &&
	(
		cd reset_keep &&

		mkdir foo.t &&
		echo precious >foo.t/file &&
		cp foo.t/file expect &&

		test_must_fail git reset --merge work 2>error &&
		test_cmp expect foo.t/file &&
		grep "Updating.*foo.t.*would lose untracked files" error
	)
'

test_setup_checkout_m () {
	git init checkout &&
	(
		cd checkout &&
		test_commit init &&

		test_write_lines file has some >filler &&
		git add filler &&
		git commit -m filler &&

		git branch stable &&

		git switch -c work &&
		echo stuff >notes.txt &&
		test_write_lines file has some words >filler &&
		git add notes.txt filler &&
		git commit -m filler &&

		git checkout stable
	)
}

test_expect_success 'checkout -m does not nuke untracked file' '
	test_setup_checkout_m &&
	(
		cd checkout &&

		# Tweak filler
		test_write_lines this file has some >filler &&
		# Make an untracked file, save its contents in "expect"
		echo precious >notes.txt &&
		cp notes.txt expect &&

		test_must_fail git checkout -m work &&
		test_cmp expect notes.txt
	)
'

test_setup_sequencing () {
	git init sequencing_$1 &&
	(
		cd sequencing_$1 &&
		test_commit init &&

		test_write_lines this file has some words >filler &&
		git add filler &&
		git commit -m filler &&

		mkdir -p foo/bar &&
		test_commit foo/bar/baz &&

		git branch simple &&
		git branch fooey &&

		git checkout fooey &&
		git rm foo/bar/baz.t &&
		echo stuff >>filler &&
		git add -u &&
		git commit -m "changes" &&

		git checkout simple &&
		echo items >>filler &&
		echo newstuff >>newfile &&
		git add filler newfile &&
		git commit -m another
	)
}

test_expect_success 'git rebase --abort and untracked files' '
	test_setup_sequencing rebase_abort_and_untracked &&
	(
		cd sequencing_rebase_abort_and_untracked &&
		git checkout fooey &&
		test_must_fail git rebase simple &&

		cat init.t &&
		git rm init.t &&
		echo precious >init.t &&
		cp init.t expect &&
		git status --porcelain &&
		test_must_fail git rebase --abort &&
		test_cmp expect init.t
	)
'

test_expect_success 'git rebase fast forwarding and untracked files' '
	test_setup_sequencing rebase_fast_forward_and_untracked &&
	(
		cd sequencing_rebase_fast_forward_and_untracked &&
		git checkout init &&
		echo precious >filler &&
		cp filler expect &&
		test_must_fail git rebase init simple &&
		test_cmp expect filler
	)
'

test_expect_failure 'git rebase --autostash and untracked files' '
	test_setup_sequencing rebase_autostash_and_untracked &&
	(
		cd sequencing_rebase_autostash_and_untracked &&
		git checkout simple &&
		git rm filler &&
		mkdir filler &&
		echo precious >filler/file &&
		cp filler/file expect &&
		git rebase --autostash init &&
		test_path_is_file filler/file
	)
'

test_expect_failure 'git stash and untracked files' '
	test_setup_sequencing stash_and_untracked_files &&
	(
		cd sequencing_stash_and_untracked_files &&
		git checkout simple &&
		git rm filler &&
		mkdir filler &&
		echo precious >filler/file &&
		cp filler/file expect &&
		git status --porcelain &&
		git stash push &&
		git status --porcelain &&
		test_path_is_file filler/file
	)
'

test_expect_success 'git am --abort and untracked dir vs. unmerged file' '
	test_setup_sequencing am_abort_and_untracked &&
	(
		cd sequencing_am_abort_and_untracked &&
		git format-patch -1 --stdout fooey >changes.mbox &&
		test_must_fail git am --3way changes.mbox &&

		# Delete the conflicted file; we will stage and commit it later
		rm filler &&

		# Put an unrelated untracked directory there
		mkdir filler &&
		echo foo >filler/file1 &&
		echo bar >filler/file2 &&

		test_must_fail git am --abort 2>errors &&
		test_path_is_dir filler &&
		grep "Updating .filler. would lose untracked files in it" errors
	)
'

test_expect_success 'git am --skip and untracked dir vs deleted file' '
	test_setup_sequencing am_skip_and_untracked &&
	(
		cd sequencing_am_skip_and_untracked &&
		git checkout fooey &&
		git format-patch -1 --stdout simple >changes.mbox &&
		test_must_fail git am --3way changes.mbox &&

		# Delete newfile
		rm newfile &&

		# Put an unrelated untracked directory there
		mkdir newfile &&
		echo foo >newfile/file1 &&
		echo bar >newfile/file2 &&

		# Change our mind about resolutions, just skip this patch
		test_must_fail git am --skip 2>errors &&
		test_path_is_dir newfile &&
		grep "Updating .newfile. would lose untracked files in it" errors
	)
'

test_done