2020-08-08 20:10:28 +00:00
|
|
|
|
package history
|
|
|
|
|
|
|
|
|
|
import (
|
2020-08-29 17:54:57 +00:00
|
|
|
|
"bytes"
|
2020-08-08 20:10:28 +00:00
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
2020-08-29 17:54:57 +00:00
|
|
|
|
"os/exec"
|
|
|
|
|
"strconv"
|
|
|
|
|
"time"
|
2020-08-08 20:10:28 +00:00
|
|
|
|
|
2020-08-09 19:33:47 +00:00
|
|
|
|
"github.com/bouncepaw/mycorrhiza/util"
|
2020-08-08 20:10:28 +00:00
|
|
|
|
)
|
|
|
|
|
|
2020-08-29 17:54:57 +00:00
|
|
|
|
// Start initializes git credentials.
|
2020-08-08 20:10:28 +00:00
|
|
|
|
func Start(wikiDir string) {
|
2020-08-29 17:54:57 +00:00
|
|
|
|
_, err := gitsh("config", "user.name", "wikimind")
|
2020-08-08 20:10:28 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
2020-08-29 17:54:57 +00:00
|
|
|
|
_, err = gitsh("config", "user.email", "wikimind@mycorrhiza")
|
2020-08-08 20:10:28 +00:00
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-29 17:54:57 +00:00
|
|
|
|
// 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
|
2020-08-09 19:33:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-29 17:54:57 +00:00
|
|
|
|
// Path to git executable. Set at init()
|
|
|
|
|
var gitpath string
|
2020-08-10 19:58:02 +00:00
|
|
|
|
|
2020-08-29 17:54:57 +00:00
|
|
|
|
func init() {
|
|
|
|
|
path, err := exec.LookPath("git")
|
2020-08-27 16:27:57 +00:00
|
|
|
|
if err != nil {
|
2020-08-29 17:54:57 +00:00
|
|
|
|
log.Fatal("Cound not find the git executable. Check your $PATH.")
|
|
|
|
|
} else {
|
|
|
|
|
log.Println("Git path is", path)
|
2020-08-27 16:27:57 +00:00
|
|
|
|
}
|
2020-08-29 17:54:57 +00:00
|
|
|
|
gitpath = path
|
2020-08-27 16:27:57 +00:00
|
|
|
|
|
2020-08-08 20:10:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-29 17:54:57 +00:00
|
|
|
|
// 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
|
2020-08-09 19:33:47 +00:00
|
|
|
|
|
2020-08-29 17:54:57 +00:00
|
|
|
|
b, err := cmd.CombinedOutput()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println("gitsh:", err)
|
|
|
|
|
}
|
|
|
|
|
return *bytes.NewBuffer(b), err
|
2020-08-09 19:33:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-29 17:54:57 +00:00
|
|
|
|
// 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
|
2020-08-09 19:33:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-29 17:54:57 +00:00
|
|
|
|
// Rename renames from `from` to `to` using `git mv`.
|
2020-08-10 19:58:02 +00:00
|
|
|
|
func Rename(from, to string) error {
|
|
|
|
|
log.Println(util.ShorterPath(from), util.ShorterPath(to))
|
2020-08-27 16:27:57 +00:00
|
|
|
|
_, err := gitsh("mv", from, to)
|
2020-08-10 19:58:02 +00:00
|
|
|
|
return err
|
2020-08-08 20:10:28 +00:00
|
|
|
|
}
|