swh.scm 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018, 2019, 2020, 2021 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2020 Jakub Kądziołka <kuba@kadziolka.net>
  4. ;;; Copyright © 2021 Xinglu Chen <public@yoctocell.xyz>
  5. ;;;
  6. ;;; This file is part of GNU Guix.
  7. ;;;
  8. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  9. ;;; under the terms of the GNU General Public License as published by
  10. ;;; the Free Software Foundation; either version 3 of the License, or (at
  11. ;;; your option) any later version.
  12. ;;;
  13. ;;; GNU Guix is distributed in the hope that it will be useful, but
  14. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;;; GNU General Public License for more details.
  17. ;;;
  18. ;;; You should have received a copy of the GNU General Public License
  19. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  20. (define-module (guix swh)
  21. #:use-module (guix base16)
  22. #:use-module (guix build utils)
  23. #:use-module ((guix build syscalls) #:select (mkdtemp!))
  24. #:use-module (web uri)
  25. #:use-module (web client)
  26. #:use-module (web response)
  27. #:use-module (json)
  28. #:use-module (srfi srfi-1)
  29. #:use-module (srfi srfi-9)
  30. #:use-module (srfi srfi-11)
  31. #:use-module (srfi srfi-19)
  32. #:use-module (ice-9 match)
  33. #:use-module (ice-9 regex)
  34. #:use-module (ice-9 popen)
  35. #:use-module ((ice-9 ftw) #:select (scandir))
  36. #:export (%swh-base-url
  37. %verify-swh-certificate?
  38. %allow-request?
  39. request-rate-limit-reached?
  40. origin?
  41. origin-type
  42. origin-url
  43. origin-visits
  44. lookup-origin
  45. visit?
  46. visit-date
  47. visit-origin
  48. visit-url
  49. visit-snapshot-url
  50. visit-status
  51. visit-number
  52. visit-snapshot
  53. branch?
  54. branch-name
  55. branch-target
  56. release?
  57. release-id
  58. release-name
  59. release-message
  60. release-target
  61. revision?
  62. revision-id
  63. revision-date
  64. revision-directory
  65. lookup-revision
  66. lookup-origin-revision
  67. content?
  68. content-checksums
  69. content-data-url
  70. content-length
  71. lookup-content
  72. directory-entry?
  73. directory-entry-name
  74. directory-entry-type
  75. directory-entry-checksums
  76. directory-entry-length
  77. directory-entry-permissions
  78. lookup-directory
  79. directory-entry-target
  80. save-reply?
  81. save-reply-origin-url
  82. save-reply-origin-type
  83. save-reply-request-date
  84. save-reply-request-status
  85. save-reply-task-status
  86. save-origin
  87. save-origin-status
  88. vault-reply?
  89. vault-reply-id
  90. vault-reply-fetch-url
  91. vault-reply-object-id
  92. vault-reply-object-type
  93. vault-reply-progress-message
  94. vault-reply-status
  95. query-vault
  96. request-cooking
  97. vault-fetch
  98. commit-id?
  99. swh-download-directory
  100. swh-download))
  101. ;;; Commentary:
  102. ;;;
  103. ;;; This module provides bindings to the HTTP interface of Software Heritage.
  104. ;;; It allows you to browse the archive, look up revisions (such as SHA1
  105. ;;; commit IDs), "origins" (code hosting URLs), content (files), etc. See
  106. ;;; <https://archive.softwareheritage.org/api/> for more information.
  107. ;;;
  108. ;;; The high-level 'swh-download' procedure allows you to download a Git
  109. ;;; revision from Software Heritage, provided it is available.
  110. ;;;
  111. ;;; Code:
  112. (define %swh-base-url
  113. ;; Presumably we won't need to change it.
  114. (make-parameter "https://archive.softwareheritage.org"))
  115. (define %verify-swh-certificate?
  116. ;; Whether to verify the X.509 HTTPS certificate for %SWH-BASE-URL.
  117. (make-parameter #t))
  118. (define (swh-url path . rest)
  119. ;; URLs returned by the API may be relative or absolute. This has changed
  120. ;; without notice before. Handle both cases by detecting whether the path
  121. ;; starts with a domain.
  122. (define root
  123. (if (string-prefix? "/" path)
  124. (string-append (%swh-base-url) path)
  125. path))
  126. (define url
  127. (string-append root (string-join rest "/" 'prefix)))
  128. ;; Ensure there's a trailing slash or we get a redirect.
  129. (if (string-suffix? "/" url)
  130. url
  131. (string-append url "/")))
  132. ;; XXX: Work around a bug in Guile 3.0.2 where #:verify-certificate? would
  133. ;; be ignored (<https://bugs.gnu.org/40486>).
  134. (define* (http-get* uri #:rest rest)
  135. (apply http-request uri #:method 'GET rest))
  136. (define* (http-post* uri #:rest rest)
  137. (apply http-request uri #:method 'POST rest))
  138. (define %date-regexp
  139. ;; Match strings like "2014-11-17T22:09:38+01:00" or
  140. ;; "2018-09-30T23:20:07.815449+00:00"".
  141. (make-regexp "^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})((\\.[0-9]+)?)([+-][0-9]{2}):([0-9]{2})$"))
  142. (define (string->date* str)
  143. "Return a SRFI-19 date parsed from STR, a date string as returned by
  144. Software Heritage."
  145. ;; We can't use 'string->date' because of the timezone format: SWH returns
  146. ;; "+01:00" when the '~z' template expects "+0100". So we roll our own!
  147. (or (and=> (regexp-exec %date-regexp str)
  148. (lambda (match)
  149. (define (ref n)
  150. (string->number (match:substring match n)))
  151. (make-date (let ((ns (match:substring match 8)))
  152. (if ns
  153. (string->number (string-drop ns 1))
  154. 0))
  155. (ref 6) (ref 5) (ref 4)
  156. (ref 3) (ref 2) (ref 1)
  157. (+ (* 3600 (ref 9)) ;time zone
  158. (if (< (ref 9) 0)
  159. (- (ref 10))
  160. (ref 10))))))
  161. str)) ;oops!
  162. (define string*
  163. ;; Converts "string or #nil" coming from JSON to "string or #f".
  164. (match-lambda
  165. ((? string? str) str)
  166. ((? null?) #f) ;Guile-JSON 3.x
  167. ('null #f))) ;Guile-JSON 4.x
  168. (define %allow-request?
  169. ;; Takes a URL and method (e.g., the 'http-get' procedure) and returns true
  170. ;; to keep going. This can be used to disallow requests when
  171. ;; 'request-rate-limit-reached?' returns true, for instance.
  172. (make-parameter (const #t)))
  173. ;; The time when the rate limit for "/origin/save" POST requests and that of
  174. ;; other requests will be reset.
  175. ;; See <https://archive.softwareheritage.org/api/#rate-limiting>.
  176. (define %save-rate-limit-reset-time 0)
  177. (define %general-rate-limit-reset-time 0)
  178. (define (request-rate-limit-reached? url method)
  179. "Return true if the rate limit has been reached for URI."
  180. (define uri
  181. (string->uri url))
  182. (define reset-time
  183. (if (and (eq? method http-post*)
  184. (string-prefix? "/api/1/origin/save/" (uri-path uri)))
  185. %save-rate-limit-reset-time
  186. %general-rate-limit-reset-time))
  187. (< (car (gettimeofday)) reset-time))
  188. (define (update-rate-limit-reset-time! url method response)
  189. "Update the rate limit reset time for URL and METHOD based on the headers in
  190. RESPONSE."
  191. (let ((uri (string->uri url)))
  192. (match (assq-ref (response-headers response) 'x-ratelimit-reset)
  193. ((= string->number (? number? reset))
  194. (if (and (eq? method http-post*)
  195. (string-prefix? "/api/1/origin/save/" (uri-path uri)))
  196. (set! %save-rate-limit-reset-time reset)
  197. (set! %general-rate-limit-reset-time reset)))
  198. (_
  199. #f))))
  200. (define* (call url decode #:optional (method http-get*)
  201. #:key (false-if-404? #t))
  202. "Invoke the endpoint at URL using METHOD. Decode the resulting JSON body
  203. using DECODE, a one-argument procedure that takes an input port. When
  204. FALSE-IF-404? is true, return #f upon 404 responses."
  205. (and ((%allow-request?) url method)
  206. (let*-values (((response port)
  207. (method url #:streaming? #t
  208. #:verify-certificate?
  209. (%verify-swh-certificate?))))
  210. ;; See <https://archive.softwareheritage.org/api/#rate-limiting>.
  211. (match (assq-ref (response-headers response) 'x-ratelimit-remaining)
  212. (#f #t)
  213. ((? (compose zero? string->number))
  214. (update-rate-limit-reset-time! url method response)
  215. (throw 'swh-error url method response))
  216. (_ #t))
  217. (cond ((= 200 (response-code response))
  218. (let ((result (decode port)))
  219. (close-port port)
  220. result))
  221. ((and false-if-404?
  222. (= 404 (response-code response)))
  223. (close-port port)
  224. #f)
  225. (else
  226. (close-port port)
  227. (throw 'swh-error url method response))))))
  228. (define-syntax define-query
  229. (syntax-rules (path)
  230. "Define a procedure that performs a Software Heritage query."
  231. ((_ (name args ...) docstring (path components ...)
  232. json->value)
  233. (define (name args ...)
  234. docstring
  235. (call (swh-url components ...) json->value)))))
  236. ;; <https://archive.softwareheritage.org/api/1/origin/https://github.com/guix-mirror/guix/get>
  237. (define-json-mapping <origin> make-origin origin?
  238. json->origin
  239. (visits-url origin-visits-url "origin_visits_url")
  240. (type origin-type)
  241. (url origin-url))
  242. ;; <https://archive.softwareheritage.org/api/1/origin/52181937/visits/>
  243. (define-json-mapping <visit> make-visit visit?
  244. json->visit
  245. (date visit-date "date" string->date*)
  246. (origin visit-origin)
  247. (url visit-url "origin_visit_url")
  248. (snapshot-url visit-snapshot-url "snapshot_url" string*) ;string | #f
  249. (status visit-status "status" string->symbol) ;'full | 'partial | 'ongoing
  250. (number visit-number "visit"))
  251. ;; <https://archive.softwareheritage.org/api/1/snapshot/4334c3ed4bb208604ed780d8687fe523837f1bd1/>
  252. (define-json-mapping <snapshot> make-snapshot snapshot?
  253. json->snapshot
  254. (branches snapshot-branches "branches" json->branches))
  255. ;; This is used for the "branches" field of snapshots.
  256. (define-record-type <branch>
  257. (make-branch name target-type target-url)
  258. branch?
  259. (name branch-name)
  260. (target-type branch-target-type) ;release | revision
  261. (target-url branch-target-url))
  262. (define (json->branches branches)
  263. (map (match-lambda
  264. ((key . value)
  265. (make-branch key
  266. (string->symbol
  267. (assoc-ref value "target_type"))
  268. (assoc-ref value "target_url"))))
  269. branches))
  270. ;; <https://archive.softwareheritage.org/api/1/release/1f44934fb6e2cefccbecd4fa347025349fa9ff76/>
  271. (define-json-mapping <release> make-release release?
  272. json->release
  273. (id release-id)
  274. (name release-name)
  275. (message release-message)
  276. (target-type release-target-type "target_type" string->symbol)
  277. (target-url release-target-url "target_url"))
  278. ;; <https://archive.softwareheritage.org/api/1/revision/359fdda40f754bbf1b5dc261e7427b75463b59be/>
  279. (define-json-mapping <revision> make-revision revision?
  280. json->revision
  281. (id revision-id)
  282. (date revision-date "date" string->date*)
  283. (directory revision-directory)
  284. (directory-url revision-directory-url "directory_url"))
  285. ;; <https://archive.softwareheritage.org/api/1/content/>
  286. (define-json-mapping <content> make-content content?
  287. json->content
  288. (checksums content-checksums "checksums" json->checksums)
  289. (data-url content-data-url "data_url")
  290. (file-type-url content-file-type-url "filetype_url")
  291. (language-url content-language-url "language_url")
  292. (length content-length)
  293. (license-url content-license-url "license_url"))
  294. (define (json->checksums checksums)
  295. (map (match-lambda
  296. ((key . value)
  297. (cons key (base16-string->bytevector value))))
  298. checksums))
  299. ;; <https://archive.softwareheritage.org/api/1/directory/27c69c5d298a43096a53affbf881e7b13f17bdcd/>
  300. (define-json-mapping <directory-entry> make-directory-entry directory-entry?
  301. json->directory-entry
  302. (name directory-entry-name)
  303. (type directory-entry-type "type"
  304. (match-lambda
  305. ("dir" 'directory)
  306. (str (string->symbol str))))
  307. (checksums directory-entry-checksums "checksums"
  308. (match-lambda
  309. (#f #f)
  310. ((? unspecified?) #f)
  311. (lst (json->checksums lst))))
  312. (id directory-entry-id "dir_id")
  313. (length directory-entry-length)
  314. (permissions directory-entry-permissions "perms")
  315. (target-url directory-entry-target-url "target_url"))
  316. ;; <https://archive.softwareheritage.org/api/1/origin/save/>
  317. (define-json-mapping <save-reply> make-save-reply save-reply?
  318. json->save-reply
  319. (origin-url save-reply-origin-url "origin_url")
  320. (origin-type save-reply-origin-type "origin_type")
  321. (request-date save-reply-request-date "save_request_date"
  322. string->date*)
  323. (request-status save-reply-request-status "save_request_status"
  324. string->symbol)
  325. (task-status save-reply-task-status "save_task_status"
  326. (match-lambda
  327. ("not created" 'not-created)
  328. ((? string? str) (string->symbol str)))))
  329. ;; <https://docs.softwareheritage.org/devel/swh-vault/api.html#vault-api-ref>
  330. (define-json-mapping <vault-reply> make-vault-reply vault-reply?
  331. json->vault-reply
  332. (id vault-reply-id)
  333. (fetch-url vault-reply-fetch-url "fetch_url")
  334. (object-id vault-reply-object-id "obj_id")
  335. (object-type vault-reply-object-type "obj_type" string->symbol)
  336. (progress-message vault-reply-progress-message "progress_message")
  337. (status vault-reply-status "status" string->symbol))
  338. ;;;
  339. ;;; RPCs.
  340. ;;;
  341. (define-query (lookup-origin url)
  342. "Return an origin for URL."
  343. (path "/api/1/origin" url "get")
  344. json->origin)
  345. (define-query (lookup-content hash type)
  346. "Return a content for HASH, of the given TYPE--e.g., \"sha256\"."
  347. (path "/api/1/content"
  348. (string-append type ":"
  349. (bytevector->base16-string hash)))
  350. json->content)
  351. (define-query (lookup-revision id)
  352. "Return the revision with the given ID, typically a Git commit SHA1."
  353. (path "/api/1/revision" id)
  354. json->revision)
  355. (define-query (lookup-directory id)
  356. "Return the directory with the given ID."
  357. (path "/api/1/directory" id)
  358. json->directory-entries)
  359. (define (json->directory-entries port)
  360. (map json->directory-entry
  361. (vector->list (json->scm port))))
  362. (define (origin-visits origin)
  363. "Return the list of visits of ORIGIN, a record as returned by
  364. 'lookup-origin'."
  365. (call (swh-url (origin-visits-url origin))
  366. (lambda (port)
  367. (map json->visit (vector->list (json->scm port))))))
  368. (define (visit-snapshot visit)
  369. "Return the snapshot corresponding to VISIT or #f if no snapshot is
  370. available."
  371. (and (visit-snapshot-url visit)
  372. (call (swh-url (visit-snapshot-url visit))
  373. json->snapshot)))
  374. (define (branch-target branch)
  375. "Return the target of BRANCH, either a <revision> or a <release>."
  376. (match (branch-target-type branch)
  377. ('release
  378. (call (swh-url (branch-target-url branch))
  379. json->release))
  380. ('revision
  381. (call (swh-url (branch-target-url branch))
  382. json->revision))))
  383. (define (lookup-origin-revision url tag)
  384. "Return a <revision> corresponding to the given TAG for the repository
  385. coming from URL. Example:
  386. (lookup-origin-revision \"https://github.com/guix-mirror/guix/\" \"v0.8\")
  387. => #<<revision> id: \"44941…\" …>
  388. The information is based on the latest visit of URL available. Return #f if
  389. URL could not be found."
  390. (match (lookup-origin url)
  391. (#f #f)
  392. (origin
  393. (match (filter (lambda (visit)
  394. ;; Return #f if (visit-snapshot VISIT) would return #f.
  395. (and (visit-snapshot-url visit)
  396. (eq? 'full (visit-status visit))))
  397. (origin-visits origin))
  398. ((visit . _)
  399. (let ((snapshot (visit-snapshot visit)))
  400. (match (and=> (find (lambda (branch)
  401. (or
  402. ;; Git specific.
  403. (string=? (string-append "refs/tags/" tag)
  404. (branch-name branch))
  405. ;; Hg specific.
  406. (string=? tag
  407. (branch-name branch))))
  408. (snapshot-branches snapshot))
  409. branch-target)
  410. ((? release? release)
  411. (release-target release))
  412. ((? revision? revision)
  413. revision)
  414. (#f ;tag not found
  415. #f))))
  416. (()
  417. #f)))))
  418. (define (release-target release)
  419. "Return the revision that is the target of RELEASE."
  420. (match (release-target-type release)
  421. ('revision
  422. (call (swh-url (release-target-url release))
  423. json->revision))))
  424. (define (directory-entry-target entry)
  425. "If ENTRY, a directory entry, has type 'directory, return its list of
  426. directory entries; if it has type 'file, return its <content> object."
  427. (call (swh-url (directory-entry-target-url entry))
  428. (match (directory-entry-type entry)
  429. ('file json->content)
  430. ('directory json->directory-entries))))
  431. (define* (save-origin url #:optional (type "git"))
  432. "Request URL to be saved."
  433. (call (swh-url "/api/1/origin/save" type "url" url) json->save-reply
  434. http-post*))
  435. (define-query (save-origin-status url type)
  436. "Return the status of a /save request for URL and TYPE (e.g., \"git\")."
  437. (path "/api/1/origin/save" type "url" url)
  438. json->save-reply)
  439. (define-query (query-vault id kind)
  440. "Ask the availability of object ID and KIND to the vault, where KIND is
  441. 'directory or 'revision. Return #f if it could not be found, or a
  442. <vault-reply> on success."
  443. ;; <https://docs.softwareheritage.org/devel/swh-vault/api.html#vault-api-ref>
  444. ;; There's a single format supported for directories and revisions and for
  445. ;; now, the "/format" bit of the URL *must* be omitted.
  446. (path "/api/1/vault" (symbol->string kind) id)
  447. json->vault-reply)
  448. (define (request-cooking id kind)
  449. "Request the cooking of object ID and KIND (one of 'directory or 'revision)
  450. to the vault. Return a <vault-reply>."
  451. (call (swh-url "/api/1/vault" (symbol->string kind) id)
  452. json->vault-reply
  453. http-post*))
  454. (define* (vault-fetch id kind
  455. #:key (log-port (current-error-port)))
  456. "Return an input port from which a bundle of the object with the given ID
  457. and KIND (one of 'directory or 'revision) can be retrieved, or #f if the
  458. object could not be found.
  459. For a directory, the returned stream is a gzip-compressed tarball. For a
  460. revision, it is a gzip-compressed stream for 'git fast-import'."
  461. (let loop ((reply (query-vault id kind)))
  462. (match reply
  463. (#f
  464. (and=> (request-cooking id kind) loop))
  465. (_
  466. (match (vault-reply-status reply)
  467. ('done
  468. ;; Fetch the bundle.
  469. (let-values (((response port)
  470. (http-get* (swh-url (vault-reply-fetch-url reply))
  471. #:streaming? #t
  472. #:verify-certificate?
  473. (%verify-swh-certificate?))))
  474. (if (= (response-code response) 200)
  475. port
  476. (begin ;shouldn't happen
  477. (close-port port)
  478. #f))))
  479. ('failed
  480. ;; Upon failure, we're supposed to try again.
  481. (format log-port "SWH vault: failure: ~a~%"
  482. (vault-reply-progress-message reply))
  483. (format log-port "SWH vault: retrying...~%")
  484. (loop (request-cooking id kind)))
  485. ((and (or 'new 'pending) status)
  486. ;; Wait until the bundle shows up.
  487. (let ((message (vault-reply-progress-message reply)))
  488. (when (eq? 'new status)
  489. (format log-port "SWH vault: \
  490. requested bundle cooking, waiting for completion...~%"))
  491. (when (string? message)
  492. (format log-port "SWH vault: ~a~%" message))
  493. ;; Wait long enough so we don't exhaust our maximum number of
  494. ;; requests per hour too fast (as of this writing, the limit is 60
  495. ;; requests per hour per IP address.)
  496. (sleep (if (eq? status 'new) 60 30))
  497. (loop (query-vault id kind)))))))))
  498. ;;;
  499. ;;; High-level interface.
  500. ;;;
  501. (define (call-with-temporary-directory proc) ;FIXME: factorize
  502. "Call PROC with a name of a temporary directory; close the directory and
  503. delete it when leaving the dynamic extent of this call."
  504. (let* ((directory (or (getenv "TMPDIR") "/tmp"))
  505. (template (string-append directory "/guix-directory.XXXXXX"))
  506. (tmp-dir (mkdtemp! template)))
  507. (dynamic-wind
  508. (const #t)
  509. (lambda ()
  510. (proc tmp-dir))
  511. (lambda ()
  512. (false-if-exception (delete-file-recursively tmp-dir))))))
  513. (define* (swh-download-directory id output
  514. #:key (log-port (current-error-port)))
  515. "Download from Software Heritage the directory with the given ID, and
  516. unpack it to OUTPUT. Return #t on success and #f on failure"
  517. (call-with-temporary-directory
  518. (lambda (directory)
  519. (match (vault-fetch id 'directory #:log-port log-port)
  520. (#f
  521. (format log-port
  522. "SWH: directory ~a could not be fetched from the vault~%"
  523. id)
  524. #f)
  525. ((? port? input)
  526. (let ((tar (open-pipe* OPEN_WRITE "tar" "-C" directory "-xzvf" "-")))
  527. (dump-port input tar)
  528. (close-port input)
  529. (let ((status (close-pipe tar)))
  530. (unless (zero? status)
  531. (error "tar extraction failure" status)))
  532. (match (scandir directory)
  533. (("." ".." sub-directory)
  534. (copy-recursively (string-append directory "/" sub-directory)
  535. output
  536. #:log (%make-void-port "w"))
  537. #t))))))))
  538. (define (commit-id? reference)
  539. "Return true if REFERENCE is likely a commit ID, false otherwise---e.g., if
  540. it is a tag name. This is based on a simple heuristic so use with care!"
  541. (and (= (string-length reference) 40)
  542. (string-every char-set:hex-digit reference)))
  543. (define* (swh-download url reference output
  544. #:key (log-port (current-error-port)))
  545. "Download from Software Heritage a checkout of the Git tag or commit
  546. REFERENCE originating from URL, and unpack it in OUTPUT. Return #t on success
  547. and #f on failure.
  548. This procedure uses the \"vault\", which contains \"cooked\" directories in
  549. the form of tarballs. If the requested directory is not cooked yet, it will
  550. wait until it becomes available, which could take several minutes."
  551. (match (if (commit-id? reference)
  552. (lookup-revision reference)
  553. (lookup-origin-revision url reference))
  554. ((? revision? revision)
  555. (format log-port "SWH: found revision ~a with directory at '~a'~%"
  556. (revision-id revision)
  557. (swh-url (revision-directory-url revision)))
  558. (swh-download-directory (revision-directory revision) output
  559. #:log-port log-port))
  560. (#f
  561. #f)))