mirror of
https://github.com/kazhala/dotbare
synced 2024-11-17 21:25:32 +00:00
114 lines
3.4 KiB
Bash
Executable File
114 lines
3.4 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
#
|
|
# init the git bare repo
|
|
#
|
|
# @params
|
|
# Globals
|
|
# ${mydir}: current dir of the script
|
|
# ${confirm}: confirm status of the user
|
|
# ${post_hooks}: post checkout actions to perform
|
|
# Arguments
|
|
# -h: show help message and exit
|
|
# -u: specify remote dotfiles url to init
|
|
# -s: clone submodules after checkout
|
|
# -y: confirm action by default and skip confirmation
|
|
|
|
set -e
|
|
set -f
|
|
|
|
mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
|
source "${mydir}"/../helper/set_variable.sh
|
|
source "${mydir}"/../helper/get_confirmation.sh
|
|
|
|
function usage() {
|
|
echo -e "Usage: dotbare finit [-h] [-y] [-s] [-u URL] ...\n"
|
|
echo -e "Init the git bare repository if doesn't exist"
|
|
# shellcheck disable=SC2016
|
|
echo -e 'The bare repository will be initialised under $DOTBARE_DIR, default to $HOME/.cfg if not set'
|
|
# shellcheck disable=SC2016
|
|
echo -e 'It will track $DOTBARE_TREE, default to $HOME if not set\n'
|
|
echo -e "Default: init the bare repository at $DOTBARE_DIR\n"
|
|
echo -e "optional arguments:"
|
|
echo -e " -h\t\tshow this help message and exit"
|
|
echo -e " -u URL\tmigrate existing dotfiles from the git URL to current system"
|
|
echo -e " -s\t\tclone submodules after checkout"
|
|
echo -e " -y\t\tconfirm action by default and skip confirmation"
|
|
}
|
|
|
|
remote_url=""
|
|
post_hooks=()
|
|
while getopts ":hu:ys" opt; do
|
|
case "$opt" in
|
|
s)
|
|
[[ ! "${post_hooks[*]}" =~ submodule ]] && \
|
|
post_hooks+=("submodule")
|
|
;;
|
|
u)
|
|
remote_url="${OPTARG}"
|
|
;;
|
|
y)
|
|
confirm='y'
|
|
;;
|
|
h)
|
|
usage
|
|
exit 0
|
|
;;
|
|
*)
|
|
echo "Invalid option: ${OPTARG}" >&2
|
|
usage
|
|
exit 1
|
|
;;
|
|
esac
|
|
done
|
|
|
|
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}"
|
|
[[ -z "${confirm}" ]] && confirm=$(get_confirmation)
|
|
[[ "${confirm}" != 'y' ]] && exit 1
|
|
|
|
if [[ -d "${DOTBARE_DIR}" ]]; then
|
|
echo "${DOTBARE_DIR} already exist"
|
|
exit 1
|
|
else
|
|
git init --bare "${DOTBARE_DIR}"
|
|
/usr/bin/git --git-dir "${DOTBARE_DIR}" --work-tree "${DOTBARE_TREE}" \
|
|
config --local status.showUntrackedFiles no
|
|
fi
|
|
else
|
|
[[ ! -d "${DOTBARE_TREE}" ]] && mkdir -p "${DOTBARE_TREE}"
|
|
cd "${DOTBARE_TREE}"
|
|
git clone --bare "${remote_url}" "${DOTBARE_DIR}"
|
|
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
|
|
fi
|
|
/usr/bin/git --git-dir "${DOTBARE_DIR}" --work-tree "${DOTBARE_TREE}" \
|
|
config --local status.showUntrackedFiles no
|
|
echo "File checkout succeeded"
|
|
|
|
for hook in "${post_hooks[@]}"; do
|
|
case "${hook}" in
|
|
submodule)
|
|
echo "Cloning submodules ..."
|
|
/usr/bin/git --git-dir "${DOTBARE_DIR}" --work-tree "${DOTBARE_TREE}" \
|
|
submodule update --init --recursive
|
|
;;
|
|
esac
|
|
done
|
|
|
|
echo "Migration completed"
|
|
exit 0
|
|
fi
|