pub struct DiffIndex { /* private fields */ }Expand description
Parsed, command-neutral index of files and added lines in a unified diff.
Keys are the decoded paths of the +++ b/<path> headers of the diff,
without the quotes and the trailing tab that git can write. They live in
the namespace that produced the diff (for git diff, relative to the
repository toplevel). DiffIndex::base records the
directory those keys are relative to, so a finding’s absolute path can be
mapped into the same namespace before lookup. Without it, an analysis root
below the toplevel silently misses every key.
Implementations§
Source§impl DiffIndex
impl DiffIndex
Sourcepub fn from_unified_diff(diff: &str) -> DiffIndex
pub fn from_unified_diff(diff: &str) -> DiffIndex
Parse a unified diff into an index of changed files and added lines.
Indexes at most MAX_ADDED_LINES added lines; totals used by
DiffIndex::net_lines keep counting past the cap.
Sourcepub fn base_line_range(
&self,
path: &str,
start: u64,
end: u64,
) -> Option<(u64, u64)>
pub fn base_line_range( &self, path: &str, start: u64, end: u64, ) -> Option<(u64, u64)>
Map an inclusive head line range of path to the base tree. path
is a key of this diff.
Unchanged lines map exactly. An added line at the start of the range
maps to the last base line that its block removed, which is the
removed line next to the unchanged lines inside the range. An added
line at the end maps to the first removed line, for the same reason.
A block that replaces several lines thus does not widen the range to
base lines that the range does not reach. A range that starts and
ends in one block maps to every line that the block removed. A file
that the diff does not name maps unchanged. None when the range
holds no base line, when the diff adds path as a new file, or when
the index stopped at MAX_ADDED_LINES.
Sourcepub fn old_path_for(&self, head_path: &str) -> Option<&str>
pub fn old_path_for(&self, head_path: &str) -> Option<&str>
Pre-rename path for head_path, in the diff’s key namespace, when the
diff renamed it.
Sourcepub fn added_line_count(&self) -> usize
pub fn added_line_count(&self) -> usize
Number of indexed added lines, capped at MAX_ADDED_LINES.
Sourcepub fn filter_scope_size(&self) -> usize
pub fn filter_scope_size(&self) -> usize
The scope that a diff filter keeps: the indexed added lines, plus one
for each touched head-tree file that adds no line, such as a pure
rename. The filter keeps a finding on a whole touched file, so 0
means that the diff touches no file and adds no line. At the
MAX_ADDED_LINES cap it returns the cap, because the files after the
cap have no indexed line and would each count as a file without lines.
Sourcepub fn hunk_count(&self) -> usize
pub fn hunk_count(&self) -> usize
Number of parsed unified-diff hunk headers.
Sourcepub fn net_lines(&self) -> i64
pub fn net_lines(&self) -> i64
Total added lines minus total removed lines, independent of the added line indexing cap.
Sourcepub fn changes_path(&self, path: &str) -> bool
pub fn changes_path(&self, path: &str) -> bool
Whether this diff changes the path, including when the path is deleted.
Sourcepub fn changed_paths(&self) -> impl Iterator<Item = &str>
pub fn changed_paths(&self) -> impl Iterator<Item = &str>
Paths changed by the diff, using the old path for deleted files.
Sourcepub fn touches_file(&self, path: &str) -> bool
pub fn touches_file(&self, path: &str) -> bool
Whether this diff touches an analyzable path in the head tree.
Sourcepub fn range_overlaps_added(&self, path: &str, start: u64, end: u64) -> bool
pub fn range_overlaps_added(&self, path: &str, start: u64, end: u64) -> bool
Whether any added line in path falls inside the inclusive 1-based
start..=end range.
Sourcepub fn first_added_line_in_range(
&self,
path: &str,
start: u64,
end: u64,
) -> Option<u64>
pub fn first_added_line_in_range( &self, path: &str, start: u64, end: u64, ) -> Option<u64>
Lowest 1-based added line inside the inclusive start..=end range.
Returns None for an invalid range or when the diff adds no line in it.
Sourcepub fn line_is_added(&self, path: &str, line: u64) -> bool
pub fn line_is_added(&self, path: &str, line: u64) -> bool
Whether the 1-based line in path was added by this diff.
Sourcepub fn line_within_added_context(
&self,
path: &str,
line: u64,
radius: u64,
) -> bool
pub fn line_within_added_context( &self, path: &str, line: u64, radius: u64, ) -> bool
Whether line sits within radius lines of any added line in path.
Sourcepub fn added_lines_in(&self, path: &str) -> Option<&HashSet<u64, FxBuildHasher>>
pub fn added_lines_in(&self, path: &str) -> Option<&HashSet<u64, FxBuildHasher>>
Set of 1-based added line numbers for path, when the diff added any.
Sourcepub fn with_base(self, base: impl Into<PathBuf>) -> DiffIndex
pub fn with_base(self, base: impl Into<PathBuf>) -> DiffIndex
Declare the directory this diff’s paths are relative to (the git
toplevel for git diff output).
Sourcepub fn with_root_offset(self, offset: impl Into<String>) -> DiffIndex
pub fn with_root_offset(self, offset: impl Into<String>) -> DiffIndex
Declare where the analysis root sits below DiffIndex::base, as a
forward-slashed relative path (empty when they are the same directory).
Findings are addressed relative to the analysis root; this diff’s keys are relative to its base. Everything that looks a finding up in this index has to cross that gap, so the index carries the offset rather than making each caller rediscover it.
Sourcepub fn root_offset(&self) -> &str
pub fn root_offset(&self) -> &str
Relative path of the analysis root below DiffIndex::base; empty
when they coincide.
Sourcepub fn key_for_root_relative<'a>(&self, rel: &'a str) -> Cow<'a, str>
pub fn key_for_root_relative<'a>(&self, rel: &'a str) -> Cow<'a, str>
Lift an analysis-root-relative path into this diff’s key namespace.
Sourcepub fn root_relative_from_key<'a>(&self, key: &'a str) -> Option<Cow<'a, str>>
pub fn root_relative_from_key<'a>(&self, key: &'a str) -> Option<Cow<'a, str>>
Lower one of this diff’s keys back to an analysis-root-relative path.
None when the key names a file outside the analysis root.
Sourcepub fn old_path_for_root_relative<'a>(
&'a self,
rel: &str,
) -> Option<Cow<'a, str>>
pub fn old_path_for_root_relative<'a>( &'a self, rel: &str, ) -> Option<Cow<'a, str>>
The pre-rename path of an analysis-root-relative path, itself analysis-root-relative. Crosses into the diff’s key namespace and back, so a monorepo package below the diff’s base resolves its renames.
Sourcepub fn base(&self) -> Option<&Path>
pub fn base(&self) -> Option<&Path>
Directory this diff’s keys are relative to, when declared via
DiffIndex::with_base.
Sourcepub fn touched_files(&self) -> impl Iterator<Item = &str>
pub fn touched_files(&self) -> impl Iterator<Item = &str>
Analyzable head-tree paths this diff touches, in the diff’s key namespace.
Sourcepub fn key_for(&self, path: &Path, fallback_root: &Path) -> Option<String>
pub fn key_for(&self, path: &Path, fallback_root: &Path) -> Option<String>
Map a finding’s path into this diff’s key namespace.
Relativizes against DiffIndex::base when one was declared, else
against fallback_root. When base == fallback_root (the analysis
root is the repository toplevel) both agree, so behavior is unchanged.
Trait Implementations§
Auto Trait Implementations§
impl Forget for DiffIndex
impl Freeze for DiffIndex
impl Move for DiffIndex
impl RefUnwindSafe for DiffIndex
impl Send for DiffIndex
impl Sync for DiffIndex
impl Unpin for DiffIndex
impl UnsafeUnpin for DiffIndex
impl UnwindSafe for DiffIndex
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more