I want to start using dotfiles in a bare repo, and commit/pull using magit. I have found the following code to use magit with a bare repo:
;; prepare the arguments
(setq dotfiles-git-dir (concat "--git-dir=" (expand-file-name "~/.dotfiles")))
(setq dotfiles-work-tree (concat "--work-tree=" (expand-file-name "~")))
;; function to start magit on dotfiles
(defun dotfiles-magit-status ()
(interactive)
(add-to-list 'magit-git-global-arguments dotfiles-git-dir)
(add-to-list 'magit-git-global-arguments dotfiles-work-tree)
(call-interactively 'magit-status))
(global-set-key (kbd "F5 d") 'dotfiles-magit-status)
;; wrapper to remove additional args before starting magit
(defun magit-status-with-removed-dotfiles-args ()
(interactive)
(setq magit-git-global-arguments (remove dotfiles-git-dir magit-git-global-arguments))
(setq magit-git-global-arguments (remove dotfiles-work-tree magit-git-global-arguments))
(call-interactively 'magit-status))
;; redirect global magit hotkey to our wrapper
(global-set-key (kbd "C-x g") 'magit-status-with-removed-dotfiles-args)
(define-key magit-file-mode-map (kbd "C-x g") 'magit-status-with-removed-dotfiles-args)
It works great, however, I would like to automatically add/remove the magit-git-global-arguments automatically depending on what directory magit is opened from: adding the bare repo config when in home, and removing it for all others.
How can I add a hook that detects when magit is about to get launched, and checks the directory, to then either add or remove the flags?