forked from openshift/geard
-
Notifications
You must be signed in to change notification settings - Fork 1
/
git.go
77 lines (61 loc) · 1.46 KB
/
git.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
package sti
import (
"bytes"
"log"
"net/url"
"os"
"os/exec"
"regexp"
"strings"
)
var gitRefExp = regexp.MustCompile(`\A[\w\d\-_\.\^~]+$`)
var gitSshUrlExp = regexp.MustCompile(`\A([\w\d\-_\.+]+@[\w\d\-_\.+]+:[\w\d\-_\.+%/]+\.git)$`)
func validateGitRef(ref string) bool {
return gitRefExp.MatchString(ref)
}
var allowedSchemes = []string{"git", "http", "https", "file"}
func validCloneSpec(source string, verbose bool) bool {
url, err := url.Parse(source)
if err != nil {
return false
}
if stringInSlice(url.Scheme, allowedSchemes) {
return true
}
// support 'git@' ssh urls and local protocol without 'file://' scheme
if url.Scheme == "" {
if strings.HasSuffix(source, ".git") || (strings.HasPrefix(source, "git@") && gitSshUrlExp.MatchString(source)) {
return true
}
}
return false
}
func gitClone(source, target string) error {
cmd := exec.Command("git", "clone", "--recursive", source, target)
cmd.Stdout, cmd.Stderr = os.Stdout, os.Stdout
if err := cmd.Start(); err != nil {
return err
}
if err := cmd.Wait(); err != nil {
return err
}
return nil
}
func gitCheckout(repo, ref string, verbose bool) error {
var buffer bytes.Buffer
cmd := exec.Command("git", "checkout", ref)
cmd.Stdout, cmd.Stderr = &buffer, &buffer
cmd.Dir = repo
err := cmd.Start()
if err != nil {
return err
}
err = cmd.Wait()
if err != nil {
if verbose {
log.Printf("Git checkout output:\n%s", buffer.String())
}
return err
}
return nil
}