-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgit.go
258 lines (213 loc) · 5.65 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package main
import (
"bufio"
"bytes"
"fmt"
"os/exec"
"path"
"strconv"
"strings"
"github.com/docker/docker/pkg/archive"
)
const (
DIFF_ADDED = "A"
DIFF_MODIFIED = "M"
DIFF_DELETED = "D"
)
var ErrNoChange = fmt.Errorf("no changes to extract")
type gitRepo struct {
Path string
}
func isGitRepo(repoPath string) bool {
return fileExists(path.Join(repoPath, ".git"))
}
func newGitRepo(path string) (*gitRepo, error) {
r := &gitRepo{Path: path}
if isGitRepo(r.Path) {
return r, nil
}
_, err := r.exec("init", path)
if err != nil {
return nil, err
}
email, _ := r.userConfig("email")
if len(email) == 0 {
if _, err := r.execInWorkTree("config", "user.email", "[email protected]"); err != nil {
return nil, err
}
}
name, _ := r.userConfig("name")
if len(name) == 0 {
if _, err := r.execInWorkTree("config", "user.name", "krgo"); err != nil {
return nil, err
}
}
return r, nil
}
func (r *gitRepo) userConfig(key string) ([]byte, error) {
return r.exec("config", "user."+key)
}
func (r *gitRepo) checkout(br branch) ([]byte, error) {
return r.execInWorkTree("checkout", br.string())
}
func (r *gitRepo) checkoutB(br branch) ([]byte, error) {
return r.execInWorkTree("checkout", "-b", br.string())
}
func (r *gitRepo) addAllAndCommit(message string) ([]byte, error) {
badd, err := r.add(".")
if err != nil {
return badd, err
}
bCi, err := r.commit(message)
return append(badd, bCi...), err
}
func (r *gitRepo) add(file string) ([]byte, error) {
return r.execInWorkTree("add", file, "--all")
}
func (r *gitRepo) commit(message string) ([]byte, error) {
out, err := r.execInWorkTree("status", "--porcelain")
if err != nil {
return out, err
}
if len(out) == 0 {
return nil, nil //nothing to commit
}
return r.execInWorkTree("commit", "-m", message)
}
func (r *gitRepo) branch() ([]branch, error) {
b, err := r.execInWorkTree("branch")
if err != nil {
return nil, err
}
rawBrs := strings.Split(string(b), "\n")
brs := make([]branch, len(rawBrs))
for i, br := range rawBrs {
brs[i] = branch(strings.TrimLeft(br, " *"))
}
return brs[:len(brs)-1], nil //remove the last empty line
}
func (r *gitRepo) currentBranch() (branch, error) {
b, err := r.execInWorkTree("symbolic-ref", "--short", "HEAD")
return branch(strings.TrimSuffix(string(b), "\n")), err
}
func (r *gitRepo) describeBranch(br branch, descr string) error {
_, err := r.execInWorkTree("config", "branch."+br.string()+".description", descr)
return err
}
func (r *gitRepo) branchDescription(br branch) ([]byte, error) {
return r.execInWorkTree("config", "branch."+br.string()+".description")
}
func (r *gitRepo) countBranch() (int, error) {
branches, err := r.branch()
if err != nil {
return -1, err
}
return len(branches), nil
}
func (r *gitRepo) diffCached() ([]byte, error) {
return r.execInWorkTree("diff", "--cached", "--name-status")
}
func (r *gitRepo) diff(br1, br2 branch) ([]byte, error) {
return r.execInWorkTree("diff", br1.string()+".."+br2.string(), "--name-status")
}
//export every uncommited changes in the current branch
func (r *gitRepo) exportUncommitedChangeSet() (archive.Archive, error) {
r.add(".")
diff, err := r.diffCached()
if err != nil {
return nil, err
}
return exportChanges(r.Path, diff)
}
func (r *gitRepo) exportChangeSet(br branch) (archive.Archive, error) {
currentBr, err := r.currentBranch()
if err != nil {
return nil, err
}
_, err = r.checkout(br)
if err != nil {
return nil, err
}
defer func() {
r.checkout(currentBr)
}()
branches, err := r.branch()
if err != nil {
return nil, err
}
switch br.number() {
case 0:
changes, err := archive.ChangesDirs(r.Path, "")
if err != nil {
return nil, err
}
var curatedChanges []archive.Change
for _, ch := range changes {
if !strings.HasPrefix(ch.Path, "/.git") {
curatedChanges = append(curatedChanges, ch)
}
}
return archive.ExportChanges(r.Path, curatedChanges)
default:
parentBr := branches[br.number()-1]
diff, _ := r.diff(parentBr, br)
return exportChanges(r.Path, diff)
}
}
func exportChanges(rootfs string, diff []byte) (archive.Archive, error) {
var changes []archive.Change
scanner := bufio.NewScanner(bytes.NewReader(diff))
for scanner.Scan() {
line := scanner.Text()
dType := strings.SplitN(line, "\t", 2)[0]
path := "/" + strings.SplitN(line, "\t", 2)[1] // important to consider the / for ExportChanges
change := archive.Change{Path: path}
switch dType {
case DIFF_MODIFIED:
change.Kind = archive.ChangeModify
case DIFF_ADDED:
change.Kind = archive.ChangeAdd
case DIFF_DELETED:
change.Kind = archive.ChangeDelete
}
changes = append(changes, change)
if err := scanner.Err(); err != nil {
return nil, err
}
}
if len(changes) == 0 {
return nil, ErrNoChange
}
return archive.ExportChanges(rootfs, changes)
}
func (r *gitRepo) execInWorkTree(args ...string) ([]byte, error) {
args = append([]string{"--git-dir=" + path.Join(r.Path, "/.git"), "--work-tree=" + r.Path}, args...)
return r.exec(args...)
}
func (r *gitRepo) exec(args ...string) ([]byte, error) {
gitPath, err := exec.LookPath("git")
if err != nil {
return nil, err
}
cmd := exec.Command(gitPath, args...)
out, err := cmd.CombinedOutput()
if err != nil {
return out, fmt.Errorf("%v (%v)", string(out), err)
}
return out, nil
}
//branch specific type for krgo
type branch string
func newBranch(n int, ID string) branch {
return branch("layer_" + strconv.Itoa(n) + "_" + ID)
}
func (br branch) number() int {
n, _ := strconv.ParseInt(strings.Split(string(br), "_")[1], 10, 64)
return int(n)
}
func (br branch) imageID() string {
return strings.Split(string(br), "_")[2]
}
func (br branch) string() string {
return string(br)
}