Browse Source

Merge pull request #99 from vmarkovtsev/master

Fix the bug with removing commits
Vadim Markovtsev 6 years ago
parent
commit
61bbc0812d
2 changed files with 17 additions and 71 deletions
  1. 10 68
      internal/core/forks.go
  2. 7 3
      labours.py

+ 10 - 68
internal/core/forks.go

@@ -143,7 +143,7 @@ func prepareRunPlan(commits []*object.Commit) []runAction {
 	}
 	fmt.Printf("}\n")*/
 	plan := generatePlan(orderNodes, hashes, mergedDag, dag, mergedSeq)
-	plan = optimizePlan(plan)
+	plan = collectGarbage(plan)
 	/*for _, p := range plan {
 		firstItem := p.Items[0]
 		switch p.Action {
@@ -591,26 +591,16 @@ func generatePlan(
 	return plan
 }
 
-// optimizePlan removes "dead" nodes and inserts `runActionDelete` disposal steps.
-//
-// |   *
-// *  /
-// |\/
-// |/
-// *
-//
-func optimizePlan(plan []runAction) []runAction {
-	// lives maps branch index to the number of commits in that branch
-	lives := map[int]int{}
+// collectGarbage inserts `runActionDelete` disposal steps.
+func collectGarbage(plan []runAction) []runAction {
 	// lastMentioned maps branch index to the index inside `plan` when that branch was last used
 	lastMentioned := map[int]int{}
 	for i, p := range plan {
 		firstItem := p.Items[0]
 		switch p.Action {
 		case runActionCommit:
-			lives[firstItem]++
 			lastMentioned[firstItem] = i
-			if firstItem == -1 {
+			if firstItem < rootBranchIndex {
 				log.Panicf("commit %s does not have an assigned branch",
 					p.Commit.Hash.String())
 			}
@@ -621,26 +611,18 @@ func optimizePlan(plan []runAction) []runAction {
 				lastMentioned[item] = i
 			}
 		case runActionEmerge:
-			lives[firstItem]++
 			lastMentioned[firstItem] = i
 		}
 	}
-	branchesToDelete := map[int]bool{}
-	for key, life := range lives {
-		if life == 1 {
-			branchesToDelete[key] = true
-			delete(lastMentioned, key)
-		}
-	}
-	var optimizedPlan []runAction
+	var garbageCollectedPlan []runAction
 	lastMentionedArr := make([][2]int, 0, len(lastMentioned) + 1)
 	for key, val := range lastMentioned {
 		if val != len(plan) - 1 {
 			lastMentionedArr = append(lastMentionedArr, [2]int{val, key})
 		}
 	}
-	if len(lastMentionedArr) == 0 && len(branchesToDelete) == 0 {
-		// early return - we have nothing to optimize
+	if len(lastMentionedArr) == 0 {
+		// early return - we have nothing to collect
 		return plan
 	}
 	sort.Slice(lastMentionedArr, func(i, j int) bool {
@@ -650,56 +632,16 @@ func optimizePlan(plan []runAction) []runAction {
 	prevpi := -1
 	for _, pair := range lastMentionedArr {
 		for pi := prevpi + 1; pi <= pair[0]; pi++ {
-			p := plan[pi]
-			switch p.Action {
-			case runActionCommit:
-				if !branchesToDelete[p.Items[0]] {
-					optimizedPlan = append(optimizedPlan, p)
-				}
-			case runActionFork:
-				var newBranches []int
-				for _, b := range p.Items {
-					if !branchesToDelete[b] {
-						newBranches = append(newBranches, b)
-					}
-				}
-				if len(newBranches) > 1 {
-					optimizedPlan = append(optimizedPlan, runAction{
-						Action: runActionFork,
-						Commit: p.Commit,
-						Items:  newBranches,
-					})
-				}
-			case runActionMerge:
-				var newBranches []int
-				for _, b := range p.Items {
-					if !branchesToDelete[b] {
-						newBranches = append(newBranches, b)
-					}
-				}
-				if len(newBranches) > 1 {
-					optimizedPlan = append(optimizedPlan, runAction{
-						Action: runActionMerge,
-						Commit: p.Commit,
-						Items:  newBranches,
-					})
-				}
-			case runActionEmerge:
-				optimizedPlan = append(optimizedPlan, p)
-			}
+			garbageCollectedPlan = append(garbageCollectedPlan, plan[pi])
 		}
 		if pair[1] >= 0 {
 			prevpi = pair[0]
-			optimizedPlan = append(optimizedPlan, runAction{
+			garbageCollectedPlan = append(garbageCollectedPlan, runAction{
 				Action: runActionDelete,
 				Commit: nil,
 				Items:  []int{pair[1]},
 			})
 		}
 	}
-	// single commit can be detected as redundant
-	if len(optimizedPlan) > 0 {
-		return optimizedPlan
-	}
-	return plan
+	return garbageCollectedPlan
 }

+ 7 - 3
labours.py

@@ -609,7 +609,10 @@ def apply_plot_style(figure, axes, legend, style, text_size, axes_size):
     for axis in ("x", "y"):
         getattr(axes, axis + "axis").get_offset_text().set_size(text_size)
         axes.tick_params(axis=axis, colors=style, labelsize=text_size)
-    axes.ticklabel_format(axis="y", style="sci", scilimits=(0, 3))
+    try:
+        axes.ticklabel_format(axis="y", style="sci", scilimits=(0, 3))
+    except AttributeError:
+        pass
     if legend is not None:
         frame = legend.get_frame()
         for setter in (frame.set_facecolor, frame.set_edgecolor):
@@ -771,15 +774,16 @@ def plot_churn_matrix(args, repo, people, matrix):
     ax.matshow(matrix, cmap=pyplot.cm.OrRd)
     ax.set_xticks(numpy.arange(0, matrix.shape[1]))
     ax.set_yticks(numpy.arange(0, matrix.shape[0]))
-    ax.set_xticklabels(["Unidentified"] + people, rotation=90, ha="center")
     ax.set_yticklabels(people, va="center")
     ax.set_xticks(numpy.arange(0.5, matrix.shape[1] + 0.5), minor=True)
+    ax.set_xticklabels(["Unidentified"] + people, rotation=45, ha="left",
+                       va="bottom", rotation_mode="anchor")
     ax.set_yticks(numpy.arange(0.5, matrix.shape[0] + 0.5), minor=True)
     ax.grid(which="minor")
     apply_plot_style(fig, ax, None, args.style, args.text_size, args.size)
     if not args.output:
         pos1 = ax.get_position()
-        pos2 = (pos1.x0 + 0.245, pos1.y0 - 0.1, pos1.width * 0.9, pos1.height * 0.9)
+        pos2 = (pos1.x0 + 0.15, pos1.y0 - 0.1, pos1.width * 0.9, pos1.height * 0.9)
         ax.set_position(pos2)
     if args.mode == "all":
         output = get_plot_path(args.output, "matrix")