git.scm 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017, 2020 Mathieu Othacehe <m.othacehe@gmail.com>
  3. ;;; Copyright © 2018-2022 Ludovic Courtès <ludo@gnu.org>
  4. ;;; Copyright © 2021 Kyle Meyer <kyle@kyleam.com>
  5. ;;; Copyright © 2021 Marius Bakke <marius@gnu.org>
  6. ;;; Copyright © 2022 Maxime Devos <maximedevos@telenet.be>
  7. ;;; Copyright © 2023 Tobias Geerinckx-Rice <me@tobias.gr>
  8. ;;;
  9. ;;; This file is part of GNU Guix.
  10. ;;;
  11. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  12. ;;; under the terms of the GNU General Public License as published by
  13. ;;; the Free Software Foundation; either version 3 of the License, or (at
  14. ;;; your option) any later version.
  15. ;;;
  16. ;;; GNU Guix is distributed in the hope that it will be useful, but
  17. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;;; GNU General Public License for more details.
  20. ;;;
  21. ;;; You should have received a copy of the GNU General Public License
  22. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  23. (define-module (guix git)
  24. #:use-module (git)
  25. #:use-module (guix i18n)
  26. #:use-module (guix base32)
  27. #:use-module (guix cache)
  28. #:use-module (gcrypt hash)
  29. #:use-module ((guix build utils)
  30. #:select (mkdir-p delete-file-recursively))
  31. #:use-module (guix store)
  32. #:use-module (guix utils)
  33. #:use-module (guix records)
  34. #:use-module (guix gexp)
  35. #:autoload (guix git-download)
  36. (git-reference-url git-reference-commit git-reference-recursive?)
  37. #:use-module (guix sets)
  38. #:use-module ((guix diagnostics) #:select (leave warning))
  39. #:use-module (guix progress)
  40. #:autoload (guix swh) (swh-download commit-id?)
  41. #:use-module (rnrs bytevectors)
  42. #:use-module (ice-9 format)
  43. #:use-module (ice-9 match)
  44. #:use-module (ice-9 ftw)
  45. #:use-module (srfi srfi-1)
  46. #:use-module (srfi srfi-11)
  47. #:use-module (srfi srfi-26)
  48. #:use-module (srfi srfi-34)
  49. #:use-module (srfi srfi-35)
  50. #:export (%repository-cache-directory
  51. honor-system-x509-certificates!
  52. url-cache-directory
  53. with-repository
  54. with-git-error-handling
  55. false-if-git-not-found
  56. update-cached-checkout
  57. url+commit->name
  58. latest-repository-commit
  59. commit-difference
  60. commit-relation
  61. commit-descendant?
  62. commit-id?
  63. remote-refs
  64. git-checkout
  65. git-checkout?
  66. git-checkout-url
  67. git-checkout-branch
  68. git-checkout-commit
  69. git-checkout-recursive?
  70. git-reference->git-checkout))
  71. (define %repository-cache-directory
  72. (make-parameter (string-append (cache-directory #:ensure? #f)
  73. "/checkouts")))
  74. (define (honor-system-x509-certificates!)
  75. "Use the system's X.509 certificates for Git checkouts over HTTPS. Honor
  76. the 'SSL_CERT_FILE' and 'SSL_CERT_DIR' environment variables."
  77. ;; On distros such as CentOS 7, /etc/ssl/certs contains only a couple of
  78. ;; files (instead of all the certificates) among which "ca-bundle.crt". On
  79. ;; other distros /etc/ssl/certs usually contains the whole set of
  80. ;; certificates along with "ca-certificates.crt". Try to choose the right
  81. ;; one.
  82. (let ((file (letrec-syntax ((choose
  83. (syntax-rules ()
  84. ((_ file rest ...)
  85. (let ((f file))
  86. (if (and f (file-exists? f))
  87. f
  88. (choose rest ...))))
  89. ((_)
  90. #f))))
  91. (choose (getenv "SSL_CERT_FILE")
  92. "/etc/ssl/certs/ca-certificates.crt"
  93. "/etc/ssl/certs/ca-bundle.crt")))
  94. (directory (or (getenv "SSL_CERT_DIR") "/etc/ssl/certs")))
  95. (and (or file
  96. (and=> (stat directory #f)
  97. (lambda (st)
  98. (> (stat:nlink st) 2))))
  99. (begin
  100. (set-tls-certificate-locations! directory file)
  101. #t))))
  102. (define %certificates-initialized?
  103. ;; Whether 'honor-system-x509-certificates!' has already been called.
  104. #f)
  105. (define-syntax-rule (with-libgit2 thunk ...)
  106. (begin
  107. ;; XXX: The right thing to do would be to call (libgit2-shutdown) here,
  108. ;; but pointer finalizers used in guile-git may be called after shutdown,
  109. ;; resulting in a segfault. Hence, let's skip shutdown call for now.
  110. (libgit2-init!)
  111. (unless %certificates-initialized?
  112. (honor-system-x509-certificates!)
  113. (set! %certificates-initialized? #t))
  114. thunk ...))
  115. (define* (url-cache-directory url
  116. #:optional (cache-directory
  117. (%repository-cache-directory))
  118. #:key recursive?)
  119. "Return the directory associated to URL in %repository-cache-directory."
  120. (string-append
  121. cache-directory "/"
  122. (bytevector->base32-string
  123. (sha256 (string->utf8 (if recursive?
  124. (string-append "R:" url)
  125. url))))))
  126. (define (show-progress progress)
  127. "Display a progress bar as we fetch Git code. PROGRESS is an
  128. <indexer-progress> record from (git)."
  129. (define total
  130. (indexer-progress-total-objects progress))
  131. (define-values (done label)
  132. (if (< (indexer-progress-received-objects progress) total)
  133. (values (indexer-progress-received-objects progress)
  134. (G_ "receiving objects"))
  135. (values (indexer-progress-indexed-objects progress)
  136. (G_ "indexing objects"))))
  137. (define %
  138. (* 100. (/ done total)))
  139. ;; TODO: Both should be handled & exposed by the PROGRESS-BAR API instead.
  140. (define width
  141. (max (- (current-terminal-columns)
  142. (string-length label) 7)
  143. 3))
  144. (define grain
  145. (match (quotient total (max 100 (* 8 width))) ; assume 1/8 glyph resolution
  146. (0 1)
  147. (x x)))
  148. (when (and (< % 100) (zero? (modulo done grain)))
  149. (erase-current-line (current-error-port))
  150. (format (current-error-port) "~a ~3,d% ~a"
  151. label (inexact->exact (round %))
  152. (progress-bar % width))
  153. (force-output (current-error-port)))
  154. (when (= % 100.)
  155. ;; We're done, erase the line.
  156. (erase-current-line (current-error-port))
  157. (force-output (current-error-port)))
  158. ;; Return true to indicate that we should go on.
  159. #t)
  160. (define (make-default-fetch-options)
  161. "Return the default fetch options."
  162. (let ((auth-method (%make-auth-ssh-agent)))
  163. ;; The #:transfer-progress and #:proxy-url options appeared in Guile-Git
  164. ;; 0.4.0. Omit them when using an older version.
  165. (catch 'wrong-number-of-args
  166. (lambda ()
  167. (make-fetch-options auth-method
  168. ;; Guile-Git doesn't distinguish between these.
  169. #:proxy-url (or (getenv "http_proxy")
  170. (getenv "https_proxy"))
  171. #:transfer-progress
  172. (and (isatty? (current-error-port))
  173. show-progress)))
  174. (lambda args
  175. (make-fetch-options auth-method)))))
  176. (define GITERR_HTTP
  177. ;; Guile-Git <= 0.5.2 lacks this constant.
  178. (let ((errors (resolve-interface '(git errors))))
  179. (if (module-defined? errors 'GITERR_HTTP)
  180. (module-ref errors 'GITERR_HTTP)
  181. 34)))
  182. (define (clone* url directory)
  183. "Clone git repository at URL into DIRECTORY. Upon failure,
  184. make sure no empty directory is left behind."
  185. (with-throw-handler #t
  186. (lambda ()
  187. (mkdir-p directory)
  188. (clone url directory
  189. (make-clone-options
  190. #:fetch-options (make-default-fetch-options))))
  191. (lambda _
  192. (false-if-exception (rmdir directory)))))
  193. (define (url+commit->name url sha1)
  194. "Return the string \"<REPO-NAME>-<SHA1:7>\" where REPO-NAME is the name of
  195. the git repository, extracted from URL and SHA1:7 the seven first digits
  196. of SHA1 string."
  197. (string-append
  198. (string-replace-substring
  199. (last (string-split url #\/)) ".git" "")
  200. "-" (string-take sha1 7)))
  201. (define (commit-id? str)
  202. "Return true if STR is likely a Git commit ID, false otherwise---e.g., if it
  203. is a tag name. This is based on a simple heuristic so use with care!"
  204. (and (= (string-length str) 40)
  205. (string-every char-set:hex-digit str)))
  206. (define (resolve-reference repository ref)
  207. "Resolve the branch, commit or tag specified by REF, and return the
  208. corresponding Git object."
  209. (let resolve ((ref ref))
  210. (match ref
  211. (('branch . branch)
  212. (let ((oid (reference-target
  213. (branch-lookup repository branch BRANCH-REMOTE))))
  214. (object-lookup repository oid)))
  215. (('symref . symref)
  216. (let ((oid (reference-name->oid repository symref)))
  217. (object-lookup repository oid)))
  218. (('commit . commit)
  219. (let ((len (string-length commit)))
  220. ;; 'object-lookup-prefix' appeared in Guile-Git in Mar. 2018, so we
  221. ;; can't be sure it's available. Furthermore, 'string->oid' used to
  222. ;; read out-of-bounds when passed a string shorter than 40 chars,
  223. ;; which is why we delay calls to it below.
  224. (if (< len 40)
  225. (object-lookup-prefix repository (string->oid commit) len)
  226. (object-lookup repository (string->oid commit)))))
  227. (('tag-or-commit . str)
  228. (cond ((and (string-contains str "-g")
  229. (match (string-split str #\-)
  230. ((version ... revision g+commit)
  231. (if (and (> (string-length g+commit) 4)
  232. (string-every char-set:digit revision)
  233. (string-every char-set:hex-digit
  234. (string-drop g+commit 1)))
  235. ;; Looks like a 'git describe' style ID, like
  236. ;; v1.3.0-7-gaa34d4d28d.
  237. (string-drop g+commit 1)
  238. #f))
  239. (_ #f)))
  240. => (lambda (commit) (resolve `(commit . ,commit))))
  241. ((or (> (string-length str) 40)
  242. (not (string-every char-set:hex-digit str)))
  243. (resolve `(tag . ,str))) ;definitely a tag
  244. (else
  245. (catch 'git-error
  246. (lambda ()
  247. (resolve `(tag . ,str)))
  248. (lambda _
  249. ;; There's no such tag, so it must be a commit ID.
  250. (resolve `(commit . ,str)))))))
  251. (('tag . tag)
  252. (let* ((oid (reference-name->oid repository
  253. (string-append "refs/tags/" tag)))
  254. (obj (object-lookup repository oid)))
  255. ;; OID may designate an "annotated tag" object or a "commit" object.
  256. ;; Return the commit object in both cases.
  257. (if (= OBJ-TAG (object-type obj))
  258. (object-lookup repository
  259. (tag-target-id (tag-lookup repository oid)))
  260. obj))))))
  261. (define (switch-to-ref repository ref)
  262. "Switch to REPOSITORY's branch, commit or tag specified by REF. Return the
  263. OID (roughly the commit hash) corresponding to REF."
  264. (define obj
  265. (resolve-reference repository ref))
  266. (reset repository obj RESET_HARD)
  267. (object-id obj))
  268. (define (call-with-repository directory proc)
  269. (let ((repository #f))
  270. (dynamic-wind
  271. (lambda ()
  272. (set! repository (repository-open directory)))
  273. (lambda ()
  274. (proc repository))
  275. (lambda ()
  276. (repository-close! repository)))))
  277. (define-syntax-rule (with-repository directory repository exp ...)
  278. "Open the repository at DIRECTORY and bind REPOSITORY to it within the
  279. dynamic extent of EXP."
  280. (call-with-repository directory
  281. (lambda (repository) exp ...)))
  282. (define (report-git-error error)
  283. "Report the given Guile-Git error."
  284. ;; Prior to Guile-Git commit b6b2760c2fd6dfaa5c0fedb43eeaff06166b3134,
  285. ;; errors would be represented by integers.
  286. (match error
  287. ((? integer? error) ;old Guile-Git
  288. (leave (G_ "Git error ~a~%") error))
  289. ((? git-error? error) ;new Guile-Git
  290. (leave (G_ "Git error: ~a~%") (git-error-message error)))))
  291. (define-syntax-rule (with-git-error-handling body ...)
  292. (catch 'git-error
  293. (lambda ()
  294. body ...)
  295. (lambda (key err)
  296. (report-git-error err))))
  297. (define* (update-submodules repository
  298. #:key (log-port (current-error-port))
  299. (fetch-options #f))
  300. "Update the submodules of REPOSITORY, a Git repository object."
  301. (for-each (lambda (name)
  302. (let ((submodule (submodule-lookup repository name)))
  303. (format log-port (G_ "updating submodule '~a'...~%")
  304. name)
  305. (submodule-update submodule
  306. #:fetch-options fetch-options)
  307. ;; Recurse in SUBMODULE.
  308. (let ((directory (string-append
  309. (repository-working-directory repository)
  310. "/" (submodule-path submodule))))
  311. (with-repository directory repository
  312. (update-submodules repository
  313. #:fetch-options fetch-options
  314. #:log-port log-port)))))
  315. (repository-submodules repository)))
  316. (define-syntax-rule (false-if-git-not-found exp)
  317. "Evaluate EXP, returning #false if a GIT_ENOTFOUND error is raised."
  318. (catch 'git-error
  319. (lambda ()
  320. exp)
  321. (lambda (key error . rest)
  322. (if (= GIT_ENOTFOUND (git-error-code error))
  323. #f
  324. (apply throw key error rest)))))
  325. (define (reference-available? repository ref)
  326. "Return true if REF, a reference such as '(commit . \"cabba9e\"), is
  327. definitely available in REPOSITORY, false otherwise."
  328. (match ref
  329. ((or ('commit . commit)
  330. ('tag-or-commit . (? commit-id? commit)))
  331. (let ((len (string-length commit))
  332. (oid (string->oid commit)))
  333. (false-if-git-not-found
  334. (->bool (if (< len 40)
  335. (object-lookup-prefix repository oid len OBJ-COMMIT)
  336. (commit-lookup repository oid))))))
  337. (_
  338. #f)))
  339. (define (clone-from-swh url tag-or-commit output)
  340. "Attempt to clone TAG-OR-COMMIT (a string), which originates from URL, using
  341. a copy archived at Software Heritage."
  342. (call-with-temporary-directory
  343. (lambda (bare)
  344. (and (swh-download url tag-or-commit bare
  345. #:archive-type 'git-bare)
  346. (let ((repository (clone* bare output)))
  347. (remote-set-url! repository "origin" url)
  348. repository)))))
  349. (define (clone/swh-fallback url ref cache-directory)
  350. "Like 'clone', but fallback to Software Heritage if the repository cannot be
  351. found at URL."
  352. (define (inaccessible-url-error? err)
  353. (let ((class (git-error-class err))
  354. (code (git-error-code err)))
  355. (or (= class GITERR_HTTP) ;404 or similar
  356. (= class GITERR_NET)))) ;unknown host, etc.
  357. (catch 'git-error
  358. (lambda ()
  359. (clone* url cache-directory))
  360. (lambda (key err)
  361. (match ref
  362. (((or 'commit 'tag-or-commit) . commit)
  363. (if (inaccessible-url-error? err)
  364. (or (clone-from-swh url commit cache-directory)
  365. (begin
  366. (warning (G_ "revision ~a of ~a \
  367. could not be fetched from Software Heritage~%")
  368. commit url)
  369. (throw key err)))
  370. (throw key err)))
  371. (_ (throw key err))))))
  372. (define cached-checkout-expiration
  373. ;; Return the expiration time procedure for a cached checkout.
  374. ;; TODO: Honor $GUIX_GIT_CACHE_EXPIRATION.
  375. ;; Use the mtime rather than the atime to cope with file systems mounted
  376. ;; with 'noatime'.
  377. (file-expiration-time (* 90 24 3600) stat:mtime))
  378. (define %checkout-cache-cleanup-period
  379. ;; Period for the removal of expired cached checkouts.
  380. (* 5 24 3600))
  381. (define (delete-checkout directory)
  382. "Delete DIRECTORY recursively, in an atomic fashion."
  383. (let ((trashed (string-append directory ".trashed")))
  384. (rename-file directory trashed)
  385. (delete-file-recursively trashed)))
  386. (define* (update-cached-checkout url
  387. #:key
  388. (ref '())
  389. recursive?
  390. (check-out? #t)
  391. starting-commit
  392. (log-port (%make-void-port "w"))
  393. (cache-directory
  394. (url-cache-directory
  395. url (%repository-cache-directory)
  396. #:recursive? recursive?)))
  397. "Update the cached checkout of URL to REF in CACHE-DIRECTORY. Return three
  398. values: the cache directory name, and the SHA1 commit (a string) corresponding
  399. to REF, and the relation of the new commit relative to STARTING-COMMIT (if
  400. provided) as returned by 'commit-relation'.
  401. REF is pair whose key is [branch | commit | tag | tag-or-commit ] and value
  402. the associated data: [<branch name> | <sha1> | <tag name> | <string>].
  403. If REF is the empty list, the remote HEAD is used.
  404. When RECURSIVE? is true, check out submodules as well, if any.
  405. When CHECK-OUT? is true, reset the cached working tree to REF; otherwise leave
  406. it unchanged."
  407. (define (cache-entries directory)
  408. (filter-map (match-lambda
  409. ((or "." "..")
  410. #f)
  411. (file
  412. (string-append directory "/" file)))
  413. (or (scandir directory) '())))
  414. (define canonical-ref
  415. ;; We used to require callers to specify "origin/" for each branch, which
  416. ;; made little sense since the cache should be transparent to them. So
  417. ;; here we append "origin/" if it's missing and otherwise keep it.
  418. (match ref
  419. (() '(symref . "refs/remotes/origin/HEAD"))
  420. (('branch . branch)
  421. `(branch . ,(if (string-prefix? "origin/" branch)
  422. branch
  423. (string-append "origin/" branch))))
  424. (_ ref)))
  425. (with-libgit2
  426. (let* ((cache-exists? (openable-repository? cache-directory))
  427. (repository (if cache-exists?
  428. (repository-open cache-directory)
  429. (clone/swh-fallback url ref cache-directory))))
  430. ;; Only fetch remote if it has not been cloned just before.
  431. (when (and cache-exists?
  432. (not (reference-available? repository ref)))
  433. (remote-fetch (remote-lookup repository "origin")
  434. #:fetch-options (make-default-fetch-options)))
  435. (when recursive?
  436. (update-submodules repository #:log-port log-port
  437. #:fetch-options (make-default-fetch-options)))
  438. ;; Note: call 'commit-relation' from here because it's more efficient
  439. ;; than letting users re-open the checkout later on.
  440. (let* ((oid (if check-out?
  441. (switch-to-ref repository canonical-ref)
  442. (object-id
  443. (resolve-reference repository canonical-ref))))
  444. (new (and starting-commit
  445. (commit-lookup repository oid)))
  446. (old (and starting-commit
  447. (false-if-git-not-found
  448. (commit-lookup repository
  449. (string->oid starting-commit)))))
  450. (relation (and starting-commit
  451. (if old
  452. (commit-relation old new)
  453. 'unrelated))))
  454. ;; Reclaim file descriptors and memory mappings associated with
  455. ;; REPOSITORY as soon as possible.
  456. (repository-close! repository)
  457. ;; Update CACHE-DIRECTORY's mtime to so the cache logic sees it.
  458. (match (gettimeofday)
  459. ((seconds . microseconds)
  460. (let ((nanoseconds (* 1000 microseconds)))
  461. (utime cache-directory
  462. seconds seconds
  463. nanoseconds nanoseconds))))
  464. ;; When CACHE-DIRECTORY is a sub-directory of the default cache
  465. ;; directory, remove expired checkouts that are next to it.
  466. (let ((parent (dirname cache-directory)))
  467. (when (string=? parent (%repository-cache-directory))
  468. (maybe-remove-expired-cache-entries parent cache-entries
  469. #:entry-expiration
  470. cached-checkout-expiration
  471. #:delete-entry delete-checkout
  472. #:cleanup-period
  473. %checkout-cache-cleanup-period)))
  474. (values cache-directory (oid->string oid) relation)))))
  475. (define* (latest-repository-commit store url
  476. #:key
  477. recursive?
  478. (log-port (%make-void-port "w"))
  479. (cache-directory
  480. (%repository-cache-directory))
  481. (ref '()))
  482. "Return two values: the content of the git repository at URL copied into a
  483. store directory and the sha1 of the top level commit in this directory. The
  484. reference to be checkout, once the repository is fetched, is specified by REF.
  485. REF is pair whose key is [branch | commit | tag] and value the associated
  486. data, respectively [<branch name> | <sha1> | <tag name>]. If REF is the empty
  487. list, the remote HEAD is used.
  488. When RECURSIVE? is true, check out submodules as well, if any.
  489. Git repositories are kept in the cache directory specified by
  490. %repository-cache-directory parameter.
  491. Log progress and checkout info to LOG-PORT."
  492. (define (dot-git? file stat)
  493. (and (string=? (basename file) ".git")
  494. (or (eq? 'directory (stat:type stat))
  495. ;; Submodule checkouts end up with a '.git' regular file that
  496. ;; contains metadata about where their actual '.git' directory
  497. ;; lives.
  498. (and recursive?
  499. (eq? 'regular (stat:type stat))))))
  500. (format log-port "updating checkout of '~a'...~%" url)
  501. (let*-values
  502. (((checkout commit _)
  503. (update-cached-checkout url
  504. #:recursive? recursive?
  505. #:ref ref
  506. #:cache-directory
  507. (url-cache-directory url cache-directory
  508. #:recursive?
  509. recursive?)
  510. #:log-port log-port))
  511. ((name)
  512. (url+commit->name url commit)))
  513. (format log-port "retrieved commit ~a~%" commit)
  514. (values (add-to-store store name #t "sha256" checkout
  515. #:select? (negate dot-git?))
  516. commit)))
  517. (define (print-git-error port key args default-printer)
  518. (match args
  519. (((? git-error? error) . _)
  520. (format port (G_ "Git error: ~a~%")
  521. (git-error-message error)))))
  522. (set-exception-printer! 'git-error print-git-error)
  523. ;;;
  524. ;;; Commit difference.
  525. ;;;
  526. (define* (commit-closure commit #:optional (visited (setq)))
  527. "Return the closure of COMMIT as a set. Skip commits contained in VISITED,
  528. a set, and adjoin VISITED to the result."
  529. (let loop ((commits (list commit))
  530. (visited visited))
  531. (match commits
  532. (()
  533. visited)
  534. ((head . tail)
  535. (if (set-contains? visited head)
  536. (loop tail visited)
  537. (loop (append (commit-parents head) tail)
  538. (set-insert head visited)))))))
  539. (define* (commit-difference new old #:optional (excluded '()))
  540. "Return the list of commits between NEW and OLD, where OLD is assumed to be
  541. an ancestor of NEW. Exclude all the commits listed in EXCLUDED along with
  542. their ancestors.
  543. Essentially, this computes the set difference between the closure of NEW and
  544. that of OLD."
  545. (let loop ((commits (list new))
  546. (result '())
  547. (visited (fold commit-closure
  548. (setq)
  549. (cons old excluded))))
  550. (match commits
  551. (()
  552. (reverse result))
  553. ((head . tail)
  554. (if (set-contains? visited head)
  555. (loop tail result visited)
  556. (loop (append (commit-parents head) tail)
  557. (cons head result)
  558. (set-insert head visited)))))))
  559. (define (commit-relation old new)
  560. "Return a symbol denoting the relation between OLD and NEW, two commit
  561. objects: 'ancestor (meaning that OLD is an ancestor of NEW), 'descendant, or
  562. 'unrelated, or 'self (OLD and NEW are the same commit)."
  563. (if (eq? old new)
  564. 'self
  565. (let ((newest (commit-closure new)))
  566. (if (set-contains? newest old)
  567. 'ancestor
  568. (let* ((seen (list->setq (commit-parents new)))
  569. (oldest (commit-closure old seen)))
  570. (if (set-contains? oldest new)
  571. 'descendant
  572. 'unrelated))))))
  573. (define (commit-descendant? new old)
  574. "Return true if NEW is the descendant of one of OLD, a list of commits.
  575. When the expected result is likely #t, this is faster than using
  576. 'commit-relation' since fewer commits need to be traversed."
  577. (let ((old (list->setq old)))
  578. (let loop ((commits (list new))
  579. (visited (setq)))
  580. (match commits
  581. (()
  582. #f)
  583. (_
  584. ;; Perform a breadth-first search as this is likely going to
  585. ;; terminate more quickly than a depth-first search.
  586. (let ((commits (remove (cut set-contains? visited <>) commits)))
  587. (or (any (cut set-contains? old <>) commits)
  588. (loop (append-map commit-parents commits)
  589. (fold set-insert visited commits)))))))))
  590. ;;
  591. ;;; Remote operations.
  592. ;;;
  593. (define* (remote-refs url #:key tags?)
  594. "Return the list of references advertised at Git repository URL. If TAGS?
  595. is true, limit to only refs/tags."
  596. (define (ref? ref)
  597. ;; Like `git ls-remote --refs', only show actual references.
  598. (and (string-prefix? "refs/" ref)
  599. (not (string-suffix? "^{}" ref))))
  600. (define (tag? ref)
  601. (string-prefix? "refs/tags/" ref))
  602. (define (include? ref)
  603. (and (ref? ref)
  604. (or (not tags?) (tag? ref))))
  605. (define (remote-head->ref remote)
  606. (let ((name (remote-head-name remote)))
  607. (and (include? name)
  608. name)))
  609. (with-libgit2
  610. (call-with-temporary-directory
  611. (lambda (cache-directory)
  612. (let* ((repository (repository-init cache-directory))
  613. ;; Create an in-memory remote so we don't touch disk.
  614. (remote (remote-create-anonymous repository url)))
  615. (remote-connect remote)
  616. (let* ((remote-heads (remote-ls remote))
  617. (refs (filter-map remote-head->ref remote-heads)))
  618. ;; Wait until we're finished with the repository before closing it.
  619. (remote-disconnect remote)
  620. (repository-close! repository)
  621. refs))))))
  622. ;;;
  623. ;;; Checkouts.
  624. ;;;
  625. ;; Representation of the "latest" checkout of a branch or a specific commit.
  626. (define-record-type* <git-checkout>
  627. git-checkout make-git-checkout
  628. git-checkout?
  629. (url git-checkout-url)
  630. (branch git-checkout-branch (default #f))
  631. (commit git-checkout-commit (default #f)) ;#f | tag | commit
  632. (recursive? git-checkout-recursive? (default #f)))
  633. (define (git-reference->git-checkout reference)
  634. "Convert the <git-reference> REFERENCE to an equivalent <git-checkout>."
  635. (git-checkout
  636. (url (git-reference-url reference))
  637. (commit (git-reference-commit reference))
  638. (recursive? (git-reference-recursive? reference))))
  639. (define* (latest-repository-commit* url #:key ref recursive? log-port)
  640. ;; Monadic variant of 'latest-repository-commit'.
  641. (lambda (store)
  642. ;; The caller--e.g., (guix scripts build)--may not handle 'git-error' so
  643. ;; translate it into '&message' conditions that we know will be properly
  644. ;; handled.
  645. (catch 'git-error
  646. (lambda ()
  647. (values (latest-repository-commit store url
  648. #:ref ref
  649. #:recursive? recursive?
  650. #:log-port log-port)
  651. store))
  652. (lambda (key error . _)
  653. (raise (condition
  654. (&message
  655. (message
  656. (match ref
  657. (('commit . commit)
  658. (format #f (G_ "cannot fetch commit ~a from ~a: ~a")
  659. commit url (git-error-message error)))
  660. (('branch . branch)
  661. (format #f (G_ "cannot fetch branch '~a' from ~a: ~a")
  662. branch url (git-error-message error)))
  663. (_
  664. (format #f (G_ "Git failure while fetching ~a: ~a")
  665. url (git-error-message error))))))))))))
  666. (define-gexp-compiler (git-checkout-compiler (checkout <git-checkout>)
  667. system target)
  668. ;; "Compile" CHECKOUT by updating the local checkout and adding it to the
  669. ;; store.
  670. (match checkout
  671. (($ <git-checkout> url branch commit recursive?)
  672. (latest-repository-commit* url
  673. #:ref (cond (commit
  674. `(tag-or-commit . ,commit))
  675. (branch
  676. `(branch . ,branch))
  677. (else '()))
  678. #:recursive? recursive?
  679. #:log-port (current-error-port)))))
  680. ;; Local Variables:
  681. ;; eval: (put 'with-repository 'scheme-indent-function 2)
  682. ;; End: