简单补丁工具
创建补丁
您有一个Subversion版本库的工作副本,您可以在其中对代码进行编辑(或真正的任何类型的文件)。 完成后,您所要做的就是构建一个仅包含您今天所做更改的补丁文件。 将以下内容放入您的父目录(或您的“bin”目录中,如果您要随地使用它)
#!/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
当您运行没有参数时,它将构建您今天所做的所有更改的归档,并列出归档中包含的文件。 它不会修改您的文件,仅创建已更改的备份副本:
$ 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
安装补丁
将补丁文件复制到要安装的任何位置的父目录后,可以使用“tar”直接解压缩:
tar -zvxf changes_2010Jul22_180554.tar.gz
您可能希望看到要从存档中解压缩的内容,以确保将其放在正确的目录中:
tar -tf changes_2010Jul22_180554.tar.gz
在我的情况下,我通常想要备份要覆盖的文件,以便我可以在必要时快速撤销该补丁。 以下脚本对于确保备份任何将通过解压缩存档而被覆盖的文件非常有用 -
#!/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
这是以补丁文件为唯一参数运行:
$ 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
脚本检查它是否可以在解压缩存档之前进行备份(如果以没有权限写入该目录的用户身份登录,可能会发生这种情况):
$ 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.
如果需要将文件还原到备份副本,只需解释“backup_replace.sh”脚本创建的归档:
$ 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