1

In my repo's .git/config I have status.showUntrackedFiles set to no (for performance, because it's a very large repo).

However, sometimes (e.g. before I push to remote) I'd like to manually check, from magit, if there are untracked files.

Can I tell magit to ignore the behavior from my repo's git config, and show me the untracked files?

Alternatives I've tried

  • Just use git status -u from the command line to get the untracked files (this is fine, but it'd be nice to see this from magit)
  • Hide untracked files using emacs (instead of .git/config) via magit-status-sections-hook, and just call magit-insert-untracked-files when I need it (This seems simple enough, but I don't want to hide untracked files by default in all repos, just this one large repo)

Solution I Settled On

It was too long for a comment, so I'll just put it here. I modified @whitetrillium's code to preserve my existing magit-status-sections-hook in addition to adding the untracked files. That function is:

(defun my-magit-status ()
   "Forcibly include untracked files at the end of magit-status."
   (interactive)
   (advice-add 'magit-get :around
               (defun magit-get--around
                   (orig-fun &rest orig-args)
                 ;; (debug)
                 (pcase-let*
                     ((`(,keys)
                       orig-args))
                   (if (string= keys "status.showUntrackedFiles")
                       "all"
                     (apply orig-fun orig-args)))))
   (let ((magit-status-sections-hook (append magit-status-sections-hook '(magit-insert-untracked-files))))
     (call-interactively 'magit-status))
   (advice-remove 'magit-get 'magit-get--around))

Then bind this to whatever you want. (e.g. (global-set-key (kbd "C-x G" 'my-magit-status), as recommended by @whitetrillium)

Then I figured I'd just add the untracked files to an existing magit-status, and I used the same advice on magit-insert-untracked-files:

(defun magit-force-insert-untracked-files ()
   "Forcibly insert untracked files (at point) in magit-status."
   (interactive)
   (defun magit-get--around (orig-fun &rest orig-args)
                 (pcase-let*
                     ((`(,keys) orig-args))
                   (if (string= keys "status.showUntrackedFiles") "all"
                     (apply orig-fun orig-args))))
   (advice-add 'magit-get :around 'magit-get--around)
   (read-only-mode -1)
   (magit-insert-untracked-files)
   (read-only-mode t)
   (advice-remove 'magit-get 'magit-get--around))

Sort of annoyingly, this performs the insert wherever your cursor is, but it's a nice way to just tack on the untracked files to your magit-status.

1 Answer 1

1

If you want this

Custom magit-status that shows only untracked files

Custom magit-status that shows only untracked files

instead of this

magit-status (C-x g) doesn't show untracked files

magit-status (C-x g) doesn't show untracked files

install this snippet, and call C-x G (my-magit-status)

(global-set-key
 (kbd "C-x G")
 (defun my-magit-status ()
   "Forcibly show only untracked files."
   (interactive)
   (advice-add 'magit-get :around
               (defun magit-get--around
                   (orig-fun &rest orig-args)
                 ;; (debug)
                 (pcase-let*
                     ((`(,keys)
                       orig-args))
                   (if (string= keys "status.showUntrackedFiles")
                       "all"
                     (apply orig-fun orig-args)))))
   (let ((magit-status-sections-hook '(magit-insert-untracked-files)))
     (call-interactively 'magit-status))
   (advice-remove 'magit-get 'magit-get--around)))

Btw, the git status screenshots are taken on a checkout of magit repo with following config.

~/src/nongnu-elpa/packages/magit$ git config --get status.showUntrackedFiles
no

I could have used a :filter-return advice, instead of a :around advice. But I find the :around advice more flexible.

1
  • Ah, I see, so we just intercept magit asking what the config value is, and return "all" instead of the value in the config. Very clever :) I modified this by substituting '(magit-insert-untracked-files) with (append magit-status-sections-hook '(magit-insert-untracked-files)) (to keep my other sections intact), but this funtion worked as-is, thank you!
    – Patrick
    Oct 18, 2022 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.