projects/guix·blob core-packages-team · refs

383 lines · 15.8 KiB

1;;; GNU Guix --- Functional package management for GNU
2;;; Copyright © 2019-2020, 2022, 2024 Ludovic Courtès <[email protected]>
3;;; Copyright © 2021 Xinglu Chen <[email protected]
4;;; Copyright © 2026 Simon Tournier <[email protected]>
5;;;
6;;; This file is part of GNU Guix.
7;;;
8;;; GNU Guix is free software; you can redistribute it and/or modify it
9;;; under the terms of the GNU General Public License as published by
10;;; the Free Software Foundation; either version 3 of the License, or (at
11;;; your option) any later version.
12;;;
13;;; GNU Guix is distributed in the hope that it will be useful, but
14;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16;;; GNU General Public License for more details.
17;;;
18;;; You should have received a copy of the GNU General Public License
19;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
20
21(define-module (test-git)
22 #:use-module (git)
23 #:use-module (guix git)
24 #:use-module (guix tests git)
25 #:use-module ((guix utils) #:select (call-with-temporary-directory))
26 #:use-module (srfi srfi-1)
27 #:use-module (srfi srfi-64)
28 #:use-module (srfi srfi-71)
29 #:use-module (ice-9 popen)
30 #:use-module (ice-9 textual-ports))
31
32;; Test the (guix git) tools.
33
34(define (git . args)
35 (let* ((pipe (apply open-pipe* OPEN_READ (git-command) args))
36 (str (string-trim-right (get-string-all pipe))))
37 (close-pipe pipe)
38 str))
39
40(test-begin "git")
41
42(test-assert "commit-difference, linear history"
43 (with-temporary-git-repository directory
44 '((add "a.txt" "A")
45 (commit "first commit")
46 (add "b.txt" "B")
47 (commit "second commit")
48 (add "c.txt" "C")
49 (commit "third commit")
50 (add "d.txt" "D")
51 (commit "fourth commit"))
52 (with-repository directory repository
53 (let ((commit1 (find-commit repository "first"))
54 (commit2 (find-commit repository "second"))
55 (commit3 (find-commit repository "third"))
56 (commit4 (find-commit repository "fourth")))
57 (and (lset= eq? (commit-difference commit4 commit1)
58 (list commit2 commit3 commit4))
59 (lset= eq? (commit-difference commit4 commit2)
60 (list commit3 commit4))
61 (equal? (commit-difference commit3 commit2)
62 (list commit3))
63
64 ;; COMMIT4 is not an ancestor of COMMIT1 so we should get the
65 ;; empty list.
66 (null? (commit-difference commit1 commit4)))))))
67
68(test-assert "commit-difference, fork"
69 (with-temporary-git-repository directory
70 '((add "a.txt" "A")
71 (commit "first commit")
72 (branch "devel")
73 (checkout "devel")
74 (add "devel/1.txt" "1")
75 (commit "first devel commit")
76 (add "devel/2.txt" "2")
77 (commit "second devel commit")
78 (checkout "master")
79 (add "b.txt" "B")
80 (commit "second commit")
81 (add "c.txt" "C")
82 (commit "third commit")
83 (merge "devel" "merge")
84 (add "d.txt" "D")
85 (commit "fourth commit"))
86 (with-repository directory repository
87 (let ((master1 (find-commit repository "first commit"))
88 (master2 (find-commit repository "second commit"))
89 (master3 (find-commit repository "third commit"))
90 (master4 (find-commit repository "fourth commit"))
91 (devel1 (find-commit repository "first devel"))
92 (devel2 (find-commit repository "second devel"))
93 (merge (find-commit repository "merge")))
94 (and (equal? (commit-difference master4 merge)
95 (list master4))
96 (lset= eq? (commit-difference master3 master1)
97 (list master3 master2))
98 (lset= eq? (commit-difference devel2 master1)
99 (list devel2 devel1))
100
101 ;; The merge occurred between MASTER2 and MASTER4 so here we
102 ;; expect to see all the commits from the "devel" branch in
103 ;; addition to those on "master".
104 (lset= eq? (commit-difference master4 master2)
105 (list master4 merge master3 devel1 devel2)))))))
106
107(test-assert "commit-difference, excluded commits"
108 (with-temporary-git-repository directory
109 '((add "a.txt" "A")
110 (commit "first commit")
111 (add "b.txt" "B")
112 (commit "second commit")
113 (add "c.txt" "C")
114 (commit "third commit")
115 (add "d.txt" "D")
116 (commit "fourth commit")
117 (add "e.txt" "E")
118 (commit "fifth commit"))
119 (with-repository directory repository
120 (let ((commit1 (find-commit repository "first"))
121 (commit2 (find-commit repository "second"))
122 (commit3 (find-commit repository "third"))
123 (commit4 (find-commit repository "fourth"))
124 (commit5 (find-commit repository "fifth")))
125 (and (lset= eq? (commit-difference commit4 commit1 (list commit2))
126 (list commit3 commit4))
127 (lset= eq? (commit-difference commit4 commit1 (list commit3))
128 (list commit4))
129 (null? (commit-difference commit4 commit1 (list commit5))))))))
130
131(test-equal "commit-relation"
132 '(self ;master3 master3
133 ancestor ;master1 master3
134 descendant ;master3 master1
135 unrelated ;master2 branch1
136 unrelated ;branch1 master2
137 ancestor ;branch1 merge
138 descendant ;merge branch1
139 ancestor ;master1 merge
140 descendant) ;merge master1
141 (with-temporary-git-repository directory
142 '((add "a.txt" "A")
143 (commit "first commit")
144 (branch "hack")
145 (checkout "hack")
146 (add "1.txt" "1")
147 (commit "branch commit")
148 (checkout "master")
149 (add "b.txt" "B")
150 (commit "second commit")
151 (add "c.txt" "C")
152 (commit "third commit")
153 (merge "hack" "merge"))
154 (with-repository directory repository
155 (let ((master1 (find-commit repository "first"))
156 (master2 (find-commit repository "second"))
157 (master3 (find-commit repository "third"))
158 (branch1 (find-commit repository "branch"))
159 (merge (find-commit repository "merge")))
160 (list (commit-relation master3 master3)
161 (commit-relation master1 master3)
162 (commit-relation master3 master1)
163 (commit-relation master2 branch1)
164 (commit-relation branch1 master2)
165 (commit-relation branch1 merge)
166 (commit-relation merge branch1)
167 (commit-relation master1 merge)
168 (commit-relation merge master1))))))
169
170(test-equal "commit-descendant?"
171 '((master3 master3 => #t)
172 (master1 master3 => #f)
173 (master3 master1 => #t)
174 (master2 branch1 => #f)
175 (master2 branch1 master1 => #t)
176 (branch1 master2 => #f)
177 (branch1 merge => #f)
178 (merge branch1 => #t)
179 (master1 merge => #f)
180 (merge master1 => #t))
181 (with-temporary-git-repository directory
182 '((add "a.txt" "A")
183 (commit "first commit")
184 (branch "hack")
185 (checkout "hack")
186 (add "1.txt" "1")
187 (commit "branch commit")
188 (checkout "master")
189 (add "b.txt" "B")
190 (commit "second commit")
191 (add "c.txt" "C")
192 (commit "third commit")
193 (merge "hack" "merge"))
194 (with-repository directory repository
195 (let ((master1 (find-commit repository "first"))
196 (master2 (find-commit repository "second"))
197 (master3 (find-commit repository "third"))
198 (branch1 (find-commit repository "branch"))
199 (merge (find-commit repository "merge")))
200 (letrec-syntax ((verify
201 (syntax-rules ()
202 ((_) '())
203 ((_ (new old ...) rest ...)
204 (cons `(new old ... =>
205 ,(commit-descendant? new
206 (list old ...)))
207 (verify rest ...))))))
208 (verify (master3 master3)
209 (master1 master3)
210 (master3 master1)
211 (master2 branch1)
212 (master2 branch1 master1)
213 (branch1 master2)
214 (branch1 merge)
215 (merge branch1)
216 (master1 merge)
217 (merge master1)))))))
218
219(test-equal "remote-refs"
220 '("refs/heads/develop" "refs/heads/master"
221 "refs/tags/v1.0" "refs/tags/v1.1")
222 (with-temporary-git-repository directory
223 '((add "a.txt" "A")
224 (commit "First commit")
225 (tag "v1.0" "release-1.0")
226 (branch "develop")
227 (checkout "develop")
228 (add "b.txt" "B")
229 (commit "Second commit")
230 (tag "v1.1" "release-1.1"))
231 (remote-refs directory)))
232
233(test-equal "remote-refs: only tags"
234 '("refs/tags/v1.0" "refs/tags/v1.1")
235 (with-temporary-git-repository directory
236 '((add "a.txt" "A")
237 (commit "First commit")
238 (tag "v1.0" "Release 1.0")
239 (add "b.txt" "B")
240 (commit "Second commit")
241 (tag "v1.1" "Release 1.1"))
242 (remote-refs directory #:tags? #t)))
243
244(test-assert "update-cached-checkout, tag"
245 (call-with-temporary-directory
246 (lambda (cache)
247 (with-temporary-git-repository directory
248 '((add "a.txt" "A")
249 (commit "First commit")
250 (tag "v1.0" "release-1.0")
251 (branch "develop")
252 (checkout "develop")
253 (add "b.txt" "B")
254 (commit "Second commit")
255 (tag "v1.1" "release-1.1")
256 (checkout "master"))
257 (let* ((tag (git "-C" directory
258 "rev-parse" "v1.1^{}"))
259 (cached-directory commit relation
260 (update-cached-checkout directory
261 #:ref '(tag . "v1.1")
262 #:cache-directory cache))
263 (head (git "-C" cached-directory
264 "rev-parse" "HEAD")))
265 (and (string=? commit head)
266 (string=? tag head)))))))
267
268(test-assert "update-cached-checkout, recursive submodules follow ref"
269 (call-with-temporary-directory
270 (lambda (cache)
271 (with-temporary-git-repository sub
272 '((add "file.txt" "v1\n")
273 (commit "submodule v1")
274 (add "file.txt" "v2\n")
275 (commit "submodule v2"))
276 (let ((sub-v1 (git "-C" sub "rev-parse" "HEAD~1"))
277 (sub-v2 (git "-C" sub "rev-parse" "HEAD")))
278 (with-temporary-git-repository main
279 `((add "root.txt" "root\n")
280 (commit "root")
281 (branch "primary")
282 (checkout "primary")
283 (submodule ,sub "modules/sub")
284 (submodule-checkout "modules/sub" ,sub-v1)
285 (commit "primary uses submodule v1")
286 (branch "release")
287 (checkout "release")
288 (submodule-checkout "modules/sub" ,sub-v2)
289 (commit "release uses submodule v2")
290 (checkout "primary"))
291 (let* ((checkout1 commit1 relation1
292 (update-cached-checkout main
293 #:recursive? #t
294 #:cache-directory cache))
295 (head-cached1 (git "-C" (in-vicinity checkout1 "modules/sub")
296 "rev-parse" "HEAD"))
297 (checkout2 commit2 relation2
298 (update-cached-checkout main
299 #:recursive? #t
300 #:ref '(branch . "release")
301 #:cache-directory cache))
302 (head-cached2 (git "-C" (in-vicinity checkout2 "modules/sub")
303 "rev-parse" "HEAD")))
304 (and
305 (string=? sub-v1 head-cached1)
306 (string=? sub-v2 head-cached2)))))))))
307
308(test-assert "update-cached-checkout, untracked files removed"
309 (call-with-temporary-directory
310 (lambda (cache)
311 (with-temporary-git-repository directory
312 '((add "a.txt" "A")
313 (add ".gitignore" ".~\n")
314 (commit "First commit"))
315 (let ((directory commit relation
316 (update-cached-checkout directory
317 #:ref '()
318 #:cache-directory cache)))
319 (close-port
320 (open-output-file (in-vicinity cache "stale-untracked-file")))
321 (let ((directory2 commit2 relation2
322 (update-cached-checkout directory
323 #:ref '()
324 #:cache-directory cache)))
325 (not (file-exists?
326 (in-vicinity cache "stale-untracked-file")))))))))
327
328(test-assert "update-cached-checkout, symref tag"
329 (call-with-temporary-directory
330 (lambda (cache)
331 (with-temporary-git-repository directory
332 '((add "a.txt" "A")
333 (commit "First commit")
334 (tag "v1.0" "release-1.0")
335 (branch "develop")
336 (checkout "develop")
337 (add "b.txt" "B")
338 (commit "Second commit")
339 (tag "v1.1" "release-1.1")
340 (checkout "master"))
341 (let* ((tag-directory (git "-C" directory
342 "rev-parse" "v1.1"))
343 (cached-directory commit relation
344 (update-cached-checkout directory
345 #:ref '(symref . "refs/tags/v1.1")
346 #:cache-directory cache))
347 (head-cached (git "-C" cached-directory
348 ;; switch-to-ref doesn't dereference the Git object,
349 ;; thus it points to a Git tag object.
350 ;; And HEAD points to a Git commit object.
351 ;; Hence, the check is against Git tag objects.
352 ;; For the difference see:
353 ;; git show-ref --dereference v1.1
354 "rev-parse" "v1.1")))
355 (and (string=? commit head-cached)
356 (string=? tag-directory head-cached)))))))
357
358(test-assert "update-cached-checkout, symref pull-request"
359 (call-with-temporary-directory
360 (lambda (cache)
361 (with-temporary-git-repository directory
362 '((add "a.txt" "A")
363 (commit "First commit")
364 (tag "v1.0" "release-1.0")
365 (branch "develop")
366 (checkout "develop")
367 (add "b.txt" "B")
368 (commit "Second commit")
369 (tag "v1.1" "release-1.1")
370 (symbolic-ref "refs/pull/1/head" "refs/heads/develop")
371 (checkout "master"))
372 (let* ((head-directory (git "-C" directory
373 "rev-parse" "refs/pull/1/head"))
374 (cached-directory commit relation
375 (update-cached-checkout directory
376 #:ref '(symref . "refs/pull/1/head")
377 #:cache-directory cache))
378 (head-cached (git "-C" cached-directory
379 "rev-parse" "HEAD")))
380 (and (string=? commit head-directory)
381 (string=? head-cached head-directory)))))))
382
383(test-end "git")