Skip to content

Instantly share code, notes, and snippets.

@tychoish tychoish/merge-script.md
Last active Aug 29, 2015

Embed
What would you like to do?

Merge Script

Overview and Use

This script keeps two directories of files in synchronized and up to date even if you edit the files in both of the directories in between invocations of the script. Think of it as "copy on steroids."

Set the UPSTREAM, DOWNSTREAM, and CACHE variables as needed before use. Also, if your system does not have the merge binary installed, you may need to install this dependency as well. In Arch Linux the rcs package provides merge.

Only use merge-script with plain text files.

Code

 #!/bin/zsh

 # merge-script
 # 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.
 #

 UPSTREAM=~/personal-wiki/project
 DOWNSTREAM=~/projects/project/notes/
 CACHE=.git/$DOWNSTREAM
 cp_arg=--preserve=timestamp

 echo \[0\]: notes sync utility
 if [ ! -d notes ]; then
     mkdir -p $DOWNSTREAM
     cp -R $cp_arg $UPSTREAM* $DOWNSTREAM
     echo \[1\]: files imported into DOWNSTREAM
     exit 0
 else
     for source in `ls $DOWNSTREAM*.{mdwn,org}`; do
         cp -n $source $CACHE
     done
     echo \[1\]: cache up to date with DOWNSTREAM
 fi

 if [ ! -d $CACHE ]; then
     mkdir -p $CACHE
     cp -R $cp_arg $UPSTREAM*.{mdwn,org} $CACHE
     echo \[2\]: cache of UPSTREAM created
 else
     for source in `ls $UPSTREAM*.{mdwn,org}` ; do
         cp -n $source $CACHE
     done
     echo \[2\]: cache up to date with UPSTREAM
 fi

 changed=()
 for cache in `ls $CACHE*.{mdwn,org}` ; do
     file=`basename $cache`
     src=$UPSTREAM$file
     dst=$DOWNSTREAM$file

     src_stat=$(stat -c "%Y" $src)
     dst_stat=$(stat -c "%Y" $dst)

     if [ "$dst_stat" -gt "$src_stat" ]; then
         merge $cache $src $dst >/dev/null
         cp $cp_arg $cache $UPSTREAM
         cp $cp_arg $cache $DOWNSTREAM
         echo \[3\]: DOWNSTREAM/$file changed, merged, and deployed
         changed=($changed $file)
     elif [ "$dst_stat" -lt "$src_stat" ]; then
         merge $cache $dst $src >/dev/null
         cp $cp_arg $cache $UPSTREAM
         cp $cp_arg $cache $DOWNSTREAM
         echo \[3\]: UPSTREAM/$file changed, merged, and deployed
         changed=($changed $file)
     fi
 done

 if [ -z "$changed" ]; then
     echo \[3\]: no files updated
 fi

 echo \[4\]: files updated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.