Skip to content

Instantly share code, notes, and snippets.

@erikmd
Last active April 3, 2023 14:21
Show Gist options
  • Save erikmd/82c4b2a50a77c98e8fe6318530c531b7 to your computer and use it in GitHub Desktop.
Save erikmd/82c4b2a50a77c98e8fe6318530c531b7 to your computer and use it in GitHub Desktop.
Config Magit pour GNU Emacs
;;; Config de package.el, MELPA et use-package -*- lexical-binding: t -*-
;;; Pour plus d'infos :
;; https://github.com/magit/magit et https://magit.vc (doc officielle)
;; https://youtu.be/mtliRYQd0j4 (tuto vidéo sur git-rebase avec Magit)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Sauter si besoin cette section (ne pas avoir ce code dédoublé dans ~/.emacs)
(require 'package)
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
(package-initialize)
(unless (package-installed-p 'use-package)
(package-refresh-contents)
(package-install 'use-package))
(eval-when-compile
(require 'use-package))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Config de Magit
(use-package magit
:ensure t
:defer t
:config
(setq magit-diff-refine-hunk 'all)
:bind (("C-x g" . magit-status)
("C-x M-g" . magit-dispatch-popup)))
(use-package magit-gitflow
:ensure t
:after magit
:config (add-hook 'magit-mode-hook 'turn-on-magit-gitflow))
;; Protect against accident pushes to upstream/pushremote
;; compatible with https://github.com/magit/magit/pull/3813
;; tested with magit-20200927.1644
(defadvice magit-push-current-to-upstream
(around my-protect-accidental-magit-push-current-to-upstream)
"Protect against accidental push to upstream.
Causes `magit-run-git-async' to ask the user for confirmation first."
(let ((my-magit-ask-before-push t))
ad-do-it))
(defadvice magit-push-current-to-pushremote
(around my-protect-accidental-magit-push-current-to-pushremote)
"Protect against accidental push to upstream.
Causes `magit-run-git-async' to ask the user for confirmation first."
(let ((my-magit-ask-before-push t))
ad-do-it))
(defun magit-git-to-string (args)
"Pretty-print the `magit-run-git-async' arguments.
Quote the substrings if need be."
(cond ((not args)
"")
((stringp args)
(shell-quote-argument args))
((listp args)
(mapconcat #'magit-git-to-string args " "))
(t (error "Unrecognized: %s" (pp-to-string args)))))
;(magit-git-to-string '("push" "-v" ("--force-with-lease") "origin" "master:refs/heads/master"))
(defadvice magit-run-git-async (around my-protect-accidental-magit-run-git-async)
"Maybe ask the user for confirmation before pushing.
Advices to `magit-push-current-to-*' trigger this query."
(if (bound-and-true-p my-magit-ask-before-push)
;; Arglist is (ARGS)
(if (y-or-n-p (format "Run 'git %s'? "
(magit-git-to-string (ad-get-args 0))))
ad-do-it
(error "Push aborted by user"))
ad-do-it))
(ad-activate 'magit-push-current-to-upstream)
(ad-activate 'magit-push-current-to-pushremote)
(ad-activate 'magit-run-git-async)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment