swh.scm 23 KB

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