2
0
mirror of https://github.com/kazhala/dotbare synced 2024-11-08 07:10:24 +00:00
dotbare/scripts/finit

98 lines
3.0 KiB
Plaintext
Raw Normal View History

2020-05-11 01:56:27 +00:00
#!/usr/bin/env bash
2020-05-09 01:06:30 +00:00
#
# init the git bare repo
#
# @params
# Globals
# ${mydir}: current dir of the script
# ${confirm}: confirm status of the user
# Arguments
# -h: show help message and exit
2020-05-09 01:28:17 +00:00
# -u: specify remote dotfiles url to init
2020-05-09 01:06:30 +00:00
set -e
set -f
2020-05-13 07:24:25 +00:00
mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
2020-05-09 01:06:30 +00:00
source "${mydir}"/../helper/set_variable.sh
source "${mydir}"/../helper/get_confirmation.sh
function usage() {
2020-05-13 03:52:28 +00:00
echo -e "Usage: dotbare finit [-h] [-u URL] ...\n"
2020-05-09 01:06:30 +00:00
echo -e "Init the git bare repository if doesn't exist"
2020-05-09 01:08:48 +00:00
# shellcheck disable=SC2016
2020-05-09 01:06:30 +00:00
echo -e 'The bare repository will be initialised under ${DOTBARE_DIR}, default to $HOME/.cfg if not set'
2020-05-09 01:08:48 +00:00
# shellcheck disable=SC2016
2020-05-09 01:06:30 +00:00
echo -e 'It will track ${DOTBARE_TREE}, default to $HOME if not set\n'
echo -e "optional arguments:"
echo -e " -h\t\tshow this help message and exit"
2020-05-09 01:28:17 +00:00
echo -e " -u URL\tmigrate existing dotfiles from the git URL to current system"
2020-05-09 01:06:30 +00:00
}
2020-05-09 01:28:17 +00:00
remote_url=""
2020-05-09 01:47:41 +00:00
while getopts ":hu:" opt; do
2020-05-09 01:06:30 +00:00
case "$opt" in
2020-05-09 01:28:17 +00:00
u)
remote_url="${OPTARG}"
break
;;
2020-05-09 01:06:30 +00:00
h)
usage
exit 0
;;
*)
echo "Invalid option: ${OPTARG}" >&2
usage
exit 1
;;
esac
done
2020-05-09 01:28:17 +00:00
if [[ -z "${remote_url}" ]]; then
echo "modify DOTBARE_DIR and DOTBARE_TREE to customize location, more information run dotbare finit -h"
echo "git bare repository will be initialised at ${DOTBARE_DIR}"
echo "git bare repository will be tracking ${DOTBARE_TREE}"
2020-05-13 02:02:58 +00:00
[[ -z "${confirm}" ]] && confirm=$(get_confirmation)
2020-05-09 01:28:17 +00:00
[[ "${confirm}" != 'y' ]] && exit 1
2020-05-09 01:06:30 +00:00
2020-05-09 01:28:17 +00:00
if [[ -d "${DOTBARE_DIR}" ]]; then
echo "${DOTBARE_DIR} already exist"
2020-05-13 02:02:58 +00:00
exit 1
2020-05-09 01:28:17 +00:00
else
git init --bare "${DOTBARE_DIR}"
/usr/bin/git --git-dir "${DOTBARE_DIR}" --work-tree "${DOTBARE_TREE}" \
config --local status.showUntrackedFiles no
fi
2020-05-09 01:06:30 +00:00
else
2020-05-13 02:19:38 +00:00
[[ ! -d "${DOTBARE_TREE}" ]] && mkdir -p "${DOTBARE_TREE}"
2020-05-09 01:28:17 +00:00
cd "${DOTBARE_TREE}"
if [[ -f ".gitignore" ]]; then
pattern=$(basename "${DOTBARE_DIR}")
pattern="^${pattern}$"
exists=$(awk -v pattern="${pattern}" '{ if ($0 ~ pattern) { print $0; } }' .gitignore)
[[ -z "${exists}" ]] && basename "${DOTBARE_DIR}" >> .gitignore
else
basename "${DOTBARE_DIR}" >> .gitignore
fi
2020-05-09 01:28:17 +00:00
git clone --bare "${remote_url}" "${DOTBARE_DIR}"
2020-05-09 04:02:00 +00:00
if ! /usr/bin/git --git-dir "${DOTBARE_DIR}" --work-tree "${DOTBARE_TREE}" checkout 2> /dev/null; then
echo "File checkout failed"
echo "Backing up pre-existing dotfiles ..."
/usr/bin/git --git-dir "${DOTBARE_DIR}" --work-tree "${DOTBARE_TREE}" checkout 2>&1 \
| awk '{
if ($0 ~ /[\t].*/) {
gsub(/^[\t]/, "", $0)
print $0
}
}' \
| xargs -I __ "${mydir}"/fbackup -p __ -m
echo "dotfiles backup succeeded, checkout continue"
/usr/bin/git --git-dir "${DOTBARE_DIR}" --work-tree "${DOTBARE_TREE}" checkout
/usr/bin/git --git-dir "${DOTBARE_DIR}" --work-tree "${DOTBARE_TREE}" \
config --local status.showUntrackedFiles no
fi
echo "File checkout succeeded"
echo "Migration completed"
2020-05-13 04:55:44 +00:00
exit 0
2020-05-09 01:06:30 +00:00
fi