Skip to main content

gix/
lib.rs

1//! This crate provides the [`Repository`] abstraction which serves as a hub into all the functionality of git.
2//!
3//! It's powerful and won't sacrifice performance while still increasing convenience compared to using the sub-crates
4//! individually. Sometimes it may hide complexity under the assumption that the performance difference doesn't matter
5//! for all but the fewest tools out there, which would be using the underlying crates directly or file an issue.
6//!
7//! ## Example
8//!
9//! This is merely an introduction, for more see the respective [`Repository`] methods.
10//! ```
11//! # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
12//! # mod doctest { include!(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/doctest.rs")); }
13//! # let repo_dir = doctest::basic_repo_dir()?;
14//! # let repo = doctest::open_repo(repo_dir)?;
15//! let head = repo.head_commit()?;
16//!
17//! assert_eq!(repo.head_name()?.expect("born").shorten(), "main");
18//! assert_eq!(head.decode()?.message, "c2\n");
19//! assert_eq!(repo.head_tree_id()?, head.tree_id()?);
20//! # Ok(()) }
21//! ```
22//!
23//! ### The Trust Model
24//!
25//! It is very simple - based on the ownership of the repository compared to the user of the current process [Trust](sec::Trust)
26//! is assigned. This can be [overridden](open::Options::with()) as well. Further, git configuration files track their trust level
27//! per section based on and sensitive values like paths to executables or certain values will be skipped if they are from a source
28//! that isn't [fully](sec::Trust::Full) trusted.
29//!
30//! That way, data can safely be obtained without risking to execute untrusted executables.
31//!
32//! Note that it's possible to let `gix` act like `git` or `git2` by setting the [open::Options::bail_if_untrusted()] option.
33//!
34//! ### The prelude and extensions
35//!
36//! With `use git_repository::prelude::*` you should be ready to go as it pulls in various extension traits to make functionality
37//! available on objects that may use it.
38//!
39//! The method signatures are still complex and may require various arguments for configuration and cache control.
40//!
41//! Most extensions to existing objects provide an `obj_with_extension.attach(&repo).an_easier_version_of_a_method()` for simpler
42//! call signatures.
43//!
44//! ### `ThreadSafe` Mode
45//!
46//! By default, the [`Repository`] isn't `Sync` and thus can't be used in certain contexts which require the `Sync` trait.
47//!
48//! To help with this, convert it with [`Repository::into_sync()`] into a [`ThreadSafeRepository`].
49//!
50//! ### Object-Access Performance
51//!
52//! Accessing objects quickly is the bread-and-butter of working with git, right after accessing references. Hence it's vital
53//! to understand which cache levels exist and how to leverage them.
54//!
55//! When accessing an object, the first cache that's queried is a  memory-capped LRU object cache, mapping their id to data and kind.
56//! It has to be specifically enabled on a [`Repository`].
57//! On miss, the object is looked up and if a pack is hit, there is a small fixed-size cache for delta-base objects.
58//!
59//! In scenarios where the same objects are accessed multiple times, the object cache can be useful and is to be configured specifically
60//! using the [`Repository::object_cache_size()`] method.
61//!
62//! Use the `cache-efficiency-debug` cargo feature to learn how efficient the cache actually is - it's easy to end up with lowered
63//! performance if the cache is not hit in 50% of the time.
64//!
65//! ### Terminology
66//!
67//! #### `WorkingTree` and `WorkTree`
68//!
69//! When reading the documentation of the canonical gix-worktree program one gets the impression work tree and working tree are used
70//! interchangeably. We use the term _work tree_ only and try to do so consistently as its shorter and assumed to be the same.
71//!
72//! ### Plumbing Crates
73//!
74//! To make using  _sub-crates_ and their types easier, these are re-exported into the root of this crate. Here we list how to access nested plumbing
75//! crates which are otherwise harder to discover:
76//!
77//! **`git_repository::`**
78//! * [`odb`]
79//!   * [`pack`][odb::pack]
80//! * [`protocol`]
81//!   * [`transport`][protocol::transport]
82//!     * [`packetline`][protocol::transport::packetline]
83//!
84//! ### `libgit2` API to `gix`
85//!
86//! This doc-aliases are used to help finding methods under a possibly changed name. Just search in the docs.
87//! Entering `git2` into the search field will also surface all methods with such annotations.
88//!
89//! What follows is a list of methods you might be missing, along with workarounds if available.
90//! * [`git2::Repository::open_bare()`](https://docs.rs/git2/*/git2/struct.Repository.html#method.open_bare) ➡ ❌ - use [`open()`] and discard if it is not bare.
91//! * [`git2::build::CheckoutBuilder::disable_filters()`](https://docs.rs/git2/*/git2/build/struct.CheckoutBuilder.html#method.disable_filters) ➡ ❌ *(filters are always applied during checkouts)*
92//! * [`git2::Repository::submodule_status()`](https://docs.rs/git2/*/git2/struct.Repository.html#method.submodule_status) ➡ [`Submodule::state()`] - status provides more information and conveniences though, and an actual worktree status isn't performed.
93//!
94//! #### Integrity checks
95//!
96//! `git2` by default performs integrity checks via [`strict_hash_verification()`](https://docs.rs/git2/latest/git2/opts/fn.strict_hash_verification.html) and
97//! [`strict_object_creation`](https://docs.rs/git2/latest/git2/opts/fn.strict_object_creation.html) which `gitoxide` *currently* **does not have**.
98//!
99//! ### Feature Flags
100#![cfg_attr(
101    all(doc, feature = "document-features"),
102    doc = ::document_features::document_features!()
103)]
104#![cfg_attr(all(doc, feature = "document-features"), feature(doc_cfg))]
105#![deny(missing_docs, unsafe_code)]
106#![allow(clippy::result_large_err)]
107
108// Re-exports to make this a potential one-stop shop crate avoiding people from having to reference various crates themselves.
109// This also means that their major version changes affect our major version, but that's alright as we directly expose their
110// APIs/instances anyway.
111pub use gix_actor as actor;
112#[cfg(feature = "attributes")]
113pub use gix_attributes as attrs;
114#[cfg(feature = "blame")]
115pub use gix_blame as blame;
116#[cfg(feature = "command")]
117pub use gix_command as command;
118pub use gix_commitgraph as commitgraph;
119#[cfg(feature = "credentials")]
120pub use gix_credentials as credentials;
121pub use gix_date as date;
122#[cfg(feature = "dirwalk")]
123pub use gix_dir as dir;
124pub use gix_error as error;
125pub use gix_features as features;
126use gix_features::threading::OwnShared;
127pub use gix_features::{
128    parallel,
129    progress::{Count, DynNestedProgress, NestedProgress, Progress},
130    threading,
131};
132pub use gix_fs as fs;
133pub use gix_glob as glob;
134pub use gix_hash as hash;
135pub use gix_hashtable as hashtable;
136#[cfg(feature = "excludes")]
137pub use gix_ignore as ignore;
138#[doc(inline)]
139#[cfg(feature = "index")]
140pub use gix_index as index;
141pub use gix_lock as lock;
142#[cfg(feature = "credentials")]
143pub use gix_negotiate as negotiate;
144pub use gix_object as objs;
145pub use gix_object::bstr;
146pub use gix_odb as odb;
147#[cfg(feature = "credentials")]
148pub use gix_prompt as prompt;
149pub use gix_protocol as protocol;
150pub use gix_ref as refs;
151pub use gix_refspec as refspec;
152pub use gix_revwalk as revwalk;
153pub use gix_sec as sec;
154pub use gix_tempfile as tempfile;
155pub use gix_trace as trace;
156pub use gix_traverse as traverse;
157pub use gix_url as url;
158#[doc(inline)]
159pub use gix_url::Url;
160pub use gix_utils as utils;
161pub use gix_validate as validate;
162pub use hash::{ObjectId, oid};
163
164pub use gix_error::{Error, Exn};
165
166pub mod interrupt;
167
168mod ext;
169///
170pub mod prelude;
171
172#[cfg(feature = "excludes")]
173mod attribute_stack;
174
175///
176pub mod path;
177
178/// The standard type for a store to handle git references.
179pub type RefStore = gix_ref::file::Store;
180/// A handle for finding objects in an object database, abstracting away caches for thread-local use.
181pub type OdbHandle = gix_odb::memory::Proxy<gix_odb::Handle>;
182/// A handle for finding objects in an object database, abstracting away caches for moving across threads.
183pub type OdbHandleArc = gix_odb::memory::Proxy<gix_odb::HandleArc>;
184
185/// A way to access git configuration
186pub(crate) type Config = OwnShared<gix_config::File<'static>>;
187
188mod types;
189#[cfg(any(feature = "excludes", feature = "attributes"))]
190pub use types::AttributeStack;
191pub use types::{
192    Blob, Commit, Head, Id, Object, ObjectDetached, Reference, Remote, Repository, Tag, ThreadSafeRepository, Tree,
193    Worktree,
194};
195#[cfg(feature = "attributes")]
196pub use types::{Pathspec, PathspecDetached, Submodule};
197
198///
199pub mod clone;
200pub mod commit;
201///
202#[cfg(feature = "dirwalk")]
203pub mod dirwalk;
204pub mod head;
205pub mod id;
206pub mod object;
207#[cfg(feature = "attributes")]
208pub mod pathspec;
209pub mod reference;
210pub mod repository;
211#[cfg(feature = "attributes")]
212pub mod submodule;
213pub mod tag;
214#[cfg(any(feature = "dirwalk", feature = "status"))]
215pub(crate) mod util;
216
217///
218pub mod progress;
219///
220pub mod push;
221
222///
223pub mod diff;
224
225///
226#[cfg(feature = "merge")]
227pub mod merge;
228
229/// Try to open a git repository in `directory` and search upwards through its parents until one is found,
230/// using default trust options which matters in case the found repository isn't owned by the current user.
231///
232/// For details, see [`ThreadSafeRepository::discover()`].
233///
234/// # Note
235///
236/// **The discovered repository might not be suitable for any operation that requires authentication with remotes**
237/// as it doesn't see the relevant git configuration.
238///
239/// To achieve that, one has to [enable `git_binary` configuration](https://github.com/GitoxideLabs/gitoxide/blob/9723e1addf52cc336d59322de039ea0537cdca36/src/plumbing/main.rs#L86)
240/// in the open-options and use [`ThreadSafeRepository::discover_opts()`] instead. Alternatively, it might be well-known
241/// that the tool is going to run in a neatly configured environment without relying on bundled configuration.
242///
243/// # Examples
244///
245/// ```
246/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
247/// # mod doctest { include!(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/doctest.rs")); }
248/// # let repo_dir = doctest::basic_repo_dir()?;
249/// let repo = doctest::discover_repo(repo_dir.join("some/very/deeply/nested/subdir"))?;
250///
251/// assert_eq!(repo.kind(), gix::repository::Kind::Common);
252/// assert_eq!(repo.head_name()?.expect("born").shorten(), "main");
253/// assert!(repo.workdir_path("this").expect("non-bare").is_file());
254/// # Ok(()) }
255/// ```
256#[allow(clippy::result_large_err)]
257pub fn discover(directory: impl AsRef<std::path::Path>) -> Result<Repository, discover::Error> {
258    ThreadSafeRepository::discover(directory).map(Into::into)
259}
260
261/// Try to discover a git repository directly from the environment.
262///
263/// For details, see [`ThreadSafeRepository::discover_with_environment_overrides_opts()`].
264#[allow(clippy::result_large_err)]
265pub fn discover_with_environment_overrides(
266    directory: impl AsRef<std::path::Path>,
267) -> Result<Repository, discover::Error> {
268    ThreadSafeRepository::discover_with_environment_overrides(directory).map(Into::into)
269}
270
271/// Try to open a git repository directly from the environment.
272///
273/// See [`ThreadSafeRepository::open_with_environment_overrides()`].
274#[allow(clippy::result_large_err)]
275pub fn open_with_environment_overrides(directory: impl Into<std::path::PathBuf>) -> Result<Repository, open::Error> {
276    ThreadSafeRepository::open_with_environment_overrides(directory, Default::default()).map(Into::into)
277}
278
279/// See [`ThreadSafeRepository::init()`], but returns a [`Repository`] instead.
280///
281/// # Examples
282///
283/// ```
284/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
285/// # mod doctest { include!(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/doctest.rs")); }
286/// # let dir = doctest::tempdir()?;
287/// let repo = gix::init(dir.path())?;
288///
289/// assert!(repo.git_dir().is_dir());
290/// assert!(repo.head_name()?.is_some());
291/// assert!(repo.head()?.is_unborn());
292/// # Ok(()) }
293/// ```
294#[allow(clippy::result_large_err)]
295pub fn init(directory: impl AsRef<std::path::Path>) -> Result<Repository, init::Error> {
296    ThreadSafeRepository::init(directory, create::Kind::WithWorktree, create::Options::default()).map(Into::into)
297}
298
299/// See [`ThreadSafeRepository::init()`], but returns a [`Repository`] instead.
300#[allow(clippy::result_large_err)]
301pub fn init_bare(directory: impl AsRef<std::path::Path>) -> Result<Repository, init::Error> {
302    ThreadSafeRepository::init(directory, create::Kind::Bare, create::Options::default()).map(Into::into)
303}
304
305/// Create a platform for configuring a bare clone from `url` to the local `path`, using default options for opening it (but
306/// amended with using configuration from the git installation to ensure all authentication options are honored).
307///
308/// See [`clone::PrepareFetch::new()`] for a function to take full control over all options.
309#[allow(clippy::result_large_err)]
310pub fn prepare_clone_bare<Url, E>(
311    url: Url,
312    path: impl AsRef<std::path::Path>,
313) -> Result<clone::PrepareFetch, clone::Error>
314where
315    Url: std::convert::TryInto<gix_url::Url, Error = E>,
316    gix_url::parse::Error: From<E>,
317{
318    clone::PrepareFetch::new(
319        url,
320        path,
321        create::Kind::Bare,
322        create::Options::default(),
323        open_opts_with_git_binary_config(),
324    )
325}
326
327/// Create a platform for configuring a clone with main working tree from `url` to the local `path`, using default options for opening it
328/// (but amended with using configuration from the git installation to ensure all authentication options are honored).
329///
330/// See [`clone::PrepareFetch::new()`] for a function to take full control over all options.
331#[allow(clippy::result_large_err)]
332pub fn prepare_clone<Url, E>(url: Url, path: impl AsRef<std::path::Path>) -> Result<clone::PrepareFetch, clone::Error>
333where
334    Url: std::convert::TryInto<gix_url::Url, Error = E>,
335    gix_url::parse::Error: From<E>,
336{
337    clone::PrepareFetch::new(
338        url,
339        path,
340        create::Kind::WithWorktree,
341        create::Options::default(),
342        open_opts_with_git_binary_config(),
343    )
344}
345
346fn open_opts_with_git_binary_config() -> open::Options {
347    use gix_sec::trust::DefaultForLevel;
348    let mut opts = open::Options::default_for_level(gix_sec::Trust::Full);
349    opts.permissions.config.git_binary = true;
350    opts
351}
352
353/// See [`ThreadSafeRepository::open()`], but returns a [`Repository`] instead.
354///
355/// # Examples
356///
357/// ```
358/// # fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
359/// # mod doctest { include!(concat!(env!("CARGO_MANIFEST_DIR"), "/tests/doctest.rs")); }
360/// # let repo_dir = doctest::basic_repo_dir()?;
361/// let repo = doctest::open_repo(repo_dir)?;
362///
363/// assert_eq!(repo.head_name()?.expect("born").shorten(), "main");
364/// assert_eq!(repo.head_commit()?.decode()?.message, "c2\n");
365/// # Ok(()) }
366/// ```
367#[allow(clippy::result_large_err)]
368#[doc(alias = "git2")]
369pub fn open(directory: impl Into<std::path::PathBuf>) -> Result<Repository, open::Error> {
370    ThreadSafeRepository::open(directory).map(Into::into)
371}
372
373/// See [`ThreadSafeRepository::open_opts()`], but returns a [`Repository`] instead.
374#[allow(clippy::result_large_err)]
375#[doc(alias = "open_ext", alias = "git2")]
376pub fn open_opts(directory: impl Into<std::path::PathBuf>, options: open::Options) -> Result<Repository, open::Error> {
377    ThreadSafeRepository::open_opts(directory, options).map(Into::into)
378}
379
380///
381pub mod create;
382
383///
384pub mod open;
385
386///
387pub mod config;
388
389///
390#[cfg(feature = "mailmap")]
391pub mod mailmap;
392
393///
394pub mod worktree;
395
396pub mod revision;
397
398#[cfg(feature = "attributes")]
399pub mod filter;
400
401///
402pub mod remote;
403
404///
405pub mod init;
406
407/// Not to be confused with 'status'.
408pub mod state;
409
410///
411#[cfg(feature = "status")]
412pub mod status;
413
414///
415pub mod shallow;
416
417///
418pub mod discover;
419
420pub mod env;
421
422#[cfg(feature = "attributes")]
423fn is_dir_to_mode(is_dir: bool) -> gix_index::entry::Mode {
424    if is_dir {
425        gix_index::entry::Mode::DIR
426    } else {
427        gix_index::entry::Mode::FILE
428    }
429}