Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathgitmap.go
More file actions
Latest commit
212 lines (183 loc) · 5.72 KB
/
Copy pathgitmap.go
File metadata and controls
212 lines (183 loc) · 5.72 KB
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
// Copyright 2024 Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package gitmap
import (
"bytes"
"errors"
"fmt"
"io"
"os/exec"
"path/filepath"
"strings"
"time"
)
var (
// will be modified during tests
gitExecstring
ErrGitNotFound=errors.New("git executable not found in $PATH")
)
typeGitRepostruct {
// TopLevelAbsPath contains the absolute path of the top-level directory.
// This is similar to the answer from "git rev-parse --show-toplevel"
// except symbolic link is not followed on non-Windows platforms.
// Note that this follows Git's way of handling paths, so expect to get forward slashes,
// even on Windows.
TopLevelAbsPathstring
// The files in this Git repository.
FilesGitMap
}
// GitMap maps filenames to Git revision information.
typeGitMapmap[string]*GitInfo
// GitInfo holds information about a Git commit.
typeGitInfostruct {
Hashstring`json:"hash"`// Commit hash
AbbreviatedHashstring`json:"abbreviatedHash"`// Abbreviated commit hash
Subjectstring`json:"subject"`// The commit message's subject/title line
AuthorNamestring`json:"authorName"`// The author name, respecting .mailmap
AuthorEmailstring`json:"authorEmail"`// The author email address, respecting .mailmap
AuthorDate time.Time`json:"authorDate"`// The author date
CommitDate time.Time`json:"commitDate"`// The commit date
Bodystring`json:"body"`// The commit message body
Parent*GitInfo`json:"-"`// The file-filtered ancestor commit, if any
}
// Ancestors returns a slice of GitInfo objects representing the ancestors.
func (g*GitInfo) Ancestors() GitInfos {
varancestorsGitInfos
forparent:=g.Parent; parent!=nil; parent=parent.Parent {
ancestors=append(ancestors, parent)
}
returnancestors
}
typeGitInfos []*GitInfo
// Reverse creates a copy of the GitInfos slice in reverse order.
func (gGitInfos) Reverse() GitInfos {
reversed:=make(GitInfos, len(g))
fori, v:=rangeg {
reversed[len(g)-1-i] =v
}
returnreversed
}
// Runner is an interface for running Git commands,
// as implemented buy *exec.Cmd.
typeRunnerinterface {
Run() error
}
// Options for the Map function
typeOptionsstruct {
Repositorystring// Path to the repository to map
Revisionstring// Use blank or HEAD for the currently active revision
GetGitCommandFuncfunc(stdout, stderr io.Writer, args...string) (Runner, error)
}
// Map creates a GitRepo with a file map from the given options.
funcMap(optsOptions) (*GitRepo, error) {
ifopts.GetGitCommandFunc==nil {
opts.GetGitCommandFunc=func(stdout, stderr io.Writer, args...string) (Runner, error) {
cmd:=exec.Command(gitExec, args...)
cmd.Stdout=stdout
cmd.Stderr=stderr
returncmd, nil
}
}
m:=make(GitMap)
a:=make(GitMap)
// First get the top level repo path
absRepoPath, err:=filepath.Abs(opts.Repository)
iferr!=nil {
returnnil, err
}
out, err:=git(opts, "-C", opts.Repository, "rev-parse", "--show-cdup")
iferr!=nil {
returnnil, err
}
cdUp:=strings.TrimSpace(string(out))
topLevelPath:=filepath.ToSlash(filepath.Join(absRepoPath, cdUp))
gitLogArgs:=strings.Fields(fmt.Sprintf(
`--name-only --no-merges --format=format:%%x1e%%H%%x1f%%h%%x1f%%s%%x1f%%aN%%x1f%%aE%%x1f%%ai%%x1f%%ci%%x1f%%b%%x1d %s`,
opts.Revision,
))
gitLogArgs=append([]string{"-c", "diff.renames=0", "-c", "log.showSignature=0", "-C", opts.Repository, "log"}, gitLogArgs...)
out, err=git(opts, gitLogArgs...)
iferr!=nil {
returnnil, err
}
entriesStr:=strings.Trim(out, "\n\x1e'")
entries:=strings.SplitSeq(entriesStr, "\x1e")
fore:=rangeentries {
lines:=strings.Split(e, "\x1d")
gitInfo, err:=toGitInfo(lines[0])
iferr!=nil {
returnnil, err
}
filenames:=strings.SplitSeq(lines[1], "\n")
forfilename:=rangefilenames {
filename:=strings.TrimSpace(filename)
iffilename=="" {
continue
}
// Cannot reuse because each GitInfo object has its own ancestor
// gitInfo.Ancestor is always nil at this point, so we're copying
gitInfoCopy:=*gitInfo
ifrootInfo, ok:=m[filename]; !ok {
m[filename] =&gitInfoCopy
} else {
varancInfo*GitInfo
ifancInfo, ok=a[filename]; !ok {
ancInfo=rootInfo
}
ancInfo.Parent=&gitInfoCopy
a[filename] =ancInfo.Parent
}
}
}
return&GitRepo{Files: m, TopLevelAbsPath: topLevelPath}, nil
}
funcgit(optsOptions, args...string) (string, error) {
varoutBuff bytes.Buffer
varerrBuff bytes.Buffer
cmd, err:=opts.GetGitCommandFunc(&outBuff, &errBuff, args...)
iferr!=nil {
return"", err
}
err=cmd.Run()
iferr!=nil {
ifee, ok:=err.(*exec.Error); ok {
ifee.Err==exec.ErrNotFound {
return"", ErrGitNotFound
}
}
return"", errors.New(strings.TrimSpace(errBuff.String()))
}
returnoutBuff.String(), nil
}
functoGitInfo(entrystring) (*GitInfo, error) {
items:=strings.Split(entry, "\x1f")
iflen(items) ==7 {
items=append(items, "")
}
authorDate, err:=time.Parse("2006-01-02 15:04:05 -0700", items[5])
iferr!=nil {
returnnil, err
}
commitDate, err:=time.Parse("2006-01-02 15:04:05 -0700", items[6])
iferr!=nil {
returnnil, err
}
return&GitInfo{
Hash: items[0],
AbbreviatedHash: items[1],
Subject: items[2],
AuthorName: items[3],
AuthorEmail: items[4],
AuthorDate: authorDate,
CommitDate: commitDate,
Body: strings.TrimSpace(items[7]),
}, nil
}
funcinit() {
initDefaults()
}
funcinitDefaults() {
gitExec="git"
}