# Config

> Merged config reads and scoped writes through Repository::config and ConfigEditor.

Configuration in grit-lib follows Git’s cascade (system, global, local, worktree, and environment overrides). Use [`Repository::config`](https://docs.rs/grit-lib/latest/grit_lib/repo/struct.Repository.html#method.config) for reads and for writes that target a scope in an open repository. For global, system, or `-f` file edits outside a repository, use [`ConfigEditor`](https://docs.rs/grit-lib/latest/grit_lib/config/struct.ConfigEditor.html).

## Loading outside a handle

[`ConfigSet::load`](https://docs.rs/grit-lib/latest/grit_lib/config/struct.ConfigSet.html) parses the cascade from disk given an [`Environment`](https://docs.rs/grit-lib/latest/grit_lib/config/environment/struct.Environment.html) and optional git directory. The library does not read `std::env` during load — embedders pass variables explicitly (see [Embedding](https://grit-scm.com/docs/library/embedding/index.md) and [Repository](https://grit-scm.com/docs/library/repository/index.md)).

## Typed values

[`ConfigSet::get_bool`](https://docs.rs/grit-lib/latest/grit_lib/config/struct.ConfigSet.html#method.get_bool) and [`ConfigSet::get_i64`](https://docs.rs/grit-lib/latest/grit_lib/config/struct.ConfigSet.html#method.get_i64) return `None` for an unset key. A value of the wrong type is a [`ConfigError::InvalidValue`](https://docs.rs/grit-lib/latest/grit_lib/error/enum.ConfigError.html) carrying the key and a [`ConfigValueError`](https://docs.rs/grit-lib/latest/grit_lib/error/enum.ConfigValueError.html) that says what was wrong: not a boolean, an empty or malformed integer, an integer that overflows `i64` once its `k`/`m`/`g` suffix is applied, or a bad color. `get_bool(key).transpose()?` reads an optional boolean and propagates a bad value as `grit_lib::error::Error::Config`. The key-less parsers [`parse_bool`](https://docs.rs/grit-lib/latest/grit_lib/config/fn.parse_bool.html), [`parse_i64`](https://docs.rs/grit-lib/latest/grit_lib/config/fn.parse_i64.html) and [`parse_color`](https://docs.rs/grit-lib/latest/grit_lib/config/fn.parse_color.html) return the bare `ConfigValueError`; call `for_key` on it to attach the key.

For a setting with a Git default, apply it to the unset case: `config.get_bool("commit.autoStage")?.unwrap_or(true)`. The `core` booleans that many operations consult have accessors carrying Git's default: [`ignore_case`](https://docs.rs/grit-lib/latest/grit_lib/config/struct.ConfigSet.html#method.ignore_case) and [`precompose_unicode`](https://docs.rs/grit-lib/latest/grit_lib/config/struct.ConfigSet.html#method.precompose_unicode) (both `false`), and [`preload_index`](https://docs.rs/grit-lib/latest/grit_lib/config/struct.ConfigSet.html#method.preload_index) and [`file_mode`](https://docs.rs/grit-lib/latest/grit_lib/config/struct.ConfigSet.html#method.file_mode) (both `true`).

## Errors

Every config failure is a specific [`ConfigError`](https://docs.rs/grit-lib/latest/grit_lib/error/enum.ConfigError.html) variant with the offending key or value in its fields, so callers match on the variant instead of the message. For example, [`canonical_key`](https://docs.rs/grit-lib/latest/grit_lib/config/fn.canonical_key.html) returns `ConfigError::InvalidKey` with an [`InvalidKeyReason`](https://docs.rs/grit-lib/latest/grit_lib/error/enum.InvalidKeyReason.html) naming the malformed part. Bad `GIT_CONFIG_COUNT` or `GIT_CONFIG_PARAMETERS` input is `InvalidEnvironmentCount`, `MissingEnvironmentKey` or `MalformedConfigParameters`. A value outside a key's fixed word list (`protocol.version`, `gpg.format`) is `UnknownValue`, and a `--local` or `--worktree` location without a repository is `RepositoryRequired`. [`ConfigSet::get_regexp`](https://docs.rs/grit-lib/latest/grit_lib/config/struct.ConfigSet.html#method.get_regexp) returns `ConfigError::InvalidPattern` when the pattern is not valid regex. [`validate_repo_config`](https://docs.rs/grit-lib/latest/grit_lib/repo/fn.validate_repo_config.html) returns `ConfigError::UnsupportedRepositoryFormatVersion` when `core.repositoryformatversion` is not supported. Submodule git-dir layout problems are not config errors. They are reported as `Error::Submodule` with a [`SubmoduleError`](https://docs.rs/grit-lib/latest/grit_lib/error/enum.SubmoduleError.html).

## Writes

Pass a [`ConfigScope`](https://docs.rs/grit-lib/latest/grit_lib/config/enum.ConfigScope.html) (`Local`, `Worktree`, `Global`, or `System`) and [`ConfigWriteOptions`](https://docs.rs/grit-lib/latest/grit_lib/config/struct.ConfigWriteOptions.html) to [`Config::set`](https://docs.rs/grit-lib/latest/grit_lib/config/struct.Config.html#method.set), [`Config::add_value`](https://docs.rs/grit-lib/latest/grit_lib/config/struct.Config.html#method.add_value), [`Config::unset`](https://docs.rs/grit-lib/latest/grit_lib/config/struct.Config.html#method.unset), [`Config::unset_all`](https://docs.rs/grit-lib/latest/grit_lib/config/struct.Config.html#method.unset_all), [`Config::rename_section`](https://docs.rs/grit-lib/latest/grit_lib/config/struct.Config.html#method.rename_section), or [`Config::remove_section`](https://docs.rs/grit-lib/latest/grit_lib/config/struct.Config.html#method.remove_section). Successful writes reload the in-memory snapshot on the open `Repository`. `ConfigWriteOptions` carries dry-run mode, optional `--type` canonicalization, Git-style `replace-all` with an optional value pattern, and unset value patterns instead of bare boolean flags.

---
title: Config
summary: Merged config reads and scoped writes through Repository::config and ConfigEditor.
---

Configuration in grit-lib follows Git’s cascade (system, global, local, worktree, and environment overrides). Use [`Repository::config`](https://docs.rs/grit-lib/latest/grit_lib/repo/struct.Repository.html#method.config) for reads and for writes that target a scope in an open repository. For global, system, or `-f` file edits outside a repository, use [`ConfigEditor`](https://docs.rs/grit-lib/latest/grit_lib/config/struct.ConfigEditor.html).

## Loading outside a handle

[`ConfigSet::load`](https://docs.rs/grit-lib/latest/grit_lib/config/struct.ConfigSet.html) parses the cascade from disk given an [`Environment`](https://docs.rs/grit-lib/latest/grit_lib/config/environment/struct.Environment.html) and optional git directory. The library does not read `std::env` during load — embedders pass variables explicitly (see [Embedding](https://grit-scm.com/docs/library/embedding/index.md) and [Repository](https://grit-scm.com/docs/library/repository/index.md)).

## Typed values

[`ConfigSet::get_bool`](https://docs.rs/grit-lib/latest/grit_lib/config/struct.ConfigSet.html#method.get_bool) and [`ConfigSet::get_i64`](https://docs.rs/grit-lib/latest/grit_lib/config/struct.ConfigSet.html#method.get_i64) return `None` for an unset key. A value of the wrong type is a [`ConfigError::InvalidValue`](https://docs.rs/grit-lib/latest/grit_lib/error/enum.ConfigError.html) carrying the key and a [`ConfigValueError`](https://docs.rs/grit-lib/latest/grit_lib/error/enum.ConfigValueError.html) that says what was wrong: not a boolean, an empty or malformed integer, an integer that overflows `i64` once its `k`/`m`/`g` suffix is applied, or a bad color. `get_bool(key).transpose()?` reads an optional boolean and propagates a bad value as `grit_lib::error::Error::Config`. The key-less parsers [`parse_bool`](https://docs.rs/grit-lib/latest/grit_lib/config/fn.parse_bool.html), [`parse_i64`](https://docs.rs/grit-lib/latest/grit_lib/config/fn.parse_i64.html) and [`parse_color`](https://docs.rs/grit-lib/latest/grit_lib/config/fn.parse_color.html) return the bare `ConfigValueError`; call `for_key` on it to attach the key.

For a setting with a Git default, apply it to the unset case: `config.get_bool("commit.autoStage")?.unwrap_or(true)`. The `core` booleans that many operations consult have accessors carrying Git's default: [`ignore_case`](https://docs.rs/grit-lib/latest/grit_lib/config/struct.ConfigSet.html#method.ignore_case) and [`precompose_unicode`](https://docs.rs/grit-lib/latest/grit_lib/config/struct.ConfigSet.html#method.precompose_unicode) (both `false`), and [`preload_index`](https://docs.rs/grit-lib/latest/grit_lib/config/struct.ConfigSet.html#method.preload_index) and [`file_mode`](https://docs.rs/grit-lib/latest/grit_lib/config/struct.ConfigSet.html#method.file_mode) (both `true`).

## Errors

Every config failure is a specific [`ConfigError`](https://docs.rs/grit-lib/latest/grit_lib/error/enum.ConfigError.html) variant with the offending key or value in its fields, so callers match on the variant instead of the message. For example, [`canonical_key`](https://docs.rs/grit-lib/latest/grit_lib/config/fn.canonical_key.html) returns `ConfigError::InvalidKey` with an [`InvalidKeyReason`](https://docs.rs/grit-lib/latest/grit_lib/error/enum.InvalidKeyReason.html) naming the malformed part. Bad `GIT_CONFIG_COUNT` or `GIT_CONFIG_PARAMETERS` input is `InvalidEnvironmentCount`, `MissingEnvironmentKey` or `MalformedConfigParameters`. A value outside a key's fixed word list (`protocol.version`, `gpg.format`) is `UnknownValue`, and a `--local` or `--worktree` location without a repository is `RepositoryRequired`. Submodule git-dir layout problems are not config errors. They are reported as `Error::Submodule` with a [`SubmoduleError`](https://docs.rs/grit-lib/latest/grit_lib/error/enum.SubmoduleError.html).

## Writes

Pass a [`ConfigScope`](https://docs.rs/grit-lib/latest/grit_lib/config/enum.ConfigScope.html) (`Local`, `Worktree`, `Global`, or `System`) and [`ConfigWriteOptions`](https://docs.rs/grit-lib/latest/grit_lib/config/struct.ConfigWriteOptions.html) to [`Config::set`](https://docs.rs/grit-lib/latest/grit_lib/config/struct.Config.html#method.set), [`Config::add_value`](https://docs.rs/grit-lib/latest/grit_lib/config/struct.Config.html#method.add_value), [`Config::unset`](https://docs.rs/grit-lib/latest/grit_lib/config/struct.Config.html#method.unset), [`Config::unset_all`](https://docs.rs/grit-lib/latest/grit_lib/config/struct.Config.html#method.unset_all), [`Config::rename_section`](https://docs.rs/grit-lib/latest/grit_lib/config/struct.Config.html#method.rename_section), or [`Config::remove_section`](https://docs.rs/grit-lib/latest/grit_lib/config/struct.Config.html#method.remove_section). Successful writes reload the in-memory snapshot on the open `Repository`. `ConfigWriteOptions` carries dry-run mode, optional `--type` canonicalization, Git-style `replace-all` with an optional value pattern, and unset value patterns instead of bare boolean flags.

Without an open `Repository`, [`ConfigEditor::global`](https://docs.rs/grit-lib/latest/grit_lib/config/struct.ConfigEditor.html#method.global), [`ConfigEditor::system`](https://docs.rs/grit-lib/latest/grit_lib/config/struct.ConfigEditor.html#method.system), or [`ConfigEditor::open`](https://docs.rs/grit-lib/latest/grit_lib/config/struct.ConfigEditor.html#method.open) edit one file. For local or worktree config tied to a path you discover yourself, call [`Repository::discover`](https://docs.rs/grit-lib/latest/grit_lib/repo/struct.Repository.html) (or [`Repository::open`](https://docs.rs/grit-lib/latest/grit_lib/repo/struct.Repository.html) when you already know the git directory) and use [`Repository::config`](https://docs.rs/grit-lib/latest/grit_lib/repo/struct.Repository.html#method.config) with the right [`ConfigScope`](https://docs.rs/grit-lib/latest/grit_lib/config/enum.ConfigScope.html) (`Local` or `Worktree`). The `grit config` command follows the same split: it edits with `ConfigEditor` when there is no repository, and `Repository::config` when one is open.

Writes go through `config.lock` and an atomic rename and keep the file's permission bits. Like Git, they do not fsync the file, so a crash may lose the update, but readers never see a partial file.

## Example

```rust
//! Read and write repository-local config through [`Repository::config`](https://grit-scm.com/docs/library/config/grit_lib::repo::Repository::config/index.md)
//! ([`grit_lib::config::Config`]).
//!
//! Source for the library guide "Config" page (included in the docs site).

use anyhow::Result;
use grit_lib::config::environment::RepositoryOptions;
use grit_lib::repo::Repository;
use std::path::{Path, PathBuf};

fn open_repo(root: &Path) -> Result<Repository> {
    let git_dir = if root.join(".git").is_dir() {
        root.join(".git")
    } else {
        root.to_path_buf()
    };
    let work_tree = if root.join(".git").is_dir() {
        Some(root)
    } else {
        None
    };
    let mut opts = RepositoryOptions::from_process();
    if let Some(wt) = work_tree {
        opts = opts.with_work_tree(wt);
    }
    Ok(Repository::open(&git_dir, &opts)?)
}

fn main() -> Result<()> {
    let root = std::env::args()
        .nth(1)
        .map(PathBuf::from)
        .or_else(|| std::env::current_dir().ok())
        .ok_or_else(|| anyhow::anyhow!("missing repository path"))?;
    let repo = open_repo(&root)?;

    repo.config().set(
        grit_lib::config::ConfigScope::Local,
        "user.name",
        "Library Guide Config",
        &grit_lib::config::ConfigWriteOptions::new(),
    )?;

    let name = repo.config().get("user.name")?.unwrap_or_default();
    println!("user.name={name}");

    let format = repo
        .config()
        .get("core.repositoryformatversion")?
        .unwrap_or_default();
    println!("core.repositoryformatversion={format}");

    Ok(())
}
```

```bash
cargo run --bin guide_config /path/to/repo
git -C /path/to/repo config user.name
```

The integration test checks the printed `user.name` against `git config user.name`.

Writes go through `config.lock` and an atomic rename and keep the file's permission bits. Like Git, they do not fsync the file, so a crash may lose the update, but readers never see a partial file.

## Example

```rust
//! Read and write repository-local config through [`Repository::config`](https://grit-scm.com/docs/library/config/grit_lib::repo::Repository::config/index.md)
//! ([`grit_lib::config::Config`]).
//!
//! Source for the library guide "Config" page (included in the docs site).

use anyhow::Result;
use grit_lib::config::environment::RepositoryOptions;
use grit_lib::repo::Repository;
use std::path::{Path, PathBuf};

fn open_repo(root: &Path) -> Result<Repository> {
    let git_dir = if root.join(".git").is_dir() {
        root.join(".git")
    } else {
        root.to_path_buf()
    };
    let work_tree = if root.join(".git").is_dir() {
        Some(root)
    } else {
        None
    };
    let mut opts = RepositoryOptions::from_process();
    if let Some(wt) = work_tree {
        opts = opts.with_work_tree(wt);
    }
    Ok(Repository::open(&git_dir, &opts)?)
}

fn main() -> Result<()> {
    let root = std::env::args()
        .nth(1)
        .map(PathBuf::from)
        .or_else(|| std::env::current_dir().ok())
        .ok_or_else(|| anyhow::anyhow!("missing repository path"))?;
    let repo = open_repo(&root)?;

    repo.config().set(
        grit_lib::config::ConfigScope::Local,
        "user.name",
        "Library Guide Config",
        &grit_lib::config::ConfigWriteOptions::new(),
    )?;

    let name = repo.config().get("user.name")?.unwrap_or_default();
    println!("user.name={name}");

    let format = repo
        .config()
        .get("core.repositoryformatversion")?
        .unwrap_or_default();
    println!("core.repositoryformatversion={format}");

    Ok(())
}
```

```bash
cargo run --bin guide_config /path/to/repo
git -C /path/to/repo config user.name
```

The integration test checks the printed `user.name` against `git config user.name`.
