paulirish.git-recent/git-recent
2020-03-20 20:09:53 +01:00

90 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
function ensure_integer() {
if ! [[ "$1" =~ ^[1-9][0-9]*$ ]]; then
echo "error: -n requires an integer argument."
exit 1
fi
}
function usage() {
echo "Usage: git recent [-n <num> | -<num>]"
echo " List latest local git branches, formatted real fancy."
}
COUNT=0
while [[ $# -gt 0 ]]; do
case "$1" in
-n )
COUNT="$2"
ensure_integer "$COUNT"
shift
;;
-[1-9]* | -n[1-9]* )
COUNT="${1#-}"
COUNT="${COUNT#n}"
ensure_integer "$COUNT"
;;
-h | --h* )
usage
exit 0
;;
* )
echo "error: unknown argument '$1'"
usage
exit 1
;;
esac
shift
done
format="\
%(HEAD) \
$branch|\
%(color:bold red)%(objectname:short)%(color:reset) \
%(color:bold green)(%(committerdate:relative))%(color:reset) \
%(color:bold blue)%(authorname)%(color:reset) \
%(color:yellow)%(upstream:track)%(color:reset)
$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