Repository

Struct Repository 

Source
pub struct Repository { /* private fields */ }
Expand description

Represents the state associated with a Git repository.

Many other types in this crate are derived from methods in this struct.

Implementations§

Source§

impl Repository

Source

pub fn open(repo_uri: impl AsRef<Path>) -> Result<Self, Error>

Open a git repository given its exact URI.

§Errors
Examples found in repository?
examples/diff.rs (line 33)
32fn init_repository_or_exit(path_to_repo: &str) -> Repository {
33    match Repository::open(path_to_repo) {
34        Ok(repo) => repo,
35        Err(e) => {
36            println!("Failed to create repository: {e:?}");
37            std::process::exit(1);
38        }
39    }
40}
Source

pub fn discover(repo_uri: impl AsRef<Path>) -> Result<Self, Error>

Attempt to open a git repository at or above repo_uri in the file system.

Examples found in repository?
examples/browsing.rs (line 24)
16fn main() {
17    let repo_path = match env::args().nth(1) {
18        Some(path) => path,
19        None => {
20            print_usage();
21            return;
22        }
23    };
24    let repo = Repository::discover(repo_path).unwrap();
25    let now = Instant::now();
26    let head = repo.head().unwrap();
27    let root = repo.root_dir(head).unwrap();
28    print_directory(&root, &repo, 0);
29
30    let elapsed_millis = now.elapsed().as_millis();
31    println!("browse with print: {elapsed_millis} ms");
32}
Source

pub fn which_namespace(&self) -> Result<Option<Namespace>, Error>

What is the current namespace we’re browsing in.

Source

pub fn switch_namespace(&self, namespace: &RefString) -> Result<(), Error>

Switch to a namespace

Source

pub fn with_namespace<T, F>( &self, namespace: &RefString, f: F, ) -> Result<T, Error>
where F: FnOnce() -> Result<T, Error>,

Source

pub fn branches<'a, G>(&'a self, pattern: G) -> Result<Branches<'a>, Error>
where G: Into<Glob<Branch>>,

Returns an iterator of branches that match pattern.

Source

pub fn branch_names<'a, G>( &'a self, filter: G, ) -> Result<BranchNames<'a>, Error>
where G: Into<Glob<Branch>>,

Lists branch names with filter.

Source

pub fn tags<'a>(&'a self, pattern: &Glob<Tag>) -> Result<Tags<'a>, Error>

Returns an iterator of tags that match pattern.

Source

pub fn tag_names<'a>( &'a self, filter: &Glob<Tag>, ) -> Result<TagNames<'a>, Error>

Lists tag names in the local RefScope.

Source

pub fn categories<'a>( &'a self, pattern: &Glob<Qualified<'_>>, ) -> Result<Categories<'a>, Error>

Source

pub fn namespaces(&self, pattern: &Glob<Namespace>) -> Result<Namespaces, Error>

Returns an iterator of namespaces that match pattern.

Source

pub fn diff( &self, from: impl Revision, to: impl Revision, ) -> Result<Diff, Error>

Get the Diff between two commits.

Examples found in repository?
examples/diff.rs (line 18)
8fn main() {
9    let options = get_options_or_exit();
10    let repo = init_repository_or_exit(&options.path_to_repo);
11    let head_oid = match options.head_revision {
12        HeadRevision::Head => repo.head().unwrap(),
13        HeadRevision::Commit(id) => Oid::from_str(&id).unwrap(),
14    };
15    let base_oid = Oid::from_str(&options.base_revision).unwrap();
16    let now = Instant::now();
17    let elapsed_nanos = now.elapsed().as_nanos();
18    let diff = repo.diff(base_oid, head_oid).unwrap();
19    print_diff_summary(&diff, elapsed_nanos);
20}
Source

pub fn diff_commit(&self, commit: impl ToCommit) -> Result<Diff, Error>

Get the Diff of a commit.

If the commit has a parent, then it the diff will be a comparison between itself and that parent. Otherwise, the left hand side of the diff will pass nothing.

Source

pub fn diff_file<P: AsRef<Path>, R: Revision>( &self, path: &P, from: R, to: R, ) -> Result<FileDiff, Error>

Get the FileDiff between two revisions for a file at path.

If path is only a directory name, not a file, returns a FileDiff for any file under path.

Source

pub fn oid(&self, oid: &str) -> Result<Oid, Error>

Parse an Oid from the given string.

Source

pub fn root_dir<C: ToCommit>(&self, commit: C) -> Result<Directory, Error>

Returns a top level Directory without nested sub-directories.

To visit inside any nested sub-directories, call directory.get(&repo) on the sub-directory.

Examples found in repository?
examples/browsing.rs (line 27)
16fn main() {
17    let repo_path = match env::args().nth(1) {
18        Some(path) => path,
19        None => {
20            print_usage();
21            return;
22        }
23    };
24    let repo = Repository::discover(repo_path).unwrap();
25    let now = Instant::now();
26    let head = repo.head().unwrap();
27    let root = repo.root_dir(head).unwrap();
28    print_directory(&root, &repo, 0);
29
30    let elapsed_millis = now.elapsed().as_millis();
31    println!("browse with print: {elapsed_millis} ms");
32}
Source

pub fn directory<C: ToCommit, P: AsRef<Path>>( &self, commit: C, path: &P, ) -> Result<Directory, Error>

Returns a Directory for path in commit.

Source

pub fn file<C: ToCommit, P: AsRef<Path>>( &self, commit: C, path: &P, ) -> Result<File, Error>

Returns a File for path in commit.

Source

pub fn tree<C: ToCommit, P: AsRef<Path>>( &self, commit: C, path: &P, ) -> Result<Tree, Error>

Returns a Tree for path in commit.

Source

pub fn blob<'a, C: ToCommit, P: AsRef<Path>>( &'a self, commit: C, path: &P, ) -> Result<Blob<BlobRef<'a>>, Error>

Returns a Blob for path in commit.

Source

pub fn blob_ref(&self, oid: Oid) -> Result<BlobRef<'_>, Error>

Source

pub fn last_commit<P, C>( &self, path: &P, rev: C, ) -> Result<Option<Commit>, Error>
where P: AsRef<Path>, C: ToCommit,

Returns the last commit, if exists, for a path in the history of rev.

Source

pub fn commit<R: Revision>(&self, rev: R) -> Result<Commit, Error>

Returns a commit for rev, if it exists.

Source

pub fn stats(&self) -> Result<Stats, Error>

Gets the Stats of this repository starting from the HEAD (see Repository::head) of the repository.

Source

pub fn stats_from<R>(&self, rev: &R) -> Result<Stats, Error>
where R: Revision,

Gets the Stats of this repository starting from the given rev.

Source

pub fn get_commit_file<'a, P, R>( &'a self, rev: &R, path: &P, ) -> Result<FileContent<'a>, Error>
where P: AsRef<Path>, R: Revision,

Retrieves the file with path in this commit.

Source

pub fn head(&self) -> Result<Oid, Error>

Returns the Oid of the current HEAD.

Examples found in repository?
examples/browsing.rs (line 26)
16fn main() {
17    let repo_path = match env::args().nth(1) {
18        Some(path) => path,
19        None => {
20            print_usage();
21            return;
22        }
23    };
24    let repo = Repository::discover(repo_path).unwrap();
25    let now = Instant::now();
26    let head = repo.head().unwrap();
27    let root = repo.root_dir(head).unwrap();
28    print_directory(&root, &repo, 0);
29
30    let elapsed_millis = now.elapsed().as_millis();
31    println!("browse with print: {elapsed_millis} ms");
32}
More examples
Hide additional examples
examples/diff.rs (line 12)
8fn main() {
9    let options = get_options_or_exit();
10    let repo = init_repository_or_exit(&options.path_to_repo);
11    let head_oid = match options.head_revision {
12        HeadRevision::Head => repo.head().unwrap(),
13        HeadRevision::Commit(id) => Oid::from_str(&id).unwrap(),
14    };
15    let base_oid = Oid::from_str(&options.base_revision).unwrap();
16    let now = Instant::now();
17    let elapsed_nanos = now.elapsed().as_nanos();
18    let diff = repo.diff(base_oid, head_oid).unwrap();
19    print_diff_summary(&diff, elapsed_nanos);
20}
Source

pub fn extract_signature( &self, commit: impl ToCommit, field: Option<&str>, ) -> Result<Option<Signature>, Error>

Extract the signature from a commit

§Arguments

field - the name of the header field containing the signature block; pass None to extract the default ‘gpgsig’

Source

pub fn history<'a, C: ToCommit>(&'a self, head: C) -> Result<History<'a>, Error>

Returns the history with the head commit.

Source

pub fn revision_branches( &self, rev: impl Revision, glob: Glob<Branch>, ) -> Result<Vec<Branch>, Error>

Lists branches that are reachable from rev.

Trait Implementations§

Source§

impl Debug for Repository

Source§

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

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

impl From<Repository> for Repository

Source§

fn from(repo: Repository) -> Self

Converts to this type from the input type.

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, 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.