0

The following code used to work with earlier versions of emacs, adding a ‘d’ option to magit's commit popup for ‘git duet’.

(require 'dash)

(defvar magit-duet--default-commit-popup-actions nil
  "Store the magit-commit-popup here for
     de-initializing. magit-duet-commit")

(defun magit-duet-commit (&optional args)
  "Create a new duet commit"
  (interactive (if current-prefix-arg
                   (list (cons "--amend" (magit-commit-arguments)))
                 (list (magit-commit-arguments))))
  (when (setq args (magit-commit-assert args))
    (magit-run-git-with-editor "duet-commit" args)))

(defun magit-duet-commit-init ()
  "Add duet-commit to magit commit popup.."
  (interactive)
  (when (eq magit-duet--default-commit-popup-actions nil)
    (setq magit-duet--default-commit-popup-actions
          (plist-get magit-commit-popup :actions))
    (plist-put
     magit-commit-popup
     :actions
     (-remove-at 5 (-insert-at 4
                               '(100 "Duet commit" magit-duet-commit)
                               (plist-get magit-commit-popup :actions))))))

(defun magit-duet-commit-deinit ()
  "Remove duet-commit from magit commit popup.."
  (interactive)
  (unless (eq magit-duet--default-commit-popup-actions nil)
    (plist-put magit-commit-popup
               :actions
               magit-duet--default-commit-popup-actions)))

Credit to jasonm23: https://gist.github.com/jasonm23/6326d278d99fe82f252a90d9b9ad61db

This no longer works with more recent versions of emacs.

How might this snippet be fixed for the latest emacs? Similarly what would be the best solution for git duet’s integration with magit overall?


Update

As per @tarsius suggestion, the following works perfectly:

(require 'dash)
(require 'magit)

(defvar magit-duet--default-commit-popup-actions nil
  "Store the magit-commit-popup here for
 de-initializing. magit-duet-commit")

(defun magit-duet-commit (&optional args)
  "Create a new duet commit"
  (interactive (if current-prefix-arg
                   (list (cons "--amend" (magit-commit-arguments)))
                 (list (magit-commit-arguments))))
  (when (setq args (magit-commit-assert args))
    (magit-run-git-with-editor "duet-commit" args)))

(defun magit-duet-commit-init ()
  "Add duet-commit to magit commit popup.."
  (interactive)
  (when (eq magit-duet--default-commit-popup-actions nil)
    (transient-append-suffix 'magit-commit "c"
      '("d" "Duet commit" magit-duet-commit))))

(add-hook 'magit-mode-hook 'magit-duet-commit-init)
1

The actual command magit-duet-commit should still work. It looks like it was created by copying magit-commit-create and then making some minor changes. That upstream command was changed slightly since it was originally copied and modified, so the copy should be updated too:

 (defun magit-duet-commit (&optional args)
-  "Create a new duet commit"
+  "Create a new commit on `HEAD' attributed to two authors.
+With a prefix argument, amend to the commit at `HEAD' instead.
+\n(git commit [--amend] ARGS)"
   (interactive (if current-prefix-arg
                    (list (cons "--amend" (magit-commit-arguments)))
                  (list (magit-commit-arguments))))
+  (if (member "--all" args)
+      (setq this-command 'magit-commit-all)
+    (setq this-command 'magit-commit-create))
   (when (setq args (magit-commit-assert args))
-    (magit-run-git-with-editor "duet-commit" args)))
+    (let ((default-directory (magit-toplevel)))
+      (magit-run-git-with-editor "duet-commit" args))))

The rest of the code was not even the proper way of doing this when it was created. Back then the magit-popup library was used to create these "transient prefix popups". Now the transient library is used instead.

Transient's manual documents how new commands can be added to an existing transient prefix. In this case:

(transient-append-suffix 'magit-commit "c"
  '("d" "Duet commit" magit-duet-commit))
  • that’s fantastic. Thanks! – ldeck Jul 18 at 17:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.