You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
dotbare/scripts/fadd

79 lines
1.9 KiB
Plaintext

4 years ago
#!/usr/bin/env bash
4 years ago
#
# Stage the selected file to git bare repo
#
# @params
# Globals
# ${mydir}: string, current directory of the executing script
4 years ago
# ${stage_type}: modified, new file, or directory to stage
# ${selected_files}: user selected files to stage
# Arguments
# -h: show help message
# -f: select a file in PWD to stage
# -d: select a directory in PWD to stage
4 years ago
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"
4 years ago
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:"
4 years ago
echo -e " -h\t\tshow this help message and exit"
echo -e " -f\t\tselect a file in current directory and stage it"
echo -e " -d\t\tselect a entire folder to stage"
}
#######################################
# stage file
# Arguments:
# $1: files to stage
#######################################
4 years ago
function stage_file() {
local file=$1
4 years ago
if [[ -z "${file}" ]]; then
4 years ago
exit 1
4 years ago
else
# shellcheck disable=SC2086
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" add ${file}
4 years ago
fi
}
4 years ago
stage_type="modified"
4 years ago
while getopts ":fhd" opt
do
case "$opt" in
f)
4 years ago
stage_type="file"
;;
d)
stage_type="dir"
4 years ago
;;
h)
usage
4 years ago
exit 0
;;
*)
echo "Invalid option: ${OPTARG}" >&2
usage
4 years ago
exit 1
;;
esac
done
4 years ago
if [[ "${stage_type}" == "file" ]]; then
selected_files=$(search_file 'f' | tr '\n' ' ')
elif [[ "${stage_type}" == "dir" ]]; then
selected_files=$(search_file 'd' | tr '\n' ' ')
4 years ago
else
selected_files=$(get_modified_file 'select files to stage' "unstaged" | tr '\n' ' ')
4 years ago
fi
4 years ago
stage_file "${selected_files}"