From ed85526917c8b892c432be74c5fd60ceff4f3ba2 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Tue, 20 Feb 2018 23:18:49 +0100 Subject: git-branch-list: Verify ID is an integer before doing `checkout` When running: $ git branch-list 2 we want to ensure the argument "2" in the example above is an integer. If so, we take it to represent a branch-list ID. Otherwise an error code should be returned. Thanks to Peter Ho on StackOverflow for the neat Bash integer test: https://stackoverflow.com/questions/2210349/bash-test-whether-string-is-valid-as-an-integer/19116862#19116862 --- git-branch-list | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/git-branch-list b/git-branch-list index bd85160..bf55624 100755 --- a/git-branch-list +++ b/git-branch-list @@ -93,7 +93,12 @@ case "$command" in exit $? ;; *) - # if $1 is an integer - checkout_branch "$1" + # If `$1` is an integer + if [ "$1" -eq "$1" ] 2>/dev/null; then + checkout_branch "$1" + else + exit 1 + fi + ;; esac -- cgit v1.2.3