Git helpers for Bash commands that need lightweight repository inspection or update behavior.
Source lib/bash/std/lib_std.sh before this library so logging and shared error handling are available.
git_update_repo <repo> [allowed_dirty_path] [expected_branch]Update a repository on its detected default branch, optionally allowing tracked changes in one specific path.git_get_current_branch <directory> <result_var>Return the current branch name through a caller-provided variable, ordetached head.git_detect_default_branch <repo> <result_var>Detect a repository's default branch from its remote HEAD and standard local fallbacks.git_worktree_path_for_branch <branch> [repo]Print the worktree path attached to a local branch.git_list_worktree_branches [repo]Print tab-separated worktree path and branch rows.git_branch_upstream <repo> <branch>Print the configured upstream ref for a local branch.git_branch_merged_to_ref <repo> <branch> <ref>Check whether a local branch is an ancestor of a ref.git_list_remote_branches [repo]Print branch names from theoriginremote.check_script_up_to_date [--fetch] <script>Check whether a tracked script appears current relative to its configured upstream.
The __git_*__ functions used by git_update_repo are implementation details
and are not part of the public API. In particular, the path-dirty predicate
checks whether tracked changes stay within an allowed path, while the update
helpers manage branch selection, retries, and cleanup.
source "/absolute/path/to/lib/bash/std/lib_std.sh"
source "/absolute/path/to/lib/bash/git/lib_git.sh"
branch=""
git_get_current_branch "$PWD" branch
log_info "Current branch: $branch"git_update_repoonly attempts updates when the checked-out branch is the detected default branch, or an explicit expected branch passed by the caller.git_update_reporetriesgit pull --ff-onlytwice by default. SetBASE_GIT_PULL_MAX_ATTEMPTSto a positive integer to change the retry count.git_get_current_branchusesgit -Cso it does not change the caller's working directory or directory stack. Missing directories and non-Git directories return success with an empty result variable.git_update_repochanges into the target repository while it runs because its submodule update sequence depends on repository-relative execution.git_update_repoonly treats an allowed dirty path as safe when every tracked change stays within that path. Rename records must have both source and destination inside the allowed path.check_script_up_to_datetreats missing git state, untracked scripts, or missing upstreams as skip conditions rather than hard failures.check_script_up_to_date <script>comparesHEADwith the local remote-tracking upstream ref. It does not fetch by default, so the result reflects the freshness of local refs.check_script_up_to_date --fetch <script>runsgit fetch --quietfirst, then compares against the refreshed upstream ref. If fetch fails, the helper logs a warning and falls back to local remote-tracking refs.check_script_up_to_datereturns2when the repository is behind upstream, and3when the script has local modifications. If both are true, local modifications take precedence and the helper returns3after logging both conditions.
BATS coverage lives in lib/bash/git/tests/lib_git.bats.