Skip to content

Commit 63ee0f9

Browse files
committed
Review and remove all TODOs where possible, update docs and comments
1 parent b7f1468 commit 63ee0f9

File tree

5 files changed

+168
-107
lines changed

5 files changed

+168
-107
lines changed

gitoxide-core/src/repository/blame.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ pub fn blame_file(
3030
fn write_blame_entries(mut out: impl std::io::Write, outcome: gix::blame::Outcome) -> Result<(), std::io::Error> {
3131
for (entry, lines_in_hunk) in outcome.entries_with_lines() {
3232
for ((actual_lno, source_lno), line) in entry
33-
.range_in_blamed_file
34-
.zip(entry.range_in_source_file)
33+
.range_in_blamed_file()
34+
.zip(entry.range_in_source_file())
3535
.zip(lines_in_hunk)
3636
{
3737
write!(

gix-blame/src/file/function.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ use crate::{BlameEntry, Error, Outcome, Statistics};
33
use gix_diff::blob::intern::TokenSource;
44
use gix_hash::ObjectId;
55
use gix_object::{bstr::BStr, FindExt};
6+
use std::num::NonZeroU32;
67
use std::ops::Range;
78

8-
// TODO: do not instantiate anything, get everything passed as argument.
99
/// Produce a list of consecutive [`BlameEntry`] instances to indicate in which commits the ranges of the file
1010
/// at `traverse[0]:<file_path>` originated in.
1111
///
@@ -142,8 +142,8 @@ where
142142
)?;
143143
let Some(modification) = changes_for_file_path else {
144144
if more_than_one_parent {
145-
// None of the changes affected the file we’re currently blaming. Pass blame
146-
// to parent.
145+
// None of the changes affected the file we’re currently blaming.
146+
// Copy blame to parent.
147147
for unblamed_hunk in &mut hunks_to_blame {
148148
unblamed_hunk.clone_blame(suspect, parent_id);
149149
}
@@ -159,8 +159,6 @@ where
159159
// Do nothing under the assumption that this always (or almost always)
160160
// implies that the file comes from a different parent, compared to which
161161
// it was modified, not added.
162-
//
163-
// TODO: I still have to figure out whether this is correct in all cases.
164162
} else {
165163
unblamed_to_out(&mut hunks_to_blame, &mut out, suspect);
166164
break;
@@ -191,25 +189,26 @@ where
191189

192190
// I don’t know yet whether it would make sense to use a data structure instead that preserves
193191
// order on insertion.
194-
out.sort_by(|a, b| a.range_in_blamed_file.start.cmp(&b.range_in_blamed_file.start));
192+
out.sort_by(|a, b| a.start_in_blamed_file.cmp(&b.start_in_blamed_file));
195193
Ok(Outcome {
196194
entries: coalesce_blame_entries(out),
197195
blob: blamed_file_blob,
198196
statistics: stats,
199197
})
200198
}
201199

202-
/// The blobs storing the blamed file in `entry` and `parent_entry` are identical which is why
203-
/// we can pass blame to the parent without further checks.
200+
/// Pass ownership of each unblamed hunk of `from` to `to`.
201+
///
202+
/// This happens when `from` didn't actually change anything in the blamed file.
204203
fn pass_blame_from_to(from: ObjectId, to: ObjectId, hunks_to_blame: &mut Vec<UnblamedHunk>) {
205204
for unblamed_hunk in hunks_to_blame {
206205
unblamed_hunk.pass_blame(from, to);
207206
}
208207
}
209208

209+
/// Convert each of the unblamed hunk in `hunks_to_blame` into a [`BlameEntry`], consuming them in the process.
210+
/// `suspect` is expected to be present in the suspect-map in each [`UnblamedHunk`].
210211
fn unblamed_to_out(hunks_to_blame: &mut Vec<UnblamedHunk>, out: &mut Vec<BlameEntry>, suspect: ObjectId) {
211-
// Every line that has not been blamed yet on a commit, is expected to have been
212-
// added when the file was added to the repository.
213212
out.extend(
214213
hunks_to_blame
215214
.drain(..)
@@ -234,14 +233,21 @@ fn coalesce_blame_entries(lines_blamed: Vec<BlameEntry>) -> Vec<BlameEntry> {
234233
let previous_entry = acc.last();
235234

236235
if let Some(previous_entry) = previous_entry {
236+
let previous_blamed_range = previous_entry.range_in_blamed_file();
237+
let current_blamed_range = entry.range_in_blamed_file();
238+
let previous_source_range = previous_entry.range_in_source_file();
239+
let current_source_range = entry.range_in_source_file();
237240
if previous_entry.commit_id == entry.commit_id
238-
&& previous_entry.range_in_blamed_file.end == entry.range_in_blamed_file.start
241+
&& previous_blamed_range.end == current_blamed_range.start
239242
// As of 2024-09-19, the check below only is in `git`, but not in `libgit2`.
240-
&& previous_entry.range_in_source_file.end == entry.range_in_source_file.start
243+
&& previous_source_range.end == current_source_range.start
241244
{
245+
// let combined_range =
242246
let coalesced_entry = BlameEntry {
243-
range_in_blamed_file: previous_entry.range_in_blamed_file.start..entry.range_in_blamed_file.end,
244-
range_in_source_file: previous_entry.range_in_source_file.start..entry.range_in_source_file.end,
247+
start_in_blamed_file: previous_blamed_range.start as u32,
248+
start_in_source_file: previous_source_range.start as u32,
249+
len: NonZeroU32::new((current_source_range.end - previous_source_range.start) as u32)
250+
.expect("BUG: hunks are never zero-sized"),
245251
commit_id: previous_entry.commit_id,
246252
};
247253

gix-blame/src/file/mod.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
//! A module with low-level types and functions.
2+
3+
use std::num::NonZeroU32;
24
use std::ops::Range;
35

46
use gix_hash::ObjectId;
@@ -67,7 +69,7 @@ fn process_change(
6769
// <----> (hunk)
6870
// <--> (unchanged)
6971

70-
(Some(hunk.clone()), None)
72+
(Some(hunk), None)
7173
} else {
7274
// <--> (hunk)
7375
// <----> (unchanged)
@@ -186,7 +188,7 @@ fn process_change(
186188
*offset_in_destination += added.end - added.start;
187189
*offset_in_destination -= number_of_lines_deleted;
188190

189-
(Some(hunk.clone()), None)
191+
(Some(hunk), None)
190192
} else if range_in_suspect.end <= added.start {
191193
// <--> (hunk)
192194
// <----> (added)
@@ -416,8 +418,9 @@ impl BlameEntry {
416418

417419
match offset {
418420
Offset::Added(added) => Self {
419-
range_in_blamed_file: (range_in_source_file.start + added)..(range_in_source_file.end + added),
420-
range_in_source_file,
421+
start_in_blamed_file: range_in_source_file.start + added,
422+
start_in_source_file: range_in_source_file.start,
423+
len: force_non_zero(range_in_source_file.len() as u32),
421424
commit_id,
422425
},
423426
Offset::Deleted(deleted) => {
@@ -427,8 +430,9 @@ impl BlameEntry {
427430
);
428431

429432
Self {
430-
range_in_blamed_file: (range_in_source_file.start - deleted)..(range_in_source_file.end - deleted),
431-
range_in_source_file,
433+
start_in_blamed_file: range_in_source_file.start - deleted,
434+
start_in_source_file: range_in_source_file.start,
435+
len: force_non_zero(range_in_source_file.len() as u32),
432436
commit_id,
433437
}
434438
}
@@ -443,12 +447,17 @@ impl BlameEntry {
443447
.expect("Private and only called when we now `commit_id` is in the suspect list");
444448

445449
Self {
446-
range_in_blamed_file: unblamed_hunk.range_in_blamed_file,
447-
range_in_source_file,
450+
start_in_blamed_file: unblamed_hunk.range_in_blamed_file.start,
451+
start_in_source_file: range_in_source_file.start,
452+
len: force_non_zero(range_in_source_file.len() as u32),
448453
commit_id,
449454
}
450455
}
451456
}
452457

458+
fn force_non_zero(n: u32) -> NonZeroU32 {
459+
NonZeroU32::new(n).expect("BUG: hunks are never empty")
460+
}
461+
453462
#[cfg(test)]
454463
mod tests;

0 commit comments

Comments
 (0)