-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreference.go
149 lines (117 loc) · 3.32 KB
/
reference.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
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
143
144
145
146
147
148
149
package storage
import (
"database/sql"
"fmt"
"github.com/emphori/go-git-sql/utils"
"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"
)
type ReferenceStorage struct {
client *sql.DB
}
// Reference loads a Git reference from storage.
func (r *ReferenceStorage) Reference(name plumbing.ReferenceName) (*plumbing.Reference, error) {
rows, err := r.client.Query(`SELECT type, hash, name, target FROM "refs" WHERE name = $1;`, name)
if err != nil {
return nil, &plumbing.UnexpectedError{
Err: err,
}
}
defer rows.Close()
if !rows.Next() {
return nil, plumbing.ErrReferenceNotFound
}
obj, err := scanReference(rows)
if err != nil {
return nil, plumbing.ErrReferenceNotFound
}
return obj, nil
}
// IterReferences returns an iterator capable of walking through all available
// Git references.
func (r *ReferenceStorage) IterReferences() (storer.ReferenceIter, error) {
rows, err := r.client.Query(`SELECT type, hash, name, target FROM "refs";`)
if err != nil {
return nil, &plumbing.UnexpectedError{
Err: err,
}
}
return utils.NewIterator(rows, scanReference)
}
// SetReference writes a Git reference to storage.
func (r *ReferenceStorage) SetReference(ref *plumbing.Reference) error {
if _, err := r.client.Exec(`INSERT INTO refs(type, hash, name, target) VALUES($1, $2, $3, $4);`, ref.Type(), ref.Hash(), ref.Name(), ref.Target()); err != nil {
return &plumbing.UnexpectedError{
Err: err,
}
}
return nil
}
// RemoveReference deletes a Git reference from storage by its unique name.
func (r *ReferenceStorage) RemoveReference(name plumbing.ReferenceName) error {
if _, err := r.client.Exec(`DELETE FROM "refs" WHERE name = $1;`, name); err != nil {
return &plumbing.UnexpectedError{
Err: err,
}
}
return nil
}
func (r *ReferenceStorage) CheckAndSetReference(new, old *plumbing.Reference) error {
if new == nil {
return nil
}
if old != nil {
if tmp, _ := r.Reference(old.Name()); tmp != nil && tmp.Hash() != old.Hash() {
return storage.ErrReferenceHasChanged
}
}
tmp, _ := r.Reference(new.Name())
if tmp != nil {
if err := r.RemoveReference(new.Name()); err != nil {
return &plumbing.UnexpectedError{
Err: err,
}
}
}
return r.SetReference(new)
}
func (r *ReferenceStorage) CountLooseRefs() (int, error) {
query, err := r.client.Query(`SELECT COUNT(*) FROM "refs";`)
if err != nil {
return 0, &plumbing.UnexpectedError{
Err: err,
}
}
defer query.Close()
var count int
query.Scan(&count)
return count, nil
}
// PackRefs is not currently implemented.
func (r *ReferenceStorage) PackRefs() error {
return &plumbing.UnexpectedError{
Err: fmt.Errorf("not supported"),
}
}
func scanReference(row *sql.Rows) (*plumbing.Reference, error) {
var t plumbing.ReferenceType
var hash string
var name plumbing.ReferenceName
var target plumbing.ReferenceName
if err := row.Scan(&t, &hash, &name, &target); err != nil {
return nil, &plumbing.UnexpectedError{
Err: err,
}
}
switch t {
case plumbing.HashReference:
return plumbing.NewHashReference(name, plumbing.NewHash(hash)), nil
case plumbing.SymbolicReference:
return plumbing.NewSymbolicReference(name, target), nil
default:
return nil, &plumbing.UnexpectedError{
Err: fmt.Errorf("unhandled ref type: %s", t.String()),
}
}
}