|
| 1 | +OPTS_SPEC="\ |
| 2 | +${0##*/} [<options>] [<pattern>] |
| 3 | + |
| 4 | +Select references using fzf. |
| 5 | +-- |
| 6 | +h,help Show the help |
| 7 | + |
| 8 | +t,tags Pick tags |
| 9 | +b,branches Pick local branches (this is the default) |
| 10 | +r,remote-branches Pick remote branches |
| 11 | +a,all Pick all references |
| 12 | +m,multi Allow multiple selections |
| 13 | +c,checkout Pass selection to 'git checkout' |
| 14 | +" |
| 15 | + |
| 16 | +checkout=0 |
| 17 | +select_branches=0 |
| 18 | +select_tags=0 |
| 19 | +select_remotes=0 |
| 20 | +fzf_opts=() |
| 21 | + |
| 22 | +eval "$(git rev-parse --parseopt -- "$@" <<<$OPTS_SPEC || echo exit $?)" |
| 23 | + |
| 24 | +while (( $# > 0 )); do |
| 25 | + case 1ドル in |
| 26 | + (-t) select_tags=1 |
| 27 | + shift |
| 28 | + ;; |
| 29 | + |
| 30 | + (-b) select_branches=1 |
| 31 | + shift |
| 32 | + ;; |
| 33 | + |
| 34 | + (-r) select_remotes=1 |
| 35 | + shift |
| 36 | + ;; |
| 37 | + |
| 38 | + (-a) select_tags=1 |
| 39 | + select_branches=1 |
| 40 | + select_remotes=1 |
| 41 | + shift |
| 42 | + ;; |
| 43 | + |
| 44 | + (-m) fzf_opts+=(-m) |
| 45 | + shift |
| 46 | + ;; |
| 47 | + |
| 48 | + (-c) checkout=1 |
| 49 | + shift |
| 50 | + ;; |
| 51 | + |
| 52 | + (--) shift |
| 53 | + break |
| 54 | + ;; |
| 55 | + |
| 56 | + (-*) DIE "unknown option: 1ドル" |
| 57 | + ;; |
| 58 | + esac |
| 59 | +done |
| 60 | +shift $(( OPTIND - 1 )) |
| 61 | + |
| 62 | +patterns=() |
| 63 | + |
| 64 | +(( $select_tags + $select_branches + $select_remotes == 0 )) && select_branches=1 |
| 65 | +[[ $select_tags == 1 ]] && patterns+=(refs/tags/) |
| 66 | +[[ $select_branches == 1 ]] && patterns+=(refs/heads/) |
| 67 | +[[ $select_remotes == 1 ]] && patterns+=(refs/remotes/) |
| 68 | + |
| 69 | +selected=($( |
| 70 | + git for-each-ref --format='%(refname)' "${patterns[@]}" | |
| 71 | + cut -f3- -d/ | |
| 72 | + fzf "${fzf_opts[@]}" |
| 73 | +)) |
| 74 | + |
| 75 | +if [[ $checkout = 1 ]]; then |
| 76 | + git checkout $selected |
| 77 | +else |
| 78 | + echo "${selected[@]}" |
| 79 | +fi |
0 commit comments