pub struct ConfigSet { /* private fields */ }Expand description
A merged view across all configuration scopes.
Entries are stored in file-order within each scope; scopes are layered in priority order (system < global < local < worktree < command).
Implementations§
Source§impl ConfigSet
impl ConfigSet
Sourcepub fn entries(&self) -> &[ConfigEntry]
pub fn entries(&self) -> &[ConfigEntry]
All merged entries in load order (for listing keys such as alias.*).
Sourcepub fn merge(&mut self, file: &ConfigFile)
pub fn merge(&mut self, file: &ConfigFile)
Merge entries from a ConfigFile into this set.
Entries are appended; later values override earlier ones for single-value lookups.
Sourcepub fn merge_set(&mut self, other: &ConfigSet)
pub fn merge_set(&mut self, other: &ConfigSet)
Merge another ConfigSet into this set (entries appended in order).
Sourcepub fn add_command_override(&mut self, key: &str, value: &str) -> Result<()>
pub fn add_command_override(&mut self, key: &str, value: &str) -> Result<()>
Add a command-line override (-c key=value).
Sourcepub fn get_last_entry(&self, key: &str) -> Option<ConfigEntry>
pub fn get_last_entry(&self, key: &str) -> Option<ConfigEntry>
Last (highest-priority) ConfigEntry for a key, including origin metadata.
Bare boolean keys are returned with ConfigEntry::value set to None (same as get,
which maps them to "true" for string lookups).
Sourcepub fn get_all(&self, key: &str) -> Vec<String>
pub fn get_all(&self, key: &str) -> Vec<String>
Get all values for a key (multi-valued; in load order).
Sourcepub fn get_all_raw(&self, key: &str) -> Vec<Option<String>>
pub fn get_all_raw(&self, key: &str) -> Vec<Option<String>>
All raw values for a key in load order, preserving None for bare boolean keys.
Matches Git’s multi-value list where NULL means a value-less / boolean-true key.
Sourcepub fn has_key(&self, key: &str) -> bool
pub fn has_key(&self, key: &str) -> bool
True if any config entry uses key (after canonicalization), including bare boolean keys.
Unlike Self::get, this does not treat a missing value as "true" — it reports whether
the key appears in the merged config at all (Git repo_config_get / git_configset_get).
Sourcepub fn get_bool(&self, key: &str) -> Option<Result<bool, String>>
pub fn get_bool(&self, key: &str) -> Option<Result<bool, String>>
Get a boolean value, interpreting true/yes/on/1 as true and
false/no/off/0 as false.
pack.allowPackReuse may be single or multi (Git enum, not a bool). Those values are
treated as unset for boolean lookup so get_bool does not error during broad config scans.
Sourcepub fn quote_path_fully(&self) -> bool
pub fn quote_path_fully(&self) -> bool
Whether pathnames in human-readable output should fully C-quote non-ASCII bytes as octal.
Maps to Git’s quote_path_fully (core.quotepath, default true). When false, UTF-8 and
other high bytes are emitted literally; only ASCII specials are escaped. Also honors
core.quotePath as an alternate spelling.
Sourcepub fn pack_write_reverse_index_default(&self) -> bool
pub fn pack_write_reverse_index_default(&self) -> bool
Default for pack.writeReverseIndex / pack.writereverseindex (Git default: true).
Tests set GIT_TEST_NO_WRITE_REV_INDEX to force no .rev output.
Sourcepub fn pack_read_reverse_index_default(&self) -> bool
pub fn pack_read_reverse_index_default(&self) -> bool
Default for pack.readReverseIndex / pack.readreverseindex (Git default: true).
Sourcepub fn effective_log_refs_config(&self, git_dir: &Path) -> LogRefsConfig
pub fn effective_log_refs_config(&self, git_dir: &Path) -> LogRefsConfig
Resolved core.logAllRefUpdates using this merged set (includes git -c / env), then Git’s
bare-repo default when the key is unset everywhere.
Sourcepub fn get_i64(&self, key: &str) -> Option<Result<i64, String>>
pub fn get_i64(&self, key: &str) -> Option<Result<i64, String>>
Get an integer value, supporting Git’s k/m/g suffixes.
Sourcepub fn pack_objects_zlib_level(&self) -> Result<i32>
pub fn pack_objects_zlib_level(&self) -> Result<i32>
Zlib deflate level for git pack-objects (Git’s pack_compression_level).
Entries are applied in Self::entries order. core.compression sets the pack level
until a pack.compression appears (Git pack_compression_seen). core.loosecompression
is ignored here — it only affects loose-object zlib, not packs.
-1 means zlib default (level 6). Valid values are -1 or 0..=9.
Sourcepub fn get_regexp(&self, pattern: &str) -> Result<Vec<&ConfigEntry>, String>
pub fn get_regexp(&self, pattern: &str) -> Result<Vec<&ConfigEntry>, String>
Get all entries matching a key pattern (regex).
Used by git config --get-regexp. Returns an error if the pattern
is not a valid regex.
Sourcepub fn load_with_options(
git_dir: Option<&Path>,
opts: &LoadConfigOptions,
) -> Result<Self>
pub fn load_with_options( git_dir: Option<&Path>, opts: &LoadConfigOptions, ) -> Result<Self>
Load the standard configuration cascade with explicit include and scope control.
See LoadConfigOptions for GIT_CONFIG_PARAMETERS / -c include behaviour.
Sourcepub fn read_early_config(
git_dir: Option<&Path>,
key: &str,
) -> Result<Vec<String>>
pub fn read_early_config( git_dir: Option<&Path>, key: &str, ) -> Result<Vec<String>>
Read configuration the way Git’s read_early_config / do_git_config_sequence does:
system (unless disabled), global files in Git order, optional repository config /
config.worktree, then GIT_CONFIG_PARAMETERS.
When git_dir is None (no discovered repository, e.g. GIT_CEILING_DIRECTORIES), only
non-repo layers are read — matching Git when discovery returns no gitdir (t1309 ceiling #2).
Returns all values for key in load order (Git’s read_early_config callback runs once per
occurrence).
This matches upstream ordering for test-tool config read_early_config (t1309, t1305).
Sourcepub fn merge_file_with_includes(
&mut self,
file: &ConfigFile,
process_includes: bool,
ctx: &IncludeContext,
) -> Result<()>
pub fn merge_file_with_includes( &mut self, file: &ConfigFile, process_includes: bool, ctx: &IncludeContext, ) -> Result<()>
Merge a single config file, optionally expanding [include] / [includeIf].
Used by grit config -f and scoped reads; ConfigSet::load_with_options uses the same
internal routine for the standard cascade.
Sourcepub fn load_repo_local_only(git_dir: &Path) -> Result<Self>
pub fn load_repo_local_only(git_dir: &Path) -> Result<Self>
Load only the repository’s own config file (plus any [include] targets).
Unlike Self::load, this ignores system/global config and environment
overrides. Used for receive-side options (e.g. transfer.fsckObjects) so a
pusher’s global configuration cannot weaken the remote repository’s policy.
Examples found in repository?
48fn main() -> grit_lib::error::Result<()> {
49 let root = tempfile::tempdir()?;
50 let repo = init_repository(root.path(), false, "main", None, "files")?;
51
52 use grit_lib::index::{Index, IndexEntry, MODE_REGULAR};
53
54 // Base commit on main: one file.
55 let blob_a = repo.odb.write(ObjectKind::Blob, b"base\n")?;
56 let mut index = Index::new();
57 index.add_or_replace(IndexEntry {
58 ctime_sec: 0,
59 ctime_nsec: 0,
60 mtime_sec: 0,
61 mtime_nsec: 0,
62 dev: 0,
63 ino: 0,
64 mode: MODE_REGULAR,
65 uid: 0,
66 gid: 0,
67 size: 0,
68 oid: blob_a,
69 flags: 7,
70 flags_extended: None,
71 path: b"base.txt".to_vec(),
72 base_index_pos: 0,
73 });
74 repo.write_index(&mut index)?;
75 let index = repo.load_index()?;
76 let tree_a = write_tree_from_index(&repo.odb, &index, "")?;
77 let commit_a = commit_from_tree(&repo, tree_a, &[], "initial\n")?;
78 refs::write_ref(&repo.git_dir, "refs/heads/main", &commit_a)?;
79
80 // Topic commit: parent A, adds picked.txt (not on main yet).
81 let blob_pick = repo.odb.write(ObjectKind::Blob, b"hello from topic\n")?;
82 let mut index = Index::new();
83 index.add_or_replace(IndexEntry {
84 ctime_sec: 0,
85 ctime_nsec: 0,
86 mtime_sec: 0,
87 mtime_nsec: 0,
88 dev: 0,
89 ino: 0,
90 mode: MODE_REGULAR,
91 uid: 0,
92 gid: 0,
93 size: 0,
94 oid: blob_a,
95 flags: 7,
96 flags_extended: None,
97 path: b"base.txt".to_vec(),
98 base_index_pos: 0,
99 });
100 index.add_or_replace(IndexEntry {
101 ctime_sec: 0,
102 ctime_nsec: 0,
103 mtime_sec: 0,
104 mtime_nsec: 0,
105 dev: 0,
106 ino: 0,
107 mode: MODE_REGULAR,
108 uid: 0,
109 gid: 0,
110 size: 0,
111 oid: blob_pick,
112 flags: 9,
113 flags_extended: None,
114 path: b"picked.txt".to_vec(),
115 base_index_pos: 0,
116 });
117 repo.write_index(&mut index)?;
118 let index = repo.load_index()?;
119 let tree_b = write_tree_from_index(&repo.odb, &index, "")?;
120 let commit_b = commit_from_tree(&repo, tree_b, &[commit_a], "add picked file\n")?;
121 refs::write_ref(&repo.git_dir, "refs/heads/topic", &commit_b)?;
122
123 // Cherry-pick `topic` onto `main` (still at A).
124 let head = resolve_revision(&repo, "main")?;
125 let picked = resolve_revision(&repo, "topic")?;
126 let picked_obj = repo.odb.read(&picked)?;
127 let picked_data = parse_commit(&picked_obj.data)?;
128 let parent = picked_data.parents.first().copied().ok_or_else(|| {
129 grit_lib::error::Error::CorruptObject("picked commit has no parent".into())
130 })?;
131
132 let base_tree = tree_of_commit(&repo, parent)?;
133 let ours_tree = tree_of_commit(&repo, head)?;
134 let theirs_tree = picked_data.tree;
135
136 let merged = merge_trees_three_way(
137 &repo,
138 base_tree,
139 ours_tree,
140 theirs_tree,
141 MergeFavor::default(),
142 WhitespaceMergeOptions::default(),
143 grit_lib::merge_trees::TreeMergeConflictPresentation {
144 label_ours: "HEAD",
145 label_theirs: grit_lib::merge_trees::TheirsConflictLabel::Fixed("picked"),
146 label_base: "parent of picked commit",
147 style: grit_lib::merge_file::ConflictStyle::Merge,
148 checkout_merge: false,
149 },
150 )?;
151
152 if !merged.conflict_content.is_empty() {
153 return Err(grit_lib::error::Error::Message(format!(
154 "merge produced {} conflict path(s); this example expects a clean pick",
155 merged.conflict_content.len()
156 )));
157 }
158
159 let new_tree = write_tree_from_index(&repo.odb, &merged.index, "")?;
160 let config = ConfigSet::load_repo_local_only(&repo.git_dir)?;
161 let msg = commit_trailers::finalize_cherry_pick_message(
162 &picked_data.message,
163 true,
164 false,
165 "Example",
166 "example@example.com",
167 &config,
168 &picked.to_hex(),
169 );
170 let new_commit = commit_from_tree(&repo, new_tree, &[head], &msg)?;
171 refs::write_ref(&repo.git_dir, "refs/heads/main", &new_commit)?;
172
173 println!("cherry-picked {} onto {}", picked, head);
174 println!("new main: {new_commit}");
175 let out = repo.odb.read(&new_commit)?;
176 println!("message:\n{}", parse_commit(&out.data)?.message);
177
178 Ok(())
179}Sourcepub fn load_protected(include_system: bool) -> Result<Self>
pub fn load_protected(include_system: bool) -> Result<Self>
Load configuration the way Git loads protected config (e.g. uploadpack.packObjectsHook).
This matches Git’s read_protected_config: system (optional), global files only (no
repository or worktree config), then command-line overrides from GIT_CONFIG_COUNT /
GIT_CONFIG_PARAMETERS. It does not read $GIT_CONFIG (Git omits that for protected
config).
Global file order matches Git: XDG git/config first (when present), then ~/.gitconfig,
unless GIT_CONFIG_GLOBAL is set (single file). When both global files exist, both are
merged so later entries win for duplicate keys.