-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor git client and add tests (cli#6525)
- Loading branch information
Showing
11 changed files
with
1,150 additions
and
354 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package git | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"errors" | ||
"io" | ||
"os/exec" | ||
|
||
"github.com/cli/cli/v2/internal/run" | ||
) | ||
|
||
type commandCtx = func(ctx context.Context, name string, args ...string) *exec.Cmd | ||
|
||
type gitCommand struct { | ||
*exec.Cmd | ||
} | ||
|
||
func (gc *gitCommand) Run() error { | ||
stderr := &bytes.Buffer{} | ||
if gc.Cmd.Stderr == nil { | ||
gc.Cmd.Stderr = stderr | ||
} | ||
// This is a hack in order to not break the hundreds of | ||
// existing tests that rely on `run.PrepareCmd` to be invoked. | ||
err := run.PrepareCmd(gc.Cmd).Run() | ||
if err != nil { | ||
ge := GitError{err: err, Stderr: stderr.String()} | ||
var exitError *exec.ExitError | ||
if errors.As(err, &exitError) { | ||
ge.ExitCode = exitError.ExitCode() | ||
} | ||
return &ge | ||
} | ||
return nil | ||
} | ||
|
||
func (gc *gitCommand) Output() ([]byte, error) { | ||
gc.Stdout = nil | ||
gc.Stderr = nil | ||
// This is a hack in order to not break the hundreds of | ||
// existing tests that rely on `run.PrepareCmd` to be invoked. | ||
out, err := run.PrepareCmd(gc.Cmd).Output() | ||
if err != nil { | ||
ge := GitError{err: err} | ||
var exitError *exec.ExitError | ||
if errors.As(err, &exitError) { | ||
ge.Stderr = string(exitError.Stderr) | ||
ge.ExitCode = exitError.ExitCode() | ||
} | ||
err = &ge | ||
} | ||
return out, err | ||
} | ||
|
||
func (gc *gitCommand) setRepoDir(repoDir string) { | ||
for i, arg := range gc.Args { | ||
if arg == "-C" { | ||
gc.Args[i+1] = repoDir | ||
return | ||
} | ||
} | ||
// Handle "--" invocations for testing purposes. | ||
var index int | ||
for i, arg := range gc.Args { | ||
if arg == "--" { | ||
index = i + 1 | ||
} | ||
} | ||
gc.Args = append(gc.Args[:index+3], gc.Args[index+1:]...) | ||
gc.Args[index+1] = "-C" | ||
gc.Args[index+2] = repoDir | ||
} | ||
|
||
// Allow individual commands to be modified from the default client options. | ||
type CommandModifier func(*gitCommand) | ||
|
||
func WithStderr(stderr io.Writer) CommandModifier { | ||
return func(gc *gitCommand) { | ||
gc.Stderr = stderr | ||
} | ||
} | ||
|
||
func WithStdout(stdout io.Writer) CommandModifier { | ||
return func(gc *gitCommand) { | ||
gc.Stdout = stdout | ||
} | ||
} | ||
|
||
func WithStdin(stdin io.Reader) CommandModifier { | ||
return func(gc *gitCommand) { | ||
gc.Stdin = stdin | ||
} | ||
} | ||
|
||
func WithRepoDir(repoDir string) CommandModifier { | ||
return func(gc *gitCommand) { | ||
gc.setRepoDir(repoDir) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package git | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
// ErrNotOnAnyBranch indicates that the user is in detached HEAD state. | ||
var ErrNotOnAnyBranch = errors.New("git: not on any branch") | ||
|
||
type NotInstalled struct { | ||
message string | ||
err error | ||
} | ||
|
||
func (e *NotInstalled) Error() string { | ||
return e.message | ||
} | ||
|
||
func (e *NotInstalled) Unwrap() error { | ||
return e.err | ||
} | ||
|
||
type GitError struct { | ||
ExitCode int | ||
Stderr string | ||
err error | ||
} | ||
|
||
func (ge *GitError) Error() string { | ||
if ge.Stderr == "" { | ||
return fmt.Sprintf("failed to run git: %v", ge.err) | ||
} | ||
return fmt.Sprintf("failed to run git: %s", ge.Stderr) | ||
} | ||
|
||
func (ge *GitError) Unwrap() error { | ||
return ge.err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters