Empty repos should not fail on pull (#513)

This commit is contained in:
Elmar Fasel
2025-04-30 03:34:07 +02:00
committed by GitHub
parent 4500043663
commit a1395a2a6f
3 changed files with 133 additions and 17 deletions

View File

@@ -1,6 +1,7 @@
package git
import (
"errors"
"fmt"
"os"
"os/exec"
@@ -26,6 +27,7 @@ type Gitter interface {
FetchAll(scm.Repo) error
FetchCloneBranch(scm.Repo) error
RepoCommitCount(scm.Repo) (int, error)
HasRemoteHeads(scm.Repo) (bool, error)
}
type GitClient struct{}
@@ -51,6 +53,35 @@ func printDebugCmd(cmd *exec.Cmd, repo scm.Repo) error {
return err
}
func (g GitClient) HasRemoteHeads(repo scm.Repo) (bool, error) {
cmd := exec.Command("git", "ls-remote", "--heads", "--quiet", "--exit-code")
err := cmd.Run()
if err == nil {
// successfully listed the remote heads
return true, nil
}
var exitError *exec.ExitError
if !errors.As(err, &exitError) {
// error, but no exit code, return err
return false, err
}
exitCode := exitError.ExitCode()
if exitCode == 0 {
// ls-remote did successfully list the remote heads
return true, nil
} else if exitCode == 2 {
// repository is empty
return false, nil
} else {
// another exit code, simply return err
return false, err
}
}
func (g GitClient) Clone(repo scm.Repo) error {
args := []string{"clone", repo.CloneURL, repo.HostPath}