git.scm 9.4 KB

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