Last active
May 4, 2021 04:47
-
-
Save pablospizzamiglio/dc912e9bbb7a20cac96aa7641193198b to your computer and use it in GitHub Desktop.
Install dotfiles from bare repo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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