Last active
          October 31, 2018 21:41 
        
      - 
      
- 
        Save markcol/b3c2026a6633c44321e51b75d63b6ed8 to your computer and use it in GitHub Desktop. 
    Shell script to setup a git repository to track dot-files. See https://developer.atlassian.com/blog/2016/02/best-way-to-store-dotfiles-git-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
    
  
  
    
  | #!/bin/bash | |
| #set -x | |
| checkout () { | |
| mkdir -p .config-backup | |
| ${GIT} checkout $@ > /dev/null 2>&1 | |
| if [ $? = 0 ]; then | |
| echo "Checked out config."; | |
| else | |
| echo "Backing up pre-existing dot files."; | |
| ${GIT} checkout $@ 2>&1 | egrep "^\s+.+" | awk {'print $1'} | xargs -I{} sh -c 'cp -a --parents {} .config-backup/ && rm {}' | |
| ${GIT} checkout $@ | |
| if [ $? != 0 ]; then | |
| echo "Unable to checkout branch $@"; | |
| exit 1 | |
| fi | |
| fi; | |
| } | |
| MYCONF="${HOME}/.cfg/" | |
| GIT="/usr/bin/git --git-dir=${MYCONF} --work-tree=${HOME}" | |
| if [ -e ${MYCONF} ]; then | |
| echo "The directory ${MYCONF} already exists. If you wish to overwrite it, remove it first." | |
| exit 1 | |
| fi | |
| # Clone repository and configure it | |
| git clone --bare https://github.com/markcol/dot-files ${MYCONF} | |
| ${GIT} remote add origin https://github.com/markcol/dot-files | |
| ${GIT} config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*" | |
| ${GIT} fetch -a | |
| ${GIT} branch --set-upstream-to origin/master master | |
| ${GIT} config status.showUntrackedFiles no | |
| # Add aliases to shell init file(s) | |
| for file in ".profile" ".bash_profile" ".zshenv"; do | |
| if [ -e ${HOME}/${file} ]; then | |
| echo "export MYCONF=${MYCONF}" >> ${HOME}/${file} | |
| echo 'alias config="/usr/bin/git --git-dir=${MYCONF} --work-tree=${HOME}"' >> ${HOME}/${file} | |
| fi | |
| done | |
| if [ -e ${HOME}/.gitignore ]; then | |
| echo "$(basename ${MYCONF})/" >> ${HOME}/.gitignore | |
| fi | |
| # Create a branch for local machine if it does not exist remotely, | |
| # otherwise track the remote branch. | |
| BRANCH="$(hostname -s)" | |
| if [ "$(${GIT} branch --list ${BRANCH})" = "" ]; then | |
| echo "Checking out files from master branch" | |
| checkout | |
| echo "Creating branch for local machine config (${BRANCH})." | |
| ${GIT} checkout -b ${BRANCH} | |
| ${GIT} push -u origin ${BRANCH} | |
| else | |
| echo "Switching to local machine branch (${BRANCH})." | |
| ${GIT} branch --set-upstream-to origin/${BRANCH} ${BRANCH} | |
| checkout ${BRANCH} | |
| fi | |
| if [ -d ~/.config-backup ]; then | |
| echo "Backups of any files that have been overwritten are in ~/.config-backup" | |
| fi | |
| echo "Please source your shell config file to get 'config' alias." | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment