mirror of
https://github.com/osmarks/mycorrhiza.git
synced 2024-12-11 21:10:26 +00:00
Do not depend on go-git anymore, clean history code a little
This commit is contained in:
parent
f33d7598ac
commit
aeee451044
2
go.mod
2
go.mod
@ -1,5 +1,3 @@
|
||||
module github.com/bouncepaw/mycorrhiza
|
||||
|
||||
go 1.14
|
||||
|
||||
require github.com/go-git/go-git/v5 v5.1.0
|
||||
|
62
go.sum
62
go.sum
@ -1,62 +0,0 @@
|
||||
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7/go.mod h1:6zEj6s6u/ghQa61ZWa/C2Aw3RkjiTBOix7dkqa1VLIs=
|
||||
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
|
||||
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
|
||||
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg=
|
||||
github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o=
|
||||
github.com/flynn/go-shlex v0.0.0-20150515145356-3f9db97f8568/go.mod h1:xEzjJPgXI435gkrCt3MPfRiAkVrwSbHsst4LCFVfpJc=
|
||||
github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
|
||||
github.com/go-git/gcfg v1.5.0 h1:Q5ViNfGF8zFgyJWPqYwA7qGFoMTEiBmdlkcfRmpIMa4=
|
||||
github.com/go-git/gcfg v1.5.0/go.mod h1:5m20vg6GwYabIxaOonVkTdrILxQMpEShl1xiMF4ua+E=
|
||||
github.com/go-git/go-billy/v5 v5.0.0 h1:7NQHvd9FVid8VL4qVUMm8XifBK+2xCoZ2lSk0agRrHM=
|
||||
github.com/go-git/go-billy/v5 v5.0.0/go.mod h1:pmpqyWchKfYfrkb/UVH4otLvyi/5gJlGI4Hb3ZqZ3W0=
|
||||
github.com/go-git/go-git-fixtures/v4 v4.0.1/go.mod h1:m+ICp2rF3jDhFgEZ/8yziagdT1C+ZpZcrJjappBCDSw=
|
||||
github.com/go-git/go-git/v5 v5.1.0 h1:HxJn9g/E7eYvKW3Fm7Jt4ee8LXfPOm/H1cdDu8vEssk=
|
||||
github.com/go-git/go-git/v5 v5.1.0/go.mod h1:ZKfuPUoY1ZqIG4QG9BDBh3G4gLM5zvPuSJAozQrZuyM=
|
||||
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||
github.com/imdario/mergo v0.3.9 h1:UauaLniWCFHWd+Jp9oCEkTBj8VO/9DKg3PV3VCNMDIg=
|
||||
github.com/imdario/mergo v0.3.9/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
|
||||
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
|
||||
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
|
||||
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
|
||||
github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd h1:Coekwdh0v2wtGp9Gmz1Ze3eVRAWJMLokvN3QjdzCHLY=
|
||||
github.com/kevinburke/ssh_config v0.0.0-20190725054713-01f96b0aa0cd/go.mod h1:CT57kijsi8u/K/BOFA39wgDQJ9CxiF4nAY/ojJ6r6mM=
|
||||
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
|
||||
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
|
||||
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
|
||||
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
|
||||
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
|
||||
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
|
||||
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
github.com/xanzy/ssh-agent v0.2.1 h1:TCbipTQL2JiiCprBWx9frJ2eJlCYT00NmctrHxVAr70=
|
||||
github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4=
|
||||
golang.org/x/crypto v0.0.0-20190219172222-a4c6cb3142f2/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073 h1:xMPOj6Pz6UipU1wXLkrtqpHbR0AVFnyPEQq/wRWz9lM=
|
||||
golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20200301022130-244492dfa37a h1:GuSPYbZzB5/dcLNCwLQLsg3obCJtX9IJhpXkvY7kzk0=
|
||||
golang.org/x/net v0.0.0-20200301022130-244492dfa37a/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190221075227-b4e8571b14e0/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527 h1:uYVVQ9WP/Ds2ROhcaGPeIdVq0RIXVLwsHlnvJ+cT1So=
|
||||
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
|
||||
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
@ -1,58 +0,0 @@
|
||||
package history
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"log"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/bouncepaw/mycorrhiza/util"
|
||||
)
|
||||
|
||||
// Revision represents a revision, duh. Hash is usually short. Username is extracted from email.
|
||||
type Revision struct {
|
||||
Hash string
|
||||
Username string
|
||||
Time time.Time
|
||||
Message string
|
||||
}
|
||||
|
||||
// Path to git executable. Set at init()
|
||||
var gitpath string
|
||||
|
||||
func init() {
|
||||
path, err := exec.LookPath("git")
|
||||
if err != nil {
|
||||
log.Fatal("Cound not find the git executable. Check your $PATH.")
|
||||
} else {
|
||||
log.Println("Git path is", path)
|
||||
}
|
||||
gitpath = path
|
||||
|
||||
}
|
||||
|
||||
// I pronounce it as [gɪt͡ʃ].
|
||||
func gitsh(args ...string) (out bytes.Buffer, err error) {
|
||||
fmt.Printf("$ %v\n", args)
|
||||
cmd := exec.Command(gitpath, args...)
|
||||
|
||||
cmd.Dir = util.WikiDir
|
||||
|
||||
b, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
log.Println("gitsh:", err)
|
||||
}
|
||||
return *bytes.NewBuffer(b), err
|
||||
}
|
||||
|
||||
// Convert a UNIX timestamp as string into a time. If nil is returned, it means that the timestamp could not be converted.
|
||||
func unixTimestampAsTime(ts string) *time.Time {
|
||||
i, err := strconv.ParseInt(ts, 10, 64)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
tm := time.Unix(i, 0)
|
||||
return &tm
|
||||
}
|
@ -1,137 +1,77 @@
|
||||
package history
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"log"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/bouncepaw/mycorrhiza/util"
|
||||
"github.com/go-git/go-git/v5"
|
||||
"github.com/go-git/go-git/v5/plumbing/object"
|
||||
)
|
||||
|
||||
type OpType int
|
||||
|
||||
const (
|
||||
TypeNone OpType = iota
|
||||
TypeEditText
|
||||
TypeEditBinary
|
||||
)
|
||||
|
||||
var WikiRepo *git.Repository
|
||||
var Worktree *git.Worktree
|
||||
|
||||
// Start initializes git credentials.
|
||||
func Start(wikiDir string) {
|
||||
ry, err := git.PlainOpen(wikiDir)
|
||||
_, err := gitsh("config", "user.name", "wikimind")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
WikiRepo = ry
|
||||
Worktree, err = WikiRepo.Worktree()
|
||||
_, err = gitsh("config", "user.email", "wikimind@mycorrhiza")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
gitsh("config", "user.name", "wikimind")
|
||||
gitsh("config", "user.email", "wikimind@mycorrhiza")
|
||||
log.Println("Wiki repository found")
|
||||
}
|
||||
|
||||
type HistoryOp struct {
|
||||
Errs []error
|
||||
opType OpType
|
||||
userMsg string
|
||||
signature *object.Signature
|
||||
name string
|
||||
email string
|
||||
// Revision represents a revision, duh. Hash is usually short. Username is extracted from email.
|
||||
type Revision struct {
|
||||
Hash string
|
||||
Username string
|
||||
Time time.Time
|
||||
Message string
|
||||
}
|
||||
|
||||
func Operation(opType OpType) *HistoryOp {
|
||||
hop := &HistoryOp{
|
||||
Errs: []error{},
|
||||
opType: opType,
|
||||
}
|
||||
return hop
|
||||
}
|
||||
// Path to git executable. Set at init()
|
||||
var gitpath string
|
||||
|
||||
func (hop *HistoryOp) gitop(args ...string) *HistoryOp {
|
||||
out, err := gitsh(args...)
|
||||
fmt.Println("out:", out.String())
|
||||
func init() {
|
||||
path, err := exec.LookPath("git")
|
||||
if err != nil {
|
||||
hop.Errs = append(hop.Errs, err)
|
||||
log.Fatal("Cound not find the git executable. Check your $PATH.")
|
||||
} else {
|
||||
log.Println("Git path is", path)
|
||||
}
|
||||
return hop
|
||||
gitpath = path
|
||||
|
||||
}
|
||||
|
||||
// WithFiles stages all passed `paths`. Paths can be rooted or not.
|
||||
func (hop *HistoryOp) WithFiles(paths ...string) *HistoryOp {
|
||||
for i, path := range paths {
|
||||
paths[i] = util.ShorterPath(path)
|
||||
// I pronounce it as [gɪt͡ʃ].
|
||||
func gitsh(args ...string) (out bytes.Buffer, err error) {
|
||||
fmt.Printf("$ %v\n", args)
|
||||
cmd := exec.Command(gitpath, args...)
|
||||
|
||||
cmd.Dir = util.WikiDir
|
||||
|
||||
b, err := cmd.CombinedOutput()
|
||||
if err != nil {
|
||||
log.Println("gitsh:", err)
|
||||
}
|
||||
return hop.gitop(append([]string{"add"}, paths...)...)
|
||||
return *bytes.NewBuffer(b), err
|
||||
}
|
||||
|
||||
// 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 {
|
||||
for _, ch := range userMsg {
|
||||
if ch == '\r' || ch == '\n' {
|
||||
break
|
||||
}
|
||||
hop.userMsg += string(ch)
|
||||
// Convert a UNIX timestamp as string into a time. If nil is returned, it means that the timestamp could not be converted.
|
||||
func unixTimestampAsTime(ts string) *time.Time {
|
||||
i, err := strconv.ParseInt(ts, 10, 64)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return hop
|
||||
tm := time.Unix(i, 0)
|
||||
return &tm
|
||||
}
|
||||
|
||||
// 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.name = username
|
||||
hop.email = username + "@mycorrhiza" // A fake email, why not
|
||||
return hop
|
||||
}
|
||||
|
||||
// Apply applies history operation by doing the commit.
|
||||
func (hop *HistoryOp) Apply() *HistoryOp {
|
||||
hop.gitop(
|
||||
"commit",
|
||||
"--author='"+hop.name+" <"+hop.email+">'",
|
||||
"--message="+hop.userMsg,
|
||||
)
|
||||
return hop
|
||||
}
|
||||
|
||||
// Rename renames from `from` to `to`. NB. It uses os.Rename internally rather than git.Move because git.Move works wrong for some reason.
|
||||
// Rename renames from `from` to `to` using `git mv`.
|
||||
func Rename(from, to string) error {
|
||||
log.Println(util.ShorterPath(from), util.ShorterPath(to))
|
||||
_, err := gitsh("mv", from, to)
|
||||
return err
|
||||
}
|
||||
|
||||
func StatusTable() (html string) {
|
||||
status, err := Worktree.Status()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
for path, stat := range status {
|
||||
html += fmt.Sprintf(`
|
||||
<tr>
|
||||
<td>%s</td>
|
||||
<td>%v</td>
|
||||
</tr>`, path, stat)
|
||||
}
|
||||
return `<table><tbody>` + html + `</tbody></table>`
|
||||
}
|
||||
|
||||
func CommitsTable() (html string) {
|
||||
commitIter, err := WikiRepo.CommitObjects()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
err = commitIter.ForEach(func(commit *object.Commit) error {
|
||||
html += fmt.Sprintf(`
|
||||
<tr>
|
||||
<td>%v</td>
|
||||
<td>%v</td>
|
||||
<td>%s</td>
|
||||
</tr>`, commit.Hash, commit.Author, commit.Message)
|
||||
return nil
|
||||
})
|
||||
return `<table><tbody>` + html + `</tbody></table>`
|
||||
}
|
||||
|
@ -1,11 +1,15 @@
|
||||
// information.go
|
||||
// Things related to gathering existing information.
|
||||
package history
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Revisions returns slice of revisions for the given hypha name.
|
||||
func Revisions(hyphaName string) ([]Revision, error) {
|
||||
var (
|
||||
out, err = gitsh(
|
||||
@ -18,9 +22,7 @@ func Revisions(hyphaName string) ([]Revision, error) {
|
||||
)
|
||||
if err == nil {
|
||||
for _, line := range strings.Split(out.String(), "\n") {
|
||||
if rev := parseRevisionLine(line); rev != nil {
|
||||
revs = append(revs, *rev)
|
||||
}
|
||||
revs = append(revs, parseRevisionLine(line))
|
||||
}
|
||||
}
|
||||
return revs, err
|
||||
@ -29,29 +31,28 @@ func Revisions(hyphaName string) ([]Revision, error) {
|
||||
// This regex is wrapped in "". For some reason, these quotes appear at some time and we have to get rid of them.
|
||||
var revisionLinePattern = regexp.MustCompile("\"(.*)\t(.*)@.*\t(.*)\t(.*)\"")
|
||||
|
||||
func parseRevisionLine(line string) *Revision {
|
||||
var (
|
||||
results = revisionLinePattern.FindStringSubmatch(line)
|
||||
rev = Revision{
|
||||
Hash: results[1],
|
||||
Username: results[2],
|
||||
Time: *unixTimestampAsTime(results[3]),
|
||||
Message: results[4],
|
||||
}
|
||||
)
|
||||
return &rev
|
||||
func parseRevisionLine(line string) Revision {
|
||||
results := revisionLinePattern.FindStringSubmatch(line)
|
||||
return Revision{
|
||||
Hash: results[1],
|
||||
Username: results[2],
|
||||
Time: *unixTimestampAsTime(results[3]),
|
||||
Message: results[4],
|
||||
}
|
||||
}
|
||||
|
||||
// Represent revision as a table row.
|
||||
func (rev *Revision) AsHtmlTableRow(hyphaName string) string {
|
||||
return fmt.Sprintf(`
|
||||
<tr>
|
||||
<td><time>%s</time></td>
|
||||
<td><a href="/rev/%s/%s">%s</a></td>
|
||||
<td>%s</td>
|
||||
<td><time>%s</time></td>
|
||||
<td>%s</td>
|
||||
</tr>`, rev.Hash, hyphaName, rev.Hash, rev.Username, rev.Time.String(), rev.Message)
|
||||
</tr>`, rev.Time.Format(time.RFC822), rev.Hash, hyphaName, rev.Hash, rev.Username, rev.Message)
|
||||
}
|
||||
|
||||
// See how the file with `filepath` looked at commit with `hash`.
|
||||
func FileAtRevision(filepath, hash string) (string, error) {
|
||||
out, err := gitsh("show", hash+":"+filepath)
|
||||
return out.String(), err
|
84
history/operations.go
Normal file
84
history/operations.go
Normal file
@ -0,0 +1,84 @@
|
||||
// history/operations.go
|
||||
// Things related to writing history.
|
||||
package history
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/bouncepaw/mycorrhiza/util"
|
||||
)
|
||||
|
||||
// OpType is the type a history operation has. Callers shall set appropriate optypes when creating history operations.
|
||||
type OpType int
|
||||
|
||||
const (
|
||||
TypeNone OpType = iota
|
||||
TypeEditText
|
||||
TypeEditBinary
|
||||
)
|
||||
|
||||
// HistoryOp is an object representing a history operation.
|
||||
type HistoryOp struct {
|
||||
// All errors are appended here.
|
||||
Errs []error
|
||||
opType OpType
|
||||
userMsg string
|
||||
name string
|
||||
email string
|
||||
}
|
||||
|
||||
// Operation is a constructor of a history operation.
|
||||
func Operation(opType OpType) *HistoryOp {
|
||||
hop := &HistoryOp{
|
||||
Errs: []error{},
|
||||
opType: opType,
|
||||
}
|
||||
return hop
|
||||
}
|
||||
|
||||
// git operation maker helper
|
||||
func (hop *HistoryOp) gitop(args ...string) *HistoryOp {
|
||||
out, err := gitsh(args...)
|
||||
if err != nil {
|
||||
fmt.Println("out:", out.String())
|
||||
hop.Errs = append(hop.Errs, err)
|
||||
}
|
||||
return hop
|
||||
}
|
||||
|
||||
// WithFiles stages all passed `paths`. Paths can be rooted or not.
|
||||
func (hop *HistoryOp) WithFiles(paths ...string) *HistoryOp {
|
||||
for i, path := range paths {
|
||||
paths[i] = util.ShorterPath(path)
|
||||
}
|
||||
// 1 git operation is more effective than n operations.
|
||||
return hop.gitop(append([]string{"add"}, paths...)...)
|
||||
}
|
||||
|
||||
// Apply applies history operation by doing the commit.
|
||||
func (hop *HistoryOp) Apply() *HistoryOp {
|
||||
hop.gitop(
|
||||
"commit",
|
||||
"--author='"+hop.name+" <"+hop.email+">'",
|
||||
"--message="+hop.userMsg,
|
||||
)
|
||||
return hop
|
||||
}
|
||||
|
||||
// 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 {
|
||||
for _, ch := range userMsg {
|
||||
if ch == '\r' || ch == '\n' {
|
||||
break
|
||||
}
|
||||
hop.userMsg += string(ch)
|
||||
}
|
||||
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.name = username
|
||||
hop.email = username + "@mycorrhiza" // A fake email, why not
|
||||
return hop
|
||||
}
|
@ -93,12 +93,12 @@ func handlerUploadText(w http.ResponseWriter, rq *http.Request) {
|
||||
hyphaData.textType = TextGemini
|
||||
hyphaData.textPath = fullPath
|
||||
}
|
||||
http.Redirect(w, rq, "/page/"+hyphaName, http.StatusSeeOther)
|
||||
history.Operation(history.TypeEditText).
|
||||
WithFiles(fullPath).
|
||||
WithMsg(fmt.Sprintf("Edit ‘%s’", hyphaName)).
|
||||
WithSignature("anon").
|
||||
Apply()
|
||||
http.Redirect(w, rq, "/page/"+hyphaName, http.StatusSeeOther)
|
||||
}
|
||||
|
||||
// handlerUploadBinary uploads a new binary part for the hypha.
|
||||
@ -156,10 +156,10 @@ func handlerUploadBinary(w http.ResponseWriter, rq *http.Request) {
|
||||
return
|
||||
}
|
||||
log.Println("Written", len(data), "of binary data for", hyphaName, "to path", fullPath)
|
||||
http.Redirect(w, rq, "/page/"+hyphaName, http.StatusSeeOther)
|
||||
history.Operation(history.TypeEditText).
|
||||
WithFiles(fullPath, hyphaData.binaryPath).
|
||||
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)
|
||||
}
|
||||
|
@ -95,9 +95,9 @@ func handlerHistory(w http.ResponseWriter, rq *http.Request) {
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Time</th>
|
||||
<th>Hash</th>
|
||||
<th>Username</th>
|
||||
<th>Time</th>
|
||||
<th>Message</th>
|
||||
</tr>
|
||||
</thead>
|
||||
|
16
main.go
16
main.go
@ -107,20 +107,6 @@ func handlerReindex(w http.ResponseWriter, rq *http.Request) {
|
||||
log.Println("Indexed", len(HyphaStorage), "hyphae")
|
||||
}
|
||||
|
||||
func handlerListCommits(w http.ResponseWriter, rq *http.Request) {
|
||||
log.Println(rq.URL)
|
||||
w.Header().Set("Content-Type", "text/html;charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(history.CommitsTable()))
|
||||
}
|
||||
|
||||
func handlerStatus(w http.ResponseWriter, rq *http.Request) {
|
||||
log.Println(rq.URL)
|
||||
w.Header().Set("Content-Type", "text/html;charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(history.StatusTable()))
|
||||
}
|
||||
|
||||
func main() {
|
||||
log.Println("Running MycorrhizaWiki β")
|
||||
|
||||
@ -145,8 +131,6 @@ func main() {
|
||||
// See http_mutators.go for /upload-binary/, /upload-text/, /edit/.
|
||||
http.HandleFunc("/list", handlerList)
|
||||
http.HandleFunc("/reindex", handlerReindex)
|
||||
http.HandleFunc("/git/list", handlerListCommits)
|
||||
http.HandleFunc("/git/status", handlerStatus)
|
||||
http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, rq *http.Request) {
|
||||
http.ServeFile(w, rq, WikiDir+"/static/favicon.ico")
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user