Repository: reviewdog/reviewdog
Pull request #1782 (2024-06-14 14:43:57)

refactor: create pathutil package
- [x] Updated Unreleased section in [CHANGELOG](https://github.com/reviewdog/reviewdog/blob/master/CHANGELOG.md) or it's not notable changes.




Patch:
diff --git a/cmd/reviewdog/doghouse.go b/cmd/reviewdog/doghouse.go
index 13e1e837..ae7578e8 100644
--- a/cmd/reviewdog/doghouse.go
+++ b/cmd/reviewdog/doghouse.go
@@ -17,7 +17,7 @@ import (
 	"github.com/reviewdog/reviewdog/cienv"
 	"github.com/reviewdog/reviewdog/doghouse"
 	"github.com/reviewdog/reviewdog/doghouse/client"
-	"github.com/reviewdog/reviewdog/filter"
+	"github.com/reviewdog/reviewdog/pathutil"
 	"github.com/reviewdog/reviewdog/project"
 	"github.com/reviewdog/reviewdog/proto/rdf"
 	"github.com/reviewdog/reviewdog/service/github/githubutils"
@@ -181,7 +181,7 @@ func postResultSet(ctx context.Context, resultSet *reviewdog.ResultMap,
 }
 
 func checkResultToAnnotation(d *rdf.Diagnostic, wd, gitRelWd string) *doghouse.Annotation {
-	d.GetLocation().Path = filter.NormalizePath(d.GetLocation().GetPath(), wd, gitRelWd)
+	d.GetLocation().Path = pathutil.NormalizePath(d.GetLocation().GetPath(), wd, gitRelWd)
 	return &doghouse.Annotation{
 		Diagnostic: d,
 	}
diff --git a/filter/diff_filter.go b/filter/diff_filter.go
index c658ece9..a449089f 100644
--- a/filter/diff_filter.go
+++ b/filter/diff_filter.go
@@ -2,10 +2,9 @@ package filter
 
 import (
 	"fmt"
-	"path/filepath"
-	"strings"
 
 	"github.com/reviewdog/reviewdog/diff"
+	"github.com/reviewdog/reviewdog/pathutil"
 	"github.com/reviewdog/reviewdog/service/serviceutil"
 )
 
@@ -168,21 +167,7 @@ func (df *DiffFilter) isSignificantLine(line *diff.Line) bool {
 type normalizedPath struct{ p string }
 
 func (df *DiffFilter) normalizePath(path string) normalizedPath {
-	return normalizedPath{p: NormalizePath(path, df.cwd, df.projectRelPath)}
-}
-
-func contains(path, base string) bool {
-	ps := splitPathList(path)
-	bs := splitPathList(base)
-	if len(ps) < len(bs) {
-		return false
-	}
-	for i := range bs {
-		if bs[i] != ps[i] {
-			return false
-		}
-	}
-	return true
+	return normalizedPath{p: pathutil.NormalizePath(path, df.cwd, df.projectRelPath)}
 }
 
 // Assuming diff path should be relative path to the project root dir by
@@ -191,25 +176,5 @@ func contains(path, base string) bool {
 // `git diff --relative` can returns relative path to current workdir, so we
 // ask users not to use it for reviewdog command.
 func (df *DiffFilter) normalizeDiffPath(filediff *diff.FileDiff) normalizedPath {
-	return normalizedPath{p: NormalizeDiffPath(filediff.PathNew, df.strip)}
-}
-
-// NormalizeDiffPath return path normalized path from given path in diff with
-// strip.
-func NormalizeDiffPath(diffpath string, strip int) string {
-	if diffpath == "/dev/null" {
-		return ""
-	}
-	path := diffpath
-	if strip > 0 && !filepath.IsAbs(path) {
-		ps := splitPathList(path)
-		if len(ps) > strip {
-			path = filepath.Join(ps[strip:]...)
-		}
-	}
-	return filepath.ToSlash(filepath.Clean(path))
-}
-
-func splitPathList(path string) []string {
-	return strings.Split(filepath.ToSlash(path), "/")
+	return normalizedPath{p: pathutil.NormalizeDiffPath(filediff.PathNew, df.strip)}
 }
diff --git a/filter/filter.go b/filter/filter.go
index 0e6ca8f3..eef96a94 100644
--- a/filter/filter.go
+++ b/filter/filter.go
@@ -1,9 +1,8 @@
 package filter
 
 import (
-	"path/filepath"
-
 	"github.com/reviewdog/reviewdog/diff"
+	"github.com/reviewdog/reviewdog/pathutil"
 	"github.com/reviewdog/reviewdog/proto/rdf"
 )
 
@@ -42,7 +41,7 @@ func FilterCheck(results []*rdf.Diagnostic, diff []*diff.FileDiff, strip int,
 	for _, result := range results {
 		check := &FilteredDiagnostic{Diagnostic: result, SourceLines: make(map[int]string)}
 		loc := result.GetLocation()
-		loc.Path = NormalizePath(loc.GetPath(), cwd, "")
+		loc.Path = pathutil.NormalizePath(loc.GetPath(), cwd, "")
 		startLine := int(loc.GetRange().GetStart().GetLine())
 		endLine := int(loc.GetRange().GetEnd().GetLine())
 		if endLine == 0 {
@@ -86,35 +85,14 @@ func FilterCheck(results []*rdf.Diagnostic, diff []*diff.FileDiff, strip int,
 	return checks
 }
 
-// NormalizePath return normalized path with workdir and relative path to
-// project.
-func NormalizePath(path, workdir, projectRelPath string) string {
-	path = filepath.Clean(path)
-	if path == "." {
-		return ""
-	}
-	// Convert absolute path to relative path only if the path is in current
-	// directory.
-	if filepath.IsAbs(path) && workdir != "" && contains(path, workdir) {
-		relPath, err := filepath.Rel(workdir, path)
-		if err == nil {
-			path = relPath
-		}
-	}
-	if !filepath.IsAbs(path) && projectRelPath != "" {
-		path = filepath.Join(projectRelPath, path)
-	}
-	return filepath.ToSlash(path)
-}
-
 func getOldPosition(filediff *diff.FileDiff, strip int, newPath string, newLine int) (oldPath string, oldLine int) {
 	if filediff == nil {
 		return "", 0
 	}
-	if NormalizeDiffPath(filediff.PathNew, strip) != newPath {
+	if pathutil.NormalizeDiffPath(filediff.PathNew, strip) != newPath {
 		return "", 0
 	}
-	oldPath = NormalizeDiffPath(filediff.PathOld, strip)
+	oldPath = pathutil.NormalizeDiffPath(filediff.PathOld, strip)
 	delta := 0
 	for _, hunk := range filediff.Hunks {
 		if newLine < hunk.StartLineNew {
diff --git a/go.mod b/go.mod
index a2b4e1ab..e36b8b5c 100644
--- a/go.mod
+++ b/go.mod
@@ -23,7 +23,7 @@ require (
 	github.com/vvakame/sdlog v1.2.0
 	github.com/xanzy/go-gitlab v0.105.0
 	go.opencensus.io v0.24.0
-	golang.org/x/build v0.0.0-20240614052551-006a6ae273bc
+	golang.org/x/build v0.0.0-20240613140737-ddd71ea2aad1
 	golang.org/x/oauth2 v0.21.0
 	golang.org/x/sync v0.7.0
 	google.golang.org/protobuf v1.34.2
diff --git a/go.sum b/go.sum
index c39ed684..54b96089 100644
--- a/go.sum
+++ b/go.sum
@@ -301,8 +301,8 @@ go.opentelemetry.io/otel/sdk v1.24.0/go.mod h1:KVrIYw6tEubO9E96HQpcmpTKDVn9gdv35
 go.opentelemetry.io/otel/trace v1.24.0 h1:CsKnnL4dUAr/0llH9FKuc698G04IrpWV0MQA/Y1YELI=
 go.opentelemetry.io/otel/trace v1.24.0/go.mod h1:HPc3Xr/cOApsBI154IU0OI0HJexz+aw5uPdbs3UCjNU=
 go.opentelemetry.io/proto/otlp v0.7.0/go.mod h1:PqfVotwruBrMGOCsRd/89rSnXhoiJIqeYNgFYFoEGnI=
-golang.org/x/build v0.0.0-20240614052551-006a6ae273bc h1:nPGnN2zlRQK7VRJB4qmPI6zNbdXUNuBlGtZQfdgYpKo=
-golang.org/x/build v0.0.0-20240614052551-006a6ae273bc/go.mod h1:qQ0HMOUQTb/P5pk3VotxACIhhBZJI7JpUbfnU8kY4fA=
+golang.org/x/build v0.0.0-20240613140737-ddd71ea2aad1 h1:hj17KDT+PzdqFqPaSVmH5j4W7u6j9+LdTsCAJFlz7cU=
+golang.org/x/build v0.0.0-20240613140737-ddd71ea2aad1/go.mod h1:qQ0HMOUQTb/P5pk3VotxACIhhBZJI7JpUbfnU8kY4fA=
 golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
 golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
 golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
diff --git a/parser/diff.go b/parser/diff.go
index 0a481632..0229e2ff 100644
--- a/parser/diff.go
+++ b/parser/diff.go
@@ -6,7 +6,7 @@ import (
 	"strings"
 
 	"github.com/reviewdog/reviewdog/diff"
-	"github.com/reviewdog/reviewdog/filter"
+	"github.com/reviewdog/reviewdog/pathutil"
 	"github.com/reviewdog/reviewdog/proto/rdf"
 )
 
@@ -57,7 +57,7 @@ func (p *DiffParser) Parse(r io.Reader) ([]*rdf.Diagnostic, error) {
 	}
 	var diagnostics []*rdf.Diagnostic
 	for _, fdiff := range filediffs {
-		path := filter.NormalizeDiffPath(fdiff.PathNew, p.strip)
+		path := pathutil.NormalizeDiffPath(fdiff.PathNew, p.strip)
 		for _, hunk := range fdiff.Hunks {
 			lnum := hunk.StartLineOld - 1
 			prevState := diff.LineUnchanged
diff --git a/pathutil/pathutil.go b/pathutil/pathutil.go
new file mode 100644
index 00000000..a714660a
--- /dev/null
+++ b/pathutil/pathutil.go
@@ -0,0 +1,61 @@
+package pathutil
+
+import (
+	"path/filepath"
+	"strings"
+)
+
+// NormalizePath return normalized path with workdir and relative path to
+// project.
+func NormalizePath(path, workdir, projectRelPath string) string {
+	path = filepath.Clean(path)
+	if path == "." {
+		return ""
+	}
+	// Convert absolute path to relative path only if the path is in current
+	// directory.
+	if filepath.IsAbs(path) && workdir != "" && contains(path, workdir) {
+		relPath, err := filepath.Rel(workdir, path)
+		if err == nil {
+			path = relPath
+		}
+	}
+	if !filepath.IsAbs(path) && projectRelPath != "" {
+		path = filepath.Join(projectRelPath, path)
+	}
+	return filepath.ToSlash(path)
+}
+
+// NormalizeDiffPath return path normalized path from given path in diff with
+// strip.
+func NormalizeDiffPath(diffpath string, strip int) string {
+	if diffpath == "/dev/null" {
+		return ""
+	}
+	path := diffpath
+	if strip > 0 && !filepath.IsAbs(path) {
+		ps := splitPathList(path)
+		if len(ps) > strip {
+			path = filepath.Join(ps[strip:]...)
+		}
+	}
+	return filepath.ToSlash(filepath.Clean(path))
+}
+
+func contains(path, base string) bool {
+	ps := splitPathList(path)
+	bs := splitPathList(base)
+	if len(ps) < len(bs) {
+		return false
+	}
+	for i := range bs {
+		if bs[i] != ps[i] {
+			return false
+		}
+	}
+	return true
+}
+
+func splitPathList(path string) []string {
+	return strings.Split(filepath.ToSlash(path), "/")
+}


Test patch:
diff --git a/filter/filter_test.go b/filter/filter_test.go
index 5e4090e3..64042bea 100644
--- a/filter/filter_test.go
+++ b/filter/filter_test.go
@@ -8,6 +8,7 @@ import (
 	"google.golang.org/protobuf/testing/protocmp"
 
 	"github.com/reviewdog/reviewdog/diff"
+	"github.com/reviewdog/reviewdog/pathutil"
 	"github.com/reviewdog/reviewdog/proto/rdf"
 )
 
@@ -569,7 +570,7 @@ func TestFilterCheckByNoFilter(t *testing.T) {
 
 func findFileDiff(filediffs []*diff.FileDiff, path string, strip int) *diff.FileDiff {
 	for _, file := range filediffs {
-		if NormalizeDiffPath(file.PathNew, strip) == path {
+		if pathutil.NormalizeDiffPath(file.PathNew, strip) == path {
 			return file
 		}
 	}