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
|
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package vcs
import (
"os/exec"
"path/filepath"
"strings"
"time"
os "github.com/kardianos/govendor/internal/vos"
)
type VcsHg struct{}
func (VcsHg) Find(dir string) (*VcsInfo, error) {
fi, err := os.Stat(filepath.Join(dir, ".hg"))
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}
if !fi.IsDir() {
return nil, nil
}
// Get info.
info := &VcsInfo{}
cmd := exec.Command("hg", "identify", "-i")
cmd.Dir = dir
output, err := cmd.CombinedOutput()
if err != nil {
return nil, err
}
rev := strings.TrimSpace(string(output))
if strings.HasSuffix(rev, "+") {
info.Dirty = true
rev = strings.TrimSuffix(rev, "+")
}
cmd = exec.Command("hg", "log", "-r", rev)
cmd.Dir = dir
output, err = cmd.CombinedOutput()
if err != nil {
return nil, err
}
for _, line := range strings.Split(string(output), "\n") {
if strings.HasPrefix(line, "changeset:") {
ss := strings.Split(line, ":")
info.Revision = strings.TrimSpace(ss[len(ss)-1])
}
if strings.HasPrefix(line, "date:") {
line = strings.TrimPrefix(line, "date:")
tm, err := time.Parse("Mon Jan 02 15:04:05 2006 -0700", strings.TrimSpace(line))
if err == nil {
info.RevisionTime = &tm
}
}
}
return info, nil
}
|