3

I'm working on a large repo with many folders, and showing the status is slow (just plain git takes a long time).

I am really only interested in one directory, how can I constrain magit to only seek the status of one directory so it will be fast.

I am specifically not looking to filter directories after the status is displayed (for an answer to that question see here: Magit show status on selected directory)

3

I had the same request (I actually opened the issue that added this feature here), but was not satisfied with the requirement to add a new file every time I wanted to limit the magit status, so I created magit-status-current-dir to open magit in the current directory.

  (defun magit-status-in-dir (dir)
"limit magit status to directory"
(interactive "D")
(require 'magit)
(let* ((root (vc-git-root dir))
       (dir (list (file-relative-name dir root)))
       (magit-status-mode-hook (cons (lambda () (setq-local magit-diff-section-file-args dir))
                                     magit-status-mode-hook)))
  (magit-status-internal root)))

(defun magit-status-current-dir ()
  "run magit in current dir"
  (interactive)
  (magit-status-in-dir default-directory))

(defun magit-clear-diff-args-if-not-dir-local ()
  (unless (assq 'magit-diff-section-file-args dir-local-variables-alist)
    (setq-local magit-diff-section-file-args nil)))

(advice-add 'magit-status :around
            (defun magit-remove-diff-args (fn &rest args)
              (let ((magit-status-mode-hook (cons 'magit-clear-diff-args-if-not-dir-local
                                                  magit-status-mode-hook)))
                (apply fn args))))
  • Ivan's answer is helpful, but this is exactly what I was looking for. Thanks. – tjb Feb 14 at 4:12
  • this is a nice answer. Just beware that it sets vc-git-root and this could have unexpected consequences. – A_P Feb 15 at 17:20
3

According to the sources, the directory filters not only affect UI, they are actually passed into git diff commands.

To avoid setting filters manually every time, according to author you can add a snippet to ~/path/to/repo/.dir-locals.el:

((magit-status-mode
  (magit-diff-section-file-args . ("lisp/"))))
  • Wow thanks, I will try it. – tjb Feb 13 at 13:28

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.