darkowlzz

go-git - Object Alternates

2 minute read Published:

This is another git related post coming out of more contributions to the src-d/go-git project. Other related post can be found by following the “go-git” tag at the bottom of this page.

Git clone offers an option to clone a repo without actually copying the git objects to a new location, but sharing the objects with the new repo. This can be done by using the --shared or -s option. It’s for local repos only.

$ git clone -s <sourcerepo> <newrepo>

This would clone the source repo files into a new repo and create an alternates files at .git/objects/info/alternates, with content as an absolute path to the source repo’s object directory.

For example:

/Users/username/rep1//.git/objects

Or, in Windows:

C:\\Users\\username\\repo1\\.git\\objects

This tells git where to look for the objects in a repo. According to gitrepository-layout documentation, there could be multiple paths in the file (one per line) and git would look them up one-by-one. Also, these paths could be relative as well. But relative to the source object directory, not repo. And that mostly looks something like this (if the source and shared repo are at the same level):

../../../sourcerepo//.git/objects

This shared object lookup works in go-git too now for both *nix and Windows.

In go-git, .git/ is abstracted as an internal type, DotGit. Since, it’s part of an internal package, it can’t be used by a user of go-git. But is available to be used in go-git development. DotGit has a method Alternates() which reads the alternates file and returns a list of all the resolved DotGits found in the alternates.

comments powered by Disqus