girt 0.11.0

An incremental, idiomatic Rust implementation of Git
Documentation
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
use std::path::PathBuf;

use super::Config;

/// Precedence class, ordered from lowest to highest.
///
/// Use scope to model precedence among explicitly selected sources, not to grant permissions.
/// Within a scope caller order still matters; user configuration requires XDG before home sources.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[non_exhaustive]
pub enum ConfigScope {
    /// Installation configuration.
    System,
    /// User configuration (XDG first, home second).
    Global,
    /// Common repository configuration.
    Local,
    /// Per-worktree configuration, when enabled by repository bootstrap.
    Worktree,
    /// Explicitly supplied runtime environment pairs.
    Environment,
    /// Caller overrides, applied last.
    Command,
}

/// A file selected by the caller; relative paths use the process working directory at resolution.
///
/// ```
/// use girt::Config;
/// use girt::config::{ConfigFile, ConfigInputs, ConfigScope};
/// let directory = tempfile::tempdir()?;
/// let source = ConfigFile::new(directory.path().join("not-created"), ConfigScope::Global);
/// assert!(Config::resolve(&ConfigInputs::default().with_files(vec![source.clone()])).is_err());
/// // Optional means NotFound is allowed; syntax and permission failures still fail.
/// let optional = source.with_optional(true);
/// let config = Config::resolve(&ConfigInputs::default().with_files(vec![optional]))?;
/// assert!(config.entries().is_empty());
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct ConfigFile {
    /// OS-native source path.
    pub path: PathBuf,
    /// System, global, local or worktree; inherited by recursively included sources.
    /// Environment/command scopes are rejected for files.
    pub scope: ConfigScope,
    /// Ignore NotFound only; permission and parse failures always propagate.
    pub optional: bool,
}

impl ConfigFile {
    /// Creates a required configuration file source; use [`Self::with_optional`] to allow absence.
    pub fn new(path: PathBuf, scope: ConfigScope) -> Self {
        Self {
            path,
            scope,
            optional: false,
        }
    }

    /// Sets [`Self::path`].
    #[must_use]
    pub fn with_path(mut self, path: PathBuf) -> Self {
        self.path = path;
        self
    }

    /// Sets [`Self::scope`].
    #[must_use]
    pub fn with_scope(mut self, scope: ConfigScope) -> Self {
        self.scope = scope;
        self
    }

    /// Sets [`Self::optional`].
    #[must_use]
    pub fn with_optional(mut self, optional: bool) -> Self {
        self.optional = optional;
        self
    }
}

/// A physical variable location. Paths and values are never emitted through tracing.
///
/// Use the path and line in diagnostics or a source editor. Runtime assignments have no file path;
/// source locations do not authorize writing the effective snapshot back to disk.
///
/// ```
/// use girt::Config;
/// use girt::config::{ConfigInputs, ConfigScope};
/// let command = Config::parse(b"[core]\neditor = command-editor\n")?;
/// let resolved = Config::resolve(&ConfigInputs::default().with_command(Some(command)))?;
/// let origin = resolved.entries()[0]
///     .origin
///     .as_ref()
///     .ok_or("origin absent")?;
/// assert_eq!(origin.scope, ConfigScope::Command);
/// assert!(origin.location.path.is_none()); // Supplied bytes have no physical file.
/// assert_eq!(origin.location.line, 2);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SourceLocation {
    /// `None` denotes supplied runtime bytes rather than a file.
    pub path: Option<PathBuf>,
    /// One-based physical line (or environment pair ordinal).
    pub line: usize,
}

/// Provenance of one effective occurrence, including its outermost-first include ancestry.
///
/// Use provenance when explaining why a setting applies, or deciding which direct file a user may
/// edit. Scope is precedence, not trust; a Global source is not automatically safe to execute as a
/// program.
///
/// ```
/// use girt::Config;
/// use girt::config::{ConfigFile, ConfigInputs, ConfigScope};
/// let directory = tempfile::tempdir()?;
/// let root = directory.path().join("config");
/// let included = directory.path().join("editor");
/// std::fs::write(&root, b"[include]\npath = editor\n")?;
/// std::fs::write(&included, b"[core]\neditor = chosen\n")?;
/// let inputs = ConfigInputs::default()
///     .with_files(vec![ConfigFile::new(root.clone(), ConfigScope::Global)]);
/// let config = Config::resolve(&inputs)?;
/// let entry = config
///     .entries()
///     .iter()
///     .find(|entry| entry.name == b"editor")
///     .ok_or("absent")?;
/// let origin = entry.origin.as_ref().ok_or("origin absent")?;
/// assert_eq!(origin.scope, ConfigScope::Global); // Includes inherit their root's scope.
/// assert_eq!(origin.location.path.as_deref(), Some(included.as_path()));
/// assert_eq!(
///     origin.included_from[0].path.as_deref(),
///     Some(root.as_path())
/// );
/// assert_eq!(origin.included_from[0].line, 2); // The physical include directive.
///
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Origin {
    /// Root source precedence class.
    pub scope: ConfigScope,
    /// Location of this occurrence.
    pub location: SourceLocation,
    /// Include directive locations, excluding this occurrence.
    pub included_from: Vec<SourceLocation>,
}

/// Explicit context for path expansion and conditional includes; no ambient environment lookup.
///
/// Supply matching and expansion facts explicitly when configuration depends on branch,
/// Git-directory aliases, home directories, or an installation prefix. These facts are matching
/// inputs, not repository discovery or a trust check.
///
/// ```
/// use girt::Config;
/// use girt::config::{ConfigFile, ConfigInputs, ConfigScope, IncludeContext};
/// let directory = tempfile::tempdir()?;
/// let root = directory.path().join("root");
/// std::fs::write(&root, b"[includeIf \"onbranch:topic/**\"]\npath = topic\n")?;
/// std::fs::write(
///     directory.path().join("topic"),
///     b"[core]\neditor = topic-editor\n",
/// )?;
/// let inputs = ConfigInputs::default()
///     .with_files(vec![ConfigFile::new(root, ConfigScope::Global)])
///     .with_context(IncludeContext::default().with_branch(Some(b"topic/work".to_vec())));
/// let effective = Config::resolve(&inputs)?;
/// assert_eq!(
///     effective.string("core", None, "editor"),
///     Some(&b"topic-editor"[..])
/// );
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct IncludeContext {
    /// Home directory for `~/` expansion.
    pub home: Option<PathBuf>,
    /// Explicit homes for `~user/` expansion, with byte-oriented user names.
    pub user_homes: Vec<(Vec<u8>, PathBuf)>,
    /// Installation prefix for `%(prefix)/` expansion.
    pub prefix: Option<PathBuf>,
    /// Absolute Git directory spellings to match, including logical and canonical aliases.
    pub git_dirs: Vec<PathBuf>,
    /// Short current branch name; absent for detached HEAD or no repository.
    pub branch: Option<Vec<u8>>,
}

impl IncludeContext {
    /// Sets [`Self::home`].
    #[must_use]
    pub fn with_home(mut self, home: Option<PathBuf>) -> Self {
        self.home = home;
        self
    }

    /// Sets [`Self::user_homes`].
    #[must_use]
    pub fn with_user_homes(mut self, user_homes: Vec<(Vec<u8>, PathBuf)>) -> Self {
        self.user_homes = user_homes;
        self
    }

    /// Sets [`Self::prefix`].
    #[must_use]
    pub fn with_prefix(mut self, prefix: Option<PathBuf>) -> Self {
        self.prefix = prefix;
        self
    }

    /// Sets [`Self::git_dirs`].
    #[must_use]
    pub fn with_git_dirs(mut self, git_dirs: Vec<PathBuf>) -> Self {
        self.git_dirs = git_dirs;
        self
    }

    /// Sets [`Self::branch`].
    #[must_use]
    pub fn with_branch(mut self, branch: Option<Vec<u8>>) -> Self {
        self.branch = branch;
        self
    }
}

/// Finite work budgets applied independently to scan and effective expansion.
///
/// ```
/// use girt::config::{ConfigFile, ConfigInputs, ConfigScope, ResolveFailure, ResolveLimits};
/// let directory = tempfile::tempdir()?;
/// let path = directory.path().join("config");
/// std::fs::write(&path, b"[core]\neditor = too-many-bytes\n")?;
/// let inputs = ConfigInputs::default()
///     .with_files(vec![ConfigFile::new(path, ConfigScope::Global)])
///     .with_limits(ResolveLimits::default().with_bytes(4));
/// let error = girt::Config::resolve(&inputs).expect_err("source exceeds selected budget");
/// assert!(matches!(error.source, ResolveFailure::Limit(_)));
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub struct ResolveLimits {
    /// Maximum nested include edges; roots have depth zero. Default: 10.
    pub depth: usize,
    /// Maximum loaded source bytes and, independently, expanded key/value bytes per pass.
    /// Cached file loads count unique path spellings. Independently caps section metadata per
    /// expansion pass: 128 logical bytes per occurrence plus section/subsection name lengths and
    /// 8 per member entry. These deterministic units bound retained membership, not allocator
    /// usage. Repeated include visits and empty headers count separately. Default: 16 MiB.
    pub bytes: usize,
    /// Maximum variable occurrences visited per pass. Default: 100,000.
    pub entries: usize,
    /// Maximum wildcard pattern × candidate work cells per match. Default: 1,000,000.
    pub match_cells: usize,
}

impl ResolveLimits {
    /// Sets [`Self::depth`].
    #[must_use]
    pub fn with_depth(mut self, depth: usize) -> Self {
        self.depth = depth;
        self
    }

    /// Sets [`Self::bytes`].
    #[must_use]
    pub fn with_bytes(mut self, bytes: usize) -> Self {
        self.bytes = bytes;
        self
    }

    /// Sets [`Self::entries`].
    #[must_use]
    pub fn with_entries(mut self, entries: usize) -> Self {
        self.entries = entries;
        self
    }

    /// Sets [`Self::match_cells`].
    #[must_use]
    pub fn with_match_cells(mut self, match_cells: usize) -> Self {
        self.match_cells = match_cells;
        self
    }
}

impl Default for ResolveLimits {
    fn default() -> Self {
        Self {
            depth: 10,
            bytes: 16 * 1024 * 1024,
            entries: 100_000,
            match_cells: 1_000_000,
        }
    }
}

/// Complete inputs for a fresh, read-only configuration snapshot.
///
/// Files are stably ordered by scope, preserving caller order within each scope. Supply XDG before
/// home configuration. Environment pairs follow files; command bytes follow environment pairs.
/// Repeated and empty values remain ordered; reset semantics belong to the consuming variable.
/// Each call rereads sources; an existing snapshot never changes. Concurrent external writers can
/// yield a mixed snapshot across files; no locks, writes, or environment mutation occur.
///
/// ```
/// use girt::Config;
/// use girt::config::ConfigInputs;
/// let inputs = ConfigInputs::default()
///     .with_environment(Some(Config::parse(
///         b"[core]\neditor = environment-editor\n",
///     )?))
///     .with_command(Some(Config::parse(b"[core]\neditor = caller-editor\n")?));
/// let effective = Config::resolve(&inputs)?;
/// assert_eq!(
///     effective.string("core", None, "editor"),
///     Some(&b"caller-editor"[..])
/// );
/// assert_eq!(effective.values("core", None, "editor").count(), 2);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct ConfigInputs {
    /// Explicit system/global/local/worktree files. Optionality belongs to each source.
    pub files: Vec<ConfigFile>,
    /// Matching and expansion context.
    pub context: IncludeContext,
    /// Already parsed environment overrides, e.g. from [`Config::from_environment`].
    pub environment: Option<Config>,
    /// Parsed caller overrides, in occurrence order.
    pub command: Option<Config>,
    /// Resource budgets.
    pub limits: ResolveLimits,
}

impl ConfigInputs {
    /// Sets [`Self::files`].
    #[must_use]
    pub fn with_files(mut self, files: Vec<ConfigFile>) -> Self {
        self.files = files;
        self
    }

    /// Sets [`Self::context`].
    #[must_use]
    pub fn with_context(mut self, context: IncludeContext) -> Self {
        self.context = context;
        self
    }

    /// Sets [`Self::environment`].
    #[must_use]
    pub fn with_environment(mut self, environment: Option<Config>) -> Self {
        self.environment = environment;
        self
    }

    /// Sets [`Self::command`].
    #[must_use]
    pub fn with_command(mut self, command: Option<Config>) -> Self {
        self.command = command;
        self
    }

    /// Sets [`Self::limits`].
    #[must_use]
    pub fn with_limits(mut self, limits: ResolveLimits) -> Self {
        self.limits = limits;
        self
    }
}

impl ConfigInputs {
    /// Selects system/global files and runtime pairs from an explicit environment snapshot.
    ///
    /// `system` is the caller's installation default. `GIT_CONFIG_SYSTEM` replaces it;
    /// `GIT_CONFIG_NOSYSTEM` disables it. `GIT_CONFIG_GLOBAL` replaces both XDG and home files.
    /// Otherwise XDG (`XDG_CONFIG_HOME/git/config`, or `HOME/.config/git/config`) precedes
    /// `HOME/.gitconfig`. Missing files are optional, including environment-selected files.
    /// Repository opening supplies local/worktree files. `GIT_CONFIG` is a Git-config CLI selector
    /// and is deliberately not interpreted. User-home and installation-prefix expansion mappings
    /// remain caller-supplied. No process-global environment is accessed.
    ///
    /// # Errors
    ///
    /// Returns invalid runtime-pair or NOSYSTEM boolean errors. On non-Unix platforms, runtime
    /// pairs must be Unicode; file-selection paths remain OS-native.
    ///
    /// ```
    /// use girt::config::ConfigInputs;
    /// let directory = tempfile::tempdir()?;
    /// let selected = directory.path().join("global");
    /// std::fs::write(&selected, b"[core]\neditor = explicit\n")?;
    /// let inputs = ConfigInputs::from_environment(
    ///     None,
    ///     |name| (name == "GIT_CONFIG_GLOBAL").then(|| selected.clone().into_os_string()),
    ///     4,
    /// )?;
    /// let config = girt::Config::resolve(&inputs)?;
    /// assert_eq!(
    ///     config.string("core", None, "editor"),
    ///     Some(&b"explicit"[..])
    /// );
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn from_environment(
        system: Option<PathBuf>,
        mut get: impl FnMut(&str) -> Option<std::ffi::OsString>,
        max_pairs: usize,
    ) -> Result<Self, super::ConfigError> {
        let mut inputs = Self::default();
        let home = get("HOME").map(PathBuf::from);
        inputs.context.home = home.clone();
        let no_system = get("GIT_CONFIG_NOSYSTEM");
        let no_system = match no_system {
            None => false,
            Some(value) => {
                #[cfg(unix)]
                let bytes = {
                    use std::os::unix::ffi::OsStrExt;
                    value.as_bytes()
                };
                #[cfg(not(unix))]
                let bytes = value
                    .to_str()
                    .ok_or(super::ConfigError {
                        line: 1,
                        reason: "invalid GIT_CONFIG_NOSYSTEM",
                    })?
                    .as_bytes();
                super::boolean(Some(bytes)).ok_or(super::ConfigError {
                    line: 1,
                    reason: "invalid GIT_CONFIG_NOSYSTEM",
                })?
            }
        };
        let system = get("GIT_CONFIG_SYSTEM").map(PathBuf::from).or(system);
        if !no_system && let Some(path) = system {
            inputs.files.push(ConfigFile {
                path,
                scope: ConfigScope::System,
                optional: true,
            });
        }
        if let Some(path) = get("GIT_CONFIG_GLOBAL") {
            inputs.files.push(ConfigFile {
                path: path.into(),
                scope: ConfigScope::Global,
                optional: true,
            });
        } else {
            let xdg = get("XDG_CONFIG_HOME")
                .filter(|s| !s.is_empty())
                .map(PathBuf::from)
                .or_else(|| home.as_ref().map(|home| home.join(".config")));
            if let Some(xdg) = xdg {
                inputs.files.push(ConfigFile {
                    path: xdg.join("git/config"),
                    scope: ConfigScope::Global,
                    optional: true,
                });
            }
            if let Some(home) = home {
                inputs.files.push(ConfigFile {
                    path: home.join(".gitconfig"),
                    scope: ConfigScope::Global,
                    optional: true,
                });
            }
        }
        #[cfg(not(unix))]
        let mut invalid_encoding = false;
        inputs.environment = Some(Config::from_environment(
            |key| {
                get(key).map(|value| {
                    #[cfg(unix)]
                    {
                        use std::os::unix::ffi::OsStringExt;
                        value.into_vec()
                    }
                    #[cfg(not(unix))]
                    {
                        match value.into_string() {
                            Ok(value) => value.into_bytes(),
                            Err(_) => {
                                invalid_encoding = true;
                                Vec::new()
                            }
                        }
                    }
                })
            },
            max_pairs,
        )?);
        #[cfg(not(unix))]
        if invalid_encoding {
            return Err(super::ConfigError {
                line: 1,
                reason: "environment pair is not Unicode",
            });
        }
        Ok(inputs)
    }
}