cmd/sgblog-comment-add/main.go | 49 ++++++++++++++++++++++++++++++++----------------- common.go | 2 +- diff --git a/cmd/sgblog-comment-add/main.go b/cmd/sgblog-comment-add/main.go index bd57ebaeef5ecb10676806795634086fcddda7bd4d879c95085cfc8cc3c52176..97408a467ebee8f6aa9a309f4c610a69599e5f99265acaf6898a644851ad66eb 100644 --- a/cmd/sgblog-comment-add/main.go +++ b/cmd/sgblog-comment-add/main.go @@ -39,6 +39,37 @@ "go.cypherpunks.ru/netstring/v2" "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 @@ ) 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 { diff --git a/common.go b/common.go index 1fc2a3108d1f260b8404093bbeab2b8ae44b3f27bbc57dccb2c3a79684ebb621..7f1c19704c22584beb16807524e1a771841ededeeefb579166a2b1bcd74a05e5 100644 --- a/common.go +++ b/common.go @@ -2,6 +2,6 @@ // SGBlog -- Git-backed CGI/inetd blogging/phlogging engine package sgblog const ( - Version = "0.6.3" + Version = "0.6.4" WhenFmt = "2006-01-02 15:04:05Z07:00" )