Use relative path in Gerrit line comments.
Change-Id: I1f453a04b1f7f546c5cf2f6852d06f0889b012e5
diff --git a/checkstyle/checkstyle.go b/checkstyle/checkstyle.go
index e81121f..454a367 100644
--- a/checkstyle/checkstyle.go
+++ b/checkstyle/checkstyle.go
@@ -25,6 +25,7 @@
"log"
"os"
"os/exec"
+ "strings"
)
var checkstylejar = flag.String("checkstylejar", "../tools/antlib/checkstyle-5.7-all.jar", "path to checkstyle jar")
@@ -43,11 +44,23 @@
}
// ReadXML reads a checkstyle XML report from reader.
-func ReadXML(reader io.Reader) ([]File, error) {
+func ReadXML(reader io.Reader, pathPrefix string) ([]File, error) {
var res struct {
Files []File `xml:"file"`
}
- return res.Files, xml.NewDecoder(reader).Decode(&res)
+ if err := xml.NewDecoder(reader).Decode(&res); err != nil {
+ return nil, err
+ }
+
+ if !strings.HasSuffix(pathPrefix, "/") {
+ pathPrefix += "/"
+ }
+
+ for i := range res.Files {
+ // Make sure the filenames are relative.
+ res.Files[i].Name = strings.TrimPrefix(res.Files[i].Name, pathPrefix)
+ }
+ return res.Files, nil
}
// Run runs checkstyle using the specified config against the specified
@@ -57,6 +70,11 @@
return []File{}, nil
}
+ workingDir, err := os.Getwd()
+ if err != nil {
+ return nil, err
+ }
+
cmd := exec.Command("java", append([]string{"-cp", *checkstylejar,
"-Dcheckstyle.header.file=eclipse/settings/code-style/google.header",
"com.puppycrawl.tools.checkstyle.Main", "-f", "xml", "-c", config},
@@ -73,5 +91,5 @@
}
// TODO(mdempsky): Check cmd.Wait() exit code?
defer cmd.Wait()
- return ReadXML(reader)
+ return ReadXML(reader, workingDir)
}
diff --git a/checkstyle/checkstyle_test.go b/checkstyle/checkstyle_test.go
index ba7e99c..8db246b 100644
--- a/checkstyle/checkstyle_test.go
+++ b/checkstyle/checkstyle_test.go
@@ -27,7 +27,7 @@
func TestReadXML(t *testing.T) {
input := `
<checkstyle>
- <file name="foo">
+ <file name="/absolute/path/foo">
<error line="15" severity="error" message="hello"/>
<error line="27" severity="warning" message="bye bye"/>
</file>
@@ -48,7 +48,7 @@
}},
}
- actual, err := checkstyle.ReadXML(strings.NewReader(input))
+ actual, err := checkstyle.ReadXML(strings.NewReader(input), "/absolute/path")
if err != nil {
t.Error("ReadXML returned failure:", err)
}