Skip to content

Commit

Permalink
Fix panic
Browse files Browse the repository at this point in the history
When your branch name is longer than 40 characters and your depth is
above the padding of 3 for the currently active branch a panic is hit.
This makes it so it's not longer to ever call strings Repeat with a
negative value.
  • Loading branch information
michaeljs1990 committed Jan 30, 2022
1 parent 22b2e50 commit c79ab86
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@

/bazel-*
/release
/arc
20 changes: 11 additions & 9 deletions lib/console/draw_graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ func DrawGraph(bnw git.BranchNodeWrapper, opts *DrawOpts) string {
return "No branches found"
}

maxDepth := bnw.MaxDepth()

// This could be part of the recursive drawLine call but is pulled out to
// reduce the complexity and allow me to understand this when I read it later.
lines := []string{}
for _, node := range bnw.RootNodes {
lines = append(lines, walkNodes(internalOpts, node, 0, []int{}, false, bnw.LongestBranchLength)...)
lines = append(lines, walkNodes(internalOpts, node, 0, []int{}, false, bnw.LongestBranchLength, maxDepth)...)
}

output := strings.Join(lines, "\n")
Expand All @@ -55,7 +57,7 @@ func DrawGraph(bnw git.BranchNodeWrapper, opts *DrawOpts) string {

// Render a single line of the flow output
func walkNodes(o *DrawOpts, n *git.BranchNode, depth int,
openDepths []int, cap bool, longestBranch int) []string {
openDepths []int, cap bool, longestBranch int, maxDepth int) []string {

var lines []string

Expand All @@ -70,7 +72,7 @@ func walkNodes(o *DrawOpts, n *git.BranchNode, depth int,
// If this is a root node we don't output much information for it. If you are using flow
// like intended this should matter but may lead to some confusion if you start committing
// to a root branch (likely main or master).
lines = append(lines, drawLine(*o, n, depth, openDepths, cap, longestBranch))
lines = append(lines, drawLine(*o, n, depth, openDepths, cap, longestBranch, maxDepth))

if len(n.Downstream) > 0 {
for i, node := range n.Downstream {
Expand All @@ -81,7 +83,7 @@ func walkNodes(o *DrawOpts, n *git.BranchNode, depth int,
cap = true
}

lines = append(lines, walkNodes(o, node, depth+1, openDepths, cap, longestBranch)...)
lines = append(lines, walkNodes(o, node, depth+1, openDepths, cap, longestBranch, maxDepth)...)
}
}

Expand Down Expand Up @@ -120,8 +122,8 @@ func isDepthOpen(openDepths []int, depth int) bool {
}

// draw a single line taking into account all of the options passed in
func drawLine(o DrawOpts, n *git.BranchNode,
depth int, openDepths []int, cap bool, longestBranch int) string {
func drawLine(o DrawOpts, n *git.BranchNode, depth int,
openDepths []int, cap bool, longestBranch int, maxDepth int) string {

padding := ""

Expand Down Expand Up @@ -152,12 +154,12 @@ func drawLine(o DrawOpts, n *git.BranchNode,
}

defaultBranchPadding := 40
if defaultBranchPadding < longestBranch {
if defaultBranchPadding < (longestBranch + maxDepth) {
// +3 is notable here because the padding is used with strings.Repeat which can't have
// a negative number. We do +1 because we want a space between the branch name and whatever
// comes after it. We do another +2 because if it's the active branch we need to leave space
// for the current branch market to be rendered and leave a space on both sides.
defaultBranchPadding = longestBranch + 3
// for the current branch marker to be rendered and leave a space on both sides.
defaultBranchPadding = longestBranch + maxDepth + 3
}

// Padding to add to the end of every branch name. This should be a reasonable number
Expand Down
39 changes: 39 additions & 0 deletions lib/git/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ func (b BranchNodeWrapper) IsEmpty() bool {
return len(b.RootNodes) == 0
}

func (b BranchNodeWrapper) MaxDepth() int {
max := 0

for _, x := range b.RootNodes {
depth := x.MaxDepth()
if depth > max {
max = depth
}
}

return max
}

type BranchNode struct {
Name string
Merge string
Expand Down Expand Up @@ -68,3 +81,29 @@ func (b BranchNode) CountDownstreams() int {

return r(b)
}

func (b BranchNode) maxDepth(i int) int {
max := i
for _, x := range b.Downstream {
i++
depth := x.maxDepth(i)
if depth > max {
max = depth
}
}

return max
}

// MaxDepth looks for the longest path from this branch
func (b BranchNode) MaxDepth() int {
max := 0
for _, x := range b.Downstream {
depth := x.maxDepth(0)
if depth > max {
max = depth
}
}

return max
}

0 comments on commit c79ab86

Please sign in to comment.