2 SGBlog -- Git-backed CGI/UCSPI blogging/phlogging/gemlogging engine
3 Copyright (C) 2020-2023 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Affero General Public License as
7 published by the Free Software Foundation, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU Affero General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 // Git-backed CGI/UCSPI blogging/phlogging/gemlogging engine
39 "go.cypherpunks.ru/recfile"
40 "go.stargrave.org/sgblog"
43 var hashFinder = regexp.MustCompile("([0-9a-f]{40})")
45 // Remove various whitespaces and excess lines, because git-notes-add
46 // will remove and we have to know exact bytes count
47 func cleanupBody(body string) []string {
48 lines := strings.Split(string(body), "\n")
49 for i, line := range lines {
50 line = strings.ReplaceAll(line, " ", " ")
51 line = strings.TrimRight(line, " \r")
57 for lines[len(lines)-1] == "" {
58 lines = lines[:len(lines)-1]
60 withoutDups := make([]string, 0, len(lines))
62 for _, line := range lines {
71 withoutDups = append(withoutDups, line)
77 gitCmd := flag.String("git-cmd", "/usr/local/bin/git", "Path to git executable")
78 gitDir := flag.String("git-dir", "", "Path to .git repository")
79 notesRef := flag.String("ref", "comments", "notes reference name")
80 umask := flag.String("umask", "027", "umask value")
81 dryRun := flag.Bool("dryrun", false, "Show comment, do not add")
82 committerEmail := flag.String(
84 "comment@blog.example.com",
85 "Git committer's email",
88 uid := syscall.Geteuid()
89 if err := syscall.Setuid(uid); err != nil {
92 umaskInt, err := strconv.ParseUint(*umask, 8, 16)
96 syscall.Umask(int(umaskInt))
98 data, err := io.ReadAll(os.Stdin)
102 if bytes.HasPrefix(data, []byte("From ")) {
103 data = data[bytes.Index(data, []byte("\n"))+1:]
105 msg, err := mail.ReadMessage(bytes.NewReader(data))
109 subj, r, err := parseEmail(msg)
113 body, err := io.ReadAll(r)
117 from := msg.Header.Get("From")
119 log.Fatal("From is missing")
124 from, err = new(mime.WordDecoder).DecodeHeader(from)
129 subj = hashFinder.FindString(subj)
131 log.Fatal("no commit hash found in subject")
133 if h, err := hex.DecodeString(subj); err != nil || len(h) != sha1.Size {
136 fromCols := strings.Fields(from)
137 if len(fromCols) == 1 {
138 if idx := strings.Index(from, "@"); idx != -1 {
139 from = strings.Trim(from[:idx], "<>")
142 from = strings.Join(fromCols[:len(fromCols)-1], " ")
146 *gitCmd, "--git-dir", *gitDir,
147 "notes", "--ref", *notesRef, "show", subj,
149 note, _ := cmd.Output()
150 note = bytes.TrimRight(note, "\r\n")
152 buf := bytes.NewBuffer(note)
153 recfileW := recfile.NewWriter(buf)
154 if _, err = recfileW.RecordStart(); err != nil {
157 // We trimmed newline, so have to start record twice
158 if _, err = recfileW.RecordStart(); err != nil {
161 if _, err = recfileW.WriteFields(
162 recfile.Field{Name: "From", Value: from},
163 recfile.Field{Name: "Date", Value: time.Now().UTC().Format(sgblog.WhenFmt)},
167 if _, err = recfileW.WriteFieldMultiline(
168 "Body", append([]string{""}, cleanupBody(string(body))...),
174 fmt.Print(buf.String())
179 *gitCmd, "--git-dir", *gitDir,
180 "notes", "--ref", *notesRef, "add",
181 "-F", "-", "-f", subj,
185 "GIT_AUTHOR_NAME=SGBlog "+sgblog.Version,
186 "GIT_AUTHOR_EMAIL="+*committerEmail,
187 "GIT_COMMITTER_NAME=SGBlog "+sgblog.Version,
188 "GIT_COMMITTER_EMAIL="+*committerEmail,
191 if err = cmd.Run(); err != nil {