#!/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] ... Interactive log viewer with action menu. Action menu contains options including revert|reset|edit|checkout|exit. Default: list all commits and prompt a menu to select action to perform. Optional arguments: -h, --help\t\tshow this help message and exit. -r, --revert\t\trevert the selected commit and skip action menu. -R, --reset\t\treset HEAD back to selected commit and skip action menu. -e, --edit\t\tedit selected commit through interactive rebase and skip action menu. -c, --checkout\tcheckout selected commit and skip action menu. -y, --yes\t\tacknowledge all actions that will be taken 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=$( 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) git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \ revert "${selected_commit}" ;; reset) git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \ reset "${selected_commit}" ;; edit) git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \ rebase -i "${selected_commit}"~ ;; checkout) git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \ checkout "${selected_commit}" ;; exit) exit 0 ;; *) exit 1 ;; esac