Skip to content

Commit

Permalink
cmd/fix: Address relative path root matching (#1093)
Browse files Browse the repository at this point in the history
Signed-off-by: Charlie Egan <[email protected]>
  • Loading branch information
charlieegan3 authored Sep 10, 2024
1 parent 52f3377 commit 6f9b32f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
45 changes: 26 additions & 19 deletions cmd/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,23 +282,6 @@ func fix(args []string, params *fixCommandParams) error {
ignore = params.ignoreFiles.v
}

// create a list of absolute paths, these will be used for the file from
// this point in order to be able to use the roots for format reporting.
absArgs := make([]string, len(args))

for i, arg := range args {
if filepath.IsAbs(arg) {
absArgs[i] = arg

continue
}

absArgs[i], err = filepath.Abs(arg)
if err != nil {
return fmt.Errorf("failed to get absolute path for %s: %w", arg, err)
}
}

filtered, err := config.FilterIgnoredPaths(args, ignore, true, "")
if err != nil {
return fmt.Errorf("failed to filter ignored paths: %w", err)
Expand All @@ -308,7 +291,24 @@ func fix(args []string, params *fixCommandParams) error {
// TODO: Figure out why filtered returns duplicates in the first place
filtered = slices.Compact(filtered)

fileProvider, err := fileprovider.NewInMemoryFileProviderFromFS(filtered...)
// convert the filtered paths to absolute paths before fixing to
// support accurate root matching.
absFiltered := make([]string, len(filtered))

for i, f := range filtered {
if filepath.IsAbs(f) {
absFiltered[i] = f

continue
}

absFiltered[i], err = filepath.Abs(f)
if err != nil {
return fmt.Errorf("failed to get absolute path for %s: %w", f, err)
}
}

fileProvider, err := fileprovider.NewInMemoryFileProviderFromFS(absFiltered...)
if err != nil {
return fmt.Errorf("failed to create file provider: %w", err)
}
Expand Down Expand Up @@ -390,10 +390,17 @@ please run fix from a clean state to support the use of git checkout for undo`,
return fmt.Errorf("failed to delete file %s: %w", file, err)
}

err = util.DeleteEmptyDirs(filepath.Dir(file))
dirs, err := util.DirCleanUpPaths(file, roots)
if err != nil {
return fmt.Errorf("failed to delete empty directories: %w", err)
}

for _, dir := range dirs {
err := os.Remove(dir)
if err != nil {
return fmt.Errorf("failed to delete directory %s: %w", dir, err)
}
}
}

for _, file := range fileProvider.ModifiedFiles() {
Expand Down
2 changes: 0 additions & 2 deletions internal/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,6 @@ func DirCleanUpPaths(target string, preserve []string) ([]string, error) {

dirs = append(dirs, dir)

fmt.Fprintln(os.Stderr, "added", dir)

dir = filepath.Dir(dir)
}

Expand Down

0 comments on commit 6f9b32f

Please sign in to comment.