You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
dotbare/scripts/freset

88 lines
2.8 KiB
Plaintext

4 years ago
#!/bin/bash
#
# unstage the selected staged file
# or reset the commit to certain point
#
# @params
# Globals
# ${mydir}: current directory of the script
# ${search_commits}: search commits and reset commits instead of files
# #{reset_option}: git reset flag, --mixed | --soft | --hard
# ${all_files}: search all files and select a commit to reset
# ${selected_files}: selected file to unstage
# ${selected_commit}: selected commit to reset
# ${selected_branch}: selected branch to reset
4 years ago
set -e
set -f
4 years ago
mydir="${0%/*}"
source "${mydir}"/../helper/set_variable.sh
source "${mydir}"/../helper/get_confirmation.sh
source "${mydir}"/../helper/git_query.sh
4 years ago
function usage() {
echo -e "Usage: dotbare freset [-h] [-c] [-S] [-H] ...\n"
4 years ago
echo -e "Reset/Unstage the selected staged file"
4 years ago
echo -e "Or reset the HEAD to certain commits by using -c flag\n"
4 years ago
echo -e "optional arguments:"
echo -e " -h\t\tshow this help message and exit"
echo -e " -a\t\tselect files and then select a commit to reset the file back to the selected comit"
echo -e " -c\t\treset commit to certain commit, default --mixed flag, reset HEAD to certain commit put all changes into modified states"
echo -e " -S\t\treset commit using --soft flag, reset HEAD to certain commit without modify working tree"
echo -e " -H\t\treset commit using --hard flag, reset HEAD to certain commit dicard all changes from the working tree"
4 years ago
}
search_commits=""
reset_option="--mixed"
all_files=""
while getopts ":hacSH" opt
4 years ago
do
case "$opt" in
a)
all_files="true"
;;
c)
search_commits="true"
;;
S)
reset_option="--soft"
;;
H)
reset_option="--hard"
;;
4 years ago
h)
usage
exit 0
;;
*)
echo "Invalid option: ${OPTARG}" >&2
usage
exit 1
;;
esac
done
if [[ -n "${search_commits}" ]]; then
selected_commit=$(get_commit)
[[ -z "${selected_commit}" ]] && exit 0
confirm=$(get_confirmation "Reset HEAD to ${selected_commit} ${reset_option}?")
[[ "${confirm}" != 'y' ]] && exit 0
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" reset "${selected_commit}" "${reset_option}"
else
if [[ -z "${all_files}" ]]; then
selected_files=$(get_staged_file 'select files to unstage' | tr '\n' ' ')
[[ -z "${selected_files}" ]] && exit 0
# shellcheck disable=SC2086
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" reset HEAD ${selected_files}
else
selected_files=$(get_git_file 'select a file to reset' | tr '\n' ' ')
[[ -z "${selected_files}" ]] && exit 0
selected_commit=$(get_commit "select the target commit" "${selected_files}")
[[ -z "${selected_commit}" ]] && exit 0
# shellcheck disable=SC2086
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" reset "${selected_commit}" ${selected_files}
fi
fi