]> Sergey Matveev's repositories - sgblog.git/commitdiff
Yet another duplicate lines removing v0.6.4
authorSergey Matveev <stargrave@stargrave.org>
Mon, 1 Jun 2020 09:06:49 +0000 (12:06 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Mon, 1 Jun 2020 09:07:03 +0000 (12:07 +0300)
cmd/sgblog-comment-add/main.go
common.go

index 31b3d01a377781cc3e8e4383a3057c943fdb432b..837666c3b8dd12a1f71b713dbefbc36c29666da4 100644 (file)
@@ -39,6 +39,37 @@ import (
        "go.stargrave.org/sgblog"
 )
 
+// 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 {
+       lines := strings.Split(string(body), "\n")
+       for i, line := range lines {
+               line = strings.ReplaceAll(line, "       ", "    ")
+               line = strings.TrimRight(line, " \r")
+               lines[i] = line
+       }
+       for lines[0] == "" {
+               lines = lines[1:]
+       }
+       for lines[len(lines)-1] == "" {
+               lines = lines[:len(lines)-1]
+       }
+       withoutDups := make([]string, 0, len(lines))
+       emptyMet := false
+       for _, line := range lines {
+               if line == "" {
+                       if emptyMet {
+                               continue
+                       }
+                       emptyMet = true
+               } else {
+                       emptyMet = false
+               }
+               withoutDups = append(withoutDups, line)
+       }
+       return strings.Join(withoutDups, "\n")
+}
+
 func main() {
        gitCmd := flag.String("git-cmd", "/usr/local/bin/git", "Path to git executable")
        gitDir := flag.String("git-dir", "", "Path to .git repository")
@@ -105,29 +136,13 @@ func main() {
        note, _ := cmd.Output()
        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. Also convert
-       // all tabs into spaces
-       lines := strings.Split(string(body), "\n")
-       for i, line := range lines {
-               line = strings.ReplaceAll(line, "       ", "    ")
-               line = strings.TrimRight(line, " \r")
-               lines[i] = line
-       }
-       for lines[0] == "" {
-               lines = lines[1:]
-       }
-       for lines[len(lines)-1] == "" {
-               lines = lines[:len(lines)-1]
-       }
-
        buf := bytes.NewBuffer(note)
        w := netstring.NewWriter(buf)
        w.WriteChunk([]byte(fmt.Sprintf(
                "From: %s\nDate: %s\n\n%s",
                from,
                time.Now().UTC().Format(sgblog.WhenFmt),
-               strings.Join(lines, "\n"),
+               cleanupBody(string(body)),
        )))
 
        if *dryRun {
index 57419d0d0301a47b5b8e1d7265e1964ea879b170..db523a65053f0ae310252ebb00388965ca106ea2 100644 (file)
--- a/common.go
+++ b/common.go
@@ -2,6 +2,6 @@
 package sgblog
 
 const (
-       Version = "0.6.3"
+       Version = "0.6.4"
        WhenFmt = "2006-01-02 15:04:05Z07:00"
 )