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

83 lines
2.0 KiB
Plaintext
Raw Normal View History

2020-05-11 01:56:27 +00:00
#!/usr/bin/env bash
2020-04-06 05:07:59 +00:00
#
# Stage the selected file to git bare repo
#
# @params
# Globals
2020-04-07 06:51:07 +00:00
# ${mydir}: string, current directory of the executing script
2020-05-15 08:50:26 +00:00
# ${stage_type}: modified, new file, or directory to stage
2020-06-21 22:47:25 +00:00
# ${selected_files}: bash array of user selected files to stage
2020-04-24 07:26:45 +00:00
# Arguments
2020-06-21 22:47:25 +00:00
# -h|--help: show help message
# -f|--file: select a file in PWD to stage
# -d|--dir: select a directory in PWD to stage
2020-04-06 05:07:59 +00:00
2020-04-10 05:05:33 +00:00
set -e
2020-04-17 07:55:22 +00:00
set -f
2020-04-10 05:05:33 +00:00
2020-05-13 07:30:54 +00:00
mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
2020-04-14 07:17:42 +00:00
source "${mydir}"/../helper/search_file.sh
source "${mydir}"/../helper/set_variable.sh
2020-04-16 05:43:33 +00:00
source "${mydir}"/../helper/git_query.sh
2020-04-07 04:45:58 +00:00
function usage() {
echo -e "Usage: dotbare fadd [-h] [-f] [-d] ...\n"
2020-06-21 22:47:25 +00:00
echo -e "Select files/directory or modified files through fzf"
2020-05-19 07:51:13 +00:00
echo -e "Stage the selected file to the dotfile gitbare repo\n"
echo -e "Default: list all modified files and stage the selected files.\n"
2020-04-07 04:45:58 +00:00
echo -e "optional arguments:"
echo -e " -h, --help\t\tshow this help message and exit"
echo -e " -f, --file\t\tselect a file in current directory and stage it"
echo -e " -d, --dir\t\tselect a entire folder to stage"
2020-04-07 04:45:58 +00:00
}
2020-04-24 07:26:45 +00:00
#######################################
# stage file
# Arguments:
2020-06-21 22:47:25 +00:00
# $1: array of files to stage
2020-04-24 07:26:45 +00:00
#######################################
2020-04-06 05:07:59 +00:00
function stage_file() {
2020-06-21 22:47:25 +00:00
local files=("$@")
[[ "${#files[@]}" -eq 0 ]] && exit 1
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" add "${files[@]}"
2020-04-06 05:07:59 +00:00
}
2020-05-15 08:50:26 +00:00
stage_type="modified"
2020-06-21 22:47:25 +00:00
selected_files=()
2020-04-06 05:07:59 +00:00
while [[ "$#" -gt 0 ]]; do
case "$1" in
-f|--file)
2020-05-15 08:50:26 +00:00
stage_type="file"
shift
2020-05-15 08:50:26 +00:00
;;
-d|--dir)
2020-05-15 08:50:26 +00:00
stage_type="dir"
shift
2020-04-06 05:07:59 +00:00
;;
-h|--help)
2020-04-07 04:45:58 +00:00
usage
2020-04-06 05:07:59 +00:00
exit 0
;;
*)
echo "Invalid option: $1" >&2
2020-04-07 04:45:58 +00:00
usage
2020-04-06 05:07:59 +00:00
exit 1
;;
esac
done
2020-06-21 22:47:25 +00:00
while IFS= read -r line; do
selected_files+=("${line}")
done < <(
if [[ "${stage_type}" == "file" ]]; then
search_file 'f'
elif [[ "${stage_type}" == "dir" ]]; then
search_file 'd'
else
get_modified_file "select files to stage" "unstaged"
fi
)
stage_file "${selected_files[@]}"