From 847c8a3e965b5c1174d01febac8c4f3a96d88fb8 Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Mon, 12 Jan 2026 13:03:32 -0500 Subject: [PATCH] [emacs] Add magit key loading --- emacs/.config/doom/config.el | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/emacs/.config/doom/config.el b/emacs/.config/doom/config.el index 5756c17..11d5f86 100644 --- a/emacs/.config/doom/config.el +++ b/emacs/.config/doom/config.el @@ -439,3 +439,36 @@ Always open the result in `eww`." ;; Bind globally to C-c l (global-set-key (kbd "C-c l") #'life-scrobble-url) + +(after! magit + (defvar my/ssh-key-injector-script + (expand-file-name "~/.local/bin/inject-ssh-keys-from-pass")) + + (defun my/ssh-agent-has-keys-p (&rest _ignore) + "Non-nil if ssh-agent currently has at least one identity loaded." + (eq 0 (call-process "ssh-add" nil nil nil "-l"))) + + (defun my/ensure-ssh-keys-loaded (&rest _ignore) + "Ensure ssh-agent has keys loaded; if not, run injector script." + (unless (my/ssh-agent-has-keys-p) + (unless (file-executable-p my/ssh-key-injector-script) + (user-error "SSH injector script not executable: %s" my/ssh-key-injector-script)) + (let ((buf (get-buffer-create "*ssh-key-injector*"))) + (with-current-buffer buf (erase-buffer)) + (let ((exit (call-process-shell-command my/ssh-key-injector-script nil buf t))) + (unless (eq exit 0) + (display-buffer buf) + (user-error "SSH key injection failed (see *ssh-key-injector*)")))))) + + ;; IMPORTANT: remove then re-add, so we don't keep an old advised function object around + (advice-remove 'magit-status #'my/ensure-ssh-keys-loaded) + (advice-add 'magit-status :before #'my/ensure-ssh-keys-loaded) + + ;; optional: + (advice-remove 'magit-fetch #'my/ensure-ssh-keys-loaded) + (advice-add 'magit-fetch :before #'my/ensure-ssh-keys-loaded) + (advice-remove 'magit-push #'my/ensure-ssh-keys-loaded) + (advice-add 'magit-push :before #'my/ensure-ssh-keys-loaded) + (advice-remove 'magit-pull #'my/ensure-ssh-keys-loaded) + (advice-add 'magit-pull :before #'my/ensure-ssh-keys-loaded)) +