Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ Print compatible API changes (default: `true`)

Path to root of git repository to compare (default: current working directory)

#### `relative-path`

Relative path to compare against (default: empty)

### Outputs

#### `semver-type`
Expand Down Expand Up @@ -83,10 +87,11 @@ Usage:
go-apidiff <oldCommit> [newCommit] [flags]

Flags:
--compare-imports Compare exported API differences of the imports in the repo.
-h, --help help for go-apidiff
--print-compatible Print compatible API changes
--repo-path string Path to root of git repository to compare (default "/home/myuser/myproject")
--compare-imports Compare exported API differences of the imports in the repo.
-h, --help help for go-apidiff
--print-compatible Print compatible API changes
--relative-path string Relative path to compare against
--repo-path string Path to root of git repository to compare (default "/home/myuser/myproject")
```

## Example output
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ and HEAD is used for newCommit."`,
}

cmd.Flags().StringVar(&opts.RepoPath, "repo-path", cwd, "Path to root of git repository to compare")
cmd.Flags().StringVar(&opts.RelativePath, "relative-path", "", "Relative path to compare against")
cmd.Flags().BoolVar(&opts.CompareImports, "compare-imports", false, "Compare exported API differences of the imports in the repo. ")
cmd.Flags().BoolVar(&printCompatible, "print-compatible", false, "Print compatible API changes")

Expand Down
10 changes: 7 additions & 3 deletions pkg/diff/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (

type Options struct {
RepoPath string
RelativePath string
OldCommit string
NewCommit string
CompareImports bool
Expand Down Expand Up @@ -97,12 +98,12 @@ func Run(opts Options) (*Diff, error) {
}
}()

selfOld, importsOld, err := getPackages(*wt, *oldHash)
selfOld, importsOld, err := getPackages(*wt, *oldHash, opts.RelativePath)
if err != nil {
return nil, fmt.Errorf("failed to get packages from old commit %q (%s): %w", opts.OldCommit, oldHash, err)
}

selfNew, importsNew, err := getPackages(*wt, *newHash)
selfNew, importsNew, err := getPackages(*wt, *newHash, opts.RelativePath)
if err != nil {
return nil, fmt.Errorf("failed to get packages from new commit %q (%s): %w", opts.NewCommit, newHash, err)
}
Expand Down Expand Up @@ -192,7 +193,7 @@ func getHashes(repo *git.Repository, oldRev, newRev plumbing.Revision) (*plumbin
return oldCommitHash, newCommitHash, nil
}

func getPackages(wt git.Worktree, hash plumbing.Hash) (map[string]*packages.Package, map[string]*packages.Package, error) {
func getPackages(wt git.Worktree, hash plumbing.Hash, relativePath string) (map[string]*packages.Package, map[string]*packages.Package, error) {
if err := wt.Checkout(&git.CheckoutOptions{Hash: hash, Force: true}); err != nil {
return nil, nil, err
}
Expand All @@ -213,6 +214,9 @@ func getPackages(wt git.Worktree, hash plumbing.Hash) (map[string]*packages.Pack
Tests: false,
BuildFlags: []string{goFlags},
}
if relativePath != "" {
cfg.Dir = filepath.Join(wt.Filesystem.Root(), relativePath)
}
pkgs, err := packages.Load(&cfg, "./...")
if err != nil {
return nil, nil, err
Expand Down