optimized hook with a faster processing and less loops
This commit is contained in:
parent
b030d04311
commit
35842555c9
|
@ -6,6 +6,7 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
@ -19,54 +20,53 @@ func readFile(filename string) string {
|
||||||
return string(contents)
|
return string(contents)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func runCommand(command string, values ...string) string {
|
||||||
cmd := exec.Command("git", "status", "-s")
|
cmd := exec.Command(command, values...)
|
||||||
var out bytes.Buffer
|
var out bytes.Buffer
|
||||||
cmd.Stdout = &out
|
cmd.Stdout = &out
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
return out.String()
|
||||||
|
}
|
||||||
|
|
||||||
resultLines := strings.Split(out.String(), "\n")
|
func main() {
|
||||||
|
gitDir := runCommand("git", "rev-parse", "--git-dir")
|
||||||
|
gitDiff := runCommand("git", "diff", "--name-only", "--cached")
|
||||||
|
|
||||||
|
absolutePath := filepath.Dir(gitDir)
|
||||||
|
|
||||||
|
resultLines := strings.Split(gitDiff, "\n")
|
||||||
if resultLines == nil {
|
if resultLines == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
re := regexp.MustCompile(`package\s(\w+)`)
|
re := regexp.MustCompile(`package\s(\w+)`)
|
||||||
names := make(map[string]bool)
|
names := make(map[string]bool)
|
||||||
for _, filenameStatus := range resultLines {
|
var prefix string
|
||||||
if filenameStatus != "" {
|
for _, filename := range resultLines {
|
||||||
explodedStatus := strings.Split(strings.TrimSpace(filenameStatus), " ")
|
if filename != "" {
|
||||||
if len(explodedStatus) > 1 {
|
content := readFile(absolutePath + "/" + filename)
|
||||||
filename := explodedStatus[len(explodedStatus)-1]
|
|
||||||
status := explodedStatus[0]
|
|
||||||
if status == "??" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
content := readFile(filename)
|
pkg := re.FindStringSubmatch(string(content))
|
||||||
|
|
||||||
pkg := re.FindStringSubmatch(string(content))
|
|
||||||
if len(pkg) > 1 {
|
|
||||||
names[pkg[1]] = true
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if len(pkg) < 2 {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
if _, found := names[pkg[1]]; !found {
|
||||||
|
if prefix != "" {
|
||||||
|
prefix += "|"
|
||||||
|
}
|
||||||
|
prefix += pkg[1]
|
||||||
|
names[pkg[1]] = true
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(names) > 0 {
|
if prefix != "" {
|
||||||
prefix := "["
|
prefix = "[" + prefix + "]"
|
||||||
i := 0
|
|
||||||
for name, _ := range names {
|
|
||||||
if i > 0 {
|
|
||||||
prefix += "|"
|
|
||||||
}
|
|
||||||
prefix += name
|
|
||||||
i++
|
|
||||||
}
|
|
||||||
prefix += "] "
|
|
||||||
filename := os.Args[1]
|
filename := os.Args[1]
|
||||||
msg := readFile(filename)
|
msg := readFile(filename)
|
||||||
ioutil.WriteFile(filename, []byte(prefix+msg), 0x777)
|
ioutil.WriteFile(filename, []byte(prefix+msg), 0x777)
|
||||||
|
|
Reference in New Issue
Block a user