100 lines
2.0 KiB
Go
100 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"log"
|
|
"os"
|
|
"regexp"
|
|
"strconv"
|
|
|
|
"sourcegraph.com/sourcegraph/go-diff/diff"
|
|
)
|
|
|
|
func main() {
|
|
var diffPath, includes, excludes, label string
|
|
var dryrun bool
|
|
|
|
log.SetFlags(0)
|
|
|
|
flag.BoolVar(&dryrun, "d", false, "Dry run mode")
|
|
flag.StringVar(&diffPath, "f", "-", "filename of diff (default: stdin)")
|
|
flag.StringVar(&includes, "m", "", "Select files matching this")
|
|
flag.StringVar(&excludes, "x", "", "Excludes files matching this")
|
|
flag.Parse()
|
|
|
|
var diffFile *os.File
|
|
if diffPath == "-" {
|
|
diffFile = os.Stdin
|
|
} else {
|
|
var err error
|
|
diffFile, err = os.Open(diffPath)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
defer diffFile.Close()
|
|
|
|
r := diff.NewMultiFileDiffReader(diffFile)
|
|
for i := 0; ; i++ {
|
|
var selected, match bool
|
|
var err error
|
|
|
|
fdiff, err := r.ReadFile()
|
|
if fdiff != nil {
|
|
label = fmt.Sprintf("orig(%s) new(%s)", fdiff.OrigName, fdiff.NewName)
|
|
}
|
|
if err == io.EOF {
|
|
break
|
|
}
|
|
if err != nil {
|
|
log.Fatalf("err read %s: %s", label, err)
|
|
continue
|
|
}
|
|
|
|
if includes != "" {
|
|
if match, err = regexp.MatchString(includes, fdiff.OrigName); err != nil {
|
|
fmt.Printf("%s\n", err)
|
|
}
|
|
|
|
if match, err = regexp.MatchString(includes, fdiff.NewName); err != nil {
|
|
fmt.Printf("%s\n", err)
|
|
}
|
|
if match {
|
|
selected = true
|
|
}
|
|
} else {
|
|
selected = true
|
|
}
|
|
if excludes != "" {
|
|
if match, err = regexp.MatchString(excludes, fdiff.OrigName); err != nil {
|
|
fmt.Printf("%s\n", err)
|
|
}
|
|
|
|
if match, err = regexp.MatchString(excludes, fdiff.NewName); err != nil {
|
|
fmt.Printf("%s\n", err)
|
|
}
|
|
if match {
|
|
selected = false
|
|
}
|
|
}
|
|
if dryrun {
|
|
fmt.Printf("File %s : %s is selected: %s\n", fdiff.OrigName, fdiff.NewName, strconv.FormatBool(selected))
|
|
continue
|
|
}
|
|
if !selected {
|
|
continue
|
|
}
|
|
|
|
out, err := diff.PrintFileDiff(fdiff)
|
|
if err != nil {
|
|
log.Fatalf("err print %s: %s", label, err)
|
|
continue
|
|
}
|
|
if _, err := os.Stdout.Write(out); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
}
|