fadd (2135B) - raw


      1 #!/usr/bin/env zsh
      2 
      3 0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
      4 0="${${(M)0:#/*}:-$PWD/$0}"
      5 
      6 # Desc: stage the selected file to git bare repo
      7 #
      8 # @params
      9 # Globals:
     10 #   ${mydir}: string, current directory of the executing script
     11 #   ${stage_type}: modified, new file, or directory to stage
     12 #   ${selected_files}: bash array of user selected files to stage
     13 # Arguments:
     14 #   -h|--help: show help message
     15 #   -f|--file: select a file in PWD to stage
     16 #   -d|--dir: select a directory in PWD to stage
     17 
     18 emulate -LR zsh
     19 setopt warncreateglobal noshortloops
     20 
     21 local mydir; mydir=${0:h:A}
     22 builtin source ${mydir}/fgit_helper
     23 
     24 function usage() {
     25   builtin print -P -- "\
     26 %F{1}%BUsage%f%b:
     27   %F{2}fadd%f [%F{13}-h%f] [%F{13}-f%f] [%F{13}-d%f] ...
     28 
     29 Select files/directories or modified files through fzf.
     30 Stage the selected file to the dotfile gitbare repo.
     31 
     32 %F{4}Default%f: list all modified files and stage the selected files.
     33 
     34 %F{1}%BOptions%f%b:
     35   %F{13}-h%f, %F{13}--help%f    Show this help message and exit
     36   %F{13}-f%f, %F{13}--file%f    Select files in current directory and stage the selected files
     37   %F{13}-d%f, %F{13}--dir%f     Select folders in current directory and stage the selected folders"
     38 }
     39 
     40 #######################################
     41 # stage file
     42 # Arguments:
     43 #   $1: array of files to stage
     44 #######################################
     45 
     46 function stage_file() {
     47   local -a files && files=("$@")
     48   (( $#files == 0 )) && { print -Pr -- "%F{1}Nothing added%f"; exit 1; }
     49   command git add "${files[@]}"
     50 }
     51 
     52 local stage_type="modified"
     53 local -a selected_files
     54 
     55 while (( $# )) {
     56   case "$1" in
     57     (-f|--file) stage_type="file"; shift ;;
     58     (-d|--dir)  stage_type="dir";  shift ;;
     59     (-h|--help) usage && exit 0 ;;
     60     (*)         print::error "Invalid option: $1" && { usage; exit 1 } ;;
     61   esac
     62 }
     63 
     64 if [[ $stage_type = file ]] {
     65   selected_files=( ${(@f)"$(search_file f)"} )
     66 } elif [[ $stage_type = dir ]] {
     67   selected_files=( ${(@f)"$(search_file d)"} )
     68 } else {
     69   selected_files=( ${(@f)"$(get_modified_file 'Select files to stage' 'unstaged')"} )
     70 }
     71 
     72 stage_file "$selected_files[@]"
     73 
     74 (( $#selected_files )) && command git status -sb