/
git.go
105 lines (92 loc) · 2.46 KB
/
git.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package sync
import (
"context"
"fmt"
"github.com/cli/cli/v2/git"
)
type gitClient interface {
CurrentBranch() (string, error)
UpdateBranch(string, string) error
CreateBranch(string, string, string) error
Fetch(string, string) error
HasLocalBranch(string) bool
IsAncestor(string, string) (bool, error)
IsDirty() (bool, error)
MergeFastForward(string) error
ResetHard(string) error
}
type gitExecuter struct {
client *git.Client
}
func (g *gitExecuter) UpdateBranch(branch, ref string) error {
cmd, err := g.client.Command(context.Background(), "update-ref", fmt.Sprintf("refs/heads/%s", branch), ref)
if err != nil {
return err
}
_, err = cmd.Output()
return err
}
func (g *gitExecuter) CreateBranch(branch, ref, upstream string) error {
ctx := context.Background()
cmd, err := g.client.Command(ctx, "branch", branch, ref)
if err != nil {
return err
}
if _, err := cmd.Output(); err != nil {
return err
}
cmd, err = g.client.Command(ctx, "branch", "--set-upstream-to", upstream, branch)
if err != nil {
return err
}
_, err = cmd.Output()
return err
}
func (g *gitExecuter) CurrentBranch() (string, error) {
return g.client.CurrentBranch(context.Background())
}
func (g *gitExecuter) Fetch(remote, ref string) error {
args := []string{"fetch", "-q", remote, ref}
cmd, err := g.client.AuthenticatedCommand(context.Background(), args...)
if err != nil {
return err
}
return cmd.Run()
}
func (g *gitExecuter) HasLocalBranch(branch string) bool {
return g.client.HasLocalBranch(context.Background(), branch)
}
func (g *gitExecuter) IsAncestor(ancestor, progeny string) (bool, error) {
args := []string{"merge-base", "--is-ancestor", ancestor, progeny}
cmd, err := g.client.Command(context.Background(), args...)
if err != nil {
return false, err
}
_, err = cmd.Output()
return err == nil, nil
}
func (g *gitExecuter) IsDirty() (bool, error) {
changeCount, err := g.client.UncommittedChangeCount(context.Background())
if err != nil {
return false, err
}
return changeCount != 0, nil
}
func (g *gitExecuter) MergeFastForward(ref string) error {
args := []string{"merge", "--ff-only", "--quiet", ref}
cmd, err := g.client.Command(context.Background(), args...)
if err != nil {
return err
}
_, err = cmd.Output()
return err
}
func (g *gitExecuter) ResetHard(ref string) error {
args := []string{"reset", "--hard", ref}
cmd, err := g.client.Command(context.Background(), args...)
if err != nil {
return err
}
_, err = cmd.Output()
return err
}