forked from hound-search/hound
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hg.go
81 lines (67 loc) · 1.23 KB
/
hg.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
package vcs
import (
"bytes"
"io"
"io/ioutil"
"os/exec"
"path/filepath"
"strings"
)
func init() {
Register(newHg, "hg", "mercurial")
}
type MercurialDriver struct{}
func newHg(b []byte) (Driver, error) {
return &MercurialDriver{}, nil
}
func (g *MercurialDriver) HeadRev(dir string) (string, error) {
cmd := exec.Command(
"hg",
"log",
"-r",
".",
"--template",
"{node}")
cmd.Dir = dir
r, err := cmd.StdoutPipe()
if err != nil {
return "", err
}
defer r.Close()
if err := cmd.Start(); err != nil {
return "", err
}
var buf bytes.Buffer
if _, err := io.Copy(&buf, r); err != nil {
return "", err
}
return strings.TrimSpace(buf.String()), cmd.Wait()
}
func (g *MercurialDriver) Pull(dir string) (string, error) {
cmd := exec.Command("hg", "pull", "-u")
cmd.Dir = dir
err := cmd.Run()
if err != nil {
return "", err
}
return g.HeadRev(dir)
}
func (g *MercurialDriver) Clone(dir, url string) (string, error) {
par, rep := filepath.Split(dir)
cmd := exec.Command(
"hg",
"clone",
url,
rep)
cmd.Dir = par
cmd.Stdout = ioutil.Discard
if err := cmd.Run(); err != nil {
return "", err
}
return g.HeadRev(dir)
}
func (g *MercurialDriver) SpecialFiles() []string {
return []string{
".hg",
}
}