git.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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
  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. (define-module (test-git)
  20. #:use-module (git)
  21. #:use-module (guix git)
  22. #:use-module (guix tests git)
  23. #:use-module (guix build utils)
  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. ;; Test the (guix git) tools.
  31. (test-begin "git")
  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
  36. '((add "a.txt" "A")
  37. (commit "first commit")
  38. (add "b.txt" "B")
  39. (commit "second commit")
  40. (add "c.txt" "C")
  41. (commit "third commit")
  42. (add "d.txt" "D")
  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)
  54. (list commit3))
  55. ;; COMMIT4 is not an ancestor of COMMIT1 so we should get the
  56. ;; empty list.
  57. (null? (commit-difference commit1 commit4)))))))
  58. (unless (which (git-command)) (test-skip 1))
  59. (test-assert "commit-difference, fork"
  60. (with-temporary-git-repository directory
  61. '((add "a.txt" "A")
  62. (commit "first commit")
  63. (branch "devel")
  64. (checkout "devel")
  65. (add "devel/1.txt" "1")
  66. (commit "first devel commit")
  67. (add "devel/2.txt" "2")
  68. (commit "second devel commit")
  69. (checkout "master")
  70. (add "b.txt" "B")
  71. (commit "second commit")
  72. (add "c.txt" "C")
  73. (commit "third commit")
  74. (merge "devel" "merge")
  75. (add "d.txt" "D")
  76. (commit "fourth commit"))
  77. (with-repository directory repository
  78. (let ((master1 (find-commit repository "first commit"))
  79. (master2 (find-commit repository "second commit"))
  80. (master3 (find-commit repository "third commit"))
  81. (master4 (find-commit repository "fourth commit"))
  82. (devel1 (find-commit repository "first devel"))
  83. (devel2 (find-commit repository "second devel"))
  84. (merge (find-commit repository "merge")))
  85. (and (equal? (commit-difference master4 merge)
  86. (list master4))
  87. (lset= eq? (commit-difference master3 master1)
  88. (list master3 master2))
  89. (lset= eq? (commit-difference devel2 master1)
  90. (list devel2 devel1))
  91. ;; The merge occurred between MASTER2 and MASTER4 so here we
  92. ;; expect to see all the commits from the "devel" branch in
  93. ;; addition to those on "master".
  94. (lset= eq? (commit-difference master4 master2)
  95. (list master4 merge master3 devel1 devel2)))))))
  96. (unless (which (git-command)) (test-skip 1))
  97. (test-assert "commit-difference, excluded commits"
  98. (with-temporary-git-repository directory
  99. '((add "a.txt" "A")
  100. (commit "first commit")
  101. (add "b.txt" "B")
  102. (commit "second commit")
  103. (add "c.txt" "C")
  104. (commit "third commit")
  105. (add "d.txt" "D")
  106. (commit "fourth commit")
  107. (add "e.txt" "E")
  108. (commit "fifth commit"))
  109. (with-repository directory repository
  110. (let ((commit1 (find-commit repository "first"))
  111. (commit2 (find-commit repository "second"))
  112. (commit3 (find-commit repository "third"))
  113. (commit4 (find-commit repository "fourth"))
  114. (commit5 (find-commit repository "fifth")))
  115. (and (lset= eq? (commit-difference commit4 commit1 (list commit2))
  116. (list commit3 commit4))
  117. (lset= eq? (commit-difference commit4 commit1 (list commit3))
  118. (list commit4))
  119. (null? (commit-difference commit4 commit1 (list commit5))))))))
  120. (unless (which (git-command)) (test-skip 1))
  121. (test-equal "commit-relation"
  122. '(self ;master3 master3
  123. ancestor ;master1 master3
  124. descendant ;master3 master1
  125. unrelated ;master2 branch1
  126. unrelated ;branch1 master2
  127. ancestor ;branch1 merge
  128. descendant ;merge branch1
  129. ancestor ;master1 merge
  130. descendant) ;merge master1
  131. (with-temporary-git-repository directory
  132. '((add "a.txt" "A")
  133. (commit "first commit")
  134. (branch "hack")
  135. (checkout "hack")
  136. (add "1.txt" "1")
  137. (commit "branch commit")
  138. (checkout "master")
  139. (add "b.txt" "B")
  140. (commit "second commit")
  141. (add "c.txt" "C")
  142. (commit "third commit")
  143. (merge "hack" "merge"))
  144. (with-repository directory repository
  145. (let ((master1 (find-commit repository "first"))
  146. (master2 (find-commit repository "second"))
  147. (master3 (find-commit repository "third"))
  148. (branch1 (find-commit repository "branch"))
  149. (merge (find-commit repository "merge")))
  150. (list (commit-relation master3 master3)
  151. (commit-relation master1 master3)
  152. (commit-relation master3 master1)
  153. (commit-relation master2 branch1)
  154. (commit-relation branch1 master2)
  155. (commit-relation branch1 merge)
  156. (commit-relation merge branch1)
  157. (commit-relation master1 merge)
  158. (commit-relation merge master1))))))
  159. (unless (which (git-command)) (test-skip 1))
  160. (test-equal "commit-descendant?"
  161. '((master3 master3 => #t)
  162. (master1 master3 => #f)
  163. (master3 master1 => #t)
  164. (master2 branch1 => #f)
  165. (master2 branch1 master1 => #t)
  166. (branch1 master2 => #f)
  167. (branch1 merge => #f)
  168. (merge branch1 => #t)
  169. (master1 merge => #f)
  170. (merge master1 => #t))
  171. (with-temporary-git-repository directory
  172. '((add "a.txt" "A")
  173. (commit "first commit")
  174. (branch "hack")
  175. (checkout "hack")
  176. (add "1.txt" "1")
  177. (commit "branch commit")
  178. (checkout "master")
  179. (add "b.txt" "B")
  180. (commit "second commit")
  181. (add "c.txt" "C")
  182. (commit "third commit")
  183. (merge "hack" "merge"))
  184. (with-repository directory repository
  185. (let ((master1 (find-commit repository "first"))
  186. (master2 (find-commit repository "second"))
  187. (master3 (find-commit repository "third"))
  188. (branch1 (find-commit repository "branch"))
  189. (merge (find-commit repository "merge")))
  190. (letrec-syntax ((verify
  191. (syntax-rules ()
  192. ((_) '())
  193. ((_ (new old ...) rest ...)
  194. (cons `(new old ... =>
  195. ,(commit-descendant? new
  196. (list old ...)))
  197. (verify rest ...))))))
  198. (verify (master3 master3)
  199. (master1 master3)
  200. (master3 master1)
  201. (master2 branch1)
  202. (master2 branch1 master1)
  203. (branch1 master2)
  204. (branch1 merge)
  205. (merge branch1)
  206. (master1 merge)
  207. (merge master1)))))))
  208. (unless (which (git-command)) (test-skip 1))
  209. (test-equal "remote-refs"
  210. '("refs/heads/develop" "refs/heads/master"
  211. "refs/tags/v1.0" "refs/tags/v1.1")
  212. (with-temporary-git-repository directory
  213. '((add "a.txt" "A")
  214. (commit "First commit")
  215. (tag "v1.0" "release-1.0")
  216. (branch "develop")
  217. (checkout "develop")
  218. (add "b.txt" "B")
  219. (commit "Second commit")
  220. (tag "v1.1" "release-1.1"))
  221. (remote-refs directory)))
  222. (unless (which (git-command)) (test-skip 1))
  223. (test-equal "remote-refs: only tags"
  224. '("refs/tags/v1.0" "refs/tags/v1.1")
  225. (with-temporary-git-repository directory
  226. '((add "a.txt" "A")
  227. (commit "First commit")
  228. (tag "v1.0" "Release 1.0")
  229. (add "b.txt" "B")
  230. (commit "Second commit")
  231. (tag "v1.1" "Release 1.1"))
  232. (remote-refs directory #:tags? #t)))
  233. (unless (which (git-command)) (test-skip 1))
  234. (test-assert "update-cached-checkout, tag"
  235. (call-with-temporary-directory
  236. (lambda (cache)
  237. (with-temporary-git-repository directory
  238. '((add "a.txt" "A")
  239. (commit "First commit")
  240. (tag "v1.0" "release-1.0")
  241. (branch "develop")
  242. (checkout "develop")
  243. (add "b.txt" "B")
  244. (commit "Second commit")
  245. (tag "v1.1" "release-1.1"))
  246. (let ((directory commit relation
  247. (update-cached-checkout directory
  248. #:ref '(tag . "v1.1")
  249. #:cache-directory cache))
  250. (head (let* ((pipe (open-pipe* OPEN_READ (git-command)
  251. "-C" directory
  252. "rev-parse" "HEAD"))
  253. (str (get-string-all pipe)))
  254. (close-pipe pipe)
  255. (string-trim-right str))))
  256. ;; COMMIT should be the ID of the commit object, not that of the tag.
  257. (string=? commit head))))))
  258. (test-end "git")