5

I currently maintain a project for a git-prompt for bash (https://github.com/magicmonty/bash-git-prompt) and I just got a bug report (https://github.com/magicmonty/bash-git-prompt/issues/97) from someone who works with Docker, who tells me, that everytime he uses the prompt, the cache is invalidated, because the .git directory is constantly touched.

I have looked into this, and found out, that it is the command git status, which touches the .git directory. It seems, that only the directory entry itself and no contents are touched. Can anyone explain, why this is needed, or is this maybe a bug in Git.

Is there a way to show all status info, without touching the .git directory?

Thanks for the help

Update:

Since the whole reason to use the git status command was, to determine the number of untracked files, I replaced it with git ls-files --others --exclude-standard | wc -l, which doesn't need a lock.

magicmonty
  • 88
  • 6

1 Answers1

7

strace git status shows that this action uses the lock file .git/index.lock, that's why the .git's mtime is updated.

git being cool, it uses the environment variable GIT_INDEX_FILE to decide which lock file to use. If unset, git uses .git/index (this is the default), but if set, git uses its value. From man git:

GIT_INDEX_FILE

This environment allows the specification of an alternate index file. If not specified, the default of $GIT_DIR/index is used.

So:

GIT_INDEX_FILE=banana git status

will not update your .git's mtime.

So you now have to make a decision as whether you want to go along this path or not (which certainly has many caveats).

Good luck!

Community
  • 1
  • 1
gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104
  • It should be remarked, that the index file has to be copied to the given new file before using the `git status`command. Otherwise git will find an empty index here. – magicmonty Nov 15 '14 at 12:20
  • @magicmonty One possibility is to make a link (and not copy). – gniourf_gniourf Nov 15 '14 at 12:29
  • But thats a bit risky, as this works completely around the idea of a locking mechanism. I suspect there is a good reason, why this files would be locked. With a link you have the possibility of a read at the same time, a write happens. In the best case there is just a wrong status but in the worst case this could lead to a corrupted index. – magicmonty Nov 17 '14 at 18:16