pub struct GitStub { /* private fields */ }Expand description
Represents a git stub: a reference to a file at a specific commit.
A git stub is stored as a string in the format commit:path, and can be
used to retrieve file contents via git cat-file blob commit:path.
Construct via FromStr (parsing) or GitStub::new.
§Invariants
- The path is non-empty.
- The path uses forward slashes (backslashes are normalized on construction).
- Every path component is a normal file or directory name (no
..,., root/, or Windows prefixes).
§Examples
use git_stub::GitStub;
let git_stub: GitStub =
"0123456789abcdef0123456789abcdef01234567:openapi/api.json"
.parse()
.unwrap();
assert_eq!(git_stub.path().as_str(), "openapi/api.json");Implementations§
Source§impl GitStub
impl GitStub
Sourcepub fn new(
commit: GitCommitHash,
path: Utf8PathBuf,
) -> Result<Self, GitStubParseError>
pub fn new( commit: GitCommitHash, path: Utf8PathBuf, ) -> Result<Self, GitStubParseError>
Creates a new GitStub with the given commit hash and path.
The path is normalized: backslashes are converted to forward slashes.
Returns an error if:
- The path is empty.
- The path contains a newline character.
- Any path component is not a normal file or directory name (e.g.,
..,., root/, or a Windows prefix).
Sourcepub fn commit(&self) -> GitCommitHash
pub fn commit(&self) -> GitCommitHash
Returns the commit hash.
Sourcepub fn to_file_contents(&self) -> String
pub fn to_file_contents(&self) -> String
Returns the canonical file contents for this git stub.
The canonical format is commit:path\n where:
- The path uses forward slashes (even on Windows).
- The file ends with a single newline.
Sourcepub fn needs_rewrite(&self) -> bool
pub fn needs_rewrite(&self) -> bool
Returns whether the input used to construct this GitStub was not in
canonical form.
A Git stub needs rewriting if it doesn’t match the canonical format:
- Missing trailing newline.
- Contains backslashes in the path.
- Has extra whitespace.
Trait Implementations§
impl Eq for GitStub
Auto Trait Implementations§
impl Freeze for GitStub
impl RefUnwindSafe for GitStub
impl Send for GitStub
impl Sync for GitStub
impl Unpin for GitStub
impl UnsafeUnpin for GitStub
impl UnwindSafe for GitStub
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
Mutably borrows from an owned value. Read more