Created
March 7, 2025 05:47
-
-
Save sh1boot/9583137b35f909b9b80a86174350120e to your computer and use it in GitHub Desktop.
Function to get current git hash without external tools.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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