pipeline.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. package core
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "log"
  8. "os"
  9. "path/filepath"
  10. "sort"
  11. "strings"
  12. "time"
  13. "github.com/pkg/errors"
  14. "gopkg.in/src-d/go-git.v4"
  15. "gopkg.in/src-d/go-git.v4/plumbing"
  16. "gopkg.in/src-d/go-git.v4/plumbing/object"
  17. "gopkg.in/src-d/hercules.v4/internal/pb"
  18. "gopkg.in/src-d/hercules.v4/internal/toposort"
  19. "gopkg.in/src-d/go-git.v4/plumbing/storer"
  20. )
  21. // ConfigurationOptionType represents the possible types of a ConfigurationOption's value.
  22. type ConfigurationOptionType int
  23. const (
  24. // BoolConfigurationOption reflects the boolean value type.
  25. BoolConfigurationOption ConfigurationOptionType = iota
  26. // IntConfigurationOption reflects the integer value type.
  27. IntConfigurationOption
  28. // StringConfigurationOption reflects the string value type.
  29. StringConfigurationOption
  30. // FloatConfigurationOption reflects a floating point value type.
  31. FloatConfigurationOption
  32. // StringsConfigurationOption reflects the array of strings value type.
  33. StringsConfigurationOption
  34. )
  35. // String() returns an empty string for the boolean type, "int" for integers and "string" for
  36. // strings. It is used in the command line interface to show the argument's type.
  37. func (opt ConfigurationOptionType) String() string {
  38. switch opt {
  39. case BoolConfigurationOption:
  40. return ""
  41. case IntConfigurationOption:
  42. return "int"
  43. case StringConfigurationOption:
  44. return "string"
  45. case FloatConfigurationOption:
  46. return "float"
  47. case StringsConfigurationOption:
  48. return "string"
  49. }
  50. panic(fmt.Sprintf("Invalid ConfigurationOptionType value %d", opt))
  51. }
  52. // ConfigurationOption allows for the unified, retrospective way to setup PipelineItem-s.
  53. type ConfigurationOption struct {
  54. // Name identifies the configuration option in facts.
  55. Name string
  56. // Description represents the help text about the configuration option.
  57. Description string
  58. // Flag corresponds to the CLI token with "--" prepended.
  59. Flag string
  60. // Type specifies the kind of the configuration option's value.
  61. Type ConfigurationOptionType
  62. // Default is the initial value of the configuration option.
  63. Default interface{}
  64. }
  65. // FormatDefault converts the default value of ConfigurationOption to string.
  66. // Used in the command line interface to show the argument's default value.
  67. func (opt ConfigurationOption) FormatDefault() string {
  68. if opt.Type == StringsConfigurationOption {
  69. return fmt.Sprintf("\"%s\"", strings.Join(opt.Default.([]string), ","))
  70. }
  71. if opt.Type != StringConfigurationOption {
  72. return fmt.Sprint(opt.Default)
  73. }
  74. return fmt.Sprintf("\"%s\"", opt.Default)
  75. }
  76. // PipelineItem is the interface for all the units in the Git commits analysis pipeline.
  77. type PipelineItem interface {
  78. // Name returns the name of the analysis.
  79. Name() string
  80. // Provides returns the list of keys of reusable calculated entities.
  81. // Other items may depend on them.
  82. Provides() []string
  83. // Requires returns the list of keys of needed entities which must be supplied in Consume().
  84. Requires() []string
  85. // ListConfigurationOptions returns the list of available options which can be consumed by Configure().
  86. ListConfigurationOptions() []ConfigurationOption
  87. // Configure performs the initial setup of the object by applying parameters from facts.
  88. // It allows to create PipelineItems in a universal way.
  89. Configure(facts map[string]interface{})
  90. // Initialize prepares and resets the item. Consume() requires Initialize()
  91. // to be called at least once beforehand.
  92. Initialize(*git.Repository)
  93. // Consume processes the next commit.
  94. // deps contains the required entities which match Depends(). Besides, it always includes
  95. // DependencyCommit and DependencyIndex.
  96. // Returns the calculated entities which match Provides().
  97. Consume(deps map[string]interface{}) (map[string]interface{}, error)
  98. // Fork clones the item the requested number of times. The data links between the clones
  99. // are up to the implementation. Needed to handle Git branches. See also Merge().
  100. // Returns a slice with `n` fresh clones. In other words, it does not include the original item.
  101. Fork(n int) []PipelineItem
  102. // Merge combines several branches together. Each is supposed to have been created with Fork().
  103. // The result is stored in the called item, thus this function returns nothing.
  104. // Merge() must update all the branches, not only self. When several branches merge, some of
  105. // them may continue to live, hence this requirement.
  106. Merge(branches []PipelineItem)
  107. }
  108. // FeaturedPipelineItem enables switching the automatic insertion of pipeline items on or off.
  109. type FeaturedPipelineItem interface {
  110. PipelineItem
  111. // Features returns the list of names which enable this item to be automatically inserted
  112. // in Pipeline.DeployItem().
  113. Features() []string
  114. }
  115. // LeafPipelineItem corresponds to the top level pipeline items which produce the end results.
  116. type LeafPipelineItem interface {
  117. PipelineItem
  118. // Flag returns the cmdline name of the item.
  119. Flag() string
  120. // Finalize returns the result of the analysis.
  121. Finalize() interface{}
  122. // Serialize encodes the object returned by Finalize() to YAML or Protocol Buffers.
  123. Serialize(result interface{}, binary bool, writer io.Writer) error
  124. }
  125. // MergeablePipelineItem specifies the methods to combine several analysis results together.
  126. type MergeablePipelineItem interface {
  127. LeafPipelineItem
  128. // Deserialize loads the result from Protocol Buffers blob.
  129. Deserialize(pbmessage []byte) (interface{}, error)
  130. // MergeResults joins two results together. Common-s are specified as the global state.
  131. MergeResults(r1, r2 interface{}, c1, c2 *CommonAnalysisResult) interface{}
  132. }
  133. // CommonAnalysisResult holds the information which is always extracted at Pipeline.Run().
  134. type CommonAnalysisResult struct {
  135. // Time of the first commit in the analysed sequence.
  136. BeginTime int64
  137. // Time of the last commit in the analysed sequence.
  138. EndTime int64
  139. // The number of commits in the analysed sequence.
  140. CommitsNumber int
  141. // The duration of Pipeline.Run().
  142. RunTime time.Duration
  143. }
  144. // BeginTimeAsTime converts the UNIX timestamp of the beginning to Go time.
  145. func (car *CommonAnalysisResult) BeginTimeAsTime() time.Time {
  146. return time.Unix(car.BeginTime, 0)
  147. }
  148. // EndTimeAsTime converts the UNIX timestamp of the ending to Go time.
  149. func (car *CommonAnalysisResult) EndTimeAsTime() time.Time {
  150. return time.Unix(car.EndTime, 0)
  151. }
  152. // Merge combines the CommonAnalysisResult with an other one.
  153. // We choose the earlier BeginTime, the later EndTime, sum the number of commits and the
  154. // elapsed run times.
  155. func (car *CommonAnalysisResult) Merge(other *CommonAnalysisResult) {
  156. if car.EndTime == 0 || other.BeginTime == 0 {
  157. panic("Merging with an uninitialized CommonAnalysisResult")
  158. }
  159. if other.BeginTime < car.BeginTime {
  160. car.BeginTime = other.BeginTime
  161. }
  162. if other.EndTime > car.EndTime {
  163. car.EndTime = other.EndTime
  164. }
  165. car.CommitsNumber += other.CommitsNumber
  166. car.RunTime += other.RunTime
  167. }
  168. // FillMetadata copies the data to a Protobuf message.
  169. func (car *CommonAnalysisResult) FillMetadata(meta *pb.Metadata) *pb.Metadata {
  170. meta.BeginUnixTime = car.BeginTime
  171. meta.EndUnixTime = car.EndTime
  172. meta.Commits = int32(car.CommitsNumber)
  173. meta.RunTime = car.RunTime.Nanoseconds() / 1e6
  174. return meta
  175. }
  176. // Metadata is defined in internal/pb/pb.pb.go - header of the binary file.
  177. type Metadata = pb.Metadata
  178. // MetadataToCommonAnalysisResult copies the data from a Protobuf message.
  179. func MetadataToCommonAnalysisResult(meta *Metadata) *CommonAnalysisResult {
  180. return &CommonAnalysisResult{
  181. BeginTime: meta.BeginUnixTime,
  182. EndTime: meta.EndUnixTime,
  183. CommitsNumber: int(meta.Commits),
  184. RunTime: time.Duration(meta.RunTime * 1e6),
  185. }
  186. }
  187. // Pipeline is the core Hercules entity which carries several PipelineItems and executes them.
  188. // See the extended example of how a Pipeline works in doc.go
  189. type Pipeline struct {
  190. // OnProgress is the callback which is invoked in Analyse() to output it's
  191. // progress. The first argument is the number of complete steps and the
  192. // second is the total number of steps.
  193. OnProgress func(int, int)
  194. // Repository points to the analysed Git repository struct from go-git.
  195. repository *git.Repository
  196. // Items are the registered building blocks in the pipeline. The order defines the
  197. // execution sequence.
  198. items []PipelineItem
  199. // The collection of parameters to create items.
  200. facts map[string]interface{}
  201. // Feature flags which enable the corresponding items.
  202. features map[string]bool
  203. }
  204. const (
  205. // ConfigPipelineDumpPath is the name of the Pipeline configuration option (Pipeline.Initialize())
  206. // which enables saving the items DAG to the specified file.
  207. ConfigPipelineDumpPath = "Pipeline.DumpPath"
  208. // ConfigPipelineDryRun is the name of the Pipeline configuration option (Pipeline.Initialize())
  209. // which disables Configure() and Initialize() invocation on each PipelineItem during the
  210. // Pipeline initialization.
  211. // Subsequent Run() calls are going to fail. Useful with ConfigPipelineDumpPath=true.
  212. ConfigPipelineDryRun = "Pipeline.DryRun"
  213. // ConfigPipelineCommits is the name of the Pipeline configuration option (Pipeline.Initialize())
  214. // which allows to specify the custom commit sequence. By default, Pipeline.Commits() is used.
  215. ConfigPipelineCommits = "commits"
  216. // DependencyCommit is the name of one of the two items in `deps` supplied to PipelineItem.Consume()
  217. // which always exists. It corresponds to the currently analyzed commit.
  218. DependencyCommit = "commit"
  219. // DependencyIndex is the name of one of the two items in `deps` supplied to PipelineItem.Consume()
  220. // which always exists. It corresponds to the currently analyzed commit's index.
  221. DependencyIndex = "index"
  222. )
  223. // NewPipeline initializes a new instance of Pipeline struct.
  224. func NewPipeline(repository *git.Repository) *Pipeline {
  225. return &Pipeline{
  226. repository: repository,
  227. items: []PipelineItem{},
  228. facts: map[string]interface{}{},
  229. features: map[string]bool{},
  230. }
  231. }
  232. // GetFact returns the value of the fact with the specified name.
  233. func (pipeline *Pipeline) GetFact(name string) interface{} {
  234. return pipeline.facts[name]
  235. }
  236. // SetFact sets the value of the fact with the specified name.
  237. func (pipeline *Pipeline) SetFact(name string, value interface{}) {
  238. pipeline.facts[name] = value
  239. }
  240. // GetFeature returns the state of the feature with the specified name (enabled/disabled) and
  241. // whether it exists. See also: FeaturedPipelineItem.
  242. func (pipeline *Pipeline) GetFeature(name string) (bool, bool) {
  243. val, exists := pipeline.features[name]
  244. return val, exists
  245. }
  246. // SetFeature sets the value of the feature with the specified name.
  247. // See also: FeaturedPipelineItem.
  248. func (pipeline *Pipeline) SetFeature(name string) {
  249. pipeline.features[name] = true
  250. }
  251. // SetFeaturesFromFlags enables the features which were specified through the command line flags
  252. // which belong to the given PipelineItemRegistry instance.
  253. // See also: AddItem().
  254. func (pipeline *Pipeline) SetFeaturesFromFlags(registry ...*PipelineItemRegistry) {
  255. var ffr *PipelineItemRegistry
  256. if len(registry) == 0 {
  257. ffr = Registry
  258. } else if len(registry) == 1 {
  259. ffr = registry[0]
  260. } else {
  261. panic("Zero or one registry is allowed to be passed.")
  262. }
  263. for _, feature := range ffr.featureFlags.Flags {
  264. pipeline.SetFeature(feature)
  265. }
  266. }
  267. // DeployItem inserts a PipelineItem into the pipeline. It also recursively creates all of it's
  268. // dependencies (PipelineItem.Requires()). Returns the same item as specified in the arguments.
  269. func (pipeline *Pipeline) DeployItem(item PipelineItem) PipelineItem {
  270. fpi, ok := item.(FeaturedPipelineItem)
  271. if ok {
  272. for _, f := range fpi.Features() {
  273. pipeline.SetFeature(f)
  274. }
  275. }
  276. queue := []PipelineItem{}
  277. queue = append(queue, item)
  278. added := map[string]PipelineItem{}
  279. for _, item := range pipeline.items {
  280. added[item.Name()] = item
  281. }
  282. added[item.Name()] = item
  283. pipeline.AddItem(item)
  284. for len(queue) > 0 {
  285. head := queue[0]
  286. queue = queue[1:]
  287. for _, dep := range head.Requires() {
  288. for _, sibling := range Registry.Summon(dep) {
  289. if _, exists := added[sibling.Name()]; !exists {
  290. disabled := false
  291. // If this item supports features, check them against the activated in pipeline.features
  292. if fpi, matches := sibling.(FeaturedPipelineItem); matches {
  293. for _, feature := range fpi.Features() {
  294. if !pipeline.features[feature] {
  295. disabled = true
  296. break
  297. }
  298. }
  299. }
  300. if disabled {
  301. continue
  302. }
  303. added[sibling.Name()] = sibling
  304. queue = append(queue, sibling)
  305. pipeline.AddItem(sibling)
  306. }
  307. }
  308. }
  309. }
  310. return item
  311. }
  312. // AddItem inserts a PipelineItem into the pipeline. It does not check any dependencies.
  313. // See also: DeployItem().
  314. func (pipeline *Pipeline) AddItem(item PipelineItem) PipelineItem {
  315. pipeline.items = append(pipeline.items, item)
  316. return item
  317. }
  318. // RemoveItem deletes a PipelineItem from the pipeline. It leaves all the rest of the items intact.
  319. func (pipeline *Pipeline) RemoveItem(item PipelineItem) {
  320. for i, reg := range pipeline.items {
  321. if reg == item {
  322. pipeline.items = append(pipeline.items[:i], pipeline.items[i+1:]...)
  323. return
  324. }
  325. }
  326. }
  327. // Len returns the number of items in the pipeline.
  328. func (pipeline *Pipeline) Len() int {
  329. return len(pipeline.items)
  330. }
  331. // Commits returns the list of commits from the history similar to `git log` over the HEAD.
  332. func (pipeline *Pipeline) Commits() ([]*object.Commit, error) {
  333. cit, err := pipeline.repository.Log(&git.LogOptions{From: plumbing.ZeroHash})
  334. if err != nil {
  335. if err == plumbing.ErrReferenceNotFound {
  336. refs, errr := pipeline.repository.References()
  337. if errr != nil {
  338. return nil, errors.Wrap(errr, "unable to list the references")
  339. }
  340. var head *plumbing.Reference
  341. refs.ForEach(func(ref *plumbing.Reference) error {
  342. if strings.HasPrefix(ref.Name().String(), "refs/heads/HEAD/") {
  343. head = ref
  344. return storer.ErrStop
  345. }
  346. return nil
  347. })
  348. if head != nil {
  349. cit, err = pipeline.repository.Log(&git.LogOptions{From: head.Hash()})
  350. }
  351. }
  352. if err != nil {
  353. return nil, errors.Wrap(err, "unable to collect the commit history")
  354. }
  355. }
  356. defer cit.Close()
  357. var result []*object.Commit
  358. cit.ForEach(func(commit *object.Commit) error {
  359. result = append(result, commit)
  360. return nil
  361. })
  362. return result, nil
  363. }
  364. type sortablePipelineItems []PipelineItem
  365. func (items sortablePipelineItems) Len() int {
  366. return len(items)
  367. }
  368. func (items sortablePipelineItems) Less(i, j int) bool {
  369. return items[i].Name() < items[j].Name()
  370. }
  371. func (items sortablePipelineItems) Swap(i, j int) {
  372. items[i], items[j] = items[j], items[i]
  373. }
  374. func (pipeline *Pipeline) resolve(dumpPath string) {
  375. graph := toposort.NewGraph()
  376. sort.Sort(sortablePipelineItems(pipeline.items))
  377. name2item := map[string]PipelineItem{}
  378. ambiguousMap := map[string][]string{}
  379. nameUsages := map[string]int{}
  380. for _, item := range pipeline.items {
  381. nameUsages[item.Name()]++
  382. }
  383. counters := map[string]int{}
  384. for _, item := range pipeline.items {
  385. name := item.Name()
  386. if nameUsages[name] > 1 {
  387. index := counters[item.Name()] + 1
  388. counters[item.Name()] = index
  389. name = fmt.Sprintf("%s_%d", item.Name(), index)
  390. }
  391. graph.AddNode(name)
  392. name2item[name] = item
  393. for _, key := range item.Provides() {
  394. key = "[" + key + "]"
  395. graph.AddNode(key)
  396. if graph.AddEdge(name, key) > 1 {
  397. if ambiguousMap[key] != nil {
  398. fmt.Fprintln(os.Stderr, "Pipeline:")
  399. for _, item2 := range pipeline.items {
  400. if item2 == item {
  401. fmt.Fprint(os.Stderr, "> ")
  402. }
  403. fmt.Fprint(os.Stderr, item2.Name(), " [")
  404. for i, key2 := range item2.Provides() {
  405. fmt.Fprint(os.Stderr, key2)
  406. if i < len(item.Provides())-1 {
  407. fmt.Fprint(os.Stderr, ", ")
  408. }
  409. }
  410. fmt.Fprintln(os.Stderr, "]")
  411. }
  412. panic("Failed to resolve pipeline dependencies: ambiguous graph.")
  413. }
  414. ambiguousMap[key] = graph.FindParents(key)
  415. }
  416. }
  417. }
  418. counters = map[string]int{}
  419. for _, item := range pipeline.items {
  420. name := item.Name()
  421. if nameUsages[name] > 1 {
  422. index := counters[item.Name()] + 1
  423. counters[item.Name()] = index
  424. name = fmt.Sprintf("%s_%d", item.Name(), index)
  425. }
  426. for _, key := range item.Requires() {
  427. key = "[" + key + "]"
  428. if graph.AddEdge(key, name) == 0 {
  429. panic(fmt.Sprintf("Unsatisfied dependency: %s -> %s", key, item.Name()))
  430. }
  431. }
  432. }
  433. // Try to break the cycles in some known scenarios.
  434. if len(ambiguousMap) > 0 {
  435. ambiguous := []string{}
  436. for key := range ambiguousMap {
  437. ambiguous = append(ambiguous, key)
  438. }
  439. sort.Strings(ambiguous)
  440. bfsorder := graph.BreadthSort()
  441. bfsindex := map[string]int{}
  442. for i, s := range bfsorder {
  443. bfsindex[s] = i
  444. }
  445. for len(ambiguous) > 0 {
  446. key := ambiguous[0]
  447. ambiguous = ambiguous[1:]
  448. pair := ambiguousMap[key]
  449. inheritor := pair[1]
  450. if bfsindex[pair[1]] < bfsindex[pair[0]] {
  451. inheritor = pair[0]
  452. }
  453. removed := graph.RemoveEdge(key, inheritor)
  454. cycle := map[string]bool{}
  455. for _, node := range graph.FindCycle(key) {
  456. cycle[node] = true
  457. }
  458. if len(cycle) == 0 {
  459. cycle[inheritor] = true
  460. }
  461. if removed {
  462. graph.AddEdge(key, inheritor)
  463. }
  464. graph.RemoveEdge(inheritor, key)
  465. graph.ReindexNode(inheritor)
  466. // for all nodes key links to except those in cycle, put the link from inheritor
  467. for _, node := range graph.FindChildren(key) {
  468. if _, exists := cycle[node]; !exists {
  469. graph.AddEdge(inheritor, node)
  470. graph.RemoveEdge(key, node)
  471. }
  472. }
  473. graph.ReindexNode(key)
  474. }
  475. }
  476. var graphCopy *toposort.Graph
  477. if dumpPath != "" {
  478. graphCopy = graph.Copy()
  479. }
  480. strplan, ok := graph.Toposort()
  481. if !ok {
  482. panic("Failed to resolve pipeline dependencies: unable to topologically sort the items.")
  483. }
  484. pipeline.items = make([]PipelineItem, 0, len(pipeline.items))
  485. for _, key := range strplan {
  486. if item, ok := name2item[key]; ok {
  487. pipeline.items = append(pipeline.items, item)
  488. }
  489. }
  490. if dumpPath != "" {
  491. // If there is a floating difference, uncomment this:
  492. // fmt.Fprint(os.Stderr, graphCopy.DebugDump())
  493. ioutil.WriteFile(dumpPath, []byte(graphCopy.Serialize(strplan)), 0666)
  494. absPath, _ := filepath.Abs(dumpPath)
  495. log.Printf("Wrote the DAG to %s\n", absPath)
  496. }
  497. }
  498. // Initialize prepares the pipeline for the execution (Run()). This function
  499. // resolves the execution DAG, Configure()-s and Initialize()-s the items in it in the
  500. // topological dependency order. `facts` are passed inside Configure(). They are mutable.
  501. func (pipeline *Pipeline) Initialize(facts map[string]interface{}) {
  502. if facts == nil {
  503. facts = map[string]interface{}{}
  504. }
  505. if _, exists := facts[ConfigPipelineCommits]; !exists {
  506. var err error
  507. facts[ConfigPipelineCommits], err = pipeline.Commits()
  508. if err != nil {
  509. log.Panicf("failed to list the commits: %v", err)
  510. }
  511. }
  512. dumpPath, _ := facts[ConfigPipelineDumpPath].(string)
  513. pipeline.resolve(dumpPath)
  514. if dryRun, _ := facts[ConfigPipelineDryRun].(bool); dryRun {
  515. return
  516. }
  517. for _, item := range pipeline.items {
  518. item.Configure(facts)
  519. }
  520. for _, item := range pipeline.items {
  521. item.Initialize(pipeline.repository)
  522. }
  523. }
  524. // Run method executes the pipeline.
  525. //
  526. // `commits` is a slice with the git commits to analyse. Multiple branches are supported.
  527. //
  528. // Returns the mapping from each LeafPipelineItem to the corresponding analysis result.
  529. // There is always a "nil" record with CommonAnalysisResult.
  530. func (pipeline *Pipeline) Run(commits []*object.Commit) (map[LeafPipelineItem]interface{}, error) {
  531. startRunTime := time.Now()
  532. onProgress := pipeline.OnProgress
  533. if onProgress == nil {
  534. onProgress = func(int, int) {}
  535. }
  536. plan := prepareRunPlan(commits)
  537. progressSteps := len(plan) + 2
  538. branches := map[int][]PipelineItem{}
  539. // we will need rootClone if there is more than one root branch
  540. rootClone := cloneItems(pipeline.items, 1)[0]
  541. var newestTime int64
  542. commitIndex := 0
  543. for index, step := range plan {
  544. onProgress(index + 1, progressSteps)
  545. firstItem := step.Items[0]
  546. switch step.Action {
  547. case runActionCommit:
  548. state := map[string]interface{}{
  549. DependencyCommit: step.Commit,
  550. DependencyIndex: commitIndex,
  551. }
  552. for _, item := range branches[firstItem] {
  553. update, err := item.Consume(state)
  554. if err != nil {
  555. log.Printf("%s failed on commit #%d (%d) %s\n",
  556. item.Name(), commitIndex + 1, index + 1, step.Commit.Hash.String())
  557. return nil, err
  558. }
  559. for _, key := range item.Provides() {
  560. val, ok := update[key]
  561. if !ok {
  562. panic(fmt.Sprintf("%s: Consume() did not return %s", item.Name(), key))
  563. }
  564. state[key] = val
  565. }
  566. }
  567. commitTime := step.Commit.Committer.When.Unix()
  568. if commitTime > newestTime {
  569. newestTime = commitTime
  570. }
  571. commitIndex++
  572. case runActionFork:
  573. for i, clone := range cloneItems(branches[firstItem], len(step.Items)-1) {
  574. branches[step.Items[i+1]] = clone
  575. }
  576. case runActionMerge:
  577. merged := make([][]PipelineItem, len(step.Items))
  578. for i, b := range step.Items {
  579. merged[i] = branches[b]
  580. }
  581. mergeItems(merged)
  582. case runActionEmerge:
  583. if firstItem == 0 {
  584. branches[firstItem] = pipeline.items
  585. } else {
  586. branches[firstItem] = cloneItems(rootClone, 1)[0]
  587. }
  588. case runActionDelete:
  589. delete(branches, firstItem)
  590. }
  591. }
  592. onProgress(len(plan) + 1, progressSteps)
  593. result := map[LeafPipelineItem]interface{}{}
  594. for index, item := range getMasterBranch(branches) {
  595. if casted, ok := item.(LeafPipelineItem); ok {
  596. result[pipeline.items[index].(LeafPipelineItem)] = casted.Finalize()
  597. }
  598. }
  599. onProgress(progressSteps, progressSteps)
  600. result[nil] = &CommonAnalysisResult{
  601. BeginTime: plan[0].Commit.Committer.When.Unix(),
  602. EndTime: newestTime,
  603. CommitsNumber: len(commits),
  604. RunTime: time.Since(startRunTime),
  605. }
  606. return result, nil
  607. }
  608. // LoadCommitsFromFile reads the file by the specified FS path and generates the sequence of commits
  609. // by interpreting each line as a Git commit hash.
  610. func LoadCommitsFromFile(path string, repository *git.Repository) ([]*object.Commit, error) {
  611. var file io.ReadCloser
  612. if path != "-" {
  613. var err error
  614. file, err = os.Open(path)
  615. if err != nil {
  616. return nil, err
  617. }
  618. defer file.Close()
  619. } else {
  620. file = os.Stdin
  621. }
  622. scanner := bufio.NewScanner(file)
  623. var commits []*object.Commit
  624. for scanner.Scan() {
  625. hash := plumbing.NewHash(scanner.Text())
  626. if len(hash) != 20 {
  627. return nil, errors.New("invalid commit hash " + scanner.Text())
  628. }
  629. commit, err := repository.CommitObject(hash)
  630. if err != nil {
  631. return nil, err
  632. }
  633. commits = append(commits, commit)
  634. }
  635. return commits, nil
  636. }