]> Sergey Matveev's repositories - sgblog.git/blobdiff - cmd/sgblog-comment-add/main.go
recfiles instead of netstrings
[sgblog.git] / cmd / sgblog-comment-add / main.go
index 837666c3b8dd12a1f71b713dbefbc36c29666da4..2b977ae7feb14c0c886851f8871ca6e1d982b32c 100644 (file)
@@ -30,18 +30,20 @@ import (
        "net/mail"
        "os"
        "os/exec"
+       "regexp"
        "strconv"
        "strings"
        "syscall"
        "time"
 
-       "go.cypherpunks.ru/netstring/v2"
        "go.stargrave.org/sgblog"
 )
 
+var hashFinder = regexp.MustCompile("([0-9a-f]{40})")
+
 // Remove various whitespaces and excess lines, because git-notes-add
 // will remove and we have to know exact bytes count
-func cleanupBody(body string) string {
+func cleanupBody(body string) []string {
        lines := strings.Split(string(body), "\n")
        for i, line := range lines {
                line = strings.ReplaceAll(line, "       ", "    ")
@@ -67,7 +69,7 @@ func cleanupBody(body string) string {
                }
                withoutDups = append(withoutDups, line)
        }
-       return strings.Join(withoutDups, "\n")
+       return withoutDups
 }
 
 func main() {
@@ -116,7 +118,10 @@ func main() {
                log.Fatal(err)
        }
 
-       subj = strings.TrimPrefix(subj, "Re: ")
+       subj = hashFinder.FindString(subj)
+       if subj == "" {
+               log.Fatal("no commit hash found in subject")
+       }
        if h, err := hex.DecodeString(subj); err != nil || len(h) != sha1.Size {
                os.Exit(0)
        }
@@ -137,13 +142,16 @@ func main() {
        note = bytes.TrimRight(note, "\r\n")
 
        buf := bytes.NewBuffer(note)
-       w := netstring.NewWriter(buf)
-       w.WriteChunk([]byte(fmt.Sprintf(
-               "From: %s\nDate: %s\n\n%s",
+       buf.WriteString(fmt.Sprintf(
+               "\n\nFrom: %s\nDate: %s\nBody:\n",
                from,
                time.Now().UTC().Format(sgblog.WhenFmt),
-               cleanupBody(string(body)),
-       )))
+       ))
+       for _, s := range cleanupBody(string(body)) {
+               buf.WriteString("+ ")
+               buf.WriteString(s)
+               buf.WriteString("\n")
+       }
 
        if *dryRun {
                fmt.Print(buf.String())