Subversion

出自 Arch Linux 中文维基

"Apache Subversion 是一套功能全面的版本控制系統,最初被設計為CVS的改進版本。其後Subversion的發展大大超出了取代CVS的原始目標,但它的基本模型、設計和接口仍然受到了這一目標的深刻影響。"

本文主要介紹架設svn伺服器的方法。有兩種流行的svn伺服器,內建的svnserve以及更高級的選擇——結合了svn插件的Apache HTTP Server

用於Subversion安裝的Apache伺服器[編輯 | 編輯原始碼]

目標[編輯 | 編輯原始碼]

這篇指南的目標是結合Apache安裝Subversion。選用Apache是因為其提供了單機svnserve不具備的諸多特性。

  • 支持 HTTPS,比 svnserve 使用的 md5 認證更加安全。
  • 細粒度的控制權。可以使用 Apache 的認證限制目錄的訪問權限。可以允許所有文件可讀,但僅允許提交 trunk,同時對另一組用戶賦予提交 tags 和 branches 的權限。
  • 一個自由的源碼庫查看器
  • Subversion 團隊正在進行無縫 webdav 集成的工作。不久你就能用任何 webdav 接口更新源碼庫中的文件。

安裝[編輯 | 編輯原始碼]

安裝 Apache HTTP Server。除了 apache 之外,還需要安裝 subversion

配置 Subversion[編輯 | 編輯原始碼]

創建一個目錄[編輯 | 編輯原始碼]

# mkdir -p /home/svn/repositories

編輯 httpd.conf[編輯 | 編輯原始碼]

請確認下列模塊加載指令在文件中列出。如果沒有請添加它們(通常你只需要添加後兩行),保持先後順序:

/etc/httpd/conf/httpd.conf
LoadModule dav_module           modules/mod_dav.so
 LoadModule dav_fs_module        modules/mod_dav_fs.so
 LoadModule dav_svn_module       modules/mod_dav_svn.so
 LoadModule authz_svn_module     modules/mod_authz_svn.so

用不用SSL?[編輯 | 編輯原始碼]

SSL允許用戶使用Apache的AuthType Basic而不必擔心有人嗅探密碼。

生成證書:

# cd /etc/httpd/conf/
# openssl req -new -x509 -keyout server.key -out server.crt -days 365 -nodes

然後添加下面的配置到/etc/httpd/conf/extra/httpd-ssl.conf,以便在虛擬主機配置指令中包含它們。

<Location /svn>
   DAV svn
   SVNParentPath /home/svn/repositories
   AuthzSVNAccessFile /home/svn/.svn-policy-file
   AuthName "SVN Repositories"
   AuthType Basic
   AuthUserFile /home/svn/.svn-auth-file
   Satisfy Any
   Require valid-user
</Location>

為了確保SSL設置已加載,取消/etc/httpd/conf/httpd.conf中SSL配置行的註釋:

Include /etc/httpd/conf/extra/httpd-ssl.conf
LoadModule ssl_module modules/mod_ssl.so	
LoadModule socache_shmcb_module modules/mod_socache_shmcb.so

創建/home/svn/.svn-policy-file[編輯 | 編輯原始碼]

[/]
* = r

[REPO_NAME:/]
USER_NAME = rw

/部分中的*用來匹配匿名用戶。對除只讀以外的任何訪問Apache AuthType Basic都會提示輸入用戶名和密碼。REPO_NAME:/一節繼承了之前的權限設置,於是匿名用戶對其有隻讀權限。最後一項設置為用戶USER_NAME授予來REPO_NAME源碼庫的讀寫權限。

創建/home/svn/.svn-auth-file[編輯 | 編輯原始碼]

這個文件可以用htpasswd或htdigest創建。這裏使用了htpasswd。同樣,因為SSL,不用過多擔心密碼嗅探。htdigest甚至會對嗅探提供更好的安全特性。

# htpasswd -cs /home/svn/.svn-auth-file USER_NAME

以上創建了文件(-c)並使用SHA1保存密碼(-s)用戶USER_NAME被創建。要添加其他用戶,可以去掉 -c 選項:

# htpasswd -s /home/svn/.svn-auth-file OTHER_USER_NAME

創建源碼庫[編輯 | 編輯原始碼]

# svnadmin create /home/svn/repositories/REPO_NAME

設置權限[編輯 | 編輯原始碼]

對Apache用戶設置新源碼庫的權限:

# chown -R http.http /home/svn/repositories/REPO_NAME

創建項目[編輯 | 編輯原始碼]

項目的目錄結構[編輯 | 編輯原始碼]

創建 branches tags trunk 臨時目錄結構:

$ cd /path/to/directoryofchoice
$ mkdir -p ~/svn-import/{branches,tags,trunk}

將源碼添加到目錄[編輯 | 編輯原始碼]

將原始碼文件放入創建的 trunk 目錄:

$ cp -R /my/existing/project/* ~/svn-import/trunk

導入項目[編輯 | 編輯原始碼]

$ svn import -m "Initial import" ~/svn-import https://yourdomain.net/svn/REPO_NAME/

測試SVN檢出[編輯 | 編輯原始碼]

$ svn checkout https://yourdomain.net/svn/REPO_NAME/ /my/svn/working/copy

如果以上所有配置都成功,你應該能得到一個受版本控制的新源碼庫的副本。

設置 Svnserve[編輯 | 編輯原始碼]

安裝軟件包[編輯 | 編輯原始碼]

安裝 subversion

創建源碼庫[編輯 | 編輯原始碼]

創建你的源碼庫

mkdir /path/to/repos/
svnadmin create /path/to/repos/repo1

初始源碼庫是空的,如果想導入文件,使用以下命令:

svn import ~/code/project1 file:///path/to/repos/repo1 --message 'Initial repository layout'

設置訪問策略[編輯 | 編輯原始碼]

編輯文件/path/to/repos/repo1/conf/svnserve.conf,在[general]中取消以下行的註釋或者添加之:

password-db = passwd

你也許想改變對匿名用戶的默認設置

anon-access = read

對允許任何人提交的源碼庫,替換"read"為"write",或者將其改為"none"來禁止所有匿名訪問。

編輯文件/path/to/repos/repo1/conf/passwd

[users]
harry = foopassword
sally = barpassword

以上定義了用戶harry和sally,分別使用密碼foopassword和barpassword,可以按需修改。

啟動伺服器守護進程[編輯 | 編輯原始碼]

在啟動伺服器之前,編輯配置文件

/etc/conf.d/svnserve
SVNSERVE_ARGS="--root=/path/to/repos"

The --root=/path/to/repos option set the root of repository tree. If you have multiple repositories use --root=/path-to/reposparent. Then access independent repositories by passing in repository name in the URL: svn://host/repo1. make sure that the user has read/write access to the repository files)

Optionally add a --listen-port if you want a different port, or other options.

By default, the service runs as root. If you want to change that, add a drop-in:

/etc/systemd/system/svnserve.service.d/50-custom.conf
[Service]
User=svn

Now start the svnserve.service daemon.

svn+ssh[編輯 | 編輯原始碼]

To use svn+ssh://, we have to have a wrapper written for svnserve.

check where the svnserve binary is located:

# which svnserve
/usr/local/bin/svnserve

Our wrapper is going to have to fall in PATH prior to this location...

Create a wrapper with the following content:

/usr/bin/svnserve
#!/bin/sh
# wrapper script for svnserve
umask 007
/usr/local/bin/svnserve -r /path/to "$@"

Then, make it executable.

-r /path/to is what makes use of the svn co svn+ssh://server.domain.com:/reponame instead of :/path/to/reponame.

Start svnserve with new wrapper script like so:

# /usr/bin/svnserve -d  ( start daemon mode )

we can also check the perms for remote users like this:

$ svn ls svn+ssh://server.domain.com:/reponame
++server.domain.com++
dev/
qa/
release/

Subversion backup and restore[編輯 | 編輯原始碼]

To back up your subversion repositories, do this for each repository you have.

$ svnadmin dump /path/to/repo > /tmp/repo.dump

To restore the backup, create the corresponding repositories first:

$ svnadmin create /path/to/repo

Then load svn dump into new repo:

$ svnadmin load /path/to/repo < /tmp/repo.dump

Setting permissions:

$ chown -R svn:svnusers /path/to/repo
$ chmod -R g+w /path/to/repo/db/

These repositories should now be all setup.

Subversion clients[編輯 | 編輯原始碼]

See also Wikipedia:Comparison of Subversion clients.

  • kdesvn — Subversion client for KDE.
https://invent.kde.org/sdk/kdesvn || kdesvn
  • RabbitVCS — Set of graphical tools written to provide simple and straightforward access to the version control systems you use.
http://rabbitvcs.org/ || rabbitvcsAUR
  • RapidSVN — GUI front-end for the Subversion revision system written in C++ using the wxWidgets framework.
http://rapidsvn.tigris.org/ || rapidsvnAUR

See also[編輯 | 編輯原始碼]