Debian Codegraph @

t/t-update.sh

raw symbols SCIP
1 #!/usr/bin/env bash
2
3 . "$(dirname "$0")/testlib.sh"
4
5 begin_test "update"
6 (
7 set -e
8
9 pre_push_hook="#!/bin/sh
10 command -v git-lfs >/dev/null 2>&1 || { printf >&2 \"\\n%s\\n\\n\" \"This repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting the 'pre-push' file in the hooks directory (set by 'core.hookspath'; usually '.git/hooks').\"; exit 2; }
11 git lfs pre-push \"\$@\""
12
13 post_checkout_hook="#!/bin/sh
14 command -v git-lfs >/dev/null 2>&1 || { printf >&2 \"\\n%s\\n\\n\" \"This repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting the 'post-checkout' file in the hooks directory (set by 'core.hookspath'; usually '.git/hooks').\"; exit 2; }
15 git lfs post-checkout \"\$@\""
16
17 post_commit_hook="#!/bin/sh
18 command -v git-lfs >/dev/null 2>&1 || { printf >&2 \"\\n%s\\n\\n\" \"This repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting the 'post-commit' file in the hooks directory (set by 'core.hookspath'; usually '.git/hooks').\"; exit 2; }
19 git lfs post-commit \"\$@\""
20
21 post_merge_hook="#!/bin/sh
22 command -v git-lfs >/dev/null 2>&1 || { printf >&2 \"\\n%s\\n\\n\" \"This repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting the 'post-merge' file in the hooks directory (set by 'core.hookspath'; usually '.git/hooks').\"; exit 2; }
23 git lfs post-merge \"\$@\""
24
25 mkdir without-pre-push
26 cd without-pre-push
27 git init
28
29 [ "Updated Git hooks." = "$(git lfs update)" ]
30 [ "$pre_push_hook" = "$(cat .git/hooks/pre-push)" ]
31 [ "$post_checkout_hook" = "$(cat .git/hooks/post-checkout)" ]
32 [ "$post_commit_hook" = "$(cat .git/hooks/post-commit)" ]
33 [ "$post_merge_hook" = "$(cat .git/hooks/post-merge)" ]
34
35 # run it again
36 [ "Updated Git hooks." = "$(git lfs update)" ]
37 [ "$pre_push_hook" = "$(cat .git/hooks/pre-push)" ]
38 [ "$post_checkout_hook" = "$(cat .git/hooks/post-checkout)" ]
39 [ "$post_commit_hook" = "$(cat .git/hooks/post-commit)" ]
40 [ "$post_merge_hook" = "$(cat .git/hooks/post-merge)" ]
41
42 # replace old hook 1
43 echo "#!/bin/sh
44 git lfs push --stdin \$*" > .git/hooks/pre-push
45 [ "Updated Git hooks." = "$(git lfs update)" ]
46 [ "$pre_push_hook" = "$(cat .git/hooks/pre-push)" ]
47
48 # replace old hook 2
49 echo "#!/bin/sh
50 git lfs push --stdin \"\$@\"" > .git/hooks/pre-push
51 [ "Updated Git hooks." = "$(git lfs update)" ]
52 [ "$pre_push_hook" = "$(cat .git/hooks/pre-push)" ]
53
54 # replace old hook 3
55 echo "#!/bin/sh
56 git lfs pre-push \"\$@\"" > .git/hooks/pre-push
57 [ "Updated Git hooks." = "$(git lfs update)" ]
58 [ "$pre_push_hook" = "$(cat .git/hooks/pre-push)" ]
59
60 # replace blank hook
61 rm .git/hooks/pre-push
62 touch .git/hooks/pre-push
63 touch .git/hooks/post-checkout
64 touch .git/hooks/post-merge
65 [ "Updated Git hooks." = "$(git lfs update)" ]
66 [ "$pre_push_hook" = "$(cat .git/hooks/pre-push)" ]
67 [ "$post_checkout_hook" = "$(cat .git/hooks/post-checkout)" ]
68 [ "$post_commit_hook" = "$(cat .git/hooks/post-commit)" ]
69 [ "$post_merge_hook" = "$(cat .git/hooks/post-merge)" ]
70
71 # replace old hook 4
72 echo "#!/bin/sh
73 command -v git-lfs >/dev/null 2>&1 || { echo >&2 \"\\nThis repository has been set up with Git LFS but Git LFS is not installed.\\n\"; exit 0; }
74 git lfs pre-push \"\$@\"" > .git/hooks/pre-push
75 [ "Updated Git hooks." = "$(git lfs update)" ]
76 [ "$pre_push_hook" = "$(cat .git/hooks/pre-push)" ]
77
78 # replace old hook 5
79 echo "#!/bin/sh
80 command -v git-lfs >/dev/null 2>&1 || { echo >&2 \"\\nThis repository has been set up with Git LFS but Git LFS is not installed.\\n\"; exit 2; }
81 git lfs pre-push \"\$@\"" > .git/hooks/pre-push
82 [ "Updated Git hooks." = "$(git lfs update)" ]
83 [ "$pre_push_hook" = "$(cat .git/hooks/pre-push)" ]
84
85 # replace old hook 6
86 echo "#!/bin/sh
87 command -v git-lfs >/dev/null 2>&1 || { echo >&2 \"\\nThis repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting '.git/hooks/pre-push'.\\n\"; exit 2; }
88 git lfs pre-push \"\$@\"" > .git/hooks/pre-push
89 [ "Updated Git hooks." = "$(git lfs update)" ]
90 [ "$pre_push_hook" = "$(cat .git/hooks/pre-push)" ]
91
92 # replace old hook 7
93 echo "#!/bin/sh
94 command -v git-lfs >/dev/null 2>&1 || { echo >&2 \"\\nThis repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting the 'pre-push' file in the hooks directory (set by 'core.hookspath'; usually '.git/hooks').\\n\"; exit 2; }
95 git lfs pre-push \"\$@\"" > .git/hooks/pre-push
96 [ "Updated Git hooks." = "$(git lfs update)" ]
97 [ "$pre_push_hook" = "$(cat .git/hooks/pre-push)" ]
98
99 # don't replace unexpected hook
100 echo "test" > .git/hooks/pre-push
101 echo "test" > .git/hooks/post-checkout
102 echo "test" > .git/hooks/post-commit
103 echo "test" > .git/hooks/post-merge
104 expected="Hook already exists: pre-push
105
106 test
107
108 To resolve this, either:
109 1: run \`git lfs update --manual\` for instructions on how to merge hooks.
110 2: run \`git lfs update --force\` to overwrite your hook."
111
112 [ "$expected" = "$(git lfs update 2>&1)" ]
113 [ "test" = "$(cat .git/hooks/pre-push)" ]
114 [ "test" = "$(cat .git/hooks/post-checkout)" ]
115 [ "test" = "$(cat .git/hooks/post-commit)" ]
116 [ "test" = "$(cat .git/hooks/post-merge)" ]
117
118 # Make sure returns non-zero
119 set +e
120 git lfs update
121 if [ $? -eq 0 ]
122 then
123 exit 1
124 fi
125 set -e
126
127 # test manual steps
128 expected="Add the following to '.git/hooks/pre-push':
129
130 #!/bin/sh
131 command -v git-lfs >/dev/null 2>&1 || { printf >&2 \"\n%s\n\n\" \"This repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting the 'pre-push' file in the hooks directory (set by 'core.hookspath'; usually '.git/hooks').\"; exit 2; }
132 git lfs pre-push \"\$@\"
133
134 Add the following to '.git/hooks/post-checkout':
135
136 #!/bin/sh
137 command -v git-lfs >/dev/null 2>&1 || { printf >&2 \"\n%s\n\n\" \"This repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting the 'post-checkout' file in the hooks directory (set by 'core.hookspath'; usually '.git/hooks').\"; exit 2; }
138 git lfs post-checkout \"\$@\"
139
140 Add the following to '.git/hooks/post-commit':
141
142 #!/bin/sh
143 command -v git-lfs >/dev/null 2>&1 || { printf >&2 \"\n%s\n\n\" \"This repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting the 'post-commit' file in the hooks directory (set by 'core.hookspath'; usually '.git/hooks').\"; exit 2; }
144 git lfs post-commit \"\$@\"
145
146 Add the following to '.git/hooks/post-merge':
147
148 #!/bin/sh
149 command -v git-lfs >/dev/null 2>&1 || { printf >&2 \"\n%s\n\n\" \"This repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting the 'post-merge' file in the hooks directory (set by 'core.hookspath'; usually '.git/hooks').\"; exit 2; }
150 git lfs post-merge \"\$@\""
151
152 [ "$expected" = "$(git lfs update --manual 2>&1)" ]
153 [ "test" = "$(cat .git/hooks/pre-push)" ]
154 [ "test" = "$(cat .git/hooks/post-checkout)" ]
155 [ "test" = "$(cat .git/hooks/post-commit)" ]
156 [ "test" = "$(cat .git/hooks/post-merge)" ]
157
158 # force replace unexpected hook
159 [ "Updated Git hooks." = "$(git lfs update --force)" ]
160 [ "$pre_push_hook" = "$(cat .git/hooks/pre-push)" ]
161 [ "$post_checkout_hook" = "$(cat .git/hooks/post-checkout)" ]
162 [ "$post_commit_hook" = "$(cat .git/hooks/post-commit)" ]
163 [ "$post_merge_hook" = "$(cat .git/hooks/post-merge)" ]
164
165 # test manual steps with core.hookspath
166 gitversion=$(git version | cut -d" " -f3)
167 set +e
168 compare_version "$gitversion" 2.9.0
169 result=$?
170 set -e
171 if [ "$result" -ne "$VERSION_LOWER" ]
172 then
173 mkdir hooks
174 rm -fr .git/hooks
175 git config core.hookspath hooks
176 echo "test" > hooks/pre-push
177 echo "test" > hooks/post-checkout
178 echo "test" > hooks/post-commit
179 echo "test" > hooks/post-merge
180
181 expected="Add the following to 'hooks/pre-push':
182
183 #!/bin/sh
184 command -v git-lfs >/dev/null 2>&1 || { printf >&2 \"\n%s\n\n\" \"This repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting the 'pre-push' file in the hooks directory (set by 'core.hookspath'; usually '.git/hooks').\"; exit 2; }
185 git lfs pre-push \"\$@\"
186
187 Add the following to 'hooks/post-checkout':
188
189 #!/bin/sh
190 command -v git-lfs >/dev/null 2>&1 || { printf >&2 \"\n%s\n\n\" \"This repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting the 'post-checkout' file in the hooks directory (set by 'core.hookspath'; usually '.git/hooks').\"; exit 2; }
191 git lfs post-checkout \"\$@\"
192
193 Add the following to 'hooks/post-commit':
194
195 #!/bin/sh
196 command -v git-lfs >/dev/null 2>&1 || { printf >&2 \"\n%s\n\n\" \"This repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting the 'post-commit' file in the hooks directory (set by 'core.hookspath'; usually '.git/hooks').\"; exit 2; }
197 git lfs post-commit \"\$@\"
198
199 Add the following to 'hooks/post-merge':
200
201 #!/bin/sh
202 command -v git-lfs >/dev/null 2>&1 || { printf >&2 \"\n%s\n\n\" \"This repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting the 'post-merge' file in the hooks directory (set by 'core.hookspath'; usually '.git/hooks').\"; exit 2; }
203 git lfs post-merge \"\$@\""
204 [ "$expected" = "$(git lfs update --manual 2>&1)" ]
205 [ "test" = "$(cat hooks/pre-push)" ]
206 [ "test" = "$(cat hooks/post-checkout)" ]
207 [ "test" = "$(cat hooks/post-commit)" ]
208 [ "test" = "$(cat hooks/post-merge)" ]
209
210 # force replace unexpected hook
211 [ "Updated Git hooks." = "$(git lfs update --force)" ]
212 [ "$pre_push_hook" = "$(cat hooks/pre-push)" ]
213 [ "$post_checkout_hook" = "$(cat hooks/post-checkout)" ]
214 [ "$post_commit_hook" = "$(cat hooks/post-commit)" ]
215 [ "$post_merge_hook" = "$(cat hooks/post-merge)" ]
216
217 test -d .git/hooks && exit 1
218 fi
219
220 has_test_dir || exit 0
221
222 echo "test with bare repository"
223 cd ..
224 git clone --mirror without-pre-push bare
225 cd bare
226 git lfs env
227 git lfs update
228 ls -al hooks
229 [ "$pre_push_hook" = "$(cat hooks/pre-push)" ]
230 )
231 end_test
232
233 begin_test "update with leading spaces"
234 (
235 set -e
236
237 reponame="update-leading-spaces"
238 git init "$reponame"
239 cd "$reponame"
240
241 [ "Updated Git hooks." = "$(git lfs update)" ]
242
243 # $pre_push_hook contains leading TAB '\t' characters
244 pre_push_hook="#!/bin/sh
245 command -v git-lfs >/dev/null 2>&1 || { echo >&2 \"\\nThis repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting '.git/hooks/pre-push'.\\n\"; exit 2; }
246 git lfs pre-push \"\$@\""
247
248 echo -n "$pre_push_hook" > .git/hooks/pre-push
249
250 [ "Updated Git hooks." = "$(git lfs update)" ]
251 )
252 end_test
253
254 begin_test "update lfs.{url}.access"
255 (
256 set -e
257
258 mkdir update-access
259 cd update-access
260 git init
261 git config lfs.http://example.com.access private
262 git config lfs.https://example.com.access private
263 git config lfs.https://example2.com.access basic
264 git config lfs.https://example3.com.access other
265
266 [ "private" = "$(git config lfs.http://example.com.access)" ]
267 [ "private" = "$(git config lfs.https://example.com.access)" ]
268 [ "basic" = "$(git config lfs.https://example2.com.access)" ]
269 [ "other" = "$(git config lfs.https://example3.com.access)" ]
270
271 expected="Updated Git hooks.
272 Updated http://example.com access from private to basic.
273 Updated https://example.com access from private to basic.
274 Removed invalid https://example3.com access of other."
275 )
276 end_test
277
278 begin_test "update: outside git repository"
279 (
280 if [ -d "hooks" ]; then
281 ls -al
282 echo "hooks dir exists"
283 exit 1
284 fi
285
286 set +e
287 git lfs update 2>&1 > check.log
288 res=$?
289 set -e
290
291 if [ "$res" = "0" ]; then
292 if [ -z "$GIT_LFS_TEST_DIR" ]; then
293 echo "Passes because $GIT_LFS_TEST_DIR is unset."
294 exit 0
295 fi
296 fi
297
298 [ "$res" = "128" ]
299
300 if [ -d "hooks" ]; then
301 ls -al
302 echo "hooks dir exists"
303 exit 1
304 fi
305
306 cat check.log
307 grep "Not in a Git repository" check.log
308 )
309 end_test