dotfiles-install: handle directories with spaces properly, auto resume installation, friendly information messages

This commit is contained in:
PowerUser64 2022-06-17 00:47:58 -07:00
parent 9008b44324
commit 24463705ee

View file

@ -1,26 +1,22 @@
#!/bin/sh #!/bin/sh
# this script will install the dotfiles from the target git repository, and back up any conflicting files # this script will install the dotfiles from the target git repository, and back up any conflicting files
# credit for this idea and basic script outline goes to https://www.atlassian.com/git/tutorials/dotfiles # credit for the basic outline of this script goes to https://www.atlassian.com/git/tutorials/dotfiles
## shellcheck
# Allow variables in single quotes
# shellcheck disable=SC2016
# read -d is not posix-complient (but it exists in sh on my machine, so it's probably fine)
# shellcheck disable=SC3045
DOTFILES_REPO_DIR="$HOME/git/dotfiles" DOTFILES_REPO_DIR="$HOME/git/dotfiles"
DOTFILES_REPO_URL="https://git.blakenorth.net/dotfiles" DOTFILES_REPO_URL="https://git.blakenorth.net/dotfiles"
DOTFILES_BACKUP_DIR="$HOME/.dotfiles-backup" DOTFILES_BACKUP_DIR="$HOME/.dotfiles-backup_$(date +'%Y-%m-%d_%H:%m:%S')"
set -e set -e
printf "\n Installing dotfiles from $DOTFILES_REPO_URL\n\n"
# check for the git command and exit if it doesn't exist # check for the git command and exit if it doesn't exist
if ! command -v git > /dev/null;then if ! command -v git > /dev/null;then
echo 'git is not installed or could not be found in PATH, please install git to proceed' echo 'git is not installed or could not be found in PATH, please install git to proceed'
exit 1 exit 1
fi fi
git clone --bare "$DOTFILES_REPO_URL" "$DOTFILES_REPO_DIR" git clone --bare "$DOTFILES_REPO_URL" "$DOTFILES_REPO_DIR" || echo "Resuming installation"
dot() { dot() {
git --git-dir="$DOTFILES_REPO_DIR" --work-tree="$HOME" "$@" git --git-dir="$DOTFILES_REPO_DIR" --work-tree="$HOME" "$@"
@ -32,15 +28,14 @@ if ! dot checkout > /dev/null 2>&1; then
mkdir -p "$DOTFILES_BACKUP_DIR" mkdir -p "$DOTFILES_BACKUP_DIR"
# get the list of files that need to be backed up # get the list of files that need to be backed up
dot checkout 2>&1 | grep -Po "(?<=\t)(.*)$" | while read -r LINE; do
while read -rd '' LINE; do LINE_DIR="$(dirname "$LINE")" # No file
LINE_DIR="${LINE%%/*}" mkdir -p "$DOTFILES_BACKUP_DIR/$LINE_DIR"
mkdir -p "$DOTFILES_BACKUP_DIR/${LINE_DIR#"$HOME"}/${LINE##*/}" mv "$HOME/$LINE" "$DOTFILES_BACKUP_DIR/$LINE_DIR"
mv "$FILE" "$DOTFILES_BACKUP_DIR" done <<< "$(dot checkout 2>&1 | grep -Po '\t\K.*$')"
done
fi fi
dot checkout dot checkout
dot config status.showUntrackedFiles no dot config status.showUntrackedFiles no
echo "Done" printf "\nInstallation complete!\n"