1 // SGBlog -- Git-backed CGI/UCSPI blogging/phlogging/gemlogging engine
2 // Copyright (C) 2020-2024 Sergey Matveev <stargrave@stargrave.org>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, version 3 of the License.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU Affero General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // Git-backed CGI/UCSPI blogging/phlogging/gemlogging engine
25 "github.com/go-git/go-git/v5"
26 "github.com/go-git/go-git/v5/plumbing"
27 "github.com/go-git/go-git/v5/plumbing/object"
28 "go.stargrave.org/sgblog"
32 branch := flag.String("branch", "refs/heads/master", "Blog's branch reference name")
33 topicsRefName := flag.String("topics-ref", "refs/notes/topics", "Topics reference name")
35 fmt.Fprintln(flag.CommandLine.Output(), "Show known topics")
41 repo, err := git.PlainOpen(".")
45 head, err := repo.Reference(plumbing.ReferenceName(*branch), false)
49 headHash := head.Hash()
51 var topicsRef *plumbing.Reference
52 var topicsTree *object.Tree
53 if notes, err := repo.Notes(); err == nil {
54 notes.ForEach(func(ref *plumbing.Reference) error {
55 if string(ref.Name()) == *topicsRefName {
61 if topicsCommit, err := repo.CommitObject(topicsRef.Hash()); err == nil {
62 topicsTree, _ = topicsCommit.Tree()
67 repoLog, err := repo.Log(&git.LogOptions{From: headHash})
71 topicsCounter := map[string]int{}
73 commit, err := repoLog.Next()
77 for _, topic := range sgblog.ParseTopics(sgblog.GetNote(repo, topicsTree, commit.Hash)) {
78 topicsCounter[topic]++
81 topics := make([]string, 0, len(topicsCounter))
82 for topic := range topicsCounter {
83 topics = append(topics, topic)
86 for _, topic := range topics {
87 fmt.Printf("%s\t%d\n", topic, topicsCounter[topic])