tychoish, a wiki

tychoish/code/ git sync library

git sync library

See the git sync control script that uses these functions, and the post that explains the functioning of this tool.

 #!/bin/zsh

 # git-sync functions 
 # Copyright (c) 2010 Sam Kleinman

 # Permission is hereby granted, free of charge, to any person
 # obtaining a copy of this software and associated documentation files
 # (the 'Software'), to deal in the Software without restriction,
 # including without limitation the rights to use, copy, modify, merge,
 # publish, distribute, sublicense, and/or sell copies of the Software,
 # and to permit persons to whom the Software is furnished to do so,
 # subject to the following conditions:

 # The above copyright notice and this permission notice shall be
 # included in all copies or substantial portions of the Software.

 # THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 # SOFTWARE.

 #
 # ABOUT GIT-SYNC FUNCTIONS 
 # 

 # These functions managing syncing and monitoring a large number of
 # git repositories, when these repositories are used as part of a
 # larger backup and syncronization "service," based on git
 # repositories. Configure the operation of this script after reading
 # the documentation below. 

 ################################################################################
 ## 
 ## Variables
 ## 
 ################################################################################

 # Configure these variables as needed to point to your repositories
 #
 # $force_commits_repo - contains all repositories that you are ok
 #                       automatically commiting changes (with a
 #                       generic message. This will not remove deleted
 #                       files from your repository. 
 #
 # $force_sync_repo - adds to the `$force_commits_repo` list with all
 #                    repos that you are comfortable performaing a
 #                    `git pull; git push` operation on automatically.
 # 
 # $all_repos - combines the previous two arrays with additional
 #              repositories that you would like to monitor for changes
 #              but would like to manually commit and publish changesets.



 force_commit_repo=()
 force_sync_repo=($force_commit_repo)
 all_repo=($force_sync_repo)

 ################################################################################
 ## 
 ## Functions
 ## 
 ################################################################################

 # Minimal configuration required. See comments within functions. 

 gss(){

 # Itterates over the $all_repo array and generates a breif display of
 # all uncommited changes to all of the monitored repositories. 

     CURRENT=`pwd`
     for dir in $all_repo; do
         STATUS=`cd $dir; git status -s -u`
         if [ `echo $STATUS | grep -c -e "^??\|M\|D"` -gt 0 ]; then 
             echo -- $dir/
             echo  "$STATUS" | sed -e 's/ D/     d/
                                       s/ M/     m/ 
                                       s/R /     */
                                       s/RM/     */
                                       s/MM/     ~/
                                       s/UU/     !/
                                       s/AA/     !/
                                       s/AM/     m/
                                       s/M /     */
                                       s/??/     u/
                                       s/D /     */
                                       s/A /     */'
             echo        
         fi
     done
     cd $CURRENT
 }

 syncup(){
 # Itterates over the $force_sync_repo array and pulls all changes from
 # the remote repository and then pushes all local changes to the
 # remote repository. This function isn't particularly smart, but it
 # might be worth running as a cronjob in some cases.
 #
 # Add if/elif statements to control the specific behavipr and
 # push/pull operations for repositories with additional branches or
 # remotes.
 # 
 # TODO - repo prioritizing/sensing if there are unpublished
 #        changes... 

     CURRENT=`pwd`

     for repo in $force_sync_repo; do 
         cd $repo;
         if [ $repo = ~/work/ ]; then 
             if [ `netcfg current | grep -c "vpnlan"` = "1" ]; then 
                 echo -- syncing $repo with derrida
                 git pull -q
                 git pull -q vpndev master
                 git push -q vpndev master
             fi
         elif [ $repo = ~/work/project ]; then 
             if [ `netcfg current | grep -c "vpnlan"` = "1" ]; then 
                 echo -- syncing $repo with vpndev
                 git pull -q
                 git push -q 
             fi
         else
             echo -- syncing $repo
             git pull -q 
             git push -q 
         fi
     done
     cd $CURRENT

     # echo -- syncing tychoish mail
     # /home/user/mail//tools/sync-mail >/dev/null 2>&1
 }

 autoci(){
 # Itterates over the $force_commits_repo array and add all unstaged
 # changes and performs a routine/generic commit to these
 # repositories. Does not remove deleted files from repositories, but
 # can if you uncomment the "for i in `git ls-files`" loop.
 # 
 # If you run this repository withot later rebasing commits, your
 # repository will have an unuseful history/log. Don't push these
 # commits to public repositories without considering who this will
 # anoy. 

     CURRENT=`pwd`

     for repo in `echo ${force_commit_repo}`; do 
         cd $repo 
         if [ `git status -s -u |grep -c -e "^??\|M\|D"` -gt 0 ]; then 
             git pull >/dev/null 2>&1 && 
             if [ $repo = ~/garen ]; then 
                 git add ledger
                 git commit -q -m "$HOST: bookkeeping updates found in automatic system sweep"
                 git add . 
                 git commit -a -q -m "$HOST: general changes to found in automatic system sweep"
             else
                 git add . 
               # for i in `git ls-files -d`; do git rm --quiet $i; done 
                 git commit -a -q -m "$HOST: changes to repo found in automatic system sweep"
             fi
             echo -- changes commited to $repo 
         fi
     done
     cd $CURRENT
 }