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

Fixing skip stage #25

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,14 @@ git push --follow-tags

1. Gusto engineers may [go/deploy-committer-with-cpe-chef](https://go/deploy-committer-with-cpe-chef).

## Autofix
## Environment Specific Configuration

Committer can auto-fix everything by default. set `export COMMITTER_AUTO_FIX=true # or 1, T, t` in your
`~/.profile` or equivalent dotfile.

## Opting out of automatic staging

Committer will stage auto-corrected files by default. In order to always leave auto-corrected files unstaged for manual staging, set `export COMMITTER_SKIP_STAGE=1` in your `~/.bashrc` or equivalent.
Set these values in your `~/.profile` or equivalent env vars `export <flag>=<value>`

| Flag | Default | Expected | Description |
|---|---|---|---|
| COMMITTER_SKIP_STAGE | False | Truthy or Falsy | Committer will stage auto-corrected files by default. In order to always leave auto-corrected files unstaged for manual staging, set this value |
| COMMITTER_AUTO_FIX | False | Truthy or Falsy | Committer can auto-fix code formatting in staged files by default |

## Best-practices
### Purely Functional Pre-Commit Hooks
Expand Down
12 changes: 6 additions & 6 deletions core/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type TaskResult struct {
fixedOutput string
}

var shouldStage = (os.Getenv("COMMITTER_SKIP_STAGE") == "")
var shouldNotStage, _ = strconv.ParseBool(os.Getenv("COMMITTER_SKIP_STAGE"))
var autoFix, _ = strconv.ParseBool(os.Getenv("COMMITTER_AUTO_FIX"))

var changedFiles, _ = exec.Command("git", "diff", "--diff-filter=ACMRTUXB", "--cached", "--name-only").Output()
Expand Down Expand Up @@ -72,12 +72,12 @@ func (task Task) Execute(fix bool) TaskResult {

if fixedOutput != "" {
// If we have output, then we've corrected files
if shouldStage {
// Stage files by default automatically
task.stageRelevantFiles()
} else {
// Explicitly fail the pre-commit hook so the files can be staged manually
if shouldNotStage {
// Fail the pre-commit hook so the files can be staged manually
success = false
} else {
// Stage files automatically otherwise.
task.stageRelevantFiles()
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions core/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,9 @@ func TestExecuteFixFailureWithFixCommand(t *testing.T) {
}

func TestExecuteFixSuccessWithFixCommandShouldNotStage(t *testing.T) {
origShouldStage := shouldStage
shouldStage = false
defer func() { shouldStage = origShouldStage }()
origShouldStage := shouldNotStage
shouldNotStage = true
defer func() { shouldNotStage = origShouldStage }()

stubExecCommand(
[]byte(`Linted: app/one.rb
Expand Down