2
0
mirror of https://github.com/kazhala/dotbare synced 2024-11-17 21:25:32 +00:00
dotbare/scripts/flog

155 lines
4.4 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-06-25 02:19:04 +00:00
# -h|--help: display help message
# -r|--revert: revert the selected commit
# -R|--reset: reset HEAD back to the selected commit
# -e|--edit: edit commmit (interactive rebase)
# -c|--checkout: checkout selected commmit
# -y|--yes: confirm action by default and skip confirmation
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-06-25 02:41:25 +00:00
echo -e "Usage: dotbare flog [-h] [-r] [-R] [-e] [-c] [-y] ...\n"
echo -e "Interactive log viewer with action menu."
echo -e "Action menu contains options including revert|reset|edit|checkout|exit.\n"
echo -e "Default: list all commits and prompt a menu to select action to perform.\n"
2020-05-02 22:55:34 +00:00
echo -e "optional arguments:"
echo -e " -h, --help\t\tshow this help message and exit."
echo -e " -r, --revert\t\trevert the selected commit and skip action menu."
echo -e " -R, --reset\t\treset HEAD back to selected commit and skip action menu."
echo -e " -e, --edit\t\tedit selected commit through interactive rebase and skip action menu."
echo -e " -c, --checkout\tcheckout selected commit and skip action menu."
echo -e " -y, --yes\t\tacknowledge all actions that will be taken 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"
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-06-25 02:19:04 +00:00
| awk -F ":" '{
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-06-25 02:19:04 +00:00
selected_commit=""
confirm=""
2020-05-02 22:55:34 +00:00
2020-06-25 02:19:04 +00:00
while [[ "$#" -gt 0 ]]; do
case "$1" in
-r|--revert)
2020-05-02 22:55:34 +00:00
selected_action="revert"
2020-06-25 02:19:04 +00:00
shift
2020-05-02 22:55:34 +00:00
;;
2020-06-25 02:19:04 +00:00
-R|--reset)
2020-05-02 22:55:34 +00:00
selected_action="reset"
2020-06-25 02:19:04 +00:00
shift
2020-05-02 22:55:34 +00:00
;;
2020-06-25 02:19:04 +00:00
-e|--edit)
2020-05-02 22:55:34 +00:00
selected_action="edit"
2020-06-25 02:19:04 +00:00
shift
2020-05-02 22:55:34 +00:00
;;
2020-06-25 02:19:04 +00:00
-c|--checkout)
2020-05-02 22:55:34 +00:00
selected_action="checkout"
2020-06-25 02:19:04 +00:00
shift
2020-05-15 08:04:43 +00:00
;;
2020-06-25 02:19:04 +00:00
-y|--yes)
2020-05-15 08:04:43 +00:00
confirm='y'
2020-06-25 02:19:04 +00:00
shift
2020-05-02 22:55:34 +00:00
;;
2020-06-25 02:19:04 +00:00
-h|--help)
2020-05-02 22:55:34 +00:00
usage
exit 0
;;
*)
2020-06-25 02:19:04 +00:00
echo "Invalid option: $1" >&2
2020-05-02 22:55:34 +00:00
usage
exit 1
;;
esac
done
2020-05-02 23:16:18 +00:00
2020-05-17 11:03:04 +00:00
while :; do
2020-05-02 23:16:18 +00:00
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
2020-06-25 02:19:04 +00:00
if [[ "${selected_action}" == "reset" ]] && [[ -z "${confirm}" ]]; then
2020-05-02 23:34:31 +00:00
echo "(dryrun) reset HEAD to ${selected_commit}"
2020-06-25 02:19:04 +00:00
elif [[ -z "${confirm}" ]]; then
2020-05-02 23:34:31 +00:00
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