forked from shazow/go-git
/
repo.go
61 lines (53 loc) · 1.01 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
package git
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
type Repository struct {
Path string
packs []*pack
commitCache map[ObjectID]*Commit
tagCache map[ObjectID]*Tag
}
func OpenRepository(path string) (*Repository, error) {
repo := &Repository{
Path: path,
}
path, err := filepath.Abs(path)
if err != nil {
return nil, err
}
fm, err := os.Stat(path)
if err != nil {
return nil, err
}
if !fm.IsDir() {
return nil, fmt.Errorf("%q is not a directory.", fm.Name())
}
packDir := filepath.Join(path, "objects", "pack")
infos, err := ioutil.ReadDir(packDir)
if err != nil {
return nil, err
}
for _, info := range infos {
if !strings.HasSuffix(info.Name(), ".pack") {
continue
}
repo.packs = append(repo.packs, &pack{
repo: repo,
id: info.Name()[:len(info.Name())-5],
})
}
return repo, nil
}
func (r *Repository) Close() (err error) {
for _, p := range r.packs {
if thisErr := p.Close(); thisErr != nil && err == nil {
err = thisErr
}
}
return nil
}