Following a link into a git-repo without lengthy dialog
Asked Answered
B

2

22

In a directory I have symbolic links into a git-administered directory (all under Linux). Every time I want to e-dit such a link I get a dialog question:

Symbolic link to Git-controlled source file; follow link? (yes or no)

And I have to type y e s to get to the file. Is there a somewhat simpler way?

Ideally, something like declaring that a directory needs no dialog.

Bisectrix answered 27/4, 2017 at 16:19 Comment(0)
D
18

Set vc-follow-symlinks. You probably want it to be nil (open link), but be sure to read the docs because t (open target) is also sensible.

(setq vc-follow-symlinks nil)

You can make this a dir local variable if you don't want it set globally.

Dittman answered 27/4, 2017 at 20:2 Comment(4)
From the docs: "If this variable is t, VC follows the link and visits the real file, telling you about it in the echo area. If it is ‘ask’, VC asks for confirmation whether it should follow the link. If nil, the link is visited and a warning displayed." So nil is essentially as if you answered "no" to the original question, and t is as if you answered "yes" to the original question. The t behavior seems more natural to me. I'm curious where nil would be preferable since I can't immediately think of such a case.Pavier
The doc for that variable says "Editing such a file through the link bypasses the version control system, which is dangerous and probably not what you want." I don't understand this. How does it bypass the VCS? and why is it dangerous?Guthrun
@Guthrun I'm not entirely sure how it's dangerous. See this answer: emacs.stackexchange.com/a/5831/651. It seems to be about the "environment" of the buffer, e.g. default-directory. Also I assume it's specifically about Emacs's internal vc package, so it might not apply if you use magit instead.Dittman
Ohh, I see. When they say "the version control system" here they mean VC itself, rather than the underlying actual VCS, e.g., Git. That makes a lot more sense. Thanks!Guthrun
A
5

An interesting question. I store all my dotfiles in the repository and have a bunch of symlinks scattered all over the filesystem so this is a known problem to me.

First, a half-solution:

(defalias 'yes-or-no-p 'y-or-n-p)

Now instead of typing a full yes + Enter you can just type a single y or n letter in all situations where yes-or-no is asked in Emacs.

Now, a real solution:

(defun vc-mode-hook ()
  (message buffer-file-name)
  (when
      (and
       (file-exists-p (buffer-file-name))
       (stringp buffer-file-name)
       (or (string-equal "/home/ja/.fluxbox/keys" buffer-file-name)
       (string-equal "<PATH_TO_ANOTHER_FILE>" buffer-file-name))
       (setq-local vc-follow-symlinks t)
       )))

(add-hook 'find-file-hook 'vc-mode-hook)

Here we create a new hook that is called every time find-file is called, for example with C-x C-f or e in Dired. It first checks if a visited file really exists on the filesystem using file-exists-p because it doesn't make sense to run it on files that haven't been created yet. Next it checks if a file name is known using stringp - it will return t when a regular file is opened but nil in a Dired buffer for example. And finally it checks if the file name is equal to one of strings provided using string-equal. If it is, it locally sets vc-follow-symlinks to t. You can add as many string-equal lines as you wish. In the example above I added /home/ja/.fluxbox/keys and an placeholder for an another file name in the next line.

Astrometry answered 2/5, 2017 at 14:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.