committer.scm.in 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. #!@GUILE@ \
  2. --no-auto-compile -s
  3. !#
  4. ;;; GNU Guix --- Functional package management for GNU
  5. ;;; Copyright © 2020, 2021 Ricardo Wurmus <rekado@elephly.net>
  6. ;;; Copyright © 2021 Sarah Morgensen <iskarian@mgsn.dev>
  7. ;;;
  8. ;;; This file is part of GNU Guix.
  9. ;;;
  10. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  11. ;;; under the terms of the GNU General Public License as published by
  12. ;;; the Free Software Foundation; either version 3 of the License, or (at
  13. ;;; your option) any later version.
  14. ;;;
  15. ;;; GNU Guix is distributed in the hope that it will be useful, but
  16. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;;; GNU General Public License for more details.
  19. ;;;
  20. ;;; You should have received a copy of the GNU General Public License
  21. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  22. ;;; Commentary:
  23. ;; This script stages and commits changes to package definitions.
  24. ;;; Code:
  25. (import (sxml xpath)
  26. (srfi srfi-1)
  27. (srfi srfi-2)
  28. (srfi srfi-9)
  29. (srfi srfi-11)
  30. (srfi srfi-26)
  31. (ice-9 format)
  32. (ice-9 popen)
  33. (ice-9 match)
  34. (ice-9 rdelim)
  35. (ice-9 regex)
  36. (ice-9 textual-ports)
  37. (guix gexp))
  38. (define* (break-string str #:optional (max-line-length 70))
  39. "Break the string STR into lines that are no longer than MAX-LINE-LENGTH.
  40. Return a single string."
  41. (define (restore-line words)
  42. (string-join (reverse words) " "))
  43. (if (<= (string-length str) max-line-length)
  44. str
  45. (let ((words+lengths (map (lambda (word)
  46. (cons word (string-length word)))
  47. (string-tokenize str))))
  48. (match (fold (match-lambda*
  49. (((word . length)
  50. (count current lines))
  51. (let ((new-count (+ count length 1)))
  52. (if (< new-count max-line-length)
  53. (list new-count
  54. (cons word current)
  55. lines)
  56. (list length
  57. (list word)
  58. (cons (restore-line current) lines))))))
  59. '(0 () ())
  60. words+lengths)
  61. ((_ last-words lines)
  62. (string-join (reverse (cons (restore-line last-words) lines))
  63. "\n"))))))
  64. (define* (break-string-with-newlines str #:optional (max-line-length 70))
  65. "Break the lines of string STR into lines that are no longer than
  66. MAX-LINE-LENGTH. Return a single string."
  67. (string-join (map (cut break-string <> max-line-length)
  68. (string-split str #\newline))
  69. "\n"))
  70. (define (read-excursion port)
  71. "Read an expression from PORT and reset the port position before returning
  72. the expression."
  73. (let ((start (ftell port))
  74. (result (read port)))
  75. (seek port start SEEK_SET)
  76. result))
  77. (define (surrounding-sexp port line-no)
  78. "Return the top-level S-expression surrounding the change at line number
  79. LINE-NO in PORT."
  80. (let loop ((i (1- line-no))
  81. (last-top-level-sexp #f))
  82. (if (zero? i)
  83. last-top-level-sexp
  84. (match (peek-char port)
  85. (#\(
  86. (let ((sexp (read-excursion port)))
  87. (read-line port)
  88. (loop (1- i) sexp)))
  89. (_
  90. (read-line port)
  91. (loop (1- i) last-top-level-sexp))))))
  92. (define-record-type <hunk>
  93. (make-hunk file-name
  94. old-line-number
  95. new-line-number
  96. diff-lines
  97. definition?)
  98. hunk?
  99. (file-name hunk-file-name)
  100. ;; Line number before the change
  101. (old-line-number hunk-old-line-number)
  102. ;; Line number after the change
  103. (new-line-number hunk-new-line-number)
  104. ;; The full diff to be used with "git apply --cached"
  105. (diff-lines hunk-diff-lines)
  106. ;; Does this hunk add a definition?
  107. (definition? hunk-definition?))
  108. (define* (hunk->patch hunk #:optional (port (current-output-port)))
  109. (let ((file-name (hunk-file-name hunk)))
  110. (format port
  111. "diff --git a/~a b/~a~%--- a/~a~%+++ b/~a~%~a"
  112. file-name file-name file-name file-name
  113. (string-join (hunk-diff-lines hunk) ""))))
  114. (define (diff-info)
  115. "Read the diff and return a list of <hunk> values."
  116. (let ((port (open-pipe* OPEN_READ
  117. "git" "diff-files"
  118. "--no-prefix"
  119. ;; Only include one context line to avoid lumping in
  120. ;; new definitions with changes to existing
  121. ;; definitions.
  122. "--unified=1"
  123. "gnu")))
  124. (define (extract-line-number line-tag)
  125. (abs (string->number
  126. (car (string-split line-tag #\,)))))
  127. (define (read-hunk)
  128. (let loop ((lines '())
  129. (definition? #false))
  130. (let ((line (read-line port 'concat)))
  131. (cond
  132. ((eof-object? line)
  133. (values (reverse lines) definition?))
  134. ((or (string-prefix? "@@ " line)
  135. (string-prefix? "diff --git" line))
  136. (unget-string port line)
  137. (values (reverse lines) definition?))
  138. (else
  139. (loop (cons line lines)
  140. (or definition?
  141. (string-prefix? "+(define" line))))))))
  142. (define info
  143. (let loop ((acc '())
  144. (file-name #f))
  145. (let ((line (read-line port)))
  146. (cond
  147. ((eof-object? line) acc)
  148. ((string-prefix? "--- " line)
  149. (match (string-split line #\space)
  150. ((_ file-name)
  151. (loop acc file-name))))
  152. ((string-prefix? "@@ " line)
  153. (match (string-split line #\space)
  154. ((_ old-start new-start . _)
  155. (let-values
  156. (((diff-lines definition?) (read-hunk)))
  157. (loop (cons (make-hunk file-name
  158. (extract-line-number old-start)
  159. (extract-line-number new-start)
  160. (cons (string-append line "\n")
  161. diff-lines)
  162. definition?) acc)
  163. file-name)))))
  164. (else (loop acc file-name))))))
  165. (close-pipe port)
  166. info))
  167. (define (lines-to-first-change hunk)
  168. "Return the number of diff lines until the first change."
  169. (1- (count (lambda (line)
  170. ((negate char-set-contains?)
  171. (char-set #\+ #\-)
  172. (string-ref line 0)))
  173. (hunk-diff-lines hunk))))
  174. (define (old-sexp hunk)
  175. "Using the diff information in HUNK return the unmodified S-expression
  176. corresponding to the top-level definition containing the staged changes."
  177. ;; TODO: We can't seek with a pipe port...
  178. (let* ((port (open-pipe* OPEN_READ
  179. "git" "cat-file" "-p" (string-append
  180. "HEAD:"
  181. (hunk-file-name hunk))))
  182. (contents (get-string-all port)))
  183. (close-pipe port)
  184. (call-with-input-string contents
  185. (lambda (port)
  186. (surrounding-sexp port
  187. (+ (lines-to-first-change hunk)
  188. (hunk-old-line-number hunk)))))))
  189. (define (new-sexp hunk)
  190. "Using the diff information in HUNK return the modified S-expression
  191. corresponding to the top-level definition containing the staged changes."
  192. (call-with-input-file (hunk-file-name hunk)
  193. (lambda (port)
  194. (surrounding-sexp port
  195. (+ (lines-to-first-change hunk)
  196. (hunk-new-line-number hunk))))))
  197. (define* (change-commit-message file-name old new #:optional (port (current-output-port)))
  198. "Print ChangeLog commit message for changes between OLD and NEW."
  199. (define (get-values expr field)
  200. (match ((sxpath `(// ,field quasiquote *)) expr)
  201. (() '())
  202. ((first . rest)
  203. (map cadadr first))))
  204. (define (listify items)
  205. (match items
  206. ((one) one)
  207. ((one two)
  208. (string-append one " and " two))
  209. ((one two . more)
  210. (string-append (string-join (drop-right items 1) ", ")
  211. ", and " (first (take-right items 1))))))
  212. (define variable-name
  213. (second old))
  214. (define version
  215. (and=> ((sxpath '(// version *any*)) new)
  216. first))
  217. (format port
  218. "gnu: ~a: Update to ~a.~%~%* ~a (~a): Update to ~a.~%"
  219. variable-name version file-name variable-name version)
  220. (for-each (lambda (field)
  221. (let ((old-values (get-values old field))
  222. (new-values (get-values new field)))
  223. (or (equal? old-values new-values)
  224. (let ((removed (lset-difference equal? old-values new-values))
  225. (added (lset-difference equal? new-values old-values)))
  226. (format port
  227. "[~a]: ~a~%" field
  228. (break-string
  229. (match (list (map symbol->string removed)
  230. (map symbol->string added))
  231. ((() added)
  232. (format #f "Add ~a."
  233. (listify added)))
  234. ((removed ())
  235. (format #f "Remove ~a."
  236. (listify removed)))
  237. ((removed added)
  238. (format #f "Remove ~a; add ~a."
  239. (listify removed)
  240. (listify added))))))))))
  241. '(inputs propagated-inputs native-inputs)))
  242. (define* (add-commit-message file-name variable-name #:optional (port (current-output-port)))
  243. "Print ChangeLog commit message for a change to FILE-NAME adding a definition."
  244. (format port
  245. "gnu: Add ~a.~%~%* ~a (~a): New variable.~%"
  246. variable-name file-name variable-name))
  247. (define* (custom-commit-message file-name variable-name message changelog
  248. #:optional (port (current-output-port)))
  249. "Print custom commit message for a change to VARIABLE-NAME in FILE-NAME, using
  250. MESSAGE as the commit message and CHANGELOG as the body of the ChangeLog
  251. entry. If CHANGELOG is #f, the commit message is reused. If CHANGELOG already
  252. contains ': ', no colon is inserted between the location and body of the
  253. ChangeLog entry."
  254. (define (trim msg)
  255. (string-trim-right (string-trim-both msg) (char-set #\.)))
  256. (define (changelog-has-location? changelog)
  257. (->bool (string-match "^[[:graph:]]+:[[:blank:]]" changelog)))
  258. (let* ((message (trim message))
  259. (changelog (if changelog (trim changelog) message))
  260. (message/f (format #f "gnu: ~a: ~a." variable-name message))
  261. (changelog/f (if (changelog-has-location? changelog)
  262. (format #f "* ~a (~a)~a."
  263. file-name variable-name changelog)
  264. (format #f "* ~a (~a): ~a."
  265. file-name variable-name changelog))))
  266. (format port
  267. "~a~%~%~a~%"
  268. (break-string-with-newlines message/f 72)
  269. (break-string-with-newlines changelog/f 72))))
  270. (define (group-hunks-by-sexp hunks)
  271. "Return a list of pairs associating all hunks with the S-expression they are
  272. modifying."
  273. (fold (lambda (sexp hunk acc)
  274. (match acc
  275. (((previous-sexp . hunks) . rest)
  276. (if (equal? sexp previous-sexp)
  277. (cons (cons previous-sexp
  278. (cons hunk hunks))
  279. rest)
  280. (cons (cons sexp (list hunk))
  281. acc)))
  282. (_
  283. (cons (cons sexp (list hunk))
  284. acc))))
  285. '()
  286. (map new-sexp hunks)
  287. hunks))
  288. (define (new+old+hunks hunks)
  289. (map (match-lambda
  290. ((new . hunks)
  291. (cons* new (old-sexp (first hunks)) hunks)))
  292. (group-hunks-by-sexp hunks)))
  293. (define %delay 1000)
  294. (define (main . args)
  295. (define* (change-commit-message* file-name old new #:rest rest)
  296. (let ((changelog #f))
  297. (match args
  298. ((or (message changelog) (message))
  299. (apply custom-commit-message
  300. file-name (second old) message changelog rest))
  301. (_
  302. (apply change-commit-message file-name old new rest)))))
  303. (match (diff-info)
  304. (()
  305. (display "Nothing to be done.\n" (current-error-port)))
  306. (hunks
  307. (let-values
  308. (((definitions changes)
  309. (partition hunk-definition? hunks)))
  310. ;; Additions.
  311. (for-each (lambda (hunk)
  312. (and-let*
  313. ((define-line (find (cut string-prefix? "+(define" <>)
  314. (hunk-diff-lines hunk)))
  315. (variable-name (and=> (string-tokenize define-line) second)))
  316. (add-commit-message (hunk-file-name hunk) variable-name)
  317. (let ((port (open-pipe* OPEN_WRITE
  318. "git" "apply"
  319. "--cached"
  320. "--unidiff-zero")))
  321. (hunk->patch hunk port)
  322. (unless (eqv? 0 (status:exit-val (close-pipe port)))
  323. (error "Cannot apply")))
  324. (let ((port (open-pipe* OPEN_WRITE "git" "commit" "-F" "-")))
  325. (add-commit-message (hunk-file-name hunk)
  326. variable-name port)
  327. (usleep %delay)
  328. (unless (eqv? 0 (status:exit-val (close-pipe port)))
  329. (error "Cannot commit"))))
  330. (usleep %delay))
  331. definitions)
  332. ;; Changes.
  333. (for-each (match-lambda
  334. ((new old . hunks)
  335. (for-each (lambda (hunk)
  336. (let ((port (open-pipe* OPEN_WRITE
  337. "git" "apply"
  338. "--cached"
  339. "--unidiff-zero")))
  340. (hunk->patch hunk port)
  341. (unless (eqv? 0 (status:exit-val (close-pipe port)))
  342. (error "Cannot apply")))
  343. (usleep %delay))
  344. hunks)
  345. (change-commit-message* (hunk-file-name (first hunks))
  346. old new)
  347. (let ((port (open-pipe* OPEN_WRITE "git" "commit" "-F" "-")))
  348. (change-commit-message* (hunk-file-name (first hunks))
  349. old new
  350. port)
  351. (usleep %delay)
  352. (unless (eqv? 0 (status:exit-val (close-pipe port)))
  353. (error "Cannot commit")))))
  354. ;; XXX: we recompute the hunks here because previous
  355. ;; insertions lead to offsets.
  356. (new+old+hunks (diff-info)))))))
  357. (apply main (cdr (command-line)))