hg-download.scm 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2014, 2015, 2016, 2017, 2019 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2016 Ricardo Wurmus <rekado@elephly.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 hg-download)
  21. #:use-module (guix gexp)
  22. #:use-module (guix store)
  23. #:use-module (guix monads)
  24. #:use-module (guix records)
  25. #:use-module (guix modules)
  26. #:use-module (guix packages)
  27. #:autoload (guix build-system gnu) (standard-packages)
  28. #:use-module (srfi srfi-34)
  29. #:use-module (srfi srfi-35)
  30. #:use-module (ice-9 match)
  31. #:use-module (ice-9 popen)
  32. #:use-module (ice-9 rdelim)
  33. #:export (hg-reference
  34. hg-reference?
  35. hg-reference-url
  36. hg-reference-changeset
  37. hg-reference-recursive?
  38. hg-predicate
  39. hg-fetch
  40. hg-version
  41. hg-file-name))
  42. ;;; Commentary:
  43. ;;;
  44. ;;; An <origin> method that fetches a specific changeset from a Mercurial
  45. ;;; repository. The repository URL and changeset ID are specified with a
  46. ;;; <hg-reference> object.
  47. ;;;
  48. ;;; Code:
  49. (define-record-type* <hg-reference>
  50. hg-reference make-hg-reference
  51. hg-reference?
  52. (url hg-reference-url)
  53. (changeset hg-reference-changeset))
  54. (define (hg-package)
  55. "Return the default Mercurial package."
  56. (let ((distro (resolve-interface '(gnu packages version-control))))
  57. (module-ref distro 'mercurial)))
  58. (define* (hg-fetch ref hash-algo hash
  59. #:optional name
  60. #:key (system (%current-system)) (guile (default-guile))
  61. (hg (hg-package)))
  62. "Return a fixed-output derivation that fetches REF, a <hg-reference>
  63. object. The output is expected to have recursive hash HASH of type
  64. HASH-ALGO (a symbol). Use NAME as the file name, or a generic name if #f."
  65. (define guile-zlib
  66. (module-ref (resolve-interface '(gnu packages guile)) 'guile-zlib))
  67. (define guile-json
  68. (module-ref (resolve-interface '(gnu packages guile)) 'guile-json-4))
  69. (define gnutls
  70. (module-ref (resolve-interface '(gnu packages tls)) 'gnutls))
  71. (define modules
  72. (delete '(guix config)
  73. (source-module-closure '((guix build hg)
  74. (guix build download-nar)))))
  75. (define build
  76. (with-imported-modules modules
  77. (with-extensions (list guile-json gnutls ;for (guix swh)
  78. guile-zlib)
  79. #~(begin
  80. (use-modules (guix build hg)
  81. (guix build download-nar))
  82. (or (hg-fetch '#$(hg-reference-url ref)
  83. '#$(hg-reference-changeset ref)
  84. #$output
  85. #:hg-command (string-append #+hg "/bin/hg"))
  86. (download-nar #$output))))))
  87. (mlet %store-monad ((guile (package->derivation guile system)))
  88. (gexp->derivation (or name "hg-checkout") build
  89. #:leaked-env-vars '("http_proxy" "https_proxy"
  90. "LC_ALL" "LC_MESSAGES" "LANG"
  91. "COLUMNS")
  92. #:system system
  93. #:local-build? #t ;don't offload repo cloning
  94. #:hash-algo hash-algo
  95. #:hash hash
  96. #:recursive? #t
  97. #:guile-for-build guile)))
  98. (define (hg-version version revision changeset)
  99. "Return the version string for packages using hg-download."
  100. ;; hg-version is almost exclusively executed while modules are being loaded.
  101. ;; This makes any errors hide their backtrace. Avoid the mysterious error
  102. ;; "Value out of range 0 to N: 7" when the commit ID is too short, which
  103. ;; can happen, for example, when the user swapped the revision and commit
  104. ;; arguments by mistake.
  105. (when (< (string-length changeset) 7)
  106. (raise
  107. (condition
  108. (&message (message "hg-version: changeset ID unexpectedly short")))))
  109. (string-append version "-" revision "." (string-take changeset 7)))
  110. (define (hg-file-name name version)
  111. "Return the file-name for packages using hg-download."
  112. (string-append name "-" version "-checkout"))
  113. (define (hg-file-list directory)
  114. "Evaluates to a list of files contained in the repository at path
  115. @var{directory}"
  116. (let* ((port (open-input-pipe (format #f "hg files --repository ~s" directory)))
  117. (files (let loop ((files '()))
  118. (let ((line (read-line port)))
  119. (cond
  120. ((eof-object? line) files)
  121. (else
  122. (loop (cons line files))))))))
  123. (close-pipe port)
  124. (map canonicalize-path files)))
  125. (define (should-select? path-list candidate)
  126. "Returns #t in case that @var{candidate} is a file that is part of the given
  127. @var{path-list}."
  128. (let ((canon-candidate (canonicalize-path candidate)))
  129. (let loop ((xs path-list))
  130. (cond
  131. ((null? xs)
  132. ;; Directories are not part of `hg files', but `local-file' will not
  133. ;; recurse if we don't return #t for directories.
  134. (equal? (array-ref (lstat candidate) 13) 'directory))
  135. ((string-contains candidate (car xs)) #t)
  136. (else (loop (cdr xs)))))))
  137. (define (hg-predicate directory)
  138. "This procedure evaluates to a predicate that reports back whether a given
  139. @var{file} - @var{stat} combination is part of the files tracked by
  140. Mercurial."
  141. (let ((files (hg-file-list directory)))
  142. (lambda (file stat)
  143. (should-select? files file))))
  144. ;;; hg-download.scm ends here