diff --git a/history/history.go b/history/history.go index f905dd6..dbe3010 100644 --- a/history/history.go +++ b/history/history.go @@ -5,6 +5,7 @@ import ( "log" "time" + "github.com/bouncepaw/mycorrhiza/util" "github.com/go-git/go-git/v5" // "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/object" @@ -44,32 +45,74 @@ func Start(wikiDir string) { log.Println("Wiki repository found") } -func Stage(path string) error { - var err error - _, err = Worktree.Add(path) - if err != nil { - log.Println(err) +type HistoryOp struct { + Errs []error + userMsg string + signature *object.Signature + isDone bool +} + +// WithFiles stages all passed `paths`. Paths can be rooted or not. +func (hop *HistoryOp) WithFiles(paths ...string) *HistoryOp { + for _, path := range paths { + if _, err := Worktree.Add(util.ShorterPath(path)); err != nil { + log.Println(err) + hop.Errs = append(hop.Errs, err) + } } - return err + 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 { + // Isn't it too imperative? + var firstLine string + for _, ch := range userMsg { + if ch == '\r' || ch == '\n' { + break + } + firstLine += string(ch) + } + hop.userMsg = userMsg + return hop +} + +// WithSignature sets a signature for the future commit. You need to pass a username only, the rest is upon us (including email and time). +func (hop *HistoryOp) WithSignature(username string) *HistoryOp { + hop.signature = &object.Signature{ + Name: username, + // A fake email, why not + Email: username + "@mycorrhiza", + When: time.Now(), + } + return hop +} + +// Apply applies history operation by doing the commit. You can't apply the same operation more than once. +func (hop *HistoryOp) Apply() *HistoryOp { + if !hop.isDone { + opts := &git.CommitOptions{ + All: false, + Author: hop.signature, + } + err := opts.Validate(WikiRepo) + if err != nil { + hop.Errs = append(hop.Errs, err) + } + // TODO: work on this section: + _, err = Worktree.Commit(hop.userMsg, opts) + if err != nil { + hop.Errs = append(hop.Errs, err) + } + } + return hop } func CommitTest() { - opts := &git.CommitOptions{ - All: false, - Author: &object.Signature{ - Name: "wikimind", - Email: "wikimind@thiswiki", - When: time.Now(), - }, - } - err := opts.Validate(WikiRepo) - if err != nil { - log.Fatal(err) - } - _, err = Worktree.Commit("This is a test commit", opts) - if err != nil { - log.Fatal(err) - } + (&HistoryOp{}). + WithUserMsg("This is a test commit"). + WithSignature("wikimind"). + Apply() log.Println("Made a test commit") } diff --git a/http_mutators.go b/http_mutators.go index d67976a..71de895 100644 --- a/http_mutators.go +++ b/http_mutators.go @@ -7,8 +7,7 @@ import ( "net/http" "os" "path/filepath" - - "github.com/bouncepaw/mycorrhiza/history" + // "github.com/bouncepaw/mycorrhiza/history" ) func init() { @@ -93,7 +92,7 @@ func handlerUploadText(w http.ResponseWriter, rq *http.Request) { hyphaData.textType = TextGemini hyphaData.textPath = fullPath } - history.Stage(shorterPath(fullPath)) + // TODO: make history changes http.Redirect(w, rq, "/page/"+hyphaName, http.StatusSeeOther) } diff --git a/main.go b/main.go index 84d4943..55c32a6 100644 --- a/main.go +++ b/main.go @@ -7,9 +7,9 @@ import ( "os" "path/filepath" "regexp" - "strings" "github.com/bouncepaw/mycorrhiza/history" + "github.com/bouncepaw/mycorrhiza/util" ) // WikiDir is a rooted path to the wiki storage directory. @@ -38,14 +38,8 @@ func HttpErr(w http.ResponseWriter, status int, name, title, errMsg string) { errMsg, name))) } -// shorterPath is used by handlerList to display shorter path to the files. It simply strips WikiDir. -func shorterPath(fullPath string) string { - tmp := strings.TrimPrefix(fullPath, WikiDir) - if tmp == "" { - return "" - } - return tmp[1:] -} +// shorterPath is used by handlerList to display shorter path to the files. It simply strips WikiDir. It was moved to util package, this is an alias. TODO: demolish. +var shorterPath = util.ShorterPath // Show all hyphae func handlerList(w http.ResponseWriter, rq *http.Request) { @@ -140,6 +134,7 @@ func main() { var err error WikiDir, err = filepath.Abs(os.Args[1]) + util.WikiDir = WikiDir if err != nil { log.Fatal(err) } diff --git a/util/util.go b/util/util.go new file mode 100644 index 0000000..f45ef9a --- /dev/null +++ b/util/util.go @@ -0,0 +1,19 @@ +package util + +import ( + "strings" +) + +var WikiDir string + +// ShorterPath is used by handlerList to display shorter path to the files. It simply strips WikiDir. +func ShorterPath(path string) string { + if strings.HasPrefix(path, WikiDir) { + tmp := strings.TrimPrefix(path, WikiDir) + if tmp == "" { + return "" + } + return tmp[1:] + } + return path +}