-
Notifications
You must be signed in to change notification settings - Fork 351
/
git.go
260 lines (235 loc) · 7.35 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package git
import (
"errors"
"fmt"
"io/fs"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"text/template"
"github.com/treeverse/lakefs/pkg/fileutil"
"golang.org/x/exp/slices"
)
const (
IgnoreFile = ".gitignore"
IgnoreDefaultMode = 0o644
NoRemoteRC = 2
)
var (
RemoteRegex = regexp.MustCompile(`(?P<server>[\w.:]+)[/:](?P<owner>[\w.-]+)/(?P<project>[\w.-]+)\.git$`)
CommitTemplates = map[string]string{
"github.com": "https://github.com/{{ .Owner }}/{{ .Project }}/commit/{{ .Ref }}",
"gitlab.com": "https://gitlab.com/{{ .Owner }}/{{ .Project }}/-/commit/{{ .Ref }}",
"bitbucket.org": "https://bitbucket.org/{{ .Owner }}/{{ .Project }}/commits/{{ .Ref }}",
}
)
type URL struct {
Server string
Owner string
Project string
}
func git(dir string, args ...string) (string, int, error) {
_, err := exec.LookPath("git") // assume git is in the path, otherwise consider as not having git support
if err != nil {
return "", 0, ErrNoGit
}
cmd := exec.Command("git", args...)
cmd.Dir = dir
out, err := cmd.CombinedOutput()
rc := 0
if err != nil {
var exitError *exec.ExitError
if errors.As(err, &exitError) {
rc = exitError.ExitCode()
} else {
rc = -1
}
}
return string(out), rc, err
}
// IsRepository Return true if dir is a path to a directory in a git repository, false otherwise
func IsRepository(dir string) bool {
_, _, err := git(dir, "rev-parse", "--is-inside-work-tree")
return err == nil
}
// GetRepositoryPath Returns the git repository root path if dir is a directory inside a git repository, otherwise returns error
func GetRepositoryPath(dir string) (string, error) {
out, _, err := git(dir, "rev-parse", "--show-toplevel")
return handleOutput(out, err)
}
func createEntriesForIgnore(dir string, paths []string, exclude bool) ([]string, error) {
var entries []string
for _, p := range paths {
pathInRepo, err := filepath.Rel(dir, p)
if err != nil {
return nil, fmt.Errorf("%s :%w", p, err)
}
isDir, err := fileutil.IsDir(p)
if err != nil && !errors.Is(err, fs.ErrNotExist) {
return nil, fmt.Errorf("%s :%w", p, err)
}
if isDir {
pathInRepo = filepath.Join(pathInRepo, "*")
}
if exclude {
pathInRepo = "!" + pathInRepo
}
entries = append(entries, pathInRepo)
}
return entries, nil
}
// updateIgnoreFileSection updates or inserts a section, identified by a marker, within a file's contents,
// and returns the modified contents as a byte slice. The section begins with "# [marker]" and ends with "# End [marker]".
// It retains existing entries and appends new entries from the provided slice.
func updateIgnoreFileSection(contents []byte, marker string, entries []string) []byte {
var lines []string
if len(contents) > 0 {
lines = strings.Split(string(contents), "\n")
}
// point to the existing section or to the end of the file
startIdx := slices.IndexFunc(lines, func(s string) bool {
return strings.HasPrefix(s, "# "+marker)
})
var endIdx int
if startIdx == -1 {
startIdx = len(lines)
endIdx = startIdx
} else {
endIdx = slices.IndexFunc(lines[startIdx:], func(s string) bool {
return s == "" || strings.HasPrefix(s, "# End "+marker)
})
if endIdx == -1 {
endIdx = len(lines)
} else {
endIdx += startIdx + 1
}
}
// collect existing entries - entries found in the section that are not commented out
var existing []string
for i := startIdx; i < endIdx; i++ {
if lines[i] == "" ||
strings.HasPrefix(lines[i], "#") ||
slices.Contains(entries, lines[i]) {
continue
}
existing = append(existing, lines[i])
}
// delete and insert new content
lines = slices.Delete(lines, startIdx, endIdx)
newContent := []string{"# " + marker}
newContent = append(newContent, existing...)
newContent = append(newContent, entries...)
newContent = append(newContent, "# End "+marker)
lines = slices.Insert(lines, startIdx, newContent...)
// join lines make sure content ends with new line
if lines[len(lines)-1] != "" {
lines = append(lines, "")
}
result := strings.Join(lines, "\n")
return []byte(result)
}
// Ignore modify/create .ignore file to include a section headed by the marker string and contains the provided ignore and exclude paths.
// If the section exists, it will append paths to the given section, otherwise writes the section at the end of the file.
// All file paths must be absolute.
// dir is a path in the git repository, if a .gitignore file is not found, a new file will be created in the repository root
func Ignore(dir string, ignorePaths, excludePaths []string, marker string) (string, error) {
gitDir, err := GetRepositoryPath(dir)
if err != nil {
return "", err
}
ignoreEntries, err := createEntriesForIgnore(gitDir, ignorePaths, false)
if err != nil {
return "", err
}
excludeEntries, err := createEntriesForIgnore(gitDir, excludePaths, true)
if err != nil {
return "", err
}
ignoreEntries = append(ignoreEntries, excludeEntries...)
// read ignore file content
ignoreFilePath := filepath.Join(gitDir, IgnoreFile)
ignoreFile, err := os.ReadFile(ignoreFilePath)
if err != nil && !os.IsNotExist(err) {
return "", err
}
// get current file mode, if exists
var mode os.FileMode = IgnoreDefaultMode
if ignoreFile != nil {
if info, err := os.Stat(ignoreFilePath); err == nil {
mode = info.Mode()
}
}
// update ignore file local section and write back
ignoreFile = updateIgnoreFileSection(ignoreFile, marker, ignoreEntries)
if err = os.WriteFile(ignoreFilePath, ignoreFile, mode); err != nil {
return "", err
}
return ignoreFilePath, nil
}
func CurrentCommit(path string) (string, error) {
out, _, err := git(path, "rev-parse", "--short", "HEAD")
return handleOutput(out, err)
}
func MetadataFor(path, ref string) (map[string]string, error) {
kv := make(map[string]string)
kv["git_commit_id"] = ref
originURL, err := Origin(path)
if errors.Is(err, ErrRemoteNotFound) {
return kv, nil // no additional data to add
} else if err != nil {
return kv, err
}
parsed := ParseURL(originURL)
if parsed != nil {
if tmpl, ok := CommitTemplates[parsed.Server]; ok {
t := template.Must(template.New("url").Parse(tmpl))
out := new(strings.Builder)
_ = t.Execute(out, struct {
Owner string
Project string
Ref string
}{
Owner: parsed.Owner,
Project: parsed.Project,
Ref: ref,
})
kv[fmt.Sprintf("::lakefs::%s::url[url:ui]", parsed.Server)] = out.String()
}
}
return kv, nil
}
func Origin(path string) (string, error) {
out, rc, err := git(path, "remote", "get-url", "origin")
if rc == NoRemoteRC {
// from Git's man page:
// "When subcommands such as add, rename, and remove can’t find the remote in question,
// the exit status is 2"
return "", nil
}
return handleOutput(out, err)
}
func ParseURL(raw string) *URL {
matches := RemoteRegex.FindStringSubmatch(raw)
if matches == nil { // TODO niro: How to handle better changes in templates?
return nil
}
return &URL{
Server: matches[RemoteRegex.SubexpIndex("server")],
Owner: matches[RemoteRegex.SubexpIndex("owner")],
Project: matches[RemoteRegex.SubexpIndex("project")],
}
}
func handleOutput(out string, err error) (string, error) {
switch {
case err == nil:
return strings.TrimSpace(out), nil
case strings.Contains(out, "not a git repository"):
return "", ErrNotARepository
case strings.Contains(out, "remote not found"):
return "", ErrRemoteNotFound
default:
return "", fmt.Errorf("%s: %w", out, err)
}
}