mirror of
https://github.com/osmarks/mycorrhiza.git
synced 2025-04-04 17:57:05 +00:00
Make commits on text edits, commits on binary edits WIP
This commit is contained in:
parent
b52419152a
commit
c03f8be5cd
@ -11,24 +11,14 @@ import (
|
||||
"github.com/go-git/go-git/v5/plumbing/object"
|
||||
)
|
||||
|
||||
/*
|
||||
// 10 should be enough
|
||||
const ShortHashLength = 10
|
||||
|
||||
type EditType int
|
||||
type OpType int
|
||||
|
||||
const (
|
||||
TypeRename EditType = iota
|
||||
TypeDelete
|
||||
TypeNone OpType = iota
|
||||
TypeEditText
|
||||
TypeEditBinary
|
||||
)
|
||||
|
||||
type Revision struct {
|
||||
ShortHash [ShortHashLength]byte
|
||||
Type EditType
|
||||
}*/
|
||||
|
||||
var WikiRepo *git.Repository
|
||||
var Worktree *git.Worktree
|
||||
|
||||
@ -47,11 +37,20 @@ func Start(wikiDir string) {
|
||||
|
||||
type HistoryOp struct {
|
||||
Errs []error
|
||||
opType OpType
|
||||
userMsg string
|
||||
signature *object.Signature
|
||||
isDone bool
|
||||
}
|
||||
|
||||
func Operation(opType OpType) *HistoryOp {
|
||||
hop := &HistoryOp{
|
||||
Errs: []error{},
|
||||
opType: opType,
|
||||
}
|
||||
return hop
|
||||
}
|
||||
|
||||
// WithFiles stages all passed `paths`. Paths can be rooted or not.
|
||||
func (hop *HistoryOp) WithFiles(paths ...string) *HistoryOp {
|
||||
for _, path := range paths {
|
||||
@ -63,8 +62,8 @@ func (hop *HistoryOp) WithFiles(paths ...string) *HistoryOp {
|
||||
return hop
|
||||
}
|
||||
|
||||
// WithUserMsg sets what user message will be used for the future commit. If it == "", a default one be used. If user messages are not supported for this one type of history operation, this user message will be dropped. If user messages exceeds one line, it is stripped down.
|
||||
func (hop *HistoryOp) WithUserMsg(userMsg string) *HistoryOp {
|
||||
// WithMsg sets what message will be used for the future commit. If user message exceeds one line, it is stripped down.
|
||||
func (hop *HistoryOp) WithMsg(userMsg string) *HistoryOp {
|
||||
// Isn't it too imperative?
|
||||
var firstLine string
|
||||
for _, ch := range userMsg {
|
||||
@ -73,7 +72,7 @@ func (hop *HistoryOp) WithUserMsg(userMsg string) *HistoryOp {
|
||||
}
|
||||
firstLine += string(ch)
|
||||
}
|
||||
hop.userMsg = userMsg
|
||||
hop.userMsg = firstLine
|
||||
return hop
|
||||
}
|
||||
|
||||
@ -108,12 +107,10 @@ func (hop *HistoryOp) Apply() *HistoryOp {
|
||||
return hop
|
||||
}
|
||||
|
||||
func CommitTest() {
|
||||
(&HistoryOp{}).
|
||||
WithUserMsg("This is a test commit").
|
||||
WithSignature("wikimind").
|
||||
Apply()
|
||||
log.Println("Made a test commit")
|
||||
func Rename(from, to string) error {
|
||||
log.Println(util.ShorterPath(from), util.ShorterPath(to))
|
||||
_, err := Worktree.Move(util.ShorterPath(from), util.ShorterPath(to))
|
||||
return err
|
||||
}
|
||||
|
||||
func StatusTable() (html string) {
|
||||
|
@ -7,7 +7,8 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
// "github.com/bouncepaw/mycorrhiza/history"
|
||||
|
||||
"github.com/bouncepaw/mycorrhiza/history"
|
||||
)
|
||||
|
||||
func init() {
|
||||
@ -92,7 +93,11 @@ func handlerUploadText(w http.ResponseWriter, rq *http.Request) {
|
||||
hyphaData.textType = TextGemini
|
||||
hyphaData.textPath = fullPath
|
||||
}
|
||||
// TODO: make history changes
|
||||
history.Operation(history.TypeEditText).
|
||||
WithFiles(fullPath).
|
||||
WithMsg(fmt.Sprintf("Edit ‘%s’", hyphaName)).
|
||||
WithSignature("anon").
|
||||
Apply()
|
||||
http.Redirect(w, rq, "/page/"+hyphaName, http.StatusSeeOther)
|
||||
}
|
||||
|
||||
@ -141,13 +146,20 @@ func handlerUploadBinary(w http.ResponseWriter, rq *http.Request) {
|
||||
}
|
||||
} else {
|
||||
if hyphaData.binaryPath != fullPath {
|
||||
if err := os.Remove(hyphaData.binaryPath); err != nil {
|
||||
if err := history.Rename(hyphaData.binaryPath, fullPath); err != nil {
|
||||
log.Println(err)
|
||||
} else {
|
||||
log.Println("Moved", hyphaData.binaryPath, "to", fullPath)
|
||||
}
|
||||
}
|
||||
hyphaData.binaryPath = fullPath
|
||||
hyphaData.binaryType = mimeType
|
||||
}
|
||||
log.Println("Written", len(data), "of binary data for", hyphaName, "to path", fullPath)
|
||||
history.Operation(history.TypeEditText).
|
||||
WithFiles(fullPath).
|
||||
WithMsg(fmt.Sprintf("Upload binary part for ‘%s’ with type ‘%s’", hyphaName, mimeType.Mime())).
|
||||
WithSignature("anon").
|
||||
Apply()
|
||||
http.Redirect(w, rq, "/page/"+hyphaName, http.StatusSeeOther)
|
||||
}
|
||||
|
9
main.go
9
main.go
@ -121,14 +121,6 @@ func handlerStatus(w http.ResponseWriter, rq *http.Request) {
|
||||
w.Write([]byte(history.StatusTable()))
|
||||
}
|
||||
|
||||
func handlerCommitTest(w http.ResponseWriter, rq *http.Request) {
|
||||
log.Println(rq.URL)
|
||||
w.Header().Set("Content-Type", "text/html;charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
history.CommitTest()
|
||||
w.Write([]byte("if you are here, a commit has been done"))
|
||||
}
|
||||
|
||||
func main() {
|
||||
log.Println("Running MycorrhizaWiki β")
|
||||
|
||||
@ -152,7 +144,6 @@ func main() {
|
||||
http.HandleFunc("/reindex", handlerReindex)
|
||||
http.HandleFunc("/git/list", handlerListCommits)
|
||||
http.HandleFunc("/git/status", handlerStatus)
|
||||
http.HandleFunc("/git/commit", handlerCommitTest)
|
||||
http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, rq *http.Request) {
|
||||
http.ServeFile(w, rq, WikiDir+"/static/favicon.ico")
|
||||
})
|
||||
|
@ -1 +1 @@
|
||||
Subproject commit b0157a17f535e99e485c8d3e85f66995754badcf
|
||||
Subproject commit 30287be4496638e342878e2adf50e0a74ce71a5f
|
Loading…
x
Reference in New Issue
Block a user