瀏覽代碼

Fix plugins

Signed-off-by: Vadim Markovtsev <vadim@sourced.tech>
Vadim Markovtsev 6 年之前
父節點
當前提交
c2bb9ca862
共有 4 個文件被更改,包括 43 次插入1 次删除
  1. 13 0
      cmd/hercules/plugin.template
  2. 13 0
      contrib/_plugin_example/churn_analysis.go
  3. 16 0
      core.go
  4. 1 1
      internal/core/forks.go

+ 13 - 0
cmd/hercules/plugin.template

@@ -30,6 +30,10 @@ import (
 // {{.name}} contains the intermediate state which is mutated by Consume(). It should implement
 // {{.name}} contains the intermediate state which is mutated by Consume(). It should implement
 // hercules.LeafPipelineItem.
 // hercules.LeafPipelineItem.
 type {{.name}} struct {
 type {{.name}} struct {
+  // No special branch merge logic is required
+  hercules.NoopMerger
+  // Process each merge commit only once
+  hercules.OneShotMergeProcessor
 }
 }
 
 
 // {{.name}}Result is returned by Finalize() and represents the analysis result.
 // {{.name}}Result is returned by Finalize() and represents the analysis result.
@@ -76,13 +80,22 @@ func ({{.varname}} *{{.name}}) Configure(facts map[string]interface{}) {
 
 
 // Initialize resets the internal temporary data structures and prepares the object for Consume().
 // Initialize resets the internal temporary data structures and prepares the object for Consume().
 func ({{.varname}} *{{.name}}) Initialize(repository *git.Repository) {
 func ({{.varname}} *{{.name}}) Initialize(repository *git.Repository) {
+  {{.varname}}.OneShotMergeProcessor.Initialize()
 }
 }
 
 
 // Consume is called for every commit in the sequence.
 // Consume is called for every commit in the sequence.
 func ({{.varname}} *{{.name}}) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
 func ({{.varname}} *{{.name}}) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
+  if !{{.varname}}.ShouldConsumeCommit(deps) {
+    return nil, nil
+  }
   return nil, nil
   return nil, nil
 }
 }
 
 
+// Fork clones the same item several times on branches.
+func ({{.varname}} *{{.name}}) Fork(n int) []hercules.PipelineItem {
+  return hercules.ForkSamePipelineItem({{.varname}}, n)
+}
+
 // Finalize produces the result of the analysis. No more Consume() calls are expected afterwards.
 // Finalize produces the result of the analysis. No more Consume() calls are expected afterwards.
 func ({{.varname}} *{{.name}}) Finalize() interface{} {
 func ({{.varname}} *{{.name}}) Finalize() interface{} {
   result := {{.name}}Result{}
   result := {{.name}}Result{}

+ 13 - 0
contrib/_plugin_example/churn_analysis.go

@@ -20,6 +20,10 @@ import (
 // ChurnAnalysis contains the intermediate state which is mutated by Consume(). It should implement
 // ChurnAnalysis contains the intermediate state which is mutated by Consume(). It should implement
 // hercules.LeafPipelineItem.
 // hercules.LeafPipelineItem.
 type ChurnAnalysis struct {
 type ChurnAnalysis struct {
+	// No special merge logic is required
+	hercules.NoopMerger
+	// Process each merge only once
+	hercules.OneShotMergeProcessor
 	TrackPeople bool
 	TrackPeople bool
 
 
 	global []editInfo
 	global []editInfo
@@ -109,9 +113,13 @@ func (churn *ChurnAnalysis) Configure(facts map[string]interface{}) {
 func (churn *ChurnAnalysis) Initialize(repository *git.Repository) {
 func (churn *ChurnAnalysis) Initialize(repository *git.Repository) {
 	churn.global = []editInfo{}
 	churn.global = []editInfo{}
 	churn.people = map[int][]editInfo{}
 	churn.people = map[int][]editInfo{}
+	churn.OneShotMergeProcessor.Initialize()
 }
 }
 
 
 func (churn *ChurnAnalysis) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
 func (churn *ChurnAnalysis) Consume(deps map[string]interface{}) (map[string]interface{}, error) {
+	if !churn.ShouldConsumeCommit(deps) {
+		return nil, nil
+	}
 	fileDiffs := deps[hercules.DependencyFileDiff].(map[string]hercules.FileDiffData)
 	fileDiffs := deps[hercules.DependencyFileDiff].(map[string]hercules.FileDiffData)
 	treeDiffs := deps[hercules.DependencyTreeChanges].(object.Changes)
 	treeDiffs := deps[hercules.DependencyTreeChanges].(object.Changes)
 	cache := deps[hercules.DependencyBlobCache].(map[plumbing.Hash]*object.Blob)
 	cache := deps[hercules.DependencyBlobCache].(map[plumbing.Hash]*object.Blob)
@@ -167,6 +175,11 @@ func (churn *ChurnAnalysis) Consume(deps map[string]interface{}) (map[string]int
 	return nil, nil
 	return nil, nil
 }
 }
 
 
+// Fork clones the same item several times on branches.
+func (churn *ChurnAnalysis) Fork(n int) []hercules.PipelineItem {
+	return hercules.ForkSamePipelineItem(churn, n)
+}
+
 func (churn *ChurnAnalysis) Finalize() interface{} {
 func (churn *ChurnAnalysis) Finalize() interface{} {
 	result := ChurnAnalysisResult{
 	result := ChurnAnalysisResult{
 		Global: editInfosToEdits(churn.global),
 		Global: editInfosToEdits(churn.global),

+ 16 - 0
core.go

@@ -44,6 +44,12 @@ type MergeablePipelineItem = core.MergeablePipelineItem
 // CommonAnalysisResult holds the information which is always extracted at Pipeline.Run().
 // CommonAnalysisResult holds the information which is always extracted at Pipeline.Run().
 type CommonAnalysisResult = core.CommonAnalysisResult
 type CommonAnalysisResult = core.CommonAnalysisResult
 
 
+// NoopMerger provides an empty Merge() method suitable for PipelineItem.
+type NoopMerger = core.NoopMerger
+
+// OneShotMergeProcessor provides the convenience method to consume merges only once.
+type OneShotMergeProcessor = core.OneShotMergeProcessor
+
 // MetadataToCommonAnalysisResult copies the data from a Protobuf message.
 // MetadataToCommonAnalysisResult copies the data from a Protobuf message.
 func MetadataToCommonAnalysisResult(meta *core.Metadata) *CommonAnalysisResult {
 func MetadataToCommonAnalysisResult(meta *core.Metadata) *CommonAnalysisResult {
 	return core.MetadataToCommonAnalysisResult(meta)
 	return core.MetadataToCommonAnalysisResult(meta)
@@ -78,6 +84,16 @@ func LoadCommitsFromFile(path string, repository *git.Repository) ([]*object.Com
 	return core.LoadCommitsFromFile(path, repository)
 	return core.LoadCommitsFromFile(path, repository)
 }
 }
 
 
+// ForkSamePipelineItem clones items by referencing the same origin.
+func ForkSamePipelineItem(origin PipelineItem, n int) []PipelineItem {
+	return core.ForkSamePipelineItem(origin ,n)
+}
+
+// ForkCopyPipelineItem clones items by copying them by value from the origin.
+func ForkCopyPipelineItem(origin PipelineItem, n int) []PipelineItem {
+	return core.ForkCopyPipelineItem(origin ,n)
+}
+
 // PipelineItemRegistry contains all the known PipelineItem-s.
 // PipelineItemRegistry contains all the known PipelineItem-s.
 type PipelineItemRegistry = core.PipelineItemRegistry
 type PipelineItemRegistry = core.PipelineItemRegistry
 
 

+ 1 - 1
internal/core/forks.go

@@ -34,7 +34,7 @@ func (proc *OneShotMergeProcessor) ShouldConsumeCommit(deps map[string]interface
 	return false
 	return false
 }
 }
 
 
-// NoopMerger provides an empty Merge() method suitable for PipelineITem.
+// NoopMerger provides an empty Merge() method suitable for PipelineItem.
 type NoopMerger struct {
 type NoopMerger struct {
 }
 }