-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathfreset
More file actions
executable file
·93 lines (84 loc) · 2.7 KB
/
Copy pathfreset
File metadata and controls
executable file
·93 lines (84 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env bash
#
# unstage the selected staged file
# or reset the commit to certain point
#
# @params
# Globals
# ${mydir}: current directory of the script
# ${reset_type}: reset type, modified files or commit
# ${reset_option}: git reset flag, --mixed | --soft | --hard
# ${selected_files}: selected file to reset
# ${selected_commit}: selected commit to reset
# ${confirm}: confirmation status of the user
# Arguments
# -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
set -e
set -f
mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "${mydir}"/../helper/set_variable.sh
source "${mydir}"/../helper/get_confirmation.sh
source "${mydir}"/../helper/git_query.sh
function usage() {
echo -e "Usage: dotbare freset [-h] [-c] [-S] [-H] [-y] ...
Reset(unstage) the selected staged files.
Reset the HEAD to certain commits by using -c flag.
Default: unstage the selected files.
Optional arguments:
-h, --help\t\tshow this help message and exit.
-c, --commit\t\treset HEAD to certain commit, default --mixed flag, reset HEAD to certain commit put all changes into modified state.
-S, --soft\t\treset commit using --soft flag, reset HEAD to certain commit without modify working tree.
-H, --hard\t\treset commit using --hard flag, reset HEAD to certain commit discard all changes from the working tree.
-y, --yes\t\tacknowledge all actions that will be taken and skip confirmation."
}
reset_option="--mixed"
reset_type="modified"
selected_files=()
confirm=""
selected_commit=""
while [[ "$#" -gt 0 ]]; do
case "$1" in
-c|--commit)
reset_type="commit"
shift
;;
-S|--soft)
reset_option="--soft"
shift
;;
-H|--hard)
reset_option="--hard"
shift
;;
-y|--yes)
confirm='y'
shift
;;
-h|--help)
usage
exit 0
;;
*)
echo "Invalid option: $1" >&2
usage
exit 1
;;
esac
done
if [[ "${reset_type}" == "commit" ]]; then
selected_commit=$(get_commit "select the target commit")
[[ -z "${selected_commit}" ]] && exit 1
[[ -z "${confirm}" ]] && confirm=$(get_confirmation "Reset HEAD to ${selected_commit} ${reset_option}?")
[[ "${confirm}" != 'y' ]] && exit 1
git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" reset "${selected_commit}" "${reset_option}"
else
while IFS= read -r line; do
selected_files+=("${line}")
done < <(get_modified_file 'select files to unstage' 'staged')
[[ "${#selected_files[@]}" -eq 0 ]] && exit 1
git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" reset "${selected_files[@]}"
fi