2
0
mirror of https://github.com/kazhala/dotbare synced 2024-11-02 09:40:27 +00:00

refactor(fedit): update fedit argument parsing

This commit is contained in:
kevin zhuang 2020-06-25 08:10:44 +10:00
parent 2d1fd20dd1
commit 52212228f9

View File

@ -7,11 +7,11 @@
# ${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
# ${selected_files}: arrays of selected file to edit
# Arguments
# -m: display modified file only
# -c: edit commit using interactive rebase
# -h: show helpe message and exit
# -m|--modified: display modified file only
# -c|--commit: edit commit using interactive rebase
# -h|--help: show helpe message and exit
set -e
set -f
@ -22,30 +22,33 @@ 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 "Select files/commits through fzf and edit selected files/commits in EDITOR\n"
echo -e "Default: list all tracked 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"
echo -e " -h, --help\t\tshow this help message and exit"
echo -e " -m, --modified\t\tonly display and edit modified files"
echo -e " -c, --commit\t\tedit commit using interactive rebase"
}
edit_type='all'
selected_files=()
while getopts ":hmc" opt; do
case "$opt" in
m)
edit_type='modified'
while [[ "$#" -gt 0 ]]; do
case "$1" in
-m|--modified)
edit_type="modified"
shift
;;
c)
edit_type='commit'
-c|--commit)
edit_type="commit"
shift
;;
h)
-h|--help)
usage
exit 0
;;
*)
echo "Invalid option: ${OPTARG}" >&2
echo "Invalid option $1" >&2
usage
exit 1
;;
@ -57,12 +60,15 @@ if [[ "${edit_type}" == "commit" ]]; then
[[ -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}
while IFS= read -r line; do
selected_files+=("${line}")
done < <(
if [[ "${edit_type}" == "modified" ]]; then
get_modified_file "select modified files to edit"
else
get_git_file "select tracked files to edit"
fi
)
[[ "${#selected_files[@]}" -eq 0 ]] && exit 1
exec "${EDITOR}" "${selected_files[@]}"
fi