gix_ref/
lib.rs

1//! A crate for handling the references stored in various formats in a git repository.
2//!
3//! References are also called _refs_ which are used interchangeably.
4//!
5//! Refs are the way to keep track of objects and come in two flavors.
6//!
7//! * symbolic refs are pointing to another reference
8//! * peeled refs point to the an object by its [`ObjectId`]
9//!
10//! They can be identified by a relative path and stored in various flavors.
11//!
12//! * **files**
13//!   * **[loose][file::Store]**
14//!     * one reference maps to a file on disk
15//!   * **packed**
16//!     * references are stored in a single human-readable file, along with their targets if they are symbolic.
17//!
18//! ## Feature Flags
19#![cfg_attr(
20    all(doc, feature = "document-features"),
21    doc = ::document_features::document_features!()
22)]
23#![cfg_attr(all(doc, feature = "document-features"), feature(doc_cfg, doc_auto_cfg))]
24#![deny(missing_docs, rust_2018_idioms, unsafe_code)]
25
26use gix_hash::{oid, ObjectId};
27pub use gix_object::bstr;
28use gix_object::bstr::{BStr, BString};
29
30#[path = "store/mod.rs"]
31mod store_impl;
32pub use store_impl::{file, packed};
33
34mod fullname;
35///
36pub mod name;
37///
38pub mod namespace;
39///
40pub mod transaction;
41
42mod parse;
43mod raw;
44
45pub use raw::Reference;
46
47mod target;
48
49///
50pub mod log;
51
52///
53pub mod peel;
54
55///
56pub mod store {
57    ///
58    pub mod init {
59
60        /// Options for use during [initialization](crate::file::Store::at).
61        #[derive(Debug, Copy, Clone, Default)]
62        pub struct Options {
63            /// How to write the ref-log.
64            pub write_reflog: super::WriteReflog,
65            /// The kind of hash to expect in
66            pub object_hash: gix_hash::Kind,
67            /// The equivalent of `core.precomposeUnicode`.
68            pub precompose_unicode: bool,
69            /// If `true`, we will avoid reading from or writing to references that contains Windows device names
70            /// to avoid side effects. This only needs to be `true` on Windows, but can be `true` on other platforms
71            /// if they need to remain compatible with Windows.
72            pub prohibit_windows_device_names: bool,
73        }
74    }
75    /// The way a file store handles the reflog
76    #[derive(Default, Debug, PartialOrd, PartialEq, Ord, Eq, Hash, Clone, Copy)]
77    pub enum WriteReflog {
78        /// Always write the reflog for all references for ref edits, unconditionally.
79        Always,
80        /// Write a ref log for ref edits according to the standard rules.
81        #[default]
82        Normal,
83        /// Never write a ref log.
84        Disable,
85    }
86
87    /// A thread-local handle for interacting with a [`Store`][crate::Store] to find and iterate references.
88    #[derive(Clone)]
89    #[allow(dead_code)]
90    pub(crate) struct Handle {
91        /// A way to access shared state with the requirement that interior mutability doesn't leak or is incorporated into error types
92        /// if it could. The latter can't happen if references to said internal aren't ever returned.
93        state: handle::State,
94    }
95
96    #[allow(dead_code)]
97    pub(crate) enum State {
98        Loose { store: file::Store },
99    }
100
101    pub(crate) mod general;
102
103    ///
104    #[path = "general/handle/mod.rs"]
105    mod handle;
106    use crate::file;
107    pub use handle::find;
108}
109
110/// The git reference store.
111/// TODO: Figure out if handles are needed at all, which depends on the ref-table implementation.
112#[allow(dead_code)]
113pub(crate) struct Store {
114    inner: store::State,
115}
116
117/// A validated complete and fully qualified reference name, safe to use for all operations.
118#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
119#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
120pub struct FullName(pub(crate) BString);
121
122/// A validated complete and fully qualified reference name, safe to use for all operations.
123#[derive(Hash, Debug, PartialEq, Eq, Ord, PartialOrd)]
124#[repr(transparent)]
125pub struct FullNameRef(BStr);
126
127/// A validated and potentially partial reference name, safe to use for common operations.
128#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd)]
129#[repr(transparent)]
130pub struct PartialNameRef(BStr);
131
132/// A validated and potentially partial reference name, safe to use for common operations.
133#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
134#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
135pub struct PartialName(BString);
136
137/// A _validated_ prefix for references to act as a namespace.
138#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
139#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
140pub struct Namespace(BString);
141
142/// Denotes the kind of reference.
143#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
144#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
145pub enum Kind {
146    /// A ref that points to an object id directly.
147    Object,
148    /// A ref that points to another reference, adding a level of indirection.
149    ///
150    /// It can be resolved to an id using the [`peel_in_place_to_id()`][`crate::file::ReferenceExt::peel_to_id_in_place()`] method.
151    Symbolic,
152}
153
154/// The various known categories of references.
155///
156/// This translates into a prefix containing all references of a given category.
157#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
158#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
159pub enum Category<'a> {
160    /// A tag in `refs/tags`
161    Tag,
162    /// A branch in `refs/heads`
163    LocalBranch,
164    /// A branch in `refs/remotes`
165    RemoteBranch,
166    /// A tag in `refs/notes`
167    Note,
168    /// Something outside of `ref/` in the current worktree, typically `HEAD`.
169    PseudoRef,
170    /// A `PseudoRef`, but referenced so that it will always refer to the main worktree by
171    /// prefixing it with `main-worktree/`.
172    MainPseudoRef,
173    /// Any reference that is prefixed with `main-worktree/refs/`
174    MainRef,
175    /// A `PseudoRef` in another _linked_ worktree, never in the main one, like `worktrees/<id>/HEAD`.
176    LinkedPseudoRef {
177        /// The name of the worktree.
178        #[cfg_attr(feature = "serde", serde(borrow))]
179        name: &'a BStr,
180    },
181    /// Any reference that is prefixed with `worktrees/<id>/refs/`.
182    LinkedRef {
183        /// The name of the worktree.
184        name: &'a BStr,
185    },
186    /// A ref that is private to each worktree (_linked_ or _main_), with `refs/bisect/` prefix
187    Bisect,
188    /// A ref that is private to each worktree (_linked_ or _main_), with `refs/rewritten/` prefix
189    Rewritten,
190    /// A ref that is private to each worktree (_linked_ or _main_), with `refs/worktree/` prefix
191    WorktreePrivate,
192    // REF_TYPE_NORMAL,	  /* normal/shared refs inside refs/        */
193}
194
195/// Denotes a ref target, equivalent to [`Kind`], but with mutable data.
196#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
197#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
198pub enum Target {
199    /// A ref that points directly to an object id.
200    Object(ObjectId),
201    /// A ref that points to another reference by its validated name, adding a level of indirection.
202    ///
203    /// Note that this is an extension of gitoxide which will be helpful in logging all reference changes.
204    Symbolic(FullName),
205}
206
207/// Denotes a ref target, equivalent to [`Kind`], but with immutable data.
208#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
209pub enum TargetRef<'a> {
210    /// A ref that points directly to an object id.
211    Object(&'a oid),
212    /// A ref that points to another reference by its validated name, adding a level of indirection.
213    Symbolic(&'a FullNameRef),
214}