synapsecns / sanguine

Synapse Monorepo
MIT License
37 stars 30 forks source link

[Git Changes Action] Sort changed_modules by diff size #1708

Open trajan0x opened 10 months ago

trajan0x commented 10 months ago

To allow faster operation on modules we care about when using max-paralell

greptile-apps[bot] commented 4 months ago

To sort changed_modules by diff size, follow these steps:

  1. Modify detector.go:

    • Update DetectChangedModules to return a map of module paths to their diff sizes.
      func DetectChangedModules(repoPath string, ct tree.Tree, includeDeps bool) (modules map[string]int, err error) {
      modules = make(map[string]int)
      // existing code...
      for _, module := range parsedWorkFile.Use {
         diffSize := 0
         if ct.HasPath(module.Path) {
             diffSize = ct.GetDiffSize(module.Path) // hypothetical function to get diff size
         }
         if includeDeps {
             deps := depGraph[module.Path]
             for _, dep := range deps {
                 if ct.HasPath(dep) {
                     diffSize += ct.GetDiffSize(dep)
                 }
             }
         }
         modules[module.Path] = diffSize
      }
      return modules, nil
      }
  2. Modify main.go:

    • Update outputModuleChanges to sort modules by diff size.
      func outputModuleChanges(workingDirectory string, ct tree.Tree, includeDeps bool, dependencyLevelResolution string) (changedJSON string, unchangedJSON string, err error) {
      var modules map[string]int
      // existing code...
      var changedModules, unchangedModules []string
      for module, diffSize := range modules {
         modName := strings.TrimPrefix(module, "./")
         if diffSize > 0 {
             changedModules = append(changedModules, modName)
         } else {
             unchangedModules = append(unchangedModules, modName)
         }
      }
      sort.Slice(changedModules, func(i, j int) bool {
         return modules[changedModules[i]] > modules[changedModules[j]]
      })
      // existing code...
      return string(marshalledChanged), string(marshalledUnchanged), nil
      }
  3. Update tree.Tree:

    • Implement GetDiffSize to return the size of the diff for a given path.
      func (t *Tree) GetDiffSize(path string) int {
      // logic to calculate and return diff size
      }

References

/contrib/git-changes-action/main.go /contrib/git-changes-action/detector/module/detector.go /contrib/git-changes-action

Ask Greptile