svn提交脚本
‘壹’ 如何设置SVN提交时强制添加注释
rem SVN强制写注释的hooks脚本(Windows)
rem 文件名是: pre-commit.bat,放到repository/hooks目录下setlocalset SVN_BINDIR="C:\Program Files\CollabNet\Subversion Server"
set REPOS=%1set TXN=%2rem check that logmessage contains at least 10 characters
%SVN_BINDIR%\svnlook log "%REPOS%" -t "%TXN%" | findstr "......" > nul
if %errorlevel% gtr 0 goto errexit 0:errecho 提交时必须填写注释(Message)! 1>&21.用SVN几个问题的解决
‘贰’ windows 2008 64bit设置svn强制提交注释
将以下代码复制到一个txt文档中,放到Repositories\XXX\hooks目录下(其中XXX为库名),并将这个txt文档改名为pre-commit.bat ,就可以对这个XXX库进行限制,此hook限制字数为10个字。
@echo
rem SVN强制写注释的hooks脚本(Windows)
rem 文件名是: pre-commit.bat,放到repository/hooks目录下
setlocal
set SVN_BINDIR="D:\Program Files\VisualSVN Server\bin"
set REPOS=%1
set TXN=%2
rem 检查是否提交日志
%SVN_BINDIR%\svnlook log -t "%TXN%" "%REPOS%" | findstr "..............." > nul
if %errorlevel% gtr 0 goto nolog
:nolog
echo Log (Message) is an important information, tracking document must be input, not the input meaningless characters, length shall not be less than 15 characters! >&2
exit 1
‘叁’ SVN到Git的一键迁移脚本(保留所有分支、Tag及提交记录)
注意:
1、由于用到grep、sort、awk,因此在Windows环境中需要在bash中运行,如果没有的话先安装Git即可;
2、需要安装svn的命令号工具,如果没有的话需要安装下:
在SVN中,每一个人提交时都需要在系统中有一个用户,它会被记录在提交信息内。如果想要将SVN用户映射到一个更好的Git作者数据中,你需要一个SVN用户到Git用户的映射。参考《 一键获取所有SVN提交作者用户名的列表 》获取所有SVN提交作者,参考下面的格式建立authors.txt文件:
保存到与authors.txt同一目录,并且修改里面的SVN地址。
在终端下(windows在bash下),切换到 svn2git.sh 所在的目录,执行: sh ./svn2git.sh 。 执行需要比较久的时间,需要耐心等待 ^o^ 。转换完成后,转换结果在当前目录下的svn2git。
参考:
http://www.meilongkui.com/archives/1965
‘肆’ 如何通过svn钩子脚本记录提交人,提交库,提交文件,提交时间
其实原理很简单,就是利用svn的钩子:post-commit,在提交过后想做什么在这个脚本里就写什么。
假设我们的SVN目录是:/home/svn
我们需要同步的web目录是:/home/www/web(前提,这个web目录也是从svn里面检出的)
‘伍’ svn服务器的代码可以同步到服务器吗
开发过程中,需要经常将SVN服务器上的代码同步到测试服务器上,一般做法,需要人工手工更新,这样很浪费工夫。下面的脚本为svnserver的钩子程序,放在svn服务器上,只要代码更新,就会自动提交的测试服务器上。
使用条件:
1、SVN主机是WIN系统,如果要在linux的SVN主机上用,需要修改下面的代码为sh脚本,道理类似,代码不同。有需要的自行更改吧。
2、SVN主机上需要安装完整版的PUTTY安装包,而不是一个EXE.
3、测试服务器可以用putty登录
@echooff
rem本脚本实现将SVN服务器A(win环境)上提交的代码,自动上传(通过pscp)到测试环境的服务器B(linux)上,如果SVN在LINUX环境下,根据本代码自行调整。
remsvn服务器上版本库地址
setreposLoc=%1
setREV=%2
rem----------------------------------------------------------------------配置开始
remsvn服务器上putty的路径
setputtyPath="D:ProgramFiles(x86)PuTTY"
rem测试环境putty登录的用户名
setusername=root
rem测试环境putty登录的密码
setpassword=password
rem测试环境IP
sethost=10.1.1.1
rem测试环境代码根地址
setremoteRootPath=/var/www/htdocs/test
rem----------------------------------------------------------------------配置结束
cd/d%puttyPath%
rem遍历提交了的代码
for/f"tokens=2delims="%%iin('svnlookchanged%reposLoc%')do(
set"var=%%i"
svnlookcat!reposLoc!!var!>temp.txt
rem替掉路径中的trunk
setnewPath=!var:trunk=!
rem通过pscp提交到测试服务器
echoy|pscp-l!username!-pw!password!temp.txt!host!:!remoteRootPath!!newPath!
)
使用方法:
将上面的代码中配置区的变量修改,并将内容保存成bat文件,命名为post-commit.bat,放在SVN服务器上版本库的hooks目录下。提交代码试试看吧。经测试可行。
当然,这个脚本可以再做的牛比点儿,可以针对某个用户的提交做更新,也可以分析SVN提交时的日志,只有当日志中有特定的字符时更新。
另外,由于上面的脚本,只更新当前的提交,所以假设只针对某个用户的提交做更新时,不能只更新当前提交,这样其它用户的提交就落掉了,需要更新整个工程。
‘陆’ 什么是svn的钩子脚本
1. SVN(Subversion)简介
SVN是类似于VSS 和 CVS 的版本管理软件,是CVS的作者另起炉灶开发的,继承了CVS的优点,避免了CVS的一些缺点.
客户端软件TortoiseSVN与资源管理器结合,使用非常方便.
2.安装文件及说明文档所在目录:
客户端安装文件:
http://203.187.176.183/TortoiseSVN-1.1.7-UNICODE_svn-1.1.4.msi
中文语言包:
http://203.187.176.183/LanguagePack_1.1.7_zh_CN.exe
3.简要说明
由于客户端软件TortoiseSVN与资源管理器结合使用,所以安装后要reboot
中文语言包使用方法: 桌面或资源管理器的任何地方右键,
4 基本操作
Svn服务器地址203.187.176.183
每人自己一个私有目录,URL分别是
svn//203.187.176.183/yhb
svn//203.187.176.183/zwb
svn//203.187.176.183/ldh
svn//203.187.176.183/pb
svn//203.187.176.183/pub
你的账号密码通过手机短信发给你.
输入你自己的URL ,比如:svn//203.187.176.183/zwb
常用操作:将一个已有的工程加入SVN
例如:有目录 e:\project1 ,导入 SVN
导入到你的URL下面的某个子目录下:
比如:svn//203.187.176.183/zwb/project1
然后用"SVN 取出"功能从刚导入的或别人导入的工程获得一个working .
Important
You can only check out into an empty directory. If you want to check out a previously imported sourcetree, Subversion will throw an error messge. You will have to check out into a different directory or delete the existing sourcetree first
要想check out (取出)到刚才进行导入的目录,必须先删除原目录中的文件,或新建个目录
取出 操作成功后,目录会有个绿色的勾, 并增加了一个目录 .svn
-------修改过的文件会有红色标识,新增加的文件没有任何标识------
―――使用SVN提交 将改动上传到SVN服务器 ――――――――――――――
改动过的文件和新增加文件会在列表中,要都选中.
‘柒’ 如何使用命令调用TortoiseSVN的提交功能
TortoiseSVN是一个GUI客户端,这个自动化指导为你展示了让TortoiseSVN对话框显示并收集客户输入,如果你希望编写不需要输入的脚本,你应该使用官方的Subversion命令行客户端。
TortoiseSVN的GUI程序叫做TortoiseProc.exe。所有的命令通过参数/command:asdf指定,其中asdf是必须的命令名(命令名详见”表 1. 有效命令及选项列表“)。大多数此类命令至少需要一个路径参数,使用/path:"some\path"指定。在下面的命令表格中,命令引用的是/command:asdf参数,余下的代表了/path:"some\path"参数。
因为一些命令需要一个目标路径的列表(例如提交一些特定的文件),/path参数可以接收多个路径,使用*分割。
TortoiseSVN 使用临时文件在 shell 扩展和主程序之间传递多个参数。从 TortoiseSVN 1.5.0 开始,废弃/notempfile参数,不再需要增加此参数。
The progress dialog which is used for commits, updates and many more commands usually stays open after the command has finished until the user presses theOK button. This can be changed by checking the corresponding option in the settings dialog. But using that setting will close the progress dialog, no matter if you start the command from your batch file or from the TortoiseSVN context menu.
To specify a different location of the configuration file, use the parameter /configdir:"path\to\config\directory". This will override the default path, including any registry setting.
如果想在进度对话框执行完毕后自动关闭,而又不必设置永久性的参数,可以传递/closeonend参数。
/closeonend:0 不自动关闭对话框
/closeonend:1 如果没发生错误则自动关闭对话框
/closeonend:2 如果没发生错误和冲突则自动关闭对话框
/closeonend:3如果没有错误、冲突和合并,会自动关闭
下面的列表列出了所有可以使用TortoiseProc.exe访问的命令,就像上面的描述,必须使用/command:asdf的形式,在列表中,因为节省空间的关系省略了/command的前缀。
表 1. 有效命令及选项列表
命令
描述
:about 显示关于对话框。如果没有给命令也会显示。
:log 打开日志对话框,/path 指定了显示日志的文件或目录,另外还有三个选项可以设置: /startrev:xxx、/endrev:xxx和/strict
:checkout 打开检出对话框,/path指定了目标路径,而/url制定了检出的URL。
:import 打开导入对话框,/path 指定了数据导入路径。
:update 将工作副本的/path更新到HEAD,如果给定参数/rev,就会弹出一个对话框询问用户需要更新到哪个修订版本。为了防止指定修订版本号/rev:1234的对话框,需要选项/nonrecursive和/ignoreexternals。
:commit 打开提交对话框,/path 指定了目标路径或需要提交的文件列表,你也可以使用参数 /logmsg 给提交窗口传递预定义的日志信息,或者你不希望将日志传递给命令行,你也可以使用/logmsgfile:path,path 指向了保存日志信息的文件。为了预先填入bug的ID(如果你设置了集成bug追踪属性),你可以使用/bugid:"the bug id here"完成这个任务。
:add 将/path的文件添加到版本控制 。
:revert 恢复工作副本的本地修改,/path说明恢复哪些条目。
:cleanup 清理中断和终止的操作,将工作副本的/path解锁。
:resolve 将/path指定文件的冲突标示为解决,如果给定/noquestion,解决不会向用户确认操作。
:repocreate 在/path创建一个版本库。
:switch 打开选项对话框。/path 指定目标目录。
:export 将/path的工作副本导出到另一个目录,如果/path指向另一个未版本控制目录,对话框会询问要导出到/path的URL。
:merge Opens the merge dialog. The /path specifies the target directory. For merging a revision range, the following options are available: /fromurl:URL, /revrange:string. For merging two repository trees, the following options are available: /fromurl:URL, /tourl:URL, /fromrev:xxx and /torev:xxx. These pre-fill the relevant fields in the merge dialog.
:mergeall Opens the merge all dialog. The /path specifies the target directory.
: Brings up the branch/tag dialog. The /path is the working to branch/tag from. And the /url is the target URL. You can also specify the /logmsg switch to pass a predefined log message to the branch/tag dialog. Or, if you don't want to pass the log message on the command line, use /logmsgfile:path, where path points to a file containing the log message.
:settings 打开设置对话框。
:remove 从版本控制里移除/path中的文件。
:rename 重命名/path的文件,会在对话框中询问新文件,为了防止一个步骤中询问相似文件,传递/noquestion。
:diff Starts the external diff program specified in the TortoiseSVN settings. The /path specifies the first file. If the option /path2 is set, then the diff program is started with those two files. If /path2 is omitted, then the diff is done between the file in /path and its BASE. To explicitly set the revision numbers use /startrev:xxx and /endrev:xxx. If/blame is set and /path2 is not set, then the diff is done by first blaming the files with the given revisions.
:showcompare
Depending on the URLs and revisions to compare, this either shows a unified diff (if the option unified is set), a dialog with a list of files that have changed or if the URLs point to files starts the diff viewer for those two files.
The options url1, url2, revision1 and revision2 must be specified. The options pegrevision, ignoreancestry, blame and unified are optional.
:conflicteditor Starts the conflict editor specified in the TortoiseSVN settings with the correct files for the conflicted file in /path.
:relocate 打开重定位对话框,/path指定了重定位的工作副本路径。
:help 打开帮助文件
:repostatus 打开为修改检出对话框,/path 指定了工作副本目录。
:repobrowser Starts the repository browser dialog, pointing to the URL of the working given in /path or /path points directly to an URL. An additional option /rev:xxx can be used to specify the revision which the repository browser should show. If the /rev:xxx is omitted, it defaults to HEAD. If /path points to an URL, the /projectpropertiespath:path/to/wcspecifies the path from where to read and use the project properties.
:ignore 将/path中的对象加入到忽略列表,也就是将这些文件添加到 svn:ignore 属性。
:blame
为 /path 选项指定的文件打开追溯对话框。
如果设置了 /startrev 和 /endrev 选项,不会显示询问追溯范围对话框,直接使用这些选项中的版本号。
如果设置了 /line:nnn 选项,TortoiseBlame 会显示指定行数。
也支持 /ignoreeol,/ignorespaces 和 /ignoreallspaces 选项。
:cat 将/path指定的工作副本或URL的文件保存到/savepath:path,修订版本号在/revision:xxx,这样可以得到特定修订版本的文件。
:createpatch 创建/path下的补丁文件。
:revisiongraph 显示/path目录下的版本变化图。
:lock Locks a file or all files in a directory given in /path. The 'lock' dialog is shown so the user can enter a comment for the lock.
:unlock Unlocks a file or all files in a directory given in /path.
:rebuildiconcache Rebuilds the windows icon cache. Only use this in case the windows icons are corrupted. A side effect of this (which can't be avoided) is that the icons on the desktop get rearranged. To suppress the message box, pass /noquestion.
:properties 显示 /path 给出的路径之属性对话框。
Examples (which should be entered on one line):
TortoiseProc.exe /command:commit
/path:"c:\svn_wc\file1.txt*c:\svn_wc\file2.txt"
/logmsg:"test log message" /closeonend:0
TortoiseProc.exe /command:update /path:"c:\svn_wc\" /closeonend:0
TortoiseProc.exe /command:log /path:"c:\svn_wc\file1.txt"
/startrev:50 /endrev:60 /closeonend:0
‘捌’ svn配置钩子更新网站目录 每次提交 程序会卡在正在发送文件 有人说是 脚本有问题 手动执行脚本没有问题
把报错信息发上来看看。
如果报错说被某个钩子阻止,就检查那个钩子是否有问题。如果那个钩子手动执行没问题,最常见的原因的就是path路径问题,一般把钩子里的命令行都改成绝对路径就OK了。
‘玖’ SVN的POST-COMMIT钩子怎么用
利用SVN的POST-COMMIT钩子自动部署代码
我们在开发的过程将代码提交到SVN后使用SVN的hook,通过post-commit脚本,在目
标文件夹根下执行svn update操作,将更新内容同步到测试环境,这样开发调试非常方便。
post-commit内容:
--------------------------------------------
#!/bin/sh
#修改为服务编码
export LANG=zh_CN.gb2312
#Set variable
REPOS="$1"
REV="$2"
SVN=/usr/bin/svn
WEB=/data/home/htdocs
LOG=/data/home/auto_svn.log
#update the code from the SVN
$SVN update $WEB --username username --password password --non-interactive
#......................
if [ $? == 0 ]
then
echo "$REPOS" "$REV" >> $LOG
echo `date` >> $LOG
echo "##############################" >> $LOG
fi
-----------------------------------------------
需要注意:
1、需要用export指定编码。
2、需要指定svn全路径。
3、代码CO出来之后,可以进行post-commit脚本的测试了。因为svn的hooks执行的时候不带有任何的环境变量,所以我们不能通过简单的 ./post-commit 进行代码的测试。必须要使用sudo su 等命令切换到svn或者apache服务器运行用户下,用下面的方法进行测试
Python代码
env – ./post-commit
==================================================================================
#!/bin/bash
export LANG=en_US.UTF-8
src=/data/webroot/ask.j1.com ##local checkout directory###
deswebroot=/data/wwwroot/ask
remoteip=210.14.70.4
/usr/local/svn/bin/svn update $src --username webapp --password | grep -v -i update|awk '{print $2}'| while read chfile
do
if [ -e $chfile ];then
if [ -f $chfile ];then
#chown ftp.ftp $chfile
dfile=$(echo $chfile |sed "s:$src\/::") ###only file name###
rsync -avz -e ssh --exclude-from=/data/svndata/ask.j1.com/exclude-file $src/$dfile root@$remoteip:$deswebroot/$dfile
else
#chown ftp.ftp $chfile
dfile=$(echo $chfile |sed "s:$src\/::")
rsync -auvz -e ssh --exclude-from=/data/svndata/ask.j1.com/exclude-file $src/$dfile/ root@$remoteip:$deswebroot/$dfile/
fi
fi
done
‘拾’ svn如何一次提交到两个服务器
svn提交只能直接提交到一个主服务器,但是可以在主服务器上增加一个hook脚本,处罚另外一个镜像服务器执行svnsync来同步本次提交