Skip to content

Commit

Permalink
add sniff command and remove support for commit alias
Browse files Browse the repository at this point in the history
  • Loading branch information
ezekg committed Jan 5, 2017
1 parent be71bf2 commit 5f7ed60
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 64 deletions.
23 changes: 14 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ To install Hound, please use `go get`. If you don't have Go installed, [get it h
go get github.com/ezekg/git-hound
```

**Alias `git commit` inside `~/.bash(rc|_profile)`:** _(optional)_
```bash
alias git='_() { if [[ "$1" == "commit" ]]; then git-hound "$@"; else git "$@"; fi }; _'
```

## Compiling
To compile for your operating system, simply run the following from the root of the project directory:
```bash
Expand All @@ -32,16 +27,26 @@ goxc -pv={VERSION} -d=releases/
```

## Usage

#### Commit
```bash
git hound commit ...
git commit ... # When using the optional alias above
# Scan changes since last commit and pass to git-commit when clean
git hound commit …
```

#### Sniff
```bash
# Scan changes since last commit
git hound sniff HEAD

# Scan entire history of repository
git hound sniff
```

## Option flags
These flags should be included inside of the `git` alias, if used.

| Flag | Type | Default | Usage |
| :------------- | :----- | :-------------- | :----------------------------------------- |
|:---------------|:-------|:----------------|:-------------------------------------------|
| `-no-color` | bool | `false` | Disable color output |
| `-config=file` | string | `.githound.yml` | Hound config file |
| `-bin=file` | string | `git` | Executable binary to use for `git` command |
Expand Down
137 changes: 82 additions & 55 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ package main
import (
"flag"
"fmt"
"github.com/ezekg/git-hound/Godeps/_workspace/src/github.com/fatih/color"
"github.com/ezekg/git-hound/Godeps/_workspace/src/sourcegraph.com/sourcegraph/go-diff/diff"
"github.com/fatih/color"
"os"
"sourcegraph.com/sourcegraph/go-diff/diff"
)

var (
version = "0.5.3"
version = "0.6.0"
showVersion = flag.Bool("v", false, "Show version")
noColor = flag.Bool("no-color", false, "Disable color output")
config = flag.String("config", ".githound.yml", "Hound config file")
Expand Down Expand Up @@ -42,65 +42,92 @@ func main() {
hound := &Hound{config: *config}
git := &Command{bin: *bin}

if ok := hound.New(); ok {
out, _ := git.Exec("diff", "-U0", "--staged")
fileDiffs, err := diff.ParseMultiFileDiff([]byte(out))
if err != nil {
color.Red(fmt.Sprintf("%s\n", err))
os.Exit(1)
if ok := hound.New(); !ok {
color.Red("No config file detected")
os.Exit(1)
}

var (
runnable bool
out string
)

switch flag.Arg(0) {
case "commit":
out, _ = git.Exec("diff", "-U0", "--staged")
runnable = true
case "sniff":
commit := flag.Arg(1)
if commit == "" {
// NOTE: This let's us get a diff containing the entire history of the repo
// by utilizing a magic commit hash. In reality, it's not magical,
// it's simply the result of sha1("tree 0\0").
commit = "4b825dc642cb6eb9a060e54bf8d69288fbee4904"
}
out, _ = git.Exec("diff", commit, "--staged")
default:
color.Red("Usage:\n git-hound commit [...]\n git-hound sniff [commit]")
os.Exit(1)
}

severeSmellCount := 0
hunkCount := 0

smells := make(chan smell)
done := make(chan bool)

for _, fileDiff := range fileDiffs {
fileName := fileDiff.NewName
hunks := fileDiff.GetHunks()

for _, hunk := range hunks {
go func(hunk *diff.Hunk) {
defer func() {
if r := recover(); r != nil {
color.Red(fmt.Sprintf("%s\n", r))
os.Exit(1)
}
}()
hound.Sniff(fileName, hunk, smells, done)
}(hunk)
hunkCount++
}
fileDiffs, err := diff.ParseMultiFileDiff([]byte(out))
if err != nil {
color.Red(fmt.Sprintf("%s\n", err))
os.Exit(1)
}

severeSmellCount := 0
hunkCount := 0

smells := make(chan smell)
done := make(chan bool)

for _, fileDiff := range fileDiffs {
fileName := fileDiff.NewName
hunks := fileDiff.GetHunks()

for _, hunk := range hunks {
go func(hunk *diff.Hunk) {
defer func() {
if r := recover(); r != nil {
color.Red(fmt.Sprintf("%s\n", r))
os.Exit(1)
}
}()
hound.Sniff(fileName, hunk, smells, done)
}(hunk)
hunkCount++
}
}

for c := 0; c < hunkCount; {
select {
case s := <-smells:
if s.severity > 1 {
severeSmellCount++
}

switch s.severity {
case 1:
color.Yellow(fmt.Sprintf("warning: %s\n", s.String()))
case 2:
color.Red(fmt.Sprintf("failure: %s\n", s.String()))
default:
color.Red(fmt.Sprintf("error: unknown severity given - %d\n", s.severity))
}
case <-done:
c++
for c := 0; c < hunkCount; {
select {
case s := <-smells:
if s.severity > 1 {
severeSmellCount++
}
}

if severeSmellCount > 0 {
fmt.Printf("%d severe smell(s) detected - please fix them before you can commit\n", severeSmellCount)
os.Exit(1)
switch s.severity {
case 1:
color.Yellow(fmt.Sprintf("warning: %s\n", s.String()))
case 2:
color.Red(fmt.Sprintf("failure: %s\n", s.String()))
default:
color.Red(fmt.Sprintf("error: unknown severity given - %d\n", s.severity))
}
case <-done:
c++
}
}

out, code := git.Exec(flag.Args()...)
fmt.Print(out)
os.Exit(code)
if severeSmellCount > 0 {
fmt.Printf("%d severe smell(s) detected - please fix them before you can commit\n", severeSmellCount)
os.Exit(1)
}

if runnable {
out, code := git.Exec(flag.Args()...)
fmt.Print(out)
os.Exit(code)
}
}

0 comments on commit 5f7ed60

Please sign in to comment.