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/netstring/v2"
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 nsr := netstring.NewReader(bytes.NewReader(data))
124 if _, err := nsr.Next(); err != nil {
127 if comment, err := ioutil.ReadAll(nsr); err == nil {
128 comments = append(comments, string(comment))
134 func parseTopics(data []byte) []string {
135 var s scanner.Scanner
136 s.Init(bytes.NewBuffer(data))
138 for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() {
139 topics = append(topics, s.TokenText())
145 func initRepo(cfg *Cfg) (*plumbing.Hash, error) {
147 repo, err = git.PlainOpen(cfg.GitPath)
151 head, err := repo.Reference(plumbing.ReferenceName(cfg.Branch), false)
155 headHash := head.Hash()
156 if notes, err := repo.Notes(); err == nil {
157 var notesRef *plumbing.Reference
158 notes.ForEach(func(ref *plumbing.Reference) error {
159 switch string(ref.Name()) {
160 case "refs/notes/commits":
162 case cfg.CommentsNotesRef:
164 case cfg.TopicsNotesRef:
170 if commentsCommit, err := repo.CommitObject(notesRef.Hash()); err == nil {
171 notesTree, _ = commentsCommit.Tree()
174 if commentsRef != nil {
175 if commentsCommit, err := repo.CommitObject(commentsRef.Hash()); err == nil {
176 commentsTree, _ = commentsCommit.Tree()
179 if topicsRef != nil {
180 if topicsCommit, err := repo.CommitObject(topicsRef.Hash()); err == nil {
181 topicsTree, _ = topicsCommit.Tree()
185 return &headHash, nil
189 if len(os.Args) == 3 && os.Args[1] == "-gopher" {