# api-layout

# grit-lib API layout

Grit’s stable surface is the **canonical domain tree** (≤25 root modules plus [`prelude`](https://docs.rs/grit-lib/latest/grit_lib/prelude/index.html)). Flat implementation modules such as `odb` or `fetch` at the crate root are **private**; use `grit_lib::objects::odb` and `grit_lib::remote::transfer` instead. The map in [`grit-lib/canonical-modules.toml`](https://github.com/gitbutlerapp/grit/blob/main/grit-lib/canonical-modules.toml) lists every previously flat path and its canonical replacement.

For naming rules, semver policy on the path to **0.6.0**, and how to add a public item, see [`grit-lib/API.md`](https://github.com/gitbutlerapp/grit/blob/main/grit-lib/API.md). If you embed against **0.5.x**, see [Migrating to 0.6](https://grit-scm.com/docs/library/api-layout/migrating-to-0.6.md/index.md).

## Domain roots

| Domain | Role |
| --- | --- |
| [`objects`](https://docs.rs/grit-lib/latest/grit_lib/objects/index.html) | Object ids/kinds, ODB, packs, MIDX, hashing, deltas, bitmaps |
| [`refs`](https://docs.rs/grit-lib/latest/grit_lib/refs/index.html) | Ref backends, reflog, refspecs, reftable, name validation |
| [`index`](https://docs.rs/grit-lib/latest/grit_lib/index/index.html) | Index file, sparse checkout, split index, untracked cache |
| [`revwalk`](https://docs.rs/grit-lib/latest/grit_lib/revwalk/index.html) | `rev-parse`, `rev-list`, merge-base, commit-graph |
| [`diff`](https://docs.rs/grit-lib/latest/grit_lib/diff/index.html) | Tree/content diff, patch-ids helpers |
| [`merge`](https://docs.rs/grit-lib/latest/grit_lib/merge/index.html) | Branch merge, pick/revert/replay, rerere, tree merge |
| [`config`](https://docs.rs/grit-lib/latest/grit_lib/config/index.html) | Config cascade, identity, init/bootstrap |
| [`remote`](https://docs.rs/grit-lib/latest/grit_lib/remote/index.html) | Remotes, fetch/push, transport, credentials |
| [`worktree`](https://docs.rs/grit-lib/latest/grit_lib/worktree/index.html) | Working tree, ignore, attributes, pathspec |
| [`stash`](https://docs.rs/grit-lib/latest/grit_lib/stash/index.html) | Stash stack (porcelain types) |
| [`notes`](https://docs.rs/grit-lib/latest/grit_lib/notes/index.html) | Git notes |
| [`bundle`](https://docs.rs/grit-lib/latest/grit_lib/bundle/index.html) | Bundle files and bundle-over-remote |
| [`parallel`](https://docs.rs/grit-lib/latest/grit_lib/parallel/index.html) | Thread policy and parallel helpers (root) |

Cross-cutting entry points (`repo`, `error`, `porcelain`, `progress`, …) remain at the crate root.

## `Repository` handles

Each [`Repository`](https://docs.rs/grit-lib/latest/grit_lib/repo/struct.Repository.html) accessor returns a handle **named like the method** (mirrors [`objects`](https://docs.rs/grit-lib/latest/grit_lib/objects/struct.Objects.html) / [`references`](https://docs.rs/grit-lib/latest/grit_lib/refs/struct.References.html)):

| Accessor | Handle type |
| --- | --- |
| [`index`](https://docs.rs/grit-lib/latest/grit_lib/repo/struct.Repository.html#method.index) | [`IndexFile`](https://docs.rs/grit-lib/latest/grit_lib/index/struct.IndexFile.html) |
| [`revwalk`](https://docs.rs/grit-lib/latest/grit_lib/repo/struct.Repository.html#method.revwalk) | [`RevWalk`](https://docs.rs/grit-lib/latest/grit_lib/revwalk/handle/struct.RevWalk.html) |
| [`remotes`](https://docs.rs/grit-lib/latest/grit_lib/repo/struct.Repository.html#method.remotes) | [`Remotes`](https://docs.rs/grit-lib/latest/grit_lib/repo_handles/type.Remotes.html) |
| [`worktrees`](https://docs.rs/grit-lib/latest/grit_lib/repo/struct.Repository.html#method.worktrees) | [`Worktrees`](https://docs.rs/grit-lib/latest/grit_lib/repo_handles/type.Worktrees.html) |
| [`bundles`](https://docs.rs/grit-lib/latest/grit_lib/repo/struct.Repository.html#method.bundles) | [`Bundles`](https://docs.rs/grit-lib/latest/grit_lib/repo_handles/type.Bundles.html) |
| `snapshot_checkouts` (`unstable-hosting` feature only, outside semver) | `SnapshotCheckouts` |

Use [`references`](https://docs.rs/grit-lib/latest/grit_lib/repo/struct.Repository.html#method.references) for resolve/list/update; the raw backend is [`references().store()`](https://docs.rs/grit-lib/latest/grit_lib/refs/struct.References.html#method.store) (same trait object as [`ref_store`](https://docs.rs/grit-lib/latest/grit_lib/repo/struct.Repository.html#method.ref_store)).

Which area belongs on which accessor, including the accessors the API 0.6 work still has to add, is in the [handle map](https://github.com/gitbutlerapp/grit/blob/main/grit-lib/API.md#handle-map) in `grit-lib/API.md`. The same section covers the other canonical forms: options structs, trailing progress and cancel parameters, and when a free function is allowed.

## Flat → canonical map

The authoritative map lives in [`grit-lib/canonical-modules.toml`](https://github.com/gitbutlerapp/grit/blob/main/grit-lib/canonical-modules.toml). CI checks that every `grit_lib::…` import in `grit-cli`, `grit-protocol`, `grit-examples`, `grit-utils`, `grit-test-support`, and `grit-lib/tests` appears in that file (`scripts/check_canonical_layout.py`, wired from `canonical_paths` integration test). Imports must use the **canonical domain path** (for example `grit_lib::objects::odb`, not the private flat `odb` module); `scripts/check_noncanonical_grit_lib_imports.py` enforces that (`no_noncanonical_grit_lib_imports` in the same integration test, and `scripts/tests/test_canonical_layout.py` in the docs CI job).

Examples:

| Private flat module | Canonical path |
| --- | --- |
| `odb` | `grit_lib::objects::odb` |
| `pack_bitmap` | `grit_lib::objects::bitmap` |
| `bitmap_walk` | `grit_lib::objects::bitmap` |
| `rev_list` | `grit_lib::revwalk::rev_list` |
| `fetch` | internal (fetch runs through `grit_lib::remote::Remote::fetch`; progress types are in `grit_lib::progress`) |
| `ignore` | `grit_lib::worktree::ignore` |
| `pathspec` | `grit_lib::worktree::pathspec` |
| `ident` (not under `config::ident`) | `grit_lib::ident` |
| `sparse_checkout` | `grit_lib::index::sparse_checkout` |
| `reflog` | `grit_lib::refs::reflog` |
| `serve` | `grit_lib::remote::serve` |
| `transfer` | `grit_lib::remote::transfer` |
| `test_support::fixtures` (bench repos; enable `test-support`, outside semver) | *(not in default rustdoc)* |
| `object_inspect` | `grit_lib::revwalk::object_inspect` |
| `quote_path` | `grit_lib::porcelain::quote_path` |
| `pack_reuse` | `grit_lib::objects::pack` |
| `pack_write` | crate-internal (`index_pack` install helpers; no public `objects::pack_write` path) |
| `fsmonitor` | `grit_lib::worktree::fsmonitor` |
| `HookName`, `PostOperationHooks` | `grit_lib::command_runner::hooks` |
| `RefStorageFormat` | `grit_lib::refs::ref_storage` |
| `OidAbbrevBatch` | `grit_lib::revwalk::object_inspect` |
| `PARALLEL_STAT_MIN_ENTRIES` | `grit_lib::worktree` |

Removed **view modules** (use domains above instead): `object_store`, `references`, `worktree_index`, `revision`, `diffing`, `merging`, `configuration`, and the empty `plumbing` scaffold.

Removed **test-only functions** that reproduced Git `test-tool` dumps or printed Git's CLI warnings, with no caller outside `grit-lib`'s own tests:

| Removed | Use instead |
| --- | --- |
| `objects::midx::format_midx_dump`, `format_midx_dump_layer`, `format_midx_show_objects`, `format_midx_show_objects_layer`, `format_midx_bitmapped_packs` | [`read_midx_objects`](https://docs.rs/grit-lib/latest/grit_lib/objects/midx/fn.read_midx_objects.html), [`read_midx_btmp_ranges`](https://docs.rs/grit-lib/latest/grit_lib/objects/midx/fn.read_midx_btmp_ranges.html) |
| `index::index_name_hash_lazy::dump_lazy_init_name_hash`, `test_lazy_init_name_hash`, `memihash_cont` | [`memihash`](https://docs.rs/grit-lib/latest/grit_lib/index/index_name_hash_lazy/fn.memihash.html) |
| `config::gitmodules::write_gitmodules_cli_option_warnings` | [`looks_like_command_line_option`](https://docs.rs/grit-lib/latest/grit_lib/config/gitmodules/fn.looks_like_command_line_option.html) |
| `config::gitmodules::gitdir::die_path_inside_submodule_when_disabled` | [`path_inside_registered_submodule`](https://docs.rs/grit-lib/latest/grit_lib/config/gitmodules/gitdir/fn.path_inside_registered_submodule.html), [`path_inside_indexed_submodule`](https://docs.rs/grit-lib/latest/grit_lib/config/gitmodules/gitdir/fn.path_inside_indexed_submodule.html) |

## Nested implementation modules

Some canonical paths re-export **submodules** of the flat implementation (for example `grit_lib::objects::odb::store` is the public path for the object-store backend). Prefer the canonical prefix in all new code.
