2
0
mirror of https://github.com/kazhala/dotbare synced 2024-11-10 01:10:25 +00:00
dotbare/scripts/fedit
2020-05-19 18:01:01 +10:00

69 lines
1.8 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# interactive menu to select file/commit to edit
#
# @params
# Globals
# ${mydir}: current directory of the script
# ${edit_type}: which type to edit, all files, modified files, commit
# ${selected_commit}: selected commit to edit
# ${selected_files}: selected file to edit
# Arguments
# -m: display modified file only
# -c: edit commit using interactive rebase
# -h: show helpe message and exit
set -e
set -f
mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "${mydir}"/../helper/set_variable.sh
source "${mydir}"/../helper/git_query.sh
function usage() {
echo -e "Usage: dotbare fedit [-h] [-m] [-c] ...\n"
echo -e "Select files/commits through fzf and edit selected files/commits\n"
echo -e "Default: list all dotfiles and edit the selected files\n"
echo -e "optional arguments:"
echo -e " -h\t\tshow this help message and exit"
echo -e " -m\t\tonly display modified file"
echo -e " -c\t\tedit commit using interactive rebase instead"
}
edit_type='all'
while getopts ":hmc" opt; do
case "$opt" in
m)
edit_type='modified'
;;
c)
edit_type='commit'
;;
h)
usage
exit 0
;;
*)
echo "Invalid option: ${OPTARG}" >&2
usage
exit 1
;;
esac
done
if [[ "${edit_type}" == "commit" ]]; then
selected_commit=$(get_commit "Select a commit to rename")
[[ -z "${selected_commit}" ]] && exit 1
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" rebase -i "${selected_commit}"~
else
if [[ "${edit_type}" == "modified" ]]; then
selected_files=$(get_modified_file "Select tracked files to edit")
else
selected_files=$(get_git_file "Select tracked files to edit")
fi
[[ -z "${selected_files}" ]] && exit 1
# shellcheck disable=SC2086
command "${EDITOR}" ${selected_files}
fi