Skip to content

Commit

Permalink
better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed Mar 23, 2024
1 parent edfd40d commit b7e8672
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 22 deletions.
4 changes: 2 additions & 2 deletions modules/git/command.go
Expand Up @@ -392,9 +392,9 @@ func (r *runStdError) Stderr() string {
return r.stderr
}

func (r *runStdError) IsExitCode(code int) bool {
func IsErrorExitCode(err error, code int) bool {
var exitError *exec.ExitError
if errors.As(r.err, &exitError) {
if errors.As(err, &exitError) {
return exitError.ExitCode() == code
}
return false
Expand Down
8 changes: 4 additions & 4 deletions modules/git/git.go
Expand Up @@ -341,7 +341,7 @@ func checkGitVersionCompatibility(gitVer *version.Version) error {

func configSet(key, value string) error {
stdout, _, err := NewCommand(DefaultContext, "config", "--global", "--get").AddDynamicArguments(key).RunStdString(nil)
if err != nil && !err.IsExitCode(1) {
if err != nil && !IsErrorExitCode(err, 1) {
return fmt.Errorf("failed to get git config %s, err: %w", key, err)
}

Expand All @@ -364,7 +364,7 @@ func configSetNonExist(key, value string) error {
// already exist
return nil
}
if err.IsExitCode(1) {
if IsErrorExitCode(err, 1) {
// not exist, set new config
_, _, err = NewCommand(DefaultContext, "config", "--global").AddDynamicArguments(key, value).RunStdString(nil)
if err != nil {
Expand All @@ -382,7 +382,7 @@ func configAddNonExist(key, value string) error {
// already exist
return nil
}
if err.IsExitCode(1) {
if IsErrorExitCode(err, 1) {
// not exist, add new config
_, _, err = NewCommand(DefaultContext, "config", "--global", "--add").AddDynamicArguments(key, value).RunStdString(nil)
if err != nil {
Expand All @@ -403,7 +403,7 @@ func configUnsetAll(key, value string) error {
}
return nil
}
if err.IsExitCode(1) {
if IsErrorExitCode(err, 1) {
// not exist
return nil
}
Expand Down
27 changes: 11 additions & 16 deletions modules/git/grep.go
Expand Up @@ -5,10 +5,10 @@ package git

import (
"bufio"
"bytes"
"context"
"errors"
"fmt"
"io"
"os"
"strconv"
"strings"
Expand All @@ -33,15 +33,9 @@ func GrepSearch(ctx context.Context, repo *Repository, search string, opts GrepO
if err != nil {
return nil, fmt.Errorf("unable to create os pipe to grep: %w", err)
}
stderrReader, stderrWriter, err := os.Pipe()
if err != nil {
return nil, fmt.Errorf("unable to create os pipe to grep: %w", err)
}
defer func() {
_ = stdoutReader.Close()
_ = stdoutWriter.Close()
_ = stderrReader.Close()
_ = stderrWriter.Close()
}()

/*
Expand All @@ -53,28 +47,26 @@ func GrepSearch(ctx context.Context, repo *Repository, search string, opts GrepO
HEAD:.changelog.yml
2^@repo: go-gitea/gitea
*/
var stderr []byte
var results []*GrepResult
cmd := NewCommand(ctx, "grep", "--null", "--break", "--heading", "--fixed-strings", "--line-number", "--ignore-case", "--full-name")
cmd.AddOptionValues("--context", fmt.Sprint(opts.ContextLineNumber))
if opts.IsFuzzy {
words := strings.Fields(search)
for _, word := range words {
cmd.AddOptionValues("-e", word)
cmd.AddOptionValues("-e", strings.TrimLeft(word, "-"))
}
} else {
cmd.AddOptionValues("-e", search)
cmd.AddOptionValues("-e", strings.TrimLeft(search, "-"))
}
cmd.AddDynamicArguments(util.IfZero(opts.RefName, "HEAD"))
stderr := bytes.Buffer{}
err = cmd.Run(&RunOpts{
Dir: repo.Path,
Stdout: stdoutWriter,
Stderr: stderrWriter,
Stderr: &stderr,
PipelineFunc: func(ctx context.Context, cancel context.CancelFunc) error {
_ = stdoutWriter.Close()
_ = stderrWriter.Close()
defer stdoutReader.Close()
defer stderrReader.Close()

isInBlock := false
scanner := bufio.NewScanner(stdoutReader)
Expand Down Expand Up @@ -106,12 +98,15 @@ func GrepSearch(ctx context.Context, repo *Repository, search string, opts GrepO
res.LineCodes = append(res.LineCodes, lineCode)
}
}
stderr, _ = io.ReadAll(stderrReader)
return scanner.Err()
},
})
if err != nil && !errors.Is(err, context.Canceled) && len(stderr) != 0 {
return nil, fmt.Errorf("unable to run git grep: %w, stderr: %s", err, string(stderr))
// git grep exits with 1 if no results are found
if IsErrorExitCode(err, 1) && stderr.Len() == 0 {
return nil, nil
}
if err != nil && !errors.Is(err, context.Canceled) {
return nil, fmt.Errorf("unable to run git grep: %w, stderr: %s", err, stderr.String())
}
return results, nil
}
3 changes: 3 additions & 0 deletions modules/git/grep_test.go
Expand Up @@ -34,4 +34,7 @@ func TestGrepSearch(t *testing.T) {
res, err = GrepSearch(context.Background(), repo, "no-such-content", GrepOptions{})
assert.NoError(t, err)
assert.Len(t, res, 0)

res, err = GrepSearch(context.Background(), &Repository{Path: "no-such-git-repo"}, "no-such-content", GrepOptions{})
assert.Error(t, err)
}

0 comments on commit b7e8672

Please sign in to comment.