Add PushDefault method to git client
This commit is contained in:
parent
6355ed7c08
commit
5a8dd35ba7
3 changed files with 78 additions and 11 deletions
|
|
@ -482,6 +482,21 @@ func (c *Client) SetBranchConfig(ctx context.Context, branch, name, value string
|
|||
return err
|
||||
}
|
||||
|
||||
// PushDefault returns the value of push.default in the config. If the value
|
||||
// is not set, it returns "simple" (the default value).
|
||||
func (c *Client) PushDefault(ctx context.Context) (string, error) {
|
||||
pushDefault, err := c.Config(ctx, "push.default")
|
||||
if err == nil {
|
||||
return pushDefault, nil
|
||||
}
|
||||
|
||||
var gitError *GitError
|
||||
if ok := errors.As(err, &gitError); ok && gitError.ExitCode == 1 {
|
||||
return "simple", nil
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
|
||||
func (c *Client) DeleteLocalTag(ctx context.Context, tag string) error {
|
||||
args := []string{"tag", "-d", tag}
|
||||
cmd, err := c.Command(ctx, args...)
|
||||
|
|
|
|||
|
|
@ -1191,6 +1191,68 @@ func Test_parseRemoteURLOrName(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestClientPushDefault(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
commandResult commandResult
|
||||
wantPushDefault string
|
||||
wantError *GitError
|
||||
}{
|
||||
{
|
||||
name: "push default is not set",
|
||||
commandResult: commandResult{
|
||||
ExitStatus: 1,
|
||||
Stderr: "error: key does not contain a section: remote.pushDefault",
|
||||
},
|
||||
wantPushDefault: "simple",
|
||||
wantError: nil,
|
||||
},
|
||||
{
|
||||
name: "push default is set to current",
|
||||
commandResult: commandResult{
|
||||
ExitStatus: 0,
|
||||
Stdout: "current",
|
||||
},
|
||||
wantPushDefault: "current",
|
||||
wantError: nil,
|
||||
},
|
||||
{
|
||||
name: "push default errors",
|
||||
commandResult: commandResult{
|
||||
ExitStatus: 128,
|
||||
Stderr: "fatal: git error",
|
||||
},
|
||||
wantPushDefault: "",
|
||||
wantError: &GitError{
|
||||
ExitCode: 128,
|
||||
Stderr: "fatal: git error",
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
cmdCtx := createMockedCommandContext(t, mockedCommands{
|
||||
`path/to/git config push.default`: tt.commandResult,
|
||||
},
|
||||
)
|
||||
client := Client{
|
||||
GitPath: "path/to/git",
|
||||
commandContext: cmdCtx,
|
||||
}
|
||||
pushDefault, err := client.PushDefault(context.Background())
|
||||
if tt.wantError != nil {
|
||||
var gitError *GitError
|
||||
require.ErrorAs(t, err, &gitError)
|
||||
assert.Equal(t, tt.wantError.ExitCode, gitError.ExitCode)
|
||||
assert.Equal(t, tt.wantError.Stderr, gitError.Stderr)
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
assert.Equal(t, tt.wantPushDefault, pushDefault)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestClientDeleteLocalTag(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
|
|
|||
|
|
@ -59,17 +59,7 @@ func NewFinder(factory *cmdutil.Factory) PRFinder {
|
|||
remotesFn: factory.Remotes,
|
||||
httpClient: factory.HttpClient,
|
||||
pushDefault: func() (string, error) {
|
||||
pushDefault, err := factory.GitClient.Config(context.Background(), "push.default")
|
||||
if err == nil {
|
||||
return pushDefault, nil
|
||||
}
|
||||
|
||||
var gitErr *git.GitError
|
||||
if ok := errors.As(err, &gitErr); ok && gitErr.ExitCode == 1 {
|
||||
return "simple", nil
|
||||
}
|
||||
|
||||
return "", err
|
||||
return factory.GitClient.PushDefault(context.Background())
|
||||
},
|
||||
progress: factory.IOStreams,
|
||||
branchConfig: func(s string) (git.BranchConfig, error) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue