2 SGBlog -- Git-backed CGI/inetd blogging/phlogging engine
3 Copyright (C) 2020 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/inetd blogging/phlogging engine
32 "github.com/go-git/go-git/v5"
33 "github.com/go-git/go-git/v5/plumbing"
34 "github.com/go-git/go-git/v5/plumbing/object"
35 "go.cypherpunks.ru/recfile"
43 sha1DigestRe = regexp.MustCompilePOSIX(fmt.Sprintf("([0-9a-f]{%d,%d})", sha1.Size*2, sha1.Size*2))
45 notesTree *object.Tree
46 commentsRef *plumbing.Reference
47 commentsTree *object.Tree
48 topicsRef *plumbing.Reference
49 topicsTree *object.Tree
68 CommentsNotesRef string
72 TopicsCachePath string
77 func msgSplit(msg string) []string {
78 lines := strings.Split(msg, "\n")
79 lines = lines[:len(lines)-1]
81 lines = []string{lines[0], "", ""}
86 func getNote(tree *object.Tree, what plumbing.Hash) []byte {
90 var entry *object.TreeEntry
92 paths := make([]string, 3)
93 paths[0] = what.String()
94 paths[1] = paths[0][:2] + "/" + paths[0][2:]
95 paths[2] = paths[1][:4+1] + "/" + paths[1][4+1:]
96 for _, p := range paths {
97 entry, err = tree.FindEntry(p)
105 blob, err := repo.BlobObject(entry.Hash)
109 r, err := blob.Reader()
113 data, err := ioutil.ReadAll(r)
117 return bytes.TrimSuffix(data, []byte{'\n'})
120 func parseComments(data []byte) []string {
121 comments := []string{}
122 r := recfile.NewReader(bytes.NewReader(data))
124 fields, err := r.Next()
128 if len(fields) != 3 ||
129 fields[0].Name != "From" ||
130 fields[1].Name != "Date" ||
131 fields[2].Name != "Body" {
134 comments = append(comments, fmt.Sprintf(
135 "%s: %s\n%s: %s\n%s",
136 fields[0].Name, fields[0].Value,
137 fields[1].Name, fields[1].Value,
144 func parseTopics(data []byte) []string {
145 var s scanner.Scanner
146 s.Init(bytes.NewBuffer(data))
148 for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() {
149 topics = append(topics, s.TokenText())
155 func initRepo(cfg *Cfg) (*plumbing.Hash, error) {
157 repo, err = git.PlainOpen(cfg.GitPath)
161 head, err := repo.Reference(plumbing.ReferenceName(cfg.Branch), false)
165 headHash := head.Hash()
166 if notes, err := repo.Notes(); err == nil {
167 var notesRef *plumbing.Reference
168 notes.ForEach(func(ref *plumbing.Reference) error {
169 switch string(ref.Name()) {
170 case "refs/notes/commits":
172 case cfg.CommentsNotesRef:
174 case cfg.TopicsNotesRef:
180 if commentsCommit, err := repo.CommitObject(notesRef.Hash()); err == nil {
181 notesTree, _ = commentsCommit.Tree()
184 if commentsRef != nil {
185 if commentsCommit, err := repo.CommitObject(commentsRef.Hash()); err == nil {
186 commentsTree, _ = commentsCommit.Tree()
189 if topicsRef != nil {
190 if topicsCommit, err := repo.CommitObject(topicsRef.Hash()); err == nil {
191 topicsTree, _ = topicsCommit.Tree()
195 return &headHash, nil
199 if len(os.Args) == 3 && os.Args[1] == "-gopher" {