Skip to content

Instantly share code, notes, and snippets.

@LeonardoMor
Last active December 5, 2023 19:33
Show Gist options
  • Save LeonardoMor/52d36750b9a6f9660f7a911ebe7a101d to your computer and use it in GitHub Desktop.
Save LeonardoMor/52d36750b9a6f9660f7a911ebe7a101d to your computer and use it in GitHub Desktop.
Keeping dot files consistent across multiple systems
#!/usr/bin/env bash
# This will keep all the dot files consistent across systems.
#
# Leonardo Mora Castro
# December, 2023
help() {
cat <<EOF
USAGE:
dotfiles.sh OPTION
OPTIONS:
-h show this help message and exit
-i initialize the dotfiles repository
-s synchronize the dotfiles repository
EOF
}
# Parse arguments
while getopts "his" opt; do
case "$opt" in
h)
help
exit 0
;;
i)
action="init"
;;
s)
action="sync"
;;
*)
help
exit 1
;;
esac
done
alias="alias config='/usr/bin/git --git-dir=\$HOME/.cfg/ --work-tree=\$HOME'"
repoURL='[email protected]:LeonardoMor/configs.git'
NvChadConfigsURL='[email protected]:LeonardoMor/NVChadConfigs.git'
nvimCustom="$HOME/.config/nvim/lua/custom"
set_alias() {
if ! grep -q "$alias" "$HOME/.bash_aliases"; then
echo "$alias" >>"$HOME/.bash_aliases"
fi
if ! grep -q '\.bash_aliases' "$HOME/.bashrc"; then
echo ". $HOME/.bash_aliases" >>"$HOME/.bashrc"
fi
source "${HOME}/.bashrc"
}
config() {
/usr/bin/git --git-dir="$HOME/.cfg/" --work-tree="$HOME" "$@"
}
trackNVChad() {
cd "$nvimCustom" || {
echo "Could not change directory to $HOME/.config/nvim/lua/custom" >&2
exit 1
}
git init
git remote add origin "$NvChadConfigsURL"
git add .
git commit -m "Add NvChad custom configs"
git push --force --set-upstream origin master
}
initialize() {
local -a dotfiles
git init --bare "$HOME/.cfg"
config config --local status.showUntrackedFiles no
# Add here the files to track
dotfiles=(
"$HOME/.bash_aliases"
"$HOME/.bashrc"
"$HOME/.tmux.conf"
)
config status
for dotfile in "${dotfiles[@]}"; do
if ! [[ -d $dotfile ]]; then
config add "$dotfile"
fi
while read -r f; do
config add "$f"
done < <(find "$dotfile" -type f)
config commit -m "Add $(basename "$dotfile")"
done
config remote add origin "$repoURL"
config push --force --set-upstream origin master
# Initialize custom NvChad configs
if trackNVChad; then
echo "NvChad custom configs now tracked"
fi
}
synchronize() {
if ! [[ -d ${HOME}/.cfg ]]; then
echo ".cfg" >>.gitignore
git clone --bare --no-recurse-submodules "$repoURL" "${HOME}/.cfg"
fi
local -a existing_dotfiles
readarray -t existing_dotfiles < <(
config checkout 2>&1 | grep -E '[[:space:]]+\.[^[:space:]]+' | awk '{ print $1 }'
)
if [[ ${#existing_dotfiles[@]} -gt 0 ]]; then
mkdir -p "${HOME}/.config-backup"
for dotfile in "${existing_dotfiles[@]}"; do
mv "$HOME/$dotfile" "${HOME}/.config-backup"
echo "Moved existing $dotfile to ${HOME}/.config-backup" >&2
done
config checkout
fi
config config --local status.showUntrackedFiles no
rm -rf "$(basename "$nvimCustom")" "$nvimCustom"
git clone "$NvChadConfigsURL" "$nvimCustom"
}
main() {
cd "$HOME" || {
echo "Could not change directory to $HOME" >&2
exit 1
}
set_alias
case "$action" in
init)
initialize
;;
sync)
synchronize
;;
*)
help
exit 1
;;
esac
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment