2 SGBlog -- Git-based 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/>.
34 "github.com/hjson/hjson-go"
35 "go.stargrave.org/sgblog"
36 "gopkg.in/src-d/go-git.v4"
37 "gopkg.in/src-d/go-git.v4/plumbing"
42 var DashLine = strings.Repeat("-", 72)
46 cfgRaw, err := ioutil.ReadFile(cfgPath)
50 var cfgGeneral map[string]interface{}
51 if err = hjson.Unmarshal(cfgRaw, &cfgGeneral); err != nil {
54 cfgRaw, err = json.Marshal(cfgGeneral)
59 if err = json.Unmarshal(cfgRaw, &cfg); err != nil {
62 if cfg.GopherDomain == "" {
63 log.Fatalln("GopherDomain is not configured")
66 headHash, err := initRepo(cfg)
71 scanner := bufio.NewScanner(io.LimitReader(os.Stdin, 1<<8))
73 log.Fatalln(errors.New("no CRLF found"))
75 selector := scanner.Text()
79 if strings.HasPrefix(selector, "offset/") {
80 offset, err := strconv.Atoi(selector[len("offset/"):])
84 repoLog, err := repo.Log(&git.LogOptions{From: *headHash})
89 for i := 0; i < offset; i++ {
90 if _, err = repoLog.Next(); err != nil {
99 var monthPrev time.Month
101 for i := 0; i < PageEntries; i++ {
102 commit, err := repoLog.Next()
107 yearCur, monthCur, dayCur := commit.Author.When.Date()
108 if dayCur != dayPrev || monthCur != monthPrev || yearCur != yearPrev {
109 menu.WriteString(fmt.Sprintf(
110 "i%04d-%02d-%02d\t\tnull.host\t1%s",
111 yearCur, monthCur, dayCur, CRLF,
113 yearPrev, monthPrev, dayPrev = yearCur, monthCur, dayCur
116 lines := msgSplit(commit.Message)
117 var commentsValue string
118 if l := len(parseComments(getNote(commentsTree, commit.Hash))); l > 0 {
119 commentsValue = fmt.Sprintf(" (%dC)", l)
121 menu.WriteString(fmt.Sprintf(
122 "0[%02d:%02d] %s (%dL)%s\t/%s\t%s\t%d%s",
123 commit.Author.When.Hour(),
124 commit.Author.When.Minute(),
128 commit.Hash.String(),
129 cfg.GopherDomain, 70, CRLF,
134 "i%s (%d-%d)\t\tnull.host\t1%s",
140 if cfg.AboutURL != "" {
141 fmt.Printf("iAbout: %s\t\tnull.host\t1%s", cfg.AboutURL, CRLF)
144 offsetPrev := offset - PageEntries
149 "1Prev\toffset/%d\t%s\t%d%s",
151 cfg.GopherDomain, 70, CRLF,
156 "1Next\toffset/%d\t%s\t%d%s",
158 cfg.GopherDomain, 70, CRLF,
161 fmt.Print(menu.String())
162 fmt.Printf("iGenerated by: SGBlog %s\t\tnull.host\t1%s", sgblog.Version, CRLF)
163 fmt.Print("." + CRLF)
164 } else if sha1DigestRe.MatchString(selector) {
165 commit, err := repo.CommitObject(plumbing.NewHash(selector[1:]))
170 "What: %s\nWhen: %s\n%s\n%s",
171 commit.Hash.String(),
172 commit.Author.When.Format(sgblog.WhenFmt),
176 notesRaw := getNote(notesTree, commit.Hash)
177 if len(notesRaw) > 0 {
178 fmt.Printf("%s\nNote:\n%s\n", DashLine, string(notesRaw))
180 for i, comment := range parseComments(getNote(commentsTree, commit.Hash)) {
181 fmt.Printf("%s\ncomment %d:\n%s\n", DashLine, i, comment)
183 fmt.Printf("%s\nGenerated by: SGBlog %s\n", DashLine, sgblog.Version)
185 log.Fatalln(errors.New("unknown selector"))