1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
#!/bin/sh
if test $# -ne 1; then
cat >>/dev/stderr <<EOF
Usage:
$0 <rutdir>
Use a heuristic to determine the branch name of the current detached
head.
EOF
exit 1
fi
rutdir=$1 ; shift
# Easy way, see what branch HEAD is on.
branch=$(git -C ${rutdir} rev-parse --abbrev-ref HEAD)
if test ${branch} != HEAD ; then
echo ${branch}
exit 0
fi
# Hard way, follow checkouts until one hits a branch.
git -C ${rutdir} reflog | awk '
BEGIN {
found = ""
}
found == "" && $3 == "checkout:" && (sha == "" || sha == $1 ) {
from = $(NF - 2)
to = $NF
# print "checkout:", "from=" from, "to=" to, "length(from)=" length(from) >> "/dev/stderr"
if (length(from) != 40) {
found = from
# print "found=" found >> "/dev/stderr"
} else {
sha=substr(from, 1, 7)
# print "sha=" sha >> "/dev/stderr"
}
}
END {
if (found) {
print found
} else {
exit 1
}
}
'
|