Skip to main content

HunkPatch

Struct HunkPatch 

Source
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: usize

Buffer line of the @@ header, for cursor restoration.

§end_line: usize

Buffer line one past the hunk’s last body line.

Implementations§

Source§

impl HunkPatch

Source

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.

Source

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.

Source

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

pub fn to_patch(&self) -> String

A standalone patch: the file header, then this hunk alone.

Always ends in a newline — git apply rejects a patch whose final line is unterminated.

Source§

impl HunkPatch

Source

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:

LineSelectedUnselected (Forward)Unselected (Reverse)
+addedstays +droppedbecomes context
-removedstays -becomes contextdropped
contextcontextcontextcontext

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§

Source§

impl Clone for HunkPatch

Source§

fn clone(&self) -> HunkPatch

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for HunkPatch

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for HunkPatch

Source§

impl PartialEq for HunkPatch

Source§

fn eq(&self, other: &HunkPatch) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for HunkPatch

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more