196 lines
4.5 KiB
Go
196 lines
4.5 KiB
Go
package gitpb
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"github.com/go-cmd/cmd"
|
|
"go.wit.com/lib/gui/shell"
|
|
"go.wit.com/log"
|
|
)
|
|
|
|
// execute something with the working directory
|
|
// set to the FullPath
|
|
func (repo *Repo) Run(cmd []string) cmd.Status {
|
|
result := shell.PathRun(repo.FullPath, cmd)
|
|
output := strings.Join(result.Stdout, "\n")
|
|
if result.Error != nil {
|
|
log.Log(WARN, "cmd:", cmd)
|
|
log.Log(WARN, "ouptput:", output)
|
|
log.Log(WARN, "failed with error:", result.Error)
|
|
}
|
|
return result
|
|
}
|
|
|
|
func (repo *Repo) RunQuiet(cmd []string) cmd.Status {
|
|
result := shell.PathRunQuiet(repo.FullPath, cmd)
|
|
return result
|
|
}
|
|
|
|
func (repo *Repo) RunEcho(cmd []string) cmd.Status {
|
|
result := shell.PathRunQuiet(repo.FullPath, cmd)
|
|
log.Log(NOW, "cmd:", repo.FullPath, cmd)
|
|
log.Warn(WARN, "cmd.Exit:", result.Exit, "cmd.Error:", result.Error)
|
|
for _, line := range result.Stdout {
|
|
log.Log(NOW, "STDOUT:", line)
|
|
}
|
|
for _, line := range result.Stderr {
|
|
log.Log(NOW, "STDERR:", line)
|
|
}
|
|
return result
|
|
}
|
|
|
|
func (repo *Repo) RunRealtime(cmd []string) cmd.Status {
|
|
return shell.PathRunRealtime(repo.GetFullPath(), cmd)
|
|
}
|
|
|
|
func (repo *Repo) RunRealtimeVerbose(cmd []string) cmd.Status {
|
|
log.Log(NOW, "Run:", repo.GetFullPath(), cmd)
|
|
return shell.PathRunRealtime(repo.GetFullPath(), cmd)
|
|
}
|
|
|
|
// error if result.Error or if result.Exit != 0
|
|
func (repo *Repo) RunStrict(cmd []string) error {
|
|
return repo.StrictRun(cmd)
|
|
}
|
|
|
|
func (repo *Repo) StrictRun(cmd []string) error {
|
|
result := repo.RunQuiet(cmd)
|
|
if result.Error != nil {
|
|
log.Warn(repo.GetGoPath(), cmd, "wow. golang is cool. an os.Error:", result.Error)
|
|
return result.Error
|
|
}
|
|
if result.Exit != 0 {
|
|
log.Warn(cmd, "failed with", result.Exit)
|
|
return errors.New(fmt.Sprint(cmd, "failed with", result.Exit))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (repo *Repo) RunStrictNew(cmd []string) (*cmd.Status, error) {
|
|
result := repo.RunQuiet(cmd)
|
|
if result.Error != nil {
|
|
log.Warn(repo.GetGoPath(), cmd, "wow. golang is cool. an os.Error:", result.Error)
|
|
return &result, result.Error
|
|
}
|
|
if result.Exit != 0 {
|
|
log.Warn(cmd, "failed with", result.Exit)
|
|
return &result, errors.New(fmt.Sprint(cmd, "failed with", result.Exit))
|
|
}
|
|
return &result, nil
|
|
}
|
|
|
|
func (repo *Repo) Exists(filename string) bool {
|
|
if repo == nil {
|
|
return false
|
|
}
|
|
testf := filepath.Join(repo.FullPath, filename)
|
|
_, err := os.Stat(testf)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func (repo *Repo) IsValidDir() bool {
|
|
if repo == nil {
|
|
return false
|
|
}
|
|
if !repo.IsGitDirectory() {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func (repo *Repo) ReadFile(fname string) ([]byte, error) {
|
|
fullname := filepath.Join(repo.FullPath, fname)
|
|
return os.ReadFile(fullname)
|
|
}
|
|
|
|
func (repo *Repo) IsGitDirectory() bool {
|
|
gitdir := filepath.Join(repo.FullPath, ".git")
|
|
info, err := os.Stat(gitdir)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return info.IsDir()
|
|
}
|
|
|
|
func (repo *Repo) IsDirectory() bool {
|
|
info, err := os.Stat(repo.FullPath)
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return info.IsDir()
|
|
}
|
|
|
|
func (repo *Repo) RunAll(all [][]string) bool {
|
|
for _, cmd := range all {
|
|
log.Log(WARN, "doAll() RUNNING: cmd =", cmd)
|
|
r := repo.Run(cmd)
|
|
if r.Error != nil {
|
|
log.Log(WARN, "doAll() err =", r.Error)
|
|
log.Log(WARN, "doAll() out =", r.Stdout)
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func (repo *Repo) RunStrictAll(all [][]string) (*cmd.Status, error) {
|
|
for _, cmd := range all {
|
|
log.Log(WARN, "doAll() RUNNING: cmd =", cmd)
|
|
if result, err := repo.RunStrictNew(cmd); err != nil {
|
|
return result, err
|
|
}
|
|
}
|
|
return nil, nil
|
|
}
|
|
|
|
func (repo *Repo) RunVerbose(cmd []string) (*cmd.Status, error) {
|
|
log.Info("Running:", repo.GetGoPath(), cmd)
|
|
r, err := repo.RunStrictNew(cmd)
|
|
if err != nil {
|
|
log.Info("Error", cmd, err)
|
|
}
|
|
for _, line := range r.Stdout {
|
|
log.Info(line)
|
|
}
|
|
for _, line := range r.Stderr {
|
|
log.Info(line)
|
|
}
|
|
return r, err
|
|
}
|
|
|
|
func (repo *Repo) RunVerboseOnError(cmd []string) (*cmd.Status, error) {
|
|
r, err := repo.RunStrictNew(cmd)
|
|
if err == nil {
|
|
return r, err
|
|
}
|
|
log.Info("Run Error:", repo.GetGoPath(), cmd, err)
|
|
for _, line := range r.Stdout {
|
|
log.Info(line)
|
|
}
|
|
for _, line := range r.Stderr {
|
|
log.Info(line)
|
|
}
|
|
return r, err
|
|
}
|
|
|
|
// only safe to run len() on STDOUT
|
|
// DO NOT TRY TO PARSE THIS EXCEPT HASH AS FIRST VALUE
|
|
// Intended to be human readable
|
|
func (repo *Repo) ConstructGitDiffLog(branch1, branch2 string) []string {
|
|
var cmd []string
|
|
cmd = append(cmd, "git")
|
|
cmd = append(cmd, "log")
|
|
cmd = append(cmd, "--format=\"%H %ae %as %s\"")
|
|
cmd = append(cmd, branch1)
|
|
cmd = append(cmd, "--not")
|
|
cmd = append(cmd, branch2)
|
|
return cmd
|
|
}
|