Skip to content
This repository was archived by the owner on Aug 7, 2025. It is now read-only.

Commit 722fd7f

Browse files
committed
Always run for changed files
1 parent 38f7219 commit 722fd7f

File tree

7 files changed

+34
-59
lines changed

7 files changed

+34
-59
lines changed

.buildkite/publish.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
VERSION=$(cat VERSION)
3+
VERSION=$(go run committer.go --version)
44
UPLOADED_PKG_FOUND=$(aws s3 --region 'us-west-2' ls 's3://vpc-access/' | grep committer-$VERSION)
55

66
if [ "$UPLOADED_PKG_FOUND" ]; then

VERSION

Lines changed: 0 additions & 1 deletion
This file was deleted.

committer.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,15 @@ import (
44
"flag"
55
"fmt"
66
"github.com/gusto/committer/core"
7-
"io/ioutil"
87
"os"
98
)
109

11-
func main() {
12-
content, _ := ioutil.ReadFile("VERSION")
13-
VERSION := string(content)
10+
const VERSION = "0.1.3"
1411

12+
func main() {
1513
version := flag.Bool("version", false, "Display version")
1614
help := flag.Bool("help", false, "Display usage")
1715
fix := flag.Bool("fix", false, "Run autocorrect for commands that support it")
18-
changed := flag.Bool("changed", false, "Run autocorrect for commands that support it")
1916
configPath := flag.String("config", "committer.yml", "Location of your config file")
2017

2118
flag.Parse()
@@ -27,16 +24,16 @@ func main() {
2724
}
2825

2926
if *version {
30-
fmt.Printf(VERSION)
27+
fmt.Printf(VERSION + "\n")
3128
return
3229
}
3330

3431
parsedConfig, err := core.NewConfigFromFile(*configPath)
3532
if err != nil {
36-
return
33+
panic(err)
3734
}
3835

39-
success := core.NewRunner(*parsedConfig, *fix, *changed).Run()
36+
success := core.NewRunner(*parsedConfig, *fix).Run()
4037

4138
if success {
4239
os.Exit(0)

configure.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash -ex
22

3-
VERSION="0.1.2"
3+
VERSION="0.1.3"
44
GIT_PRE_COMMIT_HOOK=".git/hooks/pre-commit"
55
COMMITTER_YML="committer.yml"
66
COMMITTER_LOCATION="/usr/local/bin/committer"

core/runner.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ type Runner struct {
77
resultChannel chan TaskResult
88
}
99

10-
func NewRunner(config Config, fix bool, changed bool) *Runner {
10+
func NewRunner(config Config, fix bool) *Runner {
1111
return &Runner{
1212
config: config,
1313
fix: fix,
14-
changed: changed,
1514
resultChannel: make(chan TaskResult),
1615
}
1716
}
@@ -21,7 +20,7 @@ func (this Runner) Run() bool {
2120

2221
for i := 0; i < len(this.config.Tasks); i += 1 {
2322
task := this.config.Tasks[i]
24-
if task.shouldRun(this.changed) {
23+
if task.shouldRun() {
2524
tasksToRun = append(tasksToRun, task)
2625
go this.processTask(task)
2726
}
@@ -36,5 +35,5 @@ func (this Runner) Run() bool {
3635
}
3736

3837
func (this Runner) processTask(task Task) {
39-
this.resultChannel <- task.Execute(this.changed, this.fix)
38+
this.resultChannel <- task.Execute(this.fix)
4039
}

core/task.go

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ var execCommand = func(command string, args ...string) ([]byte, error) {
4646
return exec.Command(command, args...).CombinedOutput()
4747
}
4848

49-
func (task Task) Execute(changed bool, fix bool) TaskResult {
49+
func (task Task) Execute(fix bool) TaskResult {
5050
// Generate command based on --fix / --changed
51-
command := task.prepareCommand(changed, fix)
51+
command := task.prepareCommand(fix)
5252

5353
// Run command
5454
output, err := execCommand(command[0], command[1:]...)
@@ -97,7 +97,7 @@ func (task Task) prepareFixedOutput(outputStr string) string {
9797
return strings.Join(fixedOutputList, "\n")
9898
}
9999

100-
func (task Task) prepareCommand(changed bool, fix bool) []string {
100+
func (task Task) prepareCommand(fix bool) []string {
101101
// Use the FixCommand or regular Command depending on the flag passed to CLI
102102
var cmdStr string
103103
if fix && task.Fix.Command != "" {
@@ -107,11 +107,8 @@ func (task Task) prepareCommand(changed bool, fix bool) []string {
107107
}
108108

109109
// Feed in changed files if we are running with --changed
110-
111-
if changed {
112-
relevantChangedFilesList := task.relevantChangedFiles(changedFilesList)
113-
cmdStr += " -- " + strings.Join(relevantChangedFilesList, " ")
114-
}
110+
relevantChangedFilesList := task.relevantChangedFiles(changedFilesList)
111+
cmdStr += " " + strings.Join(relevantChangedFilesList, " ")
115112

116113
return strings.Split(cmdStr, " ")
117114
}
@@ -125,24 +122,16 @@ func (task Task) stageRelevantFiles() {
125122
}
126123
}
127124

128-
func (task Task) shouldRun(changed bool) bool {
129-
// Always run all tasks if we aren't just looking at changed files
130-
if !changed {
131-
return true
132-
}
133-
134-
if task.Fix.Command != "" {
135-
for _, file := range changedFilesList {
136-
match, err := regexp.MatchString(task.Files, file)
125+
func (task Task) shouldRun() bool {
126+
for _, file := range changedFilesList {
127+
match, err := regexp.MatchString(task.Files, file)
137128

138-
if err != nil {
139-
panic(err)
140-
}
141-
if match {
142-
return true
143-
}
129+
if err != nil {
130+
panic(err)
131+
}
132+
if match {
133+
return true
144134
}
145135
}
146-
147136
return false
148137
}

core/task_test.go

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,13 @@ import (
77
"testing"
88
)
99

10-
func TestShouldRunNotChanged(t *testing.T) {
11-
task := Task{
12-
Name: "task",
13-
Command: "run-task",
14-
}
15-
16-
assert.True(t, task.shouldRun(false), "It runs when not in changed mode")
17-
}
18-
1910
func TestShouldRunNotChangedNoFix(t *testing.T) {
2011
task := Task{
2112
Name: "task",
2213
Command: "run-task",
2314
}
2415

25-
assert.False(t, task.shouldRun(true), "It does not run when in changed mode with no fix command")
16+
assert.False(t, task.shouldRun(), "It does not run when in changed mode with no fix command")
2617
}
2718

2819
func TestShouldRunNotChangedWithFix(t *testing.T) {
@@ -32,7 +23,7 @@ func TestShouldRunNotChangedWithFix(t *testing.T) {
3223
}
3324
task.Fix.Command = "run-fix"
3425

35-
assert.True(t, task.shouldRun(true), "It does not run when in changed mode with no fix command")
26+
assert.True(t, task.shouldRun(), "It does not run when in changed mode with no fix command")
3627
}
3728

3829
func TestPrepareCommandNoChangeNoFix(t *testing.T) {
@@ -43,7 +34,7 @@ func TestPrepareCommandNoChangeNoFix(t *testing.T) {
4334
assert.Equal(
4435
t,
4536
[]string{"run-task"},
46-
task.prepareCommand(false, true),
37+
task.prepareCommand(true),
4738
"It runs the fix command when fix is true",
4839
)
4940
}
@@ -55,7 +46,7 @@ func TestPrepareCommandNoChangeFix(t *testing.T) {
5546
assert.Equal(
5647
t,
5748
[]string{"run-fix"},
58-
task.prepareCommand(false, true),
49+
task.prepareCommand(true),
5950
"It runs the fix command when fix is true",
6051
)
6152
}
@@ -72,7 +63,7 @@ func TestPrepareCommandWithChanged(t *testing.T) {
7263
assert.Equal(
7364
t,
7465
[]string{"run-task", "--", "three.txt"},
75-
task.prepareCommand(true, false),
66+
task.prepareCommand(false),
7667
"It correctly passes only the relevant files",
7768
)
7869
}
@@ -145,7 +136,7 @@ func TestExecuteSuccess(t *testing.T) {
145136
Command: "run-task",
146137
}
147138

148-
result := task.Execute(false, false)
139+
result := task.Execute(false)
149140
assert.True(t, result.success, "The result is successful")
150141
assert.Equal(t, result.task, task, "It attaches the task")
151142
assert.Equal(t, result.output, "Output!", "It attaches the task")
@@ -161,7 +152,7 @@ func TestExecuteFailure(t *testing.T) {
161152
Command: "run-task",
162153
}
163154

164-
result := task.Execute(false, false)
155+
result := task.Execute(false)
165156
assert.False(t, result.success, "The result is failed")
166157
assert.Equal(t, result.task, task, "It attaches the task")
167158
assert.Equal(t, result.output, "Output!", "It attaches the task")
@@ -177,7 +168,7 @@ func TestExecuteFixSuccessNoFixCommand(t *testing.T) {
177168
Command: "run-task",
178169
}
179170

180-
result := task.Execute(false, true)
171+
result := task.Execute(true)
181172
assert.True(t, result.success, "The result is successful")
182173
assert.Equal(t, result.task, task, "It attaches the task")
183174
assert.Equal(t, result.output, "Output!", "It does not grep through the output")
@@ -201,7 +192,7 @@ Linted: app/three.rb
201192
task.Fix.Command = "run-fix"
202193
task.Fix.Output = "Fixed:"
203194

204-
result := task.Execute(false, true)
195+
result := task.Execute(true)
205196
assert.True(t, result.success, "The result is successful")
206197
assert.Equal(t, result.task, task, "It attaches the task")
207198
assert.Equal(t, result.output, "Linted: app/one.rb\nFixed: app/two.rb\nLinted: app/three.rb\n", "It attaches the entire output")
@@ -222,7 +213,7 @@ func TestExecuteFixFailureWithFixCommand(t *testing.T) {
222213
task.Fix.Command = "run-fix"
223214
task.Fix.Output = "Fixed:"
224215

225-
result := task.Execute(false, true)
216+
result := task.Execute(true)
226217
assert.False(t, result.success, "The result is successful")
227218
assert.Equal(t, result.task, task, "It attaches the task")
228219
assert.Equal(t, result.output, "Failed!", "It attaches the entire output")
@@ -250,7 +241,7 @@ Linted: app/three.rb
250241
task.Fix.Command = "run-fix"
251242
task.Fix.Output = "Fixed:"
252243

253-
result := task.Execute(false, true)
244+
result := task.Execute(true)
254245
assert.False(t, result.success, "The result is marked unsuccessful so changes can be staged")
255246
assert.Equal(t, result.task, task, "It attaches the task")
256247
assert.Equal(t, result.output, "Linted: app/one.rb\nFixed: app/two.rb\nLinted: app/three.rb\n", "It attaches the entire output")

0 commit comments

Comments
 (0)