From 4f78647c51d43d4524d15db3696bf05b802f5c46 Mon Sep 17 00:00:00 2001 From: PowerUser64 Date: Wed, 15 Jun 2022 19:51:37 -0700 Subject: [PATCH] dotfiles-install: now handles file paths with spaces --- .local/bin/dotfiles-install.sh | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/.local/bin/dotfiles-install.sh b/.local/bin/dotfiles-install.sh index 0028d69..65cb028 100755 --- a/.local/bin/dotfiles-install.sh +++ b/.local/bin/dotfiles-install.sh @@ -1,18 +1,22 @@ -#!/bin/bash +#!/bin/sh # 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 -# TODO: make it so line 31 (DOTFILES_BACKUP_LIST) can handle spaces (do it in a for loop) +## 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_URL="https://git.blakenorth.net/dotfiles" -DOTFILES_BACKUP_DIR="$HOME/.dotfiles-backup" # cannot contain the character '#' +DOTFILES_BACKUP_DIR="$HOME/.dotfiles-backup" set -e # check for the git command and exit if it doesn't exist 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 fi @@ -22,19 +26,21 @@ dot() { git --git-dir="$DOTFILES_REPO_DIR" --work-tree="$HOME" "$@" } -# remove the '&> /dev/null' part if things don't seem to work right -if dot checkout > /dev/null 2>&1; then - echo "Checked out dotfiles." -else +# remove the '> /dev/null' part if things don't seem to work right +if ! dot checkout > /dev/null 2>&1; then echo "Backing up pre-existing dotfiles." + mkdir -p "$DOTFILES_BACKUP_DIR" + # get the list of files that need to be backed up - DOTFILES_BACKUP_LIST=($(dot checkout 2>&1 | grep -Po "(?<=\t)(.*)$")) - # create directories for everything that needs one - mkdir -p "$DOTFILES_BACKUP_DIR" $(printf '%s\n' "${DOTFILES_BACKUP_LIST[@]}" | grep -Po '.*/' | sed "s#.*#$DOTFILES_BACKUP_DIR/&#g") - # backup existing files to avoid them being overwritten - printf '%s\n' "${DOTFILES_BACKUP_LIST[@]}" | xargs -I{} mv "$HOME"/{} "$DOTFILES_BACKUP_DIR"/{} + dot checkout 2>&1 | grep -Po "(?<=\t)(.*)$" | + while read -rd '' LINE; do + LINE_DIR="${LINE%%/*}" + mkdir -p "$DOTFILES_BACKUP_DIR/${LINE_DIR#"$HOME"}/${LINE##*/}" + mv "$FILE" "$DOTFILES_BACKUP_DIR" + done fi dot checkout dot config status.showUntrackedFiles no +echo "Done"