git-rebase.el 22.1 KB
Newer Older
1
;;; git-rebase.el --- Edit Git rebase files  -*- lexical-binding: t -*-
2

Jonas Bernoulli's avatar
Jonas Bernoulli committed
3
;; Copyright (C) 2010-2018  The Magit Project Contributors
4
;;
5 6
;; You should have received a copy of the AUTHORS.md file which
;; lists all contributors.  If not, see http://magit.vc/authors.
7

8
;; Author: Phil Jackson <phil@shellarchive.co.uk>
Jonas Bernoulli's avatar
Jonas Bernoulli committed
9
;; Maintainer: Jonas Bernoulli <jonas@bernoul.li>
10

11 12 13 14
;; This file is not part of GNU Emacs.

;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
15 16
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
17 18 19 20 21 22

;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

23
;; You should have received a copy of the GNU General Public License
24
;; along with this file.  If not, see <http://www.gnu.org/licenses/>.
25 26 27

;;; Commentary:

28 29 30
;; This package assists the user in editing the list of commits to be
;; rewritten during an interactive rebase.

31
;; When the user initiates an interactive rebase, e.g. using "r e" in
32 33 34 35 36 37 38 39 40 41 42 43
;; a Magit buffer or on the command line using "git rebase -i REV",
;; Git invokes the `$GIT_SEQUENCE_EDITOR' (or if that is undefined
;; `$GIT_EDITOR' or even `$EDITOR') letting the user rearrange, drop,
;; reword, edit, and squash commits.

;; This package provides the major-mode `git-rebase-mode' which makes
;; doing so much more fun, by making the buffer more colorful and
;; providing the following commands:
;;
;;   C-c C-c  Tell Git to make it happen.
;;   C-c C-k  Tell Git that you changed your mind, i.e. abort.
;;
44 45 46
;;   p        Move point to previous line.
;;   n        Move point to next line.
;;
47 48 49 50
;;   M-p      Move the commit at point up.
;;   M-n      Move the commit at point down.
;;
;;   k        Drop the commit at point.
51
;;   c        Don't drop the commit at point.
52 53 54 55 56 57
;;   r        Change the message of the commit at point.
;;   e        Edit the commit at point.
;;   s        Squash the commit at point, into the one above.
;;   f        Like "s" but don't also edit the commit message.
;;   x        Add a script to be run with the commit at point
;;            being checked out.
58
;;   z        Add noop action at point.
59
;;
60 61 62
;;   SPC      Show the commit at point in another buffer.
;;   RET      Show the commit at point in another buffer and
;;            select its window.
63 64 65
;;   C-/      Undo last change.

;; You should probably also read the `git-rebase' manpage.
66 67 68

;;; Code:

69
(require 'dash)
70 71
(require 'easymenu)
(require 'server)
Jonas Bernoulli's avatar
Jonas Bernoulli committed
72
(require 'with-editor)
73
(require 'magit)
74

75 76 77 78 79
(and (require 'async-bytecomp nil t)
     (memq 'magit (bound-and-true-p async-bytecomp-allowed-packages))
     (fboundp 'async-bytecomp-package-mode)
     (async-bytecomp-package-mode 1))

80 81
(eval-when-compile (require 'recentf))

Jonas Bernoulli's avatar
Jonas Bernoulli committed
82
;;; Options
83
;;;; Variables
Jonas Bernoulli's avatar
Jonas Bernoulli committed
84

85
(defgroup git-rebase nil
86
  "Edit Git rebase sequences."
87
  :link '(info-link "(magit)Editing Rebase Sequences")
88 89
  :group 'tools)

90 91
(defcustom git-rebase-auto-advance t
  "Whether to move to next line after changing a line."
Jonas Bernoulli's avatar
Jonas Bernoulli committed
92
  :group 'git-rebase
93 94
  :type 'boolean)

95
(defcustom git-rebase-show-instructions t
96
  "Whether to show usage instructions inside the rebase buffer."
97 98 99
  :group 'git-rebase
  :type 'boolean)

Jonas Bernoulli's avatar
Jonas Bernoulli committed
100 101 102 103 104
(defcustom git-rebase-confirm-cancel t
  "Whether confirmation is required to cancel."
  :group 'git-rebase
  :type 'boolean)

105 106
;;;; Faces

107
(defgroup git-rebase-faces nil
108
  "Faces used by Git-Rebase mode."
109
  :group 'faces
110
  :group 'git-rebase)
111

112
(defface git-rebase-hash '((t (:inherit magit-hash)))
113 114 115
  "Face for commit hashes."
  :group 'git-rebase-faces)

116
(defface git-rebase-description nil
117
  "Face for commit descriptions."
118 119
  :group 'git-rebase-faces)

120
(defface git-rebase-killed-action
121
  '((t (:inherit font-lock-comment-face :strike-through t)))
122
  "Face for commented action and exec lines."
123
  :group 'git-rebase-faces)
124

125 126 127 128 129 130 131
(defface git-rebase-comment-hash
  '((t (:inherit git-rebase-hash :weight bold)))
  "Face for commit hashes in commit message comments."
  :group 'git-rebase-faces)

(defface git-rebase-comment-heading
  '((t :inherit font-lock-keyword-face))
132
  "Face for headings in rebase message comments."
133 134
  :group 'git-commit-faces)

Jonas Bernoulli's avatar
Jonas Bernoulli committed
135 136
;;; Keymaps

137 138
(defvar git-rebase-mode-map
  (let ((map (make-sparse-keymap)))
139
    (set-keymap-parent map special-mode-map)
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
    (cond ((featurep 'jkl)
           (define-key map [return]    'git-rebase-show-commit)
           (define-key map (kbd   "i") 'git-rebase-backward-line)
           (define-key map (kbd   "k") 'forward-line)
           (define-key map (kbd "M-i") 'git-rebase-move-line-up)
           (define-key map (kbd "M-k") 'git-rebase-move-line-down)
           (define-key map (kbd   "p") 'git-rebase-pick)
           (define-key map (kbd   ",") 'git-rebase-kill-line))
          (t
           (define-key map (kbd "C-m") 'git-rebase-show-commit)
           (define-key map (kbd   "p") 'git-rebase-backward-line)
           (define-key map (kbd   "n") 'forward-line)
           (define-key map (kbd "M-p") 'git-rebase-move-line-up)
           (define-key map (kbd "M-n") 'git-rebase-move-line-down)
           (define-key map (kbd   "c") 'git-rebase-pick)
           (define-key map (kbd   "k") 'git-rebase-kill-line)
           (define-key map (kbd "C-k") 'git-rebase-kill-line)))
157 158 159 160 161 162 163 164 165
    (define-key map (kbd "e") 'git-rebase-edit)
    (define-key map (kbd "m") 'git-rebase-edit)
    (define-key map (kbd "f") 'git-rebase-fixup)
    (define-key map (kbd "q") 'undefined)
    (define-key map (kbd "r") 'git-rebase-reword)
    (define-key map (kbd "w") 'git-rebase-reword)
    (define-key map (kbd "s") 'git-rebase-squash)
    (define-key map (kbd "x") 'git-rebase-exec)
    (define-key map (kbd "y") 'git-rebase-insert)
166
    (define-key map (kbd "z") 'git-rebase-noop)
167 168 169 170 171 172
    (define-key map (kbd "SPC")     'git-rebase-show-or-scroll-up)
    (define-key map (kbd "DEL")     'git-rebase-show-or-scroll-down)
    (define-key map (kbd "C-x C-t") 'git-rebase-move-line-up)
    (define-key map [M-up]          'git-rebase-move-line-up)
    (define-key map [M-down]        'git-rebase-move-line-down)
    (define-key map [remap undo]    'git-rebase-undo)
173
    map)
174
  "Keymap for Git-Rebase mode.")
175

176 177 178 179 180 181 182 183
(cond ((featurep 'jkl)
       (put 'git-rebase-reword       :advertised-binding "r")
       (put 'git-rebase-move-line-up :advertised-binding (kbd "M-i"))
       (put 'git-rebase-kill-line    :advertised-binding ","))
      (t
       (put 'git-rebase-reword       :advertised-binding "r")
       (put 'git-rebase-move-line-up :advertised-binding (kbd "M-p"))
       (put 'git-rebase-kill-line    :advertised-binding "k")))
184

185 186 187 188 189 190 191 192 193
(easy-menu-define git-rebase-mode-menu git-rebase-mode-map
  "Git-Rebase mode menu"
  '("Rebase"
    ["Pick" git-rebase-pick t]
    ["Reword" git-rebase-reword t]
    ["Edit" git-rebase-edit t]
    ["Squash" git-rebase-squash t]
    ["Fixup" git-rebase-fixup t]
    ["Kill" git-rebase-kill-line t]
194
    ["Noop" git-rebase-noop t]
195
    ["Execute" git-rebase-exec t]
196 197 198
    ["Move Down" git-rebase-move-line-down t]
    ["Move Up" git-rebase-move-line-up t]
    "---"
Jonas Bernoulli's avatar
Jonas Bernoulli committed
199 200
    ["Cancel" with-editor-cancel t]
    ["Finish" with-editor-finish t]))
201

202
(defvar git-rebase-command-descriptions
203 204 205 206 207 208 209 210 211 212
  '((with-editor-finish           . "tell Git to make it happen")
    (with-editor-cancel           . "tell Git that you changed your mind, i.e. abort")
    (git-rebase-backward-line     . "move point to previous line")
    (forward-line                 . "move point to next line")
    (git-rebase-move-line-up      . "move the commit at point up")
    (git-rebase-move-line-down    . "move the commit at point down")
    (git-rebase-show-or-scroll-up . "show the commit at point in another buffer")
    (git-rebase-show-commit
     . "show the commit at point in another buffer and select its window")
    (undo                         . "undo last change")
213
    (git-rebase-kill-line         . "drop the commit at point")
214
    (git-rebase-insert            . "insert a line for an arbitrary commit")
215
    (git-rebase-noop              . "add noop action at point")))
216

Jonas Bernoulli's avatar
Jonas Bernoulli committed
217 218
;;; Commands

219 220 221
(defun git-rebase-pick ()
  "Use commit on current line."
  (interactive)
222
  (git-rebase-set-action "pick"))
223 224 225 226

(defun git-rebase-reword ()
  "Edit message of commit on current line."
  (interactive)
227
  (git-rebase-set-action "reword"))
228 229 230 231

(defun git-rebase-edit ()
  "Stop at the commit on the current line."
  (interactive)
232
  (git-rebase-set-action "edit"))
233 234 235 236

(defun git-rebase-squash ()
  "Meld commit on current line into previous commit, edit message."
  (interactive)
237
  (git-rebase-set-action "squash"))
238 239 240 241

(defun git-rebase-fixup ()
  "Meld commit on current line into previous commit, discard its message."
  (interactive)
242
  (git-rebase-set-action "fixup"))
243

244 245
(defvar-local git-rebase-line nil)
(defvar-local git-rebase-comment-re nil)
246

247
(defun git-rebase-set-action (action)
248 249
  (goto-char (line-beginning-position))
  (if (and (looking-at git-rebase-line)
250
           (not (string-match-p "\\(e\\|exec\\|noop\\)$" (match-string 1))))
251 252 253 254 255 256
      (let ((inhibit-read-only t))
        (replace-match action t t nil 1)
        (when git-rebase-auto-advance
          (forward-line)))
    (ding)))

257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
(defun git-rebase-line-p (&optional pos)
  (save-excursion
    (when pos (goto-char pos))
    (goto-char (line-beginning-position))
    (looking-at-p git-rebase-line)))

(defun git-rebase-region-bounds ()
  (when (use-region-p)
    (let ((beg (save-excursion (goto-char (region-beginning))
                               (line-beginning-position)))
          (end (save-excursion (goto-char (region-end))
                               (line-end-position))))
      (when (and (git-rebase-line-p beg)
                 (git-rebase-line-p end))
        (list beg (1+ end))))))

273 274
(defun git-rebase-move-line-down (n)
  "Move the current commit (or command) N lines down.
275 276 277
If N is negative, move the commit up instead.  With an active
region, move all the lines that the region touches, not just the
current line."
278
  (interactive "p")
279 280 281 282 283
  (-let* (((beg end) (or (git-rebase-region-bounds)
                         (list (line-beginning-position)
                               (1+ (line-end-position)))))
          (pt-offset (- (point) beg))
          (mark-offset (and mark-active (- (mark) beg))))
284 285 286 287 288 289 290 291
    (save-restriction
      (narrow-to-region
       (point-min)
       (1+ (save-excursion
             (goto-char (point-min))
             (while (re-search-forward git-rebase-line nil t))
             (point))))
      (if (or (and (< n 0) (= beg (point-min)))
292 293
              (and (> n 0) (= end (point-max)))
              (> end (point-max)))
294 295 296 297 298 299
          (ding)
        (goto-char (if (< n 0) beg end))
        (forward-line n)
        (atomic-change-group
          (let ((inhibit-read-only t))
            (insert (delete-and-extract-region beg end)))
300 301 302 303 304
          (let ((new-beg (- (point) (- end beg))))
            (when (use-region-p)
              (setq deactivate-mark nil)
              (set-mark (+ new-beg mark-offset)))
            (goto-char (+ new-beg pt-offset))))))))
305 306 307

(defun git-rebase-move-line-up (n)
  "Move the current commit (or command) N lines up.
308 309 310
If N is negative, move the commit down instead.  With an active
region, move all the lines that the region touches, not just the
current line."
311 312
  (interactive "p")
  (git-rebase-move-line-down (- n)))
313

314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
(defun git-rebase-highlight-region (start end window rol)
  (let ((inhibit-read-only t)
        (deactivate-mark nil)
        (bounds (git-rebase-region-bounds)))
    (mapc #'delete-overlay magit-section-highlight-overlays)
    (when bounds
      (magit-section-make-overlay (car bounds) (cadr bounds)
                                  'magit-section-heading-selection))
    (if (and bounds (not magit-keep-region-overlay))
        (funcall (default-value 'redisplay-unhighlight-region-function) rol)
      (funcall (default-value 'redisplay-highlight-region-function)
               start end window rol))))

(defun git-rebase-unhighlight-region (rol)
  (mapc #'delete-overlay magit-section-highlight-overlays)
  (funcall (default-value 'redisplay-unhighlight-region-function) rol))

331 332 333
(defun git-rebase-kill-line ()
  "Kill the current action line."
  (interactive)
334 335
  (goto-char (line-beginning-position))
  (when (and (looking-at git-rebase-line)
336
             (not (eq (char-after) (string-to-char comment-start))))
337
    (let ((inhibit-read-only t))
338 339
      (insert comment-start)
      (insert " "))
340 341
    (when git-rebase-auto-advance
      (forward-line))))
342

343 344
(defun git-rebase-insert (rev)
  "Read an arbitrary commit and insert it below current line."
345
  (interactive (list (magit-read-branch-or-commit "Insert revision")))
346
  (forward-line)
347
  (--if-let (magit-rev-format "%h %s" rev)
348 349 350
      (let ((inhibit-read-only t))
        (insert "pick " it ?\n))
    (user-error "Unknown revision")))
351

352 353
(defun git-rebase-exec (arg)
  "Insert a shell command to be run after the proceeding commit.
354

355 356 357 358
If there already is such a command on the current line, then edit
that instead.  With a prefix argument insert a new command even
when there already is one on the current line.  With empty input
remove the command on the current line, if any."
359
  (interactive "P")
360 361 362
  (let ((inhibit-read-only t) initial command)
    (unless arg
      (goto-char (line-beginning-position))
363 364
      (when (looking-at (concat git-rebase-comment-re "?"
                                "\\(e\\|exec\\) \\(.*\\)"))
365 366 367
        (setq initial (match-string-no-properties 2))))
    (setq command (read-shell-command "Execute: " initial))
    (pcase (list command initial)
368
      (`("" nil) (ding))
369 370 371 372 373 374 375 376
      (`(""  ,_)
       (delete-region (match-beginning 0) (1+ (match-end 0))))
      (`(,_ nil)
       (forward-line)
       (insert (concat "exec " command "\n"))
       (unless git-rebase-auto-advance
         (forward-line -1)))
      (_
377
       (replace-match (concat "exec " command) t t)
378 379 380
       (if git-rebase-auto-advance
           (forward-line)
         (goto-char (line-beginning-position)))))))
381

382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
(defun git-rebase-noop (&optional arg)
  "Add noop action at point.

If the current line already contains a a noop action, leave it
unchanged.  If there is a commented noop action present, remove
the comment.  Otherwise add a new noop action.  With a prefix
argument insert a new noop action regardless what is already
present on the current line.

A noop action can be used to make git perform a rebase even if
no commits are selected.  Without the noop action present, git
would see an empty file and therefore do nothing."
  (interactive "P")
  (goto-char (line-beginning-position))
  ;; The extra space at the end is only there to make the action
  ;; consistent with the others (action argument). This keeps
  ;; the regexp `git-rebase-line' from getting complicated.
  (let ((noop-string "noop \n"))
    (when (or arg (not (looking-at noop-string)))
      (let ((inhibit-read-only t))
        (if (and (not arg)
                 (looking-at (concat comment-start noop-string)))
            (delete-char 1)
          (insert noop-string))))))

407
(defun git-rebase-undo (&optional arg)
408 409
  "Undo some previous changes.
Like `undo' but works in read-only buffers."
410 411 412 413
  (interactive "P")
  (let ((inhibit-read-only t))
    (undo arg)))

414 415 416 417 418 419
(defun git-rebase--show-commit (&optional scroll)
  (let ((disable-magit-save-buffers t))
    (save-excursion
      (goto-char (line-beginning-position))
      (--if-let (and (looking-at git-rebase-line)
                     (match-string 2))
420 421 422 423
          (pcase scroll
            (`up   (magit-diff-show-or-scroll-up))
            (`down (magit-diff-show-or-scroll-down))
            (_     (apply #'magit-show-commit it (magit-diff-arguments))))
424 425
        (ding)))))

426
(defun git-rebase-show-commit ()
427
  "Show the commit on the current line if any."
428
  (interactive)
429 430 431
  (git-rebase--show-commit))

(defun git-rebase-show-or-scroll-up ()
432 433 434 435 436 437
  "Update the commit buffer for commit on current line.

Either show the commit at point in the appropriate buffer, or if
that buffer is already being displayed in the current frame and
contains information about that commit, then instead scroll the
buffer up."
438
  (interactive)
439
  (git-rebase--show-commit 'up))
440

441 442 443 444 445 446 447 448 449 450
(defun git-rebase-show-or-scroll-down ()
  "Update the commit buffer for commit on current line.

Either show the commit at point in the appropriate buffer, or if
that buffer is already being displayed in the current frame and
contains information about that commit, then instead scroll the
buffer down."
  (interactive)
  (git-rebase--show-commit 'down))

451 452 453 454
(defun git-rebase-backward-line (&optional n)
  "Move N lines backward (forward if N is negative).
Like `forward-line' but go into the opposite direction."
  (interactive "p")
455
  (forward-line (- (or n 1))))
456

Jonas Bernoulli's avatar
Jonas Bernoulli committed
457 458
;;; Mode

459
;;;###autoload
460
(define-derived-mode git-rebase-mode special-mode "Git Rebase"
461 462 463 464 465 466
  "Major mode for editing of a Git rebase file.

Rebase files are generated when you run 'git rebase -i' or run
`magit-interactive-rebase'.  They describe how Git should perform
the rebase.  See the documentation for git-rebase (e.g., by
running 'man git-rebase' at the command line) for details."
467
  :group 'git-rebase
468 469 470
  (setq comment-start (or (magit-get "core.commentChar") "#"))
  (setq git-rebase-comment-re (concat "^" (regexp-quote comment-start)))
  (setq git-rebase-line
471
        (concat "^\\(" (regexp-quote comment-start) "? *"
472
                "\\(?:[fprse]\\|pick\\|reword\\|edit\\|squash\\|fixup\\|exec\\|noop\\)\\) "
473
                "\\(?:\\([^ \n]+\\) \\(.*\\)\\)?"))
474
  (setq font-lock-defaults (list (git-rebase-mode-font-lock-keywords) t t))
475
  (unless git-rebase-show-instructions
476
    (let ((inhibit-read-only t))
477
      (flush-lines git-rebase-comment-re)))
478 479 480
  (unless with-editor-mode
    ;; Maybe already enabled when using `shell-command' or an Emacs shell.
    (with-editor-mode 1))
Jonas Bernoulli's avatar
Jonas Bernoulli committed
481 482
  (when git-rebase-confirm-cancel
    (add-hook 'with-editor-cancel-query-functions
483
              'git-rebase-cancel-confirm nil t))
484 485
  (setq-local redisplay-highlight-region-function 'git-rebase-highlight-region)
  (setq-local redisplay-unhighlight-region-function 'git-rebase-unhighlight-region)
486
  (add-hook 'with-editor-pre-cancel-hook  'git-rebase-autostash-save  nil t)
487
  (add-hook 'with-editor-post-cancel-hook 'git-rebase-autostash-apply nil t)
488 489 490 491
  (setq imenu-prev-index-position-function
        #'magit-imenu--rebase-prev-index-position-function)
  (setq imenu-extract-index-name-function
        #'magit-imenu--rebase-extract-index-name-function)
492 493
  (when (boundp 'save-place)
    (setq save-place nil)))
Jonas Bernoulli's avatar
Jonas Bernoulli committed
494 495

(defun git-rebase-cancel-confirm (force)
496 497
  (or (not (buffer-modified-p))
      force
498
      (magit-confirm 'abort-rebase "Abort this rebase" nil 'noabort)))
499

500 501 502 503 504 505 506 507
(defun git-rebase-autostash-save ()
  (--when-let (magit-file-line (magit-git-dir "rebase-merge/autostash"))
    (push (cons 'stash it) with-editor-cancel-alist)))

(defun git-rebase-autostash-apply ()
  (--when-let (cdr (assq 'stash with-editor-cancel-alist))
    (magit-stash-apply it)))

508 509 510
(defun git-rebase-match-comment-line (limit)
  (re-search-forward (concat git-rebase-comment-re ".*") limit t))

511 512
(defun git-rebase-mode-font-lock-keywords ()
  "Font lock keywords for Git-Rebase mode."
513 514 515 516 517 518 519 520 521
  (let ((action-re "\
\\([efprs]\\|pick\\|reword\\|edit\\|squash\\|fixup\\) \\([^ \n]+\\) \\(.*\\)"))
    `((,(concat "^" action-re)
       (1 'font-lock-keyword-face)
       (2 'git-rebase-hash)
       (3 'git-rebase-description))
      ("^\\(exec\\) \\(.*\\)"
       (1 'font-lock-keyword-face)
       (2 'git-rebase-description))
522 523
      ("^\\(noop\\)"
       (1 'font-lock-keyword-face))
524 525 526
      (git-rebase-match-comment-line 0 'font-lock-comment-face)
      (,(concat git-rebase-comment-re " *" action-re)
       0 'git-rebase-killed-action t)
527 528
      ("\\[[^[]*\\]"
       0 'magit-keyword t)
529 530 531 532 533
      (,(format "^%s Rebase \\([^ ]*\\) onto \\([^ ]*\\)" comment-start)
       (1 'git-rebase-comment-hash t)
       (2 'git-rebase-comment-hash t))
      (,(format "^%s \\(Commands:\\)" comment-start)
       (1 'git-rebase-comment-heading t)))))
534

535
(defun git-rebase-mode-show-keybindings ()
536 537 538 539
  "Modify the \"Commands:\" section of the comment Git generates
at the bottom of the file so that in place of the one-letter
abbreviation for the command, it shows the command's keybinding.
By default, this is the same except for the \"pick\" command."
540 541 542
  (let ((inhibit-read-only t))
    (save-excursion
      (goto-char (point-min))
543
      (when (and git-rebase-show-instructions
544
                 (re-search-forward
545
                  (concat git-rebase-comment-re "\\s-+p, pick")
546
                  nil t))
547
        (goto-char (line-beginning-position))
548
        (--each git-rebase-command-descriptions
549 550
          (insert (format "%s %-8s %s\n"
                          comment-start
551 552
                          (substitute-command-keys (format "\\[%s]" (car it)))
                          (cdr it))))
553
        (while (re-search-forward (concat git-rebase-comment-re
554 555
                                          "\\(  ?\\)\\([^\n,],\\) "
                                          "\\([^\n ]+\\) ")
556
                                  nil t)
557
          (let ((cmd (intern (concat "git-rebase-" (match-string 3)))))
558 559 560 561 562 563
            (if (not (fboundp cmd))
                (delete-region (line-beginning-position) (1+ (line-end-position)))
              (replace-match " " t t nil 1)
              (replace-match
               (format "%-8s"
                       (mapconcat #'key-description
564
                                  (--remove (eq (elt it 0) 'menu-bar)
565 566 567
                                            (reverse (where-is-internal cmd)))
                                  ", "))
               t t nil 2))))))))
568 569 570 571 572 573 574 575

(add-hook 'git-rebase-mode-hook 'git-rebase-mode-show-keybindings t)

(defun git-rebase-mode-disable-before-save-hook ()
  (set (make-local-variable 'before-save-hook) nil))

(add-hook 'git-rebase-mode-hook 'git-rebase-mode-disable-before-save-hook)

576 577
;;;###autoload
(defconst git-rebase-filename-regexp "/git-rebase-todo\\'")
578 579
;;;###autoload
(add-to-list 'auto-mode-alist
580
             (cons git-rebase-filename-regexp 'git-rebase-mode))
581

582 583
(add-to-list 'with-editor-server-window-alist
             (cons git-rebase-filename-regexp 'switch-to-buffer))
584

585 586 587
(eval-after-load 'recentf
  '(add-to-list 'recentf-exclude git-rebase-filename-regexp))

588 589
(add-to-list 'with-editor-file-name-history-exclude git-rebase-filename-regexp)

590 591
(provide 'git-rebase)
;;; git-rebase.el ends here