git.scm 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 (srfi srfi-1)
  25. #:use-module (srfi srfi-64))
  26. ;; Test the (guix git) tools.
  27. (test-begin "git")
  28. ;; 'with-temporary-git-repository' relies on the 'git' command.
  29. (unless (which (git-command)) (test-skip 1))
  30. (test-assert "commit-difference, linear history"
  31. (with-temporary-git-repository directory
  32. '((add "a.txt" "A")
  33. (commit "first commit")
  34. (add "b.txt" "B")
  35. (commit "second commit")
  36. (add "c.txt" "C")
  37. (commit "third commit")
  38. (add "d.txt" "D")
  39. (commit "fourth commit"))
  40. (with-repository directory repository
  41. (let ((commit1 (find-commit repository "first"))
  42. (commit2 (find-commit repository "second"))
  43. (commit3 (find-commit repository "third"))
  44. (commit4 (find-commit repository "fourth")))
  45. (and (lset= eq? (commit-difference commit4 commit1)
  46. (list commit2 commit3 commit4))
  47. (lset= eq? (commit-difference commit4 commit2)
  48. (list commit3 commit4))
  49. (equal? (commit-difference commit3 commit2)
  50. (list commit3))
  51. ;; COMMIT4 is not an ancestor of COMMIT1 so we should get the
  52. ;; empty list.
  53. (null? (commit-difference commit1 commit4)))))))
  54. (unless (which (git-command)) (test-skip 1))
  55. (test-assert "commit-difference, fork"
  56. (with-temporary-git-repository directory
  57. '((add "a.txt" "A")
  58. (commit "first commit")
  59. (branch "devel")
  60. (checkout "devel")
  61. (add "devel/1.txt" "1")
  62. (commit "first devel commit")
  63. (add "devel/2.txt" "2")
  64. (commit "second devel commit")
  65. (checkout "master")
  66. (add "b.txt" "B")
  67. (commit "second commit")
  68. (add "c.txt" "C")
  69. (commit "third commit")
  70. (merge "devel" "merge")
  71. (add "d.txt" "D")
  72. (commit "fourth commit"))
  73. (with-repository directory repository
  74. (let ((master1 (find-commit repository "first commit"))
  75. (master2 (find-commit repository "second commit"))
  76. (master3 (find-commit repository "third commit"))
  77. (master4 (find-commit repository "fourth commit"))
  78. (devel1 (find-commit repository "first devel"))
  79. (devel2 (find-commit repository "second devel"))
  80. (merge (find-commit repository "merge")))
  81. (and (equal? (commit-difference master4 merge)
  82. (list master4))
  83. (lset= eq? (commit-difference master3 master1)
  84. (list master3 master2))
  85. (lset= eq? (commit-difference devel2 master1)
  86. (list devel2 devel1))
  87. ;; The merge occurred between MASTER2 and MASTER4 so here we
  88. ;; expect to see all the commits from the "devel" branch in
  89. ;; addition to those on "master".
  90. (lset= eq? (commit-difference master4 master2)
  91. (list master4 merge master3 devel1 devel2)))))))
  92. (unless (which (git-command)) (test-skip 1))
  93. (test-assert "commit-difference, excluded commits"
  94. (with-temporary-git-repository directory
  95. '((add "a.txt" "A")
  96. (commit "first commit")
  97. (add "b.txt" "B")
  98. (commit "second commit")
  99. (add "c.txt" "C")
  100. (commit "third commit")
  101. (add "d.txt" "D")
  102. (commit "fourth commit")
  103. (add "e.txt" "E")
  104. (commit "fifth commit"))
  105. (with-repository directory repository
  106. (let ((commit1 (find-commit repository "first"))
  107. (commit2 (find-commit repository "second"))
  108. (commit3 (find-commit repository "third"))
  109. (commit4 (find-commit repository "fourth"))
  110. (commit5 (find-commit repository "fifth")))
  111. (and (lset= eq? (commit-difference commit4 commit1 (list commit2))
  112. (list commit3 commit4))
  113. (lset= eq? (commit-difference commit4 commit1 (list commit3))
  114. (list commit4))
  115. (null? (commit-difference commit4 commit1 (list commit5))))))))
  116. (unless (which (git-command)) (test-skip 1))
  117. (test-equal "commit-relation"
  118. '(self ;master3 master3
  119. ancestor ;master1 master3
  120. descendant ;master3 master1
  121. unrelated ;master2 branch1
  122. unrelated ;branch1 master2
  123. ancestor ;branch1 merge
  124. descendant ;merge branch1
  125. ancestor ;master1 merge
  126. descendant) ;merge master1
  127. (with-temporary-git-repository directory
  128. '((add "a.txt" "A")
  129. (commit "first commit")
  130. (branch "hack")
  131. (checkout "hack")
  132. (add "1.txt" "1")
  133. (commit "branch commit")
  134. (checkout "master")
  135. (add "b.txt" "B")
  136. (commit "second commit")
  137. (add "c.txt" "C")
  138. (commit "third commit")
  139. (merge "hack" "merge"))
  140. (with-repository directory repository
  141. (let ((master1 (find-commit repository "first"))
  142. (master2 (find-commit repository "second"))
  143. (master3 (find-commit repository "third"))
  144. (branch1 (find-commit repository "branch"))
  145. (merge (find-commit repository "merge")))
  146. (list (commit-relation master3 master3)
  147. (commit-relation master1 master3)
  148. (commit-relation master3 master1)
  149. (commit-relation master2 branch1)
  150. (commit-relation branch1 master2)
  151. (commit-relation branch1 merge)
  152. (commit-relation merge branch1)
  153. (commit-relation master1 merge)
  154. (commit-relation merge master1))))))
  155. (unless (which (git-command)) (test-skip 1))
  156. (test-equal "commit-descendant?"
  157. '((master3 master3 => #t)
  158. (master1 master3 => #f)
  159. (master3 master1 => #t)
  160. (master2 branch1 => #f)
  161. (master2 branch1 master1 => #t)
  162. (branch1 master2 => #f)
  163. (branch1 merge => #f)
  164. (merge branch1 => #t)
  165. (master1 merge => #f)
  166. (merge master1 => #t))
  167. (with-temporary-git-repository directory
  168. '((add "a.txt" "A")
  169. (commit "first commit")
  170. (branch "hack")
  171. (checkout "hack")
  172. (add "1.txt" "1")
  173. (commit "branch commit")
  174. (checkout "master")
  175. (add "b.txt" "B")
  176. (commit "second commit")
  177. (add "c.txt" "C")
  178. (commit "third commit")
  179. (merge "hack" "merge"))
  180. (with-repository directory repository
  181. (let ((master1 (find-commit repository "first"))
  182. (master2 (find-commit repository "second"))
  183. (master3 (find-commit repository "third"))
  184. (branch1 (find-commit repository "branch"))
  185. (merge (find-commit repository "merge")))
  186. (letrec-syntax ((verify
  187. (syntax-rules ()
  188. ((_) '())
  189. ((_ (new old ...) rest ...)
  190. (cons `(new old ... =>
  191. ,(commit-descendant? new
  192. (list old ...)))
  193. (verify rest ...))))))
  194. (verify (master3 master3)
  195. (master1 master3)
  196. (master3 master1)
  197. (master2 branch1)
  198. (master2 branch1 master1)
  199. (branch1 master2)
  200. (branch1 merge)
  201. (merge branch1)
  202. (master1 merge)
  203. (merge master1)))))))
  204. (unless (which (git-command)) (test-skip 1))
  205. (test-equal "remote-refs"
  206. '("refs/heads/develop" "refs/heads/master"
  207. "refs/tags/v1.0" "refs/tags/v1.1")
  208. (with-temporary-git-repository directory
  209. '((add "a.txt" "A")
  210. (commit "First commit")
  211. (tag "v1.0" "release-1.0")
  212. (branch "develop")
  213. (checkout "develop")
  214. (add "b.txt" "B")
  215. (commit "Second commit")
  216. (tag "v1.1" "release-1.1"))
  217. (remote-refs directory)))
  218. (unless (which (git-command)) (test-skip 1))
  219. (test-equal "remote-refs: only tags"
  220. '("refs/tags/v1.0" "refs/tags/v1.1")
  221. (with-temporary-git-repository directory
  222. '((add "a.txt" "A")
  223. (commit "First commit")
  224. (tag "v1.0" "Release 1.0")
  225. (add "b.txt" "B")
  226. (commit "Second commit")
  227. (tag "v1.1" "Release 1.1"))
  228. (remote-refs directory #:tags? #t)))
  229. (test-end "git")