Search code examples
shellzshoh-my-zsh

Auto update custom folder in Oh My Zsh


I have my own custom theme for Oh My Zsh that lives in the .oh-my-zsh/custom folder. I have it hosted on GitHub (as described in the OMZ docs under Version control of customizations) so all I have to do is

cd ~/.oh-my-zsh/custom ; git pull ; popd

and it updates fine but then I have to visit every machine/VM where I use this custom theme and run the command manually. How can I tie into the auto-update system for Oh My Zsh to update my custom theme?


Solution

  • After trying to contribute a pull request that was mostly ignored, I hacked a way to tie into Oh My Zsh's updates. By watching the mod date of .git's FETCH_HEAD file I am able to trigger my update whenever Oh My Zsh fetches from github which is more than sufficient for my use.

    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
    # self-update
    
    ts_file=~/.zsh-custom-update
    
    upgrade_custom() {
      printf '\033[0;34m%s\033[0m\n' "Upgrading the custom files"
      cd "$ZSH_CUSTOM"
      if git pull --rebase --stat origin master
      then
        printf '\033[0;34m%s\033[0m\n' 'Hooray! The custom files have been updated and/or are at the current version.'
      else
        printf '\033[0;31m%s\033[0m\n' 'There was an error updating. Try again later?'
      fi
    }
    
    upgrade_custom_update() {
      echo -n "$1" >! $ts_file
    }
    
    upgrade_custom_check() {
      if [[ "$OSTYPE" == darwin* ]]; then
        ts=$(stat -f '%Sm' $ZSH/.git/FETCH_HEAD || echo 'missing' )
      else
        ts=$(stat -c %y $ZSH/.git/FETCH_HEAD || echo 'missing' )
      fi
    
      if [[ "$ts" == $( cat $ts_file || echo 'missing' ) ]] ; then
        #echo 'up to date'
      else
        upgrade_custom_update "$ts"
        upgrade_custom    
      fi
    }
    
    upgrade_custom_check
    

    I'm inexperienced at writing zsh scripts so feedback is welcome. I use both Mac & Linux but I'm not sure the stat command will work on BSDs.