Skip to content

Instantly share code, notes, and snippets.

@alexispurslane
Last active May 27, 2024 02:11
Show Gist options
  • Save alexispurslane/890ea9971787c979f8c59c8decf132c4 to your computer and use it in GitHub Desktop.
Save alexispurslane/890ea9971787c979f8c59c8decf132c4 to your computer and use it in GitHub Desktop.
If you're trying to add a project using `project.el` that git deems of "dubious" ownership, `project.el` will crash. Here's a nice fix.

As the tagline says, if you're trying to use Emacs's built-n project-management package, project.el, to add a git version-controlled project that git deems untrustworthy and then do a project-find-{files,dir} on it, it will currently crash, because the output of the git ls-files outputs a bunch of errors to stderr and nothing to stdout, and it doesn't expect to have an empty list of files and directories. To resolve this, I've created a nice snipped of code that will detect, when you add a project, whether git deems it unsafe (git ls-files returns code 128 to mean that) and ask you whether you want to add it to your list of safe directories:

(advice-add 'project-remember-project
                :before (lambda (pr &optional something)
                            (when (and (eq 'Git (cadr pr))
                                       (let ((default-directory (caddr pr)))
                                           (= (shell-command "git ls-files") 128)))
                                (when (y-or-n-p "Dubious ownership of repository detected. Treat as safe? ")
                                    (shell-command
                                     (concat "git config --global --add safe.directory "
                                             (eshell-escape-arg (directory-file-name (file-local-name (caddr pr))))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment