Last active
July 13, 2018 11:25
-
-
Save tazjin/277467b2cb99103f4cfe71f90cbc35b0 to your computer and use it in GitHub Desktop.
Advise magit-git-push to ask for user confirmation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; This demonstrates how to advise[1] magit functions that perform | |
;; potentially destructive operations (for example `magit-git-push') | |
;; with a confirmation prompt. | |
;; | |
;; The TL;DR of advising is that a function is "wrapped" around the | |
;; function to be advised, with various different behaviours that can | |
;; be used (e.g. only conditionally executing the original function). | |
;; | |
;; [1]: https://www.gnu.org/software/emacs/manual/html_node/elisp/Advising-Functions.html#Advising-Functions | |
;; | |
;; This defines a function that prompts a user for confirmation before | |
;; pushing a branch. | |
;; | |
;; #'yes-or-no-p is a built-in function in emacs that takes a prompt | |
;; string and shows it to the user, returning `t' (Lisp's `true' | |
;; value) or `nil' (Lisp's "falsy" value) depending on the user's | |
;; choice. | |
;; | |
;; The arguments to this function are identical to the ones received | |
;; by `magit-git-push' (the function to be advised itself). | |
(defun magit-confirm-push (branch target args) | |
;; Construct a prompt string from the supplied arguments ... | |
(let ((prompt (format "Do you want to push branch '%s' to '%s'?" branch target))) | |
;; ... and prompt the user with it! | |
(yes-or-no-p prompt))) | |
;; This call advises the function `magit-git-push' to execute the | |
;; prompt function defined above when it is called. | |
;; | |
;; The `:before-while' advice "place" defines that the original | |
;; function should only be called if the advising function returns | |
;; `t'. | |
(advice-add 'magit-git-push :before-while #'magit-confirm-push) | |
;; That's it, pushing in magit will now ask for confirmation! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment