This is a long standing issue https://github.com/magit/magit/issues/2708 . I feel that a prompt for deleting the transient file would compensate in most cases. Is there a reliable way to achieve this? Thanks!
I have added the following to my .init.el file:
(defun magit-remove-git-lock-file () "Remove git's index lock file, if it exists." (interactive) (let ((base (magit-toplevel))) (delete-file (concat base "/.git/index.lock"))))
This gives me a function to resolve this condition when it occurs.
As a response to other statements made, please note that when this happens to me, there was no other process running git commands, and the lock file did indeed remain visible on-disk for many minutes, forcing me to delete it either manually or via this code snippet.
I'm using a similar command via
advice-add #'magit-refresh :before. However I think a prompt would be better.– AmosApr 27, 2018 at 11:46
Is there a reliable way to achieve this?
No. It is inherently risky to do this because what you are trying to accomplish is to run multiple things in parallel that cannot be run in parallel and the file you want to remove is the file whose purpose it is to prevent that from happening.
Also I don't know how that could be implemented.
From the linked issue, it sounds like the file wouldn't exist by the time you got the prompt anyway: "I have started checking to see if the file actually exists and have yet to find [an] instance when it does."– npostavsApr 20, 2018 at 12:09