|
| 1 | +#! /bin/bash |
| 2 | + |
| 3 | +VERSION="0.1.0" |
| 4 | + |
| 5 | +. git-commands-utils |
| 6 | + |
| 7 | +while getopts ":hv-:" opt; do |
| 8 | + case $opt in |
| 9 | + -) |
| 10 | + case "${OPTARG}" in |
| 11 | + help) show_help;; |
| 12 | + version) show_version "$VERSION";; |
| 13 | + *) error "Unimplemented option --${OPTARG}";; |
| 14 | + esac |
| 15 | + ;; |
| 16 | + h) show_help;; |
| 17 | + v) show_version "$VERSION";; |
| 18 | + \?) error "Invalid option: -$OPTARG";; |
| 19 | + :) error "Option -$OPTARG requires an argument.";; |
| 20 | + *) error "Unimplemented option: -$OPTARG";; |
| 21 | + esac |
| 22 | +done |
| 23 | + |
| 24 | +start_count=0 |
| 25 | +end_count=0 |
| 26 | + |
| 27 | +input=${!OPTIND} |
| 28 | +range_regex="^([[:digit:]]+)\.\.([[:digit:]])+$" |
| 29 | +digits_regex="^[[:digit:]]+$" |
| 30 | +if [[ -z "$input" ]]; then |
| 31 | + error_and_continue "a number of stashes to drop must be specified" |
| 32 | + usage_error "git abandon <count>" "git abandon <start>..<end>" |
| 33 | +elif [[ "$input" =~ $range_regex ]]; then |
| 34 | + start_count="${BASH_REMATCH[1]}" |
| 35 | + end_count="${BASH_REMATCH[2]}" |
| 36 | +elif [[ "$input" =~ $digits_regex ]]; then |
| 37 | + start_count=0 |
| 38 | + end_count=$((input-1)) |
| 39 | +else |
| 40 | + error_and_continue "a number of stashes to drop must be specified" |
| 41 | + error_and_continue "used '$input'" |
| 42 | + usage_error "git abandon <count>" "git abandon <start>..<end>" |
| 43 | +fi |
| 44 | + |
| 45 | +# adjust for current stash count |
| 46 | +stash_list=$(git stash list) |
| 47 | +IFS=$'\n' read -rd '' -a stash_list <<< "$stash_list" |
| 48 | +stash_list_count="${#stash_list[@]}" |
| 49 | + |
| 50 | +if (( "$start_count" >= "$stash_list_count" )); then |
| 51 | + error_and_continue "start too high" |
| 52 | + error "only $stash_list_count stashes exist" |
| 53 | +elif (( "$end_count" >= "$stash_list_count" )); then |
| 54 | + end_count=$(($stash_list_count-1)) |
| 55 | +fi |
| 56 | + |
| 57 | +drop_count=$((end_count-start_count)) |
| 58 | + |
| 59 | +for ((i=0;i<=$drop_count;i++)); do |
| 60 | + git stash drop stash@{"$start_count"} |
| 61 | +done |
0 commit comments