@@ -21,12 +21,14 @@ use crate::Error;
21
21
/// use gix_blame::BlameRanges;
22
22
///
23
23
/// // 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);
25
25
///
26
26
/// // 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
+ /// );
30
32
/// ```
31
33
///
32
34
/// # Line Number Representation
@@ -40,39 +42,53 @@ use crate::Error;
40
42
/// An empty `BlameRanges` (created via `BlameRanges::new()` or `BlameRanges::default()`) means
41
43
/// to blame the entire file, similar to running `git blame` without line number arguments.
42
44
#[ 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 > > ) ,
47
51
}
48
52
49
53
/// Lifecycle
50
54
impl 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
-
58
55
/// Create from a single range.
59
56
///
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] )
63
65
}
64
66
65
67
/// Create from multiple ranges.
66
68
///
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) ;
73
82
}
74
83
result
75
84
}
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
+ }
76
92
}
77
93
78
94
impl BlameRanges {
@@ -81,60 +97,51 @@ impl BlameRanges {
81
97
/// The range should be 1-based inclusive.
82
98
/// If the new range overlaps with or is adjacent to an existing range,
83
99
/// 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
+ }
86
108
}
87
109
88
110
/// Attempts to merge the new range with any existing ranges.
89
111
/// 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 ( ( ) )
97
131
}
132
+ _ => Err ( Error :: InvalidOneBasedLineRange ) ,
98
133
}
99
- // If no overlap found, add it as a new range
100
- self . ranges . push ( new_range) ;
101
134
}
102
135
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]
129
142
}
130
- result . push ( start..end ) ;
143
+ Self :: PartialFile ( ranges ) => ranges . clone ( ) ,
131
144
}
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 ( )
138
145
}
139
146
}
140
147
@@ -334,6 +341,17 @@ pub struct UnblamedHunk {
334
341
}
335
342
336
343
impl 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
+
337
355
pub ( crate ) fn has_suspect ( & self , suspect : & ObjectId ) -> bool {
338
356
self . suspects . iter ( ) . any ( |entry| entry. 0 == * suspect)
339
357
}
0 commit comments