Avoid error in TLS connections due to incorrect format
[emacs.git] / admin / gitmerge.el
blobc8cf2dcc565bba770c5cd99359a03e6e8500072c
1 ;;; gitmerge.el --- help merge one Emacs branch into another
3 ;; Copyright (C) 2010-2015 Free Software Foundation, Inc.
5 ;; Authors: David Engster <deng@randomsample.de>
6 ;; Stefan Monnier <monnier@iro.umontreal.ca>
8 ;; Keywords: maint
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 <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
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
31 ;; - M-x gitmerge
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
35 ;; up-to-date).
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.
45 ;;; Code:
47 (require 'vc-git)
48 (require 'smerge-mode)
50 (defvar gitmerge-skip-regexp
51 "back[- ]?port\\|merge\\|sync\\|re-?generate\\|bump version\\|from trunk\\|\
52 Auto-commit"
53 "Regexp matching logs of revisions that might be skipped.
54 `gitmerge-missing' will ask you if it should skip any matches.")
56 (defvar gitmerge-status-file (expand-file-name "gitmerge-status"
57 user-emacs-directory)
58 "File where missing commits will be saved between sessions.")
60 (defvar gitmerge-ignore-branches-regexp
61 "origin/\\(\\(HEAD\\|master\\)$\\|\\(old-branches\\|other-branches\\)/\\)"
62 "Regexp matching branches we want to ignore.")
64 (defface gitmerge-skip-face
65 '((t (:strike-through t)))
66 "Face for skipped commits.")
68 (defconst gitmerge-default-branch "origin/emacs-24"
69 "Default for branch that should be merged.")
71 (defconst gitmerge-buffer "*gitmerge*"
72 "Working buffer for gitmerge.")
74 (defconst gitmerge-output-buffer "*gitmerge output*"
75 "Buffer for displaying git output.")
77 (defconst gitmerge-warning-buffer "*gitmerge warnings*"
78 "Buffer where gitmerge will display any warnings.")
80 (defvar gitmerge-log-regexp
81 "^\\([A-Z ]\\)\\s-*\\([0-9a-f]+\\) \\(.+?\\): \\(.*\\)$")
83 (defvar gitmerge-mode-map
84 (let ((map (make-keymap)))
85 (define-key map [(l)] 'gitmerge-show-log)
86 (define-key map [(d)] 'gitmerge-show-diff)
87 (define-key map [(f)] 'gitmerge-show-files)
88 (define-key map [(s)] 'gitmerge-toggle-skip)
89 (define-key map [(m)] 'gitmerge-start-merge)
90 map)
91 "Keymap for gitmerge major mode.")
94 (defvar gitmerge-mode-font-lock-keywords
95 `((,gitmerge-log-regexp
96 (1 font-lock-warning-face)
97 (2 font-lock-constant-face)
98 (3 font-lock-builtin-face)
99 (4 font-lock-comment-face))))
101 (defvar gitmerge--commits nil)
102 (defvar gitmerge--from nil)
104 (defun gitmerge-get-sha1 ()
105 "Get SHA1 from commit at point."
106 (save-excursion
107 (goto-char (point-at-bol))
108 (when (looking-at "^[A-Z ]\\s-*\\([a-f0-9]+\\)")
109 (match-string 1))))
111 (defun gitmerge-show-log ()
112 "Show log of commit at point."
113 (interactive)
114 (save-selected-window
115 (let ((commit (gitmerge-get-sha1)))
116 (when commit
117 (pop-to-buffer (get-buffer-create gitmerge-output-buffer))
118 (fundamental-mode)
119 (erase-buffer)
120 (call-process "git" nil t nil "log" "-1" commit)
121 (goto-char (point-min))
122 (gitmerge-highlight-skip-regexp)))))
124 (defun gitmerge-show-diff ()
125 "Show diff of commit at point."
126 (interactive)
127 (save-selected-window
128 (let ((commit (gitmerge-get-sha1)))
129 (when commit
130 (pop-to-buffer (get-buffer-create gitmerge-output-buffer))
131 (erase-buffer)
132 (call-process "git" nil t nil "diff-tree" "-p" commit)
133 (goto-char (point-min))
134 (diff-mode)))))
136 (defun gitmerge-show-files ()
137 "Show changed files of commit at point."
138 (interactive)
139 (save-selected-window
140 (let ((commit (gitmerge-get-sha1)))
141 (when commit
142 (pop-to-buffer (get-buffer-create gitmerge-output-buffer))
143 (erase-buffer)
144 (fundamental-mode)
145 (call-process "git" nil t nil "diff" "--name-only" (concat commit "^!"))
146 (goto-char (point-min))))))
148 (defun gitmerge-toggle-skip ()
149 "Toggle skipping of commit at point."
150 (interactive)
151 (let ((commit (gitmerge-get-sha1))
152 skip)
153 (when commit
154 (save-excursion
155 (goto-char (point-at-bol))
156 (when (looking-at "^\\([A-Z ]\\)\\s-*\\([a-f0-9]+\\)")
157 (setq skip (string= (match-string 1) " "))
158 (goto-char (match-beginning 2))
159 (gitmerge-handle-skip-overlay skip)
160 (dolist (ct gitmerge--commits)
161 (when (string-match commit (car ct))
162 (setcdr ct (when skip "M"))))
163 (goto-char (point-at-bol))
164 (setq buffer-read-only nil)
165 (delete-char 1)
166 (insert (if skip "M" " "))
167 (setq buffer-read-only t))))))
169 (defun gitmerge-highlight-skip-regexp ()
170 "Highlight strings that match `gitmerge-skip-regexp'."
171 (save-excursion
172 (while (re-search-forward gitmerge-skip-regexp nil t)
173 (put-text-property (match-beginning 0) (match-end 0)
174 'face 'font-lock-warning-face))))
176 (defun gitmerge-missing (from)
177 "Return the list of revisions that need to be merged from FROM.
178 Will detect a default set of skipped revision by looking at
179 cherry mark and search for `gitmerge-skip-regexp'. The result is
180 a list with entries of the form (SHA1 . SKIP), where SKIP denotes
181 if and why this commit should be skipped."
182 (let (commits)
183 ;; Go through the log and remember all commits that match
184 ;; `gitmerge-skip-regexp' or are marked by --cherry-mark.
185 (with-temp-buffer
186 (call-process "git" nil t nil "log" "--cherry-mark" from
187 (concat "^" (car (vc-git-branches))))
188 (goto-char (point-max))
189 (while (re-search-backward "^commit \\(.+\\) \\([0-9a-f]+\\).*" nil t)
190 (let ((cherrymark (match-string 1))
191 (commit (match-string 2)))
192 (push (list commit) commits)
193 (if (string= cherrymark "=")
194 ;; Commit was recognized as backported by cherry-mark.
195 (setcdr (car commits) "C")
196 (save-excursion
197 (let ((case-fold-search t))
198 (while (not (looking-at "^\\s-+[^ ]+"))
199 (forward-line))
200 (when (re-search-forward gitmerge-skip-regexp nil t)
201 (setcdr (car commits) "R"))))))
202 (delete-region (point) (point-max))))
203 (nreverse commits)))
205 (defun gitmerge-setup-log-buffer (commits from)
206 "Create the buffer for choosing commits."
207 (with-current-buffer (get-buffer-create gitmerge-buffer)
208 (erase-buffer)
209 (call-process "git" nil t nil "log"
210 "--pretty=format:%h %<(20,trunc) %an: %<(100,trunc) %s"
211 from (concat "^" (car (vc-git-branches))))
212 (goto-char (point-min))
213 (while (looking-at "^\\([a-f0-9]+\\)")
214 (let ((skipreason (gitmerge-skip-commit-p (match-string 1) commits)))
215 (if (null skipreason)
216 (insert " ")
217 (insert skipreason " ")
218 (gitmerge-handle-skip-overlay t)))
219 (forward-line))
220 (current-buffer)))
222 (defun gitmerge-handle-skip-overlay (skip)
223 "Create or delete overlay on SHA1, depending on SKIP."
224 (when (looking-at "[0-9a-f]+")
225 (if skip
226 (let ((ov (make-overlay (point)
227 (match-end 0))))
228 (overlay-put ov 'face 'gitmerge-skip-face))
229 (remove-overlays (point) (match-end 0)
230 'face 'gitmerge-skip-face))))
232 (defun gitmerge-skip-commit-p (commit skips)
233 "Tell whether COMMIT should be skipped.
234 COMMIT is an (possibly abbreviated) SHA1. SKIPS is list of
235 cons'es with commits that should be skipped and the reason.
236 Return value is string which denotes reason, or nil if commit
237 should not be skipped."
238 (let (found skip)
239 (while (and (setq skip (pop skips))
240 (not found))
241 (when (string-match commit (car skip))
242 (setq found (cdr skip))))
243 found))
245 (defun gitmerge-resolve (file)
246 "Try to resolve conflicts in FILE with smerge.
247 Returns non-nil if conflicts remain."
248 (unless (file-exists-p file) (error "Gitmerge-resolve: Can't find %s" file))
249 (with-demoted-errors
250 (let ((exists (find-buffer-visiting file)))
251 (with-current-buffer (let ((enable-local-variables :safe)
252 (enable-local-eval nil))
253 (find-file-noselect file))
254 (if (buffer-modified-p)
255 (user-error "Unsaved changes in %s" (current-buffer)))
256 (save-excursion
257 (cond
258 ((derived-mode-p 'change-log-mode)
259 ;; Fix up dates before resolving the conflicts.
260 (goto-char (point-min))
261 (let ((diff-auto-refine-mode nil))
262 (while (re-search-forward smerge-begin-re nil t)
263 (smerge-match-conflict)
264 (smerge-ensure-match 3)
265 (let ((start1 (match-beginning 1))
266 (end1 (match-end 1))
267 (start3 (match-beginning 3))
268 (end3 (copy-marker (match-end 3) t)))
269 (goto-char start3)
270 (while (re-search-forward change-log-start-entry-re end3 t)
271 (let* ((str (match-string 0))
272 (newstr (save-match-data
273 (concat (add-log-iso8601-time-string)
274 (when (string-match " *\\'" str)
275 (match-string 0 str))))))
276 (replace-match newstr t t)))
277 ;; change-log-resolve-conflict prefers to put match-1's
278 ;; elements first (for equal dates), whereas we want to put
279 ;; match-3's first.
280 (let ((match3 (buffer-substring start3 end3))
281 (match1 (buffer-substring start1 end1)))
282 (delete-region start3 end3)
283 (goto-char start3)
284 (insert match1)
285 (delete-region start1 end1)
286 (goto-char start1)
287 (insert match3)))))
288 ;; (pop-to-buffer (current-buffer)) (debug 'before-resolve)
290 ;; Try to resolve the conflicts.
291 (cond
292 ((member file '("configure" "lisp/ldefs-boot.el"
293 "lisp/emacs-lisp/cl-loaddefs.el"))
294 ;; We are in the file's buffer, so names are relative.
295 (call-process "git" nil t nil "checkout" "--"
296 (file-name-nondirectory file))
297 (revert-buffer nil 'noconfirm))
299 (goto-char (point-max))
300 (while (re-search-backward smerge-begin-re nil t)
301 (save-excursion
302 (ignore-errors
303 (smerge-match-conflict)
304 (smerge-resolve))))
305 ;; (when (derived-mode-p 'change-log-mode)
306 ;; (pop-to-buffer (current-buffer)) (debug 'after-resolve))
307 (save-buffer)))
308 (goto-char (point-min))
309 (prog1 (re-search-forward smerge-begin-re nil t)
310 (unless exists (kill-buffer))))))))
312 (defun gitmerge-commit-message (beg end skip branch)
313 "Create commit message for merging BEG to END from BRANCH.
314 SKIP denotes whether those commits are actually skipped. If END
315 is nil, only the single commit BEG is merged."
316 (with-temp-buffer
317 ;; We do not insert "; " for non-skipped messages,
318 ;; because the date of those entries is helpful in figuring out
319 ;; when things got merged, since git does not track that.
320 (insert (if skip "; " "")
321 "Merge from " branch "\n\n"
322 (if skip
323 (concat "The following commit"
324 (if end "s were " " was ")
325 "skipped:\n\n")
326 ""))
327 (apply 'call-process "git" nil t nil "log" "--oneline"
328 (if end (list (concat beg "~.." end))
329 `("-1" ,beg)))
330 (insert "\n")
331 (buffer-string)))
333 (defun gitmerge-apply (missing from)
334 "Merge commits in MISSING from branch FROM.
335 MISSING must be a list of SHA1 strings."
336 (with-current-buffer (get-buffer-create gitmerge-output-buffer)
337 (erase-buffer)
338 (let* ((skip (cdar missing))
339 (beg (car (pop missing)))
340 end commitmessage)
341 ;; Determine last revision with same boolean skip status.
342 (while (and missing
343 (eq (null (cdar missing))
344 (null skip)))
345 (setq end (car (pop missing))))
346 (setq commitmessage
347 (gitmerge-commit-message beg end skip from))
348 (message "%s %s%s"
349 (if skip "Skipping" "Merging")
350 (substring beg 0 6)
351 (if end (concat ".." (substring end 0 6)) ""))
352 (unless end
353 (setq end beg))
354 (unless (zerop
355 (apply 'call-process "git" nil t nil "merge" "--no-ff"
356 (append (when skip '("-s" "ours"))
357 `("-m" ,commitmessage ,end))))
358 (gitmerge-write-missing missing from)
359 (gitmerge-resolve-unmerged)))
360 missing))
362 (defun gitmerge-resolve-unmerged ()
363 "Resolve all files that are unmerged.
364 Throw an user-error if we cannot resolve automatically."
365 (with-current-buffer (get-buffer-create gitmerge-output-buffer)
366 (erase-buffer)
367 (let (files conflicted)
368 ;; List unmerged files
369 (if (not (zerop
370 (call-process "git" nil t nil
371 "diff" "--name-only" "--diff-filter=U")))
372 (error "Error listing unmerged files. Resolve manually.")
373 (goto-char (point-min))
374 (while (not (eobp))
375 (push (buffer-substring (point) (line-end-position)) files)
376 (forward-line))
377 (dolist (file files)
378 (if (gitmerge-resolve file)
379 ;; File still has conflicts
380 (setq conflicted t)
381 ;; Mark as resolved
382 (call-process "git" nil t nil "add" file)))
383 (when conflicted
384 (with-current-buffer (get-buffer-create gitmerge-warning-buffer)
385 (erase-buffer)
386 (insert "For the following files, conflicts could\n"
387 "not be resolved automatically:\n\n")
388 (call-process "git" nil t nil
389 "diff" "--name-only" "--diff-filter=U")
390 (insert "\nResolve the conflicts manually, then run gitmerge again."
391 "\nNote:\n - You don't have to add resolved files or "
392 "commit the merge yourself (but you can)."
393 "\n - You can safely close this Emacs session and do this "
394 "in a new one."
395 "\n - When running gitmerge again, remember that you must "
396 "that from within the Emacs repo.\n")
397 (pop-to-buffer (current-buffer)))
398 (user-error "Resolve the conflicts manually"))))))
400 (defun gitmerge-repo-clean ()
401 "Return non-nil if repository is clean."
402 (with-temp-buffer
403 (call-process "git" nil t nil
404 "diff" "--staged" "--name-only")
405 (call-process "git" nil t nil
406 "diff" "--name-only")
407 (zerop (buffer-size))))
409 (defun gitmerge-maybe-resume ()
410 "Check if we have to resume a merge.
411 If so, add no longer conflicted files and commit."
412 (let ((mergehead (file-exists-p
413 (expand-file-name ".git/MERGE_HEAD" default-directory)))
414 (statusexist (file-exists-p gitmerge-status-file)))
415 (when (and mergehead (not statusexist))
416 (user-error "Unfinished merge, but no record of a previous gitmerge run"))
417 (when (and (not mergehead)
418 (not (gitmerge-repo-clean)))
419 (user-error "Repository is not clean"))
420 (when statusexist
421 (if (not (y-or-n-p "Resume merge? "))
422 (progn
423 (delete-file gitmerge-status-file)
424 ;; No resume.
425 nil)
426 (message "OK, resuming...")
427 (gitmerge-resolve-unmerged)
428 ;; Commit the merge.
429 (when mergehead
430 (with-current-buffer (get-buffer-create gitmerge-output-buffer)
431 (erase-buffer)
432 (unless (zerop (call-process "git" nil t nil
433 "commit" "--no-edit"))
434 (error "Git error during merge - fix it manually"))))
435 ;; Successfully resumed.
436 t))))
438 (defun gitmerge-get-all-branches ()
439 "Return list of all branches, including remotes."
440 (with-temp-buffer
441 (unless (zerop (call-process "git" nil t nil
442 "branch" "-a"))
443 (error "Git error listing remote branches"))
444 (goto-char (point-min))
445 (let (branches branch)
446 (while (not (eobp))
447 (when (looking-at "^[^\\*]\\s-*\\(?:remotes/\\)?\\(.+\\)$")
448 (setq branch (match-string 1))
449 (unless (string-match gitmerge-ignore-branches-regexp branch)
450 (push branch branches)))
451 (forward-line))
452 (nreverse branches))))
454 (defun gitmerge-write-missing (missing from)
455 "Write list of commits MISSING into `gitmerge-status-file'.
456 Branch FROM will be prepended to the list."
457 (with-current-buffer
458 (find-file-noselect gitmerge-status-file)
459 (erase-buffer)
460 (insert
461 (prin1-to-string (append (list from) missing))
462 "\n")
463 (save-buffer)
464 (kill-buffer)))
466 (defun gitmerge-read-missing ()
467 "Read list of missing commits from `gitmerge-status-file'."
468 (with-current-buffer
469 (find-file-noselect gitmerge-status-file)
470 (unless (zerop (buffer-size))
471 (prog1 (read (buffer-string))
472 (kill-buffer)))))
474 (define-derived-mode gitmerge-mode special-mode "gitmerge"
475 "Major mode for Emacs branch merging."
476 (set-syntax-table text-mode-syntax-table)
477 (setq buffer-read-only t)
478 (setq-local truncate-lines t)
479 (setq-local font-lock-defaults '(gitmerge-mode-font-lock-keywords)))
481 (defun gitmerge (from)
482 "Merge from branch FROM into `default-directory'."
483 (interactive
484 (if (not (vc-git-root default-directory))
485 (user-error "Not in a git tree")
486 (let ((default-directory (vc-git-root default-directory)))
487 (list
488 (if (gitmerge-maybe-resume)
489 'resume
490 (completing-read "Merge branch: " (gitmerge-get-all-branches)
491 nil t gitmerge-default-branch))))))
492 (let ((default-directory (vc-git-root default-directory)))
493 (if (eq from 'resume)
494 (progn
495 (setq gitmerge--commits (gitmerge-read-missing))
496 (setq gitmerge--from (pop gitmerge--commits))
497 ;; Directly continue with the merge.
498 (gitmerge-start-merge))
499 (setq gitmerge--commits (gitmerge-missing from))
500 (setq gitmerge--from from)
501 (when (null gitmerge--commits)
502 (user-error "Nothing to merge"))
503 (with-current-buffer
504 (gitmerge-setup-log-buffer gitmerge--commits gitmerge--from)
505 (goto-char (point-min))
506 (insert (propertize "Commands: " 'font-lock-face 'bold)
507 "(s) Toggle skip, (l) Show log, (d) Show diff, "
508 "(f) Show files, (m) Start merge\n"
509 (propertize "Flags: " 'font-lock-face 'bold)
510 "(C) Detected backport (cherry-mark), (R) Log matches "
511 "regexp, (M) Manually picked\n\n")
512 (gitmerge-mode)
513 (pop-to-buffer (current-buffer))))))
515 (defun gitmerge-start-merge ()
516 (interactive)
517 (when (not (vc-git-root default-directory))
518 (user-error "Not in a git tree"))
519 (let ((default-directory (vc-git-root default-directory)))
520 (while gitmerge--commits
521 (setq gitmerge--commits
522 (gitmerge-apply gitmerge--commits gitmerge--from)))
523 (when (file-exists-p gitmerge-status-file)
524 (delete-file gitmerge-status-file))
525 (message "Merging from %s...done" gitmerge--from)))
527 (provide 'gitmerge)
529 ;;; gitmerge.el ends here