-
Notifications
You must be signed in to change notification settings - Fork 0
/
branch.go
54 lines (47 loc) · 1.59 KB
/
branch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package git_cmd
import (
"fmt"
"github.com/xunull/goc/commandx"
"strings"
)
func (g *GitApi) CreateBranch(name string, ops ...Option) (string, error) {
_ = g.getOption(ops...)
cmd := []string{"git", "branch", name}
cmdRes := commandx.RunCommand(cmd, commandx.WithDir(g.Dir))
if cmdRes.Success {
return strings.TrimSpace(cmdRes.Stdout.String()), nil
} else {
return cmdRes.Stderr.String(), cmdRes.Err
}
}
func (g *GitApi) CheckoutRemoteBranch(local, remote, branch string, ops ...Option) (string, error) {
_ = g.getOption(ops...)
cmd := []string{"git", "checkout", "-b", local, fmt.Sprintf("%s/%s", remote, branch)}
cmdRes := commandx.RunCommand(cmd, commandx.WithDir(g.Dir))
if cmdRes.Success {
return strings.TrimSpace(cmdRes.Stdout.String()), nil
} else {
return cmdRes.Stderr.String(), cmdRes.Err
}
}
func (g *GitApi) PushU(local, remote string, ops ...Option) (string, error) {
_ = g.getOption(ops...)
cmd := []string{"git", "push", "-u", remote, local}
cmdRes := commandx.RunCommand(cmd, commandx.WithDir(g.Dir))
if cmdRes.Success {
return strings.TrimSpace(cmdRes.Stdout.String()), nil
} else {
return cmdRes.Stderr.String(), cmdRes.Err
}
}
func (g *GitApi) SetUpstreamTo(remote, remoteBranch, localBranch string, ops ...Option) (string, error) {
_ = g.getOption(ops...)
t := fmt.Sprintf("--set-upstream-to=%s/%s", remote, remoteBranch)
cmd := []string{"git", "branch", t, localBranch}
cmdRes := commandx.RunCommand(cmd, commandx.WithDir(g.Dir))
if cmdRes.Success {
return strings.TrimSpace(cmdRes.Stdout.String()), nil
} else {
return cmdRes.Stderr.String(), cmdRes.Err
}
}