1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2019, 2020, 2022 Ludovic Courtès <ludo@gnu.org>
3 ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz
5 ;;; This file is part of GNU Guix.
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.
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.
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/>.
20 (define-module (test-git)
22 #:use-module (guix git)
23 #:use-module (guix tests git)
24 #:use-module (guix build utils)
25 #:use-module (srfi srfi-1)
26 #:use-module (srfi srfi-64))
28 ;; Test the (guix git) tools.
32 ;; 'with-temporary-git-repository' relies on the 'git' command.
33 (unless (which (git-command)) (test-skip 1))
34 (test-assert "commit-difference, linear history"
35 (with-temporary-git-repository directory
37 (commit "first commit")
39 (commit "second commit")
41 (commit "third commit")
43 (commit "fourth commit"))
44 (with-repository directory repository
45 (let ((commit1 (find-commit repository "first"))
46 (commit2 (find-commit repository "second"))
47 (commit3 (find-commit repository "third"))
48 (commit4 (find-commit repository "fourth")))
49 (and (lset= eq? (commit-difference commit4 commit1)
50 (list commit2 commit3 commit4))
51 (lset= eq? (commit-difference commit4 commit2)
52 (list commit3 commit4))
53 (equal? (commit-difference commit3 commit2)
56 ;; COMMIT4 is not an ancestor of COMMIT1 so we should get the
58 (null? (commit-difference commit1 commit4)))))))
60 (unless (which (git-command)) (test-skip 1))
61 (test-assert "commit-difference, fork"
62 (with-temporary-git-repository directory
64 (commit "first commit")
67 (add "devel/1.txt" "1")
68 (commit "first devel commit")
69 (add "devel/2.txt" "2")
70 (commit "second devel commit")
73 (commit "second commit")
75 (commit "third commit")
76 (merge "devel" "merge")
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)
89 (lset= eq? (commit-difference master3 master1)
90 (list master3 master2))
91 (lset= eq? (commit-difference devel2 master1)
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)))))))
100 (unless (which (git-command)) (test-skip 1))
101 (test-assert "commit-difference, excluded commits"
102 (with-temporary-git-repository directory
104 (commit "first commit")
106 (commit "second commit")
108 (commit "third commit")
110 (commit "fourth commit")
112 (commit "fifth commit"))
113 (with-repository directory repository
114 (let ((commit1 (find-commit repository "first"))
115 (commit2 (find-commit repository "second"))
116 (commit3 (find-commit repository "third"))
117 (commit4 (find-commit repository "fourth"))
118 (commit5 (find-commit repository "fifth")))
119 (and (lset= eq? (commit-difference commit4 commit1 (list commit2))
120 (list commit3 commit4))
121 (lset= eq? (commit-difference commit4 commit1 (list commit3))
123 (null? (commit-difference commit4 commit1 (list commit5))))))))
125 (unless (which (git-command)) (test-skip 1))
126 (test-equal "commit-relation"
127 '(self ;master3 master3
128 ancestor ;master1 master3
129 descendant ;master3 master1
130 unrelated ;master2 branch1
131 unrelated ;branch1 master2
132 ancestor ;branch1 merge
133 descendant ;merge branch1
134 ancestor ;master1 merge
135 descendant) ;merge master1
136 (with-temporary-git-repository directory
138 (commit "first commit")
142 (commit "branch commit")
145 (commit "second commit")
147 (commit "third commit")
148 (merge "hack" "merge"))
149 (with-repository directory repository
150 (let ((master1 (find-commit repository "first"))
151 (master2 (find-commit repository "second"))
152 (master3 (find-commit repository "third"))
153 (branch1 (find-commit repository "branch"))
154 (merge (find-commit repository "merge")))
155 (list (commit-relation master3 master3)
156 (commit-relation master1 master3)
157 (commit-relation master3 master1)
158 (commit-relation master2 branch1)
159 (commit-relation branch1 master2)
160 (commit-relation branch1 merge)
161 (commit-relation merge branch1)
162 (commit-relation master1 merge)
163 (commit-relation merge master1))))))
165 (unless (which (git-command)) (test-skip 1))
166 (test-equal "commit-descendant?"
167 '((master3 master3 => #t)
168 (master1 master3 => #f)
169 (master3 master1 => #t)
170 (master2 branch1 => #f)
171 (master2 branch1 master1 => #t)
172 (branch1 master2 => #f)
173 (branch1 merge => #f)
174 (merge branch1 => #t)
175 (master1 merge => #f)
176 (merge master1 => #t))
177 (with-temporary-git-repository directory
179 (commit "first commit")
183 (commit "branch commit")
186 (commit "second commit")
188 (commit "third commit")
189 (merge "hack" "merge"))
190 (with-repository directory repository
191 (let ((master1 (find-commit repository "first"))
192 (master2 (find-commit repository "second"))
193 (master3 (find-commit repository "third"))
194 (branch1 (find-commit repository "branch"))
195 (merge (find-commit repository "merge")))
196 (letrec-syntax ((verify
199 ((_ (new old ...) rest ...)
200 (cons `(new old ... =>
201 ,(commit-descendant? new
203 (verify rest ...))))))
204 (verify (master3 master3)
208 (master2 branch1 master1)
213 (merge master1)))))))
215 (unless (which (git-command)) (test-skip 1))
216 (test-equal "remote-refs"
217 '("refs/heads/develop" "refs/heads/master"
218 "refs/tags/v1.0" "refs/tags/v1.1")
219 (with-temporary-git-repository directory
221 (commit "First commit")
222 (tag "v1.0" "release-1.0")
226 (commit "Second commit")
227 (tag "v1.1" "release-1.1"))
228 (remote-refs directory)))
230 (unless (which (git-command)) (test-skip 1))
231 (test-equal "remote-refs: only tags"
232 '("refs/tags/v1.0" "refs/tags/v1.1")
233 (with-temporary-git-repository directory
235 (commit "First commit")
236 (tag "v1.0" "Release 1.0")
238 (commit "Second commit")
239 (tag "v1.1" "Release 1.1"))
240 (remote-refs directory #:tags? #t)))