The second batch
[git] / t / t3705-add-sparse-checkout.sh
1 #!/bin/sh
2
3 test_description='git add in sparse checked out working trees'
4
5 . ./test-lib.sh
6
7 SPARSE_ENTRY_BLOB=""
8
9 # Optionally take a printf format string to write to the sparse_entry file
10 setup_sparse_entry () {
11         # 'sparse_entry' might already be in the index with the skip-worktree
12         # bit set. Remove it so that the subsequent git add can update it.
13         git update-index --force-remove sparse_entry &&
14         if test $# -eq 1
15         then
16                 printf "$1" >sparse_entry
17         else
18                 >sparse_entry
19         fi &&
20         git add sparse_entry &&
21         git update-index --skip-worktree sparse_entry &&
22         SPARSE_ENTRY_BLOB=$(git rev-parse :sparse_entry)
23 }
24
25 test_sparse_entry_unchanged () {
26         echo "100644 $SPARSE_ENTRY_BLOB 0       sparse_entry" >expected &&
27         git ls-files --stage sparse_entry >actual &&
28         test_cmp expected actual
29 }
30
31 setup_gitignore () {
32         test_when_finished rm -f .gitignore &&
33         cat >.gitignore <<-EOF
34         *
35         !/sparse_entry
36         EOF
37 }
38
39 test_expect_success 'setup' "
40         cat >sparse_error_header <<-EOF &&
41         The following pathspecs didn't match any eligible path, but they do match index
42         entries outside the current sparse checkout:
43         EOF
44
45         cat >sparse_hint <<-EOF &&
46         hint: Disable or modify the sparsity rules if you intend to update such entries.
47         hint: Disable this message with \"git config advice.updateSparsePath false\"
48         EOF
49
50         echo sparse_entry | cat sparse_error_header - >sparse_entry_error &&
51         cat sparse_entry_error sparse_hint >error_and_hint
52 "
53
54 test_expect_success 'git add does not remove sparse entries' '
55         setup_sparse_entry &&
56         rm sparse_entry &&
57         test_must_fail git add sparse_entry 2>stderr &&
58         test_cmp error_and_hint stderr &&
59         test_sparse_entry_unchanged
60 '
61
62 test_expect_success 'git add -A does not remove sparse entries' '
63         setup_sparse_entry &&
64         rm sparse_entry &&
65         setup_gitignore &&
66         git add -A 2>stderr &&
67         test_must_be_empty stderr &&
68         test_sparse_entry_unchanged
69 '
70
71 test_expect_success 'git add . does not remove sparse entries' '
72         setup_sparse_entry &&
73         rm sparse_entry &&
74         setup_gitignore &&
75         test_must_fail git add . 2>stderr &&
76
77         cat sparse_error_header >expect &&
78         echo . >>expect &&
79         cat sparse_hint >>expect &&
80
81         test_cmp expect stderr &&
82         test_sparse_entry_unchanged
83 '
84
85 for opt in "" -f -u --ignore-removal --dry-run
86 do
87         test_expect_success "git add${opt:+ $opt} does not update sparse entries" '
88                 setup_sparse_entry &&
89                 echo modified >sparse_entry &&
90                 test_must_fail git add $opt sparse_entry 2>stderr &&
91                 test_cmp error_and_hint stderr &&
92                 test_sparse_entry_unchanged
93         '
94 done
95
96 test_expect_success 'git add --refresh does not update sparse entries' '
97         setup_sparse_entry &&
98         git ls-files --debug sparse_entry | grep mtime >before &&
99         test-tool chmtime -60 sparse_entry &&
100         test_must_fail git add --refresh sparse_entry 2>stderr &&
101         test_cmp error_and_hint stderr &&
102         git ls-files --debug sparse_entry | grep mtime >after &&
103         test_cmp before after
104 '
105
106 test_expect_success 'git add --chmod does not update sparse entries' '
107         setup_sparse_entry &&
108         test_must_fail git add --chmod=+x sparse_entry 2>stderr &&
109         test_cmp error_and_hint stderr &&
110         test_sparse_entry_unchanged &&
111         ! test -x sparse_entry
112 '
113
114 test_expect_success 'git add --renormalize does not update sparse entries' '
115         test_config core.autocrlf false &&
116         setup_sparse_entry "LINEONE\r\nLINETWO\r\n" &&
117         echo "sparse_entry text=auto" >.gitattributes &&
118         test_must_fail git add --renormalize sparse_entry 2>stderr &&
119         test_cmp error_and_hint stderr &&
120         test_sparse_entry_unchanged
121 '
122
123 test_expect_success 'git add --dry-run --ignore-missing warn on sparse path' '
124         setup_sparse_entry &&
125         rm sparse_entry &&
126         test_must_fail git add --dry-run --ignore-missing sparse_entry 2>stderr &&
127         test_cmp error_and_hint stderr &&
128         test_sparse_entry_unchanged
129 '
130
131 test_expect_success 'do not advice about sparse entries when they do not match the pathspec' '
132         setup_sparse_entry &&
133         test_must_fail git add nonexistent 2>stderr &&
134         grep "fatal: pathspec .nonexistent. did not match any files" stderr &&
135         ! grep -F -f sparse_error_header stderr
136 '
137
138 test_expect_success 'do not warn when pathspec matches dense entries' '
139         setup_sparse_entry &&
140         echo modified >sparse_entry &&
141         >dense_entry &&
142         git add "*_entry" 2>stderr &&
143         test_must_be_empty stderr &&
144         test_sparse_entry_unchanged &&
145         git ls-files --error-unmatch dense_entry
146 '
147
148 test_expect_success 'add obeys advice.updateSparsePath' '
149         setup_sparse_entry &&
150         test_must_fail git -c advice.updateSparsePath=false add sparse_entry 2>stderr &&
151         test_cmp sparse_entry_error stderr
152
153 '
154
155 test_done