git_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][git_hash::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 feature = "document-features",
21 cfg_attr(doc, doc = ::document_features::document_features!())
22)]
23#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
24#![deny(missing_docs, rust_2018_idioms, unsafe_code)]
25
26use std::borrow::Cow;
27
28use git_hash::{oid, ObjectId};
29pub use git_object::bstr;
30use git_object::bstr::{BStr, BString};
31
32#[path = "store/mod.rs"]
33mod store_impl;
34pub use store_impl::{file, packed};
35
36mod fullname;
37///
38pub mod name;
39///
40pub mod namespace;
41///
42pub mod transaction;
43
44mod parse;
45mod raw;
46
47pub use raw::Reference;
48
49mod target;
50
51///
52pub mod log;
53
54///
55pub mod peel;
56
57///
58pub mod store {
59 /// The way a file store handles the reflog
60 #[derive(Debug, PartialOrd, PartialEq, Ord, Eq, Hash, Clone, Copy)]
61 pub enum WriteReflog {
62 /// Always write the reflog for all references for ref edits, unconditionally.
63 Always,
64 /// Write a ref log for ref edits according to the standard rules.
65 Normal,
66 /// Never write a ref log.
67 Disable,
68 }
69
70 impl Default for WriteReflog {
71 fn default() -> Self {
72 WriteReflog::Normal
73 }
74 }
75
76 /// A thread-local handle for interacting with a [`Store`][crate::Store] to find and iterate references.
77 #[derive(Clone)]
78 #[allow(dead_code)]
79 pub(crate) struct Handle {
80 /// A way to access shared state with the requirement that interior mutability doesn't leak or is incorporated into error types
81 /// if it could. The latter can't happen if references to said internal aren't ever returned.
82 state: handle::State,
83 }
84
85 #[allow(dead_code)]
86 pub(crate) enum State {
87 Loose { store: file::Store },
88 }
89
90 pub(crate) mod general;
91
92 ///
93 #[path = "general/handle/mod.rs"]
94 mod handle;
95 pub use handle::find;
96
97 use crate::file;
98}
99
100/// The git reference store.
101/// TODO: Figure out if handles are needed at all, which depends on the ref-table implementation.
102#[allow(dead_code)]
103pub(crate) struct Store {
104 inner: store::State,
105}
106
107/// Indicate that the given BString is a validate reference name or path that can be used as path on disk or written as target
108/// of a symbolic reference
109#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
110#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
111pub struct FullName(pub(crate) BString);
112
113/// A validated and potentially partial reference name - it can safely be used for common operations.
114#[derive(Hash, Debug, PartialEq, Eq, Ord, PartialOrd)]
115#[repr(transparent)]
116pub struct FullNameRef(BStr);
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)]
120pub struct PartialNameCow<'a>(Cow<'a, BStr>);
121
122/// A validated complete and fully qualified referenced reference name, safe to use for all operations.
123#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd)]
124#[repr(transparent)]
125pub struct PartialNameRef(BStr);
126
127/// A validated complete and fully qualified owned reference name, safe to use for all operations.
128#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd)]
129pub struct PartialName(BString);
130
131/// A _validated_ prefix for references to act as a namespace.
132#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
133pub struct Namespace(BString);
134
135/// Denotes the kind of reference.
136#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
137#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
138pub enum Kind {
139 /// A ref that points to an object id
140 Peeled,
141 /// A ref that points to another reference, adding a level of indirection.
142 ///
143 /// It can be resolved to an id using the [`peel_in_place_to_id()`][`crate::file::ReferenceExt::peel_to_id_in_place()`] method.
144 Symbolic,
145}
146
147/// The various known categories of references.
148///
149/// This translates into a prefix containing all references of a given category.
150#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
151pub enum Category<'a> {
152 /// A tag in `refs/tags`
153 Tag,
154 /// A branch in `refs/heads`
155 LocalBranch,
156 /// A branch in `refs/remotes`
157 RemoteBranch,
158 /// A tag in `refs/notes`
159 Note,
160 /// Something outside of `ref/` in the current worktree, typically `HEAD`.
161 PseudoRef,
162 /// A `PseudoRef`, but referenced so that it will always refer to the main worktree by
163 /// prefixing it with `main-worktree/`.
164 MainPseudoRef,
165 /// Any reference that is prefixed with `main-worktree/refs/`
166 MainRef,
167 /// A `PseudoRef` in another _linked_ worktree, never in the main one, like `worktrees/<id>/HEAD`.
168 LinkedPseudoRef {
169 /// The name of the worktree.
170 name: &'a BStr,
171 },
172 /// Any reference that is prefixed with `worktrees/<id>/refs/`.
173 LinkedRef {
174 /// The name of the worktree.
175 name: &'a BStr,
176 },
177 /// A ref that is private to each worktree (_linked_ or _main_), with `refs/bisect/` prefix
178 Bisect,
179 /// A ref that is private to each worktree (_linked_ or _main_), with `refs/rewritten/` prefix
180 Rewritten,
181 /// A ref that is private to each worktree (_linked_ or _main_), with `refs/worktree/` prefix
182 WorktreePrivate,
183 // REF_TYPE_NORMAL, /* normal/shared refs inside refs/ */
184}
185
186/// Denotes a ref target, equivalent to [`Kind`], but with mutable data.
187#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
188#[cfg_attr(feature = "serde1", derive(serde::Serialize, serde::Deserialize))]
189pub enum Target {
190 /// A ref that points to an object id
191 Peeled(ObjectId),
192 /// A ref that points to another reference by its validated name, adding a level of indirection.
193 ///
194 /// Note that this is an extension of gitoxide which will be helpful in logging all reference changes.
195 Symbolic(FullName),
196}
197
198/// Denotes a ref target, equivalent to [`Kind`], but with immutable data.
199#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
200pub enum TargetRef<'a> {
201 /// A ref that points to an object id
202 Peeled(&'a oid),
203 /// A ref that points to another reference by its validated name, adding a level of indirection.
204 Symbolic(&'a FullNameRef),
205}