Skip to content

Commit 02928f1

Browse files
committed
NewRepositoryFromGitDir(): new function
If you already have the desired `GIT_DIR`, there's no need to determine it from the current path.
1 parent 51cf26b commit 02928f1

1 file changed

Lines changed: 31 additions & 16 deletions

File tree

git/git.go

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,36 @@ func smartJoin(path, relPath string) string {
3737
return filepath.Join(path, relPath)
3838
}
3939

40-
// NewRepository creates a new repository object that can be used for
41-
// running `git` commands within that repository.
40+
// NewRepositoryFromGitDir creates a new `Repository` object that can
41+
// be used for running `git` commands, given the value of `GIT_DIR`
42+
// for the repository.
43+
func NewRepositoryFromGitDir(gitDir string) (*Repository, error) {
44+
// Find the `git` executable to be used:
45+
gitBin, err := findGitBin()
46+
if err != nil {
47+
return nil, fmt.Errorf(
48+
"could not find 'git' executable (is it in your PATH?): %w", err,
49+
)
50+
}
51+
52+
repo := Repository{
53+
gitDir: gitDir,
54+
gitBin: gitBin,
55+
}
56+
57+
full, err := repo.IsFull()
58+
if err != nil {
59+
return nil, fmt.Errorf("determining whether the repository is a full clone: %w", err)
60+
}
61+
if !full {
62+
return nil, errors.New("this appears to be a shallow clone; full clone required")
63+
}
64+
65+
return &repo, nil
66+
}
67+
68+
// NewRepository creates a new `Repository` object that can be used
69+
// for running `git` commands within `path`.
4270
func NewRepository(path string) (*Repository, error) {
4371
// Find the `git` executable to be used:
4472
gitBin, err := findGitBin()
@@ -68,20 +96,7 @@ func NewRepository(path string) (*Repository, error) {
6896
}
6997
gitDir := smartJoin(path, string(bytes.TrimSpace(out)))
7098

71-
repo := Repository{
72-
gitDir: gitDir,
73-
gitBin: gitBin,
74-
}
75-
76-
full, err := repo.IsFull()
77-
if err != nil {
78-
return nil, fmt.Errorf("determining whether the repository is a full clone: %w", err)
79-
}
80-
if !full {
81-
return nil, errors.New("this appears to be a shallow clone; full clone required")
82-
}
83-
84-
return &repo, nil
99+
return NewRepositoryFromGitDir(gitDir)
85100
}
86101

87102
// IsFull returns `true` iff `repo` appears to be a full clone.

0 commit comments

Comments
 (0)