Skip to content

Commit 1d75c74

Browse files
committed
Repository.IsFull(): new method
Extract a method to determine whether the repository seems to be a full clone. Call it from `NewRepository()`.
1 parent 29fc882 commit 1d75c74

1 file changed

Lines changed: 23 additions & 3 deletions

File tree

git/git.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"errors"
66
"fmt"
7+
"io/fs"
78
"os"
89
"os/exec"
910
"path/filepath"
@@ -71,17 +72,36 @@ func NewRepository(path string) (*Repository, error) {
7172
gitBin: gitBin,
7273
}
7374

75+
full, err := repo.IsFull()
76+
if err != nil {
77+
return nil, fmt.Errorf("determining whether the repository is a full clone: %w", err)
78+
}
79+
if !full {
80+
return nil, errors.New("this appears to be a shallow clone; full clone required")
81+
}
82+
83+
return &repo, nil
84+
}
85+
86+
// IsFull returns `true` iff `repo` appears to be a full clone.
87+
func (repo *Repository) IsFull() (bool, error) {
7488
shallow, err := repo.GitPath("shallow")
7589
if err != nil {
76-
return nil, err
90+
return false, err
7791
}
7892

7993
_, err = os.Lstat(shallow)
8094
if err == nil {
81-
return nil, errors.New("this appears to be a shallow clone; full clone required")
95+
return false, nil
8296
}
8397

84-
return &repo, nil
98+
if !errors.Is(err, fs.ErrNotExist) {
99+
return false, err
100+
}
101+
102+
// The `shallow` file is absent, which is what we expect
103+
// for a full clone.
104+
return true, nil
85105
}
86106

87107
func (repo *Repository) GitCommand(callerArgs ...string) *exec.Cmd {

0 commit comments

Comments
 (0)