Centos下SVN环境部署记录

 

大多数情况下,我们日常工作中用的版本控制系统都会选择分布式的Git,它相比于集中式的SVN有很多优势。但是有些项目软件基于自身限制,可能只支持SVN做工程同步。废话就不多说了,下面记录下SVN的部署和使用过程:

1)安装SVN

[root@svn-server ~]# rpm -qa subversion
[root@svn-server ~]# yum remove subversion
[root@svn-server ~]# yum -y install subversion
[root@svn-server ~]# svnversion --version
 
启动svn,启动时要指定svn的仓库目录
[root@svn-server ~]# mkdir -p /data/svn
[root@svn-server ~]# /usr/bin/svnserve -d -r /data/svn
[root@svn-server ~]# ps -ef|grep svn
root     19826     1  0 10:52 ?        00:00:00 /usr/bin/svnserve -d -r /data/svn
root     19829 19688  0 10:52 pts/1    00:00:00 grep svn
[root@svn-server ~]# lsof -i:3690                     #svn默认端口是3690
COMMAND    PID USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
svnserve 19826 root    3u  IPv4 12251011      0t0  TCP *:svn (LISTEN)

特别注意:
svnserver的启动命令要使用上面的"/usr/bin/svnserve -d -r /data/svn"
不要使用"service svnserve start"命令来启动,否则会造成svn下载时报错:svn: No repository found in 'svn://*.*.*.*/*'

设置开机启动
[root@svn-server ~]# echo "/usr/bin/svnserve -d -r /data/svn" >> /etc/rc.local
 
停止和重启SVN
[root@svn-server ~]# killall svnserve
[root@svn-server ~]# ps -ef|grep svn
[root@svn-server ~]# /usr/bin/svnserve -d -r /data/svn
[root@svn-server ~]# ps -ef|grep svn
 
如果已经有svn在运行,可以换一个端口运行
[root@svn-server ~]# /usr/bin/svnserve -d -r /data/svn --listen-port 3391
[root@svn-server ~]# lsof -i:3391
 
关闭防火墙,否则要打开3690端口
[root@svn-server ~]# /etc/init.d/iptables stop

2)代码库创建及配置

如下面创建两个代码库,库名为kevin和grace
[root@svn-server ~]# svnadmin create /data/svn/kevin
[root@svn-server ~]# svnadmin create /data/svn/grace
[root@svn-server ~]# ls /data/svn/kevin/
conf  db  format  hooks  locks  README.txt
[root@svn-server ~]# ls /data/svn/grace/
conf  db  format  hooks  locks  README.txt

配置代码库,这里以kevin代码库为例进行说明
[root@svn-server ~]# cd /data/svn/kevin/conf/
[root@svn-server conf]# ll
total 12
-rw-r--r--. 1 root root 1080 May 31 10:59 authz           #权限控制文件
-rw-r--r--. 1 root root  309 May 31 10:59 passwd          #帐号密码文件
-rw-r--r--. 1 root root 2279 May 31 10:59 svnserve.conf   #SVN服务配置文件

设置该代码库的登录帐号和密码(由于是svn自己启动的,没有借助于apache启动,所以这里的密码是明文)
[root@svn-server conf]# vim passwd
......
[users]
# harry = harryssecret
# sally = sallyssecret
wangshibo = wangshibo@123
hanlili = hanlili@123
zhanghuan = zhanghuan@123
limin = limin@123

设置该代码库的操作权限
权限主体可以是别名,用户组、用户或*;别名在前面加&;用户组在前面加@;*表示全部用户;
权限可以是w、r、wr和空,空表示没有任何权限。
[root@svn-server conf]# vim authz
.....
[aliases]         #设置别名
# joe = /C=XZ/ST=Dessert/L=Snake City/O=Snake Oil, Ltd./OU=Research Institute/CN=Joe Average
ops = wanghsibo,hanlili

[groups]         #设置组
# harry_and_sally = harry,sally
# harry_sally_and_joe = harry,sally,&joe
admin = wanghsibo,hanlili           #创建一个admin组,将用户加入到组
devha = zhuanghuan,limin

# [/foo/bar]     
# harry = rw
# &joe = r
# * =

# [repository:/baz/fuz]
# @harry_and_sally = rw
# * = r

[/]         #根目录权限设置,用户对kevin代码库根目录的读写权限
wangshibo = rw
hanlili = rw
@devha = r

[/haha/test]       
&ops = rw
limin = rw

[repository:/yunwei/kaixin]
* = rw

修改svnserve.conf文件(在[general]区域添加下面四行内容)
[root@svn-server conf]# vim svnserve.conf  
.....
[general]
anon-access = none           #匿名用户可读
auth-access = write          #授权用户可写 
password-db = passwd         #使用哪个文件作为账号文件。由于同在一个目录路径下,所以这里不用全路径
authz-db = authz             #使用哪个文件作为权限文件
realm = /data/svn/kevin      #认证命名空间,subversion会在认证提示里显示,并且作为凭证缓存的关键字

重启svn
[root@svn-server conf]# killall svnserve 
[root@svn-server conf]# ps -ef|grep svn
root     20137 19688  0 11:41 pts/1    00:00:00 grep svn
[root@svn-server conf]# /usr/bin/svnserve -d -r /data/svn
[root@svn-server conf]# ps -ef|grep svn
root     20139     1  0 11:41 ?        00:00:00 /usr/bin/svnserve -d -r /data/svn
root     20141 19688  0 11:41 pts/1    00:00:00 grep svn

3)SVN客户端常规操作命令总结

客户机要安装svn,确保有svn相关操作命令
[root@localhost ~]# yum install -y subversion

=======================================================================
查看svn的相关操作命令
[root@localhost svndata]# svn --help
usage: svn <subcommand> [options] [args]
Subversion command-line client, version 1.6.11.
Type 'svn help <subcommand>' for help on a specific subcommand.
Type 'svn --version' to see the program version and RA modules
  or 'svn --version --quiet' to see just the version number.

Most subcommands take file and/or directory arguments, recursing
on the directories.  If no arguments are supplied to such a
command, it recurses on the current directory (inclusive) by default.

Available subcommands:
   add
   blame (praise, annotate, ann)
   cat
   changelist (cl)
   checkout (co)
   cleanup
   commit (ci)
   copy (cp)
   delete (del, remove, rm)
   diff (di)
   export
   help (?, h)
   import
   info
   list (ls)
   lock
   log
   merge
   mergeinfo
   mkdir
   move (mv, rename, ren)
   propdel (pdel, pd)
   propedit (pedit, pe)
   propget (pget, pg)
   proplist (plist, pl)
   propset (pset, ps)
   resolve
   resolved
   revert
   status (stat, st)
   switch (sw)
   unlock
   update (up)

Subversion is a tool for version control.
For additional information, see http://subversion.tigris.org/

=======================================================================
客户机下载svn代码库文件(192.168.10.202是上面svn服务端地址。即下面kevin代码库)
即从版本库中导出
[root@localhost svndata]# svn checkout svn://192.168.10.202/kevin
Authentication realm: <svn://192.168.10.202:3690> /data/svn/kevin
Password for 'root':               #首次需要输入本机root密码
Authentication realm: <svn://192.168.10.202:3690> /data/svn/kevin
Username: wangshibo                #输入svn设置的用户名,这里选择wangshibo
Password for 'wangshibo':          #输入wangshibo密码

-----------------------------------------------------------------------
ATTENTION!  Your password for authentication realm:

   <svn://192.168.10.202:3690> /data/svn/kevin

can only be stored to disk unencrypted!  You are advised to configure
your system so that Subversion can store passwords encrypted, if
possible.  See the documentation for details.

You can avoid future appearances of this warning by setting the value
of the 'store-plaintext-passwords' option to either 'yes' or 'no' in
'/root/.subversion/servers'.
-----------------------------------------------------------------------
Store password unencrypted (yes/no)? yes
Checked out revision 0.

=======================================================================
需要注意:也可以使用带用户名和密码的访问(svn co 等同于svn checkout):
[root@localhost svndata]# svn co --username wangshibo --password wangshibo@123 svn://192.168.10.202/kevin

-----------------------------------------------------------------------
ATTENTION!  Your password for authentication realm:

   <svn://192.168.10.202:3690> /data/svn/kevin

can only be stored to disk unencrypted!  You are advised to configure
your system so that Subversion can store passwords encrypted, if
possible.  See the documentation for details.

You can avoid future appearances of this warning by setting the value
of the 'store-plaintext-passwords' option to either 'yes' or 'no' in
'/root/.subversion/servers'.
-----------------------------------------------------------------------
Store password unencrypted (yes/no)? yes
Checked out revision 0.

[root@localhost svndata]# ls
kevin
[root@localhost svndata]# cd kevin/
[root@localhost kevin]# ll -a
total 16
drwxr-xr-x. 3 root root 4096 May 31 14:41 .
drwxr-xr-x. 3 root root 4096 May 31 14:17 ..
drwxr-xr-x. 6 root root 4096 May 31 14:42 .svn

+++++++++++++++++++++++++++++++++
温馨提示:
svn checkout(即svn co)表示检出。这样下载到的svn代码库里包括.svn
# svn co http://路径(目录或文件的全路径) [本地目录全路径] --username 用户名 --password 密码
# svn co svn://路径(目录或文件的全路径) [本地目录全路径] --username 用户名 --password 密码
# svn checkout http://路径(目录或文件的全路径) [本地目录全路径] --username 用户名
# svn checkout svn://路径(目录或文件的全路径) [本地目录全路径] --username 用户名

注意:如果不带--password 参数传输密码的话,会提示输入密码,建议不要用明文的--password 选项。
  其中 username 与 password前是两个短线,不是一个。
  不指定本地目录全路径,则检出到当前目录下。
例子:
svn co svn://192.168.10.202/kevin /data/svndata --username wangshibo --password wangshibo@123
svn co http://192.168.10.202/kevin --username wangshibo --password wangshibo@123 
svn checkout svn://192.168.10.202/kevin /data/svndata --username wangshibo --password wangshibo@123
svn checkout http://192.168.10.202/kevin --username wangshibo --password wangshibo@123

=======================================================================
svn导出(导出一个干净的不带.svn文件夹的目录树)
svn export [-r 版本号] http://路径(目录或文件的全路径) [本地目录全路径] --username 用户名
svn export [-r 版本号] svn://路径(目录或文件的全路径) [本地目录全路径] --username 用户名
svn export 本地检出的(即带有.svn文件夹的)目录全路径 要导出的本地目录全路径
注意:
第一种从版本库导出干净工作目录树的形式是指定URL,如果指定了修订版本号,会导出相应的版本,如果没有指定修订版本,则会导出最新的,导出到指定位置。
如果省略 本地目录全路径,URL的最后一部分会作为本地目录的名字。
第二种形式是指定 本地检出的目录全路径 到 要导出的本地目录全路径,所有的本地修改将会保留,但是不在版本控制下(即没提交的新文件,因为.svn文件夹
里没有与之相关的信息记录)的文件不会拷贝。

例子:
注意/opt/svndata目录不能提前创建,下面导出命令执行后会自动创建该目录
即把kevin版本库里的所有文件都导出到本地的/op/svndata目录下了,不包括.svn
[root@localhost ~]# svn export svn://192.168.10.202/kevin /opt/svndata/ --username wangshibo --password wangshibo@123
A    /opt/svndata
A    /opt/svndata/test.html
Exported revision 7.

[root@localhost ~]# ls /opt/svndata/        #如上,kevin版本库里还没有任何文件
[root@localhost ~]# cd /opt/svndata/
[root@localhost svndata]# ll -a             #查看,发现导出后没有带.svn
total 12
drwxr-xr-x. 2 root root 4096 May 31 14:52 .
drwxr-xr-x. 4 root root 4096 May 31 14:52 ..

=======================================================================
添加新文件(svn add)
注:告诉SVN服务器要添加文件了,还要用svn commint -m真实的上传上去!
svn add test.php #添加test.php 
svn commit -m "添加我的测试用test.php" test.php  #提交新加的文件到svn服务器里
svn add *.php #添加当前目录下所有的php文件
svn commit -m "添加我的测试用全部php文件" *.php

[root@localhost kevin]# echo "test123123" > test.html
[root@localhost kevin]# svn add test.html 
A         test.html
[root@localhost kevin]# svn commit -m "this is test html" 
Adding         test.html
Transmitting file data .
Committed revision 1.
[root@localhost kevin]# mkdir haha
[root@localhost kevin]# svn add haha
A         haha
[root@localhost kevin]# svn commit -m "add haha"         #"svn commit"可以简写成"svn ci"
Adding         haha

Committed revision 2.
[root@localhost kevin]# ls
haha  test.html

=======================================================================
svn 提交
svn commit -m "提交备注信息文本" [-N] [--no-unlock] 文件名
svn ci -m "提交备注信息文本" [-N] [--no-unlock] 文件名
必须带上-m参数,参数可以为空,但是必须写上-m

例子:
svn commit -m "提交当前目录下的全部在版本控制下的文件" *    #注意这个*表示全部文件
svn commit -m "提交我的测试用test.php" test.php
svn commit -m "提交我的测试用test.php" -N --no-unlock test.php    #保持锁就用–no-unlock开关
svn ci -m "提交当前目录下的全部在版本控制下的文件" *    #注意这个*表示全部文件
svn ci -m "提交我的测试用test.php" test.php
svn ci -m "提交我的测试用test.php" -N --no-unlock test.php    #保持锁就用–no-unlock开关

=======================================================================
svn更新操作。即把svn服务器上最新的版本更新下来
[root@localhost kevin]# svn update     或者"svn up"
At revision 1.

=======================================================================
svn查看
[root@localhost kevin]# svn info
Path: .
URL: svn://192.168.10.202/kevin
Repository Root: svn://192.168.10.202/kevin
Repository UUID: a5e3da23-8188-47af-afb7-fe4507492688
Revision: 1
Node Kind: directory
Schedule: normal
Last Changed Author: wangshibo
Last Changed Rev: 1
Last Changed Date: 2018-05-31 14:21:46 +0800 (Thu, 31 May 2018)

=======================================================================
svn删除文件(简写svn del)
svn delete svn://路径(目录或文件的全路径) -m "删除备注信息文本"
推荐如下操作:
# svn delete 文件名 
# svn ci -m "删除备注信息文本"

[root@localhost kevin]# svn delete haha             #或者svn del haha
D         haha
[root@localhost kevin]# svn commit -m "del haha"    #或者svn ci -m "del haha"
Deleting       haha

Committed revision 4.
[root@localhost kevin]# ls
test.html

[root@localhost kevin]# svn delete svn://192.168.10.202/kevin/test.html -m "删除测试文件test.html
[root@localhost kevin]# svn update
D         haha
D         test.html
Updated to revision 8.
[root@localhost kevin]# ls
[root@localhost kevin]# 

注意:svn的删除使用delete,而不是rm

=======================================================================
svn查看日志
[root@localhost kevin]# svn log    #显示所有文件的所有修改记录
------------------------------------------------------------------------
r1 | wangshibo | 2018-05-31 14:21:46 +0800 (Thu, 31 May 2018) | 1 line

this is test html
------------------------------------------------------------------------
[root@localhost kevin]# svn log test.html     #显示test.html这个文件的所有修改记录,及其版本号的变化 
------------------------------------------------------------------------
r1 | wangshibo | 2018-05-31 14:21:46 +0800 (Thu, 31 May 2018) | 1 line

this is test html
------------------------------------------------------------------------

=======================================================================
版本库下的文件和目录列表 
[root@localhost kevin]# svn ls
test.html

=======================================================================
恢复本地修改 
svn revert: 恢复原始未改变的工作副本文件 (恢复大部份的本地修改)。
revert: 用法: revert PATH... 
注意: 本子命令不会存取网络,并且会解除冲突的状况。但是它不会恢复被删除的目录;也不会恢复已经经过svn commit提交过的文件
[root@localhost kevin]# cat test.html 
test123123
[root@localhost kevin]# echo "5555" >> test.html 
[root@localhost kevin]# cat test.html 
test123123
5555
[root@localhost kevin]# svn revert test.html 
Reverted 'test.html'
[root@localhost kevin]# cat test.html 
test123123

=======================================================================
加锁/解锁 
svn lock -m “加锁备注信息文本“ [--force] 文件名 
svn unlock 文件名
例子:
# svn lock -m “锁信测试用test.php文件“ test.php 
# svn unlock test.php

=======================================================================
比较差异 
svn diff 文件名 
svn diff -r 修正版本号m:修正版本号n 文件名
例子:
# svn diff test.php<- 将修改的文件与基础版本比较
# svn diff -r 200:201 test.php<- 对 修正版本号200 和 修正版本号201 比较差异

=======================================================================
查看文件或目录状态(简称svn st)
# svn st 目录路径/名
# svn status 目录路径/名   
目录下的文件和子目录的状态,正常状态不显示 
?:不在svn的控制中; M:内容被修改;C:发生冲突;A:预定加入到版本库;K:被锁定

# svn -v 目录路径/名
# svn status -v 目录路径/名
显示文件和子目录状态
第一列保持相同,第二列显示工作版本号,
第三和第四列显示最后一次修改的版本号和修改人 
注意:
svn status、svn diff和 svn revert这三条命令在没有网络的情况下也可以执行的,原因是svn在本地的.svn中保留了本地版本的原始拷贝。
[root@localhost kevin]# svn status -v test.html 
1        1 wangshibo    test.html

=======================================================================
解决冲突 
# svn resolved [本地目录全路径]

例子:
# svn update
C foo.c
Updated to revision 31.

如果你在更新时得到冲突,你的工作拷贝会产生三个新的文件:
# ls
foo.c
foo.c.mine
foo.c.r30
foo.c.r31
当你解决了foo.c的冲突,并且准备提交,运行svn resolved让你的工作拷贝知道你已经完成了所有事情。
你可以仅仅删除冲突的文件并且提交,但是svn resolved除

=======================================================================
新建一个分支copy
# svn copy branchA branchB -m "make B branch" 
表示从branchA拷贝出一个新分支branchB

=======================================================================
合并内容到分支merge
# svn merge branchA branchB 
把对branchA的修改合并到分支branchB
posted @ 2017-02-24 15:48  散尽浮华  阅读(8992)  评论(0编辑  收藏  举报