git-download.scm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014, 2015, 2016, 2017, 2018, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2017 Mathieu Lirzin <mthl@gnu.org>
  4. ;;; Copyright © 2017 Christopher Baines <mail@cbaines.net>
  5. ;;; Copyright © 2020 Jakub Kądziołka <kuba@kadziolka.net>
  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 git-download)
  22. #:use-module (guix gexp)
  23. #:use-module (guix store)
  24. #:use-module (guix monads)
  25. #:use-module (guix records)
  26. #:use-module (guix packages)
  27. #:use-module (guix modules)
  28. #:autoload (guix build-system gnu) (standard-packages)
  29. #:autoload (git bindings) (libgit2-init!)
  30. #:autoload (git repository) (repository-open
  31. repository-close!
  32. repository-discover
  33. repository-head
  34. repository-working-directory)
  35. #:autoload (git submodule) (repository-submodules
  36. submodule-lookup
  37. submodule-path)
  38. #:autoload (git commit) (commit-lookup commit-tree)
  39. #:autoload (git reference) (reference-target)
  40. #:autoload (git tree) (tree-list)
  41. #:use-module (ice-9 match)
  42. #:use-module (ice-9 vlist)
  43. #:use-module (srfi srfi-1)
  44. #:use-module (srfi srfi-34)
  45. #:use-module (srfi srfi-35)
  46. #:export (git-reference
  47. git-reference?
  48. git-reference-url
  49. git-reference-commit
  50. git-reference-recursive?
  51. git-fetch
  52. git-version
  53. git-file-name
  54. git-predicate))
  55. ;;; Commentary:
  56. ;;;
  57. ;;; An <origin> method that fetches a specific commit from a Git repository.
  58. ;;; The repository URL and commit hash are specified with a <git-reference>
  59. ;;; object.
  60. ;;;
  61. ;;; Code:
  62. (define-record-type* <git-reference>
  63. git-reference make-git-reference
  64. git-reference?
  65. (url git-reference-url)
  66. (commit git-reference-commit)
  67. (recursive? git-reference-recursive? ; whether to recurse into sub-modules
  68. (default #f)))
  69. (define (git-package)
  70. "Return the default Git package."
  71. (let ((distro (resolve-interface '(gnu packages version-control))))
  72. (module-ref distro 'git-minimal)))
  73. (define* (git-fetch ref hash-algo hash
  74. #:optional name
  75. #:key (system (%current-system)) (guile (default-guile))
  76. (git (git-package)))
  77. "Return a fixed-output derivation that fetches REF, a <git-reference>
  78. object. The output is expected to have recursive hash HASH of type
  79. HASH-ALGO (a symbol). Use NAME as the file name, or a generic name if #f."
  80. (define inputs
  81. ;; When doing 'git clone --recursive', we need sed, grep, etc. to be
  82. ;; available so that 'git submodule' works.
  83. (if (git-reference-recursive? ref)
  84. (standard-packages)
  85. ;; The 'swh-download' procedure requires tar and gzip.
  86. `(("gzip" ,(module-ref (resolve-interface '(gnu packages compression))
  87. 'gzip))
  88. ("tar" ,(module-ref (resolve-interface '(gnu packages base))
  89. 'tar)))))
  90. (define guile-json
  91. (module-ref (resolve-interface '(gnu packages guile)) 'guile-json-4))
  92. (define guile-zlib
  93. (module-ref (resolve-interface '(gnu packages guile)) 'guile-zlib))
  94. (define gnutls
  95. (module-ref (resolve-interface '(gnu packages tls)) 'gnutls))
  96. (define modules
  97. (delete '(guix config)
  98. (source-module-closure '((guix build git)
  99. (guix build utils)
  100. (guix build download-nar)
  101. (guix swh)))))
  102. (define build
  103. (with-imported-modules modules
  104. (with-extensions (list guile-json gnutls ;for (guix swh)
  105. guile-zlib)
  106. #~(begin
  107. (use-modules (guix build git)
  108. (guix build utils)
  109. (guix build download-nar)
  110. (guix swh)
  111. (ice-9 match))
  112. (define recursive?
  113. (call-with-input-string (getenv "git recursive?") read))
  114. ;; The 'git submodule' commands expects Coreutils, sed,
  115. ;; grep, etc. to be in $PATH.
  116. (set-path-environment-variable "PATH" '("bin")
  117. (match '#+inputs
  118. (((names dirs outputs ...) ...)
  119. dirs)))
  120. (setvbuf (current-output-port) 'line)
  121. (setvbuf (current-error-port) 'line)
  122. (or (git-fetch (getenv "git url") (getenv "git commit")
  123. #$output
  124. #:recursive? recursive?
  125. #:git-command (string-append #+git "/bin/git"))
  126. (download-nar #$output)
  127. ;; As a last resort, attempt to download from Software Heritage.
  128. ;; Disable X.509 certificate verification to avoid depending
  129. ;; on nss-certs--we're authenticating the checkout anyway.
  130. ;; XXX: Currently recursive checkouts are not supported.
  131. (and (not recursive?)
  132. (parameterize ((%verify-swh-certificate? #f))
  133. (format (current-error-port)
  134. "Trying to download from Software Heritage...~%")
  135. (swh-download (getenv "git url") (getenv "git commit")
  136. #$output))))))))
  137. (mlet %store-monad ((guile (package->derivation guile system)))
  138. (gexp->derivation (or name "git-checkout") build
  139. ;; Use environment variables and a fixed script name so
  140. ;; there's only one script in store for all the
  141. ;; downloads.
  142. #:script-name "git-download"
  143. #:env-vars
  144. `(("git url" . ,(git-reference-url ref))
  145. ("git commit" . ,(git-reference-commit ref))
  146. ("git recursive?" . ,(object->string
  147. (git-reference-recursive? ref))))
  148. #:leaked-env-vars '("http_proxy" "https_proxy"
  149. "LC_ALL" "LC_MESSAGES" "LANG"
  150. "COLUMNS")
  151. #:system system
  152. #:local-build? #t ;don't offload repo cloning
  153. #:hash-algo hash-algo
  154. #:hash hash
  155. #:recursive? #t
  156. #:guile-for-build guile)))
  157. (define (git-version version revision commit)
  158. "Return the version string for packages using git-download."
  159. ;; git-version is almost exclusively executed while modules are being loaded.
  160. ;; This makes any errors hide their backtrace. Avoid the mysterious error
  161. ;; "Value out of range 0 to N: 7" when the commit ID is too short, which
  162. ;; can happen, for example, when the user swapped the revision and commit
  163. ;; arguments by mistake.
  164. (when (< (string-length commit) 7)
  165. (raise
  166. (condition
  167. (&message (message "git-version: commit ID unexpectedly short")))))
  168. (string-append version "-" revision "." (string-take commit 7)))
  169. (define (git-file-name name version)
  170. "Return the file-name for packages using git-download."
  171. (string-append name "-" version "-checkout"))
  172. ;;;
  173. ;;; 'git-predicate'.
  174. ;;;
  175. (define* (git-file-list directory #:optional prefix #:key (recursive? #t))
  176. "Return the list of files checked in in the Git repository at DIRECTORY.
  177. The result is similar to that of the 'git ls-files' command, except that it
  178. also includes directories, not just regular files.
  179. When RECURSIVE? is true, also list files in submodules, similar to the 'git
  180. ls-files --recurse-submodules' command. This is enabled by default.
  181. The returned file names are relative to DIRECTORY, which is not necessarily
  182. the root of the checkout. If a PREFIX is provided, it is prepended to each
  183. file name."
  184. (let* (;; 'repository-working-directory' always returns a trailing "/",
  185. ;; so add one here to ease the comparisons below.
  186. (directory (string-append (canonicalize-path directory) "/"))
  187. (dot-git (repository-discover directory))
  188. (repository (repository-open dot-git))
  189. (workdir (repository-working-directory repository))
  190. (head (repository-head repository))
  191. (oid (reference-target head))
  192. (commit (commit-lookup repository oid))
  193. (tree (commit-tree commit))
  194. (files (tree-list tree))
  195. (submodules (if recursive?
  196. (map (lambda (name)
  197. (submodule-path
  198. (submodule-lookup repository name)))
  199. (repository-submodules repository))
  200. '()))
  201. (relative (and (not (string=? workdir directory))
  202. (string-drop directory (string-length workdir))))
  203. (included? (lambda (path)
  204. (or (not relative)
  205. (string-prefix? relative path))))
  206. (make-relative (lambda (path)
  207. (if relative
  208. (string-drop path (string-length relative))
  209. path)))
  210. (add-prefix (lambda (path)
  211. (if prefix
  212. (string-append prefix "/" path)
  213. path)))
  214. (rectify (compose add-prefix make-relative)))
  215. (repository-close! repository)
  216. (append
  217. (if (or relative prefix)
  218. (filter-map (lambda (file)
  219. (and (included? file)
  220. (rectify file)))
  221. files)
  222. files)
  223. (append-map (lambda (submodule)
  224. (if (included? submodule)
  225. (git-file-list
  226. (string-append workdir submodule)
  227. (rectify submodule))
  228. '()))
  229. submodules))))
  230. (define* (git-predicate directory #:key (recursive? #t))
  231. "Return a predicate that returns true if a file is part of the Git checkout
  232. living at DIRECTORY. If DIRECTORY does not lie within a Git checkout, and
  233. upon Git errors, return #f instead of a predicate.
  234. When RECURSIVE? is true, the predicate also returns true if a file is part of
  235. any Git submodule under DIRECTORY. This is enabled by default.
  236. The returned predicate takes two arguments FILE and STAT where FILE is an
  237. absolute file name and STAT is the result of 'lstat'."
  238. (libgit2-init!)
  239. (catch 'git-error
  240. (lambda ()
  241. (let* ((files (git-file-list directory #:recursive? recursive?))
  242. (inodes (fold (lambda (file result)
  243. (let* ((file (string-append directory "/" file))
  244. (stat (false-if-exception (lstat file))))
  245. ;; Ignore FILE if it has been deleted.
  246. (if stat
  247. (vhash-consv (stat:ino stat) (stat:dev stat)
  248. result)
  249. result)))
  250. vlist-null
  251. files)))
  252. (lambda (file stat)
  253. ;; Comparing file names is always tricky business so we rely on inode
  254. ;; numbers instead.
  255. (match (vhash-assv (stat:ino stat) inodes)
  256. ((_ . dev) (= dev (stat:dev stat)))
  257. (#f #f)))))
  258. (const #f)))
  259. ;;; git-download.scm ends here