git.scm 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017 Mathieu Othacehe <m.othacehe@gmail.com>
  3. ;;; Copyright © 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
  4. ;;;
  5. ;;; This file is part of GNU Guix.
  6. ;;;
  7. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or (at
  10. ;;; your option) any later version.
  11. ;;;
  12. ;;; GNU Guix is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;;; GNU General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (guix git)
  20. #:use-module (git)
  21. #:use-module (git object)
  22. #:use-module (guix i18n)
  23. #:use-module (guix base32)
  24. #:use-module (gcrypt hash)
  25. #:use-module ((guix build utils) #:select (mkdir-p))
  26. #:use-module (guix store)
  27. #:use-module (guix utils)
  28. #:use-module (guix records)
  29. #:use-module (guix gexp)
  30. #:use-module (guix sets)
  31. #:use-module (rnrs bytevectors)
  32. #:use-module (ice-9 match)
  33. #:use-module (srfi srfi-1)
  34. #:use-module (srfi srfi-11)
  35. #:use-module (srfi srfi-34)
  36. #:use-module (srfi srfi-35)
  37. #:export (%repository-cache-directory
  38. honor-system-x509-certificates!
  39. with-repository
  40. update-cached-checkout
  41. latest-repository-commit
  42. commit-difference
  43. git-checkout
  44. git-checkout?
  45. git-checkout-url
  46. git-checkout-branch))
  47. (define %repository-cache-directory
  48. (make-parameter (string-append (cache-directory #:ensure? #f)
  49. "/checkouts")))
  50. (define (honor-system-x509-certificates!)
  51. "Use the system's X.509 certificates for Git checkouts over HTTPS. Honor
  52. the 'SSL_CERT_FILE' and 'SSL_CERT_DIR' environment variables."
  53. ;; On distros such as CentOS 7, /etc/ssl/certs contains only a couple of
  54. ;; files (instead of all the certificates) among which "ca-bundle.crt". On
  55. ;; other distros /etc/ssl/certs usually contains the whole set of
  56. ;; certificates along with "ca-certificates.crt". Try to choose the right
  57. ;; one.
  58. (let ((file (letrec-syntax ((choose
  59. (syntax-rules ()
  60. ((_ file rest ...)
  61. (let ((f file))
  62. (if (and f (file-exists? f))
  63. f
  64. (choose rest ...))))
  65. ((_)
  66. #f))))
  67. (choose (getenv "SSL_CERT_FILE")
  68. "/etc/ssl/certs/ca-certificates.crt"
  69. "/etc/ssl/certs/ca-bundle.crt")))
  70. (directory (or (getenv "SSL_CERT_DIR") "/etc/ssl/certs")))
  71. (and (or file
  72. (and=> (stat directory #f)
  73. (lambda (st)
  74. (> (stat:nlink st) 2))))
  75. (begin
  76. (set-tls-certificate-locations! directory file)
  77. #t))))
  78. (define %certificates-initialized?
  79. ;; Whether 'honor-system-x509-certificates!' has already been called.
  80. #f)
  81. (define-syntax-rule (with-libgit2 thunk ...)
  82. (begin
  83. ;; XXX: The right thing to do would be to call (libgit2-shutdown) here,
  84. ;; but pointer finalizers used in guile-git may be called after shutdown,
  85. ;; resulting in a segfault. Hence, let's skip shutdown call for now.
  86. (libgit2-init!)
  87. (unless %certificates-initialized?
  88. (honor-system-x509-certificates!)
  89. (set! %certificates-initialized? #t))
  90. thunk ...))
  91. (define* (url-cache-directory url
  92. #:optional (cache-directory
  93. (%repository-cache-directory))
  94. #:key recursive?)
  95. "Return the directory associated to URL in %repository-cache-directory."
  96. (string-append
  97. cache-directory "/"
  98. (bytevector->base32-string
  99. (sha256 (string->utf8 (if recursive?
  100. (string-append "R:" url)
  101. url))))))
  102. (define (clone* url directory)
  103. "Clone git repository at URL into DIRECTORY. Upon failure,
  104. make sure no empty directory is left behind."
  105. (with-throw-handler #t
  106. (lambda ()
  107. (mkdir-p directory)
  108. ;; Note: Explicitly pass options to work around the invalid default
  109. ;; value in Guile-Git: <https://bugs.gnu.org/29238>.
  110. (if (module-defined? (resolve-interface '(git))
  111. 'clone-init-options)
  112. (clone url directory (clone-init-options))
  113. (clone url directory)))
  114. (lambda _
  115. (false-if-exception (rmdir directory)))))
  116. (define (url+commit->name url sha1)
  117. "Return the string \"<REPO-NAME>-<SHA1:7>\" where REPO-NAME is the name of
  118. the git repository, extracted from URL and SHA1:7 the seven first digits
  119. of SHA1 string."
  120. (string-append
  121. (string-replace-substring
  122. (last (string-split url #\/)) ".git" "")
  123. "-" (string-take sha1 7)))
  124. (define (switch-to-ref repository ref)
  125. "Switch to REPOSITORY's branch, commit or tag specified by REF. Return the
  126. OID (roughly the commit hash) corresponding to REF."
  127. (define obj
  128. (let resolve ((ref ref))
  129. (match ref
  130. (('branch . branch)
  131. (let ((oid (reference-target
  132. (branch-lookup repository branch BRANCH-REMOTE))))
  133. (object-lookup repository oid)))
  134. (('commit . commit)
  135. (let ((len (string-length commit)))
  136. ;; 'object-lookup-prefix' appeared in Guile-Git in Mar. 2018, so we
  137. ;; can't be sure it's available. Furthermore, 'string->oid' used to
  138. ;; read out-of-bounds when passed a string shorter than 40 chars,
  139. ;; which is why we delay calls to it below.
  140. (if (< len 40)
  141. (if (module-defined? (resolve-interface '(git object))
  142. 'object-lookup-prefix)
  143. (object-lookup-prefix repository (string->oid commit) len)
  144. (raise (condition
  145. (&message
  146. (message "long Git object ID is required")))))
  147. (object-lookup repository (string->oid commit)))))
  148. (('tag-or-commit . str)
  149. (if (or (> (string-length str) 40)
  150. (not (string-every char-set:hex-digit str)))
  151. (resolve `(tag . ,str)) ;definitely a tag
  152. (catch 'git-error
  153. (lambda ()
  154. (resolve `(tag . ,str)))
  155. (lambda _
  156. ;; There's no such tag, so it must be a commit ID.
  157. (resolve `(commit . ,str))))))
  158. (('tag . tag)
  159. (let ((oid (reference-name->oid repository
  160. (string-append "refs/tags/" tag))))
  161. ;; Get the commit that the tag at OID refers to. This is not
  162. ;; strictly needed, but it's more consistent to always return the
  163. ;; OID of a commit.
  164. (object-lookup repository
  165. (tag-target-id (tag-lookup repository oid))))))))
  166. (reset repository obj RESET_HARD)
  167. (object-id obj))
  168. (define (call-with-repository directory proc)
  169. (let ((repository #f))
  170. (dynamic-wind
  171. (lambda ()
  172. (set! repository (repository-open directory)))
  173. (lambda ()
  174. (proc repository))
  175. (lambda ()
  176. (repository-close! repository)))))
  177. (define-syntax-rule (with-repository directory repository exp ...)
  178. "Open the repository at DIRECTORY and bind REPOSITORY to it within the
  179. dynamic extent of EXP."
  180. (call-with-repository directory
  181. (lambda (repository) exp ...)))
  182. (define (load-git-submodules)
  183. "Attempt to load (git submodules), which was missing until Guile-Git 0.2.0.
  184. Return true on success, false on failure."
  185. (match (false-if-exception (resolve-interface '(git submodule)))
  186. (#f
  187. (set! load-git-submodules (const #f))
  188. #f)
  189. (iface
  190. (module-use! (resolve-module '(guix git)) iface)
  191. (set! load-git-submodules (const #t))
  192. #t)))
  193. (define* (update-submodules repository
  194. #:key (log-port (current-error-port)))
  195. "Update the submodules of REPOSITORY, a Git repository object."
  196. ;; Guile-Git < 0.2.0 did not have (git submodule).
  197. (if (load-git-submodules)
  198. (for-each (lambda (name)
  199. (let ((submodule (submodule-lookup repository name)))
  200. (format log-port (G_ "updating submodule '~a'...~%")
  201. name)
  202. (submodule-update submodule)
  203. ;; Recurse in SUBMODULE.
  204. (let ((directory (string-append
  205. (repository-working-directory repository)
  206. "/" (submodule-path submodule))))
  207. (with-repository directory repository
  208. (update-submodules repository
  209. #:log-port log-port)))))
  210. (repository-submodules repository))
  211. (format (current-error-port)
  212. (G_ "Support for submodules is missing; \
  213. please upgrade Guile-Git.~%"))))
  214. (define (reference-available? repository ref)
  215. "Return true if REF, a reference such as '(commit . \"cabba9e\"), is
  216. definitely available in REPOSITORY, false otherwise."
  217. (match ref
  218. (('commit . commit)
  219. (catch 'git-error
  220. (lambda ()
  221. (->bool (commit-lookup repository (string->oid commit))))
  222. (lambda (key error . rest)
  223. (if (= GIT_ENOTFOUND (git-error-code error))
  224. #f
  225. (apply throw key error rest)))))
  226. (_
  227. #f)))
  228. (define* (update-cached-checkout url
  229. #:key
  230. (ref '(branch . "master"))
  231. recursive?
  232. (log-port (%make-void-port "w"))
  233. (cache-directory
  234. (url-cache-directory
  235. url (%repository-cache-directory)
  236. #:recursive? recursive?)))
  237. "Update the cached checkout of URL to REF in CACHE-DIRECTORY. Return two
  238. values: the cache directory name, and the SHA1 commit (a string) corresponding
  239. to REF.
  240. REF is pair whose key is [branch | commit | tag | tag-or-commit ] and value
  241. the associated data: [<branch name> | <sha1> | <tag name> | <string>].
  242. When RECURSIVE? is true, check out submodules as well, if any."
  243. (define canonical-ref
  244. ;; We used to require callers to specify "origin/" for each branch, which
  245. ;; made little sense since the cache should be transparent to them. So
  246. ;; here we append "origin/" if it's missing and otherwise keep it.
  247. (match ref
  248. (('branch . branch)
  249. `(branch . ,(if (string-prefix? "origin/" branch)
  250. branch
  251. (string-append "origin/" branch))))
  252. (_ ref)))
  253. (with-libgit2
  254. (let* ((cache-exists? (openable-repository? cache-directory))
  255. (repository (if cache-exists?
  256. (repository-open cache-directory)
  257. (clone* url cache-directory))))
  258. ;; Only fetch remote if it has not been cloned just before.
  259. (when (and cache-exists?
  260. (not (reference-available? repository ref)))
  261. (remote-fetch (remote-lookup repository "origin")))
  262. (when recursive?
  263. (update-submodules repository #:log-port log-port))
  264. (let ((oid (switch-to-ref repository canonical-ref)))
  265. ;; Reclaim file descriptors and memory mappings associated with
  266. ;; REPOSITORY as soon as possible.
  267. (when (module-defined? (resolve-interface '(git repository))
  268. 'repository-close!)
  269. (repository-close! repository))
  270. (values cache-directory (oid->string oid))))))
  271. (define* (latest-repository-commit store url
  272. #:key
  273. recursive?
  274. (log-port (%make-void-port "w"))
  275. (cache-directory
  276. (%repository-cache-directory))
  277. (ref '(branch . "master")))
  278. "Return two values: the content of the git repository at URL copied into a
  279. store directory and the sha1 of the top level commit in this directory. The
  280. reference to be checkout, once the repository is fetched, is specified by REF.
  281. REF is pair whose key is [branch | commit | tag] and value the associated
  282. data, respectively [<branch name> | <sha1> | <tag name>].
  283. When RECURSIVE? is true, check out submodules as well, if any.
  284. Git repositories are kept in the cache directory specified by
  285. %repository-cache-directory parameter.
  286. Log progress and checkout info to LOG-PORT."
  287. (define (dot-git? file stat)
  288. (and (string=? (basename file) ".git")
  289. (or (eq? 'directory (stat:type stat))
  290. ;; Submodule checkouts end up with a '.git' regular file that
  291. ;; contains metadata about where their actual '.git' directory
  292. ;; lives.
  293. (and recursive?
  294. (eq? 'regular (stat:type stat))))))
  295. (format log-port "updating checkout of '~a'...~%" url)
  296. (let*-values
  297. (((checkout commit)
  298. (update-cached-checkout url
  299. #:recursive? recursive?
  300. #:ref ref
  301. #:cache-directory
  302. (url-cache-directory url cache-directory
  303. #:recursive?
  304. recursive?)
  305. #:log-port log-port))
  306. ((name)
  307. (url+commit->name url commit)))
  308. (format log-port "retrieved commit ~a~%" commit)
  309. (values (add-to-store store name #t "sha256" checkout
  310. #:select? (negate dot-git?))
  311. commit)))
  312. (define (print-git-error port key args default-printer)
  313. (match args
  314. (((? git-error? error) . _)
  315. (format port (G_ "Git error: ~a~%")
  316. (git-error-message error)))))
  317. (set-exception-printer! 'git-error print-git-error)
  318. ;;;
  319. ;;; Commit difference.
  320. ;;;
  321. (define* (commit-closure commit #:optional (visited (setq)))
  322. "Return the closure of COMMIT as a set. Skip commits contained in VISITED,
  323. a set, and adjoin VISITED to the result."
  324. (let loop ((commits (list commit))
  325. (visited visited))
  326. (match commits
  327. (()
  328. visited)
  329. ((head . tail)
  330. (if (set-contains? visited head)
  331. (loop tail visited)
  332. (loop (append (commit-parents head) tail)
  333. (set-insert head visited)))))))
  334. (define* (commit-difference new old #:optional (excluded '()))
  335. "Return the list of commits between NEW and OLD, where OLD is assumed to be
  336. an ancestor of NEW. Exclude all the commits listed in EXCLUDED along with
  337. their ancestors.
  338. Essentially, this computes the set difference between the closure of NEW and
  339. that of OLD."
  340. (let loop ((commits (list new))
  341. (result '())
  342. (visited (commit-closure old (list->setq excluded))))
  343. (match commits
  344. (()
  345. (reverse result))
  346. ((head . tail)
  347. (if (set-contains? visited head)
  348. (loop tail result visited)
  349. (loop (append (commit-parents head) tail)
  350. (cons head result)
  351. (set-insert head visited)))))))
  352. ;;;
  353. ;;; Checkouts.
  354. ;;;
  355. ;; Representation of the "latest" checkout of a branch or a specific commit.
  356. (define-record-type* <git-checkout>
  357. git-checkout make-git-checkout
  358. git-checkout?
  359. (url git-checkout-url)
  360. (branch git-checkout-branch (default "master"))
  361. (commit git-checkout-commit (default #f)) ;#f | tag | commit
  362. (recursive? git-checkout-recursive? (default #f)))
  363. (define* (latest-repository-commit* url #:key ref recursive? log-port)
  364. ;; Monadic variant of 'latest-repository-commit'.
  365. (lambda (store)
  366. ;; The caller--e.g., (guix scripts build)--may not handle 'git-error' so
  367. ;; translate it into '&message' conditions that we know will be properly
  368. ;; handled.
  369. (catch 'git-error
  370. (lambda ()
  371. (values (latest-repository-commit store url
  372. #:ref ref
  373. #:recursive? recursive?
  374. #:log-port log-port)
  375. store))
  376. (lambda (key error . _)
  377. (raise (condition
  378. (&message
  379. (message
  380. (match ref
  381. (('commit . commit)
  382. (format #f (G_ "cannot fetch commit ~a from ~a: ~a")
  383. commit url (git-error-message error)))
  384. (('branch . branch)
  385. (format #f (G_ "cannot fetch branch '~a' from ~a: ~a")
  386. branch url (git-error-message error)))
  387. (_
  388. (format #f (G_ "Git failure while fetching ~a: ~a")
  389. url (git-error-message error))))))))))))
  390. (define-gexp-compiler (git-checkout-compiler (checkout <git-checkout>)
  391. system target)
  392. ;; "Compile" CHECKOUT by updating the local checkout and adding it to the
  393. ;; store.
  394. (match checkout
  395. (($ <git-checkout> url branch commit recursive?)
  396. (latest-repository-commit* url
  397. #:ref (if commit
  398. `(tag-or-commit . ,commit)
  399. `(branch . ,branch))
  400. #:recursive? recursive?
  401. #:log-port (current-error-port)))))
  402. ;; Local Variables:
  403. ;; eval: (put 'with-repository 'scheme-indent-function 2)
  404. ;; End: