committer.scm.in 17 KB

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