Skip to content

Commit 096724b

Browse files
committed
Extract remove_commits_not_on_a_branch
1 parent 9a75d09 commit 096724b

1 file changed

Lines changed: 49 additions & 38 deletions

File tree

src/graph.rs

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -160,44 +160,8 @@ impl GitGraph {
160160
track::correct_fork_merges(&commits, &indices, &mut all_branches)?;
161161
track::assign_sources_targets(&commits, &indices, &mut all_branches);
162162

163-
// Remove commits not on a branch. This will give all commits a new index.
164-
let filtered_commits: Vec<CommitInfo> = commits
165-
.into_iter()
166-
.filter(|info| info.branch_trace.is_some())
167-
.collect();
168-
169-
// Create indices from git object id into the filtered commits
170-
let filtered_indices: HashMap<Oid, usize> = filtered_commits
171-
.iter()
172-
.enumerate()
173-
.map(|(idx, info)| (info.oid, idx))
174-
.collect();
175-
176-
// Map from old index to new index. None, if old index was removed
177-
let index_map: HashMap<usize, Option<&usize>> = indices
178-
.iter()
179-
.map(|(oid, index)| (*index, filtered_indices.get(oid)))
180-
.collect();
181-
182-
// Update branch.range from old to new index. Shrink if endpoints were removed.
183-
for branch in all_branches.iter_mut() {
184-
if let Some(mut start_idx) = branch.range.0 {
185-
let mut idx0 = index_map[&start_idx];
186-
while idx0.is_none() {
187-
start_idx += 1;
188-
idx0 = index_map[&start_idx];
189-
}
190-
branch.range.0 = Some(*idx0.unwrap());
191-
}
192-
if let Some(mut end_idx) = branch.range.1 {
193-
let mut idx0 = index_map[&end_idx];
194-
while idx0.is_none() {
195-
end_idx -= 1;
196-
idx0 = index_map[&end_idx];
197-
}
198-
branch.range.1 = Some(*idx0.unwrap());
199-
}
200-
}
163+
let (filtered_commits, filtered_indices) =
164+
remove_commits_not_on_a_branch(commits, indices, &mut all_branches);
201165

202166
let all_commits = 0..filtered_commits.len();
203167
let tracks = TrackMap {
@@ -230,6 +194,53 @@ impl GitGraph {
230194
}
231195
}
232196

197+
/// Consume commits and indices, and return filtered versions
198+
fn remove_commits_not_on_a_branch(
199+
commits: Vec<CommitInfo>,
200+
indices: HashMap<Oid, usize>,
201+
all_branches: &mut [BranchInfo],
202+
) -> (Vec<CommitInfo>, HashMap<Oid, usize>) {
203+
// Remove commits not on a branch. This will give all commits a new index.
204+
let filtered_commits: Vec<CommitInfo> = commits
205+
.into_iter()
206+
.filter(|info| info.branch_trace.is_some())
207+
.collect();
208+
209+
// Create indices from git object id into the filtered commits
210+
let filtered_indices: HashMap<Oid, usize> = filtered_commits
211+
.iter()
212+
.enumerate()
213+
.map(|(idx, info)| (info.oid, idx))
214+
.collect();
215+
216+
// Map from old index to new index. None, if old index was removed
217+
let index_map: HashMap<usize, Option<&usize>> = indices
218+
.iter()
219+
.map(|(oid, index)| (*index, filtered_indices.get(oid)))
220+
.collect();
221+
222+
// Update branch.range from old to new index. Shrink if endpoints were removed.
223+
for branch in all_branches.iter_mut() {
224+
if let Some(mut start_idx) = branch.range.0 {
225+
let mut idx0 = index_map[&start_idx];
226+
while idx0.is_none() {
227+
start_idx += 1;
228+
idx0 = index_map[&start_idx];
229+
}
230+
branch.range.0 = Some(*idx0.unwrap());
231+
}
232+
if let Some(mut end_idx) = branch.range.1 {
233+
let mut idx0 = index_map[&end_idx];
234+
while idx0.is_none() {
235+
end_idx -= 1;
236+
idx0 = index_map[&end_idx];
237+
}
238+
branch.range.1 = Some(*idx0.unwrap());
239+
}
240+
}
241+
(filtered_commits, filtered_indices)
242+
}
243+
233244
/// Information about the current HEAD
234245
pub struct HeadInfo {
235246
pub oid: Oid,

0 commit comments

Comments
 (0)