projects/guix·blob next-master · refs

282 lines · 11.2 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;;;
5;;; This file is part of GNU Guix.
6;;;
7;;; GNU Guix is free software; you can redistribute it and/or modify it
8;;; under the terms of the GNU General Public License as published by
9;;; the Free Software Foundation; either version 3 of the License, or (at
10;;; your option) any later version.
11;;;
12;;; GNU Guix is distributed in the hope that it will be useful, but
13;;; WITHOUT ANY WARRANTY; without even the implied warranty of
14;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15;;; GNU General Public License for more details.
16;;;
17;;; You should have received a copy of the GNU General Public License
18;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19
20(define-module (test-git)
21 #:use-module (git)
22 #:use-module (guix git)
23 #:use-module (guix tests git)
24 #:use-module ((guix utils) #:select (call-with-temporary-directory))
25 #:use-module (srfi srfi-1)
26 #:use-module (srfi srfi-64)
27 #:use-module (srfi srfi-71)
28 #:use-module (ice-9 popen)
29 #:use-module (ice-9 textual-ports))
30
31;; Test the (guix git) tools.
32
33(test-begin "git")
34
35(test-assert "commit-difference, linear history"
36 (with-temporary-git-repository directory
37 '((add "a.txt" "A")
38 (commit "first commit")
39 (add "b.txt" "B")
40 (commit "second commit")
41 (add "c.txt" "C")
42 (commit "third commit")
43 (add "d.txt" "D")
44 (commit "fourth commit"))
45 (with-repository directory repository
46 (let ((commit1 (find-commit repository "first"))
47 (commit2 (find-commit repository "second"))
48 (commit3 (find-commit repository "third"))
49 (commit4 (find-commit repository "fourth")))
50 (and (lset= eq? (commit-difference commit4 commit1)
51 (list commit2 commit3 commit4))
52 (lset= eq? (commit-difference commit4 commit2)
53 (list commit3 commit4))
54 (equal? (commit-difference commit3 commit2)
55 (list commit3))
56
57 ;; COMMIT4 is not an ancestor of COMMIT1 so we should get the
58 ;; empty list.
59 (null? (commit-difference commit1 commit4)))))))
60
61(test-assert "commit-difference, fork"
62 (with-temporary-git-repository directory
63 '((add "a.txt" "A")
64 (commit "first commit")
65 (branch "devel")
66 (checkout "devel")
67 (add "devel/1.txt" "1")
68 (commit "first devel commit")
69 (add "devel/2.txt" "2")
70 (commit "second devel commit")
71 (checkout "master")
72 (add "b.txt" "B")
73 (commit "second commit")
74 (add "c.txt" "C")
75 (commit "third commit")
76 (merge "devel" "merge")
77 (add "d.txt" "D")
78 (commit "fourth commit"))
79 (with-repository directory repository
80 (let ((master1 (find-commit repository "first commit"))
81 (master2 (find-commit repository "second commit"))
82 (master3 (find-commit repository "third commit"))
83 (master4 (find-commit repository "fourth commit"))
84 (devel1 (find-commit repository "first devel"))
85 (devel2 (find-commit repository "second devel"))
86 (merge (find-commit repository "merge")))
87 (and (equal? (commit-difference master4 merge)
88 (list master4))
89 (lset= eq? (commit-difference master3 master1)
90 (list master3 master2))
91 (lset= eq? (commit-difference devel2 master1)
92 (list devel2 devel1))
93
94 ;; The merge occurred between MASTER2 and MASTER4 so here we
95 ;; expect to see all the commits from the "devel" branch in
96 ;; addition to those on "master".
97 (lset= eq? (commit-difference master4 master2)
98 (list master4 merge master3 devel1 devel2)))))))
99
100(test-assert "commit-difference, excluded commits"
101 (with-temporary-git-repository directory
102 '((add "a.txt" "A")
103 (commit "first commit")
104 (add "b.txt" "B")
105 (commit "second commit")
106 (add "c.txt" "C")
107 (commit "third commit")
108 (add "d.txt" "D")
109 (commit "fourth commit")
110 (add "e.txt" "E")
111 (commit "fifth commit"))
112 (with-repository directory repository
113 (let ((commit1 (find-commit repository "first"))
114 (commit2 (find-commit repository "second"))
115 (commit3 (find-commit repository "third"))
116 (commit4 (find-commit repository "fourth"))
117 (commit5 (find-commit repository "fifth")))
118 (and (lset= eq? (commit-difference commit4 commit1 (list commit2))
119 (list commit3 commit4))
120 (lset= eq? (commit-difference commit4 commit1 (list commit3))
121 (list commit4))
122 (null? (commit-difference commit4 commit1 (list commit5))))))))
123
124(test-equal "commit-relation"
125 '(self ;master3 master3
126 ancestor ;master1 master3
127 descendant ;master3 master1
128 unrelated ;master2 branch1
129 unrelated ;branch1 master2
130 ancestor ;branch1 merge
131 descendant ;merge branch1
132 ancestor ;master1 merge
133 descendant) ;merge master1
134 (with-temporary-git-repository directory
135 '((add "a.txt" "A")
136 (commit "first commit")
137 (branch "hack")
138 (checkout "hack")
139 (add "1.txt" "1")
140 (commit "branch commit")
141 (checkout "master")
142 (add "b.txt" "B")
143 (commit "second commit")
144 (add "c.txt" "C")
145 (commit "third commit")
146 (merge "hack" "merge"))
147 (with-repository directory repository
148 (let ((master1 (find-commit repository "first"))
149 (master2 (find-commit repository "second"))
150 (master3 (find-commit repository "third"))
151 (branch1 (find-commit repository "branch"))
152 (merge (find-commit repository "merge")))
153 (list (commit-relation master3 master3)
154 (commit-relation master1 master3)
155 (commit-relation master3 master1)
156 (commit-relation master2 branch1)
157 (commit-relation branch1 master2)
158 (commit-relation branch1 merge)
159 (commit-relation merge branch1)
160 (commit-relation master1 merge)
161 (commit-relation merge master1))))))
162
163(test-equal "commit-descendant?"
164 '((master3 master3 => #t)
165 (master1 master3 => #f)
166 (master3 master1 => #t)
167 (master2 branch1 => #f)
168 (master2 branch1 master1 => #t)
169 (branch1 master2 => #f)
170 (branch1 merge => #f)
171 (merge branch1 => #t)
172 (master1 merge => #f)
173 (merge master1 => #t))
174 (with-temporary-git-repository directory
175 '((add "a.txt" "A")
176 (commit "first commit")
177 (branch "hack")
178 (checkout "hack")
179 (add "1.txt" "1")
180 (commit "branch commit")
181 (checkout "master")
182 (add "b.txt" "B")
183 (commit "second commit")
184 (add "c.txt" "C")
185 (commit "third commit")
186 (merge "hack" "merge"))
187 (with-repository directory repository
188 (let ((master1 (find-commit repository "first"))
189 (master2 (find-commit repository "second"))
190 (master3 (find-commit repository "third"))
191 (branch1 (find-commit repository "branch"))
192 (merge (find-commit repository "merge")))
193 (letrec-syntax ((verify
194 (syntax-rules ()
195 ((_) '())
196 ((_ (new old ...) rest ...)
197 (cons `(new old ... =>
198 ,(commit-descendant? new
199 (list old ...)))
200 (verify rest ...))))))
201 (verify (master3 master3)
202 (master1 master3)
203 (master3 master1)
204 (master2 branch1)
205 (master2 branch1 master1)
206 (branch1 master2)
207 (branch1 merge)
208 (merge branch1)
209 (master1 merge)
210 (merge master1)))))))
211
212(test-equal "remote-refs"
213 '("refs/heads/develop" "refs/heads/master"
214 "refs/tags/v1.0" "refs/tags/v1.1")
215 (with-temporary-git-repository directory
216 '((add "a.txt" "A")
217 (commit "First commit")
218 (tag "v1.0" "release-1.0")
219 (branch "develop")
220 (checkout "develop")
221 (add "b.txt" "B")
222 (commit "Second commit")
223 (tag "v1.1" "release-1.1"))
224 (remote-refs directory)))
225
226(test-equal "remote-refs: only tags"
227 '("refs/tags/v1.0" "refs/tags/v1.1")
228 (with-temporary-git-repository directory
229 '((add "a.txt" "A")
230 (commit "First commit")
231 (tag "v1.0" "Release 1.0")
232 (add "b.txt" "B")
233 (commit "Second commit")
234 (tag "v1.1" "Release 1.1"))
235 (remote-refs directory #:tags? #t)))
236
237(test-assert "update-cached-checkout, tag"
238 (call-with-temporary-directory
239 (lambda (cache)
240 (with-temporary-git-repository directory
241 '((add "a.txt" "A")
242 (commit "First commit")
243 (tag "v1.0" "release-1.0")
244 (branch "develop")
245 (checkout "develop")
246 (add "b.txt" "B")
247 (commit "Second commit")
248 (tag "v1.1" "release-1.1"))
249 (let ((directory commit relation
250 (update-cached-checkout directory
251 #:ref '(tag . "v1.1")
252 #:cache-directory cache))
253 (head (let* ((pipe (open-pipe* OPEN_READ (git-command)
254 "-C" directory
255 "rev-parse" "HEAD"))
256 (str (get-string-all pipe)))
257 (close-pipe pipe)
258 (string-trim-right str))))
259 ;; COMMIT should be the ID of the commit object, not that of the tag.
260 (string=? commit head))))))
261
262(test-assert "update-cached-checkout, untracked files removed"
263 (call-with-temporary-directory
264 (lambda (cache)
265 (with-temporary-git-repository directory
266 '((add "a.txt" "A")
267 (add ".gitignore" ".~\n")
268 (commit "First commit"))
269 (let ((directory commit relation
270 (update-cached-checkout directory
271 #:ref '()
272 #:cache-directory cache)))
273 (close-port
274 (open-output-file (in-vicinity cache "stale-untracked-file")))
275 (let ((directory2 commit2 relation2
276 (update-cached-checkout directory
277 #:ref '()
278 #:cache-directory cache)))
279 (not (file-exists?
280 (in-vicinity cache "stale-untracked-file")))))))))
281
282(test-end "git")