Fix primitive diffs for deleted hyphae

This commit is contained in:
Timur Ismagilov 2022-05-31 13:33:25 +03:00
parent cbf7ae50d3
commit 9e99e8da11
2 changed files with 21 additions and 12 deletions

View File

@ -6,6 +6,7 @@ import (
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"github.com/bouncepaw/mycorrhiza/cfg" "github.com/bouncepaw/mycorrhiza/cfg"
"github.com/bouncepaw/mycorrhiza/files"
"github.com/bouncepaw/mycorrhiza/history" "github.com/bouncepaw/mycorrhiza/history"
"github.com/bouncepaw/mycorrhiza/hyphae" "github.com/bouncepaw/mycorrhiza/hyphae"
"github.com/bouncepaw/mycorrhiza/util" "github.com/bouncepaw/mycorrhiza/util"
@ -13,6 +14,7 @@ import (
"github.com/gorilla/mux" "github.com/gorilla/mux"
"log" "log"
"net/http" "net/http"
"path/filepath"
"strconv" "strconv"
"strings" "strings"
) )
@ -49,17 +51,22 @@ func handlerPrimitiveDiff(w http.ResponseWriter, rq *http.Request) {
http.Error(w, "403 bad request", http.StatusBadRequest) http.Error(w, "403 bad request", http.StatusBadRequest)
return return
} }
switch h := hyphae.ByName(util.CanonicalName(slug)).(type) { var (
case *hyphae.EmptyHypha: mycoFilePath string
http.Error(w, "404 not found", http.StatusNotFound) h = hyphae.ByName(util.CanonicalName(slug))
)
switch h := h.(type) {
case hyphae.ExistingHypha: case hyphae.ExistingHypha:
text, err := history.PrimitiveDiffAtRevision(h.TextFilePath(), revHash) mycoFilePath = h.TextFilePath()
if err != nil { case *hyphae.EmptyHypha:
http.Error(w, err.Error(), http.StatusInternalServerError) mycoFilePath = filepath.Join(files.HyphaeDir(), h.CanonicalName()+".myco")
return
}
primitiveDiff(viewutil.MetaFrom(w, rq), h, revHash, text)
} }
text, err := history.PrimitiveDiffAtRevision(mycoFilePath, revHash)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
primitiveDiff(viewutil.MetaFrom(w, rq), h, revHash, text)
} }
// handlerRecentChanges displays the /recent-changes/ page. // handlerRecentChanges displays the /recent-changes/ page.
@ -163,7 +170,7 @@ type primitiveDiffData struct {
Text string Text string
} }
func primitiveDiff(meta viewutil.Meta, h hyphae.ExistingHypha, hash, text string) { func primitiveDiff(meta viewutil.Meta, h hyphae.Hypha, hash, text string) {
viewutil.ExecutePage(meta, chainPrimitiveDiff, primitiveDiffData{ viewutil.ExecutePage(meta, chainPrimitiveDiff, primitiveDiffData{
BaseData: &viewutil.BaseData{}, BaseData: &viewutil.BaseData{},
HyphaName: h.CanonicalName(), HyphaName: h.CanonicalName(),

View File

@ -104,13 +104,15 @@ func handlerRevision(w http.ResponseWriter, rq *http.Request) {
var ( var (
textContents string textContents string
err error err error
mycoFilePath string
) )
switch h := h.(type) { switch h := h.(type) {
case hyphae.ExistingHypha: case hyphae.ExistingHypha:
textContents, err = history.FileAtRevision(h.TextFilePath(), revHash) mycoFilePath = h.TextFilePath()
case *hyphae.EmptyHypha: case *hyphae.EmptyHypha:
textContents, err = history.FileAtRevision(filepath.Join(files.HyphaeDir(), h.CanonicalName()+".myco"), revHash) mycoFilePath = filepath.Join(files.HyphaeDir(), h.CanonicalName()+".myco")
} }
textContents, err = history.FileAtRevision(mycoFilePath, revHash)
if err == nil { if err == nil {
ctx, _ := mycocontext.ContextFromStringInput(textContents, shroom.MarkupOptions(hyphaName)) ctx, _ := mycocontext.ContextFromStringInput(textContents, shroom.MarkupOptions(hyphaName))
contents = mycomarkup.BlocksToHTML(ctx, mycomarkup.BlockTree(ctx)) contents = mycomarkup.BlocksToHTML(ctx, mycomarkup.BlockTree(ctx))