/
git_native.go
126 lines (101 loc) · 2.79 KB
/
git_native.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package gitWrapper
import (
"fmt"
"github.com/hashload/boss/core/paths"
"github.com/hashload/boss/env"
"github.com/hashload/boss/models"
"github.com/hashload/boss/msg"
git2 "gopkg.in/src-d/go-git.v4"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
)
func checkHasGitClient() {
command := exec.Command("where", "git")
_, err := command.Output()
if err != nil {
msg.Die("Git.exe not found in path")
}
}
func CloneCacheNative(dep models.Dependency) *git2.Repository {
msg.Info("Downloading dependency %s", dep.Repository)
getWrapperClone(dep)
return GetRepository(dep)
}
func UpdateCacheNative(dep models.Dependency) *git2.Repository {
msg.Info("Updating dependency %s", dep.GetName())
getWrapperFetch(dep)
return GetRepository(dep)
}
func getWrapperClone(dep models.Dependency) {
checkHasGitClient()
paths.EnsureCacheDir(dep)
dir := "--separate-git-dir=" + filepath.Join(env.GetCacheDir(), dep.GetHashName())
dirModule := filepath.Join(env.GetModulesDir(), dep.GetName())
_ = os.RemoveAll(dir)
cmd := exec.Command("git", "clone", dir, dep.GetURL(), dirModule)
if err := runCommand(cmd); err != nil {
msg.Die(err.Error())
}
initSubmodulesNative(dep)
_ = os.Remove(filepath.Join(dirModule, ".git"))
}
func writeDotGitFile(dep models.Dependency) {
mask := fmt.Sprintf("gitdir: %s\n", filepath.Join(env.GetCacheDir(), dep.GetHashName()))
path := filepath.Join(env.GetModulesDir(), dep.GetName(), ".git")
_ = ioutil.WriteFile(path, []byte(mask), os.ModePerm)
}
func getWrapperFetch(dep models.Dependency) {
checkHasGitClient()
writeDotGitFile(dep)
dirModule := filepath.Join(env.GetModulesDir(), dep.GetName())
cmdReset := exec.Command("git", "reset", "--hard")
cmdReset.Dir = dirModule
if err := runCommand(cmdReset); err != nil {
msg.Die(err.Error())
}
cmd := exec.Command("git", "fetch", "--all")
cmd.Dir = dirModule
if err := runCommand(cmd); err != nil {
msg.Die(err.Error())
}
initSubmodulesNative(dep)
_ = os.Remove(filepath.Join(dirModule, ".git"))
}
func initSubmodulesNative(dep models.Dependency) {
dirModule := filepath.Join(env.GetModulesDir(), dep.GetName())
cmd := exec.Command("git", "submodule", "update", "--init", "--recursive")
cmd.Dir = dirModule
if err := runCommand(cmd); err != nil {
msg.Die(err.Error())
}
}
func runCommand(cmd *exec.Cmd) error {
cmd.Stdout = newWriter(false)
cmd.Stderr = newWriter(true)
if err := cmd.Start(); err != nil {
return err
}
if err := cmd.Wait(); err != nil {
return err
}
return nil
}
type writer struct {
io.Writer
errorWritter bool
}
func newWriter(errorWritter bool) *writer {
return &writer{errorWritter: errorWritter}
}
func (writer *writer) Write(p []byte) (n int, err error) {
var str = " " + string(p)
if writer.errorWritter {
msg.Err(str)
} else {
msg.Info(str)
}
return len(p), nil
}