2020-05-11 01:56:27 +00:00
|
|
|
#!/usr/bin/env bash
|
2020-04-24 06:21:41 +00:00
|
|
|
#
|
|
|
|
# interactive menu to select file/commit to edit
|
|
|
|
#
|
|
|
|
# @params
|
|
|
|
# Globals
|
2020-04-24 07:26:45 +00:00
|
|
|
# ${mydir}: current directory of the script
|
2020-05-15 08:29:29 +00:00
|
|
|
# ${edit_type}: which type to edit, all files, modified files, commit
|
2020-04-24 07:26:45 +00:00
|
|
|
# ${selected_commit}: selected commit to edit
|
|
|
|
# ${selected_files}: selected file to edit
|
2020-04-24 06:21:41 +00:00
|
|
|
# Arguments
|
2020-04-24 07:26:45 +00:00
|
|
|
# -m: display modified file only
|
|
|
|
# -c: edit commit using interactive rebase
|
|
|
|
# -h: show helpe message and exit
|
2020-04-24 06:21:41 +00:00
|
|
|
|
|
|
|
set -e
|
|
|
|
set -f
|
|
|
|
|
2020-05-13 07:30:54 +00:00
|
|
|
mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
2020-04-24 06:21:41 +00:00
|
|
|
source "${mydir}"/../helper/set_variable.sh
|
|
|
|
source "${mydir}"/../helper/git_query.sh
|
|
|
|
|
|
|
|
function usage() {
|
2020-05-13 03:52:28 +00:00
|
|
|
echo -e "Usage: dotbare fedit [-h] [-m] [-c] ...\n"
|
2020-05-19 07:51:13 +00:00
|
|
|
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"
|
2020-04-24 06:21:41 +00:00
|
|
|
echo -e "optional arguments:"
|
2020-05-19 08:01:01 +00:00
|
|
|
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"
|
2020-04-24 06:21:41 +00:00
|
|
|
}
|
|
|
|
|
2020-05-15 08:29:29 +00:00
|
|
|
edit_type='all'
|
2020-04-24 06:21:41 +00:00
|
|
|
|
2020-04-24 07:05:55 +00:00
|
|
|
while getopts ":hmc" opt; do
|
2020-04-24 06:21:41 +00:00
|
|
|
case "$opt" in
|
|
|
|
m)
|
2020-05-15 08:29:29 +00:00
|
|
|
edit_type='modified'
|
2020-04-24 06:21:41 +00:00
|
|
|
;;
|
|
|
|
c)
|
2020-05-15 08:29:29 +00:00
|
|
|
edit_type='commit'
|
2020-04-24 06:21:41 +00:00
|
|
|
;;
|
|
|
|
h)
|
|
|
|
usage
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Invalid option: ${OPTARG}" >&2
|
|
|
|
usage
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2020-05-15 08:29:29 +00:00
|
|
|
if [[ "${edit_type}" == "commit" ]]; then
|
2020-04-24 06:49:44 +00:00
|
|
|
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}"~
|
2020-04-24 06:21:41 +00:00
|
|
|
else
|
2020-05-15 08:29:29 +00:00
|
|
|
if [[ "${edit_type}" == "modified" ]]; then
|
2020-04-24 06:26:15 +00:00
|
|
|
selected_files=$(get_modified_file "Select tracked files to edit")
|
|
|
|
else
|
|
|
|
selected_files=$(get_git_file "Select tracked files to edit")
|
|
|
|
fi
|
2020-04-24 06:49:44 +00:00
|
|
|
[[ -z "${selected_files}" ]] && exit 1
|
|
|
|
# shellcheck disable=SC2086
|
2020-04-24 06:24:29 +00:00
|
|
|
command "${EDITOR}" ${selected_files}
|
2020-04-24 06:21:41 +00:00
|
|
|
fi
|