Skip to content

Instantly share code, notes, and snippets.

@sminez
Last active November 4, 2017 19:44
Show Gist options
  • Save sminez/47fb0d4684beb7192a2c2ec1252f727c to your computer and use it in GitHub Desktop.
Save sminez/47fb0d4684beb7192a2c2ec1252f727c to your computer and use it in GitHub Desktop.
Dotfile manager script
#! /bin/bash
# ----------
# Initialise and manage your dotfiles using a ~/.dots repo
# restore using `git clone --bare <remote-repo>`
dot_dir=".dots"
dot_dot_file=".my-dots"
_dots="git --git-dir=$HOME/$dot_dir/ --work-tree=$HOME"
extra_args=""
add_remote=false
passthrough=true
GREEN='\033[0;32m'
NC='\033[0m' # No Color
while (( $# )); do
arg="$1"
shift
case ${arg} in
init)
current_dir=$(pwd)
echo -e "${GREEN}Initialising ~/$dot_dir repo...${NC}"
cd "$HOME"
mkdir $dot_dir
git init --bare "$HOME"/$dot_dir
$_dots config status.showUntrackedFiles no
echo -e "${GREEN}Creating ~/$dot_dot_file...${NC}"
touch ~/$dot_dot_file
echo $dot_dot_file >> ~/$dot_dot_file
cd "$current_dir"
echo -e "${GREEN}Done${NC}"
passthrough=false
;;
update)
current_dir=$(pwd)
echo -e "${GREEN}Updating dotfiles found in ~/$dot_dot_file...${NC}"
cd "$HOME"
$_dots add $(cat "$HOME"/$dot_dot_file | tr '\n' ' ')
$_dots commit -m"Updating dots: $(date)"
cd "$HOME"/$dot_dir
has_remote=$(git remote show)
echo $has_remote
cd "$HOME"
if [[ "$has_remote" != "" ]]; then
echo -e "${GREEN}Pushing dots to the remote repo...${NC}"
$_dots push
fi
echo -e "${GREEN}Done${NC}"
cd "$current_dir"
passthrough=false
;;
remote)
add_remote=true
;;
*)
extra_args="$extra_args $arg"
esac
done
if [ "$passthrough" = true ]; then
# Restore all other args as positional args so that
# they can be passed on to git
set -- $extra_args $@
# Adding the remote repo is a special case
if [ "$add_remote" = true ]; then
echo -e "${GREEN}Adding remote repo: $1 ...${NC}"
$_dots remote add origin "$1"
echo -e "${GREEN}Making initial commit...${NC}"
$_dots push -u origin master
echo -e "${GREEN}Done${NC}"
else
$_dots "$@"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment