update-guix-package.scm 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017, 2018 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2020 Maxim Cournoyer <maxim.cournoyer@gmail.com>
  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. ;;; Commentary:
  20. ;;;
  21. ;;; This scripts updates the definition of the 'guix' package in Guix for the
  22. ;;; current commit. It requires Git to be installed.
  23. ;;;
  24. ;;; Code:
  25. (use-modules (guix)
  26. (guix ui)
  27. (guix git-download)
  28. (guix upstream)
  29. (guix utils)
  30. (guix base32)
  31. (guix build utils)
  32. (guix scripts hash)
  33. (gnu packages package-management)
  34. (ice-9 match)
  35. (ice-9 popen)
  36. (ice-9 regex)
  37. (ice-9 textual-ports)
  38. (srfi srfi-1)
  39. (srfi srfi-2)
  40. (srfi srfi-26))
  41. (define %top-srcdir
  42. (string-append (current-source-directory) "/.."))
  43. (define (package-definition-location)
  44. "Return the source properties of the definition of the 'guix' package."
  45. (call-with-input-file (location-file (package-location guix))
  46. (lambda (port)
  47. (let loop ()
  48. (match (read port)
  49. ((? eof-object?)
  50. (error "definition of 'guix' package could not be found"
  51. (port-filename port)))
  52. (('define-public 'guix value)
  53. (source-properties value))
  54. (_
  55. (loop)))))))
  56. (define* (update-definition commit hash
  57. #:key version old-hash)
  58. "Return a one-argument procedure that takes a string, the definition of the
  59. 'guix' package, and returns a string, the update definition for VERSION,
  60. COMMIT."
  61. (define (linear-offset str line column)
  62. ;; Return the offset in characters to reach LINE and COLUMN (both
  63. ;; zero-indexed) in STR.
  64. (call-with-input-string str
  65. (lambda (port)
  66. (let loop ((offset 0))
  67. (cond ((and (= (port-column port) column)
  68. (= (port-line port) line))
  69. offset)
  70. ((eof-object? (read-char port))
  71. (error "line and column not reached!"
  72. str))
  73. (else
  74. (loop (+ 1 offset))))))))
  75. (define (update-hash str)
  76. ;; Replace OLD-HASH with HASH in STR.
  77. (string-replace-substring str
  78. (bytevector->nix-base32-string old-hash)
  79. (bytevector->nix-base32-string hash)))
  80. (lambda (str)
  81. (match (call-with-input-string str read)
  82. (('let (('version old-version)
  83. ('commit old-commit)
  84. ('revision old-revision))
  85. defn)
  86. (let* ((location (source-properties defn))
  87. (line (assq-ref location 'line))
  88. (column 0)
  89. (offset (linear-offset str line column)))
  90. (string-append (format #f "(let ((version \"~a\")
  91. (commit \"~a\")
  92. (revision ~a))\n"
  93. (or version old-version)
  94. commit
  95. (if (and version
  96. (not (string=? version old-version)))
  97. 0
  98. (+ 1 old-revision)))
  99. (string-drop (update-hash str) offset))))
  100. (exp
  101. (error "'guix' package definition is not as expected" exp)))))
  102. (define (git-add-worktree directory commit)
  103. "Create a new git worktree at DIRECTORY, detached on commit COMMIT."
  104. (invoke "git" "worktree" "add" "--detach" directory commit))
  105. (define (call-with-temporary-git-worktree commit proc)
  106. "Execute PROC in the context of a temporary git worktree created from
  107. COMMIT. PROC receives the temporary directory file name as an argument."
  108. (call-with-temporary-directory
  109. (lambda (tmp-directory)
  110. (dynamic-wind
  111. (lambda ()
  112. #t)
  113. (lambda ()
  114. (git-add-worktree tmp-directory commit)
  115. (proc tmp-directory))
  116. (lambda ()
  117. (invoke "git" "worktree" "remove" "--force" tmp-directory))))))
  118. (define %savannah-guix-git-repo-push-url-regexp
  119. "git.(savannah|sv).gnu.org:?/srv/git/guix.git \\(push\\)")
  120. (define-syntax-rule (with-input-pipe-to-string prog arg ...)
  121. (let* ((input-pipe (open-pipe* OPEN_READ prog arg ...))
  122. (output (get-string-all input-pipe))
  123. (exit-val (status:exit-val (close-pipe input-pipe))))
  124. (unless (zero? exit-val)
  125. (error (format #f "Command ~s exited with non-zero exit status: ~s"
  126. (string-join (list prog arg ...)) exit-val)))
  127. (string-trim-both output)))
  128. (define (find-origin-remote)
  129. "Find the name of the git remote with the Savannah Guix git repo URL."
  130. (and-let* ((remotes (string-split (with-input-pipe-to-string
  131. "git" "remote" "-v")
  132. #\newline))
  133. (origin-entry (find (cut string-match
  134. %savannah-guix-git-repo-push-url-regexp
  135. <>)
  136. remotes)))
  137. (first (string-split origin-entry #\tab))))
  138. (define (commit-already-pushed? remote commit)
  139. "True if COMMIT is found in the REMOTE repository."
  140. (not (string-null? (with-input-pipe-to-string
  141. "git" "branch" "-r" "--contains" commit
  142. (string-append remote "/master")))))
  143. (define (keep-source-in-store store source)
  144. "Add SOURCE to the store under the name that the 'guix' package expects."
  145. ;; Add SOURCE to the store, but this time under the real name used in the
  146. ;; 'origin'. This allows us to build the package without having to make a
  147. ;; real checkout; thus, it also works when working on a private branch.
  148. (reload-module
  149. (resolve-module '(gnu packages package-management)))
  150. (let* ((source (add-to-store store
  151. (origin-file-name (package-source guix))
  152. #t "sha256" source
  153. #:select? (git-predicate source)))
  154. (root (store-path-package-name source)))
  155. ;; Add an indirect GC root for SOURCE in the current directory.
  156. (false-if-exception (delete-file root))
  157. (symlink source root)
  158. (add-indirect-root store
  159. (string-append (getcwd) "/" root))
  160. (info (G_ "source code kept in ~a (GC root: ~a)~%")
  161. source root)))
  162. (define (main . args)
  163. (match args
  164. ((commit version)
  165. (with-directory-excursion %top-srcdir
  166. (or (getenv "GUIX_ALLOW_ME_TO_USE_PRIVATE_COMMIT")
  167. (let ((remote (find-origin-remote)))
  168. (unless remote
  169. (leave (G_ "Failed to find the origin git remote.~%")))
  170. (commit-already-pushed? remote commit))
  171. (leave (G_ "Commit ~a is not pushed upstream. Aborting.~%") commit))
  172. (call-with-temporary-git-worktree commit
  173. (lambda (tmp-directory)
  174. (let* ((hash (nix-base32-string->bytevector
  175. (string-trim-both
  176. (with-output-to-string
  177. (lambda ()
  178. (guix-hash "-rx" tmp-directory))))))
  179. (location (package-definition-location))
  180. (old-hash (content-hash-value
  181. (origin-hash (package-source guix)))))
  182. (edit-expression location
  183. (update-definition commit hash
  184. #:old-hash old-hash
  185. #:version version))
  186. ;; When GUIX_ALLOW_ME_TO_USE_PRIVATE_COMMIT is set, the sources are
  187. ;; added to the store. This is used as part of 'make release'.
  188. (when (getenv "GUIX_ALLOW_ME_TO_USE_PRIVATE_COMMIT")
  189. (with-store store
  190. (keep-source-in-store store tmp-directory))))))))
  191. ((commit)
  192. ;; Automatically deduce the version and revision numbers.
  193. (main commit #f))))
  194. (apply main (cdr (command-line)))