-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommit.go
More file actions
142 lines (131 loc) · 3.76 KB
/
Copy pathcommit.go
File metadata and controls
142 lines (131 loc) · 3.76 KB
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
package history
import (
"bufio"
"bytes"
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"sync"
gogit "github.com/go-git/go-git/v6"
"github.com/go-git/go-git/v6/plumbing"
)
type objectID [sha256.Size]byte
const rawObjectBufferSize = 8 << 10
var rawObjectReaderPool = sync.Pool{
New: func() any { return bufio.NewReaderSize(nil, rawObjectBufferSize) },
}
// VisitCommitTrees walks every commit reachable from the repository's refs
// and invokes visit with each commit's root tree hash. Commits beyond a
// shallow boundary are treated as having no parents.
func (r *Repo) VisitCommitTrees(visit func(plumbing.Hash) error) error {
roots, err := historyRoots(r.r)
if err != nil {
return err
}
shallow, err := r.r.Storer.Shallow()
if err != nil {
return err
}
boundary := make(map[plumbing.Hash]bool, len(shallow))
for _, hash := range shallow {
boundary[hash] = true
}
return visitCommitTrees(r.r, roots, boundary, visit)
}
func visitCommitTrees(r *gogit.Repository, roots []plumbing.Hash, boundary map[plumbing.Hash]bool, visit func(plumbing.Hash) error) error {
seen := make(map[plumbing.Hash]struct{})
stack := append([]plumbing.Hash(nil), roots...)
for len(stack) > 0 {
last := len(stack) - 1
hash := stack[last]
stack = stack[:last]
if _, ok := seen[hash]; ok {
continue
}
tree, err := readCommitTreeAndParents(r, hash, !boundary[hash], &stack)
if err != nil {
return err
}
seen[hash] = struct{}{}
if err := visit(tree); err != nil {
return err
}
}
return nil
}
func readCommitTreeAndParents(r *gogit.Repository, hash plumbing.Hash, includeParents bool, parents *[]plumbing.Hash) (tree plumbing.Hash, resultErr error) {
encoded, err := r.Storer.EncodedObject(plumbing.CommitObject, hash)
if err != nil {
return tree, err
}
if encoded.Type() != plumbing.CommitObject {
return tree, fmt.Errorf("object %s is %s, want commit", hash, encoded.Type())
}
reader, err := encoded.Reader()
if err != nil {
return tree, err
}
defer func() {
if err := reader.Close(); resultErr == nil {
resultErr = err
}
}()
buffer := rawObjectReaderPool.Get().(*bufio.Reader)
buffer.Reset(reader)
defer func() {
buffer.Reset(nil)
rawObjectReaderPool.Put(buffer)
}()
line, readErr := buffer.ReadSlice('\n')
if readErr != nil && (readErr != io.EOF || len(line) == 0) {
return tree, fmt.Errorf("commit %s tree: %w", hash, readErr)
}
line = bytes.TrimSuffix(line, []byte{'\n'})
value, ok := bytes.CutPrefix(line, []byte("tree "))
if !ok {
return tree, fmt.Errorf("commit %s has no leading tree header", hash)
}
tree, err = parseCommitObjectID(value, hash.Size())
if err != nil {
return tree, fmt.Errorf("commit %s tree: %w", hash, err)
}
if readErr == io.EOF {
return tree, nil
}
for {
line, readErr = buffer.ReadSlice('\n')
if readErr != nil && (readErr != io.EOF || len(line) == 0) {
return tree, fmt.Errorf("commit %s parent: %w", hash, readErr)
}
line = bytes.TrimSuffix(line, []byte{'\n'})
value, ok = bytes.CutPrefix(line, []byte("parent "))
if !ok {
return tree, nil
}
parent, err := parseCommitObjectID(value, hash.Size())
if err != nil {
return tree, fmt.Errorf("commit %s parent: %w", hash, err)
}
if includeParents {
*parents = append(*parents, parent)
}
if readErr == io.EOF {
return tree, nil
}
}
}
func parseCommitObjectID(value []byte, size int) (plumbing.Hash, error) {
if len(value) != hex.EncodedLen(size) {
return plumbing.ZeroHash, fmt.Errorf("invalid object ID %q", value)
}
var decoded objectID
if _, err := hex.Decode(decoded[:size], value); err != nil {
return plumbing.ZeroHash, fmt.Errorf("invalid object ID %q: %w", value, err)
}
hash, ok := plumbing.FromBytes(decoded[:size])
if !ok {
return plumbing.ZeroHash, fmt.Errorf("invalid object ID %q", value)
}
return hash, nil
}