]> Sergey Matveev's repositories - sgblog.git/blobdiff - cmd/sgblog-comment-add/main.go
Trim possible \r
[sgblog.git] / cmd / sgblog-comment-add / main.go
index b52e68a4347b230f67c9846cb66fce4e2062560f..100f5f2138339b56e058277c5f1af9c29f2922d7 100644 (file)
@@ -35,15 +35,20 @@ import (
        "time"
 
        "go.cypherpunks.ru/netstring/v2"
+       "go.stargrave.org/sgblog"
 )
 
-const WhenFmt = "2006-01-02 15:04:05Z07:00"
-
 func main() {
        gitCmd := flag.String("git-cmd", "/usr/local/bin/git", "Path to git executable")
        gitDir := flag.String("git-dir", "", "Path to .git repository")
        notesRef := flag.String("ref", "comments", "notes reference name")
        umask := flag.String("umask", "027", "umask value")
+       dryRun := flag.Bool("dryrun", false, "Show comment, do not add")
+       committerEmail := flag.String(
+               "committer-email",
+               "comment@blog.example.com",
+               "Git committer's email",
+       )
        flag.Parse()
        uid := syscall.Geteuid()
        if err := syscall.Setuid(uid); err != nil {
@@ -75,24 +80,31 @@ func main() {
                log.Fatal("no body")
        }
 
+       subj = strings.TrimPrefix(subj, "Re: ")
        if h, err := hex.DecodeString(subj); err != nil || len(h) != sha1.Size {
                os.Exit(0)
        }
        fromCols := strings.Fields(from)
-       from = strings.Join(fromCols[:len(fromCols)-1], " ")
+       if len(fromCols) == 1 {
+               if idx := strings.Index(from, "@"); idx != -1 {
+                       from = strings.Trim(from[:idx], "<>")
+               }
+       } else {
+               from = strings.Join(fromCols[:len(fromCols)-1], " ")
+       }
 
        cmd := exec.Command(
                *gitCmd, "--git-dir", *gitDir,
                "notes", "--ref", *notesRef, "show", subj,
        )
        note, _ := cmd.Output()
-       note = bytes.TrimSuffix(note, []byte{'\n'})
+       note = bytes.TrimRight(note, "\r\n")
 
        // Remove trailing whitespaces, because git-notes-add will remove
        // them anyway, and we have to know exact bytes count
        lines := strings.Split(string(body), "\n")
        for i, line := range lines {
-               lines[i] = strings.TrimRight(line, " ")
+               lines[i] = strings.TrimRight(line, " \r")
        }
        for lines[len(lines)-1] == "" {
                lines = lines[:len(lines)-1]
@@ -103,15 +115,27 @@ func main() {
        w.WriteChunk([]byte(fmt.Sprintf(
                "From: %s\nDate: %s\n\n%s",
                from,
-               time.Now().Format(WhenFmt),
+               time.Now().UTC().Format(sgblog.WhenFmt),
                strings.Join(lines, "\n"),
        )))
 
+       if *dryRun {
+               fmt.Print(buf.String())
+               os.Exit(0)
+       }
+
        cmd = exec.Command(
                *gitCmd, "--git-dir", *gitDir,
                "notes", "--ref", *notesRef, "add",
                "-F", "-", "-f", subj,
        )
+       cmd.Env = append(
+               cmd.Env,
+               "GIT_AUTHOR_NAME=SGBlog "+sgblog.Version,
+               "GIT_AUTHOR_EMAIL="+*committerEmail,
+               "GIT_COMMITTER_NAME=SGBlog "+sgblog.Version,
+               "GIT_COMMITTER_EMAIL="+*committerEmail,
+       )
        cmd.Stdin = buf
        if err = cmd.Run(); err != nil {
                log.Fatal(err)