forked from speedata/gogit
-
Notifications
You must be signed in to change notification settings - Fork 32
/
repo_object.go
103 lines (88 loc) · 2.1 KB
/
repo_object.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
package git
import (
"fmt"
"io"
"os"
)
// Who am I?
type ObjectType int
const (
ObjectCommit ObjectType = 0x10
ObjectTree ObjectType = 0x20
ObjectBlob ObjectType = 0x30
ObjectTag ObjectType = 0x40
)
func (t ObjectType) String() string {
switch t {
case ObjectCommit:
return "commit"
case ObjectTree:
return "tree"
case ObjectBlob:
return "blob"
default:
return ""
}
}
// Given a SHA1, find the pack it is in and the offset, or return nil if not
// found.
func (repo *Repository) findObjectPack(id sha1) (*idxFile, uint64) {
for _, indexfile := range repo.indexfiles {
if offset, ok := indexfile.offsetValues[id]; ok {
return indexfile, offset
}
}
return nil, 0
}
func (repo *Repository) HaveObject(idStr string) (found, packed bool, err error) {
id, err := NewIdFromString(idStr)
if err != nil {
return
}
return repo.haveObject(id)
}
func (repo *Repository) haveObject(id sha1) (found, packed bool, err error) {
sha1 := id.String()
_, err = os.Stat(filepathFromSHA1(repo.Path, sha1))
if err == nil {
found = true
return
} else if !os.IsNotExist(err) {
return
} else if os.IsNotExist(err) {
err = nil
}
pack, _ := repo.findObjectPack(id)
if pack == nil {
return
}
found, packed = true, true
return
}
func (repo *Repository) getRawObject(id sha1, metaOnly bool) (ObjectType, int64, io.ReadCloser, error) {
sha1 := id.String()
found, packed, err := repo.haveObject(id)
switch {
case err != nil:
return 0, 0, nil, err
case !found:
return 0, 0, nil, fmt.Errorf("Object not found %s", sha1)
case !packed:
return readObjectFile(filepathFromSHA1(repo.Path, sha1), metaOnly)
}
pack, offset := repo.findObjectPack(id)
return readObjectBytes(pack.packpath, &repo.indexfiles, offset, metaOnly)
}
// Get the type of an object.
func (repo *Repository) objectType(id sha1) (ObjectType, error) {
objtype, _, _, err := repo.getRawObject(id, true)
if err != nil {
return 0, err
}
return objtype, nil
}
// Get (inflated) size of an object.
func (repo *Repository) objectSize(id sha1) (int64, error) {
_, length, _, err := repo.getRawObject(id, true)
return length, err
}