git-download.scm 9.9 KB

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