2020-05-11 01:56:27 +00:00
|
|
|
#!/usr/bin/env bash
|
2020-05-02 22:55:34 +00:00
|
|
|
#
|
|
|
|
# git log interactive viewer
|
|
|
|
#
|
|
|
|
# @params
|
|
|
|
# Globals
|
2020-05-02 23:53:50 +00:00
|
|
|
# ${mydir}: current dir of the script
|
|
|
|
# ${selected_action}: action to take on the selected commit
|
|
|
|
# ${selected_commit}: user selected commit
|
|
|
|
# ${confirm}: confirm status of user
|
2020-05-02 22:55:34 +00:00
|
|
|
# Arguments
|
2020-05-02 23:53:50 +00:00
|
|
|
# -h: display help message
|
|
|
|
# -r: revert the selected commit
|
|
|
|
# -R: reset HEAD back to the selected commit
|
|
|
|
# -e: edit commmit (interactive rebase)
|
|
|
|
# -c: checkout selected commmit
|
2020-05-02 22:55:34 +00:00
|
|
|
|
|
|
|
set -e
|
|
|
|
set -f
|
|
|
|
|
2020-05-13 07:30:54 +00:00
|
|
|
mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
2020-05-02 22:55:34 +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:52:28 +00:00
|
|
|
echo -e "Usage: dotbare flog [-h] [-r] [-R] [-e] [-c] ...\n"
|
2020-05-02 22:55:34 +00:00
|
|
|
echo -e "Interactive log viewer with action menu"
|
|
|
|
echo -e "Action menu contains command including revert|reset|edit|checkout"
|
|
|
|
echo -e "optional arguments:"
|
2020-05-02 23:53:50 +00:00
|
|
|
echo -e " -h\tshow this help message and exit"
|
|
|
|
echo -e " -r\trevert the selected commit"
|
|
|
|
echo -e " -R\treset HEAD back to selected commit"
|
|
|
|
echo -e " -e\tedit selected commit through interactive rebase"
|
|
|
|
echo -e " -c\tcheckout selected commit"
|
2020-05-15 08:04:43 +00:00
|
|
|
echo -e " -y\tconfirm action by default and skip confirmation"
|
2020-05-02 22:55:34 +00:00
|
|
|
}
|
|
|
|
|
2020-05-02 23:16:18 +00:00
|
|
|
#######################################
|
|
|
|
# draw action menu for selected commit
|
|
|
|
# Arguments:
|
2020-05-02 23:53:50 +00:00
|
|
|
# $1: selected commit hash, used to display commit message in fzf header
|
|
|
|
# $2: selected action, if selected, skip menu, return action
|
2020-05-02 23:16:18 +00:00
|
|
|
# Outputs:
|
|
|
|
# ${selected_action}: user selected action
|
|
|
|
#######################################
|
|
|
|
function draw_menu() {
|
2020-05-02 23:53:50 +00:00
|
|
|
local selected_commit="$1"
|
|
|
|
local selected_action="$2"
|
2020-05-02 23:58:15 +00:00
|
|
|
local menu header message
|
2020-05-02 23:16:18 +00:00
|
|
|
if [[ -n "$selected_action" ]]; then
|
|
|
|
echo "${selected_action}"
|
|
|
|
else
|
2020-05-02 23:53:50 +00:00
|
|
|
menu="revert: revert the selected commit\n"
|
|
|
|
menu="${menu}reset: reset HEAD to the selected commit using --mixed flag\n"
|
|
|
|
menu="${menu}edit: edit selected commit through interactive rebase\n"
|
|
|
|
menu="${menu}checkout: checkout the selected commit\n"
|
2020-05-02 23:58:15 +00:00
|
|
|
menu="${menu}exit: quit dotbare flog"
|
|
|
|
message=$(
|
|
|
|
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \
|
|
|
|
log --format=%B -n 1 "${selected_commit}"
|
|
|
|
)
|
|
|
|
header="commit ${selected_commit}: ${message}"
|
2020-05-02 23:53:50 +00:00
|
|
|
selected_action=$(echo -e "${menu}" \
|
2020-05-02 23:58:15 +00:00
|
|
|
| fzf --no-multi --header="${header}" \
|
2020-05-02 23:53:50 +00:00
|
|
|
| awk '{
|
2020-05-02 23:58:15 +00:00
|
|
|
gsub(/:/, "", $1)
|
|
|
|
print $1
|
|
|
|
}'
|
2020-05-02 23:53:50 +00:00
|
|
|
)
|
2020-05-02 23:16:18 +00:00
|
|
|
echo "${selected_action}"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2020-05-02 22:55:34 +00:00
|
|
|
selected_action=""
|
|
|
|
|
2020-05-15 08:04:43 +00:00
|
|
|
while getopts ":hrRecy" opt; do
|
2020-05-02 22:55:34 +00:00
|
|
|
case "$opt" in
|
|
|
|
r)
|
|
|
|
selected_action="revert"
|
|
|
|
;;
|
|
|
|
R)
|
|
|
|
selected_action="reset"
|
|
|
|
;;
|
|
|
|
e)
|
|
|
|
selected_action="edit"
|
|
|
|
;;
|
|
|
|
c)
|
|
|
|
selected_action="checkout"
|
2020-05-15 08:04:43 +00:00
|
|
|
;;
|
|
|
|
y)
|
|
|
|
confirm='y'
|
2020-05-02 22:55:34 +00:00
|
|
|
;;
|
|
|
|
h)
|
|
|
|
usage
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Invalid option: ${OPTARG}" >&2
|
|
|
|
usage
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
2020-05-02 23:16:18 +00:00
|
|
|
|
|
|
|
while True; do
|
|
|
|
selected_commit=$(get_commit)
|
2020-05-02 23:34:31 +00:00
|
|
|
[[ -z "${selected_commit}" ]] && exit 1
|
2020-05-02 23:53:50 +00:00
|
|
|
selected_action=$(draw_menu "${selected_commit}" "${selected_action}")
|
2020-05-02 23:34:31 +00:00
|
|
|
[[ -n "${selected_action}" ]] && break
|
2020-05-02 23:16:18 +00:00
|
|
|
done
|
2020-05-02 23:34:31 +00:00
|
|
|
|
|
|
|
if [[ "${selected_action}" != 'exit' ]]; then
|
|
|
|
if [[ "${selected_action}" == "reset" ]]; then
|
|
|
|
echo "(dryrun) reset HEAD to ${selected_commit}"
|
|
|
|
else
|
|
|
|
echo "(dryrun) ${selected_action} ${selected_commit}"
|
|
|
|
fi
|
2020-05-15 08:04:43 +00:00
|
|
|
[[ -z "${confirm}" ]] && confirm=$(get_confirmation)
|
2020-05-02 23:34:31 +00:00
|
|
|
[[ "${confirm}" != 'y' ]] && exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
case "${selected_action}" in
|
|
|
|
revert)
|
|
|
|
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \
|
|
|
|
revert "${selected_commit}"
|
|
|
|
;;
|
|
|
|
reset)
|
|
|
|
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \
|
|
|
|
reset "${selected_commit}"
|
|
|
|
;;
|
|
|
|
edit)
|
|
|
|
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \
|
|
|
|
rebase -i "${selected_commit}"~
|
|
|
|
;;
|
|
|
|
checkout)
|
|
|
|
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \
|
|
|
|
checkout "${selected_commit}"
|
|
|
|
;;
|
|
|
|
exit)
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|