goploy/internal/pkg/git.go

103 lines
2.0 KiB
Go
Raw Normal View History

2022-04-09 21:53:37 +08:00
// Copyright 2022 The Goploy Authors. All rights reserved.
// Use of this source code is governed by a GPLv3-style
// license that can be found in the LICENSE file.
2022-11-23 10:30:02 +08:00
package pkg
2020-08-04 14:28:25 +08:00
import (
"bytes"
2022-12-08 14:01:48 +08:00
"fmt"
2020-08-04 14:28:25 +08:00
"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 {
2022-12-08 14:01:48 +08:00
return fmt.Errorf("error: %s, detail: %s", err, git.Err.String())
2020-08-04 14:28:25 +08:00
}
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
}