Skip to content

Instantly share code, notes, and snippets.

@pablospizzamiglio
Last active May 4, 2021 04:47
Show Gist options
  • Save pablospizzamiglio/dc912e9bbb7a20cac96aa7641193198b to your computer and use it in GitHub Desktop.
Save pablospizzamiglio/dc912e9bbb7a20cac96aa7641193198b to your computer and use it in GitHub Desktop.
Install dotfiles from bare repo
# curl -Lks <link-to-this-gist> | bash
set -e
#------------------------------------------------------------------------------#
# Download
#------------------------------------------------------------------------------#
echo "> Downloading dotfiles..."
dir=.dotfiles
git clone --quiet --bare https://github.com/pablospizzamiglio/dotfiles.git "$HOME/$dir"
function cfg {
git --git-dir="$HOME/$dir" --work-tree="$HOME" "$@";
}
#------------------------------------------------------------------------------#
# Backup already existing dotfiles
#------------------------------------------------------------------------------#
files=($(cfg ls-tree -r HEAD | awk '{print $NF}'))
bkp=.dotfiles-backup
for f in "${files[@]}"; do
# File at root ==> back up file
if [[ $(basename "$f") = "$f" ]]; then
[[ -f "$HOME/$f" ]] && mkdir -p "$HOME/$bkp" && mv "$HOME/$f" "$HOME/$bkp" && echo "> Backing up: $f ==> $bkp/$f"
# File in nested directory ==> back up outermost directory
else
d=${f%%/*}
if [[ -d "$HOME/$d" ]]; then
[[ -d "$HOME/$bkp/$d" ]] && rm -rf "$HOME/$bkp/$d"
mkdir -p "$HOME/$bkp" && mv "$HOME/$d" "$HOME/$bkp" && echo "> Backing up: $d/ ==> $bkp/$d/"
fi
fi
done
#------------------------------------------------------------------------------#
# Install
#------------------------------------------------------------------------------#
cfg checkout
cfg submodule --quiet init
cfg submodule --quiet update
cfg config status.showUntrackedFiles no
echo "> Success! The following dotfiles have been installed to $HOME:"
printf ' %s\n' "${files[@]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment