/
utils.go
177 lines (148 loc) · 4.72 KB
/
utils.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// SPDX-License-Identifier: Apache-2.0
package gitinterface
import (
"errors"
"fmt"
"path"
"strings"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/jonboulle/clockwork"
)
const (
RefPrefix = "refs/"
BranchRefPrefix = "refs/heads/"
TagRefPrefix = "refs/tags/"
RemoteRefPrefix = "refs/remotes/"
)
var (
ErrReferenceNotFound = plumbing.ErrReferenceNotFound
clock = clockwork.NewRealClock()
)
// GetTip returns the hash of the tip of the specified ref.
func GetTip(repo *git.Repository, refName string) (plumbing.Hash, error) {
ref, err := repo.Reference(plumbing.ReferenceName(refName), true)
if err != nil {
return plumbing.ZeroHash, err
}
return ref.Hash(), nil
}
// ResetCommit sets a Git reference with the name refName to the commit
// specified by its hash as commitID. Note that the commit must already be in
// the repository's object store.
func ResetCommit(repo *git.Repository, refName string, commitID plumbing.Hash) error {
currentHEAD, err := repo.Head()
if err != nil {
return err
}
wt, err := repo.Worktree()
if err != nil {
return err
}
if err := wt.Checkout(&git.CheckoutOptions{Branch: plumbing.ReferenceName(refName)}); err != nil {
return err
}
if err := wt.Reset(&git.ResetOptions{Commit: commitID, Mode: git.MergeReset}); err != nil {
return err
}
if currentHEAD.Type() == plumbing.HashReference {
return wt.Checkout(&git.CheckoutOptions{Hash: currentHEAD.Hash()})
}
return wt.Checkout(&git.CheckoutOptions{Branch: currentHEAD.Name()})
}
// ResetDueToError is a helper used to reverse a change applied to a ref due to
// an error encountered after the change but part of the same operation. This
// ensures that gittuf operations are atomic. Otherwise, a repository may enter
// a violation state where a ref is updated without accompanying RSL entries or
// other metadata changes.
func ResetDueToError(cause error, repo *git.Repository, refName string, commitID plumbing.Hash) error {
if err := ResetCommit(repo, refName, commitID); err != nil {
return fmt.Errorf("unable to reset %s to %s, caused by following error: %w", refName, commitID.String(), cause)
}
return cause
}
// AbsoluteReference returns the fully qualified reference path for the provided
// Git ref.
func AbsoluteReference(repo *git.Repository, target string) (string, error) {
if strings.HasPrefix(target, RefPrefix) {
return target, nil
}
if target == plumbing.HEAD.String() {
ref, err := repo.Reference(plumbing.HEAD, false)
if err != nil {
return "", err
}
return ref.Target().String(), nil
}
// Check if branch
refName := plumbing.NewBranchReferenceName(target)
_, err := repo.Reference(refName, false)
if err == nil {
return string(refName), nil
}
if !errors.Is(err, plumbing.ErrReferenceNotFound) {
return "", err
}
// Check if tag
refName = plumbing.NewTagReferenceName(target)
_, err = repo.Reference(refName, false)
if err == nil {
return string(refName), nil
}
if !errors.Is(err, plumbing.ErrReferenceNotFound) {
return "", err
}
return "", ErrReferenceNotFound
}
// RefSpec creates a Git refspec for the specified ref. For more information on
// the Git refspec, please consult:
// https://git-scm.com/book/en/v2/Git-Internals-The-Refspec.
func RefSpec(repo *git.Repository, refName, remoteName string, fastForwardOnly bool) (config.RefSpec, error) {
var (
refPath string
err error
)
refPath = refName
if !strings.HasPrefix(refPath, RefPrefix) {
refPath, err = AbsoluteReference(repo, refName)
if err != nil {
return "", err
}
}
if strings.HasPrefix(refPath, TagRefPrefix) {
// TODO: check if this is correct, AFAICT tags aren't tracked in the
// remotes namespace.
fastForwardOnly = true
}
// local is always refPath, destination depends on remoteName
localPath := refPath
var remotePath string
if len(remoteName) > 0 {
remotePath = RemoteRef(refPath, remoteName)
} else {
remotePath = refPath
}
refSpecString := fmt.Sprintf("%s:%s", localPath, remotePath)
if !fastForwardOnly {
refSpecString = fmt.Sprintf("+%s", refSpecString)
}
return config.RefSpec(refSpecString), nil
}
func RemoteRef(refName, remoteName string) string {
var remotePath string
switch {
case strings.HasPrefix(refName, BranchRefPrefix):
// refs/heads/<path> -> refs/remotes/<remote>/<path>
rest := strings.TrimPrefix(refName, BranchRefPrefix)
remotePath = path.Join(RemoteRefPrefix, remoteName, rest)
case strings.HasPrefix(refName, TagRefPrefix):
// refs/tags/<path> -> refs/tags/<path>
remotePath = refName
default:
// refs/<path> -> refs/remotes/<remote>/<path>
rest := strings.TrimPrefix(refName, RefPrefix)
remotePath = path.Join(RemoteRefPrefix, remoteName, rest)
}
return remotePath
}