Skip to content

git: Optimize check for reference existence

Patrick Steinhardt requested to merge pks-optimize-contains-ref into master

In order to check whether a reference exists inside of a Git repository, we're executing git log --max-count=1 $REFERENCE. Using porcelain commands for scripting tasks is not recommended by the Git project and also typically a lot slower compared to using plumbing commands. As a result, our use of git-log(1) is both surprising and inefficient here.

Convert the code to use git rev-parse --verify $REFERENCE. A simple benchmark looping around repo.ContainsRef(ctx, "refs/heads/master" shows that this buys us roughly a 50% performance boost compared to the previous implementation.

The following benchmark uses git-log(1):

goos: linux
goarch: amd64
pkg: gitlab.com/gitlab-org/gitaly/internal/git
BenchmarkLocalRepository_ContainsRef-6   	     909	   1339892 ns/op
PASS
ok  	gitlab.com/gitlab-org/gitaly/internal/git	1.422s

And this one uses git-rev-parse(1):

goos: linux
goarch: amd64
pkg: gitlab.com/gitlab-org/gitaly/internal/git
BenchmarkLocalRepository_ContainsRef-6   	    1431	    815856 ns/op
PASS
ok  	gitlab.com/gitlab-org/gitaly/internal/git	1.325s

Merge request reports