committer.scm.in 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #!@GUILE@ \
  2. --no-auto-compile -s
  3. !#
  4. ;;; GNU Guix --- Functional package management for GNU
  5. ;;; Copyright © 2020 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-9)
  27. (ice-9 format)
  28. (ice-9 popen)
  29. (ice-9 match)
  30. (ice-9 rdelim)
  31. (ice-9 textual-ports))
  32. (define (read-excursion port)
  33. "Read an expression from PORT and reset the port position before returning
  34. the expression."
  35. (let ((start (ftell port))
  36. (result (read port)))
  37. (seek port start SEEK_SET)
  38. result))
  39. (define (surrounding-sexp port line-no)
  40. "Return the top-level S-expression surrounding the change at line number
  41. LINE-NO in PORT."
  42. (let loop ((i (1- line-no))
  43. (last-top-level-sexp #f))
  44. (if (zero? i)
  45. last-top-level-sexp
  46. (match (peek-char port)
  47. (#\(
  48. (let ((sexp (read-excursion port)))
  49. (read-line port)
  50. (loop (1- i) sexp)))
  51. (_
  52. (read-line port)
  53. (loop (1- i) last-top-level-sexp))))))
  54. (define-record-type <hunk>
  55. (make-hunk file-name
  56. old-line-number
  57. new-line-number
  58. diff)
  59. hunk?
  60. (file-name hunk-file-name)
  61. ;; Line number before the change
  62. (old-line-number hunk-old-line-number)
  63. ;; Line number after the change
  64. (new-line-number hunk-new-line-number)
  65. ;; The full diff to be used with "git apply --cached"
  66. (diff hunk-diff))
  67. (define* (hunk->patch hunk #:optional (port (current-output-port)))
  68. (let ((file-name (hunk-file-name hunk)))
  69. (format port
  70. "diff --git a/~a b/~a~%--- a/~a~%+++ b/~a~%~a"
  71. file-name file-name file-name file-name
  72. (hunk-diff hunk))))
  73. (define (diff-info)
  74. "Read the diff and return a list of <hunk> values."
  75. (let ((port (open-pipe* OPEN_READ
  76. "git" "diff"
  77. "--no-prefix"
  78. ;; Do not include any context lines. This makes it
  79. ;; easier to find the S-expression surrounding the
  80. ;; change.
  81. "--unified=0")))
  82. (define (extract-line-number line-tag)
  83. (abs (string->number
  84. (car (string-split line-tag #\,)))))
  85. (define (read-hunk)
  86. (reverse
  87. (let loop ((lines '()))
  88. (let ((line (read-line port 'concat)))
  89. (cond
  90. ((eof-object? line) lines)
  91. ((or (string-prefix? "@@ " line)
  92. (string-prefix? "diff --git" line))
  93. (unget-string port line)
  94. lines)
  95. (else (loop (cons line lines))))))))
  96. (define info
  97. (let loop ((acc '())
  98. (file-name #f))
  99. (let ((line (read-line port)))
  100. (cond
  101. ((eof-object? line) acc)
  102. ((string-prefix? "--- " line)
  103. (match (string-split line #\space)
  104. ((_ file-name)
  105. (loop acc file-name))))
  106. ((string-prefix? "@@ " line)
  107. (match (string-split line #\space)
  108. ((_ old-start new-start . _)
  109. (loop (cons (make-hunk file-name
  110. (extract-line-number old-start)
  111. (extract-line-number new-start)
  112. (string-join (cons* line "\n"
  113. (read-hunk)) ""))
  114. acc)
  115. file-name))))
  116. (else (loop acc file-name))))))
  117. (close-pipe port)
  118. info))
  119. (define (old-sexp hunk)
  120. "Using the diff information in HUNK return the unmodified S-expression
  121. corresponding to the top-level definition containing the staged changes."
  122. ;; TODO: We can't seek with a pipe port...
  123. (let* ((port (open-pipe* OPEN_READ
  124. "git" "show" (string-append "HEAD:"
  125. (hunk-file-name hunk))))
  126. (contents (get-string-all port)))
  127. (close-pipe port)
  128. (call-with-input-string contents
  129. (lambda (port)
  130. (surrounding-sexp port (hunk-old-line-number hunk))))))
  131. (define (new-sexp hunk)
  132. "Using the diff information in HUNK return the modified S-expression
  133. corresponding to the top-level definition containing the staged changes."
  134. (call-with-input-file (hunk-file-name hunk)
  135. (lambda (port)
  136. (surrounding-sexp port
  137. (hunk-new-line-number hunk)))))
  138. (define* (commit-message file-name old new #:optional (port (current-output-port)))
  139. "Print ChangeLog commit message for changes between OLD and NEW."
  140. (define (get-values expr field)
  141. (match ((sxpath `(// ,field quasiquote *)) expr)
  142. (() '())
  143. ((first . rest)
  144. (map cadadr first))))
  145. (define (listify items)
  146. (match items
  147. ((one) one)
  148. ((one two)
  149. (string-append one " and " two))
  150. ((one two . more)
  151. (string-append (string-join (drop-right items 1) ", ")
  152. ", and " (first (take-right items 1))))))
  153. (define variable-name
  154. (second old))
  155. (define version
  156. (and=> ((sxpath '(// version *any*)) new)
  157. first))
  158. (format port
  159. "gnu: ~a: Update to ~a.~%~%* ~a (~a): Update to ~a.~%"
  160. variable-name version file-name variable-name version)
  161. (for-each (lambda (field)
  162. (let ((old-values (get-values old field))
  163. (new-values (get-values new field)))
  164. (or (equal? old-values new-values)
  165. (let ((removed (lset-difference equal? old-values new-values))
  166. (added (lset-difference equal? new-values old-values)))
  167. (format port
  168. "[~a]: ~a~%" field
  169. (match (list (map symbol->string removed)
  170. (map symbol->string added))
  171. ((() added)
  172. (format #f "Add ~a."
  173. (listify added)))
  174. ((removed ())
  175. (format #f "Remove ~a."
  176. (listify removed)))
  177. ((removed added)
  178. (format #f "Remove ~a; add ~a."
  179. (listify removed)
  180. (listify added)))))))))
  181. '(inputs propagated-inputs native-inputs)))
  182. (define (group-hunks-by-sexp hunks)
  183. "Return a list of pairs associating all hunks with the S-expression they are
  184. modifying."
  185. (fold (lambda (sexp hunk acc)
  186. (match acc
  187. (((previous-sexp . hunks) . rest)
  188. (if (equal? sexp previous-sexp)
  189. (cons (cons previous-sexp
  190. (cons hunk hunks))
  191. rest)
  192. (cons (cons sexp (list hunk))
  193. acc)))
  194. (_
  195. (cons (cons sexp (list hunk))
  196. acc))))
  197. '()
  198. (map new-sexp hunks)
  199. hunks))
  200. (define (new+old+hunks hunks)
  201. (map (match-lambda
  202. ((new . hunks)
  203. (cons* new (old-sexp (first hunks)) hunks)))
  204. (group-hunks-by-sexp hunks)))
  205. (define (main . args)
  206. (match (diff-info)
  207. (()
  208. (display "Nothing to be done." (current-error-port)))
  209. (hunks
  210. (for-each (match-lambda
  211. ((new old . hunks)
  212. (for-each (lambda (hunk)
  213. (let ((port (open-pipe* OPEN_WRITE
  214. "git" "apply"
  215. "--cached"
  216. "--unidiff-zero")))
  217. (hunk->patch hunk port)
  218. (unless (eqv? 0 (status:exit-val (close-pipe port)))
  219. (error "Cannot apply")))
  220. (sleep 1))
  221. hunks)
  222. (commit-message (hunk-file-name (first hunks))
  223. old new
  224. (current-output-port))
  225. (let ((port (open-pipe* OPEN_WRITE "git" "commit" "-F" "-")))
  226. (commit-message (hunk-file-name (first hunks))
  227. old new
  228. port)
  229. (sleep 1)
  230. (unless (eqv? 0 (status:exit-val (close-pipe port)))
  231. (error "Cannot commit")))))
  232. (new+old+hunks hunks)))))
  233. (main)