Created
October 24, 2017 15:20
-
-
Save npasserini/c8e517f9e42549d504d250a54637d0e1 to your computer and use it in GitHub Desktop.
A menu for selecting branches to checkout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| TITLE="Branch Selector" | |
| MENU="Select a branch to checkout:" | |
| DEFAULT_BRANCH=$(git symbolic-ref -q HEAD) | |
| DEFAULT_BRANCH=${DEFAULT_BRANCH##refs/heads/} | |
| DEFAULT=0 | |
| OPTIONS=() | |
| tempBranches=() | |
| BRANCHES=() | |
| eval "$(git for-each-ref --shell --format='tempBranches+=(%(refname))' refs/heads/)" | |
| i=0 | |
| max_width=0 | |
| for branch in "${tempBranches[@]}"; do | |
| OPTIONS+=($i) | |
| branch=${branch:11} | |
| BRANCHES+=($branch) | |
| OPTIONS+=($branch) | |
| if [ $branch = $DEFAULT_BRANCH ]; then | |
| DEFAULT=$i | |
| fi | |
| if [ "${#branch}" -gt "$max_width" ]; then | |
| max_width="${#branch}" | |
| fi | |
| i="$((i+1))" | |
| done | |
| WIDTH="$((max_width+9))" | |
| CHOICE_HEIGHT="$((i))" | |
| HEIGHT="$((CHOICE_HEIGHT+8))" | |
| CHOICE=$(dialog --keep-tite\ | |
| --clear \ | |
| --title "$TITLE" \ | |
| --default-item $DEFAULT \ | |
| --menu "$MENU" \ | |
| $HEIGHT $WIDTH $CHOICE_HEIGHT \ | |
| "${OPTIONS[@]}" \ | |
| 2>&1 >/dev/tty) | |
| if [[ "$CHOICE" == "" ]]; then | |
| exit 1 | |
| fi | |
| git checkout ${BRANCHES[CHOICE]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment