Skip to content

Commit 70bd815

Browse files
committed
refactor(config): route all git calls through internal/git.Git
config.Config previously shelled out to git directly via exec.Command, bypassing the safeexec.LookPath resolution that internal/git provides. This adds ConfigGet/ConfigSet/ConfigUnset/ConfigSetWithComment/ConfigGetRegexp methods to git.Git, rewires Config to accept a *git.Git dependency (Load(path) becomes New(g)), and flips the construction order in all cmd/ call sites so git.New runs first.
1 parent 9d01b0c commit 70bd815

23 files changed

Lines changed: 217 additions & 104 deletions

cmd/adopt.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ func runAdopt(cmd *cobra.Command, args []string) error {
3636
return err
3737
}
3838

39-
cfg, err := config.Load(cwd)
39+
g := git.New(cwd)
40+
41+
cfg, err := config.New(g)
4042
if err != nil {
4143
return err
4244
}
4345

44-
g := git.New(cwd)
45-
4646
// Parent is the required positional argument
4747
parent := args[0]
4848

cmd/adopt_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
func TestAdoptBranch(t *testing.T) {
1313
dir := setupTestRepo(t)
1414

15-
cfg, _ := config.Load(dir)
1615
g := git.New(dir)
16+
cfg, _ := config.New(g)
1717

1818
trunk, _ := g.CurrentBranch()
1919
cfg.SetTrunk(trunk)
@@ -40,8 +40,8 @@ func TestAdoptBranch(t *testing.T) {
4040
func TestAdoptRejectsAlreadyTracked(t *testing.T) {
4141
dir := setupTestRepo(t)
4242

43-
cfg, _ := config.Load(dir)
4443
g := git.New(dir)
44+
cfg, _ := config.New(g)
4545

4646
trunk, _ := g.CurrentBranch()
4747
cfg.SetTrunk(trunk)
@@ -60,8 +60,8 @@ func TestAdoptRejectsAlreadyTracked(t *testing.T) {
6060
func TestAdoptRejectsUntrackedParent(t *testing.T) {
6161
dir := setupTestRepo(t)
6262

63-
cfg, _ := config.Load(dir)
6463
g := git.New(dir)
64+
cfg, _ := config.New(g)
6565

6666
trunk, _ := g.CurrentBranch()
6767
cfg.SetTrunk(trunk)
@@ -81,8 +81,8 @@ func TestAdoptRejectsUntrackedParent(t *testing.T) {
8181
func TestAdoptDetectsCycle(t *testing.T) {
8282
dir := setupTestRepo(t)
8383

84-
cfg, _ := config.Load(dir)
8584
g := git.New(dir)
85+
cfg, _ := config.New(g)
8686

8787
trunk, _ := g.CurrentBranch()
8888
cfg.SetTrunk(trunk)
@@ -117,8 +117,8 @@ func TestAdoptDetectsCycle(t *testing.T) {
117117
func TestAdoptStoresForkPoint(t *testing.T) {
118118
dir := setupTestRepo(t)
119119

120-
cfg, _ := config.Load(dir)
121120
g := git.New(dir)
121+
cfg, _ := config.New(g)
122122

123123
trunk, _ := g.CurrentBranch()
124124
cfg.SetTrunk(trunk)

cmd/cascade.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ func runCascade(cmd *cobra.Command, args []string) error {
4747
return err
4848
}
4949

50-
cfg, err := config.Load(cwd)
50+
g := git.New(cwd)
51+
52+
cfg, err := config.New(g)
5153
if err != nil {
5254
return err
5355
}
5456

55-
g := git.New(cwd)
56-
5757
// Check if cascade already in progress
5858
if state.Exists(g.GetGitDir()) {
5959
return errors.New("operation already in progress; use 'gh stack continue' or 'gh stack abort'")

cmd/continue.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func runContinue(cmd *cobra.Command, args []string) error {
6363

6464
fmt.Printf("%s Completed %s\n", s.SuccessIcon(), s.Branch(st.Current))
6565

66-
cfg, err := config.Load(cwd)
66+
cfg, err := config.New(g)
6767
if err != nil {
6868
return err
6969
}

cmd/create.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ func runCreate(cmd *cobra.Command, args []string) error {
3939
return err
4040
}
4141

42-
cfg, err := config.Load(cwd)
42+
g := git.New(cwd)
43+
44+
cfg, err := config.New(g)
4345
if err != nil {
4446
return err
4547
}
4648

47-
g := git.New(cwd)
48-
4949
// Get current branch
5050
currentBranch, err := g.CurrentBranch()
5151
if err != nil {

cmd/create_test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ import (
1414
func TestCreateFromTrunk(t *testing.T) {
1515
dir := setupTestRepo(t)
1616

17-
cfg, _ := config.Load(dir)
18-
cfg.SetTrunk("main")
19-
2017
g := git.New(dir)
18+
cfg, _ := config.New(g)
19+
cfg.SetTrunk("main")
2120

2221
// Simulate what create command does
2322
currentBranch, _ := g.CurrentBranch()
@@ -57,8 +56,8 @@ func TestCreateFromTrunk(t *testing.T) {
5756
func TestCreateFromTrackedBranch(t *testing.T) {
5857
dir := setupTestRepo(t)
5958

60-
cfg, _ := config.Load(dir)
6159
g := git.New(dir)
60+
cfg, _ := config.New(g)
6261

6362
currentBranch, _ := g.CurrentBranch()
6463
cfg.SetTrunk(currentBranch)
@@ -86,8 +85,8 @@ func TestCreateFromTrackedBranch(t *testing.T) {
8685
func TestCreateRejectsUntrackedBranch(t *testing.T) {
8786
dir := setupTestRepo(t)
8887

89-
cfg, _ := config.Load(dir)
9088
g := git.New(dir)
89+
cfg, _ := config.New(g)
9190

9291
trunk, _ := g.CurrentBranch()
9392
cfg.SetTrunk(trunk)
@@ -119,8 +118,8 @@ func TestCreateRejectsUntrackedBranch(t *testing.T) {
119118
func TestCreateWithStagedChanges(t *testing.T) {
120119
dir := setupTestRepo(t)
121120

122-
cfg, _ := config.Load(dir)
123121
g := git.New(dir)
122+
cfg, _ := config.New(g)
124123

125124
trunk, _ := g.CurrentBranch()
126125
cfg.SetTrunk(trunk)
@@ -151,8 +150,8 @@ func TestCreateWithStagedChanges(t *testing.T) {
151150
func TestCreateEmptyWithStagedChanges(t *testing.T) {
152151
dir := setupTestRepo(t)
153152

154-
cfg, _ := config.Load(dir)
155153
g := git.New(dir)
154+
cfg, _ := config.New(g)
156155

157156
trunk, _ := g.CurrentBranch()
158157
cfg.SetTrunk(trunk)
@@ -197,8 +196,8 @@ func TestBranchAlreadyExists(t *testing.T) {
197196
func TestCreateStoresForkPoint(t *testing.T) {
198197
dir := setupTestRepo(t)
199198

200-
cfg, _ := config.Load(dir)
201199
g := git.New(dir)
200+
cfg, _ := config.New(g)
202201

203202
trunk, _ := g.CurrentBranch()
204203
cfg.SetTrunk(trunk)

cmd/init.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ func runInit(cmd *cobra.Command, args []string) error {
3232
return err
3333
}
3434

35-
cfg, err := config.Load(cwd)
35+
g := git.New(cwd)
36+
37+
cfg, err := config.New(g)
3638
if err != nil {
3739
return err
3840
}
3941

40-
g := git.New(cwd)
41-
4242
// Determine trunk branch
4343
trunk := trunkFlag
4444
if trunk == "" {

cmd/init_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"testing"
99

1010
"github.com/boneskull/gh-stack/internal/config"
11+
"github.com/boneskull/gh-stack/internal/git"
1112
)
1213

1314
func setupTestRepo(t *testing.T) string {
@@ -37,9 +38,10 @@ func TestInitCommand(t *testing.T) {
3738
dir := setupTestRepo(t)
3839

3940
// Verify the config package works for init
40-
cfg, err := config.Load(dir)
41+
g := git.New(dir)
42+
cfg, err := config.New(g)
4143
if err != nil {
42-
t.Fatalf("Load failed: %v", err)
44+
t.Fatalf("New failed: %v", err)
4345
}
4446

4547
err = cfg.SetTrunk("main")

cmd/link.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ func runLink(cmd *cobra.Command, args []string) error {
3535
return err
3636
}
3737

38-
cfg, err := config.Load(cwd)
38+
g := git.New(cwd)
39+
40+
cfg, err := config.New(g)
3941
if err != nil {
4042
return err
4143
}
42-
43-
g := git.New(cwd)
4444
branch, err := g.CurrentBranch()
4545
if err != nil {
4646
return err

cmd/link_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
func TestLinkPR(t *testing.T) {
1212
dir := setupTestRepo(t)
1313

14-
cfg, _ := config.Load(dir)
1514
g := git.New(dir)
15+
cfg, _ := config.New(g)
1616

1717
trunk, _ := g.CurrentBranch()
1818
cfg.SetTrunk(trunk)
@@ -40,8 +40,8 @@ func TestLinkPR(t *testing.T) {
4040
func TestLinkRejectsUntrackedBranch(t *testing.T) {
4141
dir := setupTestRepo(t)
4242

43-
cfg, _ := config.Load(dir)
4443
g := git.New(dir)
44+
cfg, _ := config.New(g)
4545

4646
trunk, _ := g.CurrentBranch()
4747
cfg.SetTrunk(trunk)
@@ -59,8 +59,8 @@ func TestLinkRejectsUntrackedBranch(t *testing.T) {
5959
func TestLinkOverwritesPR(t *testing.T) {
6060
dir := setupTestRepo(t)
6161

62-
cfg, _ := config.Load(dir)
6362
g := git.New(dir)
63+
cfg, _ := config.New(g)
6464

6565
trunk, _ := g.CurrentBranch()
6666
cfg.SetTrunk(trunk)

0 commit comments

Comments
 (0)