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
30 "go.cypherpunks.ru/netstring/v2"
31 "gopkg.in/src-d/go-git.v4"
32 "gopkg.in/src-d/go-git.v4/plumbing"
33 "gopkg.in/src-d/go-git.v4/plumbing/object"
41 sha1DigestRe = regexp.MustCompilePOSIX(fmt.Sprintf("([0-9a-f]{%d,%d})", sha1.Size*2, sha1.Size*2))
43 notesTree *object.Tree
44 commentsRef *plumbing.Reference
45 commentsTree *object.Tree
64 CommentsNotesRef string
70 func msgSplit(msg string) []string {
71 lines := strings.Split(msg, "\n")
72 lines = lines[:len(lines)-1]
74 lines = []string{lines[0], "", ""}
79 func getNote(tree *object.Tree, what plumbing.Hash) []byte {
83 var entry *object.TreeEntry
85 paths := make([]string, 3)
86 paths[0] = what.String()
87 paths[1] = paths[0][:2] + "/" + paths[0][2:]
88 paths[2] = paths[1][:4+1] + "/" + paths[1][4+1:]
89 for _, p := range paths {
90 entry, err = tree.FindEntry(p)
98 blob, err := repo.BlobObject(entry.Hash)
102 r, err := blob.Reader()
106 data, err := ioutil.ReadAll(r)
110 return bytes.TrimSuffix(data, []byte{'\n'})
113 func parseComments(data []byte) []string {
114 comments := []string{}
115 nsr := netstring.NewReader(bytes.NewReader(data))
117 if _, err := nsr.Next(); err != nil {
120 if comment, err := ioutil.ReadAll(nsr); err == nil {
121 comments = append(comments, string(comment))
127 func initRepo(cfg *Cfg) (*plumbing.Hash, error) {
129 repo, err = git.PlainOpen(cfg.GitPath)
133 head, err := repo.Reference(plumbing.ReferenceName(cfg.Branch), false)
137 headHash := head.Hash()
138 if notes, err := repo.Notes(); err == nil {
139 var notesRef *plumbing.Reference
140 notes.ForEach(func(ref *plumbing.Reference) error {
141 switch string(ref.Name()) {
142 case "refs/notes/commits":
144 case cfg.CommentsNotesRef:
150 if commentsCommit, err := repo.CommitObject(notesRef.Hash()); err == nil {
151 notesTree, _ = commentsCommit.Tree()
154 if commentsRef != nil {
155 if commentsCommit, err := repo.CommitObject(commentsRef.Hash()); err == nil {
156 commentsTree, _ = commentsCommit.Tree()
160 return &headHash, nil
164 if len(os.Args) == 3 && os.Args[1] == "-gopher" {