Change error handling on ReadBranchConfig to respect git Exit Codes

This commit is contained in:
Tyler McGoffin 2025-01-10 09:59:21 -08:00
parent 3d2748789e
commit 322a43feff
2 changed files with 13 additions and 15 deletions

View file

@ -392,12 +392,10 @@ func (c *Client) ReadBranchConfig(ctx context.Context, branch string) (BranchCon
out, err := cmd.Output()
if err != nil {
// This is the error we expect if the git command does not run successfully.
// Note: err is non-nil if the command is successful but has no output
// If the ExitCode is 1, then we just didn't find any config for the branch.
var gitError *GitError
if errors.As(err, &gitError) {
if gitError.Stderr != "" {
return BranchConfig{}, err
}
if ok := errors.As(err, &gitError); ok && gitError.ExitCode != 1 {
return BranchConfig{}, err
}
return BranchConfig{}, nil
}

View file

@ -744,16 +744,7 @@ func TestClientReadBranchConfig(t *testing.T) {
wantError: nil,
},
{
name: "output error",
cmdExitStatus: 1,
cmdStdout: "",
cmdStderr: "git error message",
branch: "trunk",
wantBranchConfig: BranchConfig{},
wantError: &GitError{},
},
{
name: "git config runs successfully but returns no output",
name: "git config runs successfully but returns no output (Exit Code 1)",
cmdExitStatus: 1,
cmdStdout: "",
cmdStderr: "",
@ -761,6 +752,15 @@ func TestClientReadBranchConfig(t *testing.T) {
wantBranchConfig: BranchConfig{},
wantError: nil,
},
{
name: "output error (Exit Code > 1)",
cmdExitStatus: 2,
cmdStdout: "",
cmdStderr: "git error message",
branch: "trunk",
wantBranchConfig: BranchConfig{},
wantError: &GitError{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {