commit 662ef5f48e77cf285fa3c888673770701c775413 Author: Martin Thielecke Date: Tue Jul 28 18:29:55 2015 +0000 initial commit, not beautiful but it works diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3714f7d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +prepare-commit-msg diff --git a/prepare-commit-msg.go b/prepare-commit-msg.go new file mode 100644 index 0000000..a7d73ad --- /dev/null +++ b/prepare-commit-msg.go @@ -0,0 +1,74 @@ +package main + +import ( + "bytes" + "io/ioutil" + "log" + "os" + "os/exec" + "regexp" + "strings" +) + +func readFile(filename string) string { + contents, err := ioutil.ReadFile(filename) + if err != nil { + log.Printf("Error during reading file: %s", err) + return "" + } + return string(contents) +} + +func main() { + cmd := exec.Command("git", "status", "-s") + var out bytes.Buffer + cmd.Stdout = &out + err := cmd.Run() + if err != nil { + panic(err) + } + + resultLines := strings.Split(out.String(), "\n") + if resultLines == nil { + return + } + + re := regexp.MustCompile(`package\s(\w+)`) + names := make(map[string]bool) + for _, filenameStatus := range resultLines { + if filenameStatus != "" { + explodedStatus := strings.Split(strings.TrimSpace(filenameStatus), " ") + if len(explodedStatus) > 1 { + filename := explodedStatus[len(explodedStatus)-1] + status := explodedStatus[0] + if status == "??" { + continue + } + + content := readFile(filename) + + pkg := re.FindStringSubmatch(string(content)) + if len(pkg) > 1 { + names[pkg[1]] = true + } + + } + } + } + + if len(names) > 0 { + prefix := "[" + i := 0 + for name, _ := range names { + if i > 0 { + prefix += "|" + } + prefix += name + i++ + } + prefix += "] " + filename := os.Args[1] + msg := readFile(filename) + ioutil.WriteFile(filename, []byte(prefix+msg), 0x777) + } +}