-
Notifications
You must be signed in to change notification settings - Fork 3
/
scm.go
107 lines (82 loc) · 2.1 KB
/
scm.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
package main
import (
"errors"
"fmt"
"regexp"
"strings"
)
type Scm interface {
SetTerminal(terminal Terminal)
Checkout(repo, branch, path string) error
ImportPathFromRepo(repoUrl string) (importPath string, err error)
BinaryName() string
}
var gitImportRegex = regexp.MustCompile("(?:[^@]+@|(?:https?|git)://)(.+)\\.git")
type GitScm struct {
term Terminal
}
func (g *GitScm) BinaryName() string {
return "git"
}
func (g *GitScm) SetTerminal(terminal Terminal) {
g.term = terminal
}
func (g *GitScm) Checkout(repo, branch, path string) (err error) {
out, err := g.term.Exec("ls " + path)
if err != nil {
fmt.Println(err.Error())
}
// If repo doesn't exist we need to clone it
if len(out) == 0 {
fmt.Println("Checkout out repo: " + repo)
out, err = g.term.Exec("git clone " + repo + " " + path)
fmt.Println(string(out))
if err != nil {
return
}
} else {
// Move to master to ensure we are on a branch
err = g.CheckoutBranch("master", path)
if err != nil {
return
}
fmt.Println("Fetching latest from repo: " + repo)
out, err = g.term.ExecPath("git pull", path)
fmt.Println(string(out))
if err != nil {
return
}
}
// Ensure we are on the correct branch
fmt.Println("Checkout out branch: " + branch)
err = g.CheckoutBranch(branch, path)
if err != nil {
return
}
// Check for submodules
fmt.Println("Checking for submodules: " + branch)
out, err = g.term.ExecPath("git submodule init", path)
fmt.Println(string(out))
if err != nil {
return
}
out, err = g.term.ExecPath("git submodule update", path)
fmt.Println(string(out))
if err != nil {
return
}
return
}
func (g *GitScm) ImportPathFromRepo(repoUrl string) (importPath string, err error) {
matches := gitImportRegex.FindStringSubmatch(repoUrl)
if matches == nil || len(matches) < 2 {
return "", errors.New("Could not determine import path from repo url: " + repoUrl)
}
importPath = strings.Replace(matches[1], ":", "/", -1)
return
}
func (g *GitScm) CheckoutBranch(branch, path string) error {
out, err := g.term.ExecPath("git checkout "+branch, path)
fmt.Println(string(out))
return err
}