git_ref/store/file/mod.rs
1use std::{
2 borrow::Cow,
3 path::{Path, PathBuf},
4};
5
6use crate::{bstr::BStr, store::WriteReflog, Namespace};
7
8/// A store for reference which uses plain files.
9///
10/// Each ref is represented as a single file on disk in a folder structure that follows the relative path
11/// used to identify [references][crate::Reference].
12#[derive(Debug, Clone)]
13pub struct Store {
14 /// The location at which loose references can be found as per conventions of a typical git repository.
15 ///
16 /// Typical base paths are `.git` repository folders.
17 git_dir: PathBuf,
18 /// Possibly the common directory at which to find shared references. Only set if this `Store` is for a work tree.
19 common_dir: Option<PathBuf>,
20 /// The kind of hash to assume in a couple of situations. Note that currently we are able to read any valid hash from files
21 /// which might want to change one day.
22 object_hash: git_hash::Kind,
23
24 /// The way to handle reflog edits
25 pub write_reflog: WriteReflog,
26 /// The namespace to use for edits and reads
27 pub namespace: Option<Namespace>,
28 /// A packed buffer which can be mapped in one version and shared as such.
29 /// It's updated only in one spot, which is prior to reading it based on file stamps.
30 /// Doing it like this has the benefit of being able to hand snapshots out to people without blocking others from updating it.
31 packed: packed::modifiable::MutableSharedBuffer,
32}
33
34mod access {
35 use std::path::Path;
36
37 use crate::file;
38
39 impl file::Store {
40 /// Return the `.git` directory at which all references are loaded.
41 ///
42 /// For worktrees, this is the linked work-tree private ref location,
43 /// then [`common_dir()`][file::Store::common_dir()] is `Some(parent_git_dir)`.
44 pub fn git_dir(&self) -> &Path {
45 &self.git_dir
46 }
47
48 /// If this is a linked work tree, there will be `Some(git_dir)` pointing to the parent repository,
49 /// while [`git_dir()`][file::Store::git_dir()] points to the location holding linked work-tree private references.
50 pub fn common_dir(&self) -> Option<&Path> {
51 self.common_dir.as_deref()
52 }
53
54 /// Similar to [`common_dir()`][file::Store::common_dir()], but it will produce either the common-dir, or the git-dir if the former
55 /// isn't present.
56 ///
57 /// This is also the directory in which the packed references file would be placed.
58 pub fn common_dir_resolved(&self) -> &Path {
59 self.common_dir.as_deref().unwrap_or(&self.git_dir)
60 }
61 }
62}
63
64/// A transaction on a file store
65pub struct Transaction<'s, 'p> {
66 store: &'s Store,
67 packed_transaction: Option<crate::store_impl::packed::Transaction>,
68 updates: Option<Vec<transaction::Edit>>,
69 packed_refs: transaction::PackedRefs<'p>,
70}
71
72pub(in crate::store_impl::file) fn path_to_name<'a>(path: impl Into<Cow<'a, Path>>) -> Cow<'a, BStr> {
73 let path = git_path::into_bstr(path.into());
74 git_path::to_unix_separators_on_windows(path)
75}
76
77///
78pub mod loose;
79mod overlay_iter;
80
81///
82pub mod iter {
83 pub use super::overlay_iter::{LooseThenPacked, Platform};
84
85 ///
86 pub mod loose_then_packed {
87 pub use super::super::overlay_iter::Error;
88 }
89}
90
91///
92pub mod log;
93
94///
95pub mod find;
96
97///
98pub mod transaction;
99
100///
101pub mod packed;
102
103mod raw_ext;
104pub use raw_ext::ReferenceExt;