forks.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. package core
  2. import (
  3. "log"
  4. "reflect"
  5. "sort"
  6. "gopkg.in/src-d/go-git.v4/plumbing/object"
  7. "gopkg.in/src-d/go-git.v4/plumbing"
  8. "gopkg.in/src-d/hercules.v4/internal/toposort"
  9. )
  10. // OneShotMergeProcessor provides the convenience method to consume merges only once.
  11. type OneShotMergeProcessor struct {
  12. merges map[plumbing.Hash]bool
  13. }
  14. // Initialize resets OneShotMergeProcessor.
  15. func (proc *OneShotMergeProcessor) Initialize() {
  16. proc.merges = map[plumbing.Hash]bool{}
  17. }
  18. // ShouldConsumeCommit returns true on regular commits. It also returns true upon
  19. // the first occurrence of a particular merge commit.
  20. func (proc *OneShotMergeProcessor) ShouldConsumeCommit(deps map[string]interface{}) bool {
  21. commit := deps[DependencyCommit].(*object.Commit)
  22. if commit.NumParents() <= 1 {
  23. return true
  24. }
  25. if !proc.merges[commit.Hash] {
  26. proc.merges[commit.Hash] = true
  27. return true
  28. }
  29. return false
  30. }
  31. // IsMergeCommit indicates whether the commit is a merge or not.
  32. func IsMergeCommit(deps map[string]interface{}) bool {
  33. return deps[DependencyCommit].(*object.Commit).NumParents() > 1
  34. }
  35. // NoopMerger provides an empty Merge() method suitable for PipelineItem.
  36. type NoopMerger struct {
  37. }
  38. // Merge does nothing.
  39. func (merger *NoopMerger) Merge(branches []PipelineItem) {
  40. // no-op
  41. }
  42. // ForkSamePipelineItem clones items by referencing the same origin.
  43. func ForkSamePipelineItem(origin PipelineItem, n int) []PipelineItem {
  44. clones := make([]PipelineItem, n)
  45. for i := 0; i < n; i++ {
  46. clones[i] = origin
  47. }
  48. return clones
  49. }
  50. // ForkCopyPipelineItem clones items by copying them by value from the origin.
  51. func ForkCopyPipelineItem(origin PipelineItem, n int) []PipelineItem {
  52. originValue := reflect.Indirect(reflect.ValueOf(origin))
  53. originType := originValue.Type()
  54. clones := make([]PipelineItem, n)
  55. for i := 0; i < n; i++ {
  56. cloneValue := reflect.New(originType).Elem()
  57. cloneValue.Set(originValue)
  58. clones[i] = cloneValue.Addr().Interface().(PipelineItem)
  59. }
  60. return clones
  61. }
  62. const (
  63. // runActionCommit corresponds to a regular commit
  64. runActionCommit = 0
  65. // runActionFork splits a branch into several parts
  66. runActionFork = iota
  67. // runActionMerge merges several branches together
  68. runActionMerge = iota
  69. // runActionEmerge starts a root branch
  70. runActionEmerge = iota
  71. // runActionDelete removes the branch as it is no longer needed
  72. runActionDelete = iota
  73. )
  74. type runAction struct {
  75. Action int
  76. Commit *object.Commit
  77. Items []int
  78. }
  79. type orderer = func(reverse, direction bool) []string
  80. func cloneItems(origin []PipelineItem, n int) [][]PipelineItem {
  81. clones := make([][]PipelineItem, n)
  82. for j := 0; j < n; j++ {
  83. clones[j] = make([]PipelineItem, len(origin))
  84. }
  85. for i, item := range origin {
  86. itemClones := item.Fork(n)
  87. for j := 0; j < n; j++ {
  88. clones[j][i] = itemClones[j]
  89. }
  90. }
  91. return clones
  92. }
  93. func mergeItems(branches [][]PipelineItem) {
  94. buffer := make([]PipelineItem, len(branches) - 1)
  95. for i, item := range branches[0] {
  96. for j := 0; j < len(branches)-1; j++ {
  97. buffer[j] = branches[j+1][i]
  98. }
  99. item.Merge(buffer)
  100. }
  101. }
  102. // getMasterBranch returns the branch with the smallest index.
  103. func getMasterBranch(branches map[int][]PipelineItem) []PipelineItem {
  104. minKey := 1 << 31
  105. var minVal []PipelineItem
  106. for key, val := range branches {
  107. if key < minKey {
  108. minKey = key
  109. minVal = val
  110. }
  111. }
  112. return minVal
  113. }
  114. // prepareRunPlan schedules the actions for Pipeline.Run().
  115. func prepareRunPlan(commits []*object.Commit) []runAction {
  116. hashes, dag := buildDag(commits)
  117. leaveRootComponent(hashes, dag)
  118. mergedDag, mergedSeq := mergeDag(hashes, dag)
  119. orderNodes := bindOrderNodes(mergedDag)
  120. collapseFastForwards(orderNodes, hashes, mergedDag, dag, mergedSeq)
  121. /*fmt.Printf("digraph Hercules {\n")
  122. for i, c := range orderNodes(false, false) {
  123. commit := hashes[c]
  124. fmt.Printf(" \"%s\"[label=\"[%d] %s\"]\n", commit.Hash.String(), i, commit.Hash.String()[:6])
  125. for _, child := range mergedDag[commit.Hash] {
  126. fmt.Printf(" \"%s\" -> \"%s\"\n", commit.Hash.String(), child.Hash.String())
  127. }
  128. }
  129. fmt.Printf("}\n")*/
  130. plan := generatePlan(orderNodes, hashes, mergedDag, dag, mergedSeq)
  131. plan = optimizePlan(plan)
  132. /*for _, p := range plan {
  133. firstItem := p.Items[0]
  134. switch p.Action {
  135. case runActionCommit:
  136. fmt.Fprintln(os.Stderr, "C", firstItem, p.Commit.Hash.String())
  137. case runActionFork:
  138. fmt.Fprintln(os.Stderr, "F", p.Items)
  139. case runActionMerge:
  140. fmt.Fprintln(os.Stderr, "M", p.Items)
  141. }
  142. }*/
  143. return plan
  144. }
  145. // buildDag generates the raw commit DAG and the commit hash map.
  146. func buildDag(commits []*object.Commit) (
  147. map[string]*object.Commit, map[plumbing.Hash][]*object.Commit) {
  148. hashes := map[string]*object.Commit{}
  149. for _, commit := range commits {
  150. hashes[commit.Hash.String()] = commit
  151. }
  152. dag := map[plumbing.Hash][]*object.Commit{}
  153. for _, commit := range commits {
  154. if _, exists := dag[commit.Hash]; !exists {
  155. dag[commit.Hash] = make([]*object.Commit, 0, 1)
  156. }
  157. for _, parent := range commit.ParentHashes {
  158. if _, exists := hashes[parent.String()]; !exists {
  159. continue
  160. }
  161. children := dag[parent]
  162. if children == nil {
  163. children = make([]*object.Commit, 0, 1)
  164. }
  165. dag[parent] = append(children, commit)
  166. }
  167. }
  168. return hashes, dag
  169. }
  170. // leaveRootComponent runs connected components analysis and throws away everything
  171. // but the part which grows from the root.
  172. func leaveRootComponent(
  173. hashes map[string]*object.Commit,
  174. dag map[plumbing.Hash][]*object.Commit) {
  175. visited := map[plumbing.Hash]bool{}
  176. var sets [][]plumbing.Hash
  177. for key := range dag {
  178. if visited[key] {
  179. continue
  180. }
  181. var set []plumbing.Hash
  182. for queue := []plumbing.Hash{key}; len(queue) > 0; {
  183. head := queue[len(queue)-1]
  184. queue = queue[:len(queue)-1]
  185. if visited[head] {
  186. continue
  187. }
  188. set = append(set, head)
  189. visited[head] = true
  190. for _, c := range dag[head] {
  191. if !visited[c.Hash] {
  192. queue = append(queue, c.Hash)
  193. }
  194. }
  195. if commit, exists := hashes[head.String()]; exists {
  196. for _, p := range commit.ParentHashes {
  197. if !visited[p] {
  198. if _, exists := hashes[p.String()]; exists {
  199. queue = append(queue, p)
  200. }
  201. }
  202. }
  203. }
  204. }
  205. sets = append(sets, set)
  206. }
  207. if len(sets) > 1 {
  208. maxlen := 0
  209. maxind := -1
  210. for i, set := range sets {
  211. if len(set) > maxlen {
  212. maxlen = len(set)
  213. maxind = i
  214. }
  215. }
  216. for i, set := range sets {
  217. if i == maxind {
  218. continue
  219. }
  220. for _, h := range set {
  221. log.Printf("warning: dropped %s from the analysis - disjoint", h.String())
  222. delete(dag, h)
  223. delete(hashes, h.String())
  224. }
  225. }
  226. }
  227. }
  228. // bindOrderNodes returns curried "orderNodes" function.
  229. func bindOrderNodes(mergedDag map[plumbing.Hash][]*object.Commit) orderer {
  230. return func(reverse, direction bool) []string {
  231. graph := toposort.NewGraph()
  232. keys := make([]plumbing.Hash, 0, len(mergedDag))
  233. for key := range mergedDag {
  234. keys = append(keys, key)
  235. }
  236. sort.Slice(keys, func(i, j int) bool { return keys[i].String() < keys[j].String() })
  237. for _, key := range keys {
  238. graph.AddNode(key.String())
  239. }
  240. for _, key := range keys {
  241. children := mergedDag[key]
  242. sort.Slice(children, func(i, j int) bool {
  243. return children[i].Hash.String() < children[j].Hash.String()
  244. })
  245. for _, c := range children {
  246. if !direction {
  247. graph.AddEdge(key.String(), c.Hash.String())
  248. } else {
  249. graph.AddEdge(c.Hash.String(), key.String())
  250. }
  251. }
  252. }
  253. order, ok := graph.Toposort()
  254. if !ok {
  255. // should never happen
  256. panic("Could not topologically sort the DAG of commits")
  257. }
  258. if reverse != direction {
  259. // one day this must appear in the standard library...
  260. for i, j := 0, len(order)-1; i < len(order)/2; i, j = i+1, j-1 {
  261. order[i], order[j] = order[j], order[i]
  262. }
  263. }
  264. return order
  265. }
  266. }
  267. // inverts `dag`
  268. func buildParents(dag map[plumbing.Hash][]*object.Commit) map[plumbing.Hash]map[plumbing.Hash]bool {
  269. parents := map[plumbing.Hash]map[plumbing.Hash]bool{}
  270. for key, vals := range dag {
  271. for _, val := range vals {
  272. myps := parents[val.Hash]
  273. if myps == nil {
  274. myps = map[plumbing.Hash]bool{}
  275. parents[val.Hash] = myps
  276. }
  277. myps[key] = true
  278. }
  279. }
  280. return parents
  281. }
  282. // mergeDag turns sequences of consecutive commits into single nodes.
  283. func mergeDag(
  284. hashes map[string]*object.Commit,
  285. dag map[plumbing.Hash][]*object.Commit) (
  286. mergedDag, mergedSeq map[plumbing.Hash][]*object.Commit) {
  287. parents := buildParents(dag)
  288. mergedDag = map[plumbing.Hash][]*object.Commit{}
  289. mergedSeq = map[plumbing.Hash][]*object.Commit{}
  290. visited := map[plumbing.Hash]bool{}
  291. for head := range dag {
  292. if visited[head] {
  293. continue
  294. }
  295. c := head
  296. for true {
  297. nextParents := parents[c]
  298. var next plumbing.Hash
  299. for p := range nextParents {
  300. next = p
  301. break
  302. }
  303. if len(nextParents) != 1 || len(dag[next]) != 1 {
  304. break
  305. }
  306. c = next
  307. }
  308. head = c
  309. var seq []*object.Commit
  310. for true {
  311. visited[c] = true
  312. seq = append(seq, hashes[c.String()])
  313. if len(dag[c]) != 1 {
  314. break
  315. }
  316. c = dag[c][0].Hash
  317. if len(parents[c]) != 1 {
  318. break
  319. }
  320. }
  321. mergedSeq[head] = seq
  322. mergedDag[head] = dag[seq[len(seq)-1].Hash]
  323. }
  324. return
  325. }
  326. // collapseFastForwards removes the fast forward merges.
  327. func collapseFastForwards(
  328. orderNodes orderer, hashes map[string]*object.Commit,
  329. mergedDag, dag, mergedSeq map[plumbing.Hash][]*object.Commit) {
  330. parents := buildParents(mergedDag)
  331. processed := map[plumbing.Hash]bool{}
  332. for _, strkey := range orderNodes(false, true) {
  333. key := hashes[strkey].Hash
  334. processed[key] = true
  335. repeat:
  336. vals, exists := mergedDag[key]
  337. if !exists {
  338. continue
  339. }
  340. if len(vals) < 2 {
  341. continue
  342. }
  343. toRemove := map[plumbing.Hash]bool{}
  344. for _, child := range vals {
  345. var queue []plumbing.Hash
  346. visited := map[plumbing.Hash]bool{child.Hash: true}
  347. childParents := parents[child.Hash]
  348. childNumOtherParents := 0
  349. for parent := range childParents {
  350. if parent != key {
  351. visited[parent] = true
  352. childNumOtherParents++
  353. queue = append(queue, parent)
  354. }
  355. }
  356. var immediateParent plumbing.Hash
  357. if childNumOtherParents == 1 {
  358. immediateParent = queue[0]
  359. }
  360. for len(queue) > 0 {
  361. head := queue[len(queue)-1]
  362. queue = queue[:len(queue)-1]
  363. if processed[head] {
  364. if head == key {
  365. toRemove[child.Hash] = true
  366. if childNumOtherParents == 1 && len(mergedDag[immediateParent]) == 1 {
  367. mergedSeq[immediateParent] = append(
  368. mergedSeq[immediateParent], mergedSeq[child.Hash]...)
  369. delete(mergedSeq, child.Hash)
  370. mergedDag[immediateParent] = mergedDag[child.Hash]
  371. delete(mergedDag, child.Hash)
  372. parents[child.Hash] = parents[immediateParent]
  373. for _, vals := range parents {
  374. for v := range vals {
  375. if v == child.Hash {
  376. delete(vals, v)
  377. vals[immediateParent] = true
  378. break
  379. }
  380. }
  381. }
  382. }
  383. }
  384. break
  385. }
  386. for parent := range parents[head] {
  387. if !visited[parent] {
  388. visited[head] = true
  389. queue = append(queue, parent)
  390. }
  391. }
  392. }
  393. }
  394. if len(toRemove) == 0 {
  395. continue
  396. }
  397. var newVals []*object.Commit
  398. for _, child := range vals {
  399. if !toRemove[child.Hash] {
  400. newVals = append(newVals, child)
  401. }
  402. }
  403. merged := false
  404. if len(newVals) == 1 {
  405. onlyChild := newVals[0].Hash
  406. if len(parents[onlyChild]) == 1 {
  407. merged = true
  408. mergedSeq[key] = append(mergedSeq[key], mergedSeq[onlyChild]...)
  409. delete(mergedSeq, onlyChild)
  410. mergedDag[key] = mergedDag[onlyChild]
  411. delete(mergedDag, onlyChild)
  412. parents[onlyChild] = parents[key]
  413. for _, vals := range parents {
  414. for v := range vals {
  415. if v == onlyChild {
  416. delete(vals, v)
  417. vals[key] = true
  418. break
  419. }
  420. }
  421. }
  422. }
  423. }
  424. if !merged {
  425. mergedDag[key] = newVals
  426. }
  427. newVals = []*object.Commit{}
  428. node := mergedSeq[key][len(mergedSeq[key])-1].Hash
  429. for _, child := range dag[node] {
  430. if !toRemove[child.Hash] {
  431. newVals = append(newVals, child)
  432. }
  433. }
  434. dag[node] = newVals
  435. if merged {
  436. goto repeat
  437. }
  438. }
  439. }
  440. // generatePlan creates the list of actions from the commit DAG.
  441. func generatePlan(
  442. orderNodes orderer, hashes map[string]*object.Commit,
  443. mergedDag, dag, mergedSeq map[plumbing.Hash][]*object.Commit) []runAction {
  444. parents := buildParents(dag)
  445. var plan []runAction
  446. branches := map[plumbing.Hash]int{}
  447. branchers := map[plumbing.Hash]map[plumbing.Hash]int{}
  448. counter := 0
  449. for _, name := range orderNodes(false, true) {
  450. commit := hashes[name]
  451. if len(parents[commit.Hash]) == 0 {
  452. branches[commit.Hash] = counter
  453. plan = append(plan, runAction{
  454. Action: runActionEmerge,
  455. Commit: commit,
  456. Items: []int{counter},
  457. })
  458. counter++
  459. }
  460. var branch int
  461. {
  462. var exists bool
  463. branch, exists = branches[commit.Hash]
  464. if !exists {
  465. branch = -1
  466. }
  467. }
  468. branchExists := func() bool { return branch >= 0 }
  469. appendCommit := func(c *object.Commit, branch int) {
  470. plan = append(plan, runAction{
  471. Action: runActionCommit,
  472. Commit: c,
  473. Items: []int{branch},
  474. })
  475. }
  476. appendMergeIfNeeded := func() {
  477. if len(parents[commit.Hash]) < 2 {
  478. return
  479. }
  480. // merge after the merge commit (the first in the sequence)
  481. var items []int
  482. minBranch := 1 << 31
  483. for parent := range parents[commit.Hash] {
  484. parentBranch := -1
  485. if parents, exists := branchers[commit.Hash]; exists {
  486. if inheritedBranch, exists := parents[parent]; exists {
  487. parentBranch = inheritedBranch
  488. }
  489. }
  490. if parentBranch == -1 {
  491. parentBranch = branches[parent]
  492. if parentBranch == -1 {
  493. log.Panicf("parent %s > %s does not have a branch assigned",
  494. parent.String(), commit.Hash.String())
  495. }
  496. }
  497. if len(dag[parent]) == 1 && minBranch > parentBranch {
  498. minBranch = parentBranch
  499. }
  500. items = append(items, parentBranch)
  501. if parentBranch != branch {
  502. appendCommit(commit, parentBranch)
  503. }
  504. }
  505. // there should be no duplicates in items
  506. if minBranch < 1 << 31 {
  507. branch = minBranch
  508. branches[commit.Hash] = minBranch
  509. } else if !branchExists() {
  510. log.Panicf("!branchExists(%s)", commit.Hash.String())
  511. }
  512. plan = append(plan, runAction{
  513. Action: runActionMerge,
  514. Commit: nil,
  515. Items: items,
  516. })
  517. }
  518. var head plumbing.Hash
  519. if subseq, exists := mergedSeq[commit.Hash]; exists {
  520. for subseqIndex, offspring := range subseq {
  521. if branchExists() {
  522. appendCommit(offspring, branch)
  523. }
  524. if subseqIndex == 0 {
  525. appendMergeIfNeeded()
  526. }
  527. }
  528. head = subseq[len(subseq)-1].Hash
  529. branches[head] = branch
  530. } else {
  531. head = commit.Hash
  532. }
  533. if len(mergedDag[commit.Hash]) > 1 {
  534. children := []int{branch}
  535. for i, child := range mergedDag[commit.Hash] {
  536. if i == 0 {
  537. branches[child.Hash] = branch
  538. continue
  539. }
  540. if _, exists := branches[child.Hash]; !exists {
  541. branches[child.Hash] = counter
  542. }
  543. parents := branchers[child.Hash]
  544. if parents == nil {
  545. parents = map[plumbing.Hash]int{}
  546. branchers[child.Hash] = parents
  547. }
  548. parents[head] = counter
  549. children = append(children, counter)
  550. counter++
  551. }
  552. plan = append(plan, runAction{
  553. Action: runActionFork,
  554. Commit: nil,
  555. Items: children,
  556. })
  557. }
  558. }
  559. return plan
  560. }
  561. // optimizePlan removes "dead" nodes and inserts `runActionDelete` disposal steps.
  562. //
  563. // | *
  564. // * /
  565. // |\/
  566. // |/
  567. // *
  568. //
  569. func optimizePlan(plan []runAction) []runAction {
  570. // lives maps branch index to the number of commits in that branch
  571. lives := map[int]int{}
  572. // lastMentioned maps branch index to the index inside `plan` when that branch was last used
  573. lastMentioned := map[int]int{}
  574. for i, p := range plan {
  575. firstItem := p.Items[0]
  576. switch p.Action {
  577. case runActionCommit:
  578. lives[firstItem]++
  579. lastMentioned[firstItem] = i
  580. if firstItem == -1 {
  581. log.Panicf("commit %s does not have an assigned branch",
  582. p.Commit.Hash.String())
  583. }
  584. case runActionFork:
  585. lastMentioned[firstItem] = i
  586. case runActionMerge:
  587. for _, item := range p.Items {
  588. lastMentioned[item] = i
  589. }
  590. case runActionEmerge:
  591. lives[firstItem]++
  592. lastMentioned[firstItem] = i
  593. }
  594. }
  595. branchesToDelete := map[int]bool{}
  596. for key, life := range lives {
  597. if life == 1 {
  598. branchesToDelete[key] = true
  599. delete(lastMentioned, key)
  600. }
  601. }
  602. var optimizedPlan []runAction
  603. lastMentionedArr := make([][2]int, 0, len(lastMentioned) + 1)
  604. for key, val := range lastMentioned {
  605. if val != len(plan) - 1 {
  606. lastMentionedArr = append(lastMentionedArr, [2]int{val, key})
  607. }
  608. }
  609. if len(lastMentionedArr) == 0 && len(branchesToDelete) == 0 {
  610. // early return - we have nothing to optimize
  611. return plan
  612. }
  613. sort.Slice(lastMentionedArr, func(i, j int) bool {
  614. return lastMentionedArr[i][0] < lastMentionedArr[j][0]
  615. })
  616. lastMentionedArr = append(lastMentionedArr, [2]int{len(plan)-1, -1})
  617. prevpi := -1
  618. for _, pair := range lastMentionedArr {
  619. for pi := prevpi + 1; pi <= pair[0]; pi++ {
  620. p := plan[pi]
  621. switch p.Action {
  622. case runActionCommit:
  623. if !branchesToDelete[p.Items[0]] {
  624. optimizedPlan = append(optimizedPlan, p)
  625. }
  626. case runActionFork:
  627. var newBranches []int
  628. for _, b := range p.Items {
  629. if !branchesToDelete[b] {
  630. newBranches = append(newBranches, b)
  631. }
  632. }
  633. if len(newBranches) > 1 {
  634. optimizedPlan = append(optimizedPlan, runAction{
  635. Action: runActionFork,
  636. Commit: p.Commit,
  637. Items: newBranches,
  638. })
  639. }
  640. case runActionMerge:
  641. var newBranches []int
  642. for _, b := range p.Items {
  643. if !branchesToDelete[b] {
  644. newBranches = append(newBranches, b)
  645. }
  646. }
  647. if len(newBranches) > 1 {
  648. optimizedPlan = append(optimizedPlan, runAction{
  649. Action: runActionMerge,
  650. Commit: p.Commit,
  651. Items: newBranches,
  652. })
  653. }
  654. case runActionEmerge:
  655. optimizedPlan = append(optimizedPlan, p)
  656. }
  657. }
  658. if pair[1] >= 0 {
  659. prevpi = pair[0]
  660. optimizedPlan = append(optimizedPlan, runAction{
  661. Action: runActionDelete,
  662. Commit: nil,
  663. Items: []int{pair[1]},
  664. })
  665. }
  666. }
  667. // single commit can be detected as redundant
  668. if len(optimizedPlan) > 0 {
  669. return optimizedPlan
  670. }
  671. return plan
  672. }