committer.scm.in 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. ((first . rest)
  204. (map cadadr first))))
  205. (define (listify items)
  206. (match items
  207. ((one) one)
  208. ((one two)
  209. (string-append one " and " two))
  210. ((one two . more)
  211. (string-append (string-join (drop-right items 1) ", ")
  212. ", and " (first (take-right items 1))))))
  213. (define variable-name
  214. (second old))
  215. (define version
  216. (and=> ((sxpath '(// version *any*)) new)
  217. first))
  218. (format port
  219. "gnu: ~a: Update to ~a.~%~%* ~a (~a): Update to ~a.~%"
  220. variable-name version file-name variable-name version)
  221. (for-each (lambda (field)
  222. (let ((old-values (get-values old field))
  223. (new-values (get-values new field)))
  224. (or (equal? old-values new-values)
  225. (let ((removed (lset-difference equal? old-values new-values))
  226. (added (lset-difference equal? new-values old-values)))
  227. (format port
  228. "[~a]: ~a~%" field
  229. (break-string
  230. (match (list (map symbol->string removed)
  231. (map symbol->string added))
  232. ((() added)
  233. (format #f "Add ~a."
  234. (listify added)))
  235. ((removed ())
  236. (format #f "Remove ~a."
  237. (listify removed)))
  238. ((removed added)
  239. (format #f "Remove ~a; add ~a."
  240. (listify removed)
  241. (listify added))))))))))
  242. '(inputs propagated-inputs native-inputs)))
  243. (define* (add-commit-message file-name variable-name #:optional (port (current-output-port)))
  244. "Print ChangeLog commit message for a change to FILE-NAME adding a definition."
  245. (format port
  246. "gnu: Add ~a.~%~%* ~a (~a): New variable.~%"
  247. variable-name file-name variable-name))
  248. (define* (custom-commit-message file-name variable-name message changelog
  249. #:optional (port (current-output-port)))
  250. "Print custom commit message for a change to VARIABLE-NAME in FILE-NAME, using
  251. MESSAGE as the commit message and CHANGELOG as the body of the ChangeLog
  252. entry. If CHANGELOG is #f, the commit message is reused. If CHANGELOG already
  253. contains ': ', no colon is inserted between the location and body of the
  254. ChangeLog entry."
  255. (define (trim msg)
  256. (string-trim-right (string-trim-both msg) (char-set #\.)))
  257. (define (changelog-has-location? changelog)
  258. (->bool (string-match "^[[:graph:]]+:[[:blank:]]" changelog)))
  259. (let* ((message (trim message))
  260. (changelog (if changelog (trim changelog) message))
  261. (message/f (format #f "gnu: ~a: ~a." variable-name message))
  262. (changelog/f (if (changelog-has-location? changelog)
  263. (format #f "* ~a (~a)~a."
  264. file-name variable-name changelog)
  265. (format #f "* ~a (~a): ~a."
  266. file-name variable-name changelog))))
  267. (format port
  268. "~a~%~%~a~%"
  269. (break-string-with-newlines message/f 72)
  270. (break-string-with-newlines changelog/f 72))))
  271. (define (add-copyright-line line)
  272. "Add the copyright line on LINE to the previous commit."
  273. (let ((author (match:substring
  274. (string-match "^\\+;;; Copyright ©[^[:alpha:]]+(.*)$" line)
  275. 1)))
  276. (format
  277. (current-output-port) "Amend and add copyright line for ~a~%" author)
  278. (system* "git" "commit" "--amend" "--no-edit")))
  279. (define (group-hunks-by-sexp hunks)
  280. "Return a list of pairs associating all hunks with the S-expression they are
  281. modifying."
  282. (fold (lambda (sexp hunk acc)
  283. (match acc
  284. (((previous-sexp . hunks) . rest)
  285. (if (equal? sexp previous-sexp)
  286. (cons (cons previous-sexp
  287. (cons hunk hunks))
  288. rest)
  289. (cons (cons sexp (list hunk))
  290. acc)))
  291. (_
  292. (cons (cons sexp (list hunk))
  293. acc))))
  294. '()
  295. (map new-sexp hunks)
  296. hunks))
  297. (define (new+old+hunks hunks)
  298. (map (match-lambda
  299. ((new . hunks)
  300. (cons* new (old-sexp (first hunks)) hunks)))
  301. (group-hunks-by-sexp hunks)))
  302. (define %delay 1000)
  303. (define (main . args)
  304. (define* (change-commit-message* file-name old new #:rest rest)
  305. (let ((changelog #f))
  306. (match args
  307. ((or (message changelog) (message))
  308. (apply custom-commit-message
  309. file-name (second old) message changelog rest))
  310. (_
  311. (apply change-commit-message file-name old new rest)))))
  312. (match (diff-info)
  313. (()
  314. (display "Nothing to be done.\n" (current-error-port)))
  315. (hunks
  316. (let-values
  317. (((definitions changes)
  318. (partition hunk-definition? hunks)))
  319. ;; Additions.
  320. (for-each (lambda (hunk)
  321. (and-let*
  322. ((define-line (find (cut string-prefix? "+(define" <>)
  323. (hunk-diff-lines hunk)))
  324. (variable-name (and=> (string-tokenize define-line) second)))
  325. (add-commit-message (hunk-file-name hunk) variable-name)
  326. (let ((port (open-pipe* OPEN_WRITE
  327. "git" "apply"
  328. "--cached"
  329. "--unidiff-zero")))
  330. (hunk->patch hunk port)
  331. (unless (eqv? 0 (status:exit-val (close-pipe port)))
  332. (error "Cannot apply")))
  333. (let ((port (open-pipe* OPEN_WRITE "git" "commit" "-F" "-")))
  334. (add-commit-message (hunk-file-name hunk)
  335. variable-name port)
  336. (usleep %delay)
  337. (unless (eqv? 0 (status:exit-val (close-pipe port)))
  338. (error "Cannot commit"))))
  339. (usleep %delay))
  340. definitions)
  341. ;; Changes.
  342. (for-each (match-lambda
  343. ((new old . hunks)
  344. (for-each (lambda (hunk)
  345. (let ((port (open-pipe* OPEN_WRITE
  346. "git" "apply"
  347. "--cached"
  348. "--unidiff-zero")))
  349. (hunk->patch hunk port)
  350. (unless (eqv? 0 (status:exit-val (close-pipe port)))
  351. (error "Cannot apply")))
  352. (usleep %delay))
  353. hunks)
  354. (define copyright-line
  355. (any (lambda (line) (and=> (string-prefix? "+;;; Copyright ©" line)
  356. (const line)))
  357. (hunk-diff-lines (first hunks))))
  358. (cond
  359. (copyright-line
  360. (add-copyright-line copyright-line))
  361. (else
  362. (let ((port (open-pipe* OPEN_WRITE "git" "commit" "-F" "-")))
  363. (change-commit-message* (hunk-file-name (first hunks))
  364. old new)
  365. (change-commit-message* (hunk-file-name (first hunks))
  366. old new
  367. port)
  368. (usleep %delay)
  369. (unless (eqv? 0 (status:exit-val (close-pipe port)))
  370. (error "Cannot commit")))))))
  371. ;; XXX: we recompute the hunks here because previous
  372. ;; insertions lead to offsets.
  373. (new+old+hunks (diff-info)))))))
  374. (apply main (cdr (command-line)))