forked from Masterminds/vcs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hg.go
162 lines (138 loc) · 3.77 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package vcs
import (
"os"
"os/exec"
"regexp"
"strings"
"time"
)
var hgDetectURL = regexp.MustCompile("default = (?P<foo>.+)\n")
// NewHgRepo creates a new instance of HgRepo. The remote and local directories
// need to be passed in.
func NewHgRepo(remote, local string) (*HgRepo, error) {
ltype, err := DetectVcsFromFS(local)
// Found a VCS other than Hg. Need to report an error.
if err == nil && ltype != Hg {
return nil, ErrWrongVCS
}
r := &HgRepo{}
r.setRemote(remote)
r.setLocalPath(local)
r.Logger = Logger
// Make sure the local Hg repo is configured the same as the remote when
// A remote value was passed in.
if err == nil && r.CheckLocal() == true {
// An Hg repo was found so test that the URL there matches
// the repo passed in here.
c := exec.Command("hg", "paths")
c.Dir = local
c.Env = envForDir(c.Dir)
out, err := c.CombinedOutput()
if err != nil {
return nil, err
}
m := hgDetectURL.FindStringSubmatch(string(out))
if m[1] != "" && m[1] != remote {
return nil, ErrWrongRemote
}
// If no remote was passed in but one is configured for the locally
// checked out Hg repo use that one.
if remote == "" && m[1] != "" {
r.setRemote(m[1])
}
}
return r, nil
}
// HgRepo implements the Repo interface for the Mercurial source control.
type HgRepo struct {
base
}
// Vcs retrieves the underlying VCS being implemented.
func (s HgRepo) Vcs() Type {
return Hg
}
// Get is used to perform an initial clone of a repository.
func (s *HgRepo) Get() error {
_, err := s.run("hg", "clone", s.Remote(), s.LocalPath())
return err
}
// Update performs a Mercurial pull to an existing checkout.
func (s *HgRepo) Update() error {
_, err := s.runFromDir("hg", "update")
return err
}
// UpdateVersion sets the version of a package currently checked out via Hg.
func (s *HgRepo) UpdateVersion(version string) error {
_, err := s.runFromDir("hg", "pull")
if err != nil {
return err
}
_, err = s.runFromDir("hg", "update", version)
return err
}
// Version retrieves the current version.
func (s *HgRepo) Version() (string, error) {
out, err := s.runFromDir("hg", "identify")
if err != nil {
return "", err
}
parts := strings.SplitN(string(out), " ", 2)
sha := parts[0]
return strings.TrimSpace(sha), nil
}
// Date retrieves the date on the latest commit.
func (s *HgRepo) Date() (time.Time, error) {
version, err := s.Version()
if err != nil {
return time.Time{}, err
}
out, err := s.runFromDir("hg", "log", "-r", version, "--template", "{date|isodatesec}")
if err != nil {
return time.Time{}, err
}
t, err := time.Parse(longForm, string(out))
if err != nil {
return time.Time{}, err
}
return t, nil
}
// CheckLocal verifies the local location is a Git repo.
func (s *HgRepo) CheckLocal() bool {
if _, err := os.Stat(s.LocalPath() + "/.hg"); err == nil {
return true
}
return false
}
// Branches returns a list of available branches
func (s *HgRepo) Branches() ([]string, error) {
out, err := s.runFromDir("hg", "branches")
if err != nil {
return []string{}, err
}
branches := s.referenceList(string(out), `(?m-s)^(\S+)`)
return branches, nil
}
// Tags returns a list of available tags
func (s *HgRepo) Tags() ([]string, error) {
out, err := s.runFromDir("hg", "tags")
if err != nil {
return []string{}, err
}
tags := s.referenceList(string(out), `(?m-s)^(\S+)`)
return tags, nil
}
// IsReference returns if a string is a reference. A reference can be a
// commit id, branch, or tag.
func (s *HgRepo) IsReference(r string) bool {
_, err := s.runFromDir("hg", "log", "-r", r)
if err == nil {
return true
}
return false
}
// IsDirty returns if the checkout has been modified from the checked
// out reference.
func (s *HgRepo) IsDirty() bool {
out, err := s.runFromDir("hg", "diff")
return err != nil || len(out) != 0
}