-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.go
More file actions
110 lines (89 loc) · 1.98 KB
/
git.go
File metadata and controls
110 lines (89 loc) · 1.98 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
package main
import (
"errors"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"golang.org/x/crypto/ssh"
"gopkg.in/src-d/go-git.v4"
gitssh "gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
)
func getAuth() (*gitssh.PublicKeys, error) {
pem, err := ioutil.ReadFile(cfg.SSH.PrivateKey)
if err != nil {
return nil, errors.New("couldn't read private key '" + cfg.SSH.PrivateKey + "'")
}
signer, err := ssh.ParsePrivateKey(pem)
if err != nil {
return nil, err
}
auth := &gitssh.PublicKeys{
User: cfg.SSH.Username,
Signer: signer,
}
return auth, nil
}
// gitClone uses go-git functions to clone.
func gitClone(url, path string) error {
auth, err := getAuth()
if err != nil {
return err
}
o := git.CloneOptions{
URL: url,
Auth: auth,
}
path = filepath.Join(cfg.Repos, path)
_, err = git.PlainClone(path, false, &o)
if err != nil {
return err
}
return nil
}
// gitPull updates a repository via go-git functions.
func gitPull(path string) error {
auth, err := getAuth()
if err != nil {
return err
}
o := git.PullOptions{
Auth: auth,
}
path = filepath.Join(cfg.Repos, path)
repo, err := git.PlainOpen(path)
if err != nil {
return err
}
w, err := repo.Worktree()
if err != nil {
return err
}
// src-d changed the API without bumping the version. Uncool.
w.Pull(&o)
return err
}
// gitExternalClone clones via whichever command line git is installed in the user's path.
// Use with anything which go-git can't handle, like VSTS.
func gitExternalClone(url, path string) error {
cmd := exec.Command("git", "-C", cfg.Repos, "clone", url, path)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
return err
}
return nil
}
// gitExternalPull updates via the installed git command.
func gitExternalPull(path string) error {
path = filepath.Join(cfg.Repos, path)
cmd := exec.Command("git", "-C", path, "pull")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
return err
}
return nil
}