Skip to content

Commit 529681b

Browse files
authored
Simplify code (#113)
Straighten out small part of code so they are easier to understand. The method is to do things one step at a time, instead of large functions that does everything.
1 parent 95d5c6f commit 529681b

1 file changed

Lines changed: 28 additions & 30 deletions

File tree

src/graph.rs

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use crate::print::colors::to_terminal_color;
2121
use crate::settings::{BranchOrder, BranchSettings, MergePatterns, Settings};
2222
use git2::{BranchType, Commit, Error, Oid, Reference, Repository};
23-
use itertools::Itertools;
2423
use regex::Regex;
2524
use std::collections::{HashMap, HashSet};
2625

@@ -773,16 +772,14 @@ fn trace_branch(
773772
any_assigned = true;
774773

775774
let commit = repository.find_commit(curr_oid)?;
776-
match commit.parent_count() {
777-
0 => {
778-
start_index = Some(*index as i32);
779-
break;
780-
}
781-
_ => {
782-
prev_index = Some(*index);
783-
curr_oid = commit.parent_id(0)?;
784-
}
775+
if commit.parent_count() == 0 {
776+
// If no parents, this is the root commit, set `start_index` and break.
777+
start_index = Some(*index as i32);
778+
break;
785779
}
780+
// Set `prev_index` to the current commit's index and move to the first parent.
781+
prev_index = Some(*index);
782+
curr_oid = commit.parent_id(0)?;
786783
}
787784

788785
let branch = &mut branches[branch_index];
@@ -901,21 +898,21 @@ fn assign_branch_columns(
901898
group_occ[found].push((start, end));
902899
}
903900

904-
let group_offset: Vec<usize> = occupied
905-
.iter()
906-
.scan(0, |acc, group| {
907-
*acc += group.len();
908-
Some(*acc)
909-
})
910-
.collect();
901+
// Compute start column of each group
902+
let mut group_offset: Vec<usize> = vec![];
903+
let mut acc = 0;
904+
for group in occupied {
905+
group_offset.push(acc);
906+
acc += group.len();
907+
}
911908

909+
// Compute branch column. Up till now we have computed the branch group
910+
// and the column offset within that group. This was to make it easy to
911+
// insert columns between groups. Now it is time to convert offset relative
912+
// to the group the final column.
912913
for branch in branches {
913914
if let Some(column) = branch.visual.column {
914-
let offset = if branch.visual.order_group == 0 {
915-
0
916-
} else {
917-
group_offset[branch.visual.order_group - 1]
918-
};
915+
let offset = group_offset[branch.visual.order_group];
919916
branch.visual.column = Some(column + offset);
920917
}
921918
}
@@ -936,14 +933,15 @@ fn branch_color<T: Clone>(
936933
unknown: &[T],
937934
counter: usize,
938935
) -> T {
939-
let color = order
940-
.iter()
941-
.find_position(|(b, _)| {
942-
(name.starts_with(ORIGIN) && b.is_match(&name[7..])) || b.is_match(name)
943-
})
944-
.map(|(_pos, col)| &col.1[counter % col.1.len()])
945-
.unwrap_or_else(|| &unknown[counter % unknown.len()]);
946-
color.clone()
936+
let stripped_name = name.strip_prefix(ORIGIN).unwrap_or(name);
937+
938+
for (regex, colors) in order {
939+
if regex.is_match(stripped_name) {
940+
return colors[counter % colors.len()].clone();
941+
}
942+
}
943+
944+
unknown[counter % unknown.len()].clone()
947945
}
948946

949947
/// Tries to extract the name of a merged-in branch from the merge commit summary.

0 commit comments

Comments
 (0)