fedit (2024B) - raw


      1 #!/usr/bin/env zsh
      2 
      3 0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
      4 0="${${(M)0:#/*}:-$PWD/$0}"
      5 
      6 # Desc: interactive menu to choose file/commit to edit
      7 #
      8 # @params
      9 # Globals:
     10 #   ${mydir}: current directory of the script
     11 #   ${edit_type}: which type to edit, all files, modified files, commit
     12 #   ${selected_commit}: selected commit to edit
     13 #   ${selected_files}: arrays of selected file to edit
     14 # Arguments:
     15 #   -m|--modified: display modified file only
     16 #   -c|--commit: edit commit using interactive rebase
     17 #   -h|--help: show helpe message and exit
     18 
     19 emulate -LR zsh
     20 setopt warncreateglobal noshortloops
     21 
     22 local mydir; mydir=${0:h:A}
     23 builtin source ${mydir}/fgit_helper
     24 
     25 function usage() {
     26   builtin print -P "\
     27 %F{1}%BUsage%f%b:
     28   %F{2}fedit%f [%F{13}-h%f] [%F{13}-m%f] [%F{13}-c%f] ...
     29 
     30 %F{11}Select%f files/commits through fzf and edit selected files/commits in EDITOR.
     31 
     32 %F{4}Default%f: list all tracked dotfiles and edit 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}-m%f, %F{13}--modified%f  Only list and edit selected modified files
     37   %F{13}-c%f, %F{13}--commit%f    List commit and edit the selected commit through interactive rebase"
     38 }
     39 
     40 local -a selected_files
     41 local edit_type selected_commit
     42 edit_type="all"
     43 
     44 while (( $# )) {
     45   case "$1" in
     46     (-m|--modified) edit_type="modified"; shift  ;;
     47     (-c|--commit)   edit_type="commit";   shift  ;;
     48     (-h|--help)     usage;                exit 0 ;;
     49     (*)             print::error "Invalid option: $1" && { usage; exit 1 } ;;
     50   esac
     51 }
     52 
     53 if [[ "$edit_type" == commit ]]; then
     54   selected_commit=$(get_commit "select a commit to edit")
     55   [[ -z "$selected_commit" ]] && exit 1
     56   command git rebase -i "$selected_commit"~
     57 else
     58   if [[ "$edit_type" = modified ]] {
     59     selected_files=( ${(@f)"$(get_modified_file 'select files to edit')"} )
     60   } else {
     61     selected_files=( ${(@f)"$(get_git_file 'select files to edit')"})
     62   }
     63 
     64   (( $#selected_files )) || exit 1
     65   exec "$EDITOR" "$selected_files[@]"
     66 fi