2021-05-11 15:14:00 +05:00
|
|
|
|
// Package history provides a git wrapper.
|
2020-08-09 01:10:28 +05:00
|
|
|
|
package history
|
|
|
|
|
|
|
|
|
|
import (
|
2020-08-29 22:54:57 +05:00
|
|
|
|
"bytes"
|
2020-08-09 01:10:28 +05:00
|
|
|
|
"fmt"
|
|
|
|
|
"log"
|
2020-08-29 22:54:57 +05:00
|
|
|
|
"os/exec"
|
2021-06-19 13:04:04 +07:00
|
|
|
|
"path/filepath"
|
2020-12-08 20:15:32 +05:00
|
|
|
|
"regexp"
|
2020-08-09 01:10:28 +05:00
|
|
|
|
|
2021-06-19 11:51:10 +07:00
|
|
|
|
"github.com/bouncepaw/mycorrhiza/files"
|
2020-08-10 00:33:47 +05:00
|
|
|
|
"github.com/bouncepaw/mycorrhiza/util"
|
2020-08-09 01:10:28 +05:00
|
|
|
|
)
|
|
|
|
|
|
2021-03-09 19:27:14 +05:00
|
|
|
|
// Path to git executable. Set at init()
|
|
|
|
|
var gitpath string
|
|
|
|
|
|
2020-12-08 20:15:32 +05:00
|
|
|
|
var renameMsgPattern = regexp.MustCompile(`^Rename ‘(.*)’ to ‘.*’`)
|
|
|
|
|
|
2021-06-06 22:12:07 +07:00
|
|
|
|
var gitEnv = []string{"GIT_COMMITTER_NAME=wikimind", "GIT_COMMITTER_EMAIL=wikimind@mycorrhiza"}
|
|
|
|
|
|
2021-03-09 19:27:14 +05:00
|
|
|
|
// Start finds git and initializes git credentials.
|
2021-05-11 15:14:00 +05:00
|
|
|
|
func Start() {
|
2021-03-09 19:27:14 +05:00
|
|
|
|
path, err := exec.LookPath("git")
|
|
|
|
|
if err != nil {
|
2021-05-11 15:14:00 +05:00
|
|
|
|
log.Fatal("Could not find the git executable. Check your $PATH.")
|
2021-03-09 19:27:14 +05:00
|
|
|
|
}
|
|
|
|
|
gitpath = path
|
2020-08-09 01:10:28 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-02 01:12:16 +08:00
|
|
|
|
// InitGitRepo checks a Git repository and initializes it if necessary.
|
2021-06-19 13:04:04 +07:00
|
|
|
|
func InitGitRepo() {
|
|
|
|
|
// Detect if the Git repo directory is a Git repository
|
|
|
|
|
isGitRepo := true
|
2021-06-23 22:02:13 +07:00
|
|
|
|
buf, err := silentGitsh("rev-parse", "--git-dir")
|
2021-06-19 13:04:04 +07:00
|
|
|
|
if err != nil {
|
|
|
|
|
isGitRepo = false
|
|
|
|
|
}
|
|
|
|
|
if isGitRepo {
|
|
|
|
|
gitDir := buf.String()
|
|
|
|
|
if filepath.IsAbs(gitDir) && !filepath.HasPrefix(gitDir, files.HyphaeDir()) {
|
|
|
|
|
isGitRepo = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !isGitRepo {
|
|
|
|
|
log.Println("Initializing Git repo at", files.HyphaeDir())
|
|
|
|
|
gitsh("init")
|
|
|
|
|
gitsh("config", "core.quotePath", "false")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-29 22:54:57 +05:00
|
|
|
|
// I pronounce it as [gɪt͡ʃ].
|
2020-09-29 23:13:24 +05:00
|
|
|
|
// gitsh is async-safe, therefore all other git-related functions in this module are too.
|
2020-08-29 22:54:57 +05:00
|
|
|
|
func gitsh(args ...string) (out bytes.Buffer, err error) {
|
|
|
|
|
fmt.Printf("$ %v\n", args)
|
|
|
|
|
cmd := exec.Command(gitpath, args...)
|
2021-06-19 11:51:10 +07:00
|
|
|
|
cmd.Dir = files.HyphaeDir()
|
2022-09-07 12:25:37 +03:00
|
|
|
|
cmd.Env = append(cmd.Environ(), gitEnv...)
|
2020-08-10 00:33:47 +05:00
|
|
|
|
|
2020-08-29 22:54:57 +05:00
|
|
|
|
b, err := cmd.CombinedOutput()
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Println("gitsh:", err)
|
|
|
|
|
}
|
|
|
|
|
return *bytes.NewBuffer(b), err
|
2020-08-10 00:33:47 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-03 23:31:19 +05:00
|
|
|
|
// silentGitsh is like gitsh, except it writes less to the stdout.
|
|
|
|
|
func silentGitsh(args ...string) (out bytes.Buffer, err error) {
|
|
|
|
|
cmd := exec.Command(gitpath, args...)
|
2021-06-19 11:51:10 +07:00
|
|
|
|
cmd.Dir = files.HyphaeDir()
|
2021-06-06 22:12:07 +07:00
|
|
|
|
cmd.Env = gitEnv
|
2021-05-03 23:31:19 +05:00
|
|
|
|
|
|
|
|
|
b, err := cmd.CombinedOutput()
|
|
|
|
|
return *bytes.NewBuffer(b), err
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-29 22:54:57 +05:00
|
|
|
|
// Rename renames from `from` to `to` using `git mv`.
|
2020-08-11 00:58:02 +05:00
|
|
|
|
func Rename(from, to string) error {
|
|
|
|
|
log.Println(util.ShorterPath(from), util.ShorterPath(to))
|
2020-11-02 00:09:41 +05:00
|
|
|
|
_, err := gitsh("mv", "--force", from, to)
|
2020-08-11 00:58:02 +05:00
|
|
|
|
return err
|
2020-08-09 01:10:28 +05:00
|
|
|
|
}
|