2
0
mirror of https://github.com/kazhala/dotbare synced 2024-11-02 09:40:27 +00:00
dotbare/scripts/flog

147 lines
3.8 KiB
Plaintext
Raw Normal View History

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
mydir="${0%/*}"
source "${mydir}"/../helper/set_variable.sh
source "${mydir}"/../helper/git_query.sh
source "${mydir}"/../helper/get_confirmation.sh
function usage() {
echo -e "Usage: dotbare flog [-h] [-f] [-d] ...\n"
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-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"
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"
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}" \
| fzf --no-multi --header="${header}" \
2020-05-02 23:53:50 +00:00
| awk '{
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-02 23:53:50 +00:00
while getopts ":hrRec" opt; do
2020-05-02 22:55:34 +00:00
case "$opt" in
r)
selected_action="revert"
break
;;
R)
selected_action="reset"
break
;;
e)
selected_action="edit"
break
;;
c)
selected_action="checkout"
break
;;
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
confirm=$(get_confirmation)
[[ "${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