From 740ff4615a9ced4a8a016365aa44674b9b0e807d Mon Sep 17 00:00:00 2001 From: Alexander Egorenkov Date: Fri, 20 Nov 2020 14:09:02 +0100 Subject: pkg/vcs/git: optimize CheckoutBranch Fetching a remote branch w/o the corresponding remote with git disables delta optimization on the server's side and the git client is forced to download the complete branch even if it already has got an older version. This leads to several problems as: * Long fetch time * Limitless growth of local git repo due to git fetch creating a new pack file every time Create a remote before fetching to fix the above issues. With a remote, only the first fetch will take some time and all the following ones shall be very fast. Furthermore, git fetch will avoid creating many pack files in .git/objects/pack. Signed-off-by: Alexander Egorenkov --- pkg/vcs/git.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'pkg/vcs') diff --git a/pkg/vcs/git.go b/pkg/vcs/git.go index 5b1efa33a..2f1f0f301 100644 --- a/pkg/vcs/git.go +++ b/pkg/vcs/git.go @@ -99,7 +99,10 @@ func (git *git) CheckoutBranch(repo, branch string) (*Commit, error) { if err := git.repair(); err != nil { return nil, err } - _, err := git.git("fetch", repo, branch) + repoHash := hash.String([]byte(repo)) + // Ignore error as we can double add the same remote and that will fail. + git.git("remote", "add", repoHash, repo) + _, err := git.git("fetch", repoHash, branch) if err != nil { return nil, err } -- cgit mrf-deployment