Skip to content

Instantly share code, notes, and snippets.

@Osse
Created November 29, 2014 13:05
Show Gist options
  • Save Osse/c2581df6c9f5a439df93 to your computer and use it in GitHub Desktop.
Save Osse/c2581df6c9f5a439df93 to your computer and use it in GitHub Desktop.
#!/bin/bash
green=$(tput setaf 2)
yellow=$(tput setaf 3)
blue=$(tput setaf 12)
reset=$(tput sgr0)
cols=$(tput cols)
# Associative array of branches and upstreams. The keys are the local branches
# and the values are their upstreams (if any)
eval "branches=($(git for-each-ref --shell --format='%(refname:short)' 'refs/heads/*'))"
eval "upstreams=($(git for-each-ref --shell --format='%(upstream:short)' 'refs/heads/*'))"
declare -A dates messages hashes order
i=0
while b=${branches[i]}; IFS=$'\t' read -r date hash message; do
dates[$b]=$date
hashes[$b]=$hash
messages[$b]=$message
((i++))
done < <(git log --no-walk --format='%at%x09%h%x09%s' "${branches[@]}" | sort -n)
max=$i
current=$(git symbolic-ref -q --short HEAD)
for ((i = 0; i < max; i++)); do
blen=${#branches[i]}
ulen=${#upstreams[i]}
(( blongest = blen > blongest ? blen : blongest ))
(( ulongest = ulen > ulongest ? ulen : ulongest ))
done
# Max length of message = width - Length of YYYY-MM-DD * master origin/master hash
(( n = cols - (23 + blongest + ulongest) ))
for ((i = 0; i < max; i++)); do
b=${branches[i]}
u=${upstreams[i]}
bfmt='%1s %s%-*s%s'
[[ $b = $current ]] && mark=* || mark=
bargs=( "$mark" "$green" "$blongest" "$b" "$reset" )
ufmt='%s%-*s%s'
uargs=( "$blue" "$ulongest" "$u" "$reset" )
hfmt='%s%s%s'
hargs=( "$yellow" "${hashes[$b]}" "$reset" )
m=${messages[$b]:0:$n}
printf "%(%Y-%m-%d)T $bfmt $ufmt $hfmt %s\n" "${dates[$b]}" "${bargs[@]}" "${uargs[@]}" "${hargs[@]}" "$m"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment