Skip to content

Commit

Permalink
apidiff: ignore internal packages
Browse files Browse the repository at this point in the history
The feature is added to apidiff which allows users to ignore internal package comparisons.

Fixes golang/go#52864.
  • Loading branch information
gh73962 committed May 16, 2022
1 parent 39d4317 commit 9b59beb
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion cmd/apidiff/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"go/token"
"go/types"
"os"
"strings"

"golang.org/x/exp/apidiff"
"golang.org/x/tools/go/gcexportdata"
Expand All @@ -17,6 +18,7 @@ import (
var (
exportDataOutfile = flag.String("w", "", "file for export data")
incompatibleOnly = flag.Bool("incompatible", false, "display only incompatible changes")
allowInternal = flag.Bool("allow-internal", false, "allow apidiff to compare internal packages")
)

func main() {
Expand Down Expand Up @@ -54,7 +56,12 @@ func main() {
}
oldpkg := mustLoadOrRead(flag.Arg(0))
newpkg := mustLoadOrRead(flag.Arg(1))

if !*allowInternal {
if isInternalPackage(oldpkg.Path()) && isInternalPackage(newpkg.Path()) {
fmt.Fprintf(os.Stderr, "Ignoring internal package %s\n", oldpkg.Path())
os.Exit(0)
}
}
report := apidiff.Changes(oldpkg, newpkg)
var err error
if *incompatibleOnly {
Expand Down Expand Up @@ -140,3 +147,15 @@ func die(format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, format+"\n", args...)
os.Exit(1)
}

func isInternalPackage(pkgPath string) bool {
switch {
case strings.HasSuffix(pkgPath, "/internal"):
return true
case strings.Contains(pkgPath, "/internal/"):
return true
case pkgPath == "internal", strings.HasPrefix(pkgPath, "internal/"):
return true
}
return false
}

0 comments on commit 9b59beb

Please sign in to comment.