mirror of
https://github.com/kazhala/dotbare
synced 2024-11-06 09:20:25 +00:00
82 lines
1.8 KiB
Bash
Executable File
82 lines
1.8 KiB
Bash
Executable File
#!/bin/bash
|
|
#
|
|
# Stage the selected file to git bare repo
|
|
#
|
|
# @params
|
|
# Globals
|
|
# ${mydir}: string, current directory of the executing script
|
|
# ${new_file}: new file path to stage
|
|
# ${new_folder}: new folder to stage
|
|
# ${stage_file}: changed file to stage
|
|
|
|
set -e
|
|
|
|
mydir="${0%/*}"
|
|
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 "Stage the selected file to the dotfile gitbare repo"
|
|
echo -e "Press escape to stop staging file\n"
|
|
echo -e "optional arguments:"
|
|
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"
|
|
}
|
|
|
|
function stage_file() {
|
|
local file=$1
|
|
if [[ -z "${file}" ]]
|
|
then
|
|
exit 0
|
|
else
|
|
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" add "${file}"
|
|
echo "${file} staged successfully"
|
|
fi
|
|
}
|
|
|
|
new_file=''
|
|
new_folder=''
|
|
|
|
while getopts ":fhd" opt
|
|
do
|
|
case "$opt" in
|
|
f)
|
|
new_file=$(search_file 'f')
|
|
[[ -z "${new_file}" ]] && exit 1
|
|
break
|
|
;;
|
|
h)
|
|
usage
|
|
exit 0
|
|
;;
|
|
d)
|
|
new_folder=$(search_file 'd')
|
|
[[ -z "${new_folder}" ]] && exit 1
|
|
break
|
|
;;
|
|
*)
|
|
echo "Invalid option: ${OPTARG}" >&2
|
|
usage
|
|
exit 1
|
|
;;
|
|
esac
|
|
done
|
|
|
|
if [[ -n "${new_file}" ]]; then
|
|
while IFS= read -r line; do
|
|
stage_file "${line}"
|
|
done <<< "${new_file}"
|
|
elif [[ -n "${new_folder}" ]]; then
|
|
while IFS= read -r line; do
|
|
stage_file "${line}"
|
|
done <<< "${new_folder}"
|
|
else
|
|
selected_files=$(get_modified_file 'select files to stage')
|
|
while IFS= read -r line; do
|
|
stage_file "${line}"
|
|
done <<< "${selected_files}"
|
|
fi
|