yellow=$(printf "\e[0;33m") cyan=$(printf "\e[0;36m") byellow=$(printf "\e[1;33m") reset=$(printf "\e[0m") die() { >&2 echo "$1" exit 1 } ensure() { "$@" || die "command failed: $*" } main() { # Clone the repository into a temporary directory, but do not check it out. # Place the .git directory at ~/.dotfiles.git. tmpdir=$(ensure mktemp -d) || die "mktemp failed" ensure git clone --no-checkout --separate-git-dir="$HOME/.dotfiles.git" \ --config status.showUntrackedFiles=no \ https://github.com/iliana/dotfiles.git "$tmpdir" # Clean up the temporary directory. ensure rm "$tmpdir/.git" ensure rmdir "$tmpdir" # Configure git to point to the right place within the rest of this script. export GIT_DIR="$HOME/.dotfiles.git" export GIT_WORK_TREE="$HOME" # Attempt to check out, and prompt the user to try again with `--force` if # it fails. if ! git checkout; then prompt="${yellow}Try again with \"${byellow}git checkout --force${yellow}\" (delete the above files)? ${cyan}[yes/no]${reset} " while true; do if [ -t 0 ] || [ -t 2 ]; then >&2 ensure printf %s "$prompt" if [ -t 0 ]; then read -r choice || die "read failed" else # stdin is not a TTY but stderr is; probably `curl | sh`. # so read from stderr. <&2 read -r choice || die "read failed" fi else die "command failed: git checkout" fi prompt="${yellow}Please type '${cyan}yes${yellow}' or '${cyan}no${yellow}':${reset} " case "$choice" in [yY][eE][sS]) ensure git checkout --force break ;; [nN][oO]) die "Exiting. Remove $GIT_DIR to clean up." ;; esac done fi # Initialize and update submodules. ensure git submodule update --init } main "$@" || exit 1