summaryrefslogtreecommitdiffstats
path: root/git-precheck
diff options
context:
space:
mode:
authorMatt Hunter <m@lfurio.us>2026-02-22 18:22:54 -0500
committerMatt Hunter <m@lfurio.us>2026-02-22 18:22:54 -0500
commitdef77dc0d6a0755849ad6593b744e7e5546794db (patch)
treed464e700718f28fc60d43ebdb5147e3a3ff00c1c /git-precheck
parent0e9f8c84a7f8c96b63837361c6215eef8123034a (diff)
parent8fc0fa9a8cf84d0d2c7ac5f4e3b4c79fadb99d8c (diff)
downloadgit-sonar-def77dc0d6a0755849ad6593b744e7e5546794db.tar.gz
git-sonar-def77dc0d6a0755849ad6593b744e7e5546794db.zip
Merge branch 'precheck'
Provide an additional entrypoint 'git-precheck', a new script intended for use by other git-related scripts to determine the initial state of a repository before continuing to do additional work. git-sonar now leverages some of the information returned by git-precheck to update its "is inside git repository" check, as well as provide a new prompt string feature which visualizes on the prompt whether a merge, rebase, etc. is in progress. * precheck: Add new prompt output element "condition" Use git-precheck to determine if we are inside a repository Add git-precheck script
Diffstat (limited to 'git-precheck')
-rwxr-xr-xgit-precheck94
1 files changed, 94 insertions, 0 deletions
diff --git a/git-precheck b/git-precheck
new file mode 100755
index 0000000..9984bbb
--- /dev/null
+++ b/git-precheck
@@ -0,0 +1,94 @@
+#!/bin/sh
+
+usage() {
+ echo "git-precheck [--quiet] [--ignore-dirty] [--ignore-untracked]"
+ echo ""
+ echo "If the current working directory is inside a git repository, examine"
+ echo "the repo for any abnormal state and return an exit code indicating"
+ echo "the status. If unclean, print a line of text describing the"
+ echo "condition."
+ echo ""
+ echo " --quiet"
+ echo " Don't actually print anything."
+ echo ""
+ echo " --ignore-dirty"
+ echo " Don't consider the presence of uncommitted changes to tracked"
+ echo " files as an abnormal state."
+ echo ""
+ echo " --ignore-untracked"
+ echo " Don't consider the presence of untracked files as an abnormal"
+ echo " state."
+ echo ""
+ echo " Exit codes:"
+ echo " 0 If inside a repository and all checks return normal"
+ echo " 1 If untracked files detected"
+ echo " 2 If dirty/modified files detected"
+ echo " 3 If any ongoing git operation is in progress"
+ echo " 4 If not inside a git repository"
+ echo ""
+ echo " Exit any other value on error or if 'precheck' operation is"
+ echo " not completed, such as when viewing this help text."
+ exit 128
+}
+
+quiet=""
+ignore_dirty=""
+ignore_untracked=""
+
+while true; do
+ case "$1" in
+ --quiet) quiet="true" ;;
+ --ignore-dirty) ignore_dirty="true" ;;
+ --ignore-untracked) ignore_untracked="true" ;;
+ --help) usage ;;
+ -h) usage ;;
+ *) break
+ esac
+ shift
+done
+
+if [ $# -ne 0 ]; then
+ printf 'precheck: Unrecognized option given: %s\n' "$1"
+ exit 128
+fi
+
+check() {
+ if git rev-parse --verify "$1" >/dev/null 2>&1; then
+ [ -z "$quiet" ] && printf '\e[0;31m%s in progress\e[0m\n' "$2"
+ exit 3
+ fi
+}
+
+if ! [ "$(git rev-parse --is-inside-work-tree 2>/dev/null)" = "true" ]; then
+ [ -z "$quiet" ] && printf '\e[0;31mNot inside a git work tree\e[0m\n'
+ exit 4
+fi
+
+check MERGE_HEAD Merge
+check REBASE_HEAD Rebase
+check REVERT_HEAD Revert
+check CHERRY_PICK_HEAD Cherry-pick
+check BISECT_EXPECTED_REV Bisect
+
+if [ -e "$(git rev-parse --git-path rebase-apply 2>/dev/null)" ]; then
+ [ -z "$quiet" ] && printf '\e[0;31mrebase-apply (am) found\e[0m\n'
+ exit 3
+fi
+
+if [ -z "$ignore_dirty" ]; then
+ if git status --porcelain 2>/dev/null \
+ | grep --quiet --invert-match '^??'; then
+ [ -z "$quiet" ] && printf '\e[0;31mModified files detected\e[0m\n'
+ exit 2
+ fi
+fi
+
+if [ -z "$ignore_untracked" ]; then
+ if git status --porcelain 2>/dev/null \
+ | grep --quiet '^??'; then
+ [ -z "$quiet" ] && printf '\e[0;31mUntracked files detected\e[0m\n'
+ exit 1
+ fi
+fi
+
+exit 0