From 813bbcf954d94aa92ebc36689cf05ca443014a15 Mon Sep 17 00:00:00 2001 From: Timur Ismagilov Date: Thu, 20 Aug 2020 22:20:13 +0500 Subject: [PATCH] Route /rev/ to see any text part at a specified revision --- history/sh.go | 11 ++++++++--- http_readers.go | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/history/sh.go b/history/sh.go index 34af389..7bee54b 100644 --- a/history/sh.go +++ b/history/sh.go @@ -89,12 +89,17 @@ func parseRevisionLine(line string) *Revision { return &rev } -func (rev *Revision) AsHtmlTableRow() string { +func (rev *Revision) AsHtmlTableRow(hyphaName string) string { return fmt.Sprintf(` - %s + %s %s %s -`, rev.Hash, rev.Username, rev.Time.String(), rev.Message) +`, rev.Hash, hyphaName, rev.Hash, rev.Username, rev.Time.String(), rev.Message) +} + +func FileAtRevision(filepath, hash string) (string, error) { + out, err := gitsh("show", hash+":"+filepath) + return out.String(), err } diff --git a/http_readers.go b/http_readers.go index 889d83d..61cca09 100644 --- a/http_readers.go +++ b/http_readers.go @@ -6,6 +6,8 @@ import ( "log" "net/http" "os" + "path" + "strings" "github.com/bouncepaw/mycorrhiza/gemtext" "github.com/bouncepaw/mycorrhiza/history" @@ -17,6 +19,48 @@ func init() { http.HandleFunc("/text/", handlerText) http.HandleFunc("/binary/", handlerBinary) http.HandleFunc("/history/", handlerHistory) + http.HandleFunc("/rev/", handlerRevision) +} + +// handlerRevision displays a specific revision of text part a page +func handlerRevision(w http.ResponseWriter, rq *http.Request) { + log.Println(rq.URL) + var ( + shorterUrl = strings.TrimPrefix(rq.URL.Path, "/rev/") + revHash = path.Dir(shorterUrl) + hyphaName = CanonicalName(strings.TrimPrefix(shorterUrl, revHash+"/")) + contents = fmt.Sprintf(`

This hypha had no text at this revision.

`) + textPath = hyphaName + "&.gmi" + textContents, err = history.FileAtRevision(textPath, revHash) + ) + log.Println(revHash, hyphaName, textPath, textContents, err) + if err == nil { + contents = gemtext.ToHtml(hyphaName, textContents) + } + form := fmt.Sprintf(` +
+ +
+ %[2]s + %[3]s +
+
+ +
+`, hyphaName, + naviTitle(hyphaName), + contents, + tree.TreeAsHtml(hyphaName, IterateHyphaNamesWith)) + w.Header().Set("Content-Type", "text/html;charset=utf-8") + w.WriteHeader(http.StatusOK) + w.Write([]byte(base(hyphaName, form))) } // handlerHistory lists all revisions of a hypha @@ -28,13 +72,13 @@ func handlerHistory(w http.ResponseWriter, rq *http.Request) { revsT, err := history.Revisions(data.textPath) if err == nil { for _, rev := range revsT { - tbody += rev.AsHtmlTableRow() + tbody += rev.AsHtmlTableRow(hyphaName) } } revsB, err := history.Revisions(data.binaryPath) if err == nil { for _, rev := range revsB { - tbody += rev.AsHtmlTableRow() + tbody += rev.AsHtmlTableRow(hyphaName) } } log.Println(revsT, revsB)