pipeline.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. package hercules
  2. import (
  3. "bufio"
  4. "errors"
  5. "flag"
  6. "fmt"
  7. "io"
  8. "io/ioutil"
  9. "os"
  10. "path/filepath"
  11. "reflect"
  12. "sort"
  13. "strings"
  14. "time"
  15. "unsafe"
  16. "gopkg.in/src-d/go-git.v4"
  17. "gopkg.in/src-d/go-git.v4/plumbing"
  18. "gopkg.in/src-d/go-git.v4/plumbing/object"
  19. "gopkg.in/src-d/hercules.v3/toposort"
  20. )
  21. type ConfigurationOptionType int
  22. const (
  23. // Boolean value type.
  24. BoolConfigurationOption ConfigurationOptionType = iota
  25. // Integer value type.
  26. IntConfigurationOption
  27. // String value type.
  28. StringConfigurationOption
  29. )
  30. const (
  31. ConfigPipelineDumpPath = "Pipeline.DumpPath"
  32. ConfigPipelineDryRun = "Pipeline.DryRun"
  33. )
  34. // ConfigurationOption allows for the unified, retrospective way to setup PipelineItem-s.
  35. type ConfigurationOption struct {
  36. // Name identifies the configuration option in facts.
  37. Name string
  38. // Description represents the help text about the configuration option.
  39. Description string
  40. // Flag corresponds to the CLI token with "-" prepended.
  41. Flag string
  42. // Type specifies the kind of the configuration option's value.
  43. Type ConfigurationOptionType
  44. // Default is the initial value of the configuration option.
  45. Default interface{}
  46. }
  47. // PipelineItem is the interface for all the units of the Git commit analysis pipeline.
  48. type PipelineItem interface {
  49. // Name returns the name of the analysis.
  50. Name() string
  51. // Provides returns the list of keys of reusable calculated entities.
  52. // Other items may depend on them.
  53. Provides() []string
  54. // Requires returns the list of keys of needed entities which must be supplied in Consume().
  55. Requires() []string
  56. // ListConfigurationOptions returns the list of available options which can be consumed by Configure().
  57. ListConfigurationOptions() []ConfigurationOption
  58. // Configure performs the initial setup of the object by applying parameters from facts.
  59. // It allows to create PipelineItems in a universal way.
  60. Configure(facts map[string]interface{})
  61. // Initialize prepares and resets the item. Consume() requires Initialize()
  62. // to be called at least once beforehand.
  63. Initialize(*git.Repository)
  64. // Consume processes the next commit.
  65. // deps contains the required entities which match Depends(). Besides, it always includes
  66. // "commit" and "index".
  67. // Returns the calculated entities which match Provides().
  68. Consume(deps map[string]interface{}) (map[string]interface{}, error)
  69. }
  70. // FeaturedPipelineItem enables switching the automatic insertion of pipeline items on or off.
  71. type FeaturedPipelineItem interface {
  72. PipelineItem
  73. // Features returns the list of names which enable this item to be automatically inserted
  74. // in Pipeline.DeployItem().
  75. Features() []string
  76. }
  77. // LeafPipelineItem corresponds to the top level pipeline items which produce the end results.
  78. type LeafPipelineItem interface {
  79. PipelineItem
  80. // Flag returns the cmdline name of the item.
  81. Flag() string
  82. // Finalize returns the result of the analysis.
  83. Finalize() interface{}
  84. // Serialize encodes the object returned by Finalize() to YAML or Protocol Buffers.
  85. Serialize(result interface{}, binary bool, writer io.Writer) error
  86. }
  87. // MergeablePipelineItem specifies the methods to combine several analysis results together.
  88. type MergeablePipelineItem interface {
  89. LeafPipelineItem
  90. // Deserialize loads the result from Protocol Buffers blob.
  91. Deserialize(pbmessage []byte) (interface{}, error)
  92. // MergeResults joins two results together.
  93. MergeResults(r1, r2 interface{}, c1, c2 CommonAnalysisResult) interface{}
  94. }
  95. // CommonAnalysisResult holds the information which is always extracted at Pipeline.Run().
  96. type CommonAnalysisResult struct {
  97. // Time of the first commit in the analysed sequence.
  98. BeginTime int64
  99. // Time of the last commit in the analysed sequence.
  100. EndTime int64
  101. // The number of commits in the analysed sequence.
  102. CommitsNumber int
  103. // The duration of Pipeline.Run().
  104. RunTime time.Duration
  105. }
  106. // PipelineItemRegistry contains all the known PipelineItem-s.
  107. type PipelineItemRegistry struct {
  108. provided map[string][]reflect.Type
  109. registered map[string]reflect.Type
  110. flags map[string]reflect.Type
  111. }
  112. // Register adds another PipelineItem to the registry.
  113. func (registry *PipelineItemRegistry) Register(example PipelineItem) {
  114. t := reflect.TypeOf(example)
  115. registry.registered[example.Name()] = t
  116. if fpi, ok := example.(LeafPipelineItem); ok {
  117. registry.flags[fpi.Flag()] = t
  118. }
  119. for _, dep := range example.Provides() {
  120. ts := registry.provided[dep]
  121. if ts == nil {
  122. ts = []reflect.Type{}
  123. }
  124. ts = append(ts, t)
  125. registry.provided[dep] = ts
  126. }
  127. }
  128. func (registry *PipelineItemRegistry) Summon(providesOrName string) []PipelineItem {
  129. if registry.provided == nil {
  130. return []PipelineItem{}
  131. }
  132. ts := registry.provided[providesOrName]
  133. items := []PipelineItem{}
  134. for _, t := range ts {
  135. items = append(items, reflect.New(t.Elem()).Interface().(PipelineItem))
  136. }
  137. if t, exists := registry.registered[providesOrName]; exists {
  138. items = append(items, reflect.New(t.Elem()).Interface().(PipelineItem))
  139. }
  140. return items
  141. }
  142. type arrayFeatureFlags struct {
  143. // Flags containts the features activated through the command line.
  144. Flags []string
  145. // Choices contains all registered features.
  146. Choices map[string]bool
  147. }
  148. func (acf *arrayFeatureFlags) String() string {
  149. return strings.Join([]string(acf.Flags), ", ")
  150. }
  151. func (acf *arrayFeatureFlags) Set(value string) error {
  152. if _, exists := acf.Choices[value]; !exists {
  153. return errors.New(fmt.Sprintf("Feature \"%s\" is not registered.", value))
  154. }
  155. acf.Flags = append(acf.Flags, value)
  156. return nil
  157. }
  158. var featureFlags = arrayFeatureFlags{Flags: []string{}, Choices: map[string]bool{}}
  159. // AddFlags inserts the cmdline options from PipelineItem.ListConfigurationOptions(),
  160. // FeaturedPipelineItem().Features() and LeafPipelineItem.Flag() into the global "flag" parser
  161. // built into the Go runtime.
  162. // Returns the "facts" which can be fed into PipelineItem.Configure() and the dictionary of
  163. // runnable analysis (LeafPipelineItem) choices. E.g. if "BurndownAnalysis" was activated
  164. // through "-burndown" cmdline argument, this mapping would contain ["BurndownAnalysis"] = *true.
  165. func (registry *PipelineItemRegistry) AddFlags() (map[string]interface{}, map[string]*bool) {
  166. flags := map[string]interface{}{}
  167. deployed := map[string]*bool{}
  168. for name, it := range registry.registered {
  169. formatHelp := func(desc string) string {
  170. return fmt.Sprintf("%s [%s]", desc, name)
  171. }
  172. itemIface := reflect.New(it.Elem()).Interface()
  173. for _, opt := range itemIface.(PipelineItem).ListConfigurationOptions() {
  174. var iface interface{}
  175. switch opt.Type {
  176. case BoolConfigurationOption:
  177. iface = interface{}(true)
  178. ptr := (**bool)(unsafe.Pointer(uintptr(unsafe.Pointer(&iface)) + unsafe.Sizeof(&iface)))
  179. *ptr = flag.Bool(opt.Flag, opt.Default.(bool), formatHelp(opt.Description))
  180. case IntConfigurationOption:
  181. iface = interface{}(0)
  182. ptr := (**int)(unsafe.Pointer(uintptr(unsafe.Pointer(&iface)) + unsafe.Sizeof(&iface)))
  183. *ptr = flag.Int(opt.Flag, opt.Default.(int), formatHelp(opt.Description))
  184. case StringConfigurationOption:
  185. iface = interface{}("")
  186. ptr := (**string)(unsafe.Pointer(uintptr(unsafe.Pointer(&iface)) + unsafe.Sizeof(&iface)))
  187. *ptr = flag.String(opt.Flag, opt.Default.(string), formatHelp(opt.Description))
  188. }
  189. flags[opt.Name] = iface
  190. }
  191. if fpi, ok := itemIface.(FeaturedPipelineItem); ok {
  192. for _, f := range fpi.Features() {
  193. featureFlags.Choices[f] = true
  194. }
  195. }
  196. if fpi, ok := itemIface.(LeafPipelineItem); ok {
  197. deployed[fpi.Name()] = flag.Bool(
  198. fpi.Flag(), false, fmt.Sprintf("Runs %s analysis.", fpi.Name()))
  199. }
  200. }
  201. {
  202. // Pipeline flags
  203. iface := interface{}("")
  204. ptr1 := (**string)(unsafe.Pointer(uintptr(unsafe.Pointer(&iface)) + unsafe.Sizeof(&iface)))
  205. *ptr1 = flag.String("dump-dag", "", "Write the pipeline DAG to a Graphviz file.")
  206. flags[ConfigPipelineDumpPath] = iface
  207. iface = interface{}(true)
  208. ptr2 := (**bool)(unsafe.Pointer(uintptr(unsafe.Pointer(&iface)) + unsafe.Sizeof(&iface)))
  209. *ptr2 = flag.Bool("dry-run", false, "Do not run any analyses - only resolve the DAG. "+
  210. "Useful for -dump-dag.")
  211. flags[ConfigPipelineDryRun] = iface
  212. }
  213. features := []string{}
  214. for f := range featureFlags.Choices {
  215. features = append(features, f)
  216. }
  217. flag.Var(&featureFlags, "feature",
  218. fmt.Sprintf("Enables specific analysis features, can be specified "+
  219. "multiple times. Available features: [%s].", strings.Join(features, ", ")))
  220. return flags, deployed
  221. }
  222. // Registry contains all known pipeline item types.
  223. var Registry = &PipelineItemRegistry{
  224. provided: map[string][]reflect.Type{},
  225. registered: map[string]reflect.Type{},
  226. flags: map[string]reflect.Type{},
  227. }
  228. type Pipeline struct {
  229. // OnProgress is the callback which is invoked in Analyse() to output it's
  230. // progress. The first argument is the number of processed commits and the
  231. // second is the total number of commits.
  232. OnProgress func(int, int)
  233. // Repository points to the analysed Git repository struct from go-git.
  234. repository *git.Repository
  235. // Items are the registered building blocks in the pipeline. The order defines the
  236. // execution sequence.
  237. items []PipelineItem
  238. // The collection of parameters to create items.
  239. facts map[string]interface{}
  240. // Feature flags which enable the corresponding items.
  241. features map[string]bool
  242. }
  243. const FactPipelineCommits = "commits"
  244. func NewPipeline(repository *git.Repository) *Pipeline {
  245. return &Pipeline{
  246. repository: repository,
  247. items: []PipelineItem{},
  248. facts: map[string]interface{}{},
  249. features: map[string]bool{},
  250. }
  251. }
  252. func (pipeline *Pipeline) GetFact(name string) interface{} {
  253. return pipeline.facts[name]
  254. }
  255. func (pipeline *Pipeline) SetFact(name string, value interface{}) {
  256. pipeline.facts[name] = value
  257. }
  258. func (pipeline *Pipeline) GetFeature(name string) (bool, bool) {
  259. val, exists := pipeline.features[name]
  260. return val, exists
  261. }
  262. func (pipeline *Pipeline) SetFeature(name string) {
  263. pipeline.features[name] = true
  264. }
  265. func (pipeline *Pipeline) SetFeaturesFromFlags() {
  266. for _, feature := range featureFlags.Flags {
  267. pipeline.SetFeature(feature)
  268. }
  269. }
  270. func (pipeline *Pipeline) DeployItem(item PipelineItem) PipelineItem {
  271. queue := []PipelineItem{}
  272. queue = append(queue, item)
  273. added := map[string]PipelineItem{}
  274. for _, item := range pipeline.items {
  275. added[item.Name()] = item
  276. }
  277. added[item.Name()] = item
  278. pipeline.AddItem(item)
  279. for len(queue) > 0 {
  280. head := queue[0]
  281. queue = queue[1:]
  282. for _, dep := range head.Requires() {
  283. for _, sibling := range Registry.Summon(dep) {
  284. if _, exists := added[sibling.Name()]; !exists {
  285. disabled := false
  286. // If this item supports features, check them against the activated in pipeline.features
  287. if fpi, matches := sibling.(FeaturedPipelineItem); matches {
  288. for _, feature := range fpi.Features() {
  289. if !pipeline.features[feature] {
  290. disabled = true
  291. break
  292. }
  293. }
  294. }
  295. if disabled {
  296. continue
  297. }
  298. added[sibling.Name()] = sibling
  299. queue = append(queue, sibling)
  300. pipeline.AddItem(sibling)
  301. }
  302. }
  303. }
  304. }
  305. return item
  306. }
  307. func (pipeline *Pipeline) AddItem(item PipelineItem) PipelineItem {
  308. pipeline.items = append(pipeline.items, item)
  309. return item
  310. }
  311. func (pipeline *Pipeline) RemoveItem(item PipelineItem) {
  312. for i, reg := range pipeline.items {
  313. if reg == item {
  314. pipeline.items = append(pipeline.items[:i], pipeline.items[i+1:]...)
  315. return
  316. }
  317. }
  318. }
  319. func (pipeline *Pipeline) Len() int {
  320. return len(pipeline.items)
  321. }
  322. // Commits returns the critical path in the repository's history. It starts
  323. // from HEAD and traces commits backwards till the root. When it encounters
  324. // a merge (more than one parent), it always chooses the first parent.
  325. func (pipeline *Pipeline) Commits() []*object.Commit {
  326. result := []*object.Commit{}
  327. repository := pipeline.repository
  328. head, err := repository.Head()
  329. if err != nil {
  330. panic(err)
  331. }
  332. commit, err := repository.CommitObject(head.Hash())
  333. if err != nil {
  334. panic(err)
  335. }
  336. // the first parent matches the head
  337. for ; err != io.EOF; commit, err = commit.Parents().Next() {
  338. if err != nil {
  339. panic(err)
  340. }
  341. result = append(result, commit)
  342. }
  343. // reverse the order
  344. for i, j := 0, len(result)-1; i < j; i, j = i+1, j-1 {
  345. result[i], result[j] = result[j], result[i]
  346. }
  347. return result
  348. }
  349. type sortablePipelineItems []PipelineItem
  350. func (items sortablePipelineItems) Len() int {
  351. return len(items)
  352. }
  353. func (items sortablePipelineItems) Less(i, j int) bool {
  354. return items[i].Name() < items[j].Name()
  355. }
  356. func (items sortablePipelineItems) Swap(i, j int) {
  357. items[i], items[j] = items[j], items[i]
  358. }
  359. func (pipeline *Pipeline) resolve(dumpPath string) {
  360. graph := toposort.NewGraph()
  361. sort.Sort(sortablePipelineItems(pipeline.items))
  362. name2item := map[string]PipelineItem{}
  363. ambiguousMap := map[string][]string{}
  364. nameUsages := map[string]int{}
  365. for _, item := range pipeline.items {
  366. nameUsages[item.Name()]++
  367. }
  368. counters := map[string]int{}
  369. for _, item := range pipeline.items {
  370. name := item.Name()
  371. if nameUsages[name] > 1 {
  372. index := counters[item.Name()] + 1
  373. counters[item.Name()] = index
  374. name = fmt.Sprintf("%s_%d", item.Name(), index)
  375. }
  376. graph.AddNode(name)
  377. name2item[name] = item
  378. for _, key := range item.Provides() {
  379. key = "[" + key + "]"
  380. graph.AddNode(key)
  381. if graph.AddEdge(name, key) > 1 {
  382. if ambiguousMap[key] != nil {
  383. fmt.Fprintln(os.Stderr, "Pipeline:")
  384. for _, item2 := range pipeline.items {
  385. if item2 == item {
  386. fmt.Fprint(os.Stderr, "> ")
  387. }
  388. fmt.Fprint(os.Stderr, item2.Name(), " [")
  389. for i, key2 := range item2.Provides() {
  390. fmt.Fprint(os.Stderr, key2)
  391. if i < len(item.Provides())-1 {
  392. fmt.Fprint(os.Stderr, ", ")
  393. }
  394. }
  395. fmt.Fprintln(os.Stderr, "]")
  396. }
  397. panic("Failed to resolve pipeline dependencies: ambiguous graph.")
  398. }
  399. ambiguousMap[key] = graph.FindParents(key)
  400. }
  401. }
  402. }
  403. counters = map[string]int{}
  404. for _, item := range pipeline.items {
  405. name := item.Name()
  406. if nameUsages[name] > 1 {
  407. index := counters[item.Name()] + 1
  408. counters[item.Name()] = index
  409. name = fmt.Sprintf("%s_%d", item.Name(), index)
  410. }
  411. for _, key := range item.Requires() {
  412. key = "[" + key + "]"
  413. if graph.AddEdge(key, name) == 0 {
  414. panic(fmt.Sprintf("Unsatisfied dependency: %s -> %s", key, item.Name()))
  415. }
  416. }
  417. }
  418. if len(ambiguousMap) > 0 {
  419. ambiguous := []string{}
  420. for key := range ambiguousMap {
  421. ambiguous = append(ambiguous, key)
  422. }
  423. sort.Strings(ambiguous)
  424. bfsorder := graph.BreadthSort()
  425. bfsindex := map[string]int{}
  426. for i, s := range bfsorder {
  427. bfsindex[s] = i
  428. }
  429. for len(ambiguous) > 0 {
  430. key := ambiguous[0]
  431. ambiguous = ambiguous[1:]
  432. pair := ambiguousMap[key]
  433. inheritor := pair[1]
  434. if bfsindex[pair[1]] < bfsindex[pair[0]] {
  435. inheritor = pair[0]
  436. }
  437. removed := graph.RemoveEdge(key, inheritor)
  438. cycle := map[string]bool{}
  439. for _, node := range graph.FindCycle(key) {
  440. cycle[node] = true
  441. }
  442. if len(cycle) == 0 {
  443. cycle[inheritor] = true
  444. }
  445. if removed {
  446. graph.AddEdge(key, inheritor)
  447. }
  448. graph.RemoveEdge(inheritor, key)
  449. graph.ReindexNode(inheritor)
  450. // for all nodes key links to except those in cycle, put the link from inheritor
  451. for _, node := range graph.FindChildren(key) {
  452. if _, exists := cycle[node]; !exists {
  453. graph.AddEdge(inheritor, node)
  454. graph.RemoveEdge(key, node)
  455. }
  456. }
  457. graph.ReindexNode(key)
  458. }
  459. }
  460. var graphCopy *toposort.Graph
  461. if dumpPath != "" {
  462. graphCopy = graph.Copy()
  463. }
  464. strplan, ok := graph.Toposort()
  465. if !ok {
  466. panic("Failed to resolve pipeline dependencies: unable to topologically sort the items.")
  467. }
  468. pipeline.items = make([]PipelineItem, 0, len(pipeline.items))
  469. for _, key := range strplan {
  470. if item, ok := name2item[key]; ok {
  471. pipeline.items = append(pipeline.items, item)
  472. }
  473. }
  474. if dumpPath != "" {
  475. // If there is a floating difference, uncomment this:
  476. // fmt.Fprint(os.Stderr, graphCopy.DebugDump())
  477. ioutil.WriteFile(dumpPath, []byte(graphCopy.Serialize(strplan)), 0666)
  478. absPath, _ := filepath.Abs(dumpPath)
  479. fmt.Fprintf(os.Stderr, "Wrote the DAG to %s\n", absPath)
  480. }
  481. }
  482. func (pipeline *Pipeline) Initialize(facts map[string]interface{}) {
  483. if facts == nil {
  484. facts = map[string]interface{}{}
  485. }
  486. if _, exists := facts[FactPipelineCommits]; !exists {
  487. facts[FactPipelineCommits] = pipeline.Commits()
  488. }
  489. dumpPath, _ := facts[ConfigPipelineDumpPath].(string)
  490. pipeline.resolve(dumpPath)
  491. if dryRun, _ := facts[ConfigPipelineDryRun].(bool); dryRun {
  492. return
  493. }
  494. for _, item := range pipeline.items {
  495. item.Configure(facts)
  496. }
  497. for _, item := range pipeline.items {
  498. item.Initialize(pipeline.repository)
  499. }
  500. }
  501. // Run method executes the pipeline.
  502. //
  503. // commits is a slice with the sequential commit history. It shall start from
  504. // the root (ascending order).
  505. //
  506. // Returns the mapping from each LeafPipelineItem to the corresponding analysis result.
  507. // There is always a "nil" record with CommonAnalysisResult.
  508. func (pipeline *Pipeline) Run(commits []*object.Commit) (map[LeafPipelineItem]interface{}, error) {
  509. startRunTime := time.Now()
  510. onProgress := pipeline.OnProgress
  511. if onProgress == nil {
  512. onProgress = func(int, int) {}
  513. }
  514. for index, commit := range commits {
  515. onProgress(index, len(commits))
  516. state := map[string]interface{}{"commit": commit, "index": index}
  517. for _, item := range pipeline.items {
  518. update, err := item.Consume(state)
  519. if err != nil {
  520. fmt.Fprintf(os.Stderr, "%s failed on commit #%d %s\n",
  521. item.Name(), index, commit.Hash.String())
  522. return nil, err
  523. }
  524. for _, key := range item.Provides() {
  525. val, ok := update[key]
  526. if !ok {
  527. panic(fmt.Sprintf("%s: Consume() did not return %s", item.Name(), key))
  528. }
  529. state[key] = val
  530. }
  531. }
  532. }
  533. onProgress(len(commits), len(commits))
  534. result := map[LeafPipelineItem]interface{}{}
  535. for _, item := range pipeline.items {
  536. if casted, ok := item.(LeafPipelineItem); ok {
  537. result[casted] = casted.Finalize()
  538. }
  539. }
  540. result[nil] = CommonAnalysisResult{
  541. BeginTime: commits[0].Author.When.Unix(),
  542. EndTime: commits[len(commits)-1].Author.When.Unix(),
  543. CommitsNumber: len(commits),
  544. RunTime: time.Since(startRunTime),
  545. }
  546. return result, nil
  547. }
  548. func LoadCommitsFromFile(path string, repository *git.Repository) ([]*object.Commit, error) {
  549. var file io.ReadCloser
  550. if path != "-" {
  551. var err error
  552. file, err = os.Open(path)
  553. if err != nil {
  554. return nil, err
  555. }
  556. defer file.Close()
  557. } else {
  558. file = os.Stdin
  559. }
  560. scanner := bufio.NewScanner(file)
  561. commits := []*object.Commit{}
  562. for scanner.Scan() {
  563. hash := plumbing.NewHash(scanner.Text())
  564. if len(hash) != 20 {
  565. return nil, errors.New("invalid commit hash " + scanner.Text())
  566. }
  567. commit, err := repository.CommitObject(hash)
  568. if err != nil {
  569. return nil, err
  570. }
  571. commits = append(commits, commit)
  572. }
  573. return commits, nil
  574. }