Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Require use of --force to fix without git #1052

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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