committer.scm.in 13 KB

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