apidiff: ignore internal packages

A feature is added to apidiff that allows users to ignore internal package comparisons.

Fixes golang/go#52864.

Change-Id: Ic390e198ef81901a9aed4212bc6f49e73ab78336
GitHub-Last-Rev: 9b59beb48848a822e94e1a39f98dba2a1d2af230
GitHub-Pull-Request: golang/exp#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>
diff --git a/cmd/apidiff/main.go b/cmd/apidiff/main.go
index a5446b7..61677fc 100644
--- a/cmd/apidiff/main.go
+++ b/cmd/apidiff/main.go
@@ -8,6 +8,7 @@
 	"go/token"
 	"go/types"
 	"os"
+	"strings"
 
 	"golang.org/x/exp/apidiff"
 	"golang.org/x/tools/go/gcexportdata"
@@ -17,6 +18,7 @@
 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() {
@@ -54,7 +56,12 @@
 		}
 		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 {
@@ -140,3 +147,15 @@
 	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
+}