3 ;; Copyright (C) 2010, 2011 Free Software Foundation, Inc.
5 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
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/>.
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*")
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"))
43 (if (not (re-search-forward "^pending merges:\n" nil t
))
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.
53 (skip-chars-forward " ")
54 (push (buffer-substring (point) (line-end-position)) 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.
66 (if (re-search-forward "^author: *\\([^<]*[^ ]\\) +<.*"
70 (if (re-search-forward
71 "^committer: *\\([^<]*[^< ]\\) +<" nil t
)
75 (if (re-search-forward
76 "^timestamp:[^0-9]*\\([-0-9]+\\)" nil t
)
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))))
87 merge
(concat author
" " timestamp
" " line1
))
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*")
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
)
106 (while (not (bzrmerge-check-match (pop merges
)))
108 (error "Unmatched tip of merged revisions")))
109 (let ((case-fold-search t
)
110 (revno (match-string 1))
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
)
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? ")))
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
))
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)))
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))
151 (start3 (match-beginning 3))
152 (end3 (copy-marker (match-end 3) t
)))
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
164 (let ((match3 (buffer-substring start3 end3
))
165 (match1 (buffer-substring start1 end1
)))
166 (delete-region start3 end3
)
169 (delete-region start1 end1
)
172 ;; (pop-to-buffer (current-buffer)) (debug 'before-resolve)
174 ;; Try to resolve the conflicts.
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
)
185 (smerge-match-conflict)
187 ;; (when (derived-mode-p 'change-log-mode)
188 ;; (pop-to-buffer (current-buffer)) (debug 'after-resolve))
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*")
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
))
223 (when (or merge skip
)
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").
255 (pop-to-buffer (current-buffer))
257 ;; (debug 'after-merge)
258 ;; Check the conflicts.
259 (let ((conflicted nil
)
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)))
269 (if files
(setq conflicted t
)))
271 (if (bzrmerge-resolve file
)
272 (setq conflicted t
)))
274 (setq bzrmerge-already-done
275 (list (cons merge skip
) from missing
))
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
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'."
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
)
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
)))
308 (setq missing
(bzrmerge-apply missing from
))))))
311 ;;; bzrmerge.el ends here