From 059b3b0e592b9429e74212d52b0c0714ed2d5c6f Mon Sep 17 00:00:00 2001 From: Peer Sommerlund Date: Mon, 8 Sep 2025 17:44:08 +0200 Subject: [PATCH 1/3] refactor: Simplify branch colouring Improve readability by switching to imperative code from functional code. --- src/graph.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/graph.rs b/src/graph.rs index f0b7727..2fd4d45 100644 --- a/src/graph.rs +++ b/src/graph.rs @@ -20,7 +20,6 @@ use crate::print::colors::to_terminal_color; use crate::settings::{BranchOrder, BranchSettings, MergePatterns, Settings}; use git2::{BranchType, Commit, Error, Oid, Reference, Repository}; -use itertools::Itertools; use regex::Regex; use std::collections::{HashMap, HashSet}; @@ -936,14 +935,15 @@ fn branch_color( unknown: &[T], counter: usize, ) -> T { - let color = order - .iter() - .find_position(|(b, _)| { - (name.starts_with(ORIGIN) && b.is_match(&name[7..])) || b.is_match(name) - }) - .map(|(_pos, col)| &col.1[counter % col.1.len()]) - .unwrap_or_else(|| &unknown[counter % unknown.len()]); - color.clone() + let stripped_name = name.strip_prefix(ORIGIN).unwrap_or(name); + + for (regex, colors) in order { + if regex.is_match(stripped_name) { + return colors[counter % colors.len()].clone(); + } + } + + unknown[counter % unknown.len()].clone() } /// Tries to extract the name of a merged-in branch from the merge commit summary. From 5ec1159b64b98cf7911613734b43c1a7680fb5eb Mon Sep 17 00:00:00 2001 From: Peer Sommerlund Date: Sun, 24 Aug 2025 07:43:08 +0200 Subject: [PATCH 2/3] refactor: Simplify trace_branch loop Replace match with if to show primary flow. --- src/graph.rs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/graph.rs b/src/graph.rs index 2fd4d45..04a8aba 100644 --- a/src/graph.rs +++ b/src/graph.rs @@ -772,16 +772,14 @@ fn trace_branch( any_assigned = true; let commit = repository.find_commit(curr_oid)?; - match commit.parent_count() { - 0 => { - start_index = Some(*index as i32); - break; - } - _ => { - prev_index = Some(*index); - curr_oid = commit.parent_id(0)?; - } + if commit.parent_count() == 0 { + // If no parents, this is the root commit, set `start_index` and break. + start_index = Some(*index as i32); + break; } + // Set `prev_index` to the current commit's index and move to the first parent. + prev_index = Some(*index); + curr_oid = commit.parent_id(0)?; } let branch = &mut branches[branch_index]; From 46ea6ccbe91d827007b05bf8477a88eec1bc7487 Mon Sep 17 00:00:00 2001 From: Peer Sommerlund Date: Sun, 17 Aug 2025 06:27:57 +0200 Subject: [PATCH 3/3] refactor: Simplify branch column assignment Change content of group_offset to include offset 0 at index 0. Convert code from functional to imperative. --- src/graph.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/graph.rs b/src/graph.rs index 04a8aba..e03823b 100644 --- a/src/graph.rs +++ b/src/graph.rs @@ -898,21 +898,21 @@ fn assign_branch_columns( group_occ[found].push((start, end)); } - let group_offset: Vec = occupied - .iter() - .scan(0, |acc, group| { - *acc += group.len(); - Some(*acc) - }) - .collect(); + // Compute start column of each group + let mut group_offset: Vec = vec![]; + let mut acc = 0; + for group in occupied { + group_offset.push(acc); + acc += group.len(); + } + // Compute branch column. Up till now we have computed the branch group + // and the column offset within that group. This was to make it easy to + // insert columns between groups. Now it is time to convert offset relative + // to the group the final column. for branch in branches { if let Some(column) = branch.visual.column { - let offset = if branch.visual.order_group == 0 { - 0 - } else { - group_offset[branch.visual.order_group - 1] - }; + let offset = group_offset[branch.visual.order_group]; branch.visual.column = Some(column + offset); } }