refactor(fedit): update fedit argument parsing

pull/13/head
kevin zhuang 4 years ago
parent 2d1fd20dd1
commit 52212228f9

@ -7,11 +7,11 @@
# ${mydir}: current directory of the script # ${mydir}: current directory of the script
# ${edit_type}: which type to edit, all files, modified files, commit # ${edit_type}: which type to edit, all files, modified files, commit
# ${selected_commit}: selected commit to edit # ${selected_commit}: selected commit to edit
# ${selected_files}: selected file to edit # ${selected_files}: arrays of selected file to edit
# Arguments # Arguments
# -m: display modified file only # -m|--modified: display modified file only
# -c: edit commit using interactive rebase # -c|--commit: edit commit using interactive rebase
# -h: show helpe message and exit # -h|--help: show helpe message and exit
set -e set -e
set -f set -f
@ -22,30 +22,33 @@ source "${mydir}"/../helper/git_query.sh
function usage() { function usage() {
echo -e "Usage: dotbare fedit [-h] [-m] [-c] ...\n" echo -e "Usage: dotbare fedit [-h] [-m] [-c] ...\n"
echo -e "Select files/commits through fzf and edit selected files/commits\n" echo -e "Select files/commits through fzf and edit selected files/commits in EDITOR\n"
echo -e "Default: list all dotfiles and edit the selected files\n" echo -e "Default: list all tracked dotfiles and edit the selected files\n"
echo -e "optional arguments:" echo -e "optional arguments:"
echo -e " -h\t\tshow this help message and exit" echo -e " -h, --help\t\tshow this help message and exit"
echo -e " -m\t\tonly display modified file" echo -e " -m, --modified\t\tonly display and edit modified files"
echo -e " -c\t\tedit commit using interactive rebase instead" echo -e " -c, --commit\t\tedit commit using interactive rebase"
} }
edit_type='all' edit_type='all'
selected_files=()
while getopts ":hmc" opt; do while [[ "$#" -gt 0 ]]; do
case "$opt" in case "$1" in
m) -m|--modified)
edit_type='modified' edit_type="modified"
shift
;; ;;
c) -c|--commit)
edit_type='commit' edit_type="commit"
shift
;; ;;
h) -h|--help)
usage usage
exit 0 exit 0
;; ;;
*) *)
echo "Invalid option: ${OPTARG}" >&2 echo "Invalid option $1" >&2
usage usage
exit 1 exit 1
;; ;;
@ -57,12 +60,15 @@ if [[ "${edit_type}" == "commit" ]]; then
[[ -z "${selected_commit}" ]] && exit 1 [[ -z "${selected_commit}" ]] && exit 1
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" rebase -i "${selected_commit}"~ /usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" rebase -i "${selected_commit}"~
else else
if [[ "${edit_type}" == "modified" ]]; then while IFS= read -r line; do
selected_files=$(get_modified_file "Select tracked files to edit") selected_files+=("${line}")
else done < <(
selected_files=$(get_git_file "Select tracked files to edit") if [[ "${edit_type}" == "modified" ]]; then
fi get_modified_file "select modified files to edit"
[[ -z "${selected_files}" ]] && exit 1 else
# shellcheck disable=SC2086 get_git_file "select tracked files to edit"
command "${EDITOR}" ${selected_files} fi
)
[[ "${#selected_files[@]}" -eq 0 ]] && exit 1
exec "${EDITOR}" "${selected_files[@]}"
fi fi

Loading…
Cancel
Save