1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/modules/mercurial/hg_repo.go
2019-08-05 10:50:12 -07:00

95 lines
2.1 KiB
Go

package mercurial
import (
"fmt"
"io/ioutil"
"os/exec"
"path"
"strings"
"github.com/wtfutil/wtf/utils"
)
type MercurialRepo struct {
Branch string
Bookmark string
ChangedFiles []string
Commits []string
Repository string
Path string
}
func NewMercurialRepo(repoPath string, commitCount int, commitFormat string) *MercurialRepo {
repo := MercurialRepo{Path: repoPath}
repo.Branch = strings.TrimSpace(repo.branch())
repo.Bookmark = strings.TrimSpace(repo.bookmark())
repo.ChangedFiles = repo.changedFiles()
repo.Commits = repo.commits(commitCount, commitFormat)
repo.Repository = strings.TrimSpace(repo.Path)
return &repo
}
/* -------------------- Unexported Functions -------------------- */
func (repo *MercurialRepo) branch() string {
arg := []string{"branch", repo.repoPath()}
cmd := exec.Command("hg", arg...)
str := utils.ExecuteCommand(cmd)
return str
}
func (repo *MercurialRepo) bookmark() string {
bookmark, err := ioutil.ReadFile(path.Join(repo.Path, ".hg", "bookmarks.current"))
if err != nil {
return ""
}
return string(bookmark)
}
func (repo *MercurialRepo) changedFiles() []string {
arg := []string{"status", repo.repoPath()}
cmd := exec.Command("hg", arg...)
str := utils.ExecuteCommand(cmd)
data := strings.Split(str, "\n")
return data
}
func (repo *MercurialRepo) commits(commitCount int, commitFormat string) []string {
numStr := fmt.Sprintf("-l %d", commitCount)
commitStr := fmt.Sprintf("--template=\"%s\n\"", commitFormat)
arg := []string{"log", repo.repoPath(), numStr, commitStr}
cmd := exec.Command("hg", arg...)
str := utils.ExecuteCommand(cmd)
data := strings.Split(str, "\n")
return data
}
func (repo *MercurialRepo) pull() string {
arg := []string{"pull", repo.repoPath()}
cmd := exec.Command("hg", arg...)
str := utils.ExecuteCommand(cmd)
return str
}
func (repo *MercurialRepo) checkout(branch string) string {
arg := []string{"checkout", repo.repoPath(), branch}
cmd := exec.Command("hg", arg...)
str := utils.ExecuteCommand(cmd)
return str
}
func (repo *MercurialRepo) repoPath() string {
return fmt.Sprintf("--repository=%s", repo.Path)
}