Fix the ChangeLog message to be more legible.
[bpt/emacs.git] / admin / bzrmerge.el
1 ;;; bzrmerge.el ---
2
3 ;; Copyright (C) 2010-2011 Free Software Foundation, Inc.
4
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
6 ;; Keywords:
7
8 ;; GNU Emacs is free software: you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
12
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
20
21 ;;; Commentary:
22
23 ;;
24
25 ;;; Code:
26
27 (eval-when-compile
28 (require 'cl)) ; assert
29
30 (defvar bzrmerge-skip-regexp
31 "back[- ]?port\\|merge\\|sync\\|re-?generate\\|bump version"
32 "Regexp matching logs of revisions that might be skipped.
33 `bzrmerge-missing' will ask you if it should skip any matches.")
34
35 (defun bzrmerge-merges ()
36 "Return the list of already merged (not yet committed) revisions.
37 The list returned is sorted by oldest-first."
38 (with-current-buffer (get-buffer-create "*bzrmerge*")
39 (erase-buffer)
40 ;; We generally want to make sure we start with a clean tree, but we also
41 ;; want to allow restarts (i.e. with some part of FROM already merged but
42 ;; not yet committed).
43 (call-process "bzr" nil t nil "status" "-v")
44 (goto-char (point-min))
45 (when (re-search-forward "^conflicts:\n" nil t)
46 (error "You still have unresolved conflicts"))
47 (let ((merges ()))
48 (if (not (re-search-forward "^pending merges:\n" nil t))
49 (when (save-excursion
50 (goto-char (point-min))
51 (re-search-forward "^[a-z ]*:\n" nil t))
52 (error "You still have uncommitted changes"))
53 ;; This is really stupid, but it seems there's no easy way to figure
54 ;; out which revisions have been merged already. The only info I can
55 ;; find is the "pending merges" from "bzr status -v", which is not
56 ;; very machine-friendly.
57 (while (not (eobp))
58 (skip-chars-forward " ")
59 (push (buffer-substring (point) (line-end-position)) merges)
60 (forward-line 1)))
61 merges)))
62
63 (defun bzrmerge-check-match (merge)
64 ;; Make sure the MERGES match the revisions on the FROM branch.
65 ;; Stupidly the best form of MERGES I can find is the one from
66 ;; "bzr status -v" which is very machine non-friendly, so I have
67 ;; to do some fuzzy matching.
68 (let ((author
69 (or
70 (save-excursion
71 (if (re-search-forward "^author: *\\([^<]*[^ ]\\) +<.*"
72 nil t)
73 (match-string 1)))
74 (save-excursion
75 (if (re-search-forward
76 "^committer: *\\([^<]*[^< ]\\) +<" nil t)
77 (match-string 1)))))
78 (timestamp
79 (save-excursion
80 (if (re-search-forward
81 "^timestamp:[^0-9]*\\([-0-9]+\\)" nil t)
82 (match-string 1))))
83 (line1
84 (save-excursion
85 (if (re-search-forward "^message:[ \n]*" nil t)
86 (buffer-substring (point) (line-end-position))))))
87 ;; The `merge' may have a truncated line1 with "...", so get
88 ;; rid of any "..." and then look for a prefix match.
89 (when (string-match "\\.+\\'" merge)
90 (setq merge (substring merge 0 (match-beginning 0))))
91 (or (string-prefix-p
92 merge (concat author " " timestamp " " line1))
93 (string-prefix-p
94 merge (concat author " " timestamp " [merge] " line1)))))
95
96 (defun bzrmerge-missing (from merges)
97 "Return the list of revisions that need to be merged.
98 MERGES is the revisions already merged but not yet committed.
99 Asks about skipping revisions with logs matching `bzrmerge-skip-regexp'.
100 The result is of the form (TOMERGE . TOSKIP) where TOMERGE and TOSKIP
101 are both lists of revnos, in oldest-first order."
102 (with-current-buffer (get-buffer-create "*bzrmerge*")
103 (erase-buffer)
104 (call-process "bzr" nil t nil "missing" "--theirs-only"
105 (expand-file-name from))
106 (let ((revnos ()) (skipped ()))
107 (pop-to-buffer (current-buffer))
108 (goto-char (point-max))
109 (while (re-search-backward "^------------------------------------------------------------\nrevno: \\([0-9.]+\\).*" nil t)
110 (save-excursion
111 (if merges
112 (while (not (bzrmerge-check-match (pop merges)))
113 (unless merges
114 (error "Unmatched tip of merged revisions")))
115 (let ((case-fold-search t)
116 (revno (match-string 1))
117 (skip nil))
118 (if (string-match "\\." revno)
119 (error "Unexpected dotted revno!")
120 (setq revno (string-to-number revno)))
121 (re-search-forward "^message:\n")
122 (while (and (not skip)
123 (re-search-forward bzrmerge-skip-regexp nil t))
124 (let ((str (buffer-substring (line-beginning-position)
125 (line-end-position))))
126 (when (string-match "\\` *" str)
127 (setq str (substring str (match-end 0))))
128 (when (string-match "[.!;, ]+\\'" str)
129 (setq str (substring str 0 (match-beginning 0))))
130 (if (save-excursion (y-or-n-p (concat str ": Skip? ")))
131 (setq skip t))))
132 (if skip
133 (push revno skipped)
134 (push revno revnos)))))
135 (delete-region (point) (point-max)))
136 (cons (nreverse revnos) (nreverse skipped)))))
137
138 (defun bzrmerge-resolve (file)
139 (unless (file-exists-p file) (error "Bzrmerge-resolve: Can't find %s" file))
140 (with-demoted-errors
141 (let ((exists (find-buffer-visiting file)))
142 (with-current-buffer (find-file-noselect file)
143 (if (buffer-modified-p)
144 (error "Unsaved changes in %s" (current-buffer)))
145 (save-excursion
146 (cond
147 ((derived-mode-p 'change-log-mode)
148 ;; Fix up dates before resolving the conflicts.
149 (goto-char (point-min))
150 (let ((diff-auto-refine-mode nil))
151 (while (re-search-forward smerge-begin-re nil t)
152 (smerge-match-conflict)
153 (smerge-ensure-match 3)
154 (let ((start1 (match-beginning 1))
155 (end1 (match-end 1))
156 (start3 (match-beginning 3))
157 (end3 (copy-marker (match-end 3) t)))
158 (goto-char start3)
159 (while (re-search-forward change-log-start-entry-re end3 t)
160 (let* ((str (match-string 0))
161 (newstr (save-match-data
162 (concat (add-log-iso8601-time-string)
163 (when (string-match " *\\'" str)
164 (match-string 0 str))))))
165 (replace-match newstr t t)))
166 ;; change-log-resolve-conflict prefers to put match-1's
167 ;; elements first (for equal dates), whereas we want to put
168 ;; match-3's first.
169 (let ((match3 (buffer-substring start3 end3))
170 (match1 (buffer-substring start1 end1)))
171 (delete-region start3 end3)
172 (goto-char start3)
173 (insert match1)
174 (delete-region start1 end1)
175 (goto-char start1)
176 (insert match3)))))
177 ;; (pop-to-buffer (current-buffer)) (debug 'before-resolve)
178 ))
179 ;; Try to resolve the conflicts.
180 (cond
181 ((member file '("configure" "lisp/ldefs-boot.el"
182 "lisp/emacs-lisp/cl-loaddefs.el"))
183 (call-process "bzr" nil t nil "revert" file)
184 (revert-buffer nil 'noconfirm))
185 (t
186 (goto-char (point-max))
187 (while (re-search-backward smerge-begin-re nil t)
188 (save-excursion
189 (ignore-errors
190 (smerge-match-conflict)
191 (smerge-resolve))))
192 ;; (when (derived-mode-p 'change-log-mode)
193 ;; (pop-to-buffer (current-buffer)) (debug 'after-resolve))
194 (save-buffer)))
195 (goto-char (point-min))
196 (prog1 (re-search-forward smerge-begin-re nil t)
197 (unless exists (kill-buffer))))))))
198
199 (defun bzrmerge-add-metadata (from endrevno)
200 "Add the metadata for a merge of FROM upto ENDREVNO.
201 Does not make other difference."
202 (if (with-temp-buffer
203 (call-process "bzr" nil t nil "status")
204 (goto-char (point-min))
205 (re-search-forward "^conflicts:\n" nil t))
206 (error "Don't know how to add metadata in the presence of conflicts")
207 (call-process "bzr" nil t nil "shelve" "--all"
208 "-m" "Bzrmerge shelved merge during skipping")
209 (call-process "bzr" nil t nil "revert")
210 (call-process "bzr" nil t nil
211 "merge" "-r" (format "%s" endrevno) from)
212 (call-process "bzr" nil t nil "revert" ".")
213 (call-process "bzr" nil t nil "unshelve")))
214
215 (defvar bzrmerge-already-done nil)
216
217 (defun bzrmerge-apply (missing from)
218 (setq from (expand-file-name from))
219 (with-current-buffer (get-buffer-create "*bzrmerge*")
220 (erase-buffer)
221 (when (equal (cdr bzrmerge-already-done) (list from missing))
222 (setq missing (car bzrmerge-already-done)))
223 (setq bzrmerge-already-done nil)
224 (let ((merge (car missing))
225 (skip (cdr missing))
226 (unsafe nil)
227 beg end)
228 (when (or merge skip)
229 (cond
230 ((and skip (or (null merge) (< (car skip) (car merge))))
231 ;; Do a "skip" (i.e. merge the meta-data only).
232 (setq beg (1- (car skip)))
233 (while (and skip (or (null merge) (< (car skip) (car merge))))
234 (assert (> (car skip) (or end beg)))
235 (setq end (pop skip)))
236 (message "Skipping %s..%s" beg end)
237 (bzrmerge-add-metadata from end))
238
239 (t
240 ;; Do a "normal" merge.
241 (assert (or (null skip) (< (car merge) (car skip))))
242 (setq beg (1- (car merge)))
243 (while (and merge (or (null skip) (< (car merge) (car skip))))
244 (assert (> (car merge) (or end beg)))
245 (setq end (pop merge)))
246 (message "Merging %s..%s" beg end)
247 (if (with-temp-buffer
248 (call-process "bzr" nil t nil "status")
249 (zerop (buffer-size)))
250 (call-process "bzr" nil t nil
251 "merge" "-r" (format "%s" end) from)
252 ;; Stupidly, "bzr merge --force -r A..B" dos not maintain the
253 ;; metadata properly except when the checkout is clean.
254 (call-process "bzr" nil t nil "merge"
255 "--force" "-r" (format "%s..%s" beg end) from)
256 ;; The merge did not update the metadata, so force the next time
257 ;; around to update it (as a "skip").
258 (setq unsafe t)
259 (push end skip))
260 (pop-to-buffer (current-buffer))
261 (sit-for 1)
262 ;; (debug 'after-merge)
263 ;; Check the conflicts.
264 (let ((conflicted nil)
265 (files ()))
266 (goto-char (point-min))
267 (when (re-search-forward "bzr: ERROR:" nil t)
268 (error "Internal Bazaar error!!"))
269 (while (re-search-forward "^Text conflict in " nil t)
270 (push (buffer-substring (point) (line-end-position)) files))
271 (if (re-search-forward "^\\([0-9]+\\) conflicts encountered" nil t)
272 (if (/= (length files) (string-to-number (match-string 1)))
273 (setq conflicted t))
274 (if files (setq conflicted t)))
275 (dolist (file files)
276 (if (bzrmerge-resolve file)
277 (setq conflicted t)))
278 (when conflicted
279 (setq bzrmerge-already-done
280 (list (cons merge skip) from missing))
281 (if unsafe
282 ;; FIXME: Obviously, we'd rather make it right rather
283 ;; than output such a warning. But I don't know how to add
284 ;; the metadata to bzr's since the technique used in
285 ;; bzrmerge-add-metadata does not work when there
286 ;; are conflicts.
287 (display-warning 'bzrmerge "Resolve conflicts manually.
288 ¡BEWARE! Important metadata is kept in this Emacs session!
289 Do not commit without re-running `M-x bzrmerge' first!"))
290 (error "Resolve conflicts manually")))))
291 (cons merge skip)))))
292
293 (defun bzrmerge (from)
294 "Merge from branch FROM into `default-directory'."
295 (interactive
296 (list
297 (let ((def
298 (with-temp-buffer
299 (call-process "bzr" nil t nil "info")
300 (goto-char (point-min))
301 (when (re-search-forward "submit branch: *" nil t)
302 (buffer-substring (point) (line-end-position))))))
303 (read-file-name "From branch: " nil nil nil def))))
304 (message "Merging from %s..." from)
305 (require 'vc-bzr)
306 (let ((default-directory (or (vc-bzr-root default-directory)
307 (error "Not in a Bzr tree"))))
308 ;; First, check the status.
309 (let* ((merges (bzrmerge-merges))
310 ;; OK, we have the status, now check the missing data.
311 (missing (bzrmerge-missing from merges)))
312 (while missing
313 (setq missing (bzrmerge-apply missing from))))))
314
315 (provide 'bzrmerge)
316 ;;; bzrmerge.el ends here