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/finit

130 lines
3.5 KiB
Bash

#!/usr/bin/env bash
#
# init or migrate the git bare repo
#
# @params
# Globals
# ${mydir}: current dir of the script
# ${confirm}: confirm status of the user
# ${post_hooks}: array of post checkout actions to perform
# ${remote_url}; remote_url to clone and migrate
# Arguments
# -h|--help: show help message and exit
# -u URL|--url URL: specify remote dotfiles url to init
# -s|--select: clone submodules after checkout
# -y|--yes: 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] ...
Init the git bare repository if doesn't exist or migrate existing dotfiles to current system.
The bare repository will be initialised under \$DOTBARE_DIR, default to \$HOME/.cfg if not set.
It will track \$DOTBARE_TREE, default to \$HOME if not set.
Migration example:
dotbare finit -u URL --submodule
Default: init the bare repository at $DOTBARE_DIR.
Optional arguments:
-h, --help\t\tshow this help message and exit.
-u URL, --url URL\tmigrate existing dotfiles from remote git repo to current system.
-s, --submodule\tclone submodules during migration.
-y, --yes\t\tacknowledge all actions that will be taken and skip confirmation."
}
remote_url=""
post_hooks=()
confirm=""
while [[ "$#" -gt 0 ]]; do
case "$1" in
-s|--submodule)
[[ ! "${post_hooks[*]}" =~ submodule ]] && \
post_hooks+=("submodule")
shift
;;
-u|--url)
[[ -z "$2" ]] && echo "Invalid option: $1" >&2 && usage && exit 1
remote_url="$2"
shift
shift
;;
-y|--yes)
confirm='y'
shift
;;
-h|--help)
usage
exit 0
;;
*)
echo "Invalid option: $1" >&2
usage
exit 1
;;
esac
done
if [[ -z "${remote_url}" ]]; then
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}"
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}"
set +e
while ! git --git-dir "${DOTBARE_DIR}" --work-tree "${DOTBARE_TREE}" checkout 2> /dev/null; do
echo "Resolving conflicts ..."
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 2> /dev/null
if git --git-dir "${DOTBARE_DIR}" --work-tree "${DOTBARE_TREE}" checkout 2> /dev/null; then
echo "All conflicts resolved"
break
fi
done
set -e
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 ..."
git --git-dir "${DOTBARE_DIR}" --work-tree "${DOTBARE_TREE}" \
submodule update --init --recursive
;;
esac
done
echo "Migration completed"
exit 0
fi