Skip to content

Commit

Permalink
fix: Require use of --force to fix without git (#1052)
Browse files Browse the repository at this point in the history
This also skips the unchanged check when working within a git repo.

Signed-off-by: Charlie Egan <charlie@styra.com>
  • Loading branch information
charlieegan3 authored Sep 4, 2024
1 parent 7a4811b commit ced7c70
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
20 changes: 14 additions & 6 deletions cmd/fix.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type fixCommandParams struct {
timeout time.Duration
dryRun bool
verbose bool
force bool
}

func (p *fixCommandParams) getConfigFile() string {
Expand Down Expand Up @@ -146,6 +147,9 @@ The linter rules with automatic fixes available are currently:
fixCommand.Flags().BoolVarP(&params.verbose, "verbose", "", false,
"show the full changes applied in the console")

fixCommand.Flags().BoolVarP(&params.force, "force", "", false,
"allow fixing of files that have uncommitted changes in git or when git is not being used")

addPprofFlag(fixCommand.Flags())

RootCommand.AddCommand(fixCommand)
Expand Down Expand Up @@ -314,14 +318,18 @@ func fix(args []string, params *fixCommandParams) error {
return fmt.Errorf("failed to fix: %w", err)
}

gitRepo, err := git.FindGitRepo(args...)
if err != nil {
return fmt.Errorf("failed to establish git repo: %w", err)
}

if gitRepo == "" && !params.force {
return errors.New("no git repo found to support undo, use --force to override")
}

// if the fixer is being run in a git repo, we must not fix files that have
// been changed.
if !params.dryRun {
gitRepo, err := git.FindGitRepo(args...)
if err != nil {
return fmt.Errorf("failed to establish git repo: %w", err)
}

if !params.dryRun && !params.force {
changedFiles := make(map[string]struct{})

if gitRepo != "" {
Expand Down
3 changes: 2 additions & 1 deletion e2e/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,8 @@ test_allow {
mustWriteToFile(t, filepath.Join(td, file), string(content))
}

err := regal(&stdout, &stderr)("fix", filepath.Join(td, "foo"), filepath.Join(td, "bar"))
// --force is required to make the changes when there is no git repo
err := regal(&stdout, &stderr)("fix", "--force", filepath.Join(td, "foo"), filepath.Join(td, "bar"))

// 0 exit status is expected as all violations should have been fixed
expectExitCode(t, err, 0, &stdout, &stderr)
Expand Down

0 comments on commit ced7c70

Please sign in to comment.