Skip to content

Commit df3dac0

Browse files
committed
refactor: remove EnsureBranchPushed functionality
- Remove EnsureBranchPushed function from gh package - Remove EnsureBranchPushedFunc variable and its usage from pr command - Remove runGitCommand helper function - Remove all related tests for EnsureBranchPushed
1 parent 2bc5ba9 commit df3dac0

6 files changed

Lines changed: 7 additions & 159 deletions

File tree

internal/cmd/cmd.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@ func Run(ctx context.Context, provider providers.Provider, stdout, stderr io.Wri
9696
}
9797

9898
func (r *Registry) UsageTo(w io.Writer) {
99-
fmt.Fprintf(w, "Usage: llm <command> [options]\n\n")
100-
fmt.Fprintf(w, "Commands:\n")
99+
_, _ = fmt.Fprintf(w, "Usage: llm <command> [options]\n\n")
100+
_, _ = fmt.Fprintf(w, "Commands:\n")
101101
r.writeCommands(w, r.commands, 2)
102102
}
103103

@@ -150,9 +150,9 @@ func findCommand(commands []*Command, name string) *Command {
150150
func (r *Registry) writeCommands(w io.Writer, commands []*Command, indent int) {
151151
for _, cmd := range commands {
152152
if indent <= 2 {
153-
fmt.Fprintf(w, "%s%-19s %s\n", spaces(indent), cmd.Usage, cmd.Description)
153+
_, _ = fmt.Fprintf(w, "%s%-19s %s\n", spaces(indent), cmd.Usage, cmd.Description)
154154
} else {
155-
fmt.Fprintf(w, "%s%-17s %s\n", spaces(indent), cmd.Usage, cmd.Description)
155+
_, _ = fmt.Fprintf(w, "%s%-17s %s\n", spaces(indent), cmd.Usage, cmd.Description)
156156
}
157157

158158
if len(cmd.Subcommands) > 0 {

internal/cmd/cmd_test.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,9 @@ func TestRunRoutesAskAndCommit(t *testing.T) {
137137
func TestRunGHSubcommands(t *testing.T) {
138138
originalGHRun := prcmd.RunFunc
139139
originalGHCreatePullRequest := prcmd.CreatePullRequestFunc
140-
originalEnsureBranchPushed := prcmd.EnsureBranchPushedFunc
141140
t.Cleanup(func() {
142141
prcmd.RunFunc = originalGHRun
143142
prcmd.CreatePullRequestFunc = originalGHCreatePullRequest
144-
prcmd.EnsureBranchPushedFunc = originalEnsureBranchPushed
145143
})
146144

147145
deps := Dependencies{
@@ -157,7 +155,6 @@ func TestRunGHSubcommands(t *testing.T) {
157155
args []string
158156
runErr error
159157
createErr error
160-
pushErr error
161158
createOutput string
162159
wantErr bool
163160
wantErrSubstr string
@@ -196,13 +193,6 @@ func TestRunGHSubcommands(t *testing.T) {
196193
wantErr: true,
197194
wantErrSubstr: "generation failed",
198195
},
199-
{
200-
name: "returns push error",
201-
args: []string{"gh", "pr"},
202-
pushErr: errors.New("push failed"),
203-
wantErr: true,
204-
wantErrSubstr: "push failed",
205-
},
206196
{
207197
name: "returns create error",
208198
args: []string{"gh", "pr"},
@@ -235,10 +225,6 @@ func TestRunGHSubcommands(t *testing.T) {
235225
return tt.createOutput, nil
236226
}
237227

238-
prcmd.EnsureBranchPushedFunc = func(client gh.Client) error {
239-
return tt.pushErr
240-
}
241-
242228
err := defaultRegistry.Run(context.Background(), deps, tt.args)
243229

244230
if (err != nil) != tt.wantErr {

internal/cmd/gh/pr/command.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ const (
1616
)
1717

1818
var (
19-
RunFunc = gh.Run
20-
CreatePullRequestFunc = gh.CreatePullRequest
21-
EnsureBranchPushedFunc = gh.EnsureBranchPushed
19+
RunFunc = gh.Run
20+
CreatePullRequestFunc = gh.CreatePullRequest
2221
)
2322

2423
func Run(ctx context.Context, provider providers.Provider, client gh.Client, stdout, stderr io.Writer, args []string) error {
@@ -31,10 +30,6 @@ func Run(ctx context.Context, provider providers.Provider, client gh.Client, std
3130
return err
3231
}
3332

34-
if err := EnsureBranchPushedFunc(client); err != nil {
35-
return err
36-
}
37-
3833
output, err := CreatePullRequestFunc(pr)
3934
if err != nil {
4035
return err

internal/gh/gh.go

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,6 @@ var (
2929
return "", fmt.Errorf("gh %s: %s", strings.Join(args, " "), strings.TrimSpace(stderr.String()))
3030
}
3131

32-
return strings.TrimSpace(stdout.String()), nil
33-
}
34-
runGitCommand = func(args ...string) (string, error) {
35-
cmd := execCommand("git", args...)
36-
var stdout, stderr bytes.Buffer
37-
cmd.Stdout = &stdout
38-
cmd.Stderr = &stderr
39-
40-
if err := cmd.Run(); err != nil {
41-
return "", fmt.Errorf("git %s: %s", strings.Join(args, " "), strings.TrimSpace(stderr.String()))
42-
}
43-
4432
return strings.TrimSpace(stdout.String()), nil
4533
}
4634
)
@@ -114,30 +102,6 @@ func CreatePullRequest(pr *PullRequest) (string, error) {
114102
return output, nil
115103
}
116104

117-
func EnsureBranchPushed(git Client) error {
118-
if _, err := runGitCommand("rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}"); err == nil {
119-
if _, err := runGitCommand("push"); err != nil {
120-
return fmt.Errorf("pushing current branch: %w", err)
121-
}
122-
return nil
123-
}
124-
125-
branch, err := git.GetCurrentBranch()
126-
if err != nil {
127-
return fmt.Errorf("getting current branch for push: %w", err)
128-
}
129-
130-
if strings.TrimSpace(branch) == "" {
131-
return fmt.Errorf("unable to determine current branch for push")
132-
}
133-
134-
if _, err := runGitCommand("push", "-u", "origin", branch); err != nil {
135-
return fmt.Errorf("pushing current branch to origin: %w", err)
136-
}
137-
138-
return nil
139-
}
140-
141105
func getBaseDiffContext(git Client) (string, string, string, error) {
142106
base, mergeBase, err := resolveBaseBranch(git)
143107
if err != nil {

internal/gh/gh_test.go

Lines changed: 0 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -385,100 +385,3 @@ func TestCreatePullRequest(t *testing.T) {
385385
})
386386
}
387387
}
388-
389-
func TestEnsureBranchPushed(t *testing.T) {
390-
originalRunGitCommand := runGitCommand
391-
t.Cleanup(func() {
392-
runGitCommand = originalRunGitCommand
393-
})
394-
395-
tests := []struct {
396-
name string
397-
git *stubGitClient
398-
runGit func(args ...string) (string, error)
399-
wantErr bool
400-
wantErrSubstr string
401-
wantCalls [][]string
402-
}{
403-
{
404-
name: "pushes tracked branch with git push",
405-
git: &stubGitClient{branch: "feature/test"},
406-
runGit: func(args ...string) (string, error) {
407-
return "", nil
408-
},
409-
wantCalls: [][]string{
410-
{"rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}"},
411-
{"push"},
412-
},
413-
},
414-
{
415-
name: "pushes untracked branch to origin with upstream",
416-
git: &stubGitClient{branch: "feature/test"},
417-
runGit: func(args ...string) (string, error) {
418-
if reflect.DeepEqual(args, []string{"rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}"}) {
419-
return "", errors.New("no upstream")
420-
}
421-
return "", nil
422-
},
423-
wantCalls: [][]string{
424-
{"rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}"},
425-
{"push", "-u", "origin", "feature/test"},
426-
},
427-
},
428-
{
429-
name: "fails when push errors",
430-
git: &stubGitClient{branch: "feature/test"},
431-
runGit: func(args ...string) (string, error) {
432-
if reflect.DeepEqual(args, []string{"push"}) {
433-
return "", errors.New("push failed")
434-
}
435-
return "", nil
436-
},
437-
wantErr: true,
438-
wantErrSubstr: "pushing current branch",
439-
wantCalls: [][]string{
440-
{"rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}"},
441-
{"push"},
442-
},
443-
},
444-
{
445-
name: "fails when branch is unknown for upstream setup",
446-
git: &stubGitClient{branch: ""},
447-
runGit: func(args ...string) (string, error) {
448-
if reflect.DeepEqual(args, []string{"rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}"}) {
449-
return "", errors.New("no upstream")
450-
}
451-
return "", nil
452-
},
453-
wantErr: true,
454-
wantErrSubstr: "unable to determine current branch",
455-
wantCalls: [][]string{
456-
{"rev-parse", "--abbrev-ref", "--symbolic-full-name", "@{u}"},
457-
},
458-
},
459-
}
460-
461-
for _, tt := range tests {
462-
t.Run(tt.name, func(t *testing.T) {
463-
var gotCalls [][]string
464-
runGitCommand = func(args ...string) (string, error) {
465-
gotCalls = append(gotCalls, append([]string(nil), args...))
466-
return tt.runGit(args...)
467-
}
468-
469-
err := EnsureBranchPushed(tt.git)
470-
471-
if (err != nil) != tt.wantErr {
472-
t.Fatalf("EnsureBranchPushed() error = %v, wantErr %v", err, tt.wantErr)
473-
}
474-
475-
if tt.wantErr && tt.wantErrSubstr != "" && (err == nil || !strings.Contains(err.Error(), tt.wantErrSubstr)) {
476-
t.Fatalf("EnsureBranchPushed() error = %v, want substring %q", err, tt.wantErrSubstr)
477-
}
478-
479-
if !reflect.DeepEqual(gotCalls, tt.wantCalls) {
480-
t.Fatalf("EnsureBranchPushed() calls = %v, want %v", gotCalls, tt.wantCalls)
481-
}
482-
})
483-
}
484-
}

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ func usage() {
7272

7373
func usageTo(w io.Writer) {
7474
cmd.UsageTo(w)
75-
fmt.Fprintf(w, "\nOptions:\n")
75+
_, _ = fmt.Fprintf(w, "\nOptions:\n")
7676
oldOutput := flag.CommandLine.Output()
7777
flag.CommandLine.SetOutput(w)
7878
defer flag.CommandLine.SetOutput(oldOutput)

0 commit comments

Comments
 (0)