-
Notifications
You must be signed in to change notification settings - Fork 0
/
git.go
63 lines (54 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
package git
import (
"bytes"
"net/url"
"os/exec"
"strings"
"github.com/pkg/errors"
)
func Clone(path, url, branch string) error {
return runcmd("git", "clone", "-b", branch, "--single-branch", url, path)
}
func Update(path, commit string) error {
if err := runcmd("git", "-C", path, "fetch"); err != nil {
return err
}
return runcmd("git", "-C", path, "checkout", commit)
}
func HeadCommit(path string) (string, error) {
cmd := exec.Command("git", "-C", path, "rev-parse", "HEAD")
output, err := cmd.Output()
return strings.Trim(string(output), "\n"), err
}
func RemoteBranchHeadCommit(url, branch string) (string, error) {
cmd := exec.Command("git", "ls-remote", url, branch)
output, err := cmd.CombinedOutput()
if err != nil {
return "", errors.Wrap(err, string(output))
}
parts := strings.Split(string(output), "\t")
return parts[0], nil
}
func IsValid(url string) bool {
err := runcmd("git", "ls-remote", url)
return err == nil
}
func runcmd(name string, arg ...string) error {
cmd := exec.Command(name, arg...)
bufErr := &bytes.Buffer{}
cmd.Stderr = bufErr
if err := cmd.Run(); err != nil {
return errors.Wrap(err, bufErr.String())
}
return nil
}
// FormatURL generates request url if is a private catalog
func FormatURL(pathURL, username, password string) string {
if len(username) > 0 && len(password) > 0 {
if u, err := url.Parse(pathURL); err == nil {
u.User = url.UserPassword(username, password)
return u.String()
}
}
return pathURL
}