git.scm 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
  3. ;;; Copyright © 2021 Sarah Morgensen <iskarian@mgsn.dev>
  4. ;;; Copyright © 2022 Maxime Devos <maximedevos@telenet.be>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  20. (define-module (guix import git)
  21. #:use-module (guix build utils)
  22. #:use-module (guix diagnostics)
  23. #:use-module (guix git)
  24. #:use-module (guix git-download)
  25. #:use-module (guix i18n)
  26. #:use-module (guix packages)
  27. #:use-module (guix upstream)
  28. #:use-module (guix utils)
  29. #:use-module (ice-9 format)
  30. #:use-module (ice-9 match)
  31. #:use-module (ice-9 rdelim)
  32. #:use-module (ice-9 regex)
  33. #:use-module (srfi srfi-1)
  34. #:use-module (srfi srfi-26)
  35. #:use-module (srfi srfi-34)
  36. #:use-module (srfi srfi-35)
  37. #:use-module (srfi srfi-71)
  38. #:export (%generic-git-updater
  39. ;; For tests.
  40. latest-git-tag-version))
  41. ;;; Commentary:
  42. ;;;
  43. ;;; This module provides a generic package updater for packages hosted on Git
  44. ;;; repositories.
  45. ;;;
  46. ;;; It tries to be smart about tag names, but if it is not automatically able
  47. ;;; to parse the tag names correctly, users can set the `release-tag-prefix',
  48. ;;; `release-tag-suffix' and `release-tag-version-delimiter' properties of the
  49. ;;; package to make the updater parse the Git tag name correctly.
  50. ;;;
  51. ;;; Possible improvements:
  52. ;;;
  53. ;;; * More robust method for trying to guess the delimiter. Maybe look at the
  54. ;;; previous version/tag combo to determine the delimiter.
  55. ;;;
  56. ;;; * Differentiate between "normal" versions, e.g., 1.2.3, and dates, e.g.,
  57. ;;; 2021.12.31. Honor a `release-tag-date-scheme?' property?
  58. ;;;
  59. ;;; Code:
  60. ;;; Errors & warnings
  61. (define-condition-type &git-no-valid-tags-error &error
  62. git-no-valid-tags-error?)
  63. (define (git-no-valid-tags-error)
  64. (raise (condition (&message (message "no valid tags found"))
  65. (&git-no-valid-tags-error))))
  66. (define-condition-type &git-no-tags-error &error
  67. git-no-tags-error?)
  68. (define (git-no-tags-error)
  69. (raise (condition (&message (message "no tags were found"))
  70. (&git-no-tags-error))))
  71. ;;; Updater
  72. (define %pre-release-words
  73. '("alpha" "beta" "rc" "dev" "test" "pre"))
  74. (define %pre-release-rx
  75. (map (lambda (word)
  76. (make-regexp (string-append ".+" word) regexp/icase))
  77. %pre-release-words))
  78. (define* (version-mapping tags #:key prefix suffix delim pre-releases?)
  79. "Given a list of Git TAGS, return an association list where the car is the
  80. version corresponding to the tag, and the cdr is the name of the tag."
  81. (define (guess-delimiter)
  82. (let ((total (length tags))
  83. (dots (reduce + 0 (map (cut string-count <> #\.) tags)))
  84. (dashes (reduce + 0 (map (cut string-count <> #\-) tags)))
  85. (underscores (reduce + 0 (map (cut string-count <> #\_) tags))))
  86. (cond
  87. ((>= dots (* total 0.35)) ".")
  88. ((>= dashes (* total 0.8)) "-")
  89. ((>= underscores (* total 0.8)) "_")
  90. (else ""))))
  91. (define delim-rx (regexp-quote (or delim (guess-delimiter))))
  92. (define suffix-rx (string-append (or suffix "") "$"))
  93. (define prefix-rx (string-append "^" (or prefix "[^[:digit:]]*")))
  94. (define pre-release-rx
  95. (if pre-releases?
  96. (string-append "(.*(" (string-join %pre-release-words "|") ").*)")
  97. ""))
  98. (define tag-rx
  99. (string-append prefix-rx "([[:digit:]][^" delim-rx "[:punct:]]*"
  100. "(" delim-rx "[^[:punct:]" delim-rx "]+)"
  101. ;; If there are no delimiters, it could mean that the
  102. ;; version just contains one number (e.g., "2"), thus, use
  103. ;; "*" instead of "+" to match zero or more numbers.
  104. (if (string=? delim-rx "") "*" "+") ")"
  105. ;; We don't want the pre-release stuff (e.g., "-alpha") be
  106. ;; part of the first group; otherwise, the "-" in "-alpha"
  107. ;; might be interpreted as a delimiter, and thus replaced
  108. ;; with "."
  109. pre-release-rx suffix-rx))
  110. (define (get-version tag)
  111. (let ((tag-match (regexp-exec (make-regexp tag-rx) tag)))
  112. (and=> (and tag-match
  113. (regexp-substitute/global
  114. #f delim-rx (match:substring tag-match 1)
  115. ;; If there were no delimiters, don't insert ".".
  116. 'pre (if (string=? delim-rx "") "" ".") 'post))
  117. (lambda (version)
  118. (if pre-releases?
  119. (string-append version (match:substring tag-match 3))
  120. version)))))
  121. (define (entry<? a b)
  122. (eq? (version-compare (car a) (car b)) '<))
  123. (stable-sort (filter-map (lambda (tag)
  124. (let ((version (get-version tag)))
  125. (and version (cons version tag))))
  126. tags)
  127. entry<?))
  128. (define* (latest-tag url #:key prefix suffix delim pre-releases?)
  129. "Return the latest version and corresponding tag available from the Git
  130. repository at URL."
  131. (define (pre-release? tag)
  132. (any (cut regexp-exec <> tag)
  133. %pre-release-rx))
  134. (let* ((tags (map (cut string-drop <> (string-length "refs/tags/"))
  135. (remote-refs url #:tags? #t)))
  136. (versions->tags
  137. (version-mapping (if pre-releases?
  138. tags
  139. (filter (negate pre-release?) tags))
  140. #:prefix prefix
  141. #:suffix suffix
  142. #:delim delim
  143. #:pre-releases? pre-releases?)))
  144. (cond
  145. ((null? tags)
  146. (git-no-tags-error))
  147. ((null? versions->tags)
  148. (git-no-valid-tags-error))
  149. (else
  150. (match (last versions->tags)
  151. ((version . tag)
  152. (values version tag)))))))
  153. (define (latest-git-tag-version package)
  154. "Given a PACKAGE, return the latest version of it and the corresponding git
  155. tag, or #false and #false if the latest version could not be determined."
  156. (guard (c ((or (git-no-tags-error? c) (git-no-valid-tags-error? c))
  157. (warning (or (package-field-location package 'source)
  158. (package-location package))
  159. (G_ "~a for ~a~%")
  160. (condition-message c)
  161. (package-name package))
  162. (values #f #f))
  163. ((eq? (exception-kind c) 'git-error)
  164. (warning (or (package-field-location package 'source)
  165. (package-location package))
  166. (G_ "failed to fetch Git repository for ~a~%")
  167. (package-name package))
  168. (values #f #f)))
  169. (let* ((source (package-source package))
  170. (url (git-reference-url (origin-uri source)))
  171. (property (cute assq-ref (package-properties package) <>)))
  172. (latest-tag url
  173. #:prefix (property 'release-tag-prefix)
  174. #:suffix (property 'release-tag-suffix)
  175. #:delim (property 'release-tag-version-delimiter)
  176. #:pre-releases? (property 'accept-pre-releases?)))))
  177. (define (git-package? package)
  178. "Return true if PACKAGE is hosted on a Git repository."
  179. (match (package-source package)
  180. ((? origin? origin)
  181. (and (eq? (origin-method origin) git-fetch)
  182. (git-reference? (origin-uri origin))))
  183. (_ #f)))
  184. (define (latest-git-release package)
  185. "Return an <upstream-source> for the latest release of PACKAGE."
  186. (let* ((name (package-name package))
  187. (old-version (package-version package))
  188. (old-reference (origin-uri (package-source package)))
  189. (new-version new-version-tag (latest-git-tag-version package)))
  190. (and new-version new-version-tag
  191. (upstream-source
  192. (package name)
  193. (version new-version)
  194. (urls (git-reference
  195. (url (git-reference-url old-reference))
  196. (commit new-version-tag)
  197. (recursive? (git-reference-recursive? old-reference))))))))
  198. (define %generic-git-updater
  199. (upstream-updater
  200. (name 'generic-git)
  201. (description "Updater for packages hosted on Git repositories")
  202. (pred git-package?)
  203. (latest latest-git-release)))