LinuxCommandLibrary

git-sh-setup

Setup Git environment for shell scripts

SYNOPSIS

. $(git --exec-path)/git-sh-setup
or
source git-sh-setup

DESCRIPTION

The git-sh-setup is an internal Bourne shell library script used extensively in Git's porcelain commands implemented as shell scripts. It provides standardized functions for error handling, usage messages, environment setup, and common utilities, ensuring consistency across Git's command-line interface.

Sourced via . git-sh-setup or source git-sh-setup at the beginning of Git scripts, it defines essential functions like die() for fatal errors with custom messages, usage() for displaying syntax help and exiting, require_work_tree to ensure a valid work tree exists, and test_toplevel for repository root detection. It also sets up key variables such as GIT_DIR, GIT_OBJECT_DIRECTORY, and handles path normalization.

This script abstracts repetitive boilerplate, allowing Git developers to focus on command logic. It checks for required tools like sed, grep, and enforces Git-specific conventions. Not intended for direct execution, running it standalone typically results in errors or minimal output due to unmet expectations. Integral to Git's extensibility, it's located in Git's exec-path, often $GIT_EXEC_PATH/git-sh-setup.

CAVEATS

Not designed for direct execution; must be sourced in a Git shell script. Assumes Git environment and may fail if sourced improperly. No command-line options; relies on pre-parsed variables.

KEY FUNCTIONS

die: Exit with error message.
usage: Print usage and exit.
require_work_tree: Ensure work tree exists.
sanity_check_ref: Validate refs.

LOCATION

Typically at $(git --exec-path)/git-sh-setup.

HISTORY

Developed by Linus Torvalds and Git contributors since Git's inception in 2005. Evolved from early shell scripts in Git 1.0, refined over versions for better portability and function coverage. Still actively maintained in modern Git releases.

SEE ALSO

git(1), sh(1), dash(1)

Copied to clipboard