1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
use std::cell::RefCell;
use crate::Result;
impl crate::Repository {
#[expect(clippy::too_many_arguments)]
pub(crate) fn from_refs_and_objects(
refs: crate::RefStore,
mut objects: crate::OdbHandle,
work_tree: Option<std::path::PathBuf>,
index_path: std::path::PathBuf,
common_dir: Option<std::path::PathBuf>,
config: crate::config::Cache,
linked_worktree_options: crate::open::Options,
#[cfg(feature = "index")] index: crate::worktree::IndexStorage,
shallow_commits: crate::shallow::CommitsStorage,
#[cfg(feature = "attributes")] modules: crate::submodule::ModulesFileStorage,
) -> Self {
setup_objects(&mut objects, &config);
crate::Repository {
bufs: Some(RefCell::new(Vec::with_capacity(4))),
work_tree,
index_path,
common_dir,
objects,
refs,
config,
options: linked_worktree_options,
#[cfg(feature = "index")]
index,
shallow_commits,
#[cfg(feature = "attributes")]
modules,
}
}
/// Convert this instance into a [`ThreadSafeRepository`][crate::ThreadSafeRepository] by dropping all thread-local data.
pub fn into_sync(self) -> crate::ThreadSafeRepository {
self.into()
}
/// Reopen this repository in place using the stored open options.
/// Use this to forcefully refresh Git configuration, drop caches, and release system resources
/// for opened object database resources (if this is the last instance to a repository).
///
/// This discards in-memory-only configuration edits and any other transient repository state that is recreated
/// during opening.
///
/// # Notes on relative paths
///
/// When the [`git_dir`](Self::git_dir()) is relative and the current working dir changed,
/// then a reload will be performed on the joined path of both to make it succeed, which makes
/// the reloaded repository git-dir absolute.
pub fn reload(&mut self) -> Result<&mut Self> {
self.reload_with(|options| options)
}
/// Reopen this repository in place after applying `configure` to a copy of its stored open options.
///
/// Reload reuses the stored trust level unless `configure` changes or clears it with
/// [`with_trust()`][crate::open::Options::with_trust()] or
/// [`with_enforced_trust()`][crate::open::Options::with_enforced_trust()].
/// The modified options are stored for subsequent reloads. On failure, this handle remains unchanged.
/// Like [`reload()`][Self::reload()], this discards transient state and refreshes configuration and caches.
///
/// # Example
///
/// ```no_run
/// # let mut repo = gix::open(".")?;
/// repo.reload_with(|options| options.cli_overrides(["core.abbrev=8"]))?;
/// # Ok::<_, gix::Error>(())
/// ```
pub fn reload_with(
&mut self,
configure: impl FnOnce(crate::open::Options) -> crate::open::Options,
) -> Result<&mut Self> {
let mut git_dir = self.git_dir().to_owned();
let options = configure(self.options.clone_with_trust()).open_path_as_is(true);
if git_dir.is_relative()
&& let Some((prev_cwd, cwd)) = self.options.current_dir.as_ref().zip(std::env::current_dir().ok())
&& *prev_cwd != cwd
{
git_dir = prev_cwd.join(git_dir);
}
*self = crate::ThreadSafeRepository::open_opts(git_dir, options)?.to_thread_local();
Ok(self)
}
}
#[cfg_attr(not(feature = "max-performance"), allow(unused_variables, unused_mut))]
pub(crate) fn setup_objects(objects: &mut crate::OdbHandle, config: &crate::config::Cache) {
objects.shared_repository_permissions = config.shared_repository_permissions;
#[cfg(feature = "max-performance")]
{
match config.pack_cache_bytes {
None => match config.static_pack_cache_limit_bytes {
None => objects.set_pack_cache(|| Box::<gix_pack::cache::lru::StaticLinkedList<64>>::default()),
Some(limit) => {
objects.set_pack_cache(move || Box::new(gix_pack::cache::lru::StaticLinkedList::<64>::new(limit)));
}
},
Some(0) => objects.unset_pack_cache(),
Some(bytes) => objects.set_pack_cache(move || -> Box<gix_odb::cache::PackCache> {
Box::new(gix_pack::cache::lru::MemoryCappedHashmap::new(bytes))
}),
}
if config.object_cache_bytes == 0 {
objects.unset_object_cache();
} else {
let bytes = config.object_cache_bytes;
objects.set_object_cache(move || Box::new(gix_pack::cache::object::MemoryCappedHashmap::new(bytes)));
}
}
}