forked from jfrog/jfrog-client-go
/
git.go
208 lines (188 loc) · 4.37 KB
/
git.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package utils
import (
"bufio"
"errors"
"os"
"path/filepath"
"strings"
"github.com/cobalt77/jfrog-client-go/utils/errorutils"
"github.com/cobalt77/jfrog-client-go/utils/io/fileutils"
)
type manager struct {
path string
err error
revision string
url string
}
func NewGitManager(path string) *manager {
dotGitPath := filepath.Join(path, ".git")
return &manager{path: dotGitPath}
}
func (m *manager) ReadConfig() error {
if m.path == "" {
return errorutils.CheckError(errors.New(".git path must be defined."))
}
m.readRevision()
m.readUrl()
return m.err
}
func (m *manager) GetUrl() string {
return m.url
}
func (m *manager) GetRevision() string {
return m.revision
}
func (m *manager) readUrl() {
if m.err != nil {
return
}
dotGitPath := filepath.Join(m.path, "config")
file, err := os.Open(dotGitPath)
if errorutils.CheckError(err) != nil {
m.err = err
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
var IsNextLineUrl bool
var originUrl string
for scanner.Scan() {
if IsNextLineUrl {
text := scanner.Text()
strings.HasPrefix(text, "url")
originUrl = strings.TrimSpace(strings.SplitAfter(text, "=")[1])
break
}
if scanner.Text() == "[remote \"origin\"]" {
IsNextLineUrl = true
}
}
if err := scanner.Err(); err != nil {
errorutils.CheckError(err)
m.err = err
return
}
if !strings.HasSuffix(originUrl, ".git") {
originUrl += ".git"
}
m.url = originUrl
// Mask url if required
regExp, err := GetRegExp(CredentialsInUrlRegexp)
if err != nil {
m.err = err
return
}
matchedResult := regExp.FindString(originUrl)
if matchedResult == "" {
return
}
m.url = MaskCredentials(originUrl, matchedResult)
}
func (m *manager) getRevisionOrBranchPath() (revision, refUrl string, err error) {
dotGitPath := filepath.Join(m.path, "HEAD")
file, e := os.Open(dotGitPath)
if errorutils.CheckError(e) != nil {
err = e
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
text := scanner.Text()
if strings.HasPrefix(text, "ref") {
refUrl = strings.TrimSpace(strings.SplitAfter(text, ":")[1])
break
}
revision = text
}
if err = scanner.Err(); err != nil {
errorutils.CheckError(err)
}
return
}
func (m *manager) readRevision() {
if m.err != nil {
return
}
// This function will either return the revision or the branch ref:
revision, ref, err := m.getRevisionOrBranchPath()
if err != nil {
m.err = err
return
}
// If the revision was returned, then we're done:
if revision != "" {
m.revision = revision
return
}
// Else, if found ref try getting revision using it.
refPath := filepath.Join(m.path, ref)
exists, err := fileutils.IsFileExists(refPath, false)
if err != nil {
m.err = err
return
}
if exists {
m.readRevisionFromRef(refPath)
return
}
// Otherwise, try to find .git/packed-refs and look for the HEAD there
m.readRevisionFromPackedRef(ref)
}
func (m *manager) readRevisionFromRef(refPath string) {
revision := ""
file, err := os.Open(refPath)
if errorutils.CheckError(err) != nil {
m.err = err
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
text := scanner.Text()
revision = strings.TrimSpace(text)
break
}
if err := scanner.Err(); err != nil {
m.err = errorutils.CheckError(err)
return
}
m.revision = revision
return
}
func (m *manager) readRevisionFromPackedRef(ref string) {
packedRefPath := filepath.Join(m.path, "packed-refs")
exists, err := fileutils.IsFileExists(packedRefPath, false)
if err != nil {
m.err = err
return
}
if exists {
file, err := os.Open(packedRefPath)
if errorutils.CheckError(err) != nil {
m.err = err
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
// Expecting to find the revision (the full extended SHA-1, or a unique leading substring) followed by the ref.
if strings.HasSuffix(line, ref) {
split := strings.Split(line, " ")
if len(split) == 2 {
m.revision = split[0]
} else {
m.err = errorutils.CheckError(errors.New("failed fetching revision for ref :" + ref + " - Unexpected line structure in packed-refs file"))
}
return
}
}
if err = scanner.Err(); err != nil {
m.err = errorutils.CheckError(err)
return
}
}
m.err = errorutils.CheckError(errors.New("failed fetching revision from git config, from ref: " + ref))
return
}