Saturday, November 24, 2012

Setup Subversion in Ubuntu


安裝 Subversion

要善用Ubuntu超級豐富的APT套件庫,不但省時省力、日後也好維護。
sudo apt-get install subversion
一道指令解決。

設定 SVN 伺服器

自己架設伺服器也只需要很少的幾個步驟。
sudo apt-get install xinet.d (EDIT: xinet.d -> xinetd)
這道指令並非與SVN直接相關,而是安裝一個xinetd的daemon程式,它就像一個伺服器服務程式的管家,負責幫忙接待想透過網路連結服務的客戶。

建立新檔案在 /etc/xinet.d/svn ,檔案內容如下:
# default: off
# description: svnserve is the server part of Subversion.
# server_args = -i -r /var/lib/svn/repositories
service svn
{
disable = no
port = 3690
socket_type = stream
protocol = tcp
wait = no
user = svn (EDIT: svn change to your username e.g.: henryY)
server = /usr/bin/svnserve
server_args = -i -r /home/svn
}

* /home/svn 是自行指定的 repository 存放目錄

接著建立 svn 的 user 及 home directory:
sudo mkdir /home/svn
sudo useradd -d /home/svn svn (EDIT:sudo useradd -d /home/svn henryY)
sudo chown svn.svn /home/svn (EDIT: sudo chown svn.henryY /home/svn)

重新啟動 xinetd ,測試SVN服務:
sudo /etc/init.d/xinetd restart
telnet localhost 3690

當telnet程式連線成功,並出現以下訊息,則表示SVN daemon已經啟動成功:
( success ( 1 2 ( ANONYMOUS ) ( edit-pipeline svndiff1 absent-entries ) ) )

建立新的 repository

cd /home/svn
sudo svnadmin create NEW_REPO (EDIT:NEW_REPO->MyTestApp)
sudo chown svn.svn NEW_REPO -R (EDIT:sudo chown svn.henryY MyTestApp)

完成之後,就能已 svn 指令(SVN client)操作。
svn list svn://localhost/NEW_REPO (EDIT: svn list svn://localhost/MyTestApp)
當然,因為都還沒有丟檔案,回傳一定是空的。

如果要操作權限(例如有帳號密碼可讀寫,訪客只能讀不能寫),則需要編輯 NEW_REPO/conf/svnserve.conf,設定以下三行(取消註解):
anon-access = read
auth-access = write
password-db = passwd

並在 NEW_REPO/conf/passwd 下新增帳號及密碼,例如:
[users]
jacky = pwd_is_blank

---= Q&A =---

SVN server在運作一段時間後,可能出現資料庫錯誤的狀況,訊息如下:
Could not open the requested SVN filesystem

此時必須使用 db_recover 指令進行修復,以Ubuntu Linux來說,安裝db4.*-util套件即可取得此程式。

以下示範在 Ubuntu Linux 的流程:

先搜尋有哪些db4.*-util版本可以安裝。
apt-cache search db4.*-util
db4.6-util - Berkeley v4.6 Database Utilities db4.2-util - Berkeley v4.2 Database Utilities db4.3-util - Berkeley v4.3 Database Utilities db4.4-util - Berkeley v4.4 Database Utilities db4.5-util - Berkeley v4.5 Database Utilities
我選擇的是db4.4-util,因此開始apt-get安裝。
apt-get install db4.4-util

安裝完成後,就會多一道 db4.4_recover 指令,切換到 svn repository directory 下執行這道指令即可修復。
更多參考資源在:

Then, if we finished all the steps above, we can "svn import"  the first version of our project repo..

type ==> sudo svn import -m "first revision checkin" /home/henryY/dev/MyNewAppProj svn://localhost/MyTestApp/trunk

There maybe an error:
Can't open file '/home/svn/MyTestApp/db/txn-current-lock': Permission denied
 If you "ls" the ./MyTestApp/db directory,
you'll see::
drwxr-sr-x 6 svn henryY 4096 Nov 25 00:40 db/
Since the "svn"  group haven't got the previlege to "Write", only user/owner (i.e. henryY) have the permission...

So, type =>
sudo chmod g+w /home/svn/MyTestApp/*
and do the command below can solved the problme!
sudo svn import -m "first revision checkin" /home/henryY/dev/MyNewAppProj svn://localhost/MyTestApp/trunk

Then, you can commit the latest changes in the working copy project root :
1.) sudo svn ci -m "Log message for the changes..." + user passwd
2.) svn  update
3.) svn log
We must need to update the svn before it reaches the head revision even you've done the commit action. Then, you can review the your working folder's svn log.

Ref: http://blog.lyhdev.com/2008/07/playing-svn-with-ubuntu.html