windows下架设subversion服务器(完整版)

来源:互联网 发布:人力资源软件排名 编辑:程序博客网 时间:2024/06/06 18:41

SVN 全名是 Subversion,它是一个开源的版本控制软件,与它类似的软件有CVS,VSS,ClearCase。而 SVN 作为一个跨平台的开源软件,具有很强的活力,目前也已经相当成熟,很多开源项目都用它来管理文档或是代码,以及项目文件的版本控制。

更为重要的是,不仅仅是团队开发,作为个人独立开发的项目(或者个人想维护的文档)也可以用 SVN 进行管理,而不需要另外一台服务器。以下为在windows环境下架设subversion服务器,实例在windows xp sp2/sp1 以及windows server 2003等win版本的系统架设成功。

一、安装SVN:

1、下载:目前最新的稳定定版,win32 1.6.0版,在官方下载地址下载,注意区分各版本适用环境,尤其是apache版本。

官方各版本下载地址:http://subversion.tigris.org/servlets/ProjectDocumentList?folderID=91

天空青年本次架设的SVN选择版本,1.6.0版(apache 2.2.x),安装包下载地址为

http://subversion.tigris.org/files/documents/15/47866/svn-win32-1.6.0.zip

SVN架设环境:Nginx 1.0.0+Apache 2.2.13+PHP 5.2.8(稳定版)+MySQL 5.05+ZendOptimizer 3.3.0+SVN 1.6.0

2、安装:下载完后,将安装包解压到 D:\deploy\Subversion

3、建立Repository:如在D:\svnsrc目录下新建sky54.net程序代码仓库,CMD打开命令控制台,输入以下命令,即可完成代码仓库sky54.net的创建

D:\deploy\Subversion\bin\svnadmin.exe create --fs-type fsfs D:\svnsrc\sky54.net

4、配置Repository:进入Repository目录,在本文实例是 D:\svnsrc\sky54.net,进入后看到sky54.net下有conf目录,进入该目录,将可发现svnserve.conf和passwd两个文件,对此分别修改和配置如下:

1)、打开svnserve.conf,配置svn用户权限

下载: svnserve.conf
  1. ### This file controls the configuration of the svnserve daemonif you
  2. ### use it to allow access to this repository.  (If you only allow
  3. ### access through httpand/or fileURLsthen this file is
  4. ### irrelevant.)
  5.  
  6. ### Visit http://subversion.tigris.org/ for more information.
  7.  
  8. [general]
  9. ### These options control access to the repository for unauthenticated
  10. ### and authenticated users.  Valid values are "write""read",
  11. ### and "none".  The sample settings below are the defaults.
  12. ###去掉前面#号,配置用户权限
  13. ###配置参数none:无任何权限; read: 只读; write:读和写
  14. ###anon-access 配置匿名用户的权限为只读
  15. anon-access = read
  16. ###auth-access 配置授权用户的权限为可读写
  17. auth-access = write
  18. ### The password-db option controls the location of the password
  19. ### database file.  Unless you specify a path starting with a /,
  20. ### the file's location is relative to the directory containing
  21. ### this configuration file.
  22. ### If SASL is enabled (see below)this file will NOT be used.
  23. ### Uncomment the line below to use the default password file.
  24. ### 去掉前面的#号,在password文件中增加相关的用户名和密码
  25. password-db = passwd
  26. ### The authz-db option controls the location of the authorization
  27. ### rules for path-based access control.  Unless you specify a path
  28. ### starting with a /, the file's location is relative to the the
  29. ### directory containing this file.  If you don't specify an
  30. ### authz-dbno path-based access control is done.
  31. ### Uncomment the line below to use the default authorization file.
  32. # authz-db = authz
  33. ### This option specifies the authentication realm of the repository.
  34. ### If two repositories have the same authentication realmthey should
  35. ### have the same password databaseand vice versa.  The default realm
  36. ### is repository's uuid.
  37. # realm = My First Repository
  38.  
  39. [sasl]
  40. ### This option specifies whether you want to use the Cyrus SASL
  41. ### library for authenticationDefault is false.
  42. ### This section will be ignored if svnserve is not built with Cyrus
  43. ### SASL supportto checkrun 'svnserve --versionand look for a line
  44. ### reading 'Cyrus SASL authentication is available.'
  45. # use-sasl = true
  46. ### These options specify the desired strength of the security layer
  47. ### that you want SASL to provide0 means no encryption1 means
  48. ### integrity-checking onlyvalues larger than 1 are correlated
  49. ### to the effective key length for encryption (e.g128 means 128-bit
  50. ### encryption)The values below are the defaults.
  51. # min-encryption = 0
  52. # max-encryption = 256

2)、打开passwd文件,配置授权用户,实例配置用户名www.sky54.net 密码123456

注意: svnserve.conf中的[general] 和 passwd 中的 [users]配置项前不能有空格, “=” 后面只能有一个空格

下载: passwd
  1. ### This file is an example password file for svnserve.
  2. ### Its format is similar to that of svnserve.confAs shown in the
  3. ### example below it contains one section labelled [users].
  4. ### The name and password for each user followone account per line.
  5.  
  6. [users]
  7. # harry = harryssecret
  8. # sally = sallyssecret
  9. www.sky54.net=123456

5、启动SVN:打开命令窗口,键入下面启动命令,如果你安装路径与本文实例不同的话,注意修改svn安装路径和Repository路径,运行命令后,发现进程管理器中有svnserve.exe,说明配置成功。

D:\deploy\Subversion\bin\svnserve.exe -d -r D:\svnsrc

SVN默认端口是3690,如果不幸这个端口被别的程序暂用,可以通过选项 –listen-port=绑定端口

SVN配置测试:

url格式为 svn://ip地址//Repository 名,在本文实例url是svn://127.0.0.1/sky54.net

至此,SVN的配置基本上可以使用,如果需要SVN跟随windows系统一起启动话,下面将进一步介绍将SVN注册到windows系统服务中。

二、配置SVN启动服务:

将SVN加入win系统启动服务项中,让subversion在windows自动启动,不用每次开机都要键入启动命令,尤其是生产环境中加入系统服务就显得很有必要。

以前的svnserve要想成为windows服务,必须依赖于svnservice或其他工具。从Subversion1.4开始,Subversion本身就集成Windows服务的工具。

1,注册svn系统服务:

在Windows NT中(包括Windows XP, Windows 2000, Windows 2003 Server)本身包含了一个安装服务的工具,叫做”Service Control”,也就是sc.exe。

例如我的Subversion安装在”D:\deploy\Subversion”,版本库在”D:\svnsrc”,而我希望对应的Subversion服务名为svn,安装这个svn服务的命令就可以这样写:

  1. sc create svn
  2.       binpath"D:\deploy\Subversion\bin\svnserve.exe --service -r D:\svnsrc"
  3.       displayname"Subversion Server"
  4.       dependTcpip
  5.       startauto

请注意,因为便于察看,上面的命令分为多行,但在实际执行时应该在一行里。另外,在以前启动svnserve时会使用”-d”选项,也就是守护进程模式,在这里不能使用,会导致服务无法启动。同样,”-i”和”-t”选项也不能使用。

实际配置中,在windows命令窗口运行下面这一行的注册系统服务命令的即可,注意修改SVN安装路径和库路径

  1. sc create svn binpath"\"D:\deploy\Subversion\bin\svnserve.exe\" --service -r D:\svnsrc" displayname="Subversion Server" dependTcpip startauto

然后即可通过命令行窗口执行net启动/停止svn服务命令,对SVN服务启动、停止管理。

2、删除服务

如果服务安装的有问题,你可能需要删除服务。要删除前面添加的服务,只需要运行删除Subversion Server服务命令即可,”Subversion Server”就是我们创建服务时使用的名字。

删除Subversion Server服务命令

  1. sc delete svn binpath"\"D:\deploy\Subversion\bin\svnserve.exe\" --service -r D:\svnsrc" displayname="Subversion Server" dependTcpip startauto

当然你也可以使用图形化的工具修改服务的属性,你可以在“开始->运行”中执行”services.msc”,然后在界面中修改。

3、启动/停止svn服务

启动svn服务命令

  1. net start svn

停止svn服务命令

  1. net stop svn

4、SVN访问:在SVN安装部分提到过,最后再次重复一下

url格式为 svn://ip地址//Repository名,在本文实例url是svn://127.0.0.1/sky54.net

三、SVN应用实例:

1、代码库同步生产环境:利用SVN的钩子Hook,实现Repository代码库与生产环境目录同步。

方法其实很简单,有过在服务器上架设SVN服务端的朋友都能大概摸到这样一个流程,无非就是启用一个POST-COMMIT钩子脚本,commit后自动部署脚本,并将代码库更新到生产环境目录中。

本文实例中生产环境目录为D:\WWWROOT\sky54.net,我们打开windows命令窗口,键入如下命令,svn命令将空库checkout到生产环境目录中。

带SVN授权用户和密码命令:

  1. D:\deploy\Subversion\bin\svn.exe checkout svn://127.0.0.1/sky54.net "D:\WWWROOT\sky54.net" --username www.sky54.net --password 123456

简化命令:

  1. D:\deploy\Subversion\bin\svn.exe co svn://127.0.0.1/sky54.net "D:\WWWROOT\sky54.net"

命令键入运行后,系统会提示,”Checked out revision 23“或者”取出版本 23“,说明check out成功!

注意,以上名只用键入任一条即可,完成代码库的Checkout,天空青年反复测试的结果,告诉我必须先checkout一次,否则后面post-commit.bat与生产环境代码无法同步。

在实例仓库D:\svnsrc\sky54.net\hooks目录下,复制post-commit.tmpl,并改名成post-commit.bat,将文件内容替换成以下命令:

下载: post-commit.bat
  1. @ECHO ON
  2. REM POST-COMMIT HOOK
  3.  
  4. set repo=%1 
  5. set rev=%2 
  6. rem post-commit日志记录目录
  7. set log="D:\svnsrc\sky54.net\post-commit_log.txt" 
  8. rem post-commit错误日志记录目录
  9. set err="D:\svnsrc\sky54.net\post-commit_err-log.txt" 
  10. rem svn.exe文件的目录
  11. set svn="D:\deploy\Subversion\bin\svn.exe" 
  12. rem svn安装目录
  13. set svnpath="D:\deploy\Subversion\bin"
  14. rem 生产环境目录
  15. set wwwroot="D:\WWWROOT\sky54.net"
  16. ( echo ==================================================
  17. echo Start commit %repor%rev%
  18. date /t  time /t ) >> %log%
  19. rem 带授权用户名和密码命令update命令
  20. rem set cmd=%svnupdate %wwwroot% --quiet --username www.sky54.net --password 123456
  21. rem 简化update命令(测试post-commit中无法同步到生产环境,请带上用户名和密码)
  22. set cmd=%svnupdate %wwwroot%
  23. (start "start-title" /D%svnpath% /BELOWNORMAL /B %cmd% >> %log%)

提示:你也可以下载本文的post-commit.bat,然后将文件中目录路径改成自己的,上传到项目代码库hooks目录(实例D:\svnsrc\sky54.net\hooks)下即可。

2、其它配置:commit强制写日志记录,以及用户只能修改自己提交文件的日志

SVN的每次commit是不需要日志log信息,为了便于管理,项目组通常会要求增加填写log习惯,可通过配置SVN的hook实现功能管理,通常的Windows下的强制log的hook脚本如下:

下载: pre-commit.bat
  1. @ECHO ON
  2. rem pre-commit HOOK
  3. setlocal
  4. set REPOS=%1
  5. set TXN=%2
  6. rem svnlook.exe目录路径
  7. set svnlook="D:\deploy\Subversion\bin\svnlook.exe" 
  8. rem 日志保证输入10个字符
  9. rem check that logmessage contains at least 5 characters
  10. %svnlooklog "%REPOS%" -t "%TXN%" | findstr ".........." > nul
  11. if %errorlevelgtr 0 goto err
  12.  
  13. rem 过滤空格字符
  14. %svnlooklog "%REPOS%" -t "%TXN%" | findstr /ic:"        " > nul
  15. if %errorlevelgtr 0 goto success
  16.  
  17. goto err_empty
  18.  
  19. :err
  20. echo 本次版本提交,必须写日志说明信息!>&2
  21. echo 请补充日志说明信息后,再提交代码,例如:功能说明等!>&2
  22. echo 输入的日志信息不少于10个字符说明(5个汉字),谢谢!>&2
  23.  
  24. :err_empty
  25. echo 本次版本提交,必须写日志说明信息!>&2
  26. echo ***********禁止空格数据***********!>&2
  27. exit 1
  28.  
  29. :success
  30. exit 0

使用说明:svn强制性写日志,字符不得小于10,汉字不少于4个,以及实现了空格过滤功能,pre-commit.bat文件目录与post-commit.bat均在项目代码库hooks目录下,实际应用时,注意修改实例中svnlook.exe路径。

pre-revprop-change.bat

便于代码库管理,防止项目组成员修改他人日志,实现SVN用户只能修改自己提交的文件日志,脚本如下:

下载: pre-revprop-change.bat
  1. @ECHO ON
  2. rem pre-revprop-change HOOK
  3. set repos=%1
  4. set rev=%2
  5. set user=%3
  6. set propname=%4
  7. set action=%5
  8. rem svnlook.exe目录路径
  9. set svnlook="D:\deploy\Subversion\bin\svnlook.exe" 
  10. for /f  "usebackq"   %%k in   (`%svnlookauthor %repos% -r %rev%`)   do   @set  var=%%k
  11.  
  12. set rightUser=0
  13. if "%3" == "%superUser%"  set rightUser=1
  14. if "%3" == "%var%" set rightUser=1
  15.  
  16. if %rightUser% == 0 goto ERROR_USER
  17.  
  18. goto :SUCCESS_EXIT
  19.  
  20. :ERROR_USER
  21. echo 只允许用户修改自己的日志 >&2
  22. goto ERROR_EXIT
  23.  
  24. :ERROR_EXIT
  25. exit 1
  26.  
  27. :SUCCESS_EXIT
  28. exit 0

实际应用提示:pre-revprop-change.bat文件上传目录,与pre-commit.bat、post-commit.bat等文件均在项目代码库hooks目录下。

原创粉丝点击