funtrack (4224B) - raw


      1 #!/usr/bin/env zsh
      2 
      3 0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
      4 0="${${(M)0:#/*}:-$PWD/$0}"
      5 
      6 # Desc: untrack selected files
      7 #
      8 # @params
      9 # Globals:
     10 #   ${mydir}: current dir of the script
     11 #   ${track_type}: determine method to use for untrack, possible values: untrack, temp, resume
     12 #   ${confirm}: user confirm status
     13 #   ${selected_files}: arrays of user selected_files for operation
     14 
     15 emulate -LR zsh
     16 setopt warncreateglobal noshortloops
     17 
     18 local mydir; mydir=${0:h:A}
     19 builtin source ${mydir}/fgit_helper
     20 
     21 function usage() {
     22   builtin print -P -- "\
     23 %F{1}%BUsage%f%b: %F{2}funtrack%f [%F{13}-h%f] [%F{13}-t%f] [%F{13}-r%f] [%F{13}-y%f] ...
     24 
     25 Untrack selected files from git.
     26 
     27 %F{4}Default%f: list all tracked files and permanently untrack the selected files (using git rm --cached filename).
     28 
     29 Files will be remove from index while keeping the file in your current system.
     30 However, when your other computers pull down the changes, the untracked files will be deleted.
     31 Make sure to run fbackup before pulling down the changes.
     32 
     33 Alternatively use the %F{13}-t%f flag (using git update-index --assume-unchanged [path]) to temporarily
     34 untrack a file but keeping the files when other computers pull down the changes.
     35 
     36 %F{1}%BOptions%f%b:
     37   %F{13}-h%f, %F{13}--help%f     Show this help message and exit
     38   %F{13}-t%f, %F{13}--temp%f     List all tracked files and temporarily ignore changes of the selected files
     39   %F{13}-r%f, %F{13}--resume%f   List all tracked files and resume tracking changes of the selected files
     40   %F{13}-y%f, %F{13}--yes%f      Acknowledge all actions that will be taken and skip confirmation"
     41 }
     42 
     43 track_type="untrack"
     44 typeset -a selected_files && selected_files=()
     45 confirm=""
     46 
     47 while [[ "$#" -gt 0 ]]; do
     48   case "$1" in
     49     -t|--temp) track_type="temp";      shift ;;
     50     -r|--resume) track_type="retrack"; shift ;;
     51     -y|--yes) confirm='y';             shift ;;
     52     -h|--help) usage && exit 0 ;;
     53     *) echo "Invalid option: $1" >&2 && usage && exit 1 ;;
     54   esac
     55 done
     56 
     57 while IFS= read -r line; do
     58   selected_files+=("${line}")
     59 done < <(get_git_file "select files to untrack")
     60 [[ "${#selected_files[@]}" -eq 0 ]] && exit 1
     61 
     62 if [[ "${track_type}" == "temp" ]]; then
     63   [[ -z "${confirm}" ]] && echo "(dryrun) git update-index --assume-unchanged" "${selected_files[@]}"
     64   [[ -z "${confirm}" ]] && confirm=$(get_confirmation "Files will be temporarily stop being tracked for changes, continue?")
     65   [[ "${confirm}" != 'y' ]] && exit 1
     66   git update-index --assume-unchanged "${selected_files[@]}"
     67   echo -e " "
     68   echo "Selected files are temporarily untracked by git, use funtrack -r to continue tracking changes."
     69   echo "Although funtrack -t won't delete the files on other machines, it is not the recommended way to untrack files."
     70   echo "funtrack -t is using git update-index --assume-unchanged under the hood"
     71   echo "Please refer to git update-index official documentation for more details"
     72 elif [[ "${track_type}" == "retrack" ]]; then
     73   [[ -z "${confirm}" ]] && echo "(dryrun) git update-index --no-assume-unchanged" "${selected_files[@]}"
     74   [[ -z "${confirm}" ]] && confirm=$(get_confirmation "Files will resume being tracked by git, continue?")
     75   [[ "${confirm}" != 'y' ]] && exit 1
     76   git update-index --no-assume-unchanged "${selected_files[@]}"
     77   echo " "
     78   echo "Selected files are being resumed for tracking by git."
     79   echo "Although funtrack -t won't delete the files on other machines, it is not the recommended way to untrack files."
     80   echo "funtrack -t is using git update-index --assume-unchanged under the hood"
     81   echo "Please refer to git update-index official documentation for more details"
     82 else
     83   [[ -z "${confirm}" ]] && echo "(dryrun) git rm --cached" "${selected_files[@]}"
     84   [[ -z "${confirm}" ]] && confirm=$(get_confirmation "Untrack the selected files?")
     85   [[ "${confirm}" != 'y' ]] && exit 1
     86   git rm --cached "${selected_files[@]}"
     87   echo -e " "
     88   echo "Selected files are being untracked by git, make sure to run fbackup on your other systems."
     89   echo "When other systems pull down this change, selected files will be deleted on other systems."
     90   echo "This is the default behavior of git rm --cached."
     91   echo "Please refer to git rm official documentation for more details"
     92 fi