committer.scm.in 16 KB

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