Skip to main content

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))]
24#![deny(missing_docs, unsafe_code)]
25
26use gix_hash::{ObjectId, oid};
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    #[expect(
90        dead_code,
91        reason = "the general reference-store handle is scaffolding for planned ref-table support"
92    )]
93    pub(crate) struct Handle {
94        /// A way to access shared state with the requirement that interior mutability doesn't leak or is incorporated into error types
95        /// if it could. The latter can't happen if references to said internal aren't ever returned.
96        state: handle::State,
97    }
98
99    #[expect(
100        dead_code,
101        reason = "the general reference-store state is scaffolding for planned ref-table support"
102    )]
103    pub(crate) enum State {
104        Loose { store: file::Store },
105    }
106
107    pub(crate) mod general;
108
109    ///
110    #[path = "general/handle/mod.rs"]
111    mod handle;
112    pub use handle::find;
113
114    use crate::file;
115}
116
117/// The git reference store.
118/// TODO: Figure out if handles are needed at all, which depends on the ref-table implementation.
119#[expect(
120    dead_code,
121    reason = "callers still use file::Store directly while this general store awaits ref-table support"
122)]
123pub(crate) struct Store {
124    inner: store::State,
125}
126
127/// A validated complete and fully qualified reference name, safe to use for all operations.
128#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
129#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
130pub struct FullName(pub(crate) BString);
131
132/// A validated complete and fully qualified reference name, safe to use for all operations.
133#[derive(Hash, Debug, PartialEq, Eq, Ord, PartialOrd)]
134#[repr(transparent)]
135pub struct FullNameRef(BStr);
136
137/// A validated and potentially partial reference name, safe to use for common operations.
138#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd)]
139#[repr(transparent)]
140pub struct PartialNameRef(BStr);
141
142/// A validated and potentially partial reference name, safe to use for common operations.
143#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
144#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
145pub struct PartialName(BString);
146
147/// A _validated_ prefix for references to act as a namespace.
148#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
149#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
150pub struct Namespace(BString);
151
152/// Denotes the kind of reference.
153#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
154#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
155pub enum Kind {
156    /// A ref that points to an object id directly.
157    Object,
158    /// A ref that points to another reference, adding a level of indirection.
159    ///
160    /// It can be resolved to an id using the [`peel_to_id()`][`crate::file::ReferenceExt::peel_to_id()`] method.
161    Symbolic,
162}
163
164/// The various known categories of references.
165///
166/// This translates into a prefix containing all references of a given category.
167#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
168#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
169pub enum Category<'a> {
170    /// A tag in `refs/tags`
171    Tag,
172    /// A branch in `refs/heads`
173    LocalBranch,
174    /// A branch in `refs/remotes`
175    RemoteBranch,
176    /// A tag in `refs/notes`
177    Note,
178    /// Something outside `ref/` in the current worktree, typically `HEAD`.
179    PseudoRef,
180    /// A `PseudoRef`, but referenced so that it will always refer to the main worktree by
181    /// prefixing it with `main-worktree/`.
182    MainPseudoRef,
183    /// Any reference that is prefixed with `main-worktree/refs/`
184    MainRef,
185    /// A `PseudoRef` in another _linked_ worktree, never in the main one, like `worktrees/<id>/HEAD`.
186    LinkedPseudoRef {
187        /// The name of the worktree.
188        #[cfg_attr(feature = "serde", serde(borrow))]
189        name: &'a BStr,
190    },
191    /// Any reference that is prefixed with `worktrees/<id>/refs/`.
192    LinkedRef {
193        /// The name of the worktree.
194        name: &'a BStr,
195    },
196    /// A ref that is private to each worktree (_linked_ or _main_), with `refs/bisect/` prefix
197    Bisect,
198    /// A ref that is private to each worktree (_linked_ or _main_), with `refs/rewritten/` prefix
199    Rewritten,
200    /// A ref that is private to each worktree (_linked_ or _main_), with `refs/worktree/` prefix
201    WorktreePrivate,
202    // REF_TYPE_NORMAL,	  /* normal/shared refs inside refs/        */
203}
204
205/// Denotes a ref target, equivalent to [`Kind`], but with mutable data.
206#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
207#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
208pub enum Target {
209    /// A ref that points directly to an object id.
210    Object(ObjectId),
211    /// A ref that points to another reference by its validated name, adding a level of indirection.
212    ///
213    /// Note that this is an extension of gitoxide which will be helpful in logging all reference changes.
214    Symbolic(FullName),
215}
216
217/// Denotes a ref target, equivalent to [`Kind`], but with immutable data.
218#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
219pub enum TargetRef<'a> {
220    /// A ref that points directly to an object id.
221    Object(&'a oid),
222    /// A ref that points to another reference by its validated name, adding a level of indirection.
223    Symbolic(&'a FullNameRef),
224}