git.scm 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2019, 2020 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 "remote-refs"
  157. '("refs/heads/develop" "refs/heads/master"
  158. "refs/tags/v1.0" "refs/tags/v1.1")
  159. (with-temporary-git-repository directory
  160. '((add "a.txt" "A")
  161. (commit "First commit")
  162. (tag "v1.0" "release-1.0")
  163. (branch "develop")
  164. (checkout "develop")
  165. (add "b.txt" "B")
  166. (commit "Second commit")
  167. (tag "v1.1" "release-1.1"))
  168. (remote-refs directory)))
  169. (unless (which (git-command)) (test-skip 1))
  170. (test-equal "remote-refs: only tags"
  171. '("refs/tags/v1.0" "refs/tags/v1.1")
  172. (with-temporary-git-repository directory
  173. '((add "a.txt" "A")
  174. (commit "First commit")
  175. (tag "v1.0" "Release 1.0")
  176. (add "b.txt" "B")
  177. (commit "Second commit")
  178. (tag "v1.1" "Release 1.1"))
  179. (remote-refs directory #:tags? #t)))
  180. (test-end "git")