Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept count as '-X' as well, add usage and error handling #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 33 additions & 10 deletions git-recent
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,43 @@ case $(uname -s) in
;;
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 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}
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
shift $((OPTIND-1))

format="\
%(HEAD) \
Expand All @@ -48,7 +71,7 @@ lessopts="--tabs=4 --quit-if-one-screen --RAW-CONTROL-CHARS --no-init"

git for-each-ref \
--color=always \
--count=$COUNT \
--count="$COUNT" \
--sort=-committerdate \
"refs/heads/" \
--format="$format" \
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Type `git recent` to see your latest local git branches
git recent


Optionally, add `-n<int>` to see the most recent `<n>` branches
Optionally, add `-n<num>` or `-<num>` to see only the `<num>` most recent branches:

git recent -n5

Expand Down