goploy/utils/Git.go

98 lines
1.8 KiB
Go
Raw Normal View History

2020-08-04 14:28:25 +08:00
package utils
import (
"bytes"
"os/exec"
)
type GIT struct {
2020-09-25 20:05:25 +08:00
Dir string
2020-08-04 14:28:25 +08:00
Output bytes.Buffer
2020-09-25 20:05:25 +08:00
Err bytes.Buffer
2020-08-04 14:28:25 +08:00
}
2020-09-25 20:05:25 +08:00
func (git *GIT) Run(operator string, options ...string) error {
2020-08-04 14:28:25 +08:00
git.Output.Reset()
git.Err.Reset()
cmd := exec.Command("git", append([]string{operator}, options...)...)
if len(git.Dir) != 0 {
cmd.Dir = git.Dir
}
cmd.Stdout = &git.Output
cmd.Stderr = &git.Err
if err := cmd.Run(); err != nil {
return err
}
return nil
}
2020-09-25 20:05:25 +08:00
func (git *GIT) Clone(options ...string) error {
if err := git.Run("clone", options...); err != nil {
2020-08-04 14:28:25 +08:00
return err
}
return nil
}
2020-09-25 20:05:25 +08:00
func (git *GIT) Checkout(options ...string) error {
if err := git.Run("checkout", options...); err != nil {
2020-08-04 14:28:25 +08:00
return err
}
return nil
}
2020-12-07 22:17:04 +08:00
func (git *GIT) Add(options ...string) error {
if err := git.Run("add", options...); err != nil {
return err
}
return nil
}
2020-09-25 20:05:25 +08:00
func (git *GIT) Pull(options ...string) error {
if err := git.Run("pull", options...); err != nil {
2020-08-04 14:28:25 +08:00
return err
}
return nil
}
2020-12-03 17:05:03 +08:00
func (git *GIT) Fetch(options ...string) error {
if err := git.Run("fetch", options...); err != nil {
return err
}
return nil
}
2020-09-25 20:05:25 +08:00
func (git *GIT) Log(options ...string) error {
if err := git.Run("log", options...); err != nil {
2020-08-04 14:28:25 +08:00
return err
}
return nil
}
2020-12-03 17:05:03 +08:00
func (git *GIT) Branch(options ...string) error {
if err := git.Run("branch", options...); err != nil {
return err
}
return nil
}
2022-01-20 21:40:29 +08:00
func (git *GIT) Current() error {
if err := git.Run("symbolic-ref", "--short", "HEAD"); err != nil {
return err
}
return nil
}
2020-12-03 17:05:03 +08:00
func (git *GIT) Reset(options ...string) error {
if err := git.Run("reset", options...); err != nil {
return err
}
return nil
}
2021-08-19 14:40:42 +08:00
func (git *GIT) LsRemote(options ...string) error {
if err := git.Run("ls-remote", options...); err != nil {
return err
}
return nil
}