-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
temprepo.go
111 lines (88 loc) · 1.91 KB
/
temprepo.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
package shoal
import (
"io"
"io/ioutil"
"os"
"path/filepath"
)
func (a *App) TempDir(source string) (string, error) {
g := a.git
if g == nil {
g = &NativeGit{}
}
tempRemote, err := ioutil.TempDir(os.TempDir(), "shoal-remote")
if err != nil {
return "", err
}
if err := g.InitBare(tempRemote); err != nil {
return "", err
}
tempLocal, err := ioutil.TempDir(os.TempDir(), "shoal-local")
if err != nil {
return "", err
}
defer os.RemoveAll(tempLocal)
// go-git fails on cloning an empty repository. So init/add remote instead.
//if err := g.Clone(tempRemote, tempLocal); err != nil {
// return "", err
//}
if err := g.Init(tempLocal); err != nil {
return "", err
}
if err := g.AddRemote(tempLocal, "origin", tempRemote); err != nil {
return "", err
}
sourceAbs, err := filepath.Abs(source)
if err != nil {
return "", err
}
if err := filepath.Walk(sourceAbs, func(path string, info os.FileInfo, err error) error {
rel, err := filepath.Rel(sourceAbs, path)
if err != nil {
return err
}
dstPath := filepath.Join(tempLocal, rel)
if info.IsDir() {
err := os.MkdirAll(dstPath, 0755)
return err
}
src, err := os.Open(path)
if err != nil {
return err
}
defer src.Close()
dst, err := os.Create(dstPath)
if err != nil {
return err
}
defer dst.Close()
_, err = io.Copy(dst, src)
if err != nil {
return err
}
_ = dst.Close()
_ = src.Close()
if err := g.Add(tempLocal, rel); err != nil {
return err
}
return nil
}); err != nil {
return "", err
}
config := map[string]string{
"user.email": "[email protected]",
"user.name": "user",
}
for k, v := range config {
if err := g.Config(tempLocal, k, v); err != nil {
return "", err
}
}
if err := g.Commit(tempLocal, "first commit"); err != nil {
return "", err
}
if err := g.Push(tempLocal, "origin", "master"); err != nil {
return "", err
}
return tempRemote, nil
}