fstash (2776B) - raw
1 #!/usr/bin/env zsh 2 3 0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}" 4 0="${${(M)0:#/*}:-$PWD/$0}" 5 6 # Desc: stash operation using fzf 7 # 8 # @params 9 # Globals: 10 # ${mydir}: current dir of the script, source purpose 11 # ${stash_command}: stash command, pop, apply or file/delete to stash file or delete stash 12 # ${selected_files}: selected files to stash 13 # ${selected_stash}: selected stash to apply 14 # ${confirm}: user confirm status 15 # Arguments: 16 # -h|--help: show help message 17 # -s|--select: select individual files and stash 18 # -d|--delete: delete selected stash 19 # -p|--pop: use pop instead of apply on the selected stash 20 21 emulate -LR zsh 22 setopt warncreateglobal noshortloops 23 24 local mydir; mydir=${0:h:A} 25 builtin source ${mydir}/fgit_helper 26 27 function usage() { 28 builtin print -P -- "\ 29 %F{1}%BUsage%f%b: %F{2}fstash%f [%F{13}-h%f] [%F{13}-s%f] [%F{13}-d%f] [%F{13}-p%f] ... 30 31 View and manage stash interactively. 32 33 %F{4}Default%f: list all stashes and apply the selected stash. 34 35 %F{1}%BOptions%f%b: 36 %F{13}-h%f, %F{13}--help%f Show this help message and exit 37 %F{13}-s%f, %F{13}--select%f List modified files and stash the selected files 38 %F{13}-d%f, %F{13}--delete%f List all stashes and delete the selected stash from stash list 39 %F{13}-p%f, %F{13}--pop%f Use 'stash pop' instead of 'stash apply'" 40 } 41 42 local stash_command confirm 43 local -a selected_files selected_stash 44 stash_command="apply" 45 46 while (( $# )) { 47 case "$1" in 48 (-p|--pop) stash_command="pop"; shift ;; 49 (-s|--select) stash_command="select"; shift ;; 50 (-d|--delete) stash_command="delete"; shift ;; 51 (-y|--yes) confirm="y"; shift ;; 52 (-h|--help) usage; exit 0 ;; 53 (*) print::error "Invalid option: $1" && { usage; exit 1 } ;; 54 esac 55 } 56 57 local line 58 59 if [[ "$stash_command" == "select" ]]; then 60 selected_files=( ${(@f)"$(get_modified_file "select files to add to a stash")"} ) 61 (( $#selected_files )) || exit 1 62 command git stash -- "$selected_files[@]" 63 elif [[ "$stash_command" == "delete" ]]; then 64 selected_stash=( ${(@f)"$(get_stash 'select stash to delete')"} ) 65 (( $#selected_stash )) || exit 1 66 [[ -z "$confirm" ]] && \ 67 foreach line ("$selected_stash[@]") { 68 print -P -- "(%F{3}dryrun%f) drop ${line}" 69 } 70 71 [[ -z "$confirm" ]] && confirm=$(get_confirmation) 72 [[ "$confirm" != y ]] && exit 1 73 74 foreach line ("$selected_stash[@]") { 75 command git stash drop "${line}" 76 } 77 else 78 selected_stash=( ${(@f)"$(get_stash 'select stash to apply' 'true')"} ) 79 (( $#selected_stash )) || exit 1 80 [[ -z "$confirm" ]] && print -P -- "(%F{3}dryrun%f) $stash_command $selected_stash" 81 [[ -z "$confirm" ]] && confirm=$(get_confirmation) 82 [[ "$confirm" != y ]] && exit 1 83 command git stash "$stash_command" "$selected_stash" 84 fi