Migrate history and main

This commit is contained in:
Timur Ismagilov 2024-09-07 22:01:27 +03:00
parent cab7c60bad
commit fcb66f2098
6 changed files with 66 additions and 52 deletions

View File

@ -3,11 +3,12 @@ package history
import ( import (
"errors" "errors"
"fmt" "fmt"
"github.com/bouncepaw/mycorrhiza/internal/cfg"
"net/url" "net/url"
"strings" "strings"
"time" "time"
"github.com/bouncepaw/mycorrhiza/internal/cfg"
"github.com/gorilla/feeds" "github.com/gorilla/feeds"
) )

View File

@ -4,7 +4,7 @@ package history
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"log" "log/slog"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"regexp" "regexp"
@ -21,12 +21,14 @@ var renameMsgPattern = regexp.MustCompile(`^Rename (.*) to .*`)
var gitEnv = []string{"GIT_COMMITTER_NAME=wikimind", "GIT_COMMITTER_EMAIL=wikimind@mycorrhiza"} var gitEnv = []string{"GIT_COMMITTER_NAME=wikimind", "GIT_COMMITTER_EMAIL=wikimind@mycorrhiza"}
// Start finds git and initializes git credentials. // Start finds git and initializes git credentials.
func Start() { func Start() error {
path, err := exec.LookPath("git") path, err := exec.LookPath("git")
if err != nil { if err != nil {
log.Fatal("Could not find the git executable. Check your $PATH.") slog.Error("Could not find the Git executable. Check your $PATH.")
return err
} }
gitpath = path gitpath = path
return nil
} }
// InitGitRepo checks a Git repository and initializes it if necessary. // InitGitRepo checks a Git repository and initializes it if necessary.
@ -44,7 +46,7 @@ func InitGitRepo() {
} }
} }
if !isGitRepo { if !isGitRepo {
log.Println("Initializing Git repo at", files.HyphaeDir()) slog.Info("Initializing Git repo", "path", files.HyphaeDir())
gitsh("init") gitsh("init")
gitsh("config", "core.quotePath", "false") gitsh("config", "core.quotePath", "false")
} }
@ -60,7 +62,7 @@ func gitsh(args ...string) (out bytes.Buffer, err error) {
b, err := cmd.CombinedOutput() b, err := cmd.CombinedOutput()
if err != nil { if err != nil {
log.Println("gitsh:", err) slog.Info("Git command failed", "err", err, "output", string(b))
} }
return *bytes.NewBuffer(b), err return *bytes.NewBuffer(b), err
} }
@ -77,7 +79,9 @@ func silentGitsh(args ...string) (out bytes.Buffer, err error) {
// Rename renames from `from` to `to` using `git mv`. // Rename renames from `from` to `to` using `git mv`.
func Rename(from, to string) error { func Rename(from, to string) error {
log.Println(util.ShorterPath(from), util.ShorterPath(to)) slog.Info("Renaming file with git mv",
"from", util.ShorterPath(from),
"to", util.ShorterPath(to))
_, err := gitsh("mv", "--force", from, to) _, err := gitsh("mv", "--force", from, to)
return err return err
} }

View File

@ -4,19 +4,21 @@ package histweb
import ( import (
"embed" "embed"
"fmt" "fmt"
"github.com/bouncepaw/mycorrhiza/history"
"github.com/bouncepaw/mycorrhiza/internal/cfg"
"github.com/bouncepaw/mycorrhiza/internal/files"
hyphae2 "github.com/bouncepaw/mycorrhiza/internal/hyphae"
"github.com/bouncepaw/mycorrhiza/util"
viewutil2 "github.com/bouncepaw/mycorrhiza/web/viewutil"
"github.com/gorilla/mux"
"html/template" "html/template"
"log" "log/slog"
"net/http" "net/http"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings" "strings"
"github.com/bouncepaw/mycorrhiza/history"
"github.com/bouncepaw/mycorrhiza/internal/cfg"
"github.com/bouncepaw/mycorrhiza/internal/files"
"github.com/bouncepaw/mycorrhiza/internal/hyphae"
"github.com/bouncepaw/mycorrhiza/util"
"github.com/bouncepaw/mycorrhiza/web/viewutil"
"github.com/gorilla/mux"
) )
func InitHandlers(rtr *mux.Router) { func InitHandlers(rtr *mux.Router) {
@ -30,9 +32,9 @@ func InitHandlers(rtr *mux.Router) {
rtr.HandleFunc("/recent-changes-atom", handlerRecentChangesAtom) rtr.HandleFunc("/recent-changes-atom", handlerRecentChangesAtom)
rtr.HandleFunc("/recent-changes-json", handlerRecentChangesJSON) rtr.HandleFunc("/recent-changes-json", handlerRecentChangesJSON)
chainPrimitiveDiff = viewutil2.CopyEnRuWith(fs, "view_primitive_diff.html", ruTranslation) chainPrimitiveDiff = viewutil.CopyEnRuWith(fs, "view_primitive_diff.html", ruTranslation)
chainRecentChanges = viewutil2.CopyEnRuWith(fs, "view_recent_changes.html", ruTranslation) chainRecentChanges = viewutil.CopyEnRuWith(fs, "view_recent_changes.html", ruTranslation)
chainHistory = viewutil2.CopyEnRuWith(fs, "view_history.html", ruTranslation) chainHistory = viewutil.CopyEnRuWith(fs, "view_history.html", ruTranslation)
} }
func handlerPrimitiveDiff(w http.ResponseWriter, rq *http.Request) { func handlerPrimitiveDiff(w http.ResponseWriter, rq *http.Request) {
@ -45,12 +47,12 @@ func handlerPrimitiveDiff(w http.ResponseWriter, rq *http.Request) {
} }
var ( var (
mycoFilePath string mycoFilePath string
h = hyphae2.ByName(util.CanonicalName(slug)) h = hyphae.ByName(util.CanonicalName(slug))
) )
switch h := h.(type) { switch h := h.(type) {
case hyphae2.ExistingHypha: case hyphae.ExistingHypha:
mycoFilePath = h.TextFilePath() mycoFilePath = h.TextFilePath()
case *hyphae2.EmptyHypha: case *hyphae.EmptyHypha:
mycoFilePath = filepath.Join(files.HyphaeDir(), h.CanonicalName()+".myco") mycoFilePath = filepath.Join(files.HyphaeDir(), h.CanonicalName()+".myco")
} }
text, err := history.PrimitiveDiffAtRevision(mycoFilePath, revHash) text, err := history.PrimitiveDiffAtRevision(mycoFilePath, revHash)
@ -58,7 +60,7 @@ func handlerPrimitiveDiff(w http.ResponseWriter, rq *http.Request) {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)
return return
} }
primitiveDiff(viewutil2.MetaFrom(w, rq), h, revHash, text) primitiveDiff(viewutil.MetaFrom(w, rq), h, revHash, text)
} }
// handlerRecentChanges displays the /recent-changes/ page. // handlerRecentChanges displays the /recent-changes/ page.
@ -68,7 +70,7 @@ func handlerRecentChanges(w http.ResponseWriter, rq *http.Request) {
if editCount > 100 { if editCount > 100 {
return return
} }
recentChanges(viewutil2.MetaFrom(w, rq), editCount, history.RecentChanges(editCount)) recentChanges(viewutil.MetaFrom(w, rq), editCount, history.RecentChanges(editCount))
} }
// handlerHistory lists all revisions of a hypha. // handlerHistory lists all revisions of a hypha.
@ -81,9 +83,11 @@ func handlerHistory(w http.ResponseWriter, rq *http.Request) {
if err == nil { if err == nil {
list = history.WithRevisions(hyphaName, revs) list = history.WithRevisions(hyphaName, revs)
} }
log.Println("Found", len(revs), "revisions for", hyphaName)
historyView(viewutil2.MetaFrom(w, rq), hyphaName, list) // TODO: extra log, not needed?
slog.Info("Found revisions", "hyphaName", hyphaName, "n", len(revs), "err", err)
historyView(viewutil.MetaFrom(w, rq), hyphaName, list)
} }
// genericHandlerOfFeeds is a helper function for the web feed handlers. // genericHandlerOfFeeds is a helper function for the web feed handlers.
@ -135,20 +139,20 @@ var (
{{define "n recent changes"}}{{.}} свеж{{if eq . 1}}ая правка{{else if le . 4}}их правок{{else}}их правок{{end}}{{end}} {{define "n recent changes"}}{{.}} свеж{{if eq . 1}}ая правка{{else if le . 4}}их правок{{else}}их правок{{end}}{{end}}
{{define "recent empty"}}Правки не найдены.{{end}} {{define "recent empty"}}Правки не найдены.{{end}}
` `
chainPrimitiveDiff, chainRecentChanges, chainHistory viewutil2.Chain chainPrimitiveDiff, chainRecentChanges, chainHistory viewutil.Chain
) )
type recentChangesData struct { type recentChangesData struct {
*viewutil2.BaseData *viewutil.BaseData
EditCount int EditCount int
Changes []history.Revision Changes []history.Revision
UserHypha string UserHypha string
Stops []int Stops []int
} }
func recentChanges(meta viewutil2.Meta, editCount int, changes []history.Revision) { func recentChanges(meta viewutil.Meta, editCount int, changes []history.Revision) {
viewutil2.ExecutePage(meta, chainRecentChanges, recentChangesData{ viewutil.ExecutePage(meta, chainRecentChanges, recentChangesData{
BaseData: &viewutil2.BaseData{}, BaseData: &viewutil.BaseData{},
EditCount: editCount, EditCount: editCount,
Changes: changes, Changes: changes,
UserHypha: cfg.UserHypha, UserHypha: cfg.UserHypha,
@ -157,13 +161,13 @@ func recentChanges(meta viewutil2.Meta, editCount int, changes []history.Revisio
} }
type primitiveDiffData struct { type primitiveDiffData struct {
*viewutil2.BaseData *viewutil.BaseData
HyphaName string HyphaName string
Hash string Hash string
Text template.HTML Text template.HTML
} }
func primitiveDiff(meta viewutil2.Meta, h hyphae2.Hypha, hash, text string) { func primitiveDiff(meta viewutil.Meta, h hyphae.Hypha, hash, text string) {
hunks := history.SplitPrimitiveDiff(text) hunks := history.SplitPrimitiveDiff(text)
if len(hunks) > 0 { if len(hunks) > 0 {
var buf strings.Builder var buf strings.Builder
@ -198,8 +202,8 @@ func primitiveDiff(meta viewutil2.Meta, h hyphae2.Hypha, hash, text string) {
text = fmt.Sprintf( text = fmt.Sprintf(
`<pre class="codeblock"><code>%s</code></pre>`, text) `<pre class="codeblock"><code>%s</code></pre>`, text)
} }
viewutil2.ExecutePage(meta, chainPrimitiveDiff, primitiveDiffData{ viewutil.ExecutePage(meta, chainPrimitiveDiff, primitiveDiffData{
BaseData: &viewutil2.BaseData{}, BaseData: &viewutil.BaseData{},
HyphaName: h.CanonicalName(), HyphaName: h.CanonicalName(),
Hash: hash, Hash: hash,
Text: template.HTML(text), Text: template.HTML(text),
@ -207,14 +211,14 @@ func primitiveDiff(meta viewutil2.Meta, h hyphae2.Hypha, hash, text string) {
} }
type historyData struct { type historyData struct {
*viewutil2.BaseData *viewutil.BaseData
HyphaName string HyphaName string
Contents string Contents string
} }
func historyView(meta viewutil2.Meta, hyphaName, contents string) { func historyView(meta viewutil.Meta, hyphaName, contents string) {
viewutil2.ExecutePage(meta, chainHistory, historyData{ viewutil.ExecutePage(meta, chainHistory, historyData{
BaseData: &viewutil2.BaseData{ BaseData: &viewutil.BaseData{
Addr: "/history/" + util.CanonicalName(hyphaName), Addr: "/history/" + util.CanonicalName(hyphaName),
}, },
HyphaName: hyphaName, HyphaName: hyphaName,

View File

@ -4,11 +4,11 @@ package history
// Things related to writing history. // Things related to writing history.
import ( import (
"fmt" "fmt"
"github.com/bouncepaw/mycorrhiza/internal/user"
"os" "os"
"path/filepath" "path/filepath"
"sync" "sync"
"github.com/bouncepaw/mycorrhiza/internal/user"
"github.com/bouncepaw/mycorrhiza/util" "github.com/bouncepaw/mycorrhiza/util"
) )

View File

@ -2,7 +2,8 @@ package history
import ( import (
"fmt" "fmt"
"log" "log/slog"
"os"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
@ -11,9 +12,11 @@ import (
"github.com/bouncepaw/mycorrhiza/internal/files" "github.com/bouncepaw/mycorrhiza/internal/files"
) )
// Revision represents a revision, duh. Hash is usually short. Username is extracted from email. // Revision represents a revision of a hypha.
type Revision struct { type Revision struct {
// Hash is usually short.
Hash string Hash string
// Username is extracted from email.
Username string Username string
Time time.Time Time time.Time
Message string Message string
@ -71,7 +74,9 @@ func (stream *recentChangesStream) next(n int) []Revision {
res, err := gitLog(args...) res, err := gitLog(args...)
if err != nil { if err != nil {
log.Fatal(err) // TODO: return error
slog.Error("Failed to git log", "err", err)
os.Exit(1)
} }
if len(res) != 0 { if len(res) != 0 {
stream.currHash = res[len(res)-1].Hash stream.currHash = res[len(res)-1].Hash
@ -103,14 +108,14 @@ func (stream recentChangesStream) iterator() func() (Revision, bool) {
func RecentChanges(n int) []Revision { func RecentChanges(n int) []Revision {
stream := newRecentChangesStream() stream := newRecentChangesStream()
revs := stream.next(n) revs := stream.next(n)
log.Printf("Found %d recent changes", len(revs)) slog.Info("Found recent changes", "n", len(revs))
return revs return revs
} }
// Revisions returns slice of revisions for the given hypha name, ordered most recent first. // Revisions returns slice of revisions for the given hypha name, ordered most recent first.
func Revisions(hyphaName string) ([]Revision, error) { func Revisions(hyphaName string) ([]Revision, error) {
revs, err := gitLog("--", hyphaName+".*") revs, err := gitLog("--", hyphaName+".*")
log.Printf("Found %d revisions for %s\n", len(revs), hyphaName) slog.Info("Found revisions", "hyphaName", hyphaName, "n", len(revs), "err", err)
return revs, err return revs, err
} }

14
main.go
View File

@ -5,13 +5,9 @@
package main package main
import ( import (
"github.com/bouncepaw/mycorrhiza/internal/categories"
"log"
"os"
"syscall"
"github.com/bouncepaw/mycorrhiza/history" "github.com/bouncepaw/mycorrhiza/history"
"github.com/bouncepaw/mycorrhiza/internal/backlinks" "github.com/bouncepaw/mycorrhiza/internal/backlinks"
"github.com/bouncepaw/mycorrhiza/internal/categories"
"github.com/bouncepaw/mycorrhiza/internal/cfg" "github.com/bouncepaw/mycorrhiza/internal/cfg"
"github.com/bouncepaw/mycorrhiza/internal/files" "github.com/bouncepaw/mycorrhiza/internal/files"
"github.com/bouncepaw/mycorrhiza/internal/hyphae" "github.com/bouncepaw/mycorrhiza/internal/hyphae"
@ -23,6 +19,8 @@ import (
"github.com/bouncepaw/mycorrhiza/web" "github.com/bouncepaw/mycorrhiza/web"
"github.com/bouncepaw/mycorrhiza/web/static" "github.com/bouncepaw/mycorrhiza/web/static"
"github.com/bouncepaw/mycorrhiza/web/viewutil" "github.com/bouncepaw/mycorrhiza/web/viewutil"
"log"
"os"
) )
func main() { func main() {
@ -48,7 +46,9 @@ func main() {
backlinks.IndexBacklinks() backlinks.IndexBacklinks()
go backlinks.RunBacklinksConveyor() go backlinks.RunBacklinksConveyor()
user.InitUserDatabase() user.InitUserDatabase()
history.Start() if err := history.Start(); err != nil {
os.Exit(1)
}
history.InitGitRepo() history.InitGitRepo()
migration.MigrateRocketsMaybe() migration.MigrateRocketsMaybe()
migration.MigrateHeadingsMaybe() migration.MigrateHeadingsMaybe()
@ -65,6 +65,6 @@ func main() {
err := serveHTTP(web.Handler()) err := serveHTTP(web.Handler())
if err != nil { if err != nil {
syscall.Exit(1) os.Exit(1)
} }
} }