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
|
package transactional
import (
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/storer"
"github.com/go-git/go-git/v5/storage"
)
// ReferenceStorage implements the storer.ReferenceStorage for the transactional package.
type ReferenceStorage struct {
storer.ReferenceStorer
temporal storer.ReferenceStorer
// deleted, remaining references at this maps are going to be deleted when
// commit is requested, the entries are added when RemoveReference is called
// and deleted if SetReference is called.
deleted map[plumbing.ReferenceName]struct{}
// packRefs if true PackRefs is going to be called in the based storer when
// commit is called.
packRefs bool
}
// NewReferenceStorage returns a new ReferenceStorer based on a base storer and
// a temporal storer.
func NewReferenceStorage(base, temporal storer.ReferenceStorer) *ReferenceStorage {
return &ReferenceStorage{
ReferenceStorer: base,
temporal: temporal,
deleted: make(map[plumbing.ReferenceName]struct{}, 0),
}
}
// SetReference honors the storer.ReferenceStorer interface.
func (r *ReferenceStorage) SetReference(ref *plumbing.Reference) error {
delete(r.deleted, ref.Name())
return r.temporal.SetReference(ref)
}
// SetReference honors the storer.ReferenceStorer interface.
func (r *ReferenceStorage) CheckAndSetReference(ref, old *plumbing.Reference) error {
if old == nil {
return r.SetReference(ref)
}
tmp, err := r.temporal.Reference(old.Name())
if err == plumbing.ErrReferenceNotFound {
tmp, err = r.ReferenceStorer.Reference(old.Name())
}
if err != nil {
return err
}
if tmp.Hash() != old.Hash() {
return storage.ErrReferenceHasChanged
}
return r.SetReference(ref)
}
// Reference honors the storer.ReferenceStorer interface.
func (r ReferenceStorage) Reference(n plumbing.ReferenceName) (*plumbing.Reference, error) {
if _, deleted := r.deleted[n]; deleted {
return nil, plumbing.ErrReferenceNotFound
}
ref, err := r.temporal.Reference(n)
if err == plumbing.ErrReferenceNotFound {
return r.ReferenceStorer.Reference(n)
}
return ref, err
}
// IterReferences honors the storer.ReferenceStorer interface.
func (r ReferenceStorage) IterReferences() (storer.ReferenceIter, error) {
baseIter, err := r.ReferenceStorer.IterReferences()
if err != nil {
return nil, err
}
temporalIter, err := r.temporal.IterReferences()
if err != nil {
return nil, err
}
return storer.NewMultiReferenceIter([]storer.ReferenceIter{
baseIter,
temporalIter,
}), nil
}
// CountLooseRefs honors the storer.ReferenceStorer interface.
func (r ReferenceStorage) CountLooseRefs() (int, error) {
tc, err := r.temporal.CountLooseRefs()
if err != nil {
return -1, err
}
bc, err := r.ReferenceStorer.CountLooseRefs()
if err != nil {
return -1, err
}
return tc + bc, nil
}
// PackRefs honors the storer.ReferenceStorer interface.
func (r ReferenceStorage) PackRefs() error {
r.packRefs = true
return nil
}
// RemoveReference honors the storer.ReferenceStorer interface.
func (r ReferenceStorage) RemoveReference(n plumbing.ReferenceName) error {
r.deleted[n] = struct{}{}
return r.temporal.RemoveReference(n)
}
// Commit it copies the reference information of the temporal storage into the
// base storage.
func (r ReferenceStorage) Commit() error {
for name := range r.deleted {
if err := r.ReferenceStorer.RemoveReference(name); err != nil {
return err
}
}
iter, err := r.temporal.IterReferences()
if err != nil {
return err
}
return iter.ForEach(func(ref *plumbing.Reference) error {
return r.ReferenceStorer.SetReference(ref)
})
}
|