-
Notifications
You must be signed in to change notification settings - Fork 146
/
file_history.go
174 lines (155 loc) · 5.62 KB
/
file_history.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
package leaves
import (
"fmt"
"io"
"sort"
"strings"
"github.com/gogo/protobuf/proto"
"gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
"gopkg.in/src-d/go-git.v4/plumbing/object"
"gopkg.in/src-d/go-git.v4/utils/merkletrie"
"gopkg.in/src-d/hercules.v7/internal/core"
"gopkg.in/src-d/hercules.v7/internal/pb"
items "gopkg.in/src-d/hercules.v7/internal/plumbing"
)
// FileHistory contains the intermediate state which is mutated by Consume(). It should implement
// LeafPipelineItem.
type FileHistory struct {
core.NoopMerger
core.OneShotMergeProcessor
files map[string][]plumbing.Hash
}
// FileHistoryResult is returned by Finalize() and represents the analysis result.
type FileHistoryResult struct {
Files map[string][]plumbing.Hash
}
// Name of this PipelineItem. Uniquely identifies the type, used for mapping keys, etc.
func (history *FileHistory) Name() string {
return "FileHistory"
}
// Provides returns the list of names of entities which are produced by this PipelineItem.
// Each produced entity will be inserted into `deps` of dependent Consume()-s according
// to this list. Also used by core.Registry to build the global map of providers.
func (history *FileHistory) Provides() []string {
return []string{}
}
// Requires returns the list of names of entities which are needed by this PipelineItem.
// Each requested entity will be inserted into `deps` of Consume(). In turn, those
// entities are Provides() upstream.
func (history *FileHistory) Requires() []string {
arr := [...]string{items.DependencyTreeChanges}
return arr[:]
}
// ListConfigurationOptions returns the list of changeable public properties of this PipelineItem.
func (history *FileHistory) ListConfigurationOptions() []core.ConfigurationOption {
return []core.ConfigurationOption{}
}
// Flag for the command line switch which enables this analysis.
func (history *FileHistory) Flag() string {
return "file-history"
}
// Description returns the text which explains what the analysis is doing.
func (history *FileHistory) Description() string {
return "Each file path is mapped to the list of commits which involve that file."
}
// Configure sets the properties previously published by ListConfigurationOptions().
func (history *FileHistory) Configure(facts map[string]interface{}) error {
return nil
}
// Initialize resets the temporary caches and prepares this PipelineItem for a series of Consume()
// calls. The repository which is going to be analysed is supplied as an argument.
func (history *FileHistory) Initialize(repository *git.Repository) error {
history.files = map[string][]plumbing.Hash{}
history.OneShotMergeProcessor.Initialize()
return nil
}
// Consume runs this PipelineItem on the next commit data.
// `deps` contain all the results from upstream PipelineItem-s as requested by Requires().
// Additionally, DependencyCommit is always present there and represents the analysed *object.Commit.
// This function returns the mapping with analysis results. The keys must be the same as
// in Provides(). If there was an error, nil is returned.
func (history *FileHistory) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
if !history.ShouldConsumeCommit(deps) {
return nil, nil
}
commit := deps[core.DependencyCommit].(*object.Commit).Hash
changes := deps[items.DependencyTreeChanges].(object.Changes)
for _, change := range changes {
action, _ := change.Action()
switch action {
case merkletrie.Insert:
hashes := make([]plumbing.Hash, 1)
hashes[0] = commit
history.files[change.To.Name] = hashes
case merkletrie.Delete:
delete(history.files, change.From.Name)
case merkletrie.Modify:
hashes := history.files[change.From.Name]
if change.From.Name != change.To.Name {
delete(history.files, change.From.Name)
}
hashes = append(hashes, commit)
history.files[change.To.Name] = hashes
}
}
return nil, nil
}
// Finalize returns the result of the analysis. Further Consume() calls are not expected.
func (history *FileHistory) Finalize() interface{} {
return FileHistoryResult{Files: history.files}
}
// Fork clones this PipelineItem.
func (history *FileHistory) Fork(n int) []core.PipelineItem {
return core.ForkSamePipelineItem(history, n)
}
// Serialize converts the analysis result as returned by Finalize() to text or bytes.
// The text format is YAML and the bytes format is Protocol Buffers.
func (history *FileHistory) Serialize(result interface{}, binary bool, writer io.Writer) error {
historyResult := result.(FileHistoryResult)
if binary {
return history.serializeBinary(&historyResult, writer)
}
history.serializeText(&historyResult, writer)
return nil
}
func (history *FileHistory) serializeText(result *FileHistoryResult, writer io.Writer) {
keys := make([]string, len(result.Files))
i := 0
for key := range result.Files {
keys[i] = key
i++
}
sort.Strings(keys)
for _, key := range keys {
hashes := result.Files[key]
strhashes := make([]string, len(hashes))
for i, hash := range hashes {
strhashes[i] = "\"" + hash.String() + "\""
}
fmt.Fprintf(writer, " - %s: [%s]\n", key, strings.Join(strhashes, ","))
}
}
func (history *FileHistory) serializeBinary(result *FileHistoryResult, writer io.Writer) error {
message := pb.FileHistoryResultMessage{
Files: map[string]*pb.FileHistory{},
}
for key, vals := range result.Files {
hashes := &pb.FileHistory{
Commits: make([]string, len(vals)),
}
for i, hash := range vals {
hashes.Commits[i] = hash.String()
}
message.Files[key] = hashes
}
serialized, err := proto.Marshal(&message)
if err != nil {
return err
}
_, err = writer.Write(serialized)
return err
}
func init() {
core.Registry.Register(&FileHistory{})
}