From eb277ab8356fe1d09c02ef1220e2abfe45bc02a3 Mon Sep 17 00:00:00 2001 From: Toon Schoenmakers Date: Thu, 21 Apr 2022 17:12:47 +0200 Subject: [PATCH] Skip the git pull on bare repositories A second run with -backup.bare-clone would fail as it would still try to retrieve a work tree and do a git pull. Now it will check whether a discovered repository is bare or not. And if it is, it will just skip the git pull and only do a git fetch down the line. --- repository.go | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/repository.go b/repository.go index 8932c66..f355b43 100644 --- a/repository.go +++ b/repository.go @@ -21,6 +21,15 @@ type Repository struct { FullName string } +func isBare(repo *git.Repository) (bool, error) { + config, err := repo.Config() + if err != nil { + return false, err + } + + return config.Core.IsBare, nil +} + func (r *Repository) CloneInto(path string, bare bool) error { var auth http.AuthMethod if r.GitURL.User != nil { @@ -39,13 +48,17 @@ func (r *Repository) CloneInto(path string, bare bool) error { if err == git.ErrRepositoryAlreadyExists { // Pull instead of clone if gitRepo, err = git.PlainOpen(path); err == nil { - if w, wErr := gitRepo.Worktree(); wErr != nil { - err = wErr - } else { - err = w.Pull(&git.PullOptions{ - Auth: auth, - Progress: os.Stdout, - }) + // we need to check whether it's a bare repo or not. + // if not we should pull, if it is then pull won't work + if isBare, bErr := isBare(gitRepo); bErr == nil && !isBare { + if w, wErr := gitRepo.Worktree(); wErr != nil { + err = wErr + } else { + err = w.Pull(&git.PullOptions{ + Auth: auth, + Progress: os.Stdout, + }) + } } } }