paulirish.git-recent/git-recent
2019-10-11 12:32:45 -07:00

74 lines
1.9 KiB
Bash
Executable file

#!/bin/bash
##
## git-recent
##
## list all local branches, sorted by last commit, formatted reall purdy
##
# Windows needs more basic format (#8, git-for-windows/git#865)
case $(uname -s) in
CYGWIN*|MINGW*|MSYS*)
branch='%(refname:short)'
spacer=' '
;;
*)
branch='%(color:yellow)%(refname:short)%(color:reset)'
spacer='%(color:black) %(color:reset)'
;;
esac
while getopts "n:" opt; do
case ${opt} in
n )
if ! [[ $OPTARG =~ ^[0-9]{1,}$ ]]; then
echo "-n should be an integer."
exit 1
fi
COUNT=${OPTARG}
shift
;;
esac
done
shift $((OPTIND-1))
## dont collect upstream details ([ahead 2, behind 54]) for big-ass git repos, it adds >5s.
# TODO, use `git count-objects -v` and consider `in-pack` and `size-pack` instead of special casing chromium
if git config --get remote.origin.url | grep -q chromium.googlesource.com; then
upstream_deets=""
else
upstream_deets="%(color:yellow)%(upstream:track)%(color:reset)"
fi
format="\
%(HEAD) \
$branch|\
%(color:bold red)%(objectname:short)%(color:reset) \
%(color:bold green)(%(committerdate:relative))%(color:reset) \
%(color:bold blue)%(authorname)%(color:reset) \
$upstream_deets
$spacer|\
%(contents:subject)
$spacer|"
lessopts="--tabs=4 --quit-if-one-screen --RAW-CONTROL-CHARS --no-init"
git for-each-ref \
--color=always \
--count=$COUNT \
--sort=-committerdate \
"refs/heads/" \
--format="$format" \
| column -ts '|' \
| less "$lessopts"
# The above command:
# for all known branches,
# (force coloring on this, especially since it's being piped)
# optionally, specify the number of branches you want to display
# sort descending by last commit
# show local branches (change to "" to include both local + remote branches)
# apply the formatting template above
# break into columns
# use the pager only if there's not enough space