Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Share Git repository between Windows VM and Linux host

I work mostly on Linux, but I also have a Windows VM, mostly to run unit tests on Windows.

In Linux I have a Git repository, which is accessible from the Windows VM using a VirtualBox Shared Folder. I don't use Git on Windows, except for our build system, which records the current Git hash to include it in the executable (running git describe --always --dirty).

Now, every time I use Git on either Linux or Windows and then again on the other system, it takes a while. For example:

  Linux$ git status
  Linux$ git status # fast (<1s)
Windows$ git status # takes a few dozen seconds
Windows$ git status # fast (<1s)
  Linux$ git status # takes a few seconds
  Linux$ git status # fast (<1s)

Is there anything I can do to prevent this from happening? I'd be fine turning off Git features on Windows, as it only needs to get a hash. However I can't change how this hash is obtained, as this is deep in the build system. I also do not want to have separate repositories on Linux and Windows and commit/push from one another, as this would result in an ever bigger overhead.

Linux git version: 2.11.0.

Windows git version: 2.14.1.windows.1.

like image 379
mcarton Avatar asked Apr 11 '18 14:04

mcarton


1 Answers

What you are seeing here is how effective Git's index is when used as a cache.

Along with all the other stuff it does, Git's index acts as a cache of data about the work-tree to speed up file system operations—well, really, to skip file system operations, if possible. It does this by recording statistics about files (and, secretly, directories, even though Git does not actually store directories). This cache is only valid if a number of conditions hold. If not, Git has to do expensive file system operations on the work-tree, which as you have seen, take literally seconds of time on Linux, and are even slower on Windows.

Shared folders violate the cache assumptions. In particular, one of the items in the index is the path of the work-tree, and the path is different inside the Linux system than it is in the Windows VM. (This would generally be true regardless of how the two systems are hosted.)

There are several obvious ways to deal with this:

  1. Don't share work-trees. That's probably the best method.
  2. Don't allow one of the systems to update the index (by making it read-only there—this may be a bit tricky).
  3. Use a separate index on one of the systems: the default index is $GIT_DIR/index where $GIT_DIR is from the environment or defaulted from git rev-parse --git-dir, but setting GIT_INDEX_FILE to a path name will override this.

The reason I suggest method 1 is that it's built in to Git, if your Git is at least version 2.5, using git worktree add. Note that each Git work-tree must be in its own branch, or use a detached HEAD; the detached HEAD method is probably fine for this purpose, just use git checkout --detach <branch> there to update.

like image 89
torek Avatar answered Oct 06 '22 21:10

torek