2
0
mirror of https://github.com/kazhala/dotbare synced 2024-11-19 15:25:46 +00:00
dotbare/scripts/flog
2020-06-28 10:05:49 +10:00

155 lines
4.3 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# git log interactive viewer
#
# @params
# Globals
# ${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
# Arguments
# -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
set -e
set -f
mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
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] [-r] [-R] [-e] [-c] [-y] ...\n"
echo -e "Interactive log viewer with action menu"
echo -e "Action menu contains command including revert|reset|edit|checkout|exit\n"
echo -e "Default: list all commits and prompt a menu for user to take action on the selected commit\n"
echo -e "optional arguments:"
echo -e " -h, --help\t\tshow this help message and exit"
echo -e " -r, --revert\t\trevert the selected commit"
echo -e " -R, --reset\t\treset HEAD back to selected commit"
echo -e " -e, --edit\t\tedit selected commit through interactive rebase"
echo -e " -c, --checkout\t\tcheckout selected commit"
echo -e " -y, --yes\t\tconfirm action by default and skip confirmation"
}
#######################################
# draw action menu for selected commit
# Arguments:
# $1: selected commit hash, used to display commit message in fzf header
# $2: selected action, if selected, skip menu, return action
# Outputs:
# ${selected_action}: user selected action
#######################################
function draw_menu() {
local selected_commit="$1"
local selected_action="$2"
local menu header message
if [[ -n "$selected_action" ]]; then
echo "${selected_action}"
else
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}"
selected_action=$(echo -e "${menu}" \
| fzf --no-multi --header="${header}" \
| awk -F ":" '{
print $1
}'
)
echo "${selected_action}"
fi
}
selected_action=""
selected_commit=""
confirm=""
while [[ "$#" -gt 0 ]]; do
case "$1" in
-r|--revert)
selected_action="revert"
shift
;;
-R|--reset)
selected_action="reset"
shift
;;
-e|--edit)
selected_action="edit"
shift
;;
-c|--checkout)
selected_action="checkout"
shift
;;
-y|--yes)
confirm='y'
shift
;;
-h|--help)
usage
exit 0
;;
*)
echo "Invalid option: $1" >&2
usage
exit 1
;;
esac
done
while :; do
selected_commit=$(get_commit)
[[ -z "${selected_commit}" ]] && exit 1
selected_action=$(draw_menu "${selected_commit}" "${selected_action}")
[[ -n "${selected_action}" ]] && break
done
if [[ "${selected_action}" != 'exit' ]]; then
if [[ "${selected_action}" == "reset" ]] && [[ -z "${confirm}" ]]; then
echo "(dryrun) reset HEAD to ${selected_commit}"
elif [[ -z "${confirm}" ]]; then
echo "(dryrun) ${selected_action} ${selected_commit}"
fi
[[ -z "${confirm}" ]] && 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