This document covers bat's Git integration system, which detects and displays file modifications relative to the Git index. The integration enables bat to show visual indicators for added, modified, and removed lines in the terminal gutter.
As of version 0.27.0, bat has transitioned from using libgit2 to the pure-Rust gix (Gitoxide) implementation for all Git operations CHANGELOG.md60
The Git integration system provides the following capabilities:
Added, Modified, or Removed (above/below) src/diff.rs10-16The system is conditionally compiled under the git feature flag Cargo.toml34
The get_git_diff function serves as the entry point for the integration. It utilizes the gix crate to perform a blob-level diff between the index entry and the current file on disk.
Sources: src/diff.rs43-85
| Entity | Description | Location |
|---|---|---|
LineChange | Enum representing the type of modification: Added, RemovedAbove, RemovedBelow, Modified. | src/diff.rs10-16 |
LineChanges | A type alias for HashMap<u32, LineChange>, mapping line numbers to their change state. | src/diff.rs18 |
ResourceKind | Defines whether a resource is the OldOrSource (Index) or NewOrDestination (Worktree). | src/diff.rs66-75 |
bat uses the Histogram diff algorithm src/diff.rs80 It also employs slider heuristics to improve the quality of the diff by shifting hunk boundaries to more natural positions (e.g., around blank lines) src/diff.rs79
To compare the file, bat sets up a diff_resource_cache with Mode::ToGit src/diff.rs52-54
gix should look at the actual file in the worktree src/diff.rs70-77The collect_changes_from_hunks function iterates through diff hunks and populates the LineChanges map:
Sources: src/diff.rs20-41
The results from get_git_diff are passed into the Controller. When the Printer renders a line, it checks the LineChanges map to determine which character to place in the gutter.
| Marker | Change Type |
|---|---|
+ | Added |
~ | Modified |
_ | RemovedBelow |
‾ | RemovedAbove |
Note: These markers are preserved even when --plain is set, provided --diff is explicitly requested CHANGELOG.md13
The src/diff.rs file contains internal tests that initialize temporary Git repositories, commit files, and verify that get_git_diff correctly identifies additions and removals src/diff.rs87-197
The BatTester framework in tests/tester/mod.rs automates Git-based testing:
TempDir tests/tester/mod.rs66gix repository tests/tester/mod.rs67bat binary and compares the output against a snapshot tests/tester/mod.rs24-51get_git_diff returns None src/diff.rs131-132Sources: src/diff.rs43-85 tests/tester/mod.rs64-102 CHANGELOG.md60
Refresh this wiki