Skip to content

Commit 4c436e2

Browse files
committed
Consolidate workspace dependencies into one bundle
1 parent 645fa91 commit 4c436e2

6 files changed

Lines changed: 41 additions & 44 deletions

File tree

internal/etch/git.go

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,17 @@ import (
77
"strings"
88
)
99

10+
// Workspace captures the repository snapshot and working directory that an etch
11+
// invocation reasons about. All process, Git, and filesystem effects are routed
12+
// through its private dependency bundle.
1013
type Workspace struct {
1114
CWD string
1215
Root string
1316
Head string
1417
Ref string
1518
Unborn bool
1619
Untracked bool
17-
git gitRunner
18-
worktree workingTreeFS
19-
temp workspaceTempStore
20-
paths workspacePaths
21-
}
22-
23-
type workspaceDeps struct {
24-
git gitRunner
25-
worktree workingTreeFS
26-
temp workspaceTempStore
27-
paths workspacePaths
28-
}
29-
30-
func (d workspaceDeps) withDefaults() workspaceDeps {
31-
if d.git == nil {
32-
d.git = realGitRunner{}
33-
}
34-
if d.worktree == nil {
35-
d.worktree = osWorkingTreeFS{}
36-
}
37-
if d.temp == nil {
38-
d.temp = osWorkspaceTempStore{}
39-
}
40-
if d.paths == nil {
41-
d.paths = osWorkspacePaths{}
42-
}
43-
return d
20+
deps workspaceDeps
4421
}
4522

4623
// OpenWorkspace opens the process working directory. Prefer OpenWorkspaceAt
@@ -58,6 +35,7 @@ func openWorkspace(untracked bool, deps workspaceDeps) (*Workspace, error) {
5835
return openWorkspaceAt(cwd, untracked, deps)
5936
}
6037

38+
// OpenWorkspaceAt opens the Git worktree containing cwd.
6139
func OpenWorkspaceAt(cwd string, untracked bool) (*Workspace, error) {
6240
return openWorkspaceAt(cwd, untracked, workspaceDeps{})
6341
}
@@ -95,7 +73,7 @@ func openWorkspaceAt(cwd string, untracked bool, deps workspaceDeps) (*Workspace
9573
if err == nil {
9674
ref = strings.TrimSpace(string(refBytes))
9775
}
98-
return &Workspace{CWD: cwd, Root: root, Head: head, Ref: ref, Unborn: unborn, Untracked: untracked, git: deps.git, worktree: deps.worktree, temp: deps.temp, paths: deps.paths}, nil
76+
return &Workspace{CWD: cwd, Root: root, Head: head, Ref: ref, Unborn: unborn, Untracked: untracked, deps: deps}, nil
9977
}
10078

10179
type ResolvedPath struct {

internal/etch/git_runner.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ type gitRunner interface {
2222
type realGitRunner struct{}
2323

2424
func (w *Workspace) gitRunner() gitRunner {
25-
if w.git != nil {
26-
return w.git
27-
}
28-
return realGitRunner{}
25+
return w.workspaceDeps().git
2926
}
3027

3128
func (realGitRunner) output(dir string, env []string, args ...string) ([]byte, error) {

internal/etch/working_tree.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ type workingTreeFS interface {
1919
type osWorkingTreeFS struct{}
2020

2121
func (w *Workspace) workingTreeFS() workingTreeFS {
22-
if w.worktree != nil {
23-
return w.worktree
24-
}
25-
return osWorkingTreeFS{}
22+
return w.workspaceDeps().worktree
2623
}
2724

2825
func (w *Workspace) readWorktreeFile(ch fileChange) ([]byte, bool, error) {

internal/etch/workspace_deps.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package etch
2+
3+
// workspaceDeps is the private bundle of injectable Workspace boundaries. Tests
4+
// can replace a specific boundary, while production constructors leave the
5+
// bundle empty and receive OS/Git backed defaults.
6+
type workspaceDeps struct {
7+
git gitRunner
8+
worktree workingTreeFS
9+
temp workspaceTempStore
10+
paths workspacePaths
11+
}
12+
13+
func (d workspaceDeps) withDefaults() workspaceDeps {
14+
if d.git == nil {
15+
d.git = realGitRunner{}
16+
}
17+
if d.worktree == nil {
18+
d.worktree = osWorkingTreeFS{}
19+
}
20+
if d.temp == nil {
21+
d.temp = osWorkspaceTempStore{}
22+
}
23+
if d.paths == nil {
24+
d.paths = osWorkspacePaths{}
25+
}
26+
return d
27+
}
28+
29+
func (w *Workspace) workspaceDeps() workspaceDeps {
30+
return w.deps.withDefaults()
31+
}

internal/etch/workspace_paths.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ type workspacePaths interface {
1717
type osWorkspacePaths struct{}
1818

1919
func (w *Workspace) pathResolver() workspacePaths {
20-
if w.paths != nil {
21-
return w.paths
22-
}
23-
return osWorkspacePaths{}
20+
return w.workspaceDeps().paths
2421
}
2522

2623
func (osWorkspacePaths) getwd() (string, error) {

internal/etch/workspace_temp.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ type workspaceTempFile interface {
2121
type osWorkspaceTempStore struct{}
2222

2323
func (w *Workspace) tempStore() workspaceTempStore {
24-
if w.temp != nil {
25-
return w.temp
26-
}
27-
return osWorkspaceTempStore{}
24+
return w.workspaceDeps().temp
2825
}
2926

3027
func (osWorkspaceTempStore) mkdir(pattern string) (string, error) {

0 commit comments

Comments
 (0)