Skip to content

Commit

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

Fixes golang/go#52864.

Change-Id: Ic390e198ef81901a9aed4212bc6f49e73ab78336
GitHub-Last-Rev: 9b59beb
GitHub-Pull-Request: #32
Reviewed-on: https://go-review.googlesource.com/c/exp/+/406534
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Jamal Carvalho <jamal@golang.org>
  • Loading branch information
gh73962 authored and jba committed May 16, 2022
1 parent 39d4317 commit 24438e5
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion cmd/apidiff/main.go
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 24438e5

Please sign in to comment.