Simple Patch Tools

Want to support HowtoForge? Become a subscriber!
 
Submitted by Everweb (Contact Author) (Forums) on Fri, 2010-07-30 17:07. :: Programming | Shell

Simple Patch Tools

Create The Patch

You have a working copy of a subversion repository in which you make edits to your code (or really any kind of file). When you're done, all you want to do is build a patch file that contains only the changes you've made today. Put the following into your parent directory (or your 'bin' directory if you are going to use it everywhere as I do) -

#!/bin/bash
# Script to build an archive of files that have been changed recently in subdirectories
# The intent is to build a 'patch' archive from the development hierarchy that can be copied to the production hierarchy (eg.publishing website changes)
#
# This shell script can be run with no parameters - in which case it will pick up the latest changes from today
# If a date parameter is specified (eg. 20100422) it will include all changes since 22.Apr.2010

datetimestamp=`date +%Y%b%d_%H%M%S`
if [ $# -gt 0 ]
then
 touch -d $* date_marker
else
 datestamp=`date "+%d %b %Y 00:00:01"`
 touch -d "$datestamp" date_marker
fi
tar --exclude=.svn -cvzf changes_$datetimestamp.tar.gz `find . -newer date_marker -type f`
rm date_marker
echo
echo Created archive: changes_$datetimestamp.tar.gz
echo

When you run this with no parameters, it will build an archive of all the changes you've made today and list the files contained in the archive. It does not modify your files, merely creates a backup copy of the ones that have changed:

$ tar_changes.sh

./winds/Tracker/view.Tracker.php
./winds/Tracker/incl.Tracker.php
./winds/Tracker/ClassTracker.php
./winds/utility/functions.php

Created archive: changes_2010Jul22_180554.tar.gz

 

Installing The Patch

Once you've copied the patch file to the parent directory of wherever you want to install it, you can unpack it directly using 'tar':

tar -zvxf changes_2010Jul22_180554.tar.gz

You may want to see what is going to be unpacked from the archive to make sure you have it in the correct directory:

tar -tf changes_2010Jul22_180554.tar.gz

In my case, I typically want to take a backup of the files that are going to be overwritten so that I can quickly undo the patch if necessary. The following script is useful for ensuring that you have a backup of any files that will be overwritten by unpacking your archive -

#!/bin/sh
#####################################################################################
# backup_replace.sh
# Script to backup files and replace them with the contents of the specified archive
# The only files backed-up are the ones that are listed in the archive
# The archive is presumed to be tar'ed and gzip'ed
#
# Usage:
# backup_replace.sh
#
#####################################################################################


if [ $# -ne 1 ]
then
   echo "Usage: backup_replace.sh "
   exit 1
fi

if [ ! -e $1 ]
then
   echo "Unable to find specified archive: $1"
   echo "Usage: backup_replace.sh "
   exit 1
fi

echo Backing-up existing files that match files in $1
tar -czf replaced_by_$1 `tar -tf $1`
if [ $? -ne 0 ]
then
   echo "Failed to backup existing files. Aborting."
   exit 1
fi

echo Extracting $1
tar -zvxf $1

echo Done

This is run with the patch file as the only parameter:

$ backup_replace.sh changes_2010Jul22_180554.tar.gz

Backing-up existing files that match files in changes_2010Jul22_180554.tar.gz
Extracting changes_2010Jul22_180554.tar.gz
./winds/Tracker/view.Tracker.php
./winds/Tracker/incl.Tracker.php
./winds/Tracker/ClassTracker.php
./winds/utility/functions.php
Done

The script checks that it can make the backup before unpacking the archive (this might happen if you are logged in as a user that does not have permission to write to the directory):

$ backup_replace.sh changes_2010Jul22_180554.tar.gz

Backing-up existing files that match files in changes_2010Jul22_180554.tar.gz
tar: replaced_by_changes_2010Jul22_180554.tar.gz: Cannot open: Permission denied
tar: Error is not recoverable: exiting now
Failed to backup existing files. Aborting.

If you need to revert the files to the backup copy, simply untar the archive created by the 'backup_replace.sh' script:

$ tar -zvxf replaced_by_changes_2010Jul22_180554.tar.gz

./winds/Tracker/view.Tracker.php
./winds/Tracker/incl.Tracker.php
./winds/Tracker/ClassTracker.php
./winds/utility/functions.php


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by Anonymous (not registered) on Thu, 2010-08-05 15:12.

Hi dude,

great scripts.

 

thanks.