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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
//! Repository-scoped configuration reads and local writes via [`Config`].
use std::fs;
use std::path::PathBuf;
use std::sync::Arc;
use crate::config::{ConfigFile, ConfigScope, ConfigSet};
use crate::error::Result;
use crate::repo::Repository;
/// Read merged config and update the repository-local config file.
#[derive(Debug, Copy, Clone)]
pub struct Config<'repo> {
repo: &'repo Repository,
}
impl<'repo> Config<'repo> {
/// Bind config operations to `repo`.
#[inline]
#[must_use]
pub fn new(repo: &'repo Repository) -> Self {
Self { repo }
}
/// Underlying repository.
#[inline]
#[must_use]
pub fn repository(&self) -> &'repo Repository {
self.repo
}
/// Shared merged config snapshot for this repository.
///
/// # Errors
///
/// Propagates config load failures.
///
/// # Examples
///
/// ```
/// # fn main() -> grit_lib::error::Result<()> {
/// use grit_lib::repo::{InitOptions, Repository};
/// let dir = std::env::temp_dir().join("grit-config-get-doc");
/// let (repo, _) = Repository::init(&dir, &InitOptions::default())?;
/// let _ = repo.config().get("core.repositoryformatversion")?;
/// # Ok(())
/// # }
/// ```
#[inline]
pub fn snapshot(&self) -> Result<Arc<ConfigSet>> {
self.repo.ensure_config_arc()
}
/// Look up a merged config value (system / global / local / worktree cascade).
///
/// # Errors
///
/// Propagates config load failures and [`crate::error::ConfigError::InvalidKey`] for malformed keys.
pub fn get(&self, key: &str) -> Result<Option<String>> {
Ok(self.snapshot()?.get(key)?)
}
/// All values for `key` in load order.
pub fn get_all(&self, key: &str) -> Result<Vec<String>> {
Ok(self.snapshot()?.get_all(key)?)
}
/// Parse a boolean config value.
pub fn get_bool(&self, key: &str) -> Result<Option<bool>> {
Ok(self.snapshot()?.get_bool(key)?)
}
/// Parse an integer config value (Git `k`/`m`/`g` suffixes supported).
pub fn get_i64(&self, key: &str) -> Result<Option<i64>> {
Ok(self.snapshot()?.get_i64(key)?)
}
/// Expand a path config value using this repository's environment.
pub fn get_path(&self, key: &str) -> Result<Option<PathBuf>> {
Ok(self.snapshot()?.get_path(self.repo.environment(), key)?)
}
/// Parse a Git color value (`#rgb`, `normal`, `bold`, etc.).
pub fn get_color(&self, key: &str) -> Result<Option<String>> {
Ok(self.snapshot()?.get_color(key)?)
}
/// Entries whose canonical key starts with `prefix` (a full key, a section such as
/// `remote.origin`, or a section prefix ending in `.`).
///
/// # Errors
///
/// Config load failures, and [`crate::error::ConfigError`] when `prefix` is empty or
/// invalid.
pub fn entries_under(&self, prefix: &str) -> Result<Vec<crate::config::ConfigEntry>> {
self.snapshot()?.entries_under(prefix)
}
/// The repository's mailmap, merged in Git's source order: `.mailmap` in the work tree,
/// then `mailmap.blob` (`HEAD:.mailmap` by default in a bare repository), then
/// `mailmap.file`.
///
/// # Errors
///
/// Config load failures and unreadable `mailmap.file` paths; a `mailmap.blob` that does
/// not resolve to a blob is skipped with a diagnostic.
pub fn mailmap(&self) -> Result<crate::mailmap::MailmapTable> {
crate::mailmap::load_mailmap_table(self.repo)
}
/// Set a key in the repository-local config file and reload the in-memory snapshot.
#[deprecated(note = "use ConfigEditor and explicit reload where needed")]
#[inline]
pub fn set_local(&self, key: &str, value: &str) -> Result<()> {
let path = crate::repo::local_config_write_path(self.repo.git_dir());
let content = fs::read_to_string(&path).unwrap_or_default();
let mut file = ConfigFile::parse(&path, &content, ConfigScope::Local)?;
file.set(key, value)?;
file.write()?;
self.repo.reload_config()
}
/// Remove `key` from the repository-local config file and reload the snapshot.
#[deprecated(note = "use ConfigEditor and explicit reload where needed")]
pub fn unset_local(&self, key: &str) -> Result<()> {
let path = crate::repo::local_config_write_path(self.repo.git_dir());
let content = fs::read_to_string(&path).unwrap_or_default();
let mut file = ConfigFile::parse(&path, &content, ConfigScope::Local)?;
file.unset(key)?;
file.write()?;
self.repo.reload_config()
}
/// Remove an entire section from the repository-local config file.
#[deprecated(note = "use ConfigEditor and explicit reload where needed")]
pub fn remove_local_section(&self, section: &str) -> Result<()> {
let path = crate::repo::local_config_write_path(self.repo.git_dir());
let content = fs::read_to_string(&path).unwrap_or_default();
let mut file = ConfigFile::parse(&path, &content, ConfigScope::Local)?;
file.remove_section(section)?;
file.write()?;
self.repo.reload_config()
}
}