跳轉到內容

版本控制/Subversion

來自 Wikibooks,開放世界的開放書籍

Subversion 設計基於 CVS

Subversion 最初被設計成一個更好的 CVS,所以它擁有 CVS 的大部分功能。通常,Subversion 對特定功能的介面類似於 CVS 的介面,除非有令人信服的理由這樣做。

Subversion 已經超越了其最初的目標,即取代 CVS,但其歷史影響了其功能和介面的選擇;今天的 Subversion 對於 CVS 使用者來說應該仍然非常熟悉。


Subversion 貫穿示例:Windows XP

[編輯 | 編輯原始碼]

貫穿示例:安裝 Subversion 和 Subversion 工具

[編輯 | 編輯原始碼]
  • 下載 msi svn-1.4.6-setup.exe
  • 執行 msi

Subversion 由多個元件組成。在這裡,我們將檢查三個元件的安裝

  • svn
  • svnadmin
  • svnlook
  • 開啟命令提示符。
C:\>svn --version
svn, version 1.4.5 (r25188)
   compiled Aug 22 2007, 20:49:04

Copyright (C) 2000-2006 CollabNet.
Subversion is open source software, see http://subversion.tigris.org/
This product includes software developed by CollabNet (http://www.Collab.Net/).

The following repository access (RA) modules are available:

* ra_dav : Module for accessing a repository via WebDAV (DeltaV) protocol.
  - handles 'http' scheme
  - handles 'https' scheme
* ra_svn : Module for accessing a repository using the svn network protocol.
  - handles 'svn' scheme
* ra_local : Module for accessing a repository on local disk.
  - handles 'file' scheme



C:\>svnadmin --version
svnadmin, version 1.4.5 (r25188)
   compiled Aug 22 2007, 20:49:04

Copyright (C) 2000-2006 CollabNet.
Subversion is open source software, see http://subversion.tigris.org/
This product includes software developed by CollabNet (http://www.Collab.Net/).

The following repository back-end (FS) modules are available:

* fs_base : Module for working with a Berkeley DB repository.
* fs_fs : Module for working with a plain file (FSFS) repository.
C:\>svnlook --version
svnlook, version 1.4.5 (r25188)
   compiled Aug 22 2007, 20:49:04

Copyright (C) 2000-2006 CollabNet.
Subversion is open source software, see http://subversion.tigris.org/
This product includes software developed by CollabNet (http://www.Collab.Net/).

The following repository back-end (FS) modules are available:

* fs_base : Module for working with a Berkeley DB repository.
* fs_fs : Module for working with a plain file (FSFS) repository.

C:\>

貫穿示例:設定倉庫

[編輯 | 編輯原始碼]

Thesvnadmin create命令用於建立倉庫。為了在這個例子中清楚地區分客戶端或伺服器,我們將建立兩個目錄serverclient. 例子中倉庫的管理是在 server 目錄中進行的,而客戶端工作是在 client 目錄中進行的。

svnadmin create repos

[編輯 | 編輯原始碼]

本節介紹如何建立倉庫。在 Subversion 的日常使用中,這只是一項您不會經常執行的任務。對於嘗試新的想法,瞭解如何設定一個簡單的倉庫非常有用。要建立倉庫,您需要使用 svnadmin 命令。

C:\>svnadmin help create
create: usage: svnadmin create REPOS_PATH

Create a new, empty repository at REPOS_PATH.

Valid options:
  --bdb-txn-nosync         : disable fsync at transaction commit [Berkeley DB]
  --bdb-log-keep           : disable automatic log file removal [Berkeley DB]
  --config-dir arg         : read user configuration files from directory ARG
  --fs-type arg            : type of repository: 'fsfs' (default) or 'bdb'
  --pre-1.4-compatible     : use format compatible with Subversion versions
                             earlier than 1.4

首先建立 server 目錄並進入該目錄。

C:\WB>MD server

C:\WB>CD server

在 repos 目錄中建立一個倉庫。

C:\WB\server>svnadmin create repos

C:\WB\server>

我們現在可以檢視倉庫的標準目錄結構。

C:\WB\server>DIR repos
 Volume in drive C is MyCDrive
 Volume Serial Number is 64A5-9C22

 Directory of C:\WB\server\repos

09/01/2009  14:27    <DIR>          .
09/01/2009  14:27    <DIR>          ..
09/01/2009  14:27    <DIR>          conf
09/01/2009  14:27    <DIR>          dav
09/01/2009  14:27    <DIR>          db
09/01/2009  14:27                 2 format
09/01/2009  14:27    <DIR>          hooks
09/01/2009  14:27    <DIR>          locks
09/01/2009  14:27               234 README.txt
               2 File(s)            236 bytes
               7 Dir(s)  146,102,767,616 bytes free
C:\WB\server>

svnlook 是 Subversion 實用程式的一部分。它提供了一種檢視倉庫的方法,而不會改變倉庫的任何內容。

C:\WB>svnlook youngest server\repos
0

C:\WB>

這表明倉庫在修訂版 0,即還沒有發生任何提交。

將檔案新增到 Subversion 倉庫

[編輯 | 編輯原始碼]

如果您有一個現有的檔案系統,您希望將其新增到版本控制中,有多種方法可以做到。

  • 使用svn import
  • 建立一個版本化的目錄,然後提交它。
svn import
[編輯 | 編輯原始碼]

svn import可用於將未版本化的目錄匯入 Subversion。匯入後,目錄仍然未版本化。要使用版本化的目錄副本進行工作,需要從倉庫簽出它。

C:\WB>svn help import
import: Commit an unversioned file or tree into the repository.
usage: import [PATH] URL

  Recursively commit a copy of PATH to URL.
  If PATH is omitted '.' is assumed.
  Parent directories are created as necessary in the repository.
  If PATH is a directory, the contents of the directory are added
  directly under URL.

Valid options:
  -q [--quiet]             : print as little as possible
  -N [--non-recursive]     : operate on single directory only
  --auto-props             : enable automatic properties
  --no-auto-props          : disable automatic properties
  -m [--message] arg       : specify log message ARG
  -F [--file] arg          : read log message from file ARG
  --force-log              : force validity of log message source
  --editor-cmd arg         : use ARG as external editor
  --encoding arg           : treat value as being in charset encoding ARG
  --no-ignore              : disregard default and svn:ignore property ignores
  --username arg           : specify a username ARG
  --password arg           : specify a password ARG
  --no-auth-cache          : do not cache authentication tokens
  --non-interactive        : do no interactive prompting
  --config-dir arg         : read user configuration files from directory ARG

在 Subversion 中設定專案的標準方法是使用以下結構

project
   +--- branches
   +--- tags
   +--- trunk

我們將在本例中遵循此結構。首先,我們建立一個臨時目錄來匯入。

C:\WB>MKDIR temp

C:\WB>cd temp

C:\WB\temp>MKDIR branches tags trunk

C:\WB\temp>cd ..

現在我們將此結構匯入倉庫。使用 -m 選項為倉庫中的每個更改提供訊息是良好的做法。在許多倉庫中,伺服器強制要求提供提交訊息。

C:\WB>svn import temp file:///C:/WB/server/repos/ -m "Initial set up."
Adding         temp\trunk
Adding         temp\branches
Adding         temp\tags

Committed revision 1.

C:\WB>

我們不會在這些貫穿示例中使用太多svnlook但以下展示了svnlook youngest命令的使用。這是一個伺服器端命令。

C:\WB>svnlook youngest server\repos
1

C:\WB>

現在我們已經匯入了 temp 目錄,我們刪除它。

C:\WB>RMDIR /S temp
temp, Are you sure (Y/N)? y

C:\WB>


客戶端

[編輯 | 編輯原始碼]

使用 Windows

[編輯 | 編輯原始碼]

我們現在將從倉庫簽出 trunk 並開始處理它。

C:\WB\server>CD ..
C:\WB>MD client
C:\WB>CD client
C:\WB\client>

您現在可以從倉庫簽出 trunk 到您的客戶端工作區。

C:\WB>svn co file:///C:/WB/server/repos/trunk client\trunk
Checked out revision 1.

C:\WB>
C:\WB>cd client

C:\WB\client>
C:\WB\client>DIR /A trunk
 Volume in drive C is PROGRAMS
 Volume Serial Number is 64A5-9C22

 Directory of C:\WB\client\trunk

23/02/2009  10:12    <DIR>          .
23/02/2009  10:12    <DIR>          ..
23/02/2009  10:12    <DIR>          .svn
               0 File(s)              0 bytes
               3 Dir(s)  145,724,104,704 bytes free

C:\WB\client>

可以看到,在.svn目錄中有一個trunk目錄。

接下來,我們進入客戶端 trunk 目錄。檢視目錄的狀態,然後檢視目錄的更詳細狀態。

C:\WB\client>CD trunk

C:\WB\client\trunk>svn status

C:\WB\client\trunk>svn status -v
                1        1 CozensJ      .

您可以使用svn info命令找出目錄的版本化位置。

C:\WB\client\trunk>svn info
Path: .
URL: file:///C:/WB/server/repos/trunk
Repository Root: file:///C:/WB/server/repos
Repository UUID: 81664a5f-b906-8c41-a38a-d61973a1aa89
Revision: 1
Node Kind: directory
Schedule: normal
Last Changed Author: CozensJ
Last Changed Rev: 1
Last Changed Date: 2009-02-23 09:46:43 +0000 (Mon, 23 Feb 2009)

我們現在繼續使用選單示例。開啟記事本並建立檔案 “menu.txt”。

C:\WB\client\trunk>notepad menu.txt

現在檢查狀態顯示 menu.txt 對 Subversion 來說是一個未知檔案。

C:\WB\client\trunk>svn status
?      menu.txt

C:\WB\client\trunk>svn add menu.txt
A         menu.txt

C:\WB\client\trunk>svn status
A      menu.txt

C:\WB\client\trunk>svn staus -v
Unknown command: 'staus'
Type 'svn help' for usage.

C:\WB\client\trunk>svn status -v
                1        1 CozensJ      .
A               0       ?   ?           menu.txt

C:\WB\client\trunk>svn diff
Index: menu.txt
===================================================================

C:\WB\client\trunk>svn diff
Index: menu.txt
===================================================================
--- menu.txt    (revision 0)
+++ menu.txt    (revision 0)
@@ -0,0 +1,11 @@
+[Date]
+Monday 5th January 2009
+
+[Starters]
+Starter of the day.
+
+[Main]
+Dish of the day.
+
+[Desserts]
+Creme Brulee

C:\WB\client\trunk>

貫穿示例:設定 Subversion 和 TortoiseSVN

[編輯 | 編輯原始碼]

本貫穿示例將向您展示如何將 Subversion 設定為一組命令列應用程式,以及如何設定 TortoiseSVN 客戶端。

貫穿示例:多個工作者和衝突

[編輯 | 編輯原始碼]

本貫穿示例將引導您完成一個使用者,然後是兩個使用者使用倉庫的過程。它將向您展示如何以及在何處更改檔案的不同版本和目錄。它接著展示衝突如何發生,以及如何解決它們。

先決條件

[編輯 | 編輯原始碼]

要執行本貫穿示例,您需要安裝以下內容

svn - Subversion 客戶端 TortoiseSvn(可選)

本貫穿示例將執行以下步驟

  • 建立一個新的倉庫。
  • Sam(使用者 1)獲取工作副本並提交一些更改。
  • Pat(使用者 2)獲取工作副本。
    • Sam 做出更改並提交。
    • Pat 做出更改並提交。
  • Pat 做出一些更改並提交。
    • Sam 更新他過時的工作副本。
  • Sam 和 Pat 做出一些更改,最終導致 Pat 發生衝突。
  • Pat 解決她的衝突。

在貫穿示例過程中,您可以使用 svn 命令列工具和 TortoiseSVN 檢視更改。

貫穿示例:Subversion 管理

[編輯 | 編輯原始碼]

本指南主要針對 Subversion 管理員。本指南將向您展示如何建立本地儲存庫並使用 file:// 模式訪問它。相同的方法用於建立完整的儲存庫,但它們被配置為使用不同的模式訪問,例如 svn:// 和 http:// 等。

華夏公益教科書