This repository has been archived by the owner on Dec 19, 2019. It is now read-only.
/
repo.go
463 lines (373 loc) · 10.5 KB
/
repo.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
package gig
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"strings"
)
//Repository represents an on disk git repository.
type Repository struct {
Path string
}
//InitBareRepository creates a bare git repository at path.
func InitBareRepository(path string) (*Repository, error) {
path, err := filepath.Abs(path)
if err != nil {
return nil, fmt.Errorf("Could not determine absolute path: %v", err)
}
cmd := exec.Command("git", "init", "--bare", path)
err = cmd.Run()
if err != nil {
return nil, err
}
return &Repository{Path: path}, nil
}
//IsBareRepository checks if path is a bare git repository.
func IsBareRepository(path string) bool {
cmd := exec.Command("git", fmt.Sprintf("--git-dir=%s", path), "rev-parse", "--is-bare-repository")
body, err := cmd.Output()
if err != nil {
return false
}
status := strings.Trim(string(body), "\n ")
return status == "true"
}
//OpenRepository opens the repository at path. Currently
//verifies that it is a (bare) repository and returns an
//error if the check fails.
func OpenRepository(path string) (*Repository, error) {
path, err := filepath.Abs(path)
if err != nil {
return nil, fmt.Errorf("git: could not determine absolute path")
}
if !IsBareRepository(path) {
return nil, fmt.Errorf("git: not a bare repository")
}
return &Repository{Path: path}, nil
}
//DiscoverRepository returns the git repository that contains the
//current working directory, or and error if the current working
//dir does not lie inside one.
func DiscoverRepository() (*Repository, error) {
cmd := exec.Command("git", "rev-parse", "--git-dir")
data, err := cmd.Output()
if err != nil {
return nil, err
}
path := strings.Trim(string(data), "\n ")
return &Repository{Path: path}, nil
}
//ReadDescription returns the contents of the description file.
func (repo *Repository) ReadDescription() string {
path := filepath.Join(repo.Path, "description")
dat, err := ioutil.ReadFile(path)
if err != nil {
return ""
}
return string(dat)
}
//WriteDescription writes the contents of the description file.
func (repo *Repository) WriteDescription(description string) error {
path := filepath.Join(repo.Path, "description")
// not atomic, fine for now
return ioutil.WriteFile(path, []byte(description), 0666)
}
// DeleteCollaborator removes a collaborator file from the repositories sharing folder.
func (repo *Repository) DeleteCollaborator(username string) error {
filePath := filepath.Join(repo.Path, "gin", "sharing", username)
return os.Remove(filePath)
}
//OpenObject returns the git object for a give id (SHA1).
func (repo *Repository) OpenObject(id SHA1) (Object, error) {
obj, err := repo.openRawObject(id)
if err != nil {
return nil, err
}
if IsStandardObject(obj.otype) {
return parseObject(obj)
}
//not a standard object, *must* be a delta object,
// we know of no other types
if !IsDeltaObject(obj.otype) {
return nil, fmt.Errorf("git: unsupported object")
}
delta, err := parseDelta(obj)
if err != nil {
return nil, err
}
chain, err := buildDeltaChain(delta, repo)
if err != nil {
return nil, err
}
//TODO: check depth, and especially expected memory usage
// beofre actually patching it
return chain.resolve()
}
func (repo *Repository) openRawObject(id SHA1) (gitObject, error) {
idstr := id.String()
opath := filepath.Join(repo.Path, "objects", idstr[:2], idstr[2:])
obj, err := openRawObject(opath)
if err == nil {
return obj, nil
} else if err != nil && !os.IsNotExist(err) {
return obj, err
}
indicies := repo.loadPackIndices()
for _, f := range indicies {
idx, err := PackIndexOpen(f)
if err != nil {
continue
}
//TODO: we should leave index files open,
defer idx.Close()
off, err := idx.FindOffset(id)
if err != nil {
continue
}
pf, err := idx.OpenPackFile()
if err != nil {
return gitObject{}, err
}
obj, err := pf.readRawObject(off)
if err != nil {
return gitObject{}, err
}
return obj, nil
}
// from inspecting the os.isNotExist source it
// seems that if we have "not found" in the message
// os.IsNotExist() report true, which is what we want
return gitObject{}, fmt.Errorf("git: object not found")
}
func (repo *Repository) loadPackIndices() []string {
target := filepath.Join(repo.Path, "objects", "pack", "*.idx")
files, err := filepath.Glob(target)
if err != nil {
panic(err)
}
return files
}
//OpenRef returns the Ref with the given name or an error
//if either no maching could be found or in case the match
//was not unique.
func (repo *Repository) OpenRef(name string) (Ref, error) {
if name == "HEAD" {
return repo.parseRef("HEAD")
}
matches := repo.listRefWithName(name)
//first search in local heads
var locals []Ref
for _, v := range matches {
if IsBranchRef(v) {
if name == v.Fullname() {
return v, nil
}
locals = append(locals, v)
}
}
// if we find a single local match
// we return it directly
if len(locals) == 1 {
return locals[0], nil
}
switch len(matches) {
case 0:
return nil, fmt.Errorf("git: ref matching %q not found", name)
case 1:
return matches[0], nil
}
return nil, fmt.Errorf("git: ambiguous ref name, multiple matches")
}
//Readlink returns the destination of a symbilc link blob object
func (repo *Repository) Readlink(id SHA1) (string, error) {
b, err := repo.OpenObject(id)
if err != nil {
return "", err
}
if b.Type() != ObjBlob {
return "", fmt.Errorf("id must point to a blob")
}
blob := b.(*Blob)
//TODO: check size and don't read unreasonable large blobs
data, err := ioutil.ReadAll(blob)
if err != nil {
return "", err
}
return string(data), nil
}
//ObjectForPath will resolve the path to an object
//for the file tree starting in the node root.
//The root object can be either a Commit, Tree or Tag.
func (repo *Repository) ObjectForPath(root Object, pathstr string) (Object, error) {
var node Object
var err error
switch o := root.(type) {
case *Tree:
node = root
case *Commit:
node, err = repo.OpenObject(o.Tree)
case *Tag:
node, err = repo.OpenObject(o.Object)
default:
return nil, fmt.Errorf("unsupported root object type")
}
if err != nil {
return nil, fmt.Errorf("could not root tree object: %v", err)
}
cleaned := path.Clean(strings.Trim(pathstr, " /"))
comps := strings.Split(cleaned, "/")
var i int
for i = 0; i < len(comps); i++ {
tree, ok := node.(*Tree)
if !ok {
cwd := strings.Join(comps[:i+1], "/")
err := &os.PathError{
Op: "convert git.Object to git.Tree",
Path: cwd,
Err: fmt.Errorf("expected tree object, got %s", node.Type()),
}
return nil, err
}
//Since we call path.Clean(), this should really
//only happen at the root, but it is safe to
//have here anyway
if comps[i] == "." || comps[i] == "/" {
continue
}
var id *SHA1
for tree.Next() {
entry := tree.Entry()
if entry.Name == comps[i] {
id = &entry.ID
break
}
}
if err = tree.Err(); err != nil {
cwd := strings.Join(comps[:i+1], "/")
return nil, &os.PathError{
Op: "find object",
Path: cwd,
Err: err}
} else if id == nil {
cwd := strings.Join(comps[:i+1], "/")
return nil, &os.PathError{
Op: "find object",
Path: cwd,
Err: os.ErrNotExist}
}
node, err = repo.OpenObject(*id)
if err != nil {
cwd := strings.Join(comps[:i+1], "/")
return nil, &os.PathError{
Op: "open object",
Path: cwd,
Err: err,
}
}
}
return node, nil
}
// usefmt is the option string used by CommitsForRef to return a formatted git commit log.
const usefmt = `--pretty=format:
Commit:=%H%n
Committer:=%cn%n
Author:=%an%n
Date-iso:=%ai%n
Date-rel:=%ar%n
Subject:=%s%n
Changes:=`
// CommitSummary represents a subset of information from a git commit.
type CommitSummary struct {
Commit string
Committer string
Author string
DateIso string
DateRelative string
Subject string
Changes []string
}
// CommitsForRef executes a custom git log command for the specified ref of the
// associated git repository and returns the resulting byte array.
func (repo *Repository) CommitsForRef(ref string) ([]CommitSummary, error) {
raw, err := commitsForRef(repo.Path, ref, usefmt)
if err != nil {
return nil, err
}
sep := ":="
var comList []CommitSummary
r := bytes.NewReader(raw)
br := bufio.NewReader(r)
var changesFlag bool
for {
// Consume line until newline character
l, err := br.ReadString('\n')
if strings.Contains(l, sep) {
splitList := strings.SplitN(l, sep, 2)
key := splitList[0]
val := splitList[1]
switch key {
case "Commit":
// reset non key line flags
changesFlag = false
newCommit := CommitSummary{Commit: val}
comList = append(comList, newCommit)
case "Committer":
comList[len(comList)-1].Committer = val
case "Author":
comList[len(comList)-1].Author = val
case "Date-iso":
comList[len(comList)-1].DateIso = val
case "Date-rel":
comList[len(comList)-1].DateRelative = val
case "Subject":
comList[len(comList)-1].Subject = val
case "Changes":
// Setting changes flag so we know, that the next lines are probably file change notification lines.
changesFlag = true
default:
fmt.Printf("[W] commits: unexpected key %q, value %q\n", key, strings.Trim(val, "\n"))
}
} else if changesFlag && strings.Contains(l, "\t") {
comList[len(comList)-1].Changes = append(comList[len(comList)-1].Changes, l)
}
// Breaks at the latest when EOF err is raised
if err != nil {
break
}
}
if err != io.EOF && err != nil {
return nil, err
}
return comList, nil
}
// commitsForRef executes a custom git log command for the specified ref of the
// given git repository with the specified log format string and returns the resulting byte array.
// Function is kept private to force handling of the []byte inside the package.
func commitsForRef(repoPath, ref, usefmt string) ([]byte, error) {
gdir := fmt.Sprintf("--git-dir=%s", repoPath)
cmd := exec.Command("git", gdir, "log", ref, usefmt, "--name-status")
body, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("failed running git log: %s\n", err.Error())
}
return body, nil
}
// BranchExists runs the "git branch <branchname> --list" command.
// It will return an error, if the command fails, true, if the result is not empty and false otherwise.
func (repo *Repository) BranchExists(branch string) (bool, error) {
gdir := fmt.Sprintf("--git-dir=%s", repo.Path)
cmd := exec.Command("git", gdir, "branch", branch, "--list")
body, err := cmd.Output()
if err != nil {
return false, err
} else if len(body) == 0 {
return false, nil
}
return true, nil
}