@@ -21,12 +21,14 @@ use crate::Error;
2121/// use gix_blame::BlameRanges;
2222///
2323/// // Blame lines 20 through 40 (inclusive)
24- /// let range = BlameRanges::from_range (20..=40);
24+ /// let range = BlameRanges::from_one_based_inclusive_range (20..=40);
2525///
2626/// // Blame multiple ranges
27- /// let mut ranges = BlameRanges::new();
28- /// ranges.add_range(1..=4); // Lines 1-4
29- /// ranges.add_range(10..=14); // Lines 10-14
27+ /// let mut ranges = BlameRanges::from_one_based_inclusive_ranges(vec![
28+ /// 1..=4, // Lines 1-4
29+ /// 10..=14, // Lines 10-14
30+ /// ]
31+ /// );
3032/// ```
3133///
3234/// # Line Number Representation
@@ -40,39 +42,53 @@ use crate::Error;
4042/// An empty `BlameRanges` (created via `BlameRanges::new()` or `BlameRanges::default()`) means
4143/// to blame the entire file, similar to running `git blame` without line number arguments.
4244#[ derive( Debug , Clone , Default ) ]
43- pub struct BlameRanges {
44- /// The ranges to blame, stored as 1-based inclusive ranges
45- /// An empty Vec means blame the entire file
46- ranges : Vec < RangeInclusive < u32 > > ,
45+ pub enum BlameRanges {
46+ /// Blame the entire file.
47+ #[ default]
48+ WholeFile ,
49+ /// Blame ranges in 0-based exclusive format.
50+ PartialFile ( Vec < Range < u32 > > ) ,
4751}
4852
4953/// Lifecycle
5054impl BlameRanges {
51- /// Create a new empty BlameRanges instance.
52- ///
53- /// An empty instance means to blame the entire file.
54- pub fn new ( ) -> Self {
55- Self :: default ( )
56- }
57-
5855 /// Create from a single range.
5956 ///
60- /// The range is 1-based, similar to git's line number format.
61- pub fn from_range ( range : RangeInclusive < u32 > ) -> Self {
62- Self { ranges : vec ! [ range] }
57+ /// Note that the input range is 1-based inclusive, as used by git, and
58+ /// the output is zero-based `BlameRanges` instance.
59+ ///
60+ /// @param range: A 1-based inclusive range.
61+ /// @return: A `BlameRanges` instance representing the range.
62+ pub fn from_one_based_inclusive_range ( range : RangeInclusive < u32 > ) -> Self {
63+ let zero_based_range = Self :: inclusive_to_zero_based_exclusive ( range) ;
64+ Self :: PartialFile ( vec ! [ zero_based_range] )
6365 }
6466
6567 /// Create from multiple ranges.
6668 ///
67- /// All ranges are 1-based.
68- /// Overlapping or adjacent ranges will be merged.
69- pub fn from_ranges ( ranges : Vec < RangeInclusive < u32 > > ) -> Self {
70- let mut result = Self :: new ( ) ;
71- for range in ranges {
72- result. merge_range ( range) ;
69+ /// Note that the input ranges are 1-based inclusive, as used by git, and
70+ /// the output is zero-based `BlameRanges` instance.
71+ ///
72+ /// @param ranges: A vec of 1-based inclusive range.
73+ /// @return: A `BlameRanges` instance representing the range.
74+ pub fn from_one_based_inclusive_ranges ( ranges : Vec < RangeInclusive < u32 > > ) -> Self {
75+ let zero_based_ranges = ranges
76+ . into_iter ( )
77+ . map ( Self :: inclusive_to_zero_based_exclusive)
78+ . collect :: < Vec < _ > > ( ) ;
79+ let mut result = Self :: PartialFile ( vec ! [ ] ) ;
80+ for range in zero_based_ranges {
81+ let _ = result. merge_range ( range) ;
7382 }
7483 result
7584 }
85+
86+ /// Convert a 1-based inclusive range to a 0-based exclusive range.
87+ fn inclusive_to_zero_based_exclusive ( range : RangeInclusive < u32 > ) -> Range < u32 > {
88+ let start = range. start ( ) - 1 ;
89+ let end = * range. end ( ) ;
90+ start..end
91+ }
7692}
7793
7894impl BlameRanges {
@@ -81,60 +97,51 @@ impl BlameRanges {
8197 /// The range should be 1-based inclusive.
8298 /// If the new range overlaps with or is adjacent to an existing range,
8399 /// they will be merged into a single range.
84- pub fn add_range ( & mut self , new_range : RangeInclusive < u32 > ) {
85- self . merge_range ( new_range) ;
100+ pub fn add_range ( & mut self , new_range : RangeInclusive < u32 > ) -> Result < ( ) , Error > {
101+ match self {
102+ Self :: PartialFile ( _) => {
103+ let zero_based_range = Self :: inclusive_to_zero_based_exclusive ( new_range) ;
104+ self . merge_range ( zero_based_range)
105+ }
106+ _ => Err ( Error :: InvalidOneBasedLineRange ) ,
107+ }
86108 }
87109
88110 /// Attempts to merge the new range with any existing ranges.
89111 /// If no merge is possible, add it as a new range.
90- fn merge_range ( & mut self , new_range : RangeInclusive < u32 > ) {
91- // Check if this range can be merged with any existing range
92- for range in & mut self . ranges {
93- // Check if ranges overlap or are adjacent
94- if new_range. start ( ) <= range. end ( ) && range. start ( ) <= new_range. end ( ) {
95- * range = * range. start ( ) . min ( new_range. start ( ) ) ..=* range. end ( ) . max ( new_range. end ( ) ) ;
96- return ;
112+ fn merge_range ( & mut self , new_range : Range < u32 > ) -> Result < ( ) , Error > {
113+ match self {
114+ Self :: PartialFile ( ref mut ranges) => {
115+ // Check if this range can be merged with any existing range
116+ for range in & mut * ranges {
117+ // Check if ranges overlap
118+ if new_range. start <= range. end && range. start <= new_range. end {
119+ * range = range. start . min ( new_range. start ) ..range. end . max ( new_range. end ) ;
120+ return Ok ( ( ) ) ;
121+ }
122+ // Check if ranges are adjacent
123+ if new_range. start == range. end || range. start == new_range. end {
124+ * range = range. start . min ( new_range. start ) ..range. end . max ( new_range. end ) ;
125+ return Ok ( ( ) ) ;
126+ }
127+ }
128+ // If no overlap or adjacency found, add it as a new range
129+ ranges. push ( new_range) ;
130+ Ok ( ( ) )
97131 }
132+ _ => Err ( Error :: InvalidOneBasedLineRange ) ,
98133 }
99- // If no overlap found, add it as a new range
100- self . ranges . push ( new_range) ;
101134 }
102135
103- /// Convert the 1-based inclusive ranges to 0-based exclusive ranges.
104- ///
105- /// This is used internally by the blame algorithm to convert from git's line number format
106- /// to the internal format used for processing.
107- ///
108- /// # Errors
109- ///
110- /// Returns `Error::InvalidLineRange` if:
111- /// - Any range starts at 0 (must be 1-based)
112- /// - Any range extends beyond the file's length
113- /// - Any range has the same start and end
114- pub fn to_zero_based_exclusive ( & self , max_lines : u32 ) -> Result < Vec < Range < u32 > > , Error > {
115- if self . ranges . is_empty ( ) {
116- let range = 0 ..max_lines;
117- return Ok ( vec ! [ range] ) ;
118- }
119-
120- let mut result = Vec :: with_capacity ( self . ranges . len ( ) ) ;
121- for range in & self . ranges {
122- if * range. start ( ) == 0 {
123- return Err ( Error :: InvalidLineRange ) ;
124- }
125- let start = range. start ( ) - 1 ;
126- let end = * range. end ( ) ;
127- if start >= max_lines || end > max_lines || start == end {
128- return Err ( Error :: InvalidLineRange ) ;
136+ /// Convert the ranges to a vector of `Range<u32>`.
137+ pub fn to_ranges ( & self , max_lines : u32 ) -> Vec < Range < u32 > > {
138+ match self {
139+ Self :: WholeFile => {
140+ let full_range = 0 ..max_lines;
141+ vec ! [ full_range]
129142 }
130- result . push ( start..end ) ;
143+ Self :: PartialFile ( ranges ) => ranges . clone ( ) ,
131144 }
132- Ok ( result)
133- }
134-
135- /// Returns true if no specific ranges are set (meaning blame entire file)
136- pub fn is_empty ( & self ) -> bool {
137- self . ranges . is_empty ( )
138145 }
139146}
140147
@@ -334,6 +341,17 @@ pub struct UnblamedHunk {
334341}
335342
336343impl UnblamedHunk {
344+ /// Create a new instance
345+ pub fn new ( range : Range < u32 > , suspect : ObjectId ) -> Self {
346+ let range_start = range. start ;
347+ let range_end = range. end ;
348+
349+ UnblamedHunk {
350+ range_in_blamed_file : range_start..range_end,
351+ suspects : [ ( suspect, range_start..range_end) ] . into ( ) ,
352+ }
353+ }
354+
337355 pub ( crate ) fn has_suspect ( & self , suspect : & ObjectId ) -> bool {
338356 self . suspects . iter ( ) . any ( |entry| entry. 0 == * suspect)
339357 }
0 commit comments