github.com/naive/revgrep@v0.0.0-20240331191128-ab485935cedc/cmd/revgrep/main.go (about)

     1  // Package main a CLI tool used to filter static analysis tools to only lines changed based on a commit reference.
     2  package main
     3  
     4  import (
     5  	"flag"
     6  	"fmt"
     7  	"os"
     8  
     9  	"github.com/naive/revgrep"
    10  )
    11  
    12  func main() {
    13  	flag.Usage = func() {
    14  		fmt.Println("Usage: revgrep [options] [from-rev] [to-rev]")
    15  		fmt.Println()
    16  		fmt.Println("from-rev filters issues to lines changed since (and including) this revision")
    17  		fmt.Println("  to-rev filters issues to lines changed since (and including) this revision, requires <from-rev>")
    18  		fmt.Println()
    19  		fmt.Println("If no revisions are given, and there are unstaged changes or untracked files, only those changes are shown")
    20  		fmt.Println("If no revisions are given, and there are no unstaged changes or untracked files, only changes in HEAD~ are shown")
    21  		fmt.Println("If from-rev is given and to-rev is not, only changes between from-rev and HEAD are shown.")
    22  		fmt.Println()
    23  		flag.PrintDefaults()
    24  	}
    25  
    26  	debug := flag.Bool("d", false, "Show debug output")
    27  	regexp := flag.String("regexp", "", "Regexp to match path, line number, optional column number, and message")
    28  	flag.Parse()
    29  
    30  	checker := revgrep.Checker{
    31  		RevisionFrom: flag.Arg(0),
    32  		RevisionTo:   flag.Arg(1),
    33  		Regexp:       *regexp,
    34  	}
    35  
    36  	if *debug {
    37  		checker.Debug = os.Stdout
    38  	}
    39  
    40  	issues, err := checker.Check(os.Stdin, os.Stderr)
    41  	if err != nil {
    42  		_, _ = fmt.Fprintln(os.Stderr, err)
    43  		os.Exit(1)
    44  	}
    45  	if len(issues) > 0 {
    46  		os.Exit(1)
    47  	}
    48  }