pub struct HunkPatch {
pub header: Vec<String>,
pub hunk: Vec<String>,
pub header_line: usize,
pub end_line: usize,
}Expand description
One hunk, plus the file header needed to apply it on its own.
Fields§
§header: Vec<String>Verbatim file-header lines: diff --git through +++.
hunk: Vec<String>The @@ line and its body, verbatim.
header_line: usizeBuffer line of the @@ header, for cursor restoration.
end_line: usizeBuffer line one past the hunk’s last body line.
Implementations§
Source§impl HunkPatch
impl HunkPatch
Sourcepub fn display_location(&self) -> String
pub fn display_location(&self) -> String
path:line naming this hunk’s position in the file — the
@@ header’s new-side start, not a buffer row. Prompts and
echoes use it, so Discard hunk at src/main.rs:42? points at
something the user can find after the buffer is rebuilt.
Falls back to the path alone, then to "hunk", so a header
this cannot parse still yields a sentence.
Sourcepub fn file_path(&self) -> Option<&str>
pub fn file_path(&self) -> Option<&str>
The file this hunk belongs to, for finding it again after the buffer is rebuilt (MG.18d).
Prefers the +++ b/ side and falls back to --- a/, so a
deletion — whose +++ is /dev/null — still names its file.
Self::display_path deliberately does not: a prompt saying
“discard hunk at /dev/null” would be worse than one that omits
the path, while a cursor restore that skipped deletions would
silently lose the user’s place on exactly the rows that are
hardest to find again.
Sourcepub fn display_path(&self) -> Option<&str>
pub fn display_path(&self) -> Option<&str>
The path from the +++ b/… line, for prompts and messages.
None for a deletion (+++ /dev/null) or a malformed header.
Display only — Self::to_patch never consults it, so a path
this cannot parse still stages correctly.
Source§impl HunkPatch
impl HunkPatch
Sourcepub fn restrict_to_rows(
&self,
selected: RangeInclusive<usize>,
direction: ApplyDirection,
) -> Option<HunkPatch>
pub fn restrict_to_rows( &self, selected: RangeInclusive<usize>, direction: ApplyDirection, ) -> Option<HunkPatch>
MG.18e: rewrite this hunk to carry only the changes on the buffer
rows in selected.
Region staging is not “a smaller hunk” — the body has to be rewritten, because a patch must still describe a complete transformation of the region it covers:
| Line | Selected | Unselected (Forward) | Unselected (Reverse) |
|---|---|---|---|
+added | stays + | dropped | becomes context |
-removed | stays - | becomes context | dropped |
| context | context | context | context |
The asymmetry is not a convention, it is what the target
contains. Applying forward, the target holds the old side: an
unselected + is not there and must not appear at all, while an
unselected - is there and survives — i.e. context. Reversed,
the target holds the new side and the roles swap exactly.
Both counts are recounted from the rewritten body; git apply
validates them against it and rejects the patch outright if they
disagree (“corrupt patch”). The two start lines are kept
verbatim: whichever side the target matches is preserved
line-for-line by the rules above, so its start is still correct,
and the other side’s start is not something git checks.
None when the selection contains no +/- line at all — a
body of pure context is a patch that does nothing, and telling
the user “nothing to stage there” beats handing git a no-op.
Trait Implementations§
impl Eq for HunkPatch
impl StructuralPartialEq for HunkPatch
Auto Trait Implementations§
impl Freeze for HunkPatch
impl RefUnwindSafe for HunkPatch
impl Send for HunkPatch
impl Sync for HunkPatch
impl Unpin for HunkPatch
impl UnsafeUnpin for HunkPatch
impl UnwindSafe for HunkPatch
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> 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