git.scm 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017, 2020 Mathieu Othacehe <m.othacehe@gmail.com>
  3. ;;; Copyright © 2018, 2019, 2020, 2021 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 (git submodule)
  23. #:use-module (guix i18n)
  24. #:use-module (guix base32)
  25. #:use-module (guix cache)
  26. #:use-module (gcrypt hash)
  27. #:use-module ((guix build utils)
  28. #:select (mkdir-p delete-file-recursively))
  29. #:use-module (guix store)
  30. #:use-module (guix utils)
  31. #:use-module (guix records)
  32. #:use-module (guix gexp)
  33. #:use-module (guix sets)
  34. #:use-module ((guix diagnostics) #:select (leave))
  35. #:use-module (guix progress)
  36. #:use-module (rnrs bytevectors)
  37. #:use-module (ice-9 format)
  38. #:use-module (ice-9 match)
  39. #:use-module (ice-9 ftw)
  40. #:use-module (srfi srfi-1)
  41. #:use-module (srfi srfi-11)
  42. #:use-module (srfi srfi-34)
  43. #:use-module (srfi srfi-35)
  44. #:export (%repository-cache-directory
  45. honor-system-x509-certificates!
  46. url-cache-directory
  47. with-repository
  48. with-git-error-handling
  49. false-if-git-not-found
  50. update-cached-checkout
  51. url+commit->name
  52. latest-repository-commit
  53. commit-difference
  54. commit-relation
  55. git-checkout
  56. git-checkout?
  57. git-checkout-url
  58. git-checkout-branch
  59. git-checkout-commit
  60. git-checkout-recursive?))
  61. (define %repository-cache-directory
  62. (make-parameter (string-append (cache-directory #:ensure? #f)
  63. "/checkouts")))
  64. (define (honor-system-x509-certificates!)
  65. "Use the system's X.509 certificates for Git checkouts over HTTPS. Honor
  66. the 'SSL_CERT_FILE' and 'SSL_CERT_DIR' environment variables."
  67. ;; On distros such as CentOS 7, /etc/ssl/certs contains only a couple of
  68. ;; files (instead of all the certificates) among which "ca-bundle.crt". On
  69. ;; other distros /etc/ssl/certs usually contains the whole set of
  70. ;; certificates along with "ca-certificates.crt". Try to choose the right
  71. ;; one.
  72. (let ((file (letrec-syntax ((choose
  73. (syntax-rules ()
  74. ((_ file rest ...)
  75. (let ((f file))
  76. (if (and f (file-exists? f))
  77. f
  78. (choose rest ...))))
  79. ((_)
  80. #f))))
  81. (choose (getenv "SSL_CERT_FILE")
  82. "/etc/ssl/certs/ca-certificates.crt"
  83. "/etc/ssl/certs/ca-bundle.crt")))
  84. (directory (or (getenv "SSL_CERT_DIR") "/etc/ssl/certs")))
  85. (and (or file
  86. (and=> (stat directory #f)
  87. (lambda (st)
  88. (> (stat:nlink st) 2))))
  89. (begin
  90. (set-tls-certificate-locations! directory file)
  91. #t))))
  92. (define %certificates-initialized?
  93. ;; Whether 'honor-system-x509-certificates!' has already been called.
  94. #f)
  95. (define-syntax-rule (with-libgit2 thunk ...)
  96. (begin
  97. ;; XXX: The right thing to do would be to call (libgit2-shutdown) here,
  98. ;; but pointer finalizers used in guile-git may be called after shutdown,
  99. ;; resulting in a segfault. Hence, let's skip shutdown call for now.
  100. (libgit2-init!)
  101. (unless %certificates-initialized?
  102. (honor-system-x509-certificates!)
  103. (set! %certificates-initialized? #t))
  104. thunk ...))
  105. (define* (url-cache-directory url
  106. #:optional (cache-directory
  107. (%repository-cache-directory))
  108. #:key recursive?)
  109. "Return the directory associated to URL in %repository-cache-directory."
  110. (string-append
  111. cache-directory "/"
  112. (bytevector->base32-string
  113. (sha256 (string->utf8 (if recursive?
  114. (string-append "R:" url)
  115. url))))))
  116. (define (show-progress progress)
  117. "Display a progress bar as we fetch Git code. PROGRESS is an
  118. <indexer-progress> record from (git)."
  119. (define total
  120. (indexer-progress-total-objects progress))
  121. (define hundredth
  122. (match (quotient (indexer-progress-total-objects progress) 100)
  123. (0 1)
  124. (x x)))
  125. (define-values (done label)
  126. (if (< (indexer-progress-received-objects progress) total)
  127. (values (indexer-progress-received-objects progress)
  128. (G_ "receiving objects"))
  129. (values (indexer-progress-indexed-objects progress)
  130. (G_ "indexing objects"))))
  131. (define %
  132. (* 100. (/ done total)))
  133. (when (and (< % 100) (zero? (modulo done hundredth)))
  134. (erase-current-line (current-error-port))
  135. (let ((width (max (- (current-terminal-columns)
  136. (string-length label) 7)
  137. 3)))
  138. (format (current-error-port) "~a ~3,d% ~a"
  139. label (inexact->exact (round %))
  140. (progress-bar % width)))
  141. (force-output (current-error-port)))
  142. (when (= % 100.)
  143. ;; We're done, erase the line.
  144. (erase-current-line (current-error-port))
  145. (force-output (current-error-port)))
  146. ;; Return true to indicate that we should go on.
  147. #t)
  148. (define (make-default-fetch-options)
  149. "Return the default fetch options."
  150. (let ((auth-method (%make-auth-ssh-agent)))
  151. ;; The #:transfer-progress and #:proxy-url options appeared in Guile-Git
  152. ;; 0.4.0. Omit them when using an older version.
  153. (catch 'wrong-number-of-args
  154. (lambda ()
  155. (make-fetch-options auth-method
  156. ;; Guile-Git doesn't distinguish between these.
  157. #:proxy-url (or (getenv "http_proxy")
  158. (getenv "https_proxy"))
  159. #:transfer-progress
  160. (and (isatty? (current-error-port))
  161. show-progress)))
  162. (lambda args
  163. (make-fetch-options auth-method)))))
  164. (define (clone* url directory)
  165. "Clone git repository at URL into DIRECTORY. Upon failure,
  166. make sure no empty directory is left behind."
  167. (with-throw-handler #t
  168. (lambda ()
  169. (mkdir-p directory)
  170. (let ((auth-method (%make-auth-ssh-agent)))
  171. (clone url directory
  172. (make-clone-options
  173. #:fetch-options (make-default-fetch-options)))))
  174. (lambda _
  175. (false-if-exception (rmdir directory)))))
  176. (define (url+commit->name url sha1)
  177. "Return the string \"<REPO-NAME>-<SHA1:7>\" where REPO-NAME is the name of
  178. the git repository, extracted from URL and SHA1:7 the seven first digits
  179. of SHA1 string."
  180. (string-append
  181. (string-replace-substring
  182. (last (string-split url #\/)) ".git" "")
  183. "-" (string-take sha1 7)))
  184. (define (resolve-reference repository ref)
  185. "Resolve the branch, commit or tag specified by REF, and return the
  186. corresponding Git object."
  187. (let resolve ((ref ref))
  188. (match ref
  189. (('branch . branch)
  190. (let ((oid (reference-target
  191. (branch-lookup repository branch BRANCH-REMOTE))))
  192. (object-lookup repository oid)))
  193. (('commit . commit)
  194. (let ((len (string-length commit)))
  195. ;; 'object-lookup-prefix' appeared in Guile-Git in Mar. 2018, so we
  196. ;; can't be sure it's available. Furthermore, 'string->oid' used to
  197. ;; read out-of-bounds when passed a string shorter than 40 chars,
  198. ;; which is why we delay calls to it below.
  199. (if (< len 40)
  200. (object-lookup-prefix repository (string->oid commit) len)
  201. (object-lookup repository (string->oid commit)))))
  202. (('tag-or-commit . str)
  203. (if (or (> (string-length str) 40)
  204. (not (string-every char-set:hex-digit str)))
  205. (resolve `(tag . ,str)) ;definitely a tag
  206. (catch 'git-error
  207. (lambda ()
  208. (resolve `(tag . ,str)))
  209. (lambda _
  210. ;; There's no such tag, so it must be a commit ID.
  211. (resolve `(commit . ,str))))))
  212. (('tag . tag)
  213. (let ((oid (reference-name->oid repository
  214. (string-append "refs/tags/" tag))))
  215. ;; OID may point to a "tag" object, but it can also point directly
  216. ;; to a "commit" object, as surprising as it may seem. Return that
  217. ;; object, whatever that is.
  218. (object-lookup repository oid))))))
  219. (define (switch-to-ref repository ref)
  220. "Switch to REPOSITORY's branch, commit or tag specified by REF. Return the
  221. OID (roughly the commit hash) corresponding to REF."
  222. (define obj
  223. (resolve-reference repository ref))
  224. (reset repository obj RESET_HARD)
  225. (object-id obj))
  226. (define (call-with-repository directory proc)
  227. (let ((repository #f))
  228. (dynamic-wind
  229. (lambda ()
  230. (set! repository (repository-open directory)))
  231. (lambda ()
  232. (proc repository))
  233. (lambda ()
  234. (repository-close! repository)))))
  235. (define-syntax-rule (with-repository directory repository exp ...)
  236. "Open the repository at DIRECTORY and bind REPOSITORY to it within the
  237. dynamic extent of EXP."
  238. (call-with-repository directory
  239. (lambda (repository) exp ...)))
  240. (define (report-git-error error)
  241. "Report the given Guile-Git error."
  242. ;; Prior to Guile-Git commit b6b2760c2fd6dfaa5c0fedb43eeaff06166b3134,
  243. ;; errors would be represented by integers.
  244. (match error
  245. ((? integer? error) ;old Guile-Git
  246. (leave (G_ "Git error ~a~%") error))
  247. ((? git-error? error) ;new Guile-Git
  248. (leave (G_ "Git error: ~a~%") (git-error-message error)))))
  249. (define-syntax-rule (with-git-error-handling body ...)
  250. (catch 'git-error
  251. (lambda ()
  252. body ...)
  253. (lambda (key err)
  254. (report-git-error err))))
  255. (define* (update-submodules repository
  256. #:key (log-port (current-error-port)))
  257. "Update the submodules of REPOSITORY, a Git repository object."
  258. (for-each (lambda (name)
  259. (let ((submodule (submodule-lookup repository name)))
  260. (format log-port (G_ "updating submodule '~a'...~%")
  261. name)
  262. (submodule-update submodule)
  263. ;; Recurse in SUBMODULE.
  264. (let ((directory (string-append
  265. (repository-working-directory repository)
  266. "/" (submodule-path submodule))))
  267. (with-repository directory repository
  268. (update-submodules repository
  269. #:log-port log-port)))))
  270. (repository-submodules repository)))
  271. (define-syntax-rule (false-if-git-not-found exp)
  272. "Evaluate EXP, returning #false if a GIT_ENOTFOUND error is raised."
  273. (catch 'git-error
  274. (lambda ()
  275. exp)
  276. (lambda (key error . rest)
  277. (if (= GIT_ENOTFOUND (git-error-code error))
  278. #f
  279. (apply throw key error rest)))))
  280. (define (reference-available? repository ref)
  281. "Return true if REF, a reference such as '(commit . \"cabba9e\"), is
  282. definitely available in REPOSITORY, false otherwise."
  283. (match ref
  284. (('commit . commit)
  285. (let ((len (string-length commit))
  286. (oid (string->oid commit)))
  287. (false-if-git-not-found
  288. (->bool (if (< len 40)
  289. (object-lookup-prefix repository oid len OBJ-COMMIT)
  290. (commit-lookup repository oid))))))
  291. (_
  292. #f)))
  293. (define cached-checkout-expiration
  294. ;; Return the expiration time procedure for a cached checkout.
  295. ;; TODO: Honor $GUIX_GIT_CACHE_EXPIRATION.
  296. ;; Use the mtime rather than the atime to cope with file systems mounted
  297. ;; with 'noatime'.
  298. (file-expiration-time (* 90 24 3600) stat:mtime))
  299. (define %checkout-cache-cleanup-period
  300. ;; Period for the removal of expired cached checkouts.
  301. (* 5 24 3600))
  302. (define (delete-checkout directory)
  303. "Delete DIRECTORY recursively, in an atomic fashion."
  304. (let ((trashed (string-append directory ".trashed")))
  305. (rename-file directory trashed)
  306. (delete-file-recursively trashed)))
  307. (define* (update-cached-checkout url
  308. #:key
  309. (ref '(branch . "master"))
  310. recursive?
  311. (check-out? #t)
  312. starting-commit
  313. (log-port (%make-void-port "w"))
  314. (cache-directory
  315. (url-cache-directory
  316. url (%repository-cache-directory)
  317. #:recursive? recursive?)))
  318. "Update the cached checkout of URL to REF in CACHE-DIRECTORY. Return three
  319. values: the cache directory name, and the SHA1 commit (a string) corresponding
  320. to REF, and the relation of the new commit relative to STARTING-COMMIT (if
  321. provided) as returned by 'commit-relation'.
  322. REF is pair whose key is [branch | commit | tag | tag-or-commit ] and value
  323. the associated data: [<branch name> | <sha1> | <tag name> | <string>].
  324. When RECURSIVE? is true, check out submodules as well, if any.
  325. When CHECK-OUT? is true, reset the cached working tree to REF; otherwise leave
  326. it unchanged."
  327. (define (cache-entries directory)
  328. (filter-map (match-lambda
  329. ((or "." "..")
  330. #f)
  331. (file
  332. (string-append directory "/" file)))
  333. (or (scandir directory) '())))
  334. (define canonical-ref
  335. ;; We used to require callers to specify "origin/" for each branch, which
  336. ;; made little sense since the cache should be transparent to them. So
  337. ;; here we append "origin/" if it's missing and otherwise keep it.
  338. (match ref
  339. (('branch . branch)
  340. `(branch . ,(if (string-prefix? "origin/" branch)
  341. branch
  342. (string-append "origin/" branch))))
  343. (_ ref)))
  344. (with-libgit2
  345. (let* ((cache-exists? (openable-repository? cache-directory))
  346. (repository (if cache-exists?
  347. (repository-open cache-directory)
  348. (clone* url cache-directory))))
  349. ;; Only fetch remote if it has not been cloned just before.
  350. (when (and cache-exists?
  351. (not (reference-available? repository ref)))
  352. (let ((auth-method (%make-auth-ssh-agent)))
  353. (remote-fetch (remote-lookup repository "origin")
  354. #:fetch-options (make-default-fetch-options))))
  355. (when recursive?
  356. (update-submodules repository #:log-port log-port))
  357. ;; Note: call 'commit-relation' from here because it's more efficient
  358. ;; than letting users re-open the checkout later on.
  359. (let* ((oid (if check-out?
  360. (switch-to-ref repository canonical-ref)
  361. (object-id
  362. (resolve-reference repository canonical-ref))))
  363. (new (and starting-commit
  364. (commit-lookup repository oid)))
  365. (old (and starting-commit
  366. (false-if-git-not-found
  367. (commit-lookup repository
  368. (string->oid starting-commit)))))
  369. (relation (and starting-commit
  370. (if old
  371. (commit-relation old new)
  372. 'unrelated))))
  373. ;; Reclaim file descriptors and memory mappings associated with
  374. ;; REPOSITORY as soon as possible.
  375. (repository-close! repository)
  376. ;; When CACHE-DIRECTORY is a sub-directory of the default cache
  377. ;; directory, remove expired checkouts that are next to it.
  378. (let ((parent (dirname cache-directory)))
  379. (when (string=? parent (%repository-cache-directory))
  380. (maybe-remove-expired-cache-entries parent cache-entries
  381. #:entry-expiration
  382. cached-checkout-expiration
  383. #:delete-entry delete-checkout
  384. #:cleanup-period
  385. %checkout-cache-cleanup-period)))
  386. (values cache-directory (oid->string oid) relation)))))
  387. (define* (latest-repository-commit store url
  388. #:key
  389. recursive?
  390. (log-port (%make-void-port "w"))
  391. (cache-directory
  392. (%repository-cache-directory))
  393. (ref '(branch . "master")))
  394. "Return two values: the content of the git repository at URL copied into a
  395. store directory and the sha1 of the top level commit in this directory. The
  396. reference to be checkout, once the repository is fetched, is specified by REF.
  397. REF is pair whose key is [branch | commit | tag] and value the associated
  398. data, respectively [<branch name> | <sha1> | <tag name>].
  399. When RECURSIVE? is true, check out submodules as well, if any.
  400. Git repositories are kept in the cache directory specified by
  401. %repository-cache-directory parameter.
  402. Log progress and checkout info to LOG-PORT."
  403. (define (dot-git? file stat)
  404. (and (string=? (basename file) ".git")
  405. (or (eq? 'directory (stat:type stat))
  406. ;; Submodule checkouts end up with a '.git' regular file that
  407. ;; contains metadata about where their actual '.git' directory
  408. ;; lives.
  409. (and recursive?
  410. (eq? 'regular (stat:type stat))))))
  411. (format log-port "updating checkout of '~a'...~%" url)
  412. (let*-values
  413. (((checkout commit _)
  414. (update-cached-checkout url
  415. #:recursive? recursive?
  416. #:ref ref
  417. #:cache-directory
  418. (url-cache-directory url cache-directory
  419. #:recursive?
  420. recursive?)
  421. #:log-port log-port))
  422. ((name)
  423. (url+commit->name url commit)))
  424. (format log-port "retrieved commit ~a~%" commit)
  425. (values (add-to-store store name #t "sha256" checkout
  426. #:select? (negate dot-git?))
  427. commit)))
  428. (define (print-git-error port key args default-printer)
  429. (match args
  430. (((? git-error? error) . _)
  431. (format port (G_ "Git error: ~a~%")
  432. (git-error-message error)))))
  433. (set-exception-printer! 'git-error print-git-error)
  434. ;;;
  435. ;;; Commit difference.
  436. ;;;
  437. (define* (commit-closure commit #:optional (visited (setq)))
  438. "Return the closure of COMMIT as a set. Skip commits contained in VISITED,
  439. a set, and adjoin VISITED to the result."
  440. (let loop ((commits (list commit))
  441. (visited visited))
  442. (match commits
  443. (()
  444. visited)
  445. ((head . tail)
  446. (if (set-contains? visited head)
  447. (loop tail visited)
  448. (loop (append (commit-parents head) tail)
  449. (set-insert head visited)))))))
  450. (define* (commit-difference new old #:optional (excluded '()))
  451. "Return the list of commits between NEW and OLD, where OLD is assumed to be
  452. an ancestor of NEW. Exclude all the commits listed in EXCLUDED along with
  453. their ancestors.
  454. Essentially, this computes the set difference between the closure of NEW and
  455. that of OLD."
  456. (let loop ((commits (list new))
  457. (result '())
  458. (visited (fold commit-closure
  459. (setq)
  460. (cons old excluded))))
  461. (match commits
  462. (()
  463. (reverse result))
  464. ((head . tail)
  465. (if (set-contains? visited head)
  466. (loop tail result visited)
  467. (loop (append (commit-parents head) tail)
  468. (cons head result)
  469. (set-insert head visited)))))))
  470. (define (commit-relation old new)
  471. "Return a symbol denoting the relation between OLD and NEW, two commit
  472. objects: 'ancestor (meaning that OLD is an ancestor of NEW), 'descendant, or
  473. 'unrelated, or 'self (OLD and NEW are the same commit)."
  474. (if (eq? old new)
  475. 'self
  476. (let ((newest (commit-closure new)))
  477. (if (set-contains? newest old)
  478. 'ancestor
  479. (let* ((seen (list->setq (commit-parents new)))
  480. (oldest (commit-closure old seen)))
  481. (if (set-contains? oldest new)
  482. 'descendant
  483. 'unrelated))))))
  484. ;;;
  485. ;;; Checkouts.
  486. ;;;
  487. ;; Representation of the "latest" checkout of a branch or a specific commit.
  488. (define-record-type* <git-checkout>
  489. git-checkout make-git-checkout
  490. git-checkout?
  491. (url git-checkout-url)
  492. (branch git-checkout-branch (default "master"))
  493. (commit git-checkout-commit (default #f)) ;#f | tag | commit
  494. (recursive? git-checkout-recursive? (default #f)))
  495. (define* (latest-repository-commit* url #:key ref recursive? log-port)
  496. ;; Monadic variant of 'latest-repository-commit'.
  497. (lambda (store)
  498. ;; The caller--e.g., (guix scripts build)--may not handle 'git-error' so
  499. ;; translate it into '&message' conditions that we know will be properly
  500. ;; handled.
  501. (catch 'git-error
  502. (lambda ()
  503. (values (latest-repository-commit store url
  504. #:ref ref
  505. #:recursive? recursive?
  506. #:log-port log-port)
  507. store))
  508. (lambda (key error . _)
  509. (raise (condition
  510. (&message
  511. (message
  512. (match ref
  513. (('commit . commit)
  514. (format #f (G_ "cannot fetch commit ~a from ~a: ~a")
  515. commit url (git-error-message error)))
  516. (('branch . branch)
  517. (format #f (G_ "cannot fetch branch '~a' from ~a: ~a")
  518. branch url (git-error-message error)))
  519. (_
  520. (format #f (G_ "Git failure while fetching ~a: ~a")
  521. url (git-error-message error))))))))))))
  522. (define-gexp-compiler (git-checkout-compiler (checkout <git-checkout>)
  523. system target)
  524. ;; "Compile" CHECKOUT by updating the local checkout and adding it to the
  525. ;; store.
  526. (match checkout
  527. (($ <git-checkout> url branch commit recursive?)
  528. (latest-repository-commit* url
  529. #:ref (if commit
  530. `(tag-or-commit . ,commit)
  531. `(branch . ,branch))
  532. #:recursive? recursive?
  533. #:log-port (current-error-port)))))
  534. ;; Local Variables:
  535. ;; eval: (put 'with-repository 'scheme-indent-function 2)
  536. ;; End: