接触Subversion

空间IDC提供Subversion功能.偶好奇查了查,
好像是一个项目开发与版本控制系统,CVS的替代产品.
虽然一般大型程序开发才会用这个东西,不过了解一下,应该不错.

Subversion的一些资料(搜罗自网络,与Subversion官方站点 http://subversion.tigris.org/)

Subversion快速入门教程(转)
作者: Rock Sun
2005-10-20
如何快速建立Subversion服务器,并且在项目中使用起来,这是大家最关心的问题,与CVS相比,Subversion有更多的选择,也更加的容易,几个命令就可以建立一套服务器环境,可以使用起来,这里配套有动画教程。

本文是使用Subversion最快速的教程,在最短的时间里帮助您建立起一套可用的服务器环境,只需略加调整就可以应用到实际项目当中。

本教程分为以下几个部门,不仅仅是快速入门,最后我们还有一些高级功能的说明,为了说明简单,教程是在windows下使用的方式,以方便资源有限的项目使用,对于UNIX环境下,区别并不大。

1. 软件下载
2. 服务器和客户端安装
3. 建立版本库(Repository)
4. 配置用户和权限
5. 运行独立服务器
6. 初始化导入
7. 基本客户端操作

1,软件下载

* 下载Subversion服务器程序。

到官方网站 的下载二进制安装文件,来到二进制包下载部分 ,找到 Windows NT, 2000, XP and 2003部分,然后选择”this directory”,这样我们可以看到许多下载的内容,目前可以下栽 svn-1.2.3-setup.exe。

* 下载Subversion的Windows客户端TortoiseSVN。

TortoiseSVN是扩展Windows Shell的一套工具,可以看作Windows资源管理器的插件,安装之后Windows就可以识别Subversion的工作目录。
官方网站是TortoiseSVN,下载方式和前面的svn服务器类似,在Download页面的我们选择Official version for Win2k/XP or higher的版本,然后在sourceforge的下载页面选择目前的最高稳定版本的安装文件TortoiseSVN-1.2.4.4479-svn-1.2.3.msi。

2,服务器和客户端安装

* 服务器安装,直接运行svn-1.2.3-setup.exe,根据提示安装即可,这样我们就有了一套服务器可以运行的环境。
* 安装TortoiseSVN,同样直接运行TortoiseSVN-1.2.4.4479-svn-1.2.3.msi按照提示安装即可,不过最后完成后会提示是否重启,其实重启只是使svn工作拷贝在windows中的特殊样式生效,与所有的实际功能无关,这里为了立刻看到好的效果,还是重新启动机器。

3,建立版本库(Repository)

运行Subversion服务器需要首先要建立一个版本库(Repository),可以看作服务器上存放数据的数据库,在安装了Subversion服务器之后,可以直接运行,如:

svnadmin create E:\svndemo\repository

就会在目录E:\svndemo\repository下创建一个版本库。

我们也可以使用TortoiseSVN图形化的完成这一步:

在目录E:\svndemo\repository下”右键->TortoiseSVN->Create Repository here…“, 然后可以选择版本库模式, 这里使用默认即可, 然后就创建了一系列目录和文件。

4,配置用户和权限

来到E:\svndemo\repository\conf目录,修改svnserve.conf:

# [general]
# password-db = passwd

改为:

[general]
password-db = passwd

然后修改同目录的passwd文件,去掉下面三行的注释:

# [users]
# harry = harryssecret
# sally = sallyssecret

最后变成:

[users]
harry = harryssecret
sally = sallyssecret

5,运行独立服务器

在任意目录下运行:

svnserve -d -r E:\svndemo\repository

我们的服务器程序就已经启动了。

6,初始化导入

来到我们想要导入的项目根目录,在这个例子里是E:\svndemo\initproject,目录下有一个readme.txt文件:

1. 右键->TortoiseSVN->Import…
2. URL of repository输入“svn://localhost/trunk”
3. ok

完成之后目录没有任何变化,如果没有报错,数据就已经全部导入到了我们刚才定义的版本库中。

7,基本客户端操作

取出版本库到一个工作拷贝:

来到任意空目录下,在本例中是E:\svndemo\wc1,运行右键->Checkout,在URL of repository中输入svn://localhost/trunk,这样我们就得到了一份工作拷贝。

在工作拷贝中作出修改并提交:

打开readme.txt,作出修改,然后右键->Commit…,这样我们就把修改提交到了版本库,我们可以运行。

察看所作的修改:

readme.txt上右键->TortoiseSVN->Show Log,这样我们就可以看到我们对这个文件所有的提交。在版本1上右键->Compare with working copy,我们可以比较工作拷贝的文件和版本1的区别。

最后,所有的内容都已经录制为动画文件,大家可以参考。

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=537628

Subversion基本使用
客户端组件供使用者使用,主要包括以下两个组件:
svn 是命令行客户端程序,用来管理数据.
Svnversion 用来查看工作拷贝的混合版本状态.
服务器组件供管理员使用,包括以下几个组件:
svnadmin 用来创建与调整Subversion的文件库的工具.
svnlook 用来查看Subversion的文件库的工具.
一,Subversion在项目中用到的操作
档案库的两种配置方案:

方案1 方案2
个人意见:
如果projectA和projectB之间互相独立,则使用方案1比较好管理.
如果projectA和projectB之间联系的比较紧密,则使用方案2比较好管理.
创建文件库(新建一个项目),命令如下: (服务端操作)
mkdir /usr/local/svn
svnadmin create /usr/local/svn /java
将目录doc的内容,直接导入至文件库的fox目录里,命令如下:
mkdir java
mkdir java/trunk //主线
mkdir java/branches //分支
mkdir java/tags //标记,用来存放不再变动的分支
svn import . http://localhost/svn/java -m “Initial repository layout” 或
svn import java http://localhost/svn/java -m “Initial repository layout”
checkout(可缩写为co)服务器文件到本地机器上(/usr/local/svn/work)
cd /usr/local/work //先建立本地的工作路径
svn co http://localhost/svn/java java –username afa –password guesswhat
网页访问方式http://myserver/svn/ 的方式
通过命令行执行例如checkout,update等各种操作(建议先切换到当前工作路径).
TortoiseSVN访问方式需要安装TortoiseSVN,可代替命令行执行各种操作.
commit 提交
当本地工作目录上修改或新建文件或文件夹时,服务器上得仓库并不会自动修改,需要使用commit命令把本地做的修改保存到服务器上.每次修改后commit,服务器上得版本号会加1.
新增文件或文件夹(先进入到当前目录)
svn add newfile/newdir
svn commit –m “message”
本地更新文件update(不带参数则更新为最新版本)
svn update –r 3 test.java //本地的test.java更新为第三次提交的版本
注意几个状态:
A 新增
D 删除
U 更新
C 冲突
G 合并
创建分支(branch)和标记(tag)
$ svn checkout http://serverip/svn/java java
$ cd java
$ svn copy trunk/calc branches/calc-branch-1.0
$ svn status
$ svn commit -m “Creating a private branch of /trunk/calc.”
合并分支
(1)合并从某个版本都某个版本
cd 到相应的目录(如 /usr/local/work/java/trunk)
$ svn merge -r 343:344 http://serverip/svn/java/branches/java-branch-1.0
(2)合并整个分支(如从341版本产生了分支到现在要合并起来)
$ svn merge -r 341:HEAD http://serverip/svn/java/branches/java-branch-1.0
(3) 合并时产生冲突的处理主要是靠手动修改.
<<<<<<>>>>>> .merge-right.r14 //这两行是分支上做的修改
修改完了之后,把svn产生的多余的控制文件删除(或用svn resolved命令)再commit就OK了.
“svn status”显示本地更新,如果没有任何更新(或刚刚commit)则没有任何信息显示.
“svn diff”显示详细的更新细节:
$ svn diff -r 343:344 http://serverip/svn/java/trunk/work.java
“svn revert”移除用户的本地更新(本地做了更新,但没有提交时有效).
“svn resolved”命令,当手工解决了版本冲突之后,在commit之前执行,用来告诉SVN”我已经解决了冲突,请让我提交吧”
svn resolved work.java //当前目录下的work.java
svn delete删除命令
cd到某个需要被删除的目录
$ svn del –force branches
$ svn commit –m “Delete the java branches”
其他命令(对服务端进行操作)
(1)svnlook tree path/java –show-ids
(2)备份与恢复(特别是对于升级Subversion的情况)
完整式备份与恢复:
$ svnadmin dump java > dumpfile
$ svnadmin create dumpjava //可以修改仓库(项目)的名字
$ svnadmin load dumpjava dumpfile1
$ svnadmin dump myrepos –revision 1001:2000 –incremental > dumpfile2
$ svnadmin dump myrepos –revision 2001:3000 –incremental > dumpfile3
$ svnadmin load newrepos < dumpfile1
$ svnadmin load newrepos < dumpfile2
$ svnadmin load newrepos < dumpfile3
二,仓库的权限设置
创建口令文件(使用Apache的htpasswd)
$ cd /urs/local/Apache/bin //进入到apache的bin目录
$ htpasswd -c /etc/svn-auth-file afa //第一次: 以 -c 建立口令文件
New password: *****
Re-type new password: *****
Adding password for user afa
$ htpasswd /etc/svn-auth-file test //第二次不用-C参数,但要指定口令文件(含路径)
New password: *******
Re-type new password: *******
Adding password for user test
$
编辑httpd.conf文件,加入身份验证
DAV svn
SVNParentPath /usr/local/svn
AuthType Basic
AuthName “Subversion repository”
AuthUserFile /etc/svn-auth-file
Require valid-user
更细致的权限控制――分组管理
DAV svn
SVNParentPath /usr/local/svn
AuthzSVNAccessFile /etc/svn/authz.conf
AuthType Basic
AuthName “Subversion repository”
AuthUserFile /etc/svn-auth-file
Require valid-user
authz.conf的内容为:
[groups]
everyone = afa,test, tommy, songjy, zhengyong, orcus, zhangq
groupA = afa,robert, zhangq
groupB = test,orcus, tommy
[projectA:/]
@groupA = rw
@everyone = r
[projectB:/]
@everyone = r
@groupB = rw

下篇这篇,是Dreamhost Wiki关于Svn的文档.非常详细
不过偶看得头疼很

Subversion
From DreamHost
Jump to: navigation, search
This article or section may require a cleanup.
We are hoping to create articles that meet certain standards. Please discuss this issue on the talk page. Editing help is available.

Subversion (official site) is a version-control system that keeps track of changes in your files on a server. Subversion helps to coordinate work among multiple people. For an introduction – read the book – it is really great.
Contents
[hide]

* 1 Subversion on Dreamhost
* 2 Quick Start
o 2.1 Gotchas
* 3 Connecting to Subversion Repository
o 3.1 Using svn+ssh
o 3.2 Using HTTP
* 4 How to use: quick reference
o 4.1 Creating Repository via Command-Line
+ 4.1.1 Gotchas
o 4.2 Importing Folders and Files
o 4.3 Loading a dumpfile
o 4.4 Using TortoiseSVN
+ 4.4.1 Create Session Key
+ 4.4.2 Setup New Session
+ 4.4.3 Other References
+ 4.4.4 Notes
o 4.5 Multiple User Access to Repository
o 4.6 Using Apache basic authentication
o 4.7 Using Subversion for Web development (automatic post-commit checkout)
* 5 Upgrading to Newest Subversion
* 6 Interfacing with Subversion from PHP
* 7 Setting up post commit emails
* 8 External Links
o 8.1 Books and tutorials
o 8.2 GUI Integration
o 8.3 Web-based repository browsers
* 9 See Also
* 10 Userbased Scripts

[edit] Subversion on Dreamhost

The current version of Subversion on Dreamhost is 1.4.2. If you want to install a newer version, please see Subversion_Installation. If your server is still running old 1.3.x version, let us know and we can upgrade it for you. Subversion 1.4.x features repository mirroring, more efficient transfer and storage of binary files in comparison with previous versions.

You can install and configure Subversion repositories from control panel.
[edit] Quick Start

To set up a Subversion repository at svn.yoursite.com for a project called “myproject”, follow these steps:

1. Create svn.yoursite.com:
1. Go to your DreamHost control panel and click Domains, then Manage Domains
2. Click Add New Domain / Sub-Domain
3. Enter “svn.yoursite.com” in the Domain To Host field, enter “svn.yoursite.com” in the Web Directory field, and select Remove www.
4. Click Fully Host This Domain Now!
2. Wait a few hours until the new DNS information propagates (that is, when visiting svn.yoursite.com in a web browser doesn’t give an error–the page will look like an empty directory listing once the DNS info has propagated).
3. Set up a Subversion repository:
1. Click Goodies, then Subversion
2. Enter “My Project” in the Project Name field, “myproject” in the Project ID field, and make sure that svn.yoursite.com is selected for the URL. Use “myproject” as the directory name.
3. Replace the dummy usernames and passwords with a username and password for yourself.
4. Click Create My New Project Repository Now!

As soon as the DreamHost Subversive Robot actually creates your repository, you can begin using it. For instance, to import a directory:

svn import some_directory http://svn.yoursite.com/myproject -m “Initial import”

[edit] Gotchas

Warning: If you create a new repository at an existing domain, the domain may be moved to a new Apache service and the DNS information will change. This will affect you if you have your DNS info hosted somewhere else than Dreamhost (you’ll have to get the new IP address and update your DNS info). This is only the case with domains that have been hosted with DreamHost for more than a year or so. Anything added more recently than that will probably not be affected.

Warning: Repositories don’t act like normal directories: You will not be able to use .htaccess files, cgi scripts, etc. in your repository directory. For instance, if you setup your repository at http://svn.example.com/, you will not be able to password protect that directory using .htaccess. You must use the Public/Private field in the Subversion panel page. Changes made in the Htaccess/WebDAV panel also do not apply to your SVN repository.

This also means you can’t install ViewVC in the main repository directory. To work around this, put your repository in http://svn.example.com/svn/ and the ViewVC cgi in http://svn.example.com/. Remember, you’ll need to use a .htaccess file to configure http://svn.example.com/, but the Public/Private field in the Subversion panel to protect http://svn.example.com/svn/.

Hastily-made repositories often don’t work: If you create your repository before its intended host is fully created (and resolved), you may have an inaccessible repository. Assuming it doesn’t yet contain any information (how could it?), just delete it and recreate it. (Alternatively, you may be experiencing a DNS propagation delay if you can access your repository via http within the DreamHost system, but see an empty directory from outside DreamHost [Apache 1.3x serving your empty domain]. If you can’t wait, try querying the DH DNS servers for your IP and using a local hosts file or NetInfo to temporarily resolve your svn host. Remember to undo this when DNS records have propagated.)

Some have reported success just submitting an edit (with no changes) to their repository in the Control Panel. This operation recreates your reponame.access and reponame.passwd files re-establishing dhapache as their group (and undoing any MD5 passwords you’ve made manually). Unfortunately, this does nothing to fix your repository if the owner has been changed from dhapache to your username.

It would be nice if the Subversion Control Panel had an explicit option to fix owner and group on a given repository, say, by running:

sudo chown -R dhapache ~/svn/reponame;
sudo chgrp dhapache ~/svn/reponame.access ~/svn/reponame.passwd;

… in the background. As it is, you have to dump, recreate and reload your repository.

Another nice feature would be a Control Panel switch to allow passwords to be created and maintained with MD5 encryption.

Some clients require MD5 passwords: If you get “access denied” warnings, you may need to use MD5 passwords instead of the standard variety created by the DreamHost Control Panel. Use htpasswd -nm username to generate the entry (ala username:passwd-hash) for your reponame.passwd file. And use an editor (ala nano reponame.passwd) to replace your standard password entry.

Don’t overwrite your reponame.access and reponame.passwd files with echo “username:passwd-hash” > reponame.passwd Overwriting the files will cause them to have your user’s group instead of dhapache, leaving your repository in a read-only or inaccessible state.

Removing repositories through the control panel doesn’t seem to remove the ~/svn/ directory. (as of Apr 12 2006) Workaround:

cd ~/svn;
mkdir old;
mv old

You have the proper permissions to do this. (You can also use WebFTP to delete or rename the repository directory.)
[edit] Connecting to Subversion Repository
[edit] Using svn+ssh

Allows for a svn client to connect directly with the repository via ssh.

You have to set up a ssh username and password. Subversion users defined in the panel will not be used for authentication.

svn+ssh://{user}@www.yourdomain.ext/home/{user}/svn/{project}

note: {project} is the ID of your subversion project, NOT the projectname.

You should also check SSH#Passwordless_Login to use this mode without being asked for the password repeated times

See also SVN book for SSH tricks
[edit] Using HTTP

1. Use the HTTP path that you used in the Subversion panel.
2. Sign in using username and password specified in the panel.

Is there any way for svn users to change their password in this mode? No. Only through panel.
Is there any way to use https instead of http? Probably, but first you will need a unique IP address and Secure Server installed on your domain.

[edit] How to use: quick reference
The instructions provided in this article or section require shell access unless otherwise stated.

You can use the PuTTY client on Windows, or SSH on UNIX.
Your account must be setup for shell access in the Control Panel if you haven’t done so already.
More information may be available on the article’s talk page.
[edit] Creating Repository via Command-Line

If you didn’t use the Dreamhost Subversion Panel, you can create the repository at a directory. If you used the Quick Start method then skip this step and go to the next one.

svnadmin create /home/user/path/to/repository

[edit] Gotchas

Handmade repos are read-only (via http): If you find that you have to tweak and reload your dumpfile a few times, it can be handy to use the CLI tools (e.g. rm -rf and svnadmin create) to wipe and recreate your repository until you get it right. But then you’ll want to use the Control Panel to delete and recreate your repository. Your test repositories will not function properly in production (they won’t permit any changes).

The Panel creates the repository with dhapache as the owner, and the reponame.access and reponame.passwd files with dhapache as the group. Trying to create your own production repo with svnadmin create is futile because you don’t have authorization to change those ownerships and group memberships (and you’re not a sudoer).

Don’t overwrite dhapache files: Don’t overwrite your reponame.access and reponame.passwd files with echo “username:passwd-hash” > reponame.passwd (e.g. if you’re trying to use MD5 passwords). Use an editor and paste in the new data. Overwriting the files will cause them to have your user’s group instead of dhapache. If your repository is relatively mature when you make this mistake, and you have no backup, you will be out of options.
[edit] Importing Folders and Files

Once the repository is created you can add folders and files that you have in another location. It is better to use this method to keep the revisions number down when first creating the repository.

svn import /home/USER/path/to/source file:///home/USER/path/to/repository –message=”Importing Project”

You have to put a message in and change the text inside to whatever you are doing at the time.
[edit] Loading a dumpfile

Sometimes, you would have an existing repository that you want to migrate to your Dreamhost svn repository. You will need to take a dump of your existing repository and load it into your Dreamhost repository.

* Dump your existing repository

svnadmin dump path-to-repo | gzip > dumpfile.gz

* Upload dumpfile.gz to your home directory.

* Load this into your repository.

If you set up using the Dreamhost Subversion Panel, then the repository will be under the $HOME/svn/{repo-id} where the repo-id is the id you used to setup the repository in the panel. Decompress the dumpfile first.

gunzip -c dumpfile.gz | svnadmin load /home/USER/path/to/repository

Command should show all files being imported, if not then check the instructions and try again.
[edit] Using TortoiseSVN

TortoiseSVN is a Windows GUI integration program for svn. Use at your own risk and is not officially supported.

This walkthrough assumes username is myname, with a website at www.mysite.com, and that the repositories are in ~/svn starting with a project named myproject. You can choose your own directory names.

Step 1: Create the repository following the Repository section of this guide. Step 2: Download and install SVN client
[edit] Create Session Key

If you don’t create the public session key, you will have to type your password in for every action in the browser and for every time TortoiseSVN connects to the server.

Start in your home directory

cd ~

Generate the key set using the passphrase of your choice and put the public key in a .ssh directory (note: the name “rsakey” isn’t required)

Write down your passphrase as you will need it later.

ssh-keygen -b 1024 -t rsa -N SomeLongTextForPassphrase -f rsakey

Create the .ssh folder

mkdir .ssh

Move the public key inside of the folder and rename it.

mv rsakey.pub .ssh/authorized_keys

Change permissions on both the folder and file for security.

chmod 700 .ssh
chmod 600 .ssh/authorized_keys

Download and install the Putty tools (scroll down to the Windows-style installer). This is an SSH client with authentication tools.

Download your “rsakey” file (your private key) from the server to your local machine (the rsakey should be in your home directory).

Use your Windows FTP client.

Load it into the PuTTYgen tool (puttygen.exe) and enter your passphrase when it asks for it.

Use SSH-2 (RSA) which will be the default.

Save it as a private key from PuTTYgen in a safe place as rsakey.ppk.
[edit] Setup New Session

1. Run PuTTY Pageant. Pageant will put an icon of a computer wearing a hat into the System tray.
2. Load a private key into it. Double click on the icon and then click on Private Key File.
3. Type in the Passphrase.

* Run Putty and make a new session:
o Hostname: www.mysite.com
o Protocol: SSH
o Saved Sessions: mysiteSSH
o Connection->Data->auto-login username: myuser
o Connection->SSH->preferred protocol: 2
o Connection->SSH->Auth->private key file: path\to\rsakey.ppk

* Click “Save” on the sessions panel to save this config.

* Open PuTTY if you want to test your session. PuTTY will notice that Pageant is running, retrieve the key automatically from Pageant, and use it to authenticate. You can now open as many PuTTY sessions as you like without having to type your passphrase again. You may get a message saying, “The server’s host key is not set in the registry.” (Insert what this means here.)

* Now find your local project directory, and copy what you want to add to Subversion into a temporary directory (let’s call it “ImportMe”), organizing it carefully — it’s smart to put your files in a “trunk” directory so that working with new branches and tags later is simple.

* Right-click on the ImportMe folder and select “Import…” from the TortoiseSVN submenu. You will be adding everything inside this folder into the root of the “myproject” repository.

* In the import dialog, enter the URL of Repository like this. Replace “mysiteSSH” with the name of your PuTTY stored session and “myusername” with your real username. If you just want a straight SSH tunnel, we’d use [email protected] here instead of mysiteSSH:

svn+ssh://mysiteSSH/home/myusername/svn/myproject

…and click “Okay”. This will load your project files into the repository.

* Next, check out the files you want to work with into your project directory (but make sure you aren’t overwriting files when you check out or you’ll get errors). This is the “SVN Checkout…” option in the folder context menu.

Everything should be pretty self-explanatory from there on.
[edit] Other References

[1] – For Windows XP

[2] – Guide from TortoiseSVN site (generate RSA key instead of DSA key).
[edit] Notes

* TortoiseSVN (1.1.3) – Go into Settings and enter TortoisePlink as the SSH client under the Network tab to connect to your repository. You can use svn+ssh://[email protected]/home/myusername/svn/myproject to specify your username directly in the URL. Along with Pageant for key-based authentication, using TortoiseSVN is very transparent and easy to use.

Also screen is getting over worked for overnight checkouts (yes overnight)

screen
svn co &> ~/log_co &
^ad

[edit] Multiple User Access to Repository

(I have only tried this in a bundy shell, not externally (with TortoiseSVN, for example)

* Create at least two users (mysvnadmin, mysvnuser)
* Create the repository and import the project with mysvnuser.
* Login (ssh) to mysvnuser’s account:

$ su mysvnuser
$ svn co file:///home/mysvnadmin/repos/project work
$ svn add file
$ svn commit -m “added file”
$ svn log
r1 | mysvnadmin | 2005-09-04 08:03:47 -0700 (04 Sep 2005) | 1 line
initial import
r2 | mysvnuser | 2005-09-04 23:34:13 -0700 (04 Oct 2005) | 1 line
added file

I had no trouble with svn+shh (on bundy):

$ svn co svn+ssh://[email protected]/home/mysvnadmin/repos/project work

[edit] Using Apache basic authentication

The Dreamhost panel provides a way to create user accounts only for Subversion- the above steps require each user to have a Unix account with shell access so they can log in via SSH.

If you want to do per-directory permissions in your repository using those accounts, you need to manually edit the .access file as described here:

Subversion manual

However, if you make changes to the user list using the Dreamhost control panel, it will overwrite any changes you make to the .access file.

You can manually add and administer the usernames and passwords for Subversion by using htpasswd to add/edit users to the .passwd file for your repository. This is not recommended though, because after manual editing either the .access or .passwd files, the group changes from dhapache to whatever the auto-generated group for your account is. This prevents the Apache module from being able to read the files. You then need to change the permissions so that they can be read by apache: chmod 644

Note that this is giving “world” read access to those files, which potentially is a security risk. Using control panel automatically resets group back to dbapache, but you will lose your per-directory settings. Using Apache and these passwords isn’t very secure anyway without https access, so this is all more for convenience. If you need security, create Unix accounts with SSH shell access as described above.
[edit] Using Subversion for Web development (automatic post-commit checkout)

Subversion can be a very useful tool for developing Web sites or Web-based applications. You may wonder, though, how that would work. After you commit your changes, how do they flow into the site?

Here’s how: you make your site into just another working copy, checked out from your Subversion repository into the appropriate publicly-accessible directory. Then, when you commit a change, you run svn update against that other working copy, and voila! All your updates are on the site.

Tortoise context menus only work in windows explorer. If you want to use the tortoise commands (checkout,update,…) on your dreamhost directories , you can map a network drive to your ftp location. You will need a tool like “novell netdrive” in order to do the mapping.

This, in fact, is the method recommended in the Subversion FAQ. The FAQ even mentions that you can go a step further and have your site update automatically whenever you commit, using Subversion’s cool “hook scripts” functionality to automatically run svn update upon commit.

A userbased script to this purpose can be found down the bottom Userbased Scripts

However, if you try that on Dreamhost, you’ll find that the post-commit hook script will never successfully execute svn update. Why? Here’s the explanation I got from DH:

The reason for why this doesn’t work is that since the [hook] script gets executed as the apache process, the script runs as user dhapache. If you try to have user dhapache (which is a normal unprivileged user) update a subversion repo in your home directory and owned by you, it will fail.

In other words, it’s a permissions issue. And due to the way Subversion is set up by DH, it’s one that you can’t fix — at least not directly.

There is a way around this, though: instead of having your hook script do the svn update directly, we’re going to put a C program in the middle. In other words, instead of the post-commit hook script calling svn update, it will now call a C program, which will execute a bash script that runs the svn update. This gets you around the “script runs as Web server user” issue.

Here’s the steps for doing this:

1. Move your non-working post-commit file to post-commit.bash.

2. Create a file named post-commit.c with the contents:

#define REAL_SCRIPT “/path/to/post-commit.bash”
#include
#include
main( ac, av )
char **av;
{
execv( REAL_SCRIPT, av );
}

(Note: CHANGE the value of the REAL_SCRIPT variable in the C program to be the full path to your newly created post-commit.bash file — something like “/home/YOURUSER/svn/YOURREPOSITORY/hooks/post-commit.bash”.)

3. Compile the C code:

gcc -o post-commit post-commit.c

4. Setuid the file:

chmod 4755 post-commit

(if the above doesn’t work, try)

chmod +s post-commit

Assuming that you have gave the right path to post-commit.bash in the C program, Subversion will now automatically execute it every time you make a commit to your repository, allowing you to do cool stuff like automatically updating a site with newly committed changes. Pretty nifty!

Why does this work? By setting the setuid/setgid bit, when dhapache calls the executable it will temporarily assume the permissions of the owner of the file (you) and perform the actions. For security reasons, this functionality is disabled on most operating systems for shell scripts, so we have to go with the C code.

You may also want to deny public access to the .svn folders that are checked out as part of a subversion repository by adding this to an .htaccess file in your web root:

RedirectMatch 403 /\.svn.*$

[edit] Upgrading to Newest Subversion

If you would like to install a newer version of subversion (e.g., for repository syncing), then you will need to go to this page

http://subversion.tigris.org/servlets/ProjectDocumentList?folderID=260&expandFolder=74

and download both the subversion-x.x.x.tar.bz2 and the subversion-deps-x.x.x.tar.bz2. The “deps” file contains all the subversion dependencies.

The newest subversion as of 2007.03.12 is 1.4.3. To install locally, create a temporary work directory and do the following.

mkdir tmpwork
cd tmpwork
wget http://subversion.tigris.org/downloads/subversion-1.4.3.tar.bz2
wget http://subversion.tigris.org/downloads/subversion-deps-1.4.3.tar.bz2
tar -xjf subversion-1.4.3.tar.bz2
tar -xjf subversion-deps-1.4.3.tar.bz2
cd subversion-1.4.3
./configure –prefix=/home/{yourlogin} –with-ssl
make
make install

That will install the new subversion into your user directory. You’ll need to change the path to include your ~/bin directory to use the new binaries.
[edit] Interfacing with Subversion from PHP

You’ll need to install the PECL extension SVN. Detailed instructions are available here: http://hp.jpsband.org/xhtml-compiler/docs/compile-svn.txt
[edit] Setting up post commit emails

Simply edit your svn/(repository name)/hooks/post-commit.tmpl file. Change /usr/lib/subversion/hook-scripts/commit-email.pl “$REPOS” “$REV” [email protected] to your commit email list. Then reinitialize SVN.

The actual file should be post-commit, not post-commit.tmpl. If you use an editor that edits in place, as opposed to (for instance) emacs which moves the original aside and creates a fresh file, you should still have the right owner/group and not need any reinitializing.

The default settings for commit-email.pl may be to use a connection to localhost instead of firing up sendmail, in which case you need the –from parameter for bounce/envelope address.
[edit] External Links
[edit] Books and tutorials

Version Control with Subversion is the Online Subversion book freely available online in its entirety. If you have shared hosting, focus on the file:// and svn+ssh:// protocols (which do not involve any actual continually running “server” processes that might not be good in a shared environment). If you have a dedicated hosting, you can do whatever you want I imagine.

* Subversion on Dreamhost

[edit] GUI Integration

* SCPlugin is a Finder contextual menu plugin for Mac OS X for subversion operations. Windows users will recognize it as the Mac analogue to TortoiseSVN.
* SVN Client Requires payed registration after 60 day trial.
* SvnX is an excellent GUI for Mac OS X.
* TortoiseSVN is a subversion client implemented as a Windows shell extension.
* RapidSVN Multi-platform GUI for Mac OS X, Linux, Windows.
* Syncro SVN Client a cross platform Subversion client.

[edit] Web-based repository browsers

These provide a number of convenient features, including RSS feeds, integrated bug tracking, collaborative documentation:

* Trac (Difficult Installation)
* ViewVC formerly ViewCVS official site, old site
* WebSVN – auto-tarball of directories, syntax highlighting – official site
* SVN::Web – on CPAN

[edit] See Also

* Subversion Installation
* CVS
* TortoiseSVN using Putty sessions

[edit] Userbased Scripts

This script creates a post-commit hook that runs another post-commit (which actually performs updates). It disables both stdout and stderr to avoid problems with Apache and TortoiseSVN. The script will set up initial import directories (standard trunk, tag, branches folders) and create the necessary hook files to automatically update your http root from /trunk.

Simply copy and paste the following script, filling in the appropriate variables with your own information, into a file on your server. Then set execute permissions (chmod 775 scriptname) and run it (./scriptname). Once it’s done you can safely delete the script and try committing changes to your site.

#./bin/bash
pname=”REPNAME”
webpage=”WEBPAGEFOLDER/”
svnurl=”http://svn.YOURHOST.com”
homedir=”/home/YOURUSER”

mkdir svninit
mkdir svninit/trunk
mkdir svninit/branches
mkdir svninit/tags

svn import svninit $svnurl/$pname -m “Initial Import”

rm -r svninit

svn co $svnurl/$pname/trunk/ $homedir/$webpage

touch post-commit.c

echo ‘#include ‘ > post-commit.c
echo ‘#include ‘ >> post-commit.c
echo ‘#include ‘ >> post-commit.c
echo ‘int main(void)’ >> post-commit.c
echo ‘{‘ >> post-commit.c
echo ‘ close(1);’ >> post-commit.c
echo ‘ close(2);’ >> post-commit.c
echo ‘ execl(“/usr/bin/svn”, “svn”, “update”, “‘$homedir’/’$webpage'”, (const char *) NULL);’ >> post-commit.c
echo ‘ return(EXIT_SUCCESS);’ >> post-commit.c
echo ‘}’ >> post-commit.c

gcc -o post-commit.bash post-commit.c
rm post-commit.c
mv -f post-commit.bash $homedir/svn/$pname/hooks/
chmod 775 $homedir/svn/$pname/hooks/post-commit.bash
chmod +s $homedir/svn/$pname/hooks/post-commit.bash

touch post-commit.c

echo ‘#define REAL_SCRIPT “‘$homedir’/svn/’$pname’/hooks/post-commit.bash”‘ > post-commit.c
echo ‘#include ‘ >> post-commit.c
echo ‘#include ‘ >> post-commit.c
echo ‘#include ‘ >> post-commit.c
echo ‘#include ‘ >> post-commit.c
echo ‘int main( ac, av )’ >> post-commit.c
echo ‘ char **av;’ >> post-commit.c
echo ‘{‘ >> post-commit.c
echo ‘ close(1);’ >> post-commit.c
echo ‘ close(2);’ >> post-commit.c
echo ‘ execv( REAL_SCRIPT, av );’ >> post-commit.c
echo ‘ return(EXIT_SUCCESS);’ >> post-commit.c
echo ‘}’ >> post-commit.c

gcc -o post-commit post-commit.c
rm post-commit.c
mv -f post-commit $homedir/svn/$pname/hooks/
chmod 775 $homedir/svn/$pname/hooks/post-commit
chmod +s $homedir/svn/$pname/hooks/post-commit

1 Response to “接触Subversion”


Comments are currently closed.