"io/ioutil"
"os"
"regexp"
+ "sort"
"strings"
+ "text/scanner"
- "go.cypherpunks.ru/netstring/v2"
- "gopkg.in/src-d/go-git.v4"
- "gopkg.in/src-d/go-git.v4/plumbing"
- "gopkg.in/src-d/go-git.v4/plumbing/object"
+ "github.com/go-git/go-git/v5"
+ "github.com/go-git/go-git/v5/plumbing"
+ "github.com/go-git/go-git/v5/plumbing/object"
)
const (
notesTree *object.Tree
commentsRef *plumbing.Reference
commentsTree *object.Tree
+ topicsRef *plumbing.Reference
+ topicsTree *object.Tree
)
type Cfg struct {
CommentsNotesRef string
CommentsEmail string
+ TopicsNotesRef string
+ TopicsCachePath string
+
GopherDomain string
}
func parseComments(data []byte) []string {
comments := []string{}
- nsr := netstring.NewReader(bytes.NewReader(data))
- for {
- if _, err := nsr.Next(); err != nil {
- break
+ isBody := false
+ comment := make([]string, 0, 4)
+ lines := strings.Split(strings.TrimSuffix(string(data), "\n"), "\n")
+ if len(lines) == 1 {
+ return comments
+ }
+ for _, s := range lines {
+ if s == "" {
+ comments = append(comments, strings.Join(comment, "\n"))
+ comment = make([]string, 0, 4)
+ isBody = false
+ continue
}
- if comment, err := ioutil.ReadAll(nsr); err == nil {
- comments = append(comments, string(comment))
+ if s == "Body:" {
+ isBody = true
+ comment = append(comment, "")
+ continue
}
+ if isBody {
+ if s == "+" {
+ comment = append(comment, "")
+ } else {
+ comment = append(comment, strings.TrimPrefix(s, "+ "))
+ }
+ continue
+ }
+ comment = append(comment, s)
+ }
+ if len(comment) > 1 {
+ comments = append(comments, strings.Join(comment, "\n"))
}
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)
notesRef = ref
case cfg.CommentsNotesRef:
commentsRef = ref
+ case cfg.TopicsNotesRef:
+ topicsRef = ref
}
return nil
})
commentsTree, _ = commentsCommit.Tree()
}
}
+ if topicsRef != nil {
+ if topicsCommit, err := repo.CommitObject(topicsRef.Hash()); err == nil {
+ topicsTree, _ = topicsCommit.Tree()
+ }
+ }
}
return &headHash, nil
}