/usr/share/gocode/src/github.com/Masterminds/vcs/hg.go is in golang-github-masterminds-vcs-dev 1.10.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | package vcs
import (
"encoding/xml"
"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) {
ins := depInstalled("hg")
if !ins {
return nil, NewLocalError("hg is not installed", nil, "")
}
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() {
// 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, NewLocalError("Unable to retrieve local repo information", err, string(out))
}
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 {
out, err := s.run("hg", "clone", s.Remote(), s.LocalPath())
if err != nil {
return NewRemoteError("Unable to get repository", err, string(out))
}
return nil
}
// Init will initialize a mercurial repository at local location.
func (s *HgRepo) Init() error {
out, err := s.run("hg", "init", s.LocalPath())
if err != nil {
return NewLocalError("Unable to initialize repository", err, string(out))
}
return nil
}
// Update performs a Mercurial pull to an existing checkout.
func (s *HgRepo) Update() error {
return s.UpdateVersion(``)
}
// UpdateVersion sets the version of a package currently checked out via Hg.
func (s *HgRepo) UpdateVersion(version string) error {
out, err := s.RunFromDir("hg", "pull")
if err != nil {
return NewLocalError("Unable to update checked out version", err, string(out))
}
if len(strings.TrimSpace(version)) > 0 {
out, err = s.RunFromDir("hg", "update", version)
} else {
out, err = s.RunFromDir("hg", "update")
}
if err != nil {
return NewLocalError("Unable to update checked out version", err, string(out))
}
return nil
}
// Version retrieves the current version.
func (s *HgRepo) Version() (string, error) {
out, err := s.RunFromDir("hg", "--debug", "identify")
if err != nil {
return "", NewLocalError("Unable to retrieve checked out version", err, string(out))
}
parts := strings.SplitN(string(out), " ", 2)
sha := parts[0]
return strings.TrimSpace(sha), nil
}
// Current returns the current version-ish. This means:
// * Branch name if on the tip of the branch
// * Tag if on a tag
// * Otherwise a revision id
func (s *HgRepo) Current() (string, error) {
out, err := s.RunFromDir("hg", "branch")
if err != nil {
return "", err
}
branch := strings.TrimSpace(string(out))
tip, err := s.CommitInfo("max(branch(" + branch + "))")
if err != nil {
return "", err
}
curr, err := s.Version()
if err != nil {
return "", err
}
if tip.Commit == curr {
return branch, nil
}
ts, err := s.TagsFromCommit(curr)
if err != nil {
return "", err
}
if len(ts) > 0 {
return ts[0], nil
}
return curr, 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{}, NewLocalError("Unable to retrieve revision date", err, "")
}
out, err := s.RunFromDir("hg", "log", "-r", version, "--template", "{date|isodatesec}")
if err != nil {
return time.Time{}, NewLocalError("Unable to retrieve revision date", err, string(out))
}
t, err := time.Parse(longForm, string(out))
if err != nil {
return time.Time{}, NewLocalError("Unable to retrieve revision date", err, string(out))
}
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{}, NewLocalError("Unable to retrieve branches", err, string(out))
}
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{}, NewLocalError("Unable to retrieve tags", err, string(out))
}
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)
return err == nil
}
// 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
}
// CommitInfo retrieves metadata about a commit.
func (s *HgRepo) CommitInfo(id string) (*CommitInfo, error) {
out, err := s.RunFromDir("hg", "log", "-r", id, "--style=xml")
if err != nil {
return nil, ErrRevisionUnavailable
}
type Author struct {
Name string `xml:",chardata"`
Email string `xml:"email,attr"`
}
type Logentry struct {
Node string `xml:"node,attr"`
Author Author `xml:"author"`
Date string `xml:"date"`
Msg string `xml:"msg"`
}
type Log struct {
XMLName xml.Name `xml:"log"`
Logs []Logentry `xml:"logentry"`
}
logs := &Log{}
err = xml.Unmarshal(out, &logs)
if err != nil {
return nil, NewLocalError("Unable to retrieve commit information", err, string(out))
}
if len(logs.Logs) == 0 {
return nil, ErrRevisionUnavailable
}
ci := &CommitInfo{
Commit: logs.Logs[0].Node,
Author: logs.Logs[0].Author.Name + " <" + logs.Logs[0].Author.Email + ">",
Message: logs.Logs[0].Msg,
}
if logs.Logs[0].Date != "" {
ci.Date, err = time.Parse(time.RFC3339, logs.Logs[0].Date)
if err != nil {
return nil, NewLocalError("Unable to retrieve commit information", err, string(out))
}
}
return ci, nil
}
// TagsFromCommit retrieves tags from a commit id.
func (s *HgRepo) TagsFromCommit(id string) ([]string, error) {
// Hg has a single tag per commit. If a second tag is added to a commit a
// new commit is created and the tag is attached to that new commit.
out, err := s.RunFromDir("hg", "log", "-r", id, "--style=xml")
if err != nil {
return []string{}, NewLocalError("Unable to retrieve tags", err, string(out))
}
type Logentry struct {
Node string `xml:"node,attr"`
Tag string `xml:"tag"`
}
type Log struct {
XMLName xml.Name `xml:"log"`
Logs []Logentry `xml:"logentry"`
}
logs := &Log{}
err = xml.Unmarshal(out, &logs)
if err != nil {
return []string{}, NewLocalError("Unable to retrieve tags", err, string(out))
}
if len(logs.Logs) == 0 {
return []string{}, NewLocalError("Unable to retrieve tags", err, string(out))
}
t := strings.TrimSpace(logs.Logs[0].Tag)
if t != "" {
return []string{t}, nil
}
return []string{}, nil
}
// Ping returns if remote location is accessible.
func (s *HgRepo) Ping() bool {
_, err := s.run("hg", "identify", s.Remote())
return err == nil
}
// ExportDir exports the current revision to the passed in directory.
func (s *HgRepo) ExportDir(dir string) error {
out, err := s.RunFromDir("hg", "archive", dir)
s.log(out)
if err != nil {
return NewLocalError("Unable to export source", err, string(out))
}
return nil
}
|