diff --git a/cmd/community/community.go b/cmd/community/community.go index 46c350e1c3..81b4f01702 100644 --- a/cmd/community/community.go +++ b/cmd/community/community.go @@ -7,7 +7,6 @@ import ( func init() { CommunityCmd.AddCommand(ListIconsCmd) CommunityCmd.AddCommand(LoadAppCmd) - CommunityCmd.AddCommand(TargetDeterminatorCmd) CommunityCmd.AddCommand(ValidateIconsCmd) CommunityCmd.AddCommand(ValidateManifestCmd) } diff --git a/cmd/community/targetdeterminator.go b/cmd/community/targetdeterminator.go deleted file mode 100644 index cd3a2d24f2..0000000000 --- a/cmd/community/targetdeterminator.go +++ /dev/null @@ -1,92 +0,0 @@ -package community - -import ( - "fmt" - "os" - "path/filepath" - "strings" - - "github.com/spf13/cobra" - "tidbyt.dev/pixlet/tools/repo" -) - -var ( - oldCommit string - newCommit string -) - -func init() { - TargetDeterminatorCmd.Flags().StringVarP(&oldCommit, "old", "o", "", "The old commit to compare against") - TargetDeterminatorCmd.MarkFlagRequired("old") - TargetDeterminatorCmd.Flags().StringVarP(&newCommit, "new", "n", "", "The new commit to compare against") - TargetDeterminatorCmd.MarkFlagRequired("new") -} - -var TargetDeterminatorCmd = &cobra.Command{ - Use: "target-determinator", - Short: "Determines what files have changed between old and new commit", - Example: ` pixlet community target-determinator \ - --old 4d69e9bbf181434229a98e87909a619634072930 \ - --new 2fc2a1fcfa48bbb0b836084c1b1e259322c4e133`, - Long: `This command determines what files have changed between two commits -so that we can limit the build in the community repo to only the files that -have changed.`, - RunE: determineTargets, -} - -func determineTargets(cmd *cobra.Command, args []string) error { - cwd, err := os.Getwd() - if err != nil { - return fmt.Errorf("could not determine targets, something went wrong with the local filesystem: %w", err) - } - - changedFiles, err := repo.DetermineChanges(cwd, oldCommit, newCommit) - if err != nil { - return fmt.Errorf("could not determine targets: %w", err) - } - - changedApps := map[string]bool{} - for _, f := range changedFiles { - dir := filepath.Dir(f) + string(os.PathSeparator) - - // We only care about things in apps/{{ app package }}/ changing and - // nothing else. Skip any file changes that are not in that structure. - parts := strings.Split(f, string(os.PathSeparator)) - if len(parts) < 3 { - continue - } - - // If the filepath does not start with apps, we also don't care about - // it. - if !strings.HasPrefix(dir, "apps") { - continue - } - - // If the directory no longer exists, we don't care about it. This would - // happen if someone deleted an app in a PR. - if !dirExists(dir) { - continue - } - - changedApps[dir] = true - } - - for app := range changedApps { - fmt.Println(app) - } - - return nil -} - -func dirExists(dir string) bool { - _, err := os.Stat(dir) - if os.IsNotExist(err) { - return false - } - - if err != nil { - return false - } - - return true -} diff --git a/tools/repo/repo.go b/tools/repo/repo.go index abd48a4bbb..f79e49c1a6 100644 --- a/tools/repo/repo.go +++ b/tools/repo/repo.go @@ -5,8 +5,6 @@ import ( "github.com/gitsight/go-vcsurl" "github.com/go-git/go-git/v5" - "github.com/go-git/go-git/v5/plumbing" - "github.com/go-git/go-git/v5/plumbing/object" ) // IsInRepo determines if the provided directory is in the provided git @@ -60,65 +58,3 @@ func RepoRoot(dir string) (string, error) { return worktree.Filesystem.Root(), nil } - -func DetermineChanges(dir string, oldCommit string, newCommit string) ([]string, error) { - // Load the repo. - repo, err := git.PlainOpenWithOptions(dir, &git.PlainOpenOptions{ - DetectDotGit: true, - }) - if err != nil { - return nil, fmt.Errorf("couldn't instantiate repo: %w", err) - } - - // Do a bunch of plumbing to get these commits usable for go-git - oldHash, err := repo.ResolveRevision(plumbing.Revision(oldCommit)) - if err != nil { - return nil, fmt.Errorf("couldn't parse old commit: %w", err) - } - newHash, err := repo.ResolveRevision(plumbing.Revision(newCommit)) - if err != nil { - return nil, fmt.Errorf("couldn't parse new commit: %w", err) - } - old, err := repo.CommitObject(*oldHash) - if err != nil { - return nil, fmt.Errorf("couldn't find old commit: %w", err) - } - new, err := repo.CommitObject(*newHash) - if err != nil { - return nil, fmt.Errorf("couldn't find new commit: %w", err) - } - oldTree, err := old.Tree() - if err != nil { - return nil, fmt.Errorf("couldn't generate tree for old commit: %w", err) - } - newTree, err := new.Tree() - if err != nil { - return nil, fmt.Errorf("couldn't generate tree for new commit: %w", err) - } - - // Diff the two trees to determine what changed. - changes, err := oldTree.Diff(newTree) - if err != nil { - return nil, fmt.Errorf("couldn't get changes between commits: %w", err) - } - - // Create a unique list of changed files. - changedFiles := []string{} - for _, change := range changes { - changedFiles = append(changedFiles, getChangeName(change)) - } - - return changedFiles, nil -} - -func getChangeName(change *object.Change) string { - var empty = object.ChangeEntry{} - - // Use the To field for Inserts and Modifications. - if change.To != empty { - return change.To.Name - } - - // The From field for Deletes. - return change.From.Name -}