Repository navigation
Expand file tree
/
Copy pathgit.go
More file actions
149 lines (126 loc) · 3.78 KB
/
Copy pathgit.go
File metadata and controls
149 lines (126 loc) · 3.78 KB
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
package cmd
import (
"os"
"os/exec"
"fmt"
"strings"
"regexp"
)
type GitVCS struct {}
// GitGet implements the getting logic for Git.
func (g *GitVCS) Get(dep *Dependency) error {
dest := fmt.Sprintf("%s/src/%s", os.Getenv("GOPATH"), dep.Name)
//Info("Cloning %s into %s\n", dep.Repository, dest)
Info("Git: ")
out, err := exec.Command("git", "clone", dep.Repository, dest).CombinedOutput()
fmt.Print(string(out))
return err
}
func (g *GitVCS) Update(dep *Dependency) error {
dest := fmt.Sprintf("%s/src/%s", os.Getenv("GOPATH"), dep.Name)
if _, err := os.Stat(dest); err != nil {
// Assume a new package?
Info("Looks like %s is a new package. Cloning.", dep.Name)
return exec.Command("git", "clone", dep.Repository, dest).Run()
}
oldDir, err := os.Getwd()
if err != nil {
return err
}
os.Chdir(dest)
defer os.Chdir(oldDir)
// Because we can't predict which branch we want to be on, and since
// we want to set checkouts explicitly, we should probably fetch.
//out, err := exec.Command("git", "pull", "--ff-only").CombinedOutput()
Info("Git: ")
out, err := exec.Command("git", "fetch", "--all").CombinedOutput()
fmt.Print(string(out))
return err
}
func (g *GitVCS) Version(dep *Dependency) error {
dest := fmt.Sprintf("%s/src/%s", os.Getenv("GOPATH"), dep.Name)
oldDir, err := os.Getwd()
if err != nil {
return err
}
os.Chdir(dest)
defer os.Chdir(oldDir)
//Info("Setting %s with 'git checkout'\n", dep.Name)
// Now get to the right reference.
if out, err := exec.Command("git", "checkout", dep.Reference).CombinedOutput(); err != nil {
fmt.Println(string(out))
return err
} else {
updatedTo := "the latest"
if dep.Reference != "" {
updatedTo = dep.Reference
}
Info("Setting version of %s to %s\n", dep.Name, updatedTo)
//fmt.Print(string(out))
}
// EXPERIMENTAL: This will keep the repo up to date according to the
// master branch on Git. Since 'master' is convention only, this isn't
// a 100% reliable way to do things.
if dep.Reference == "" {
Info("No Git reference set. Trying to update 'master'...\n")
dep.Reference = "master"
}
branchref := fmt.Sprintf("origin/%s", dep.Reference)
//err = exec.Command("git", "show-ref", "-q", branchref).Run()
out, err := exec.Command("git", "show-ref", branchref).CombinedOutput()
if err == nil {
//Info("Git: Found branch %s", string(out))
//Debug("Reference %s is to a branch.", dep.Reference)
// git merge --ff-only origin $VERSION
out, err := exec.Command("git", "pull", "--ff-only", "origin", dep.Reference).CombinedOutput()
Info("Git: (merge) %s", string(out))
if err != nil {
return err
}
}
// EXPERIMENTAL: Show how far behind we are.
out, err = exec.Command("git", "rev-list", "--count", "HEAD..origin").CombinedOutput()
if err == nil {
count := strings.TrimSpace(string(out))
if count != "0" {
var c string
switch len(count) {
// 0-9, not that bad
case 1:
c = Yellow
// 10-99, we're getting behind
case 2:
c = Red
// Whoa! We're falling way behind!
default:
c = BoldRed
}
Info(Color(c, fmt.Sprintf("Git: %s is %s behind origin.\n", dep.Name, count)))
}
}
return nil
}
func (g *GitVCS) LastCommit(dep *Dependency) (string, error) {
dest := fmt.Sprintf("%s/src/%s", os.Getenv("GOPATH"), dep.Name)
oldDir, err := os.Getwd()
if err != nil {
return "", err
}
os.Chdir(dest)
defer os.Chdir(oldDir)
out, err := exec.Command("git", "log", "-n", "1", "--pretty=format:%h%d").CombinedOutput()
if err != nil {
return "", err
}
parts := strings.SplitN(string(out), " ", 2)
sha := parts[0]
// Send back a tag if a tag matches.
if len(parts) > 1 && strings.Contains(parts[1], "tag: ") {
re := regexp.MustCompile("tag: ([^,)]*)")
subs := re.FindStringSubmatch(parts[1])
if len(subs) > 1 {
return subs[1], nil
}
}
return sha, nil
}