Skip to content

Commit f647cec

Browse files
committed
feat(internal): Do not hide error & correctly split conf arguments
Signed-off-by: Cezar Craciunoiu <cezar.craciunoiu@gmail.com>
1 parent 30192f7 commit f647cec

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

internal/checkpatch/checkpatch.go

+11-14
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
package checkpatch
99

1010
import (
11-
"bytes"
1211
"context"
1312
"fmt"
1413
"io"
1514
"os"
1615
"os/exec"
16+
"path/filepath"
1717
"strconv"
1818
"strings"
1919

@@ -76,15 +76,19 @@ func NewCheckpatch(ctx context.Context, file string, opts ...PatchOption) (*Patc
7676
}
7777

7878
args := []string{
79+
"--patch",
7980
"--color=never",
81+
"--root=" + filepath.Dir(filepath.Dir(filepath.Dir(patch.script))),
8082
}
8183

8284
// Add options from the conf file in the PR
8385
content, err := os.ReadFile(patch.conf)
8486
if err != nil {
8587
return nil, fmt.Errorf("could not read checkpatch configuration file: %w", err)
8688
}
87-
args = append(args, strings.Split(strings.TrimSpace(string(content)), "\n")...)
89+
for _, line := range strings.Split(strings.TrimSpace(string(content)), "\n") {
90+
args = append(args, strings.Split(line, " ")...)
91+
}
8892

8993
// Extra ignores from the commits.
9094
if len(patch.ignores) > 0 {
@@ -97,27 +101,20 @@ func NewCheckpatch(ctx context.Context, file string, opts ...PatchOption) (*Patc
97101
args = append(args, file)
98102

99103
c := exec.Command(patch.script, args...)
104+
c.Stderr = patch.stderr
100105
c.Env = os.Environ()
101106

102107
log.G(ctx).Info(
103108
strings.Join(append([]string{patch.script}, args...), " "),
104109
)
105110

106-
var b bytes.Buffer
107-
c.Stdout = &b
108-
c.Stderr = patch.stderr
109-
110-
if err := c.Start(); err != nil {
111-
return nil, fmt.Errorf("could not start checkpatch.pl: %w", err)
111+
var out []byte
112+
if out, err = c.Output(); err != nil && out == nil {
113+
return nil, fmt.Errorf("running checkpatch.pl failed: %w", err)
112114
}
113115

114-
// Checkpatch may return non-zero exit code, so we can safely ignore the
115-
// returning error here.
116-
_ = c.Wait()
117-
118116
var note *Note
119-
120-
for _, line := range strings.Split(strings.TrimSuffix(b.String(), "\n"), "\n") {
117+
for _, line := range strings.Split(strings.TrimSuffix(string(out), "\n"), "\n") {
121118
if warning := strings.TrimPrefix(line, "WARNING:"); len(warning) < len(line) {
122119
split := strings.SplitN(warning, ":", 2)
123120
if len(split) != 2 {

0 commit comments

Comments
 (0)