一、SVN介绍

SVN的全称是Subversion,即版本控制系统。它是最流行的一个开放源代码的版本控制系统。作为一个开源的版本控制系统,Subversion管理着随时间改变的数据。这些数据放置在一个中央资料档案库(Repository)中。这个档案库很像一个普通的文件服务器,不过它会记住每一次文件的变动。这样就可以把档案恢复到旧的版本,或是浏览文件的变动历史。Subversion是一个通用的系统,可用来管理任何类型的文件,其中包括程序源码。
SVN采用客户端/服务器体系,项目的各种版本都存储在服务器上,程序开发人员首先将从服务器上获得一份项目的最新版本,并将其复制到本机,然后在此基础上,每个开发人员可以在自己的客户端进行独立的开发工作,并且可以随时将新代码提交给服务器。当然也可以通过更新操作获取服务器上的最新代码,从而保持与其他开发者所使用版本的一致性。 [3]
SVN的客户端有两类,一类是基于Web的WebSVN等,另一类是以Tortoise SVN为代表的客户端软件。前者需要Web服务器的支持,后者需要用户在本地安装客户端,两种都有免费的开源软件供使用。SVN存储版本数据也两种方式:BDB(一种事务安全型表类型)和FSFS(一种不需要数据库的存储系统)。因为BDB方式在服务器中断时,有可能锁住数据,所以还是FSFS方式更安全一点。

(一)SVN工作机制

SVN系统具体是如何实现对项目软件的版本控制,一方面通过实现历史操作记录查阅。在任意一台服务器中都可以添加一个SVN版本库,而相应的版本库中存放大量的程序和文档,而这些项目资源主要通过配置管理员依据不同的配置管理计划对不同项目的组员分配与之相符合的访问权限,进而实现对资源的统一管理;只有SVN标本过版本库中的资源,项目组成员可以对版本资源库中的资源进行访问。
一次简单的访问过程包括:相关项目组员首先在客户操作端建立一个从版本库检索出来的项目文件,而后就可以对拷贝的档案进行修改,最后通过SVN提交命令将其修改后的项目文件提交到终端服务器,终端服务器最终会对修改后的项目文件做最后的综合更新记录。
修改过的文件在修改未被提交到服务器前,SVN服务器只会对已经提交到网络端服务器的项目文档进行更新审核,并与其他人的合并,在此之前修改过的文档是保密的,提交之后SVN络端服务器会将修改后与修改之前的数据进行比较,并在后台对修改内容就行标注显示,进而实现对历史操作记录的更新记载。最终实现项目组组员既能检索出旧版本,又能通过SVN实现新旧版本的对比,另一方面SVN通过进行组员间的协同开发实现对项目软件的版本控制。协同开发一般是指版本控制系统间接受并处理不同用户提交的各种不同性质版本的资源代码,同时允许各个用户之间在遵循相应规则范围内实现合作开发。如何处理好有矛盾的版本控制系统才是能够协同开发的关键,像是多个程序编码员同时对同一份资源代码进行修改、提交到SVN版本库,就有可能发生提交后的版本意见想法相冲等问题。

(二)SVN优势

1.存储
SVN服务器既具有CVS所具有数据储存的优点,像是信息资源存储后会形成资源树结构,便于存储的同时,数据一般不会丢失,同时又拥有自己的特色。SVN是通过关系数据库及二进制的存储方式,同时解决了既往不能同时读写同一文件等问题,同时增添了自己特有的“零或一”原则。
2.速度
与人们初始的CVS相比,SVN在速度运行方面有很大提升。因为SVN服务器只支持少量的信息、资源传输,与其他系统相比,更支持的是离线模式,因此避免了网络拥挤现象的出现。
3.安全性
SVN是一种技术性更加安全的产品,实现了系统和控制两方面的结合。一方面可以将系统整体的安全功能有效地分布在分支系统中,进而保证分支系统能正常运行,从而使各分支系统能够互补,最终在系统整体性的安全性得以保障,通过均衡原则实现最终追求安全的目的。

二、安装SVN

1、安装(Fedora发行版)

  1. //服务端
  2. [mate@localhost ~]$ sudo dnf install subversion
  3. //GUI客户端
  4. [mate@localhost ~]$ sudo dnf install rabbitvcs*

三、配置SVN

1、新建仓库

创建仓库时会根据创建的仓库名称创建一个文件夹

  1. #创建仓库
  2. mate@matelearn:~/SVN$ svnadmin create JAC-Project
  3. #根据仓库名自动生成一个文件夹
  4. mate@matelearn:~/SVN$ ls
  5. JAC-Project
  6. #进入文件夹查看仓库树
  7. mate@matelearn:~/SVN$ cd JAC-Project
  8. mate@matelearn:~/SVN/JAC-Project$ ls
  9. conf db format hooks locks README.txt
  10. mate@matelearn:~/SVN/JAC-Project$ tree
  11. .
  12. ├── conf
  13. ├── authz
  14. ├── hooks-env.tmpl
  15. ├── passwd
  16. └── svnserve.conf
  17. ├── db
  18. ├── current
  19. ├── format
  20. ├── fsfs.conf
  21. ├── fs-type
  22. ├── min-unpacked-rev
  23. ├── revprops
  24. └── 0
  25. └── 0
  26. ├── revs
  27. └── 0
  28. └── 0
  29. ├── transactions
  30. ├── txn-current
  31. ├── txn-current-lock
  32. ├── txn-protorevs
  33. ├── uuid
  34. └── write-lock
  35. ├── format
  36. ├── hooks
  37. ├── post-commit.tmpl
  38. ├── post-lock.tmpl
  39. ├── post-revprop-change.tmpl
  40. ├── post-unlock.tmpl
  41. ├── pre-commit.tmpl
  42. ├── pre-lock.tmpl
  43. ├── pre-revprop-change.tmpl
  44. ├── pre-unlock.tmpl
  45. └── start-commit.tmpl
  46. ├── locks
  47. ├── db.lock
  48. └── db-logs.lock
  49. └── README.txt

2、配置仓库

对仓库的配置主要是对conf目录下的文件进行配置

(1)添加用户及权限:authz文件

  1. ### This file is an example authorization file for svnserve.
  2. ### Its format is identical to that of mod_authz_svn authorization
  3. ### files.
  4. ### As shown below each section defines authorizations for the path and
  5. ### (optional) repository specified by the section name.
  6. ### The authorizations follow. An authorization line can refer to:
  7. ### - a single user,
  8. ### - a group of users defined in a special [groups] section,
  9. ### - an alias defined in a special [aliases] section,
  10. ### - all authenticated users, using the '$authenticated' token,
  11. ### - only anonymous users, using the '$anonymous' token,
  12. ### - anyone, using the '*' wildcard.
  13. ###
  14. ### A match can be inverted by prefixing the rule with '~'. Rules can
  15. ### grant read ('r') access, read-write ('rw') access, or no access
  16. ### ('').
  17. [aliases]
  18. # joe = /C=XZ/ST=Dessert/L=Snake City/O=Snake Oil, Ltd./OU=Research Institute/CN=Joe Average
  19. [groups]
  20. # harry_and_sally = harry,sally
  21. # harry_sally_and_joe = harry,sally,&joe
  22. # [/foo/bar]
  23. # harry = rw
  24. # &joe = r
  25. # * =
  26. # [repository:/baz/fuz]
  27. # @harry_and_sally = rw
  28. # * = r
  29. #
  30. #
  31. [/]
  32. mate = wr

(2)设置密码:passwd

  1. ### This file is an example password file for svnserve.
  2. ### Its format is similar to that of svnserve.conf. As shown in the
  3. ### example below it contains one section labelled [users].
  4. ### The name and password for each user follow, one account per line.
  5. [users]
  6. # harry = harryssecret
  7. # sally = sallyssecret
  8. #
  9. mate = zmy20141321

(3)服务配置:svnserve.conf

  1. ### This file controls the configuration of the svnserve daemon, if you
  2. ### use it to allow access to this repository. (If you only allow
  3. ### access through http: and/or file: URLs, then this file is
  4. ### irrelevant.)
  5. ### Visit http://subversion.apache.org/ for more information.
  6. [general]
  7. ### The anon-access and auth-access options control access to the
  8. ### repository for unauthenticated (a.k.a. anonymous) users and
  9. ### authenticated users, respectively.
  10. ### Valid values are "write", "read", and "none".
  11. ### Setting the value to "none" prohibits both reading and writing;
  12. ### "read" allows read-only access, and "write" allows complete
  13. ### read/write access to the repository.
  14. ### The sample settings below are the defaults and specify that anonymous
  15. ### users have read-only access to the repository, while authenticated
  16. ### users have read and write access to the repository.
  17. anon-access = none
  18. auth-access = write
  19. ### The password-db option controls the location of the password
  20. ### database file. Unless you specify a path starting with a /,
  21. ### the file's location is relative to the directory containing
  22. ### this configuration file.
  23. ### If SASL is enabled (see below), this file will NOT be used.
  24. ### Uncomment the line below to use the default password file.
  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
  29. ### directory containing this file. The specified path may be a
  30. ### repository relative URL (^/) or an absolute file:// URL to a text
  31. ### file in a Subversion repository. If you don't specify an authz-db,
  32. ### no path-based access control is done.
  33. ### Uncomment the line below to use the default authorization file.
  34. authz-db = authz
  35. ### The groups-db option controls the location of the groups file.
  36. ### Unless you specify a path starting with a /, the file's location is
  37. ### relative to the directory containing this file. The specified path
  38. ### may be a repository relative URL (^/) or an absolute file:// URL to a
  39. ### text file in a Subversion repository.
  40. # groups-db = groups
  41. ### This option specifies the authentication realm of the repository.
  42. ### If two repositories have the same authentication realm, they should
  43. ### have the same password database, and vice versa. The default realm
  44. ### is repository's uuid.
  45. realm = JAC-Project
  46. ### The force-username-case option causes svnserve to case-normalize
  47. ### usernames before comparing them against the authorization rules in the
  48. ### authz-db file configured above. Valid values are "upper" (to upper-
  49. ### case the usernames), "lower" (to lowercase the usernames), and
  50. ### "none" (to compare usernames as-is without case conversion, which
  51. ### is the default behavior).
  52. # force-username-case = none
  53. ### The hooks-env options specifies a path to the hook script environment
  54. ### configuration file. This option overrides the per-repository default
  55. ### and can be used to configure the hook script environment for multiple
  56. ### repositories in a single file, if an absolute path is specified.
  57. ### Unless you specify an absolute path, the file's location is relative
  58. ### to the directory containing this file.
  59. # hooks-env = hooks-env
  60. [sasl]
  61. ### This option specifies whether you want to use the Cyrus SASL
  62. ### library for authentication. Default is false.
  63. ### This section will be ignored if svnserve is not built with Cyrus
  64. ### SASL support; to check, run 'svnserve --version' and look for a line
  65. ### reading 'Cyrus SASL authentication is available.'
  66. # use-sasl = true
  67. ### These options specify the desired strength of the security layer
  68. ### that you want SASL to provide. 0 means no encryption, 1 means
  69. ### integrity-checking only, values larger than 1 are correlated
  70. ### to the effective key length for encryption (e.g. 128 means 128-bit
  71. ### encryption). The values below are the defaults.
  72. # min-encryption = 0
  73. # max-encryption = 256

四、启动仓库

1、启动单个仓库

方式一:-r直接指定到版本库(称之为单库svnserve方式)

svnserve -d -r /home/mate/SVN/JAC-Project
在这种情况下,一个svnserve只能为一个版本库工作。
authz配置文件中对版本库权限的配置应这样写:
[/]
@admin=rw
user2=r
使用类似这样的URL:svn://192.168.0.1/ 即可访问JAC-Project版本库

2、多库启动

SVN目录下有Documents、JAC-Project两个仓库
image.png

方式二:指定到版本库的上级目录(称之为多库svnserve方式)

svnserve -d -r /home/mate/SVN
这种情况,一个svnserve可以为多个版本库工作
svnserve会检查目录下的所有库的配置文件,

使用类似这样的URL:svn://192.168.0.1/Documents 即可访问 Documents 版本库。

3、添加系统自启

(1)创建自启脚本

[Unit]
Description=SVN service
After=network.target mount_toshiba.service

[Service]
Type=forking
WorkingDirectory=/home/pi/toshiba/SVN
ExecStart=svnserve -d -r ./
TimeoutSec=0
RemainAfterExit=yes
SysVStartPriority=99

[Install] 
WantedBy=multi-user.target

(2)复制脚本至目录:/etc/systemd/system/

##将这个文件用 root 用户保存到 /etc/systemd/system 目录:
sudo cp myscript.service /etc/systemd/system/
##然后就可以用下面的命令尝试启动服务了:
sudo systemctl start myscript.service
##停止服务:
sudo systemctl stop myscript.service
##设置开机时自动运行:
sudo systemctl enable myscript.service
##systemctl 命令还可以用来重启或禁用它。

五、常见问题及解决方案

(一)文件被锁定

用SVN经常出现被锁定而无法提交的问题,选择解锁又提示没有文件被锁定,很是头疼。这里整理了一下SVN 被锁定的几种解决方法:

  • 出现这个问题后使用“清理”即”Clean up“功能,如果还不行,就直接到上一级目录,再执行“清理”,然后再“更新”。
  • 有时候如果看到某个包里面的文件夹没有SVN的标志,直接用“Ctrl+Delete”手工删除,然后“清理”,最后“更新”或“提交”。
  • 有时候有的文件死活无法提交和更新,有lock字样的提示,:首先点击该文件,然后getLock,再选中stealLock,然后release lock即可
  • 教大家最狠的一招,在项目中如果是出现某个文件死锁的情况无法解决的情况下,叫开发中所有的人把文件上传了解锁了,清空版本库地址:安装目录:SvnTracsvnprojectsdblocks这个文件夹路径里面的全部内容。一切ok!