Skip to content

Commit

Permalink
pkg/diff/run.go: reset worktree after loading packages (#2)
Browse files Browse the repository at this point in the history
After loading Go packages, the go.mod and go.sum files are sometimes modified.
This causes subsequent checkouts of other commits to fail, which results in
the command failing to produce API differences.

This commit performs a hard reset on the worktree after the packages have been
loaded so that subsequent checkouts succeed. This should be safe because
go-apidiff checks to ensure that the tree is clean when the program starts.
  • Loading branch information
joelanford committed Jun 11, 2020
1 parent 7ccff33 commit 1ae45eb
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pkg/diff/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,21 @@ func getPackages(wt git.Worktree, hash plumbing.Hash) (map[string]*packages.Pack
}
}
}

// Reset the worktree. Sometimes loading the packages can cause the
// worktree to become dirty. It seems like this occurs because package
// loading can change go.mod and go.sum.
//
// TODO(joelanford): If go-git starts to support checking out of specific
// files we can update this to be less aggressive and only checkout
// go.mod and go.sum instead of resetting the entire tree.
if err := wt.Reset(&git.ResetOptions{
Mode: git.HardReset,
Commit: hash,
}); err != nil {
return nil, nil, fmt.Errorf("failed to hard reset to %v: %w", hash, err)
}

return selfPkgs, importPkgs, nil
}

Expand Down

0 comments on commit 1ae45eb

Please sign in to comment.