2
0
mirror of https://github.com/kazhala/dotbare synced 2024-11-17 21:25:32 +00:00
dotbare/scripts/funtrack

104 lines
4.0 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
2020-05-15 08:41:53 +00:00
# ${track_type}: what method to use for untrack, untrack, update, retrack
2020-05-04 23:27:53 +00:00
# ${confirm}: use confirm status
# ${selected_files}: user selected_files for operation
2020-05-04 22:41:35 +00:00
# Arguments
2020-05-04 23:27:53 +00:00
# -h: display help message and exit
# -s: temporarily untrack files
# -S: resume track of temp untracked files
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() {
2020-05-13 03:47:09 +00:00
echo -e "Usage: dotbare funtrack [-h] [-s] [-S] ...\n"
2020-05-04 22:41:35 +00:00
echo -e "Untrack selected files from git\n"
echo -e "Note: by default, the untrack will remove the file from index while"
echo -e "keeping the file in your current system (git rm --cached filename)"
echo -e "however, when your other computer pull down the changes, the file will be removed"
echo -e "make sure to run dotbare fbackup before pulling down the changes, alternativly"
echo -e "use the -s flag (git update-index --assume-unchanged [path])\n"
echo -e "optional arguments:"
2020-05-04 23:27:53 +00:00
echo -e " -h\tshow this help message and exit"
echo -e " -s\ttemporarily ignore changes of selected files"
echo -e " \tuse -S flag to resume tracking changes of selected_files"
echo -e " -S\tresume tracking changes of selected files"
2020-05-15 08:04:43 +00:00
echo -e " -y\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"
2020-05-04 23:23:49 +00:00
2020-05-15 08:04:43 +00:00
while getopts ":hsSy" opt
2020-05-04 22:41:35 +00:00
do
case "$opt" in
2020-05-04 23:23:49 +00:00
s)
2020-05-15 08:41:53 +00:00
track_type="update"
2020-05-04 23:23:49 +00:00
;;
S)
2020-05-15 08:41:53 +00:00
track_type="retrack"
2020-05-04 23:23:49 +00:00
;;
2020-05-15 08:04:43 +00:00
y)
confirm='y'
;;
2020-05-04 22:41:35 +00:00
h)
usage
exit 0
;;
*)
echo "Invalid option: ${OPTARG}" >&2
usage
exit 1
;;
esac
done
selected_files=$(get_git_file "select files to untrack" | tr "\n" " ")
[[ -z "${selected_files}" ]] && exit 1
2020-05-04 23:23:49 +00:00
2020-05-15 08:41:53 +00:00
if [[ "${track_type}" == "update" ]]; then
2020-05-04 23:23:49 +00:00
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
# shellcheck disable=SC2086
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" update-index --assume-unchanged ${selected_files}
echo -e " "
echo "Selected files are temporarily untracked by git, use -S to continue tracking changes."
echo "dotbare funtrack -s is not the recommanded way to untrack files, although it won't delete"
echo "the files on other machines, please refer to git update-index official"
echo "documentation for more information"
2020-05-15 08:41:53 +00:00
elif [[ "${track_type}" == "retrack" ]]; then
2020-05-04 23:23:49 +00:00
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
# shellcheck disable=SC2086
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" update-index --no-assume-unchanged ${selected_files}
echo " "
echo "Selected files are being resumed for track by git."
echo "dotbare funtrack -s not the recommanded way to untrack files, although it won't delete"
echo "the files on other machines, please refer to git update-index official"
echo "documentation for more information"
else
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
# shellcheck disable=SC2086
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" rm --cached ${selected_files}
echo -e " "
echo "Selected files are being untracked by git, make sure to run dotbare fbackup"
echo "on your other systems. When other system pull down this change, selected files"
echo "will be removed, this is the default behavior of git rm --cached, more information"
echo "refer to dotbare funtrack -h or README"
fi