Skip to content

Instantly share code, notes, and snippets.

@sh1boot
Created March 7, 2025 05:47
Show Gist options
  • Save sh1boot/9583137b35f909b9b80a86174350120e to your computer and use it in GitHub Desktop.
Save sh1boot/9583137b35f909b9b80a86174350120e to your computer and use it in GitHub Desktop.
Function to get current git hash without external tools.
function githead() {
local commondir ref hash _
local gitdir="${1:a}"
until [[ -e "$gitdir/.git" ]]; do
local updir="${gitdir%/*}"
[[ "$updir" == "$gitdir" ]] && return 1
gitdir="$updir"
done
gitdir="$gitdir/.git"
[[ -f "$gitdir" ]] && read _ gitdir < "$gitdir"
read _ ref < "$gitdir/HEAD"
local commondir="$gitdir/commondir"
[[ -e "$commondir" ]] && gitdir="$gitdir/$(<$commondir)"
ref="$gitdir/$ref"
read hash <"$ref"
print -r "${hash::12}"
return 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment