committer.scm.in 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. (define (read-excursion port)
  36. "Read an expression from PORT and reset the port position before returning
  37. the expression."
  38. (let ((start (ftell port))
  39. (result (read port)))
  40. (seek port start SEEK_SET)
  41. result))
  42. (define (surrounding-sexp port line-no)
  43. "Return the top-level S-expression surrounding the change at line number
  44. LINE-NO in PORT."
  45. (let loop ((i (1- line-no))
  46. (last-top-level-sexp #f))
  47. (if (zero? i)
  48. last-top-level-sexp
  49. (match (peek-char port)
  50. (#\(
  51. (let ((sexp (read-excursion port)))
  52. (read-line port)
  53. (loop (1- i) sexp)))
  54. (_
  55. (read-line port)
  56. (loop (1- i) last-top-level-sexp))))))
  57. (define-record-type <hunk>
  58. (make-hunk file-name
  59. old-line-number
  60. new-line-number
  61. diff-lines
  62. definition?)
  63. hunk?
  64. (file-name hunk-file-name)
  65. ;; Line number before the change
  66. (old-line-number hunk-old-line-number)
  67. ;; Line number after the change
  68. (new-line-number hunk-new-line-number)
  69. ;; The full diff to be used with "git apply --cached"
  70. (diff-lines hunk-diff-lines)
  71. ;; Does this hunk add a definition?
  72. (definition? hunk-definition?))
  73. (define* (hunk->patch hunk #:optional (port (current-output-port)))
  74. (let ((file-name (hunk-file-name hunk)))
  75. (format port
  76. "diff --git a/~a b/~a~%--- a/~a~%+++ b/~a~%~a"
  77. file-name file-name file-name file-name
  78. (string-join (hunk-diff-lines hunk) ""))))
  79. (define (diff-info)
  80. "Read the diff and return a list of <hunk> values."
  81. (let ((port (open-pipe* OPEN_READ
  82. "git" "diff-files"
  83. "--no-prefix"
  84. ;; Only include one context line to avoid lumping in
  85. ;; new definitions with changes to existing
  86. ;; definitions.
  87. "--unified=1"
  88. "gnu")))
  89. (define (extract-line-number line-tag)
  90. (abs (string->number
  91. (car (string-split line-tag #\,)))))
  92. (define (read-hunk)
  93. (let loop ((lines '())
  94. (definition? #false))
  95. (let ((line (read-line port 'concat)))
  96. (cond
  97. ((eof-object? line)
  98. (values (reverse lines) definition?))
  99. ((or (string-prefix? "@@ " line)
  100. (string-prefix? "diff --git" line))
  101. (unget-string port line)
  102. (values (reverse lines) definition?))
  103. (else
  104. (loop (cons line lines)
  105. (or definition?
  106. (string-prefix? "+(define" line))))))))
  107. (define info
  108. (let loop ((acc '())
  109. (file-name #f))
  110. (let ((line (read-line port)))
  111. (cond
  112. ((eof-object? line) acc)
  113. ((string-prefix? "--- " line)
  114. (match (string-split line #\space)
  115. ((_ file-name)
  116. (loop acc file-name))))
  117. ((string-prefix? "@@ " line)
  118. (match (string-split line #\space)
  119. ((_ old-start new-start . _)
  120. (let-values
  121. (((diff-lines definition?) (read-hunk)))
  122. (loop (cons (make-hunk file-name
  123. (extract-line-number old-start)
  124. (extract-line-number new-start)
  125. (cons (string-append line "\n")
  126. diff-lines)
  127. definition?) acc)
  128. file-name)))))
  129. (else (loop acc file-name))))))
  130. (close-pipe port)
  131. info))
  132. (define (lines-to-first-change hunk)
  133. "Return the number of diff lines until the first change."
  134. (1- (count (lambda (line)
  135. ((negate char-set-contains?)
  136. (char-set #\+ #\-)
  137. (string-ref line 0)))
  138. (hunk-diff-lines hunk))))
  139. (define (old-sexp hunk)
  140. "Using the diff information in HUNK return the unmodified S-expression
  141. corresponding to the top-level definition containing the staged changes."
  142. ;; TODO: We can't seek with a pipe port...
  143. (let* ((port (open-pipe* OPEN_READ
  144. "git" "cat-file" "-p" (string-append
  145. "HEAD:"
  146. (hunk-file-name hunk))))
  147. (contents (get-string-all port)))
  148. (close-pipe port)
  149. (call-with-input-string contents
  150. (lambda (port)
  151. (surrounding-sexp port
  152. (+ (lines-to-first-change hunk)
  153. (hunk-old-line-number hunk)))))))
  154. (define (new-sexp hunk)
  155. "Using the diff information in HUNK return the modified S-expression
  156. corresponding to the top-level definition containing the staged changes."
  157. (call-with-input-file (hunk-file-name hunk)
  158. (lambda (port)
  159. (surrounding-sexp port
  160. (+ (lines-to-first-change hunk)
  161. (hunk-new-line-number hunk))))))
  162. (define* (change-commit-message file-name old new #:optional (port (current-output-port)))
  163. "Print ChangeLog commit message for changes between OLD and NEW."
  164. (define (get-values expr field)
  165. (match ((sxpath `(// ,field quasiquote *)) expr)
  166. (() '())
  167. ((first . rest)
  168. (map cadadr first))))
  169. (define (listify items)
  170. (match items
  171. ((one) one)
  172. ((one two)
  173. (string-append one " and " two))
  174. ((one two . more)
  175. (string-append (string-join (drop-right items 1) ", ")
  176. ", and " (first (take-right items 1))))))
  177. (define variable-name
  178. (second old))
  179. (define version
  180. (and=> ((sxpath '(// version *any*)) new)
  181. first))
  182. (format port
  183. "gnu: ~a: Update to ~a.~%~%* ~a (~a): Update to ~a.~%"
  184. variable-name version file-name variable-name version)
  185. (for-each (lambda (field)
  186. (let ((old-values (get-values old field))
  187. (new-values (get-values new field)))
  188. (or (equal? old-values new-values)
  189. (let ((removed (lset-difference equal? old-values new-values))
  190. (added (lset-difference equal? new-values old-values)))
  191. (format port
  192. "[~a]: ~a~%" field
  193. (match (list (map symbol->string removed)
  194. (map symbol->string added))
  195. ((() added)
  196. (format #f "Add ~a."
  197. (listify added)))
  198. ((removed ())
  199. (format #f "Remove ~a."
  200. (listify removed)))
  201. ((removed added)
  202. (format #f "Remove ~a; add ~a."
  203. (listify removed)
  204. (listify added)))))))))
  205. '(inputs propagated-inputs native-inputs)))
  206. (define* (add-commit-message file-name variable-name #:optional (port (current-output-port)))
  207. "Print ChangeLog commit message for a change to FILE-NAME adding a definition."
  208. (format port
  209. "gnu: Add ~a.~%~%* ~a (~a): New variable.~%"
  210. variable-name file-name variable-name))
  211. (define (group-hunks-by-sexp hunks)
  212. "Return a list of pairs associating all hunks with the S-expression they are
  213. modifying."
  214. (fold (lambda (sexp hunk acc)
  215. (match acc
  216. (((previous-sexp . hunks) . rest)
  217. (if (equal? sexp previous-sexp)
  218. (cons (cons previous-sexp
  219. (cons hunk hunks))
  220. rest)
  221. (cons (cons sexp (list hunk))
  222. acc)))
  223. (_
  224. (cons (cons sexp (list hunk))
  225. acc))))
  226. '()
  227. (map new-sexp hunks)
  228. hunks))
  229. (define (new+old+hunks hunks)
  230. (map (match-lambda
  231. ((new . hunks)
  232. (cons* new (old-sexp (first hunks)) hunks)))
  233. (group-hunks-by-sexp hunks)))
  234. (define %delay 1000)
  235. (define (main . args)
  236. (match (diff-info)
  237. (()
  238. (display "Nothing to be done.\n" (current-error-port)))
  239. (hunks
  240. (let-values
  241. (((definitions changes)
  242. (partition hunk-definition? hunks)))
  243. ;; Additions.
  244. (for-each (lambda (hunk)
  245. (and-let*
  246. ((define-line (find (cut string-prefix? "+(define" <>)
  247. (hunk-diff-lines hunk)))
  248. (variable-name (and=> (string-tokenize define-line) second)))
  249. (add-commit-message (hunk-file-name hunk) variable-name)
  250. (let ((port (open-pipe* OPEN_WRITE
  251. "git" "apply"
  252. "--cached"
  253. "--unidiff-zero")))
  254. (hunk->patch hunk port)
  255. (unless (eqv? 0 (status:exit-val (close-pipe port)))
  256. (error "Cannot apply")))
  257. (let ((port (open-pipe* OPEN_WRITE "git" "commit" "-F" "-")))
  258. (add-commit-message (hunk-file-name hunk)
  259. variable-name port)
  260. (usleep %delay)
  261. (unless (eqv? 0 (status:exit-val (close-pipe port)))
  262. (error "Cannot commit"))))
  263. (usleep %delay))
  264. definitions)
  265. ;; Changes.
  266. (for-each (match-lambda
  267. ((new old . hunks)
  268. (for-each (lambda (hunk)
  269. (let ((port (open-pipe* OPEN_WRITE
  270. "git" "apply"
  271. "--cached"
  272. "--unidiff-zero")))
  273. (hunk->patch hunk port)
  274. (unless (eqv? 0 (status:exit-val (close-pipe port)))
  275. (error "Cannot apply")))
  276. (usleep %delay))
  277. hunks)
  278. (change-commit-message (hunk-file-name (first hunks))
  279. old new
  280. (current-output-port))
  281. (let ((port (open-pipe* OPEN_WRITE "git" "commit" "-F" "-")))
  282. (change-commit-message (hunk-file-name (first hunks))
  283. old new
  284. port)
  285. (usleep %delay)
  286. (unless (eqv? 0 (status:exit-val (close-pipe port)))
  287. (error "Cannot commit")))))
  288. ;; XXX: we recompute the hunks here because previous
  289. ;; insertions lead to offsets.
  290. (new+old+hunks (diff-info)))))))
  291. (main)