Struct git_repository::Repository
source · [−]Expand description
A thread-local handle to interact with a repository from a single thread.
It is Send
but not Sync
- for the latter you can convert it to_sync()
.
Note that it clones itself so that it is empty, requiring the user to configure each clone separately, specifically
and explicitly. This is to have the fastest-possible default configuration available by default, but allow
those who experiment with workloads to get speed boosts of 2x or more.
Fields
refs: RefStore
A ref store with shared ownership (or the equivalent of it).
objects: OdbHandle
A way to access objects.
Implementations
sourceimpl Repository
impl Repository
Configure how caches are used to speed up various git repository operations
sourcepub fn object_cache_size(&mut self, bytes: impl Into<Option<usize>>)
pub fn object_cache_size(&mut self, bytes: impl Into<Option<usize>>)
Sets the amount of space used at most for caching most recently accessed fully decoded objects, to Some(bytes)
,
or None
to deactivate it entirely.
Note that it is unset by default but can be enabled once there is time for performance optimization. Well-chosen cache sizes can improve performance particularly if objects are accessed multiple times in a row. The cache is configured to grow gradually.
Note that a cache on application level should be considered as well as the best object access is not doing one.
sourcepub fn apply_environment(self) -> Self
pub fn apply_environment(self) -> Self
Read well-known environment variables related to caches and apply them to this instance, but not to clones of it - each needs their own configuration.
Note that environment configuration never fails due to invalid environment values, but it should be used with caution as it could be used to cause high memory consumption.
Use the GITOXIDE_DISABLE_PACK_CACHE
environment variable to turn off any pack cache, which can be beneficial when it’s known that
the cache efficiency is low. Use GITOXIDE_PACK_CACHE_MEMORY=512MB
to use up to 512MB of RAM for the pack delta base
cache. If none of these are set, the default cache is fast enough to nearly never cause a (marginal) slow-down while providing
some gains most of the time. Note that the value given is per-thread.
Use the GITOXIDE_OBJECT_CACHE_MEMORY=16mb
to set the given amount of memory to store full objects, on a per-thread basis.
sourceimpl Repository
impl Repository
Configuration
sourcepub fn config_snapshot(&self) -> Snapshot<'_>
pub fn config_snapshot(&self) -> Snapshot<'_>
Return Return a snapshot of the configuration as seen upon opening the repository.
sourcepub fn open_options(&self) -> &Options
pub fn open_options(&self) -> &Options
The options used to open the repository.
sourcepub fn object_hash(&self) -> Kind
pub fn object_hash(&self) -> Kind
The kind of object hash the repository is configured to use.
sourceimpl Repository
impl Repository
Identity handling.
sourcepub fn user_default(&self) -> SignatureRef<'_>
pub fn user_default(&self) -> SignatureRef<'_>
sourcepub fn committer(&self) -> Option<SignatureRef<'_>>
pub fn committer(&self) -> Option<SignatureRef<'_>>
Return the committer as configured by this repository, which is determined by…
- …the git configuration
committer.name|email
… - …the
GIT_COMMITTER_(NAME|EMAIL|DATE)
environment variables… - …the configuration for
user.name|email
as fallback…
…and in that order, or None
if there was nothing configured. In that case, one may use the
committer_or_default()
method.
Note
The values are cached when the repository is instantiated.
sourcepub fn committer_or_default(&self) -> SignatureRef<'_>
pub fn committer_or_default(&self) -> SignatureRef<'_>
Like committer()
, but may use a default value in case nothing is configured.
Return the author as configured by this repository, which is determined by…
- …the git configuration
author.name|email
… - …the
GIT_AUTHOR_(NAME|EMAIL|DATE)
environment variables… - …the configuration for
user.name|email
as fallback…
…and in that order, or None
if there was nothing configured. In that case, one may use the
author_or_default()
method.
Note
The values are cached when the repository is instantiated.
Like author()
, but may use a default value in case nothing is configured.
sourceimpl Repository
impl Repository
sourcepub fn into_sync(self) -> ThreadSafeRepository
pub fn into_sync(self) -> ThreadSafeRepository
Convert this instance into a ThreadSafeRepository
by dropping all thread-local data.
sourceimpl Repository
impl Repository
sourcepub fn common_dir(&self) -> &Path
pub fn common_dir(&self) -> &Path
Returns the main git repository if this is a repository on a linked work-tree, or the git_dir
itself.
sourcepub fn path(&self) -> &Path
pub fn path(&self) -> &Path
The path to the .git
directory itself, or equivalent if this is a bare repository.
sourcepub fn work_dir(&self) -> Option<&Path>
pub fn work_dir(&self) -> Option<&Path>
Return the work tree containing all checked out files, if there is one.
sourcepub fn install_dir(&self) -> Result<PathBuf>
pub fn install_dir(&self) -> Result<PathBuf>
The directory of the binary path of the current process.
sourcepub fn prefix(&self) -> Option<Result<PathBuf>>
pub fn prefix(&self) -> Option<Result<PathBuf>>
Returns the relative path which is the components between the working tree and the current working dir (CWD).
Note that there may be None
if there is no work tree, even though the PathBuf
will be empty
if the CWD is at the root of the work tree.
sourcepub fn git_dir(&self) -> &Path
pub fn git_dir(&self) -> &Path
Return the path to the repository itself, containing objects, references, configuration, and more.
Synonymous to path()
.
sourcepub fn git_dir_trust(&self) -> Trust
pub fn git_dir_trust(&self) -> Trust
The trust we place in the git-dir, with lower amounts of trust causing access to configuration to be limited.
sourceimpl Repository
impl Repository
Methods related to object creation.
sourcepub fn rev_parse(&self, spec: impl AsRef<str>) -> Result<Id<'_>, Error>
pub fn rev_parse(&self, spec: impl AsRef<str>) -> Result<Id<'_>, Error>
Parse a revision specification and turn it into the full id to the object it describes, similar to git rev-parse
.
NOTE that currently this only parses full hex names.
sourcepub fn find_object(
&self,
id: impl Into<ObjectId>
) -> Result<Object<'_>, Error<Error>>
pub fn find_object(
&self,
id: impl Into<ObjectId>
) -> Result<Object<'_>, Error<Error>>
Find the object with id
in the object database or return an error if it could not be found.
There are various legitimate reasons for an object to not be present, which is why
try_find_object(…)
might be preferable instead.
Important
As a shared buffer is written to back the object data, the returned ObjectRef
will prevent other
find_object()
operations from succeeding while alive.
To bypass this limit, clone this sync::Handle
instance.
Performance Note
In order to get the kind of the object, is must be fully decoded from storage if it is packed with deltas. Loose object could be partially decoded, even though that’s not implemented.
sourcepub fn try_find_object(
&self,
id: impl Into<ObjectId>
) -> Result<Option<Object<'_>>, Error>
pub fn try_find_object(
&self,
id: impl Into<ObjectId>
) -> Result<Option<Object<'_>>, Error>
Try to find the object with id
or return None
it it wasn’t found.
Important
As a shared buffer is written to back the object data, the returned ObjectRef
will prevent other
try_find_object()
operations from succeeding while alive.
To bypass this limit, clone this sync::Handle
instance.
sourcepub fn write_object(&self, object: impl WriteTo) -> Result<Id<'_>, Error>
pub fn write_object(&self, object: impl WriteTo) -> Result<Id<'_>, Error>
Write the given object into the object database and return its object id.
sourcepub fn tag(
&self,
name: impl AsRef<str>,
target: impl AsRef<oid>,
target_kind: Kind,
tagger: Option<SignatureRef<'_>>,
message: impl AsRef<str>,
constraint: PreviousValue
) -> Result<Reference<'_>, Error>
pub fn tag(
&self,
name: impl AsRef<str>,
target: impl AsRef<oid>,
target_kind: Kind,
tagger: Option<SignatureRef<'_>>,
message: impl AsRef<str>,
constraint: PreviousValue
) -> Result<Reference<'_>, Error>
Create a tag reference named name
(without refs/tags/
prefix) pointing to a newly created tag object
which in turn points to target
and return the newly created reference.
It will be created with constraint
which is most commonly to only create it
or to force overwriting a possibly existing tag.
sourcepub fn commit<Name, E>(
&self,
reference: Name,
author: SignatureRef<'_>,
committer: SignatureRef<'_>,
message: impl AsRef<str>,
tree: impl Into<ObjectId>,
parents: impl IntoIterator<Item = impl Into<ObjectId>>
) -> Result<Id<'_>, Error> where
Name: TryInto<FullName, Error = E>,
Error: From<E>,
pub fn commit<Name, E>(
&self,
reference: Name,
author: SignatureRef<'_>,
committer: SignatureRef<'_>,
message: impl AsRef<str>,
tree: impl Into<ObjectId>,
parents: impl IntoIterator<Item = impl Into<ObjectId>>
) -> Result<Id<'_>, Error> where
Name: TryInto<FullName, Error = E>,
Error: From<E>,
Create a new commit object with author
, committer
and message
referring to tree
with parents
, and point reference
to it. The commit is written without message encoding field, which can be assumed to be UTF-8.
reference
will be created if it doesn’t exist, and can be "HEAD"
to automatically write-through to the symbolic reference
that HEAD
points to if it is not detached. For this reason, detached head states cannot be created unless the HEAD
is detached
already. The reflog will be written as canonical git would do, like <operation> (<detail>): <summary>
.
The first parent id in parents
is expected to be the current target of reference
and the operation will fail if it is not.
If there is no parent, the reference
is expected to not exist yet.
The method fails immediately if a reference
lock can’t be acquired.
sourceimpl Repository
impl Repository
Obtain and alter references comfortably
sourcepub fn tag_reference(
&self,
name: impl AsRef<str>,
target: impl Into<ObjectId>,
constraint: PreviousValue
) -> Result<Reference<'_>, Error>
pub fn tag_reference(
&self,
name: impl AsRef<str>,
target: impl Into<ObjectId>,
constraint: PreviousValue
) -> Result<Reference<'_>, Error>
Create a lightweight tag with given name
(and without refs/tags/
prefix) pointing to the given target
, and return it as reference.
It will be created with constraint
which is most commonly to only create it
or to force overwriting a possibly existing tag.
sourcepub fn namespace(&self) -> Option<&Namespace>
pub fn namespace(&self) -> Option<&Namespace>
Returns the currently set namespace for references, or None
if it is not set.
Namespaces allow to partition references, and is configured per Easy
.
sourcepub fn clear_namespace(&mut self) -> Option<Namespace>
pub fn clear_namespace(&mut self) -> Option<Namespace>
Remove the currently set reference namespace and return it, affecting only this Easy
.
sourcepub fn set_namespace<'a, Name, E>(
&mut self,
namespace: Name
) -> Result<Option<Namespace>, Error> where
Name: TryInto<&'a PartialNameRef, Error = E>,
Error: From<E>,
pub fn set_namespace<'a, Name, E>(
&mut self,
namespace: Name
) -> Result<Option<Namespace>, Error> where
Name: TryInto<&'a PartialNameRef, Error = E>,
Error: From<E>,
Set the reference namespace to the given value, like "foo"
or "foo/bar"
.
Note that this value is shared across all Easy…
instances as the value is stored in the shared Repository
.
sourcepub fn reference<Name, E>(
&self,
name: Name,
target: impl Into<ObjectId>,
constraint: PreviousValue,
log_message: impl Into<BString>
) -> Result<Reference<'_>, Error> where
Name: TryInto<FullName, Error = E>,
Error: From<E>,
pub fn reference<Name, E>(
&self,
name: Name,
target: impl Into<ObjectId>,
constraint: PreviousValue,
log_message: impl Into<BString>
) -> Result<Reference<'_>, Error> where
Name: TryInto<FullName, Error = E>,
Error: From<E>,
Create a new reference with name
, like refs/heads/branch
, pointing to target
, adhering to constraint
during creation and writing log_message
into the reflog. Note that a ref-log will be written even if log_message
is empty.
The newly created Reference is returned.
sourcepub fn edit_reference(
&self,
edit: RefEdit,
lock_mode: Fail,
log_committer: SignatureRef<'_>
) -> Result<Vec<RefEdit>, Error>
pub fn edit_reference(
&self,
edit: RefEdit,
lock_mode: Fail,
log_committer: SignatureRef<'_>
) -> Result<Vec<RefEdit>, Error>
Edit a single reference as described in edit
, handle locks via lock_mode
and write reference logs as log_committer
.
One or more RefEdit
s are returned - symbolic reference splits can cause more edits to be performed. All edits have the previous
reference values set to the ones encountered at rest after acquiring the respective reference’s lock.
sourcepub fn edit_references(
&self,
edits: impl IntoIterator<Item = RefEdit>,
lock_mode: Fail,
log_committer: SignatureRef<'_>
) -> Result<Vec<RefEdit>, Error>
pub fn edit_references(
&self,
edits: impl IntoIterator<Item = RefEdit>,
lock_mode: Fail,
log_committer: SignatureRef<'_>
) -> Result<Vec<RefEdit>, Error>
Edit one or more references as described by their edits
, with lock_mode
deciding on how to handle competing
transactions. log_committer
is the name appearing in reference logs.
Returns all reference edits, which might be more than where provided due the splitting of symbolic references, and whose previous (old) values are the ones seen on in storage after the reference was locked.
sourcepub fn head(&self) -> Result<Head<'_>, Error>
pub fn head(&self) -> Result<Head<'_>, Error>
Return the repository head, an abstraction to help dealing with the HEAD
reference.
The HEAD
reference can be in various states, for more information, the documentation of Head
.
sourcepub fn head_id(&self) -> Result<Id<'_>, Error>
pub fn head_id(&self) -> Result<Id<'_>, Error>
Resolve the HEAD
reference, follow and peel its target and obtain its object id.
Note that this may fail for various reasons, most notably because the repository is freshly initialized and doesn’t have any commits yet.
Also note that the returned id is likely to point to a commit, but could also point to a tree or blob. It won’t, however, point to a tag as these are always peeled.
sourcepub fn head_name(&self) -> Result<Option<FullName>, Error>
pub fn head_name(&self) -> Result<Option<FullName>, Error>
Return the name to the symbolic reference HEAD
points to, or None
if the head is detached.
sourcepub fn head_commit(&self) -> Result<Commit<'_>, Error>
pub fn head_commit(&self) -> Result<Commit<'_>, Error>
Return the commit object the HEAD
reference currently points to after peeling it fully.
Note that this may fail for various reasons, most notably because the repository is freshly initialized and doesn’t have any commits yet. It could also fail if the head does not point to a commit.
sourcepub fn find_reference<'a, Name, E>(
&self,
name: Name
) -> Result<Reference<'_>, Error> where
Name: TryInto<&'a PartialNameRef, Error = E>,
Error: From<E>,
pub fn find_reference<'a, Name, E>(
&self,
name: Name
) -> Result<Reference<'_>, Error> where
Name: TryInto<&'a PartialNameRef, Error = E>,
Error: From<E>,
Find the reference with the given partial or full name
, like main
, HEAD
, heads/branch
or origin/other
,
or return an error if it wasn’t found.
Consider try_find_reference(…)
if the reference might not exist
without that being considered an error.
sourcepub fn references(&self) -> Result<Platform<'_>, Error>
pub fn references(&self) -> Result<Platform<'_>, Error>
sourcepub fn try_find_reference<'a, Name, E>(
&self,
name: Name
) -> Result<Option<Reference<'_>>, Error> where
Name: TryInto<&'a PartialNameRef, Error = E>,
Error: From<E>,
pub fn try_find_reference<'a, Name, E>(
&self,
name: Name
) -> Result<Option<Reference<'_>>, Error> where
Name: TryInto<&'a PartialNameRef, Error = E>,
Error: From<E>,
Try to find the reference named name
, like main
, heads/branch
, HEAD
or origin/other
, and return it.
Otherwise return None
if the reference wasn’t found.
If the reference is expected to exist, use find_reference()
.
sourceimpl Repository
impl Repository
sourcepub fn remote_ref(
&self,
short_branch_name: &str
) -> Option<Result<Cow<'_, FullNameRef>, ValidateNameError>>
pub fn remote_ref(
&self,
short_branch_name: &str
) -> Option<Result<Cow<'_, FullNameRef>, ValidateNameError>>
Returns a reference to the remote associated with the given short_branch_name
, typically main
instead of refs/heads/main
.
Returns None
if the remote reference was not found.
May return an error if the reference is invalid.
sourcepub fn branch_remote_name(
&self,
short_branch_name: &str
) -> Option<Cow<'_, BStr>>
pub fn branch_remote_name(
&self,
short_branch_name: &str
) -> Option<Cow<'_, BStr>>
Returns the name of the remote associated with the given short_branch_name
, typically main
instead of refs/heads/main
.
In some cases, the returned name will be an URL.
Returns None
if the remote was not found.
sourceimpl Repository
impl Repository
sourcepub fn load_index(&self) -> Option<Result<File, Error>>
pub fn load_index(&self) -> Option<Result<File, Error>>
Load the index file of this repository’s workspace, if present.
Note that it is loaded into memory each time this method is called, but also is independent of the workspace.
sourcepub fn load_mailmap(&self) -> Snapshot
pub fn load_mailmap(&self) -> Snapshot
Similar to load_mailmap_into()
, but ignores all errors and returns at worst
an empty mailmap, e.g. if there is no mailmap or if there were errors loading them.
This represents typical usage within git, which also works with what’s there without considering a populated mailmap a reason to abort an operation, considering it optional.
sourcepub fn load_mailmap_into(&self, target: &mut Snapshot) -> Result<(), Error>
pub fn load_mailmap_into(&self, target: &mut Snapshot) -> Result<(), Error>
Try to merge mailmaps from the following locations into target
:
- read the
.mailmap
file without following symlinks from the working tree, if present - OR read
HEAD:.mailmap
if this repository is bare (i.e. has no working tree), if themailmap.blob
is not set. - read the mailmap as configured in
mailmap.blob
, if set. - read the file as configured by
mailmap.file
, following symlinks, if set.
Only the first error will be reported, and as many source mailmaps will be merged into target
as possible.
Parsing errors will be ignored.
sourceimpl Repository
impl Repository
sourcepub fn state(&self) -> Option<InProgress>
pub fn state(&self) -> Option<InProgress>
Returns the status of an in progress operation on a repository or None
if no operation is currently in progress.
Note to be confused with the repositories ‘status’.
sourceimpl Repository
impl Repository
Worktree iteration
sourcepub fn worktrees(&self) -> Result<Vec<Proxy<'_>>>
pub fn worktrees(&self) -> Result<Vec<Proxy<'_>>>
Return a list of all linked worktrees sorted by private git dir path as a lightweight proxy.
Note that these need additional processing to become usable, but provide a first glimpse a typical worktree information.
sourcepub fn worktree_repos(&self) -> !
pub fn worktree_repos(&self) -> !
Iterate all linked worktrees in sort order and resolve them, ignoring those without an accessible work tree, into repositories
whose worktree()
is the worktree currently being iterated.
Note that for convenience all io errors are squelched so if there is a chance for IO errors during
traversal of an owned directory, better use list()
directly. The latter allows to resolve repositories
even if the worktree checkout isn’t accessible.
sourceimpl Repository
impl Repository
Interact with individual worktrees and their information.
sourcepub fn main_repo(&self) -> Result<Repository, Error>
pub fn main_repo(&self) -> Result<Repository, Error>
Return the repository owning the main worktree.
Note that it might be the one that is currently open if this repository dosn’t point to a linked worktree. Also note that the main repo might be bare.
sourcepub fn worktree(&self) -> Option<Worktree<'_>>
pub fn worktree(&self) -> Option<Worktree<'_>>
Return the currently set worktree if there is one, acting as platform providing a validated worktree base path.
Note that there would be None
if this repository is bare
and the parent Repository
was instantiated without
registered worktree in the current working dir.
sourcepub fn is_bare(&self) -> bool
pub fn is_bare(&self) -> bool
Return true if this repository is bare, and has no main work tree.
This is not to be confused with the worktree()
worktree, which may exists if this instance
was opened in a worktree that was created separately.
sourcepub fn open_index(&self) -> Result<File, Error>
pub fn open_index(&self) -> Result<File, Error>
Open a new copy of the index file and decode it entirely.
It will use the index.threads
configuration key to learn how many threads to use.
Note that it may fail if there is no index.
impl Repository
Internal
Trait Implementations
sourceimpl Clone for Repository
impl Clone for Repository
sourceimpl Debug for Repository
impl Debug for Repository
sourceimpl From<&ThreadSafeRepository> for Repository
impl From<&ThreadSafeRepository> for Repository
sourcefn from(repo: &ThreadSafeRepository) -> Self
fn from(repo: &ThreadSafeRepository) -> Self
Converts to this type from the input type.
sourceimpl From<Repository> for ThreadSafeRepository
impl From<Repository> for ThreadSafeRepository
sourcefn from(r: Repository) -> Self
fn from(r: Repository) -> Self
Converts to this type from the input type.
sourceimpl From<ThreadSafeRepository> for Repository
impl From<ThreadSafeRepository> for Repository
sourcefn from(repo: ThreadSafeRepository) -> Self
fn from(repo: ThreadSafeRepository) -> Self
Converts to this type from the input type.
sourceimpl PartialEq<Repository> for Repository
impl PartialEq<Repository> for Repository
Auto Trait Implementations
impl !RefUnwindSafe for Repository
impl Send for Repository
impl !Sync for Repository
impl Unpin for Repository
impl !UnwindSafe for Repository
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more