-
Notifications
You must be signed in to change notification settings - Fork 1
/
modified_files_regexp_matches.go
228 lines (209 loc) · 6.68 KB
/
modified_files_regexp_matches.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
// Copyright 2024 Aviator Technologies, Inc.
// SPDX-License-Identifier: MIT
package nichegit
import (
"bytes"
"fmt"
"io"
"net/http"
"regexp"
"sort"
"github.com/aviator-co/niche-git/debug"
"github.com/aviator-co/niche-git/internal/diff"
"github.com/aviator-co/niche-git/internal/fetch"
"github.com/bmatcuk/doublestar"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/format/packfile"
"github.com/go-git/go-git/v5/storage/memory"
)
type ModifiedFilePattern struct {
// FilePathPattern is a list of doublestar matching patterns for the file paths.
FilePathPattern []string
// FileContentPattern is an optional regular expression pattern for the file content.
FileContentPattern *regexp.Regexp
}
type ModificationStatus string
const (
ModificationStatusAdded ModificationStatus = "ADDED"
ModificationStatusDeleted ModificationStatus = "DELETED"
ModificationStatusModified ModificationStatus = "MODIFIED"
)
type ModifiedFilePatternMatch struct {
Before int `json:"before"`
After int `json:"after"`
}
type ModifiedFile struct {
Path string `json:"path"`
Status ModificationStatus `json:"status"`
Matches map[string]*ModifiedFilePatternMatch `json:"matches,omitempty"`
}
func FetchModifiedFilesWithRegexpMatch(
repoURL string,
client *http.Client,
commitHash1, commitHash2 plumbing.Hash,
patterns map[string]ModifiedFilePattern,
) ([]*ModifiedFile, debug.FetchDebugInfo, *debug.FetchDebugInfo, error) {
packfilebs, fetchDebugInfo, err := fetch.FetchBlobNonePackfile(repoURL, client, []plumbing.Hash{commitHash1, commitHash2})
if err != nil {
return nil, fetchDebugInfo, nil, err
}
storage := memory.NewStorage()
parser, err := packfile.NewParserWithStorage(packfile.NewScanner(bytes.NewReader(packfilebs)), storage)
if err != nil {
return nil, fetchDebugInfo, nil, fmt.Errorf("failed to parse packfile: %v", err)
}
if _, err := parser.Parse(); err != nil {
return nil, fetchDebugInfo, nil, fmt.Errorf("failed to parse packfile: %v", err)
}
tree1, err := getTreeFromCommit(storage, commitHash1)
if err != nil {
return nil, fetchDebugInfo, nil, err
}
tree2, err := getTreeFromCommit(storage, commitHash2)
if err != nil {
return nil, fetchDebugInfo, nil, err
}
modified, err := diff.DiffTree(storage, tree1, tree2)
if err != nil {
return nil, fetchDebugInfo, nil, fmt.Errorf("failed to take file diffs: %v", err)
}
packfilebs, fetchBlobDebugInfo, err := fetch.FetchBlobPackfile(repoURL, client, getBlobHashes(modified))
blobFetchDebugInfo := &fetchBlobDebugInfo
if err != nil {
return nil, fetchDebugInfo, blobFetchDebugInfo, err
}
parser, err = packfile.NewParserWithStorage(packfile.NewScanner(bytes.NewReader(packfilebs)), storage)
if err != nil {
return nil, fetchDebugInfo, blobFetchDebugInfo, fmt.Errorf("failed to parse packfile: %v", err)
}
if _, err := parser.Parse(); err != nil {
return nil, fetchDebugInfo, blobFetchDebugInfo, fmt.Errorf("failed to parse packfile: %v", err)
}
var ret []*ModifiedFile
for path, hashes := range modified {
m, err := matchWithPattern(storage, patterns, path, hashes)
if err != nil {
return nil, fetchDebugInfo, blobFetchDebugInfo, err
}
ret = append(ret, m)
}
sort.Slice(ret, func(i, j int) bool {
return ret[i].Path < ret[j].Path
})
return ret, fetchDebugInfo, blobFetchDebugInfo, nil
}
func getBlobHashes(modified map[string]diff.BlobHashes) []plumbing.Hash {
m := map[plumbing.Hash]struct{}{}
for _, hashes := range modified {
if !hashes.BlobHash1.IsZero() {
m[hashes.BlobHash1] = struct{}{}
}
if !hashes.BlobHash2.IsZero() {
m[hashes.BlobHash2] = struct{}{}
}
}
hashes := make([]plumbing.Hash, 0, len(m))
for hash := range m {
hashes = append(hashes, hash)
}
return hashes
}
func matchWithPattern(storage *memory.Storage, patterns map[string]ModifiedFilePattern, path string, blobs diff.BlobHashes) (*ModifiedFile, error) {
status := ModificationStatusModified
var content1 []byte
var content2 []byte
if blobs.BlobHash1.IsZero() {
// File is added.
status = ModificationStatusAdded
} else {
var err error
content1, err = getBlobContent(storage, blobs.BlobHash1)
if err != nil {
return nil, fmt.Errorf("cannot read the file %q: %v", path, err)
}
}
if blobs.BlobHash2.IsZero() {
// File is deleted.
status = ModificationStatusDeleted
} else {
var err error
content2, err = getBlobContent(storage, blobs.BlobHash2)
if err != nil {
return nil, fmt.Errorf("cannot read the file %q: %v", path, err)
}
}
matches := map[string]*ModifiedFilePatternMatch{}
for name, pattern := range patterns {
match, err := matchPattern(pattern, path, status, content1, content2)
if err != nil {
return nil, fmt.Errorf("cannot match the pattern %q: %v", name, err)
}
if match != nil {
matches[name] = match
}
}
ret := &ModifiedFile{
Path: path,
Status: status,
}
if len(matches) > 0 {
ret.Matches = matches
}
return ret, nil
}
func getBlobContent(storage *memory.Storage, hash plumbing.Hash) ([]byte, error) {
blob, err := storage.EncodedObject(plumbing.BlobObject, hash)
if err != nil {
return nil, fmt.Errorf("cannot open the hash %q: %v", hash.String(), err)
}
rd, err := blob.Reader()
if err != nil {
return nil, fmt.Errorf("cannot open the blob: %v", err)
}
defer rd.Close()
bs, err := io.ReadAll(rd)
if err != nil {
return nil, fmt.Errorf("cannot read the blob: %v", err)
}
return bs, nil
}
func matchPattern(pattern ModifiedFilePattern, path string, status ModificationStatus, content1, content2 []byte) (*ModifiedFilePatternMatch, error) {
if matched, err := matchPath(pattern.FilePathPattern, path); err != nil {
return nil, err
} else if !matched {
return nil, nil
}
if pattern.FileContentPattern == nil {
switch status {
case ModificationStatusAdded:
return &ModifiedFilePatternMatch{Before: 0, After: 1}, nil
case ModificationStatusDeleted:
return &ModifiedFilePatternMatch{Before: 1, After: 0}, nil
case ModificationStatusModified:
return &ModifiedFilePatternMatch{Before: 1, After: 1}, nil
}
}
before := len(pattern.FileContentPattern.FindAll(content1, -1))
after := len(pattern.FileContentPattern.FindAll(content2, -1))
if before == 0 && after == 0 {
// No match.
return nil, nil
}
return &ModifiedFilePatternMatch{Before: before, After: after}, nil
}
func matchPath(doublestartPatterns []string, path string) (bool, error) {
for i := len(doublestartPatterns) - 1; i >= 0; i-- {
pat := doublestartPatterns[i]
resultIfMatched := true
if pat[0] == '!' {
resultIfMatched = false
pat = pat[1:]
}
if matched, err := doublestar.Match(pat, path); err != nil {
return false, err
} else if matched {
return resultIfMatched, nil
}
}
return false, nil
}