]> Sergey Matveev's repositories - sgblog.git/blobdiff - cmd/sgblog/main.go
Topics support
[sgblog.git] / cmd / sgblog / main.go
index 68a3914e04c3240591c84e6ee74128f0f048dbfe..2e4a483e27d1683c0e064481578c61bf57b4558f 100644 (file)
@@ -25,7 +25,9 @@ import (
        "io/ioutil"
        "os"
        "regexp"
+       "sort"
        "strings"
+       "text/scanner"
 
        "github.com/go-git/go-git/v5"
        "github.com/go-git/go-git/v5/plumbing"
@@ -43,6 +45,8 @@ var (
        notesTree    *object.Tree
        commentsRef  *plumbing.Reference
        commentsTree *object.Tree
+       topicsRef    *plumbing.Reference
+       topicsTree   *object.Tree
 )
 
 type Cfg struct {
@@ -64,6 +68,9 @@ type Cfg struct {
        CommentsNotesRef string
        CommentsEmail    string
 
+       TopicsNotesRef  string
+       TopicsCachePath string
+
        GopherDomain string
 }
 
@@ -124,6 +131,17 @@ func parseComments(data []byte) []string {
        return comments
 }
 
+func parseTopics(data []byte) []string {
+       var s scanner.Scanner
+       s.Init(bytes.NewBuffer(data))
+       topics := []string{}
+       for tok := s.Scan(); tok != scanner.EOF; tok = s.Scan() {
+               topics = append(topics, s.TokenText())
+       }
+       sort.Strings(topics)
+       return topics
+}
+
 func initRepo(cfg *Cfg) (*plumbing.Hash, error) {
        var err error
        repo, err = git.PlainOpen(cfg.GitPath)
@@ -143,6 +161,8 @@ func initRepo(cfg *Cfg) (*plumbing.Hash, error) {
                                notesRef = ref
                        case cfg.CommentsNotesRef:
                                commentsRef = ref
+                       case cfg.TopicsNotesRef:
+                               topicsRef = ref
                        }
                        return nil
                })
@@ -156,6 +176,11 @@ func initRepo(cfg *Cfg) (*plumbing.Hash, error) {
                                commentsTree, _ = commentsCommit.Tree()
                        }
                }
+               if topicsRef != nil {
+                       if topicsCommit, err := repo.CommitObject(topicsRef.Hash()); err == nil {
+                               topicsTree, _ = topicsCommit.Tree()
+                       }
+               }
        }
        return &headHash, nil
 }