2020-05-11 01:56:27 +00:00
|
|
|
#!/usr/bin/env bash
|
2020-04-10 06:09:08 +00:00
|
|
|
#
|
|
|
|
# unstage the selected staged file
|
|
|
|
# or reset the commit to certain point
|
|
|
|
#
|
|
|
|
# @params
|
|
|
|
# Globals
|
|
|
|
# ${mydir}: current directory of the script
|
2020-06-22 00:48:04 +00:00
|
|
|
# ${reset_type}: reset type, modified files or commit
|
2020-04-24 06:21:41 +00:00
|
|
|
# ${reset_option}: git reset flag, --mixed | --soft | --hard
|
2020-06-22 00:48:04 +00:00
|
|
|
# ${selected_files}: selected file to reset
|
2020-04-17 07:55:22 +00:00
|
|
|
# ${selected_commit}: selected commit to reset
|
2020-04-24 07:26:45 +00:00
|
|
|
# ${confirm}: confirmation status of the user
|
|
|
|
# Arguments
|
2020-06-22 00:48:04 +00:00
|
|
|
# -h|--help: show help message and quit
|
|
|
|
# -c|--commit: reset commit
|
|
|
|
# -S|--soft: use --soft flag
|
|
|
|
# -H|--hard: use --hard flag
|
|
|
|
# -y|--yes: confirm action by default and skip confirmation
|
2020-04-10 06:09:08 +00:00
|
|
|
|
|
|
|
set -e
|
2020-04-17 07:55:22 +00:00
|
|
|
set -f
|
2020-04-10 06:09:08 +00:00
|
|
|
|
2020-05-13 07:30:54 +00:00
|
|
|
mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
2020-04-14 07:17:42 +00:00
|
|
|
source "${mydir}"/../helper/set_variable.sh
|
|
|
|
source "${mydir}"/../helper/get_confirmation.sh
|
2020-04-16 06:21:02 +00:00
|
|
|
source "${mydir}"/../helper/git_query.sh
|
2020-04-10 06:09:08 +00:00
|
|
|
|
|
|
|
function usage() {
|
2020-05-13 03:52:28 +00:00
|
|
|
echo -e "Usage: dotbare freset [-h] [-a] [-c] [-S] [-H] ...\n"
|
2020-04-10 06:09:08 +00:00
|
|
|
echo -e "Reset/Unstage the selected staged file"
|
2020-04-11 06:38:05 +00:00
|
|
|
echo -e "Or reset the HEAD to certain commits by using -c flag\n"
|
2020-06-22 00:48:04 +00:00
|
|
|
echo -e "Default: unstage the selected files\n"
|
2020-04-10 06:09:08 +00:00
|
|
|
echo -e "optional arguments:"
|
2020-06-22 00:48:04 +00:00
|
|
|
echo -e " -h, --help\t\tshow this help message and exit"
|
|
|
|
echo -e " -c, --commit\t\treset commit to certain commit, default --mixed flag, reset HEAD to certain commit put all changes into modified state"
|
|
|
|
echo -e " -S, --soft\t\treset commit using --soft flag, reset HEAD to certain commit without modify working tree"
|
|
|
|
echo -e " -H, --hard\t\treset commit using --hard flag, reset HEAD to certain commit dicard all changes from the working tree"
|
|
|
|
echo -e " -y, --yes\t\tconfirm action by default and skip confirmation"
|
2020-04-10 06:09:08 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 07:20:13 +00:00
|
|
|
reset_option="--mixed"
|
2020-05-15 08:33:57 +00:00
|
|
|
reset_type="modified"
|
2020-06-22 00:31:55 +00:00
|
|
|
selected_files=()
|
2020-04-10 06:41:12 +00:00
|
|
|
|
2020-06-22 00:48:04 +00:00
|
|
|
while [[ "$#" -gt 0 ]]; do
|
|
|
|
case "$1" in
|
|
|
|
-c|--commit)
|
2020-05-15 08:33:57 +00:00
|
|
|
reset_type="commit"
|
2020-06-22 00:48:04 +00:00
|
|
|
shift
|
2020-04-10 06:41:12 +00:00
|
|
|
;;
|
2020-06-22 00:48:04 +00:00
|
|
|
-S|--soft)
|
2020-04-10 07:20:13 +00:00
|
|
|
reset_option="--soft"
|
2020-06-22 00:48:04 +00:00
|
|
|
shift
|
2020-04-10 07:20:13 +00:00
|
|
|
;;
|
2020-06-22 00:48:04 +00:00
|
|
|
-H|--hard)
|
2020-04-10 07:20:13 +00:00
|
|
|
reset_option="--hard"
|
2020-06-22 00:48:04 +00:00
|
|
|
shift
|
2020-04-10 07:20:13 +00:00
|
|
|
;;
|
2020-06-22 00:48:04 +00:00
|
|
|
-y|--yes)
|
2020-05-15 08:04:43 +00:00
|
|
|
confirm='y'
|
2020-06-22 00:48:04 +00:00
|
|
|
shift
|
2020-05-15 08:04:43 +00:00
|
|
|
;;
|
2020-06-22 00:48:04 +00:00
|
|
|
-h|--help)
|
2020-04-10 06:09:08 +00:00
|
|
|
usage
|
|
|
|
exit 0
|
|
|
|
;;
|
2020-06-22 00:48:04 +00:00
|
|
|
*)
|
|
|
|
echo "Invalid option: $1" >&2
|
2020-04-10 06:09:08 +00:00
|
|
|
usage
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2020-05-15 08:33:57 +00:00
|
|
|
if [[ "${reset_type}" == "commit" ]]; then
|
2020-04-17 07:21:16 +00:00
|
|
|
selected_commit=$(get_commit)
|
|
|
|
[[ -z "${selected_commit}" ]] && exit 0
|
2020-05-15 08:04:43 +00:00
|
|
|
[[ -z "${confirm}" ]] && confirm=$(get_confirmation "Reset HEAD to ${selected_commit} ${reset_option}?")
|
|
|
|
[[ "${confirm}" != 'y' ]] && exit 1
|
2020-04-17 07:21:16 +00:00
|
|
|
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" reset "${selected_commit}" "${reset_option}"
|
2020-04-10 06:41:12 +00:00
|
|
|
else
|
2020-06-22 00:31:55 +00:00
|
|
|
while IFS= read -r line; do
|
|
|
|
selected_files+=("${line}")
|
|
|
|
done < <(get_modified_file 'select files to unstage' 'staged')
|
|
|
|
[[ "${#selected_files}" -eq 0 ]] && exit 1
|
|
|
|
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" reset HEAD "${selected_files[@]}"
|
2020-04-10 06:41:12 +00:00
|
|
|
fi
|