Summarize git repo info into shell variables
This is designed to replace multiple calls to git
with a single use of
eval $(git-status-vars)
. It’s especially useful for generating a shell prompt.
This is intended to be generally usable in any theme that wants to report git
information in any shell with sh
-like strings. I use it in my personal ZSH
theme.
Installation
You can download binaries from the GitHub releases page. Just
extract them and copy the file inside into your $PATH
, e.g. /usr/local/bin
.
The most common ones are:
- Linux: x86-64, ARM
- macOS: Intel, Apple silicon
- Windows on x86-64
If you have cargo
, you can just do cargo install git-status-vars
to install
from source, or if you’ve installed cargo binstall
you can use
that (cargo binstall git-status-vars
).
Usage
if ; then
fi
This outputs a bunch of sh
compatible environment variables about the current
repository. The repository is found by looking at each of the following in order
and taking the first that matches:
- Command line parameter. A repository directory, or a subdirectory of a repository, may be passed on the command line.
- The
$GIT_DIR
environment variable, just likegit
. - A
.git
directory in the working directory or one of its parents.
git-status-vars
will always output repo_state=
, but all other variables may
be left out. In particular, if it can’t find a repository, it will output only
repo_state=NotFound
.
Example prompt function with git-status-vars
Equivalent prompt function without git-status-vars
Typical output
~/projects/git-status-vars ❯ git-status-vars
repo_state=Clean
repo_workdir=/Users/daniel/projects/git-status-vars/
repo_empty=false
repo_bare=false
head_ref_length=1
head_ref1_name=refs/heads/main
head_ref1_short=main
head_ref1_kind=direct
head_ref1_error=''
head_hash=2df6b768e60fbf899d8c8dc4a20385f30ee5da24
head_ahead=0
head_behind=0
head_upstream_error=''
untracked_count=0
unstaged_count=0
staged_count=0
conflicted_count=0
~/projects/git-status-vars ❯ cd /
/ ❯ git-status-vars
repo_state=NotFound
Performance
git-status-vars
is generally faster than multiple calls to git
, though git
is fast enough that the difference will not usually be perceptible. On my laptop
git-status-vars
typically runs in around 8 ms whereas the fallback code
involving multiple calls to git
takes around 25 ms.
I have not tested this on large repositories.
Rust Crate
I’m not sure how useful it is, but this may be used from other Rust code.
Currently the minimum supported Rust version (MSRV) is 1.64.
Development and contributions
See DEVELOPMENT.md for notes on contributing to this repo, the license, how changes are tracked, and how releases are made.
Development status
This is stable. I don’t have plans for additional features, but if you have ideas please either submit an issue or a pull request.
I will periodically update this to ensure that it doesn’t bit rot as dependencies are updated, but you should not expect active development.