2
0
mirror of https://github.com/kazhala/dotbare synced 2024-11-04 06:00:45 +00:00
dotbare/scripts/fadd
2020-06-28 10:05:49 +10:00

83 lines
2.0 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# Stage the selected file to git bare repo
#
# @params
# Globals
# ${mydir}: string, current directory of the executing script
# ${stage_type}: modified, new file, or directory to stage
# ${selected_files}: bash array of user selected files to stage
# Arguments
# -h|--help: show help message
# -f|--file: select a file in PWD to stage
# -d|--dir: select a directory in PWD to stage
set -e
set -f
mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "${mydir}"/../helper/search_file.sh
source "${mydir}"/../helper/set_variable.sh
source "${mydir}"/../helper/git_query.sh
function usage() {
echo -e "Usage: dotbare fadd [-h] [-f] [-d] ...\n"
echo -e "Select files/directory or modified files through fzf"
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"
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"
}
#######################################
# stage file
# Arguments:
# $1: array of files to stage
#######################################
function stage_file() {
local files=("$@")
[[ "${#files[@]}" -eq 0 ]] && exit 1
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" add "${files[@]}"
}
stage_type="modified"
selected_files=()
while [ "$#" -gt 0 ]; do
case "$1" in
-f|--file)
stage_type="file"
shift
;;
-d|--dir)
stage_type="dir"
shift
;;
-h|--help)
usage
exit 0
;;
*)
echo "Invalid option: $1" 1>&2
usage
exit 1
;;
esac
done
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[@]}"