Skip to content

Commit

Permalink
Run tasks only on folders with changes
Browse files Browse the repository at this point in the history
  • Loading branch information
lewislbr committed Dec 5, 2021
1 parent 38e4d69 commit 642d35f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
8 changes: 5 additions & 3 deletions README.md
Expand Up @@ -4,9 +4,11 @@ Ready is a program to run tasks before a commit using a [pre-commit git hook](ht

For example, you can automatically run formatting, linting, and testing when running `git commit`, so you are assured every commit is up to the standards, issues are spotted early, and to avoid any CI pipeline failures down the road.

For now, tasks are run on all the repository files, not just the staged ones.
Tasks are run on the repository folder that contains files with changes, including the root. Monorepos are supported by using the `directory` option on the configuration file, where tasks will be run only on subfolders with changes.

At any time, tasks can be run without committing by running `ready`.
At any time, tasks can be run without committing by running `ready`, or `ready -all` to run all tasks.

If there are no changes, no tasks will be run.

Additionally, to commit without running any task, the [`-n/--no-verify` git commit flag](https://git-scm.com/docs/git-commit#Documentation/git-commit.txt--n) can be used.

Expand Down Expand Up @@ -44,7 +46,7 @@ tasks:
command: golangci-lint run
```

By default, commands will be run in the root directory, but they can be scoped to nested directories:
By default, commands will be run in the root directory, but they can be scoped to nested directories with the `directory` option:

```yaml
tasks:
Expand Down
49 changes: 42 additions & 7 deletions ready.go
Expand Up @@ -2,6 +2,7 @@ package main

import (
"errors"
"flag"
"fmt"
"log"
"os"
Expand Down Expand Up @@ -37,17 +38,43 @@ func main() {
os.Exit(0)
}

all := flag.Bool("all", false, "Run all tasks without commit")

flag.Parse()

cfg, err := newConfig().withYAML()
if err != nil {
log.Fatalf("Failed to get config: %v\n", err)
}

total := len(cfg.Tasks)
successes := 0
failures := 0
start := time.Now()

for i, t := range cfg.Tasks {
fmt.Printf("⏳ Running task %d of %d: %q... ", i+1, total, t.Name)
for _, t := range cfg.Tasks {
if !*all {
dirs, err := exec.Command("git", "diff", "--dirstat=files,0", "HEAD").CombinedOutput()
if err != nil {
log.Fatalf("Error determining folders with changes: %v\n", err)
}

files, err := exec.Command("git", "diff", "--name-only", "HEAD").CombinedOutput()
if err != nil {
log.Fatalf("Error determining files with changes: %v\n", err)
}

if t.Directory == "" {
if len(files) == 0 {
continue
}
} else {
if len(dirs) == 0 || !strings.Contains(string(dirs), t.Directory) {
continue
}
}
}

fmt.Printf("Running task %s... ⏳ ", t.Name)

output, err := runTask(t)
if err != nil {
Expand All @@ -58,24 +85,32 @@ func main() {
continue
}

successes++

if output == "" {
fmt.Printf("Success ✅\n\n")
} else {
fmt.Printf("Success ✅\n\n%v\n", output)
}
}

if successes == 0 && failures == 0 {
fmt.Println("Nothing to do 💤")

os.Exit(1)
}

if failures > 0 {
if failures == 1 {
fmt.Printf("Got a failure ⚠️ Please fix it and commit again\n\n")
fmt.Printf("Got 1 failure. Please fix it and try again ⚠️ \n\n")
} else {
fmt.Printf("Got some failures ⚠️ Please fix them and commit again\n\n")
fmt.Printf("Got %d failures. Please fix them and try again ⚠️ \n\n", failures)
}

os.Exit(1)
}

fmt.Printf("All tasks completed successfully in %v ✨\n\n", time.Since(start).Round(time.Millisecond))
fmt.Printf("%d tasks completed successfully in %v ✨\n\n", successes, time.Since(start).Round(time.Millisecond))
}

func installHook() error {
Expand Down Expand Up @@ -109,7 +144,7 @@ fi
latest_state=$(git diff --name-only)
if [[ $latest_state != $initial_state ]]; then
echo "Some files have been modified by the hook. Please handle them and commit again"
echo "Some files have been modified by the hook. Please handle them and commit again 🔧"
exit 1
fi
Expand Down

0 comments on commit 642d35f

Please sign in to comment.