@@ -83,7 +83,8 @@ pub fn print_unicode(graph: &GitGraph, settings: &Settings) -> Result<UnicodeGra
8383 None
8484 } ;
8585
86- // Compute commit text into text_lines
86+ // Compute commit text into text_lines and add blank rows
87+ // if needed to match branch graph inserts.
8788 let mut index_map = vec ! [ ] ;
8889 let mut text_lines = vec ! [ ] ;
8990 let mut offset = 0 ;
@@ -420,11 +421,38 @@ fn hline(
420421 }
421422}
422423
423- /// Calculates required additional rows
424+ /// Calculates required additional rows to visually connect commits that
425+ /// are not direct descendants in the main commit list. These "inserts"
426+ // represent the horizontal lines in the graph.
427+ ///
428+ /// # Arguments
429+ ///
430+ /// * `graph`: A reference to the `GitGraph` structure containing the
431+ // commit and branch information.
432+ /// * `compact`: A boolean indicating whether to use a compact layout,
433+ // potentially merging some insertions with commits.
434+ ///
435+ /// # Returns
436+ ///
437+ /// A `HashMap` where the keys are the indices of commits in the
438+ /// `graph.commits` vector, and the values are vectors of vectors
439+ /// of `Occ`. Each inner vector represents a potential row of
440+ /// insertions needed *before* the commit at the key index. The
441+ /// `Occ` enum describes what occupies a cell in that row
442+ /// (either a commit or a range representing a connection).
443+ ///
424444fn get_inserts ( graph : & GitGraph , compact : bool ) -> HashMap < usize , Vec < Vec < Occ > > > {
445+ // Initialize an empty HashMap to store the required insertions. The key is the commit
446+ // index, and the value is a vector of rows, where each row is a vector of Occupations (`Occ`).
425447 let mut inserts: HashMap < usize , Vec < Vec < Occ > > > = HashMap :: new ( ) ;
426448
449+ // First, for each commit, we initialize an entry in the `inserts`
450+ // map with a single row containing the commit itself. This ensures
451+ // that every commit has a position in the grid.
427452 for ( idx, info) in graph. commits . iter ( ) . enumerate ( ) {
453+ // Get the visual column assigned to the branch of this commit. Unwrap is safe here
454+ // because `branch_trace` should always point to a valid branch with an assigned column
455+ // for commits that are included in the filtered graph.
428456 let column = graph. all_branches [ info. branch_trace . unwrap ( ) ]
429457 . visual
430458 . column
@@ -433,30 +461,56 @@ fn get_inserts(graph: &GitGraph, compact: bool) -> HashMap<usize, Vec<Vec<Occ>>>
433461 inserts. insert ( idx, vec ! [ vec![ Occ :: Commit ( idx, column) ] ] ) ;
434462 }
435463
464+ // Now, iterate through the commits again to identify connections
465+ // needed between parents that are not directly adjacent in the
466+ // `graph.commits` list.
436467 for ( idx, info) in graph. commits . iter ( ) . enumerate ( ) {
468+ // If the commit has a branch trace (meaning it belongs to a visualized branch).
437469 if let Some ( trace) = info. branch_trace {
470+ // Get the `BranchInfo` for the current commit's branch.
438471 let branch = & graph. all_branches [ trace] ;
472+ // Get the visual column of the current commit's branch. Unwrap is safe as explained above.
439473 let column = branch. visual . column . unwrap ( ) ;
440474
475+ // Iterate through the two possible parents of the current commit.
441476 for p in 0 ..2 {
477+ // If the commit has a parent at this index (0 for the first parent, 1 for the second).
442478 if let Some ( par_oid) = info. parents [ p] {
479+ // Try to find the index of the parent commit in the `graph.commits` vector.
443480 if let Some ( par_idx) = graph. indices . get ( & par_oid) {
444481 let par_info = & graph. commits [ * par_idx] ;
445482 let par_branch = & graph. all_branches [ par_info. branch_trace . unwrap ( ) ] ;
446483 let par_column = par_branch. visual . column . unwrap ( ) ;
484+ // Determine the sorted range of columns between the current commit and its parent.
447485 let column_range = sorted ( column, par_column) ;
448486
487+ // If the column of the current commit is different from the column of its parent,
488+ // it means we need to draw a horizontal line (an "insert") to connect them.
449489 if column != par_column {
490+ // Find the index in the `graph.commits` list where the visual connection
491+ // should deviate from the parent's line. This helps in drawing the graph
492+ // correctly when branches diverge or merge.
450493 let split_index = super :: get_deviate_index ( graph, idx, * par_idx) ;
494+ // Access the entry in the `inserts` map for the `split_index`.
451495 match inserts. entry ( split_index) {
496+ // If there's already an entry at this `split_index` (meaning other
497+ // insertions might be needed before this commit).
452498 Occupied ( mut entry) => {
499+ // Find the first available row in the existing vector of rows
500+ // where the new range doesn't overlap with existing occupations.
453501 let mut insert_at = entry. get ( ) . len ( ) ;
454502 for ( insert_idx, sub_entry) in entry. get ( ) . iter ( ) . enumerate ( ) {
455503 let mut occ = false ;
504+ // Check for overlaps with existing `Occ` in the current row.
456505 for other_range in sub_entry {
506+ // Check if the current column range overlaps with the other range.
457507 if other_range. overlaps ( & column_range) {
458508 match other_range {
509+ // If the other occupation is a commit.
459510 Occ :: Commit ( target_index, _) => {
511+ // In compact mode, we might allow overlap with the commit itself
512+ // for merge commits (specifically the second parent) to keep the
513+ // graph tighter.
460514 if !compact
461515 || !info. is_merge
462516 || idx != * target_index
@@ -466,7 +520,9 @@ fn get_inserts(graph: &GitGraph, compact: bool) -> HashMap<usize, Vec<Vec<Occ>>>
466520 break ;
467521 }
468522 }
523+ // If the other occupation is a range (another connection).
469524 Occ :: Range ( o_idx, o_par_idx, _, _) => {
525+ // Avoid overlap with connections between the same commits.
470526 if idx != * o_idx && par_idx != o_par_idx {
471527 occ = true ;
472528 break ;
@@ -475,12 +531,15 @@ fn get_inserts(graph: &GitGraph, compact: bool) -> HashMap<usize, Vec<Vec<Occ>>>
475531 }
476532 }
477533 }
534+ // If no overlap is found in this row, we can insert here.
478535 if !occ {
479536 insert_at = insert_idx;
480537 break ;
481538 }
482539 }
540+ // Get a mutable reference to the vector of rows for this `split_index`.
483541 let vec = entry. get_mut ( ) ;
542+ // If no suitable row was found, add a new row.
484543 if insert_at == vec. len ( ) {
485544 vec. push ( vec ! [ Occ :: Range (
486545 idx,
@@ -489,6 +548,7 @@ fn get_inserts(graph: &GitGraph, compact: bool) -> HashMap<usize, Vec<Vec<Occ>>>
489548 column_range. 1 ,
490549 ) ] ) ;
491550 } else {
551+ // Otherwise, insert the new range into the found row.
492552 vec[ insert_at] . push ( Occ :: Range (
493553 idx,
494554 * par_idx,
@@ -497,7 +557,9 @@ fn get_inserts(graph: &GitGraph, compact: bool) -> HashMap<usize, Vec<Vec<Occ>>>
497557 ) ) ;
498558 }
499559 }
560+ // If there's no entry at this `split_index` yet.
500561 Vacant ( entry) => {
562+ // Create a new entry with a single row containing the range.
501563 entry. insert ( vec ! [ vec![ Occ :: Range (
502564 idx,
503565 * par_idx,
@@ -513,6 +575,7 @@ fn get_inserts(graph: &GitGraph, compact: bool) -> HashMap<usize, Vec<Vec<Occ>>>
513575 }
514576 }
515577
578+ // Return the map of required insertions.
516579 inserts
517580}
518581
0 commit comments