You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
dotbare/scripts/flog

149 lines
4.1 KiB
Bash

#!/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: 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
# -y: 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] ...\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\t\tshow this help message and exit"
echo -e " -r\t\trevert the selected commit"
echo -e " -R\t\treset HEAD back to selected commit"
echo -e " -e\t\tedit selected commit through interactive rebase"
echo -e " -c\t\tcheckout selected commit"
echo -e " -y\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 '{
gsub(/:/, "", $1)
print $1
}'
)
echo "${selected_action}"
fi
}
selected_action=""
while getopts ":hrRecy" opt; do
case "$opt" in
r)
selected_action="revert"
;;
R)
selected_action="reset"
;;
e)
selected_action="edit"
;;
c)
selected_action="checkout"
;;
y)
confirm='y'
;;
h)
usage
exit 0
;;
*)
echo "Invalid option: ${OPTARG}" >&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" ]]; then
echo "(dryrun) reset HEAD to ${selected_commit}"
else
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