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

79 lines
2.1 KiB
Plaintext
Raw Normal View History

2020-04-06 05:07:59 +00:00
#!/bin/bash
#
# Stage the selected file to git bare repo
#
# @params
# Globals
# ${new_file}: new file path to stage
# ${new_folder}: new folder to stage
# ${stage_file}: changed file to stage
2020-04-07 04:45:58 +00:00
[[ -z "${DOTBARE_DIR}" ]] && DOTBARE_DIR="$HOME/.cfg/"
[[ -z "${DOTBARE_TREE}" ]] && DOTBARE_TREE="$HOME"
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"
}
2020-04-06 05:07:59 +00:00
function stage_file() {
local file=$1
if [[ -z "${file}" ]]
then
exit 0
else
2020-04-07 04:45:58 +00:00
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" add "${file}"
2020-04-06 05:07:59 +00:00
echo "${file} staged successfully"
fi
}
new_file=''
new_folder=''
while getopts ":fhd" opt
do
case "$opt" in
f)
new_file=$(fd -H -d 1 -t f | fzf --multi --exit-0 --preview "head -50 {}")
[[ -z "${new_file}" ]] && exit 1
break
;;
h)
2020-04-07 04:45:58 +00:00
usage
2020-04-06 05:07:59 +00:00
exit 0
;;
d)
# don't allow add directory from home folder to decrease errors
[[ "$PWD" == "$HOME" ]] && exit 0
new_folder=$(fd -H -d 1 -t d -E .git | fzf --exit-0 --preview "tree -L 1 -C --dirsfirst {}")
[[ -z "${new_folder}" ]] && exit 1
break
;;
*)
echo "Invalid option: ${OPTARG}" >&2
2020-04-07 04:45:58 +00:00
usage
2020-04-06 05:07:59 +00:00
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
stage_file "${new_folder}"
else
2020-04-07 04:45:58 +00:00
selected_files=$(/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" diff --name-only | \
fzf --multi --exit-0 --preview "/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" diff --color=always $HOME/{}" | \
2020-04-06 05:07:59 +00:00
awk -v home=$HOME '{print home "/" $0}')
while IFS= read -r line; do
stage_file "${line}"
done <<< "${selected_files}"
fi