1 ;;; gitmerge.el --- help merge one Emacs branch into another
3 ;; Copyright (C) 2010-2018 Free Software Foundation, Inc.
5 ;; Authors: David Engster <deng@randomsample.de>
6 ;; Stefan Monnier <monnier@iro.umontreal.ca>
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
25 ;; Rewrite of bzrmerge.el, but using git.
27 ;; In a nutshell: For merging foo into master, do
29 ;; - 'git checkout master' in Emacs repository
30 ;; - Start Emacs, cd to Emacs repository
32 ;; - Choose branch 'foo' or 'origin/foo', depending on whether you
33 ;; like to merge from a local tracking branch or from the remote
34 ;; (does not make a difference if the local tracking branch is
36 ;; - Mark commits you'd like to skip, meaning to only merge their
37 ;; metadata (merge strategy 'ours').
38 ;; - Hit 'm' to start merging. Skipped commits will be merged separately.
39 ;; - If conflicts cannot be resolved automatically, you'll have to do
40 ;; it manually. In that case, resolve the conflicts and restart
41 ;; gitmerge, which will automatically resume. It will add resolved
42 ;; files, commit the pending merge and continue merging the rest.
43 ;; - Inspect master branch, and if everything looks OK, push.
48 (require 'smerge-mode
)
50 (defvar gitmerge-skip-regexp
51 ;; We used to include "sync" in there, but in my experience it only
52 ;; caused false positives. --Stef
53 (let ((skip "back[- ]?port\\|cherry picked from commit\\|\
54 \\(do\\( no\\|n['’]\\)t\\|no need to\\) merge\\|\
55 bump \\(Emacs \\)?version\\|Auto-commit"))
56 (if noninteractive skip
57 ;; "Regenerate" is quite prone to false positives.
58 ;; We only want to skip merging things like AUTHORS and ldefs-boot.
59 ;; These should be covered by "bump version" and "auto-commit".
60 ;; It doesn't do much harm if we merge one of those files by mistake.
61 ;; So it's better to err on the side of false negatives.
62 (concat skip
"\\|re-?generate\\|from trunk")))
63 "Regexp matching logs of revisions that might be skipped.
64 `gitmerge-missing' will ask you if it should skip any matches.")
66 (defvar gitmerge-minimum-missing
10
67 "Minimum number of missing commits to consider merging in batch mode.")
69 (defvar gitmerge-status-file
(expand-file-name "gitmerge-status"
71 "File where missing commits will be saved between sessions.")
73 (defvar gitmerge-ignore-branches-regexp
74 "origin/\\(\\(HEAD\\|master\\)$\\|\\(old-branches\\|other-branches\\)/\\)"
75 "Regexp matching branches we want to ignore.")
77 (defface gitmerge-skip-face
78 '((t (:strike-through t
)))
79 "Face for skipped commits.")
81 (defvar gitmerge-default-branch nil
82 "Default for branch that should be merged.
83 If nil, the function `gitmerge-default-branch' guesses.")
85 (defconst gitmerge-buffer
"*gitmerge*"
86 "Working buffer for gitmerge.")
88 (defconst gitmerge-output-buffer
"*gitmerge output*"
89 "Buffer for displaying git output.")
91 (defconst gitmerge-warning-buffer
"*gitmerge warnings*"
92 "Buffer where gitmerge will display any warnings.")
94 (defvar gitmerge-log-regexp
95 "^\\([A-Z ]\\)\\s-*\\([0-9a-f]+\\) \\(.+?\\): \\(.*\\)$")
97 (defvar gitmerge-mode-map
98 (let ((map (make-keymap)))
99 (define-key map
[(l)] 'gitmerge-show-log
)
100 (define-key map
[(d)] 'gitmerge-show-diff
)
101 (define-key map
[(f)] 'gitmerge-show-files
)
102 (define-key map
[(s)] 'gitmerge-toggle-skip
)
103 (define-key map
[(m)] 'gitmerge-start-merge
)
105 "Keymap for gitmerge major mode.")
108 (defvar gitmerge-mode-font-lock-keywords
109 `((,gitmerge-log-regexp
110 (1 font-lock-warning-face
)
111 (2 font-lock-constant-face
)
112 (3 font-lock-builtin-face
)
113 (4 font-lock-comment-face
))))
115 (defvar gitmerge--commits nil
)
116 (defvar gitmerge--from nil
)
118 (defun gitmerge-emacs-version (&optional branch
)
119 "Return the major version of Emacs, optionally in BRANCH."
122 (insert-file-contents "configure.ac")
123 (call-process "git" nil t nil
"show" (format "%s:configure.ac" branch
))
124 (goto-char (point-min)))
125 (re-search-forward "^AC_INIT([^,]+, \\([0-9]+\\)\\.")
126 (string-to-number (match-string 1))))
128 (defun gitmerge-default-branch ()
129 "Default for branch that should be merged; eg \"origin/emacs-26\"."
130 (or gitmerge-default-branch
131 (format "origin/emacs-%s" (1- (gitmerge-emacs-version)))))
133 (defun gitmerge-get-sha1 ()
134 "Get SHA1 from commit at point."
136 (goto-char (point-at-bol))
137 (when (looking-at "^[A-Z ]\\s-*\\([a-f0-9]+\\)")
140 (defun gitmerge-show-log ()
141 "Show log of commit at point."
143 (save-selected-window
144 (let ((commit (gitmerge-get-sha1)))
146 (pop-to-buffer (get-buffer-create gitmerge-output-buffer
))
149 (call-process "git" nil t nil
"log" "-1" commit
)
150 (goto-char (point-min))
151 (gitmerge-highlight-skip-regexp)))))
153 (defun gitmerge-show-diff ()
154 "Show diff of commit at point."
156 (save-selected-window
157 (let ((commit (gitmerge-get-sha1)))
159 (pop-to-buffer (get-buffer-create gitmerge-output-buffer
))
161 (call-process "git" nil t nil
"diff-tree" "-p" commit
)
162 (goto-char (point-min))
165 (defun gitmerge-show-files ()
166 "Show changed files of commit at point."
168 (save-selected-window
169 (let ((commit (gitmerge-get-sha1)))
171 (pop-to-buffer (get-buffer-create gitmerge-output-buffer
))
174 (call-process "git" nil t nil
"diff" "--name-only" (concat commit
"^!"))
175 (goto-char (point-min))))))
177 (defun gitmerge-toggle-skip ()
178 "Toggle skipping of commit at point."
180 (let ((commit (gitmerge-get-sha1))
184 (goto-char (point-at-bol))
185 (when (looking-at "^\\([A-Z ]\\)\\s-*\\([a-f0-9]+\\)")
186 (setq skip
(string= (match-string 1) " "))
187 (goto-char (match-beginning 2))
188 (gitmerge-handle-skip-overlay skip
)
189 (dolist (ct gitmerge--commits
)
190 (when (string-match commit
(car ct
))
191 (setcdr ct
(when skip
"M"))))
192 (goto-char (point-at-bol))
193 (setq buffer-read-only nil
)
195 (insert (if skip
"M" " "))
196 (setq buffer-read-only t
))))))
198 (defun gitmerge-highlight-skip-regexp ()
199 "Highlight strings that match `gitmerge-skip-regexp'."
201 (let ((case-fold-search t
))
202 (while (re-search-forward gitmerge-skip-regexp nil t
)
203 (put-text-property (match-beginning 0) (match-end 0)
204 'face
'font-lock-warning-face
)))))
206 (defun gitmerge-missing (from)
207 "Return the list of revisions that need to be merged from FROM.
208 Will detect a default set of skipped revision by looking at
209 cherry mark and search for `gitmerge-skip-regexp'. The result is
210 a list with entries of the form (SHA1 . SKIP), where SKIP denotes
211 if and why this commit should be skipped."
212 (message "Finding missing commits...")
214 ;; Go through the log and remember all commits that match
215 ;; `gitmerge-skip-regexp' or are marked by --cherry-mark.
217 (call-process "git" nil t nil
"log" "--cherry-mark" "--left-only"
219 (concat from
"..." (car (vc-git-branches))))
220 (goto-char (point-max))
221 (while (re-search-backward "^commit \\(.+\\) \\([0-9a-f]+\\).*" nil t
)
222 (let ((cherrymark (match-string 1))
223 (commit (match-string 2)))
224 (push (list commit
) commits
)
225 (if (string= cherrymark
"=")
226 ;; Commit was recognized as backported by cherry-mark.
227 (setcdr (car commits
) "C")
229 (let ((case-fold-search t
))
230 (while (not (looking-at "^\\s-+[^ ]+"))
232 (when (re-search-forward gitmerge-skip-regexp nil t
)
233 (setcdr (car commits
) "R"))))))
234 (delete-region (point) (point-max))))
235 (message "Finding missing commits...done")
238 (defun gitmerge-setup-log-buffer (commits from
)
239 "Create the buffer for choosing commits."
240 (with-current-buffer (get-buffer-create gitmerge-buffer
)
242 (call-process "git" nil t nil
"log" "--left-only"
243 "--pretty=format:%h %<(20,trunc) %an: %<(100,trunc) %s"
244 (concat from
"..." (car (vc-git-branches))))
245 (goto-char (point-min))
246 (while (looking-at "^\\([a-f0-9]+\\)")
247 (let ((skipreason (gitmerge-skip-commit-p (match-string 1) commits
)))
248 (if (null skipreason
)
250 (insert skipreason
" ")
251 (gitmerge-handle-skip-overlay t
)))
255 (defun gitmerge-handle-skip-overlay (skip)
256 "Create or delete overlay on SHA1, depending on SKIP."
257 (when (looking-at "[0-9a-f]+")
259 (let ((ov (make-overlay (point)
261 (overlay-put ov
'face
'gitmerge-skip-face
))
262 (remove-overlays (point) (match-end 0)
263 'face
'gitmerge-skip-face
))))
265 (defun gitmerge-skip-commit-p (commit skips
)
266 "Tell whether COMMIT should be skipped.
267 COMMIT is an (possibly abbreviated) SHA1. SKIPS is list of
268 cons'es with commits that should be skipped and the reason.
269 Return value is string which denotes reason, or nil if commit
270 should not be skipped."
272 (while (and (setq skip
(pop skips
))
274 (when (string-match commit
(car skip
))
275 (setq found
(cdr skip
))))
278 (defun gitmerge-resolve (file)
279 "Try to resolve conflicts in FILE with smerge.
280 Returns non-nil if conflicts remain."
281 (unless (file-exists-p file
) (error "Gitmerge-resolve: Can't find %s" file
))
283 (let ((exists (find-buffer-visiting file
)))
284 (with-current-buffer (let ((enable-local-variables :safe
)
285 (enable-local-eval nil
))
286 (find-file-noselect file
))
287 (if (buffer-modified-p)
288 (user-error "Unsaved changes in %s" (current-buffer)))
291 ((derived-mode-p 'change-log-mode
)
292 ;; Fix up dates before resolving the conflicts.
293 (goto-char (point-min))
294 (let ((diff-auto-refine-mode nil
))
295 (while (re-search-forward smerge-begin-re nil t
)
296 (smerge-match-conflict)
297 (smerge-ensure-match 3)
298 (let ((start1 (match-beginning 1))
300 (start3 (match-beginning 3))
301 (end3 (copy-marker (match-end 3) t
)))
303 (while (re-search-forward change-log-start-entry-re end3 t
)
304 (let* ((str (match-string 0))
305 (newstr (save-match-data
306 (concat (add-log-iso8601-time-string)
307 (when (string-match " *\\'" str
)
308 (match-string 0 str
))))))
309 (replace-match newstr t t
)))
310 ;; change-log-resolve-conflict prefers to put match-1's
311 ;; elements first (for equal dates), whereas we want to put
313 (let ((match3 (buffer-substring start3 end3
))
314 (match1 (buffer-substring start1 end1
)))
315 (delete-region start3 end3
)
318 (delete-region start1 end1
)
321 ;; (pop-to-buffer (current-buffer)) (debug 'before-resolve)
323 ;; Try to resolve the conflicts.
326 ((and (equal file
"etc/NEWS")
330 (gitmerge-emacs-version gitmerge--from
))))
333 (y-or-n-p "Try to fix NEWS conflict? ")))
334 (let ((relfile (file-name-nondirectory file
))
335 (tempfile (make-temp-file "gitmerge")))
338 (call-process "git" nil
`(:file
,tempfile
) nil
"diff"
339 (format ":1:%s" file
)
340 (format ":3:%s" file
))
341 (call-process "git" nil t nil
"reset" "--" relfile
)
342 (call-process "git" nil t nil
"checkout" "--" relfile
)
343 (revert-buffer nil
'noconfirm
)
344 (call-process "patch" tempfile nil nil temp
)
345 (call-process "git" nil t nil
"add" "--" temp
))
346 (delete-file tempfile
))))
348 ((member file
'("lisp/ldefs-boot.el"))
349 ;; We are in the file's buffer, so names are relative.
350 (call-process "git" nil t nil
"reset" "--"
351 (file-name-nondirectory file
))
352 (call-process "git" nil t nil
"checkout" "--"
353 (file-name-nondirectory file
))
354 (revert-buffer nil
'noconfirm
))
356 (goto-char (point-max))
357 (while (re-search-backward smerge-begin-re nil t
)
360 (smerge-match-conflict)
362 ;; (when (derived-mode-p 'change-log-mode)
363 ;; (pop-to-buffer (current-buffer)) (debug 'after-resolve))
365 (goto-char (point-min))
366 (prog1 (re-search-forward smerge-begin-re nil t
)
367 (unless exists
(kill-buffer))))))))
369 (defun gitmerge-commit-message (beg end skip branch
)
370 "Create commit message for merging BEG to END from BRANCH.
371 SKIP denotes whether those commits are actually skipped. If END
372 is nil, only the single commit BEG is merged."
374 ;; We do not insert "; " for non-skipped messages,
375 ;; because the date of those entries is helpful in figuring out
376 ;; when things got merged, since git does not track that.
377 (insert (if skip
"; " "")
378 "Merge from " branch
"\n\n"
380 (concat "The following commit"
381 (if end
"s were " " was ")
384 (apply 'call-process
"git" nil t nil
"log" "--oneline"
385 (if end
(list (concat beg
"~.." end
))
388 ;; Truncate to 72 chars so that the resulting ChangeLog line fits in 80.
389 (goto-char (point-min))
390 (while (re-search-forward "^\\(.\\{69\\}\\).\\{4,\\}" nil t
)
391 (replace-match "\\1..."))
394 (defun gitmerge-apply (missing from
)
395 "Merge commits in MISSING from branch FROM.
396 MISSING must be a list of SHA1 strings."
397 (with-current-buffer (get-buffer-create gitmerge-output-buffer
)
399 (let* ((skip (cdar missing
))
400 (beg (car (pop missing
)))
402 ;; Determine last revision with same boolean skip status.
404 (eq (null (cdar missing
))
406 (setq end
(car (pop missing
))))
408 (gitmerge-commit-message beg end skip from
))
410 (if skip
"Skipping" "Merging")
412 (if end
(concat ".." (substring end
0 6)) ""))
416 (apply 'call-process
"git" nil t nil
"merge" "--no-ff"
417 (append (when skip
'("-s" "ours"))
418 `("-m" ,commitmessage
,end
))))
419 (gitmerge-write-missing missing from
)
420 (gitmerge-resolve-unmerged)))
423 (defun gitmerge-resolve-unmerged ()
424 "Resolve all files that are unmerged.
425 Throw an user-error if we cannot resolve automatically."
426 (with-current-buffer (get-buffer-create gitmerge-output-buffer
)
428 (let (files conflicted
)
429 ;; List unmerged files
431 (call-process "git" nil t nil
432 "diff" "--name-only" "--diff-filter=U")))
433 (error "Error listing unmerged files. Resolve manually.")
434 (goto-char (point-min))
436 (push (buffer-substring (point) (line-end-position)) files
)
439 (if (gitmerge-resolve file
)
440 ;; File still has conflicts
443 (call-process "git" nil t nil
"add" file
)))
445 (and files
(not (gitmerge-commit))
446 (error "Error committing resolution - fix it manually"))
447 (with-current-buffer (get-buffer-create gitmerge-warning-buffer
)
449 (insert "For the following files, conflicts could\n"
450 "not be resolved automatically:\n\n")
453 (call-process "git" nil t nil
454 "diff" "--name-only" "--diff-filter=U")
457 (if noninteractive
(message "Conflicts in:\n%s" conflicts
)))
458 (insert "\nResolve the conflicts manually, then run gitmerge again."
459 "\nNote:\n - You don't have to add resolved files or "
460 "commit the merge yourself (but you can)."
461 "\n - You can safely close this Emacs session and do this "
463 "\n - When running gitmerge again, remember that you must "
464 "do that from within the Emacs repo.\n")
465 (pop-to-buffer (current-buffer)))
466 (user-error "Resolve the conflicts manually"))))))
468 (defun gitmerge-repo-clean ()
469 "Return non-nil if repository is clean."
471 (call-process "git" nil t nil
472 "diff" "--staged" "--name-only")
473 (call-process "git" nil t nil
474 "diff" "--name-only")
475 (zerop (buffer-size))))
477 (defun gitmerge-commit ()
478 "Commit, and return non-nil if it succeeds."
479 (with-current-buffer (get-buffer-create gitmerge-output-buffer
)
481 (eq 0 (call-process "git" nil t nil
"commit" "--no-edit"))))
483 (defun gitmerge-maybe-resume ()
484 "Check if we have to resume a merge.
485 If so, add no longer conflicted files and commit."
486 (let ((mergehead (file-exists-p
487 (expand-file-name ".git/MERGE_HEAD" default-directory
)))
488 (statusexist (file-exists-p gitmerge-status-file
)))
489 (when (and mergehead
(not statusexist
))
490 (user-error "Unfinished merge, but no record of a previous gitmerge run"))
491 (when (and (not mergehead
)
492 (not (gitmerge-repo-clean)))
493 (user-error "Repository is not clean"))
495 (if (or noninteractive
(not (y-or-n-p "Resume merge? ")))
497 (delete-file gitmerge-status-file
)
500 (message "OK, resuming...")
501 (gitmerge-resolve-unmerged)
504 (or (gitmerge-commit)
505 (error "Git error during merge - fix it manually")))
506 ;; Successfully resumed.
509 (defun gitmerge-get-all-branches ()
510 "Return list of all branches, including remotes."
512 (unless (zerop (call-process "git" nil t nil
514 (error "Git error listing remote branches"))
515 (goto-char (point-min))
516 (let (branches branch
)
518 (when (looking-at "^[^\\*]\\s-*\\(?:remotes/\\)?\\(.+\\)$")
519 (setq branch
(match-string 1))
520 (unless (string-match gitmerge-ignore-branches-regexp branch
)
521 (push branch branches
)))
523 (nreverse branches
))))
525 (defun gitmerge-write-missing (missing from
)
526 "Write list of commits MISSING into `gitmerge-status-file'.
527 Branch FROM will be prepended to the list."
529 (find-file-noselect gitmerge-status-file
)
532 (prin1-to-string (append (list from
) missing
))
537 (defun gitmerge-read-missing ()
538 "Read list of missing commits from `gitmerge-status-file'."
540 (find-file-noselect gitmerge-status-file
)
541 (unless (zerop (buffer-size))
542 (prog1 (read (buffer-string))
545 (define-derived-mode gitmerge-mode special-mode
"gitmerge"
546 "Major mode for Emacs branch merging."
547 (set-syntax-table text-mode-syntax-table
)
548 (setq buffer-read-only t
)
549 (setq-local truncate-lines t
)
550 (setq-local font-lock-defaults
'(gitmerge-mode-font-lock-keywords)))
552 (defun gitmerge (from)
553 "Merge from branch FROM into `default-directory'."
555 (if (not (vc-git-root default-directory
))
556 (user-error "Not in a git tree")
557 (let ((default-directory (vc-git-root default-directory
)))
559 (if (gitmerge-maybe-resume)
562 (or (pop command-line-args-left
)
563 (gitmerge-default-branch))
564 (completing-read "Merge branch: "
565 (gitmerge-get-all-branches)
566 nil t
(gitmerge-default-branch))))))))
567 (let ((default-directory (vc-git-root default-directory
)))
568 (if (eq from
'resume
)
570 (setq gitmerge--commits
(gitmerge-read-missing))
571 (setq gitmerge--from
(pop gitmerge--commits
))
572 ;; Directly continue with the merge.
573 (gitmerge-start-merge))
574 (setq gitmerge--commits
(gitmerge-missing from
))
575 (setq gitmerge--from from
)
576 (when (null gitmerge--commits
)
577 (user-error "Nothing to merge"))
579 gitmerge-minimum-missing
580 (< (length gitmerge--commits
) gitmerge-minimum-missing
)
581 (user-error "Number of missing commits (%s) is less than %s"
582 (length gitmerge--commits
)
583 gitmerge-minimum-missing
))
585 (gitmerge-setup-log-buffer gitmerge--commits gitmerge--from
)
586 (goto-char (point-min))
587 (insert (propertize "Commands: " 'font-lock-face
'bold
)
588 "(s) Toggle skip, (l) Show log, (d) Show diff, "
589 "(f) Show files, (m) Start merge\n"
590 (propertize "Flags: " 'font-lock-face
'bold
)
591 "(C) Detected backport (cherry-mark), (R) Log matches "
592 "regexp, (M) Manually picked\n\n")
594 (pop-to-buffer (current-buffer))
595 (if noninteractive
(gitmerge-start-merge))))))
597 (defun gitmerge-start-merge ()
599 (when (not (vc-git-root default-directory
))
600 (user-error "Not in a git tree"))
601 (let ((default-directory (vc-git-root default-directory
)))
602 (while gitmerge--commits
603 (setq gitmerge--commits
604 (gitmerge-apply gitmerge--commits gitmerge--from
)))
605 (when (file-exists-p gitmerge-status-file
)
606 (delete-file gitmerge-status-file
))
607 (message "Merging from %s...done" gitmerge--from
)))
611 ;;; gitmerge.el ends here