|
97 | 97 | return Ok(Outcome::default()); |
98 | 98 | } |
99 | 99 |
|
100 | | - // This function assumes that `range` has 1-based inclusive line numbers and converts it to the |
101 | | - // format internally used: 0-based line numbers stored in ranges that are exclusive at the |
102 | | - // end. |
103 | | - let one_based_inclusive_to_zero_based_exclusive_range = || -> Result<Range<u32>, Error> { |
104 | | - if let Some(range) = range { |
105 | | - if range.start == 0 { |
106 | | - return Err(Error::InvalidLineRange); |
107 | | - } |
108 | | - let start = range.start - 1; |
109 | | - let end = range.end; |
110 | | - if start >= num_lines_in_blamed || end > num_lines_in_blamed || start == end { |
111 | | - return Err(Error::InvalidLineRange); |
112 | | - } |
113 | | - Ok(start..end) |
114 | | - } else { |
115 | | - Ok(0..num_lines_in_blamed) |
116 | | - } |
117 | | - }; |
118 | | - |
119 | | - let range_in_blamed_file = one_based_inclusive_to_zero_based_exclusive_range()?; |
120 | | - |
| 100 | + let range_in_blamed_file = one_based_inclusive_to_zero_based_exclusive_range(range, num_lines_in_blamed)?; |
121 | 101 | let mut hunks_to_blame = vec![UnblamedHunk { |
122 | 102 | range_in_blamed_file: range_in_blamed_file.clone(), |
123 | 103 | suspects: [(suspect, range_in_blamed_file)].into(), |
@@ -283,6 +263,25 @@ where |
283 | 263 | }) |
284 | 264 | } |
285 | 265 |
|
| 266 | +/// This function assumes that `range` has 1-based inclusive line numbers and converts it to the |
| 267 | +/// format internally used: 0-based line numbers stored in ranges that are exclusive at the |
| 268 | +/// end. |
| 269 | +fn one_based_inclusive_to_zero_based_exclusive_range( |
| 270 | + range: Option<Range<u32>>, |
| 271 | + max_lines: u32, |
| 272 | +) -> Result<Range<u32>, Error> { |
| 273 | + let Some(range) = range else { return Ok(0..max_lines) }; |
| 274 | + if range.start == 0 { |
| 275 | + return Err(Error::InvalidLineRange); |
| 276 | + } |
| 277 | + let start = range.start - 1; |
| 278 | + let end = range.end; |
| 279 | + if start >= max_lines || end > max_lines || start == end { |
| 280 | + return Err(Error::InvalidLineRange); |
| 281 | + } |
| 282 | + Ok(start..end) |
| 283 | +} |
| 284 | + |
286 | 285 | /// Pass ownership of each unblamed hunk of `from` to `to`. |
287 | 286 | /// |
288 | 287 | /// This happens when `from` didn't actually change anything in the blamed file. |
|
0 commit comments