2
0
mirror of https://github.com/kazhala/dotbare synced 2024-11-02 09:40:27 +00:00
dotbare/scripts/funtrack

108 lines
4.7 KiB
Plaintext
Raw Normal View History

2020-05-11 01:56:27 +00:00
#!/usr/bin/env bash
2020-05-04 22:41:35 +00:00
#
# untrack selected files
#
# @params
# Globals
2020-05-04 23:27:53 +00:00
# ${mydir}: current dir of the script
# ${track_type}: determine method to use for untrack, possible values: untrack, temp, resume
# ${confirm}: user confirm status
# ${selected_files}: arrays of user selected_files for operation
2020-05-04 22:41:35 +00:00
# Arguments
# -h|--help: display help message and exit
# -t|--temp: temporarily untrack files
# -r|--resume: resume track of temp untracked files
# -y|--yes: confirm action by default and skip confirmation
2020-05-04 22:41:35 +00:00
set -e
set -f
2020-05-13 07:30:54 +00:00
mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
2020-05-04 22:41:35 +00:00
source "${mydir}"/../helper/set_variable.sh
source "${mydir}"/../helper/git_query.sh
source "${mydir}"/../helper/get_confirmation.sh
function usage() {
echo -e "Usage: dotbare funtrack [-h] [-t] [-r] [-y] ...\n"
2020-05-04 22:41:35 +00:00
echo -e "Untrack selected files from git\n"
echo -e "Default: launch fzf and the selected files will be untracked (using git rm --cached filename).\n"
echo -e "Files will be remove from index while keeping the file in your current system."
echo -e "However, when your other computers pull down the changes, the untracked files will be deleted."
echo -e "Make sure to run dotbare fbackup before pulling down the changes.\n"
echo -e "Alternatively use the -t flag (using git update-index --assume-unchanged [path]) to temporarily"
echo -e "untrack a file but keeping the files when other computers pull down the changes."
echo -e "More information please refere to dotbare's github.\n"
2020-05-04 22:41:35 +00:00
echo -e "optional arguments:"
echo -e " -h, --help\t\tshow this help message and exit"
echo -e " -t, --temp\t\ttemporarily ignore changes of the selected files"
echo -e " -r, --resume\t\tresume tracking changes of the selected files"
echo -e " -y, --yes\t\tconfirm action by default and skip confirmation"
2020-05-04 22:41:35 +00:00
}
2020-05-15 08:41:53 +00:00
track_type="untrack"
selected_files=()
confirm=""
2020-05-04 23:23:49 +00:00
while [[ "$#" -gt 0 ]]; do
case "$1" in
-t|--temp)
track_type="temp"
shift
2020-05-04 23:23:49 +00:00
;;
-r|--resume)
2020-05-15 08:41:53 +00:00
track_type="retrack"
shift
2020-05-04 23:23:49 +00:00
;;
-y|--yes)
2020-05-15 08:04:43 +00:00
confirm='y'
shift
2020-05-15 08:04:43 +00:00
;;
-h|--help)
2020-05-04 22:41:35 +00:00
usage
exit 0
;;
*)
echo "Invalid option: $1" >&2
2020-05-04 22:41:35 +00:00
usage
exit 1
;;
esac
done
while IFS= read -r line; do
selected_files+=("${line}")
done < <(get_git_file "select files to untrack")
[[ "${#selected_files[@]}" -eq 0 ]] && exit 1
2020-05-04 22:41:35 +00:00
if [[ "${track_type}" == "temp" ]]; then
[[ -z "${confirm}" ]] && echo "(dryrun) dotbare update-index --assume-unchanged ${selected_files[@]}"
2020-05-15 08:04:43 +00:00
[[ -z "${confirm}" ]] && confirm=$(get_confirmation "Files will be temporarily stop being tracked for changes, continue?")
2020-05-04 23:23:49 +00:00
[[ "${confirm}" != 'y' ]] && exit 1
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" update-index --assume-unchanged "${selected_files[@]}"
2020-05-04 23:23:49 +00:00
echo -e " "
echo "Selected files are temporarily untracked by git, use dotbare funtrack -r to continue tracking changes."
echo "Although dotbare funtrack -t won't delete the files on other machines, it is not the recommended way to untrack files."
echo "dotbare funtrack -t is using git update-index --assume-unchanged under the hood"
echo "Please refer to git update-index official documentation for more details or visit dotbare's github page"
2020-05-15 08:41:53 +00:00
elif [[ "${track_type}" == "retrack" ]]; then
[[ -z "${confirm}" ]] && echo "(dryrun) dotbare update-index --no-assume-unchanged ${selected_files[@]}"
2020-05-15 08:04:43 +00:00
[[ -z "${confirm}" ]] && confirm=$(get_confirmation "Files will resume being tracked by git, continue?")
2020-05-04 23:23:49 +00:00
[[ "${confirm}" != 'y' ]] && exit 1
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" update-index --no-assume-unchanged "${selected_files[@]}"
2020-05-04 23:23:49 +00:00
echo " "
echo "Selected files are being resumed for tracking by git."
echo "Although dotbare funtrack -t won't delete the files on other machines, it is not the recommended way to untrack files."
echo "dotbare funtrack -t is using git update-index --assume-unchanged under the hood"
echo "Please refer to git update-index official documentation for more details or visit dotbare's github page"
2020-05-04 23:23:49 +00:00
else
[[ -z "${confirm}" ]] && echo "(dryrun) dotbare rm --cached ${selected_files[@]}"
2020-05-15 08:04:43 +00:00
[[ -z "${confirm}" ]] && confirm=$(get_confirmation "Untrack the selected files?")
2020-05-04 23:23:49 +00:00
[[ "${confirm}" != 'y' ]] && exit 1
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" rm --cached "${selected_files[@]}"
2020-05-04 23:23:49 +00:00
echo -e " "
echo "Selected files are being untracked by git, make sure to run dotbare fbackup on your other systems."
echo "When other systems pull down this change, selected files will be deleted on other systems."
echo "This is the default behavior of git rm --cached."
echo "Please refer to git rm official documentation for more details or visit dotbare's github page"
2020-05-04 23:23:49 +00:00
fi