git-download.scm 10 KB

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