| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 |
|
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 |
|
| 27 | (use-modules (guix)
|
| 28 | (guix ui)
|
| 29 | (guix git-download)
|
| 30 | (guix upstream)
|
| 31 | (guix utils)
|
| 32 | (guix base32)
|
| 33 | (guix build utils)
|
| 34 | (guix scripts hash)
|
| 35 | (gnu packages package-management)
|
| 36 | (ice-9 match)
|
| 37 | (ice-9 popen)
|
| 38 | (ice-9 regex)
|
| 39 | (ice-9 textual-ports)
|
| 40 | (srfi srfi-1)
|
| 41 | (srfi srfi-2)
|
| 42 | (srfi srfi-26))
|
| 43 |
|
| 44 | (define %top-srcdir
|
| 45 | (string-append (current-source-directory) "/.."))
|
| 46 |
|
| 47 | (define (package-definition-location)
|
| 48 | "Return the source properties of the definition of the 'guix' package."
|
| 49 | (call-with-input-file (location-file (package-location guix))
|
| 50 | (lambda (port)
|
| 51 | (let loop ()
|
| 52 | (match (read port)
|
| 53 | ((? eof-object?)
|
| 54 | (error "definition of 'guix' package could not be found"
|
| 55 | (port-filename port)))
|
| 56 | (('define-public 'guix value)
|
| 57 | (source-properties value))
|
| 58 | (_
|
| 59 | (loop)))))))
|
| 60 |
|
| 61 | (define* (update-definition commit hash
|
| 62 | #:key version old-hash)
|
| 63 | "Return a one-argument procedure that takes a string, the definition of the
|
| 64 | 'guix' package, and returns a string, the update definition for VERSION,
|
| 65 | COMMIT."
|
| 66 | (define (linear-offset str line column)
|
| 67 | |
| 68 | |
| 69 | (call-with-input-string str
|
| 70 | (lambda (port)
|
| 71 | (let loop ((offset 0))
|
| 72 | (cond ((and (= (port-column port) column)
|
| 73 | (= (port-line port) line))
|
| 74 | offset)
|
| 75 | ((eof-object? (read-char port))
|
| 76 | (error "line and column not reached!"
|
| 77 | str))
|
| 78 | (else
|
| 79 | (loop (+ 1 offset))))))))
|
| 80 |
|
| 81 | (define (update-hash str)
|
| 82 | |
| 83 | (string-replace-substring str
|
| 84 | (bytevector->nix-base32-string old-hash)
|
| 85 | (bytevector->nix-base32-string hash)))
|
| 86 |
|
| 87 | (lambda (str)
|
| 88 | (match (call-with-input-string str read)
|
| 89 | (('let (('version old-version)
|
| 90 | ('commit old-commit)
|
| 91 | ('revision old-revision))
|
| 92 | defn)
|
| 93 | (let* ((location (source-properties defn))
|
| 94 | (line (assq-ref location 'line))
|
| 95 | (column 0)
|
| 96 | (offset (linear-offset str line column)))
|
| 97 | (string-append (format #f "(let ((version \"~a\")
|
| 98 | (commit \"~a\")
|
| 99 | (revision ~a))\n"
|
| 100 | (or version old-version)
|
| 101 | commit
|
| 102 | (if (and version
|
| 103 | (not (string=? version old-version)))
|
| 104 | 0
|
| 105 | (+ 1 old-revision)))
|
| 106 | (string-drop (update-hash str) offset))))
|
| 107 | (exp
|
| 108 | (error "'guix' package definition is not as expected" exp)))))
|
| 109 |
|
| 110 | (define (git-add-worktree directory commit)
|
| 111 | "Create a new git worktree at DIRECTORY, detached on commit COMMIT."
|
| 112 | (invoke "git" "worktree" "add" "--detach" directory commit))
|
| 113 |
|
| 114 | (define (call-with-temporary-git-worktree commit proc)
|
| 115 | "Execute PROC in the context of a temporary git worktree created from
|
| 116 | COMMIT. PROC receives the temporary directory file name as an argument."
|
| 117 | (call-with-temporary-directory
|
| 118 | (lambda (tmp-directory)
|
| 119 | (dynamic-wind
|
| 120 | (lambda ()
|
| 121 | #t)
|
| 122 | (lambda ()
|
| 123 | (git-add-worktree tmp-directory commit)
|
| 124 | (proc tmp-directory))
|
| 125 | (lambda ()
|
| 126 | (invoke "git" "worktree" "remove" "--force" tmp-directory))))))
|
| 127 |
|
| 128 | (define %guix-git-repo-push-url-regexp
|
| 129 | "(git.guix.gnu.org|codeberg.org/guix|[email protected]:guix)/guix(.git)? \\(push\\)")
|
| 130 |
|
| 131 | (define (with-input-pipe-to-string prog . args)
|
| 132 | (let* ((input-pipe (apply open-pipe* OPEN_READ prog args))
|
| 133 | (output (get-string-all input-pipe))
|
| 134 | (exit-val (status:exit-val (close-pipe input-pipe))))
|
| 135 | (unless (zero? exit-val)
|
| 136 | (error (format #f "Command ~s exited with non-zero exit status: ~s"
|
| 137 | (string-join (cons prog args)) exit-val)))
|
| 138 | (string-trim-both output)))
|
| 139 |
|
| 140 | (define (find-origin-remote)
|
| 141 | "Find the name of the git remote with the Guix git repo URL."
|
| 142 | (and-let* ((remotes (string-split (with-input-pipe-to-string
|
| 143 | "git" "remote" "-v")
|
| 144 | #\newline))
|
| 145 | (origin-entry (find (cut string-match
|
| 146 | %guix-git-repo-push-url-regexp
|
| 147 | <>)
|
| 148 | remotes)))
|
| 149 | (first (string-split origin-entry #\tab))))
|
| 150 |
|
| 151 | (define (commit-already-pushed? remote commit)
|
| 152 | "True if COMMIT is found in the REMOTE repository."
|
| 153 | (not (string-null? (with-input-pipe-to-string
|
| 154 | "git" "branch" "-r" "--contains" commit
|
| 155 | (string-append remote "/master")))))
|
| 156 |
|
| 157 | (define (keep-source-in-store store source)
|
| 158 | "Add SOURCE to the store under the name that the 'guix' package expects."
|
| 159 |
|
| 160 | |
| 161 | |
| 162 | |
| 163 | (reload-module
|
| 164 | (resolve-module '(gnu packages package-management)))
|
| 165 |
|
| 166 | (let* ((source (add-to-store store
|
| 167 | (origin-file-name (package-source guix))
|
| 168 | #t "sha256" source
|
| 169 | #:select? (git-predicate source)))
|
| 170 | (root (store-path-package-name source)))
|
| 171 |
|
| 172 | |
| 173 | (false-if-exception (delete-file root))
|
| 174 | (symlink source root)
|
| 175 | (add-indirect-root store
|
| 176 | (string-append (getcwd) "/" root))
|
| 177 |
|
| 178 | (info (G_ "source code kept in ~a (GC root: ~a)~%")
|
| 179 | source root)))
|
| 180 |
|
| 181 |
|
| 182 | (define (main . args)
|
| 183 | (match args
|
| 184 | ((commit version)
|
| 185 | (with-directory-excursion %top-srcdir
|
| 186 | (or (getenv "GUIX_ALLOW_ME_TO_USE_PRIVATE_COMMIT")
|
| 187 | (let ((remote (find-origin-remote)))
|
| 188 | (unless remote
|
| 189 | (leave (G_ "Failed to find the origin git remote.~%")))
|
| 190 | (commit-already-pushed? remote commit))
|
| 191 | (leave (G_ "Commit ~a is not pushed upstream. Aborting.~%") commit))
|
| 192 | (call-with-temporary-git-worktree commit
|
| 193 | (lambda (tmp-directory)
|
| 194 | (let* ((hash (nix-base32-string->bytevector
|
| 195 | (string-trim-both
|
| 196 | (with-output-to-string
|
| 197 | (lambda ()
|
| 198 | (guix-hash "-rx" tmp-directory))))))
|
| 199 | (location (package-definition-location))
|
| 200 | (old-hash (content-hash-value
|
| 201 | (origin-hash (package-source guix)))))
|
| 202 | (edit-expression location
|
| 203 | (update-definition commit hash
|
| 204 | #:old-hash old-hash
|
| 205 | #:version version))
|
| 206 | |
| 207 | |
| 208 | (when (getenv "GUIX_ALLOW_ME_TO_USE_PRIVATE_COMMIT")
|
| 209 | (with-store store
|
| 210 | (keep-source-in-store store tmp-directory))))))))
|
| 211 | ((commit)
|
| 212 | |
| 213 | (main commit #f))))
|
| 214 |
|
| 215 | (apply main (cdr (command-line)))
|