-
Notifications
You must be signed in to change notification settings - Fork 0
/
repo_object.go
79 lines (71 loc) · 2 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
package git
import (
"errors"
"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 ""
}
}
func (repo *Repository) getRawObject(id sha1, metaOnly bool) (ObjectType, int64, io.ReadCloser, error) {
// first we need to find out where the commit is stored
sha1 := id.String()
objpath := filepathFromSHA1(repo.Path, sha1)
_, err := os.Stat(objpath)
if os.IsNotExist(err) {
// doesn't exist, let's look if we find the object somewhere else
for _, indexfile := range repo.indexfiles {
if offset := indexfile.offsetValues[id]; offset != 0 {
return readObjectBytes(indexfile.packpath, &repo.indexfiles, offset, metaOnly)
}
}
return 0, 0, nil, errors.New(fmt.Sprintf("Object not found %s", sha1))
}
return readObjectFile(objpath, 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) {
sha1 := id.String()
// todo: this is mostly the same as getRawObject -> merge
// difference is the boolean in readObjectBytes and readObjectFile
objpath := filepathFromSHA1(repo.Path, sha1)
_, err := os.Stat(objpath)
if os.IsNotExist(err) {
// doesn't exist, let's look if we find the object somewhere else
for _, indexfile := range repo.indexfiles {
if offset := indexfile.offsetValues[id]; offset != 0 {
_, length, _, err := readObjectBytes(indexfile.packpath, &repo.indexfiles, offset, true)
return length, err
}
}
return 0, errors.New("Object not found")
}
_, length, _, err := readObjectFile(objpath, true)
return length, err
}