git.el: Add commands for cherry-pick and revert.
Support for cherry-picking and reverting commits, with automatic formatting of the commit log message. Bound to C-c C-p and C-c C-v respectively. Signed-off-by: Alexandre Julliard <julliard@winehq.org>
Alexandre Julliard committed
Nov 23, 2008 at 14:34 UTC
ab69e3e43a12cf02505f3e9e561c49c1fe8a81a6
1 file changed
+42
contrib/emacs/git.el
+42
@@ -1411,6 +1411,44 @@ amended version of it."
1411
(git-setup-commit-buffer commit)
1412
(git-commit-file))))
1413
1414
+(defun git-cherry-pick-commit (arg)
1415
+ "Cherry-pick a commit."
1416
+ (interactive (list (git-read-commit-name "Cherry-pick commit: ")))
1417
+ (unless git-status (error "Not in git-status buffer."))
1418
+ (let ((commit (git-rev-parse (concat arg "^0"))))
1419
+ (unless commit (error "Not a valid commit '%s'." arg))
1420
+ (when (git-rev-parse (concat commit "^2"))
1421
+ (error "Cannot cherry-pick a merge commit."))
1422
+ (let ((files (git-get-commit-files commit))
1423
+ (ok (git-call-process-display-error "cherry-pick" "-n" commit)))
1424
+ (git-update-status-files files ok)
1425
+ (with-current-buffer (git-setup-commit-buffer commit)
1426
+ (goto-char (point-min))
1427
+ (if (re-search-forward "^\n*Signed-off-by:" nil t 1)
1428
+ (goto-char (match-beginning 0))
1429
+ (goto-char (point-max)))
1430
+ (insert "(cherry picked from commit " commit ")\n"))
1431
+ (when ok (git-commit-file)))))
1432
+
1433
+(defun git-revert-commit (arg)
1434
+ "Revert a commit."
1435
+ (interactive (list (git-read-commit-name "Revert commit: ")))
1436
+ (unless git-status (error "Not in git-status buffer."))
1437
+ (let ((commit (git-rev-parse (concat arg "^0"))))
1438
+ (unless commit (error "Not a valid commit '%s'." arg))
1439
+ (when (git-rev-parse (concat commit "^2"))
1440
+ (error "Cannot revert a merge commit."))
1441
+ (let ((files (git-get-commit-files commit))
1442
+ (subject (git-get-commit-description commit))
1443
+ (ok (git-call-process-display-error "revert" "-n" commit)))
1444
+ (git-update-status-files files ok)
1445
+ (when (string-match "^[0-9a-f]+ - \\(.*\\)$" subject)
1446
+ (setq subject (match-string 1 subject)))
1447
+ (git-setup-log-buffer (get-buffer-create "*git-commit*")
1448
+ (git-get-merge-heads) nil nil (format "Revert \"%s\"" subject) nil
1449
+ (format "This reverts commit %s.\n" commit))
1450
+ (when ok (git-commit-file)))))
1451
+
1452
(defun git-find-file ()
1453
"Visit the current file in its own buffer."
1454
(interactive)
@@ -1512,6 +1550,8 @@ amended version of it."
1550
(define-key commit-map "\C-a" 'git-amend-commit)
1551
(define-key commit-map "\C-b" 'git-branch)
1552
(define-key commit-map "\C-o" 'git-checkout)
1553
+ (define-key commit-map "\C-p" 'git-cherry-pick-commit)
1554
+ (define-key commit-map "\C-v" 'git-revert-commit)
1555
; the diff submap
1556
(define-key diff-map "b" 'git-diff-file-base)
1557
(define-key diff-map "c" 'git-diff-file-combined)
@@ -1534,6 +1574,8 @@ amended version of it."
1574
["Commit" git-commit-file t]
1575
["Checkout..." git-checkout t]
1576
["New Branch..." git-branch t]
1577
+ ["Cherry-pick Commit..." git-cherry-pick-commit t]
1578
+ ["Revert Commit..." git-revert-commit t]
1579
("Merge"
1580
["Next Unmerged File" git-next-unmerged-file t]
1581
["Prev Unmerged File" git-prev-unmerged-file t]