git-download.scm 10 KB

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