-
Notifications
You must be signed in to change notification settings - Fork 0
/
git.go
105 lines (92 loc) · 2.26 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 git
import (
"context"
"fmt"
"io/ioutil"
"net/url"
"os"
"os/exec"
"github.com/kamichidu/go-hariti"
)
type Git struct{}
func (self *Git) Clone(c context.Context, bundle *hariti.RemoteBundle, update bool) error {
log := hariti.LoggerFromContextKey(c)
out := hariti.WriterFromContext(c)
errOut := hariti.ErrWriterFromContext(c)
var cmd *exec.Cmd
if info, err := os.Stat(bundle.LocalPath); err != nil {
log.Infof("Cloning %s to %s\n", bundle.URL, bundle.LocalPath)
cmd = exec.Command("git", "clone", "--recursive", bundle.URL.String(), bundle.LocalPath)
cmd.Stdout = out
cmd.Stderr = errOut
} else if info.IsDir() && update {
log.Infof("Pulling in %s", bundle.LocalPath)
cmd = exec.Command("git", "pull", "--ff", "--ff-only")
cmd.Dir = bundle.LocalPath
cmd.Stdout = out
cmd.Stderr = errOut
} else {
return nil
}
errCh := make(chan error, 1)
go func() {
errCh <- cmd.Run()
}()
select {
case <-c.Done():
cmd.Process.Kill()
return c.Err()
case err := <-errCh:
return err
}
}
func (self *Git) IsModified(c context.Context, bundle *hariti.RemoteBundle) (bool, error) {
out := hariti.WriterFromContext(c)
errOut := hariti.ErrWriterFromContext(c)
var cmd *exec.Cmd
if info, err := os.Stat(bundle.LocalPath); err != nil {
return false, fmt.Errorf("Repository %s not cloned into %s", bundle.URL, bundle.LocalPath)
} else if !info.IsDir() {
return false, fmt.Errorf("%s doesn't seems like a repository %s", bundle.LocalPath, bundle.URL)
} else {
cmd = exec.Command("git", "diff", "--exit-code")
cmd.Dir = bundle.LocalPath
cmd.Stdout = out
cmd.Stderr = errOut
}
errCh := make(chan error, 1)
go func() {
errCh <- cmd.Run()
}()
select {
case <-c.Done():
cmd.Process.Kill()
return false, c.Err()
case err := <-errCh:
if err != nil {
return true, err
} else {
return false, nil
}
}
}
func (self *Git) CanHandle(c context.Context, u *url.URL) bool {
cmd := exec.Command("git", "ls-remote", u.String())
cmd.Stdout = ioutil.Discard
cmd.Stderr = ioutil.Discard
errCh := make(chan error, 1)
go func() {
errCh <- cmd.Run()
}()
select {
case <-c.Done():
cmd.Process.Kill()
return false
case err := <-errCh:
return err == nil
}
}
var _ hariti.VCS = (*Git)(nil)
func init() {
hariti.RegisterVCS(new(Git))
}