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