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