版本控制 - Mercurial

41
1 版本控制 - Mercurial A quick introduction Vincent CY Liao 2010-06-06, revised at 2014-01-19

description

使用 Mercurial 進行版本控制

Transcript of 版本控制 - Mercurial

Page 1: 版本控制 - Mercurial

1

版本控制 - Mercurial

A quick introduction

Vincent CY Liao2010-06-06, revised at 2014-01-19

Page 2: 版本控制 - Mercurial

2

Outline

■ 序論

● 什麼是 Mercurial ?

● Mercurial vs. Subversion

■ 使用 Mercurial● 安裝與首次設定

● 日常使用■ 建立檔案庫■ 編修檔案■ 與其他成員合作

Page 3: 版本控制 - Mercurial

3

序論

■ 什麼是 Mercurial ?

■ Mercurial vs. Subversion

Page 4: 版本控制 - Mercurial

4

什麼是 Mercurial ?

■ 分散式版本控制系統 (Distributed Version Control System, DVCS)

● 大部份作業的進行不需網路

● 使開發能夠較為獨立不受影響

■ 發展現況● 活躍的發展中

■ 最新的版本 : 2.8.2 (Jan. 1, 2014)

● 在業界已廣泛使用■ Google, Sun Oracle, Mozilla …

● 支援多種作業系統■ AIX, Darwin, FreeBSD, Linux, Solaris, Windows ...

http://mercurial.selenic.com/

Page 5: 版本控制 - Mercurial

5

Mercurial vs. Subversion

repo

repo

repo

repo

clone

cloneclonepull/push

commit

repo

branch

workcopy

workcopy

workcopy

checkout

checkout

checkout

update

commit

Mercurial Subversion

Page 6: 版本控制 - Mercurial

6

Mercurial vs. Subversion (cont.)

mainrepo

repo

commit:bug commit:

bug-fix

push

repo

pull

一次取得修正好的版本

repo

workcopy

commit:bug

workcopy

update

取得尚有問題的版本

Mercurial Subversion

( 在 commit 時無需連線主檔案庫 )

Page 7: 版本控制 - Mercurial

7

Mercurial vs. Subversion (cont.)

Network-1

repo

commit

Network-2

repo

repo

commit

clone

Mercurial Subversion

pull+merge

Network-1

Network-2

repo

workcopy

Mobile Storage

workcopy

( 可在分離的網路使用 )

Page 8: 版本控制 - Mercurial

8

使用 Mercurial

■ 安裝與首次設定● 安裝流程概觀

● 取得軟體

● 建置與安裝

● 首次設定

Page 9: 版本控制 - Mercurial

9

安裝流程概觀

■ 前置要件

● Python 2.4+

■ 安裝流程● 取得軟體

● 安裝 Python■ 若系統中沒有 Python 或是版本太舊才需要進行這步驟

■ 使用 python -V 指令檢視系統的 Python 版本

● 安裝 Mercurial

Page 10: 版本控制 - Mercurial

10

取得軟體 (Python)http://www.python.org/download/

Page 11: 版本控制 - Mercurial

11

取得軟體 (Mercurial)http://mercurial.selenic.com/downloads/

Page 12: 版本控制 - Mercurial

12

建置與安裝

■ 準備安裝目錄

■ 安裝 Python ( 如需要 )

cd ~mkdir bin

mkdir tmpcd tmptar -jxf ~/Python-2.7.5.tar.bz2

./configure –prefix=/home/USER/bin/python-2.7makemake install

cd ~/binln -s python-2.7/bin/python ../python --versionexport PATH=~/bin:$PATH

Page 13: 版本控制 - Mercurial

13

建置與安裝 (cont.)

■ 安裝 Mercurialcd tmp; tar -jxf ~/mercurial-2.6.3.tar.gzcd mercurial-2.6.3/make PREFIX=/home/USER/bin/mercurial installcp contrib/hgk ~/bin/mercurial/bin

cd ~/binln -s mercurial/bin/hg . ; ln -s mercurial/bin/hgk .

Page 14: 版本控制 - Mercurial

14

首次設定

■ 編輯設定檔 ~/.hgrc

● 也可針對 repository 有不同的設定

■ %REPO%/.hg/hgrc

[ui]username = Alicia <[email protected]>

[extensions]hgk =record =

Page 15: 版本控制 - Mercurial

15

使用 Mercurial

■ 日常使用● 建立檔案庫

● 檔案新增、更名 / 搬移、刪除

● 檢視與送交變更

● 忽略檔案

● 合併其他成員的變更■ 單一開發分支的合併■ 多個開發分支的合併

Page 16: 版本控制 - Mercurial

16

使用 Mercurial

■ 日常使用 (cont.)● 解決變更衝突

● 檢視紀錄

Page 17: 版本控制 - Mercurial

17

建立檔案庫

■ 建立檔案庫

● mkdir proj

● cd proj/

● hg init■ 在 proj/ 資料夾內會出現存放版本控制資訊的 .hg/ 資料夾

■ 從主檔案庫建立開發分支

● hg clone /home/dev/proj proj-my-workcopy

■ 建立檔案庫只需在專案開始時進行一次

Page 18: 版本控制 - Mercurial

18

檔案新增、更名 / 搬移、刪除

■ 檔案新增

● hg add FILE.name

■ 檔案更名或搬移

● hg mv OLDFILE.name NEWFILE.name

■ 檔案刪除

● hg rm FILE.name

■ 檔案補刪除

● hg rm -A FILE.name

Page 19: 版本控制 - Mercurial

19

檢視與送交變更

■ 檢視變更狀況

● hg st

■ 送交變更

● hg ci [FILE]■ 整個工作資料夾的所有變更都會被送入■ 要送交部份變更可指明檔名

.hg/

commit

Page 20: 版本控制 - Mercurial

20

忽略檔案

■ 可透過 .hgignore 檔指定要忽略的檔案

● 指定的檔案不會在 hg st 的輸出中顯示

syntax: glob

*.oa.outDoxydoxy-doc*~.DS_Store*.a

src/productsrc/executable

Page 21: 版本控制 - Mercurial

21

合併其他成員的變更

■ 從其他檔案庫取得變更

● hg incoming PATH_TO_OTHER_REPO

● hg pull PATH_TO_OTHER_REPO

■ 將變更反應到檔案上

● hg update

● hg merge

other-repo

.hg/ .hg/

pull update / merge

Page 22: 版本控制 - Mercurial

22

單一開發分支的合併

0:ab

1:f7

2:e1

0:ab

1:f7

2:e1clone 0:ab

1:f7

2:e1

0:ab

1:f7

2:e1

3:ca

編輯

Page 23: 版本控制 - Mercurial

23

單一開發分支的合併 (cont.)

0:ab

1:f7

2:e1

0:ab

1:f7

2:e1

3:ca

合併

0:ab

1:f7

2:e1

0:ab

1:f7

2:e1

3:ca

3:ca

pullupdate

Page 24: 版本控制 - Mercurial

24

多個開發分支的合併

0:ab

1:f7

2:e1

0:ab

1:f7

2:e1

0:ab

1:f7

2:e1

clone

clone

編輯

0:ab

1:f7

2:e1

3:ca

0:ab

1:f7

2:e1

3:b9

Page 25: 版本控制 - Mercurial

25

多個開發分支的合併 (cont.)

合併

0:ab

1:f7

2:e1

3:ca

0:ab

1:f7

2:e1

3:b9

4:ca

pull

0:ab

1:f7

2:e1

3:b9

4:ca

5:df

mergecommit

0:ab

1:f7

2:e1

3:ca

0:ab

1:f7

2:e1

3:b9

Page 26: 版本控制 - Mercurial

26

解決變更衝突

■ 變更衝突的起因● 要合併的變更與本地端的變更有所重疊

■ 解決變更衝突

● Mercurial 會將雙方的變更都留在檔案內

● 協調後手動修改

● 修改完成後將檔案標示為已解決衝突

■ 標示檔案為已解決衝突

● hg resolve -m FILE.name

Page 27: 版本控制 - Mercurial

27

檢視紀錄

■ 文字模式

● hg log [FILE]

■ 視窗模式

● hg view■ 系統上須安裝有 Tcl/Tk

■ 網頁模式

● hg serve -a 127.0.0.1 -p 8000■ 到瀏覽器上透過 http://127.0.0.1:8000/ 作為 URL 連線

■ 未給參數的話預設是啟動在所有介面上的 8000 埠▸ 有安全顧慮的話最好指定 -a 參數指定介面

■ 按下 CTRL+C 終止

Page 28: 版本控制 - Mercurial

28

檢視紀錄 (cont.)

Page 29: 版本控制 - Mercurial

29

檢視紀錄 (cont.)

Page 30: 版本控制 - Mercurial

30

小撇步

■ 推送變更到多臺主機

■ 送交部份變更

■ 合併兩個獨立的檔案庫

Page 31: 版本控制 - Mercurial

31

推送變更到多臺主機

■ 可以一定程度的作為佈署的手段

■ 增加 paths 設定到檔案庫的設定中

● %REPO%/.hg/hgrc

● hg push [PATH_NAME]

[paths]default = ssh://[email protected]/project-1dev01 = ssh://[email protected]/repo/project-1dev02 = ssh://[email protected]/repo/project-1pubserv = http://[email protected]/project/my-project-1

Page 32: 版本控制 - Mercurial

32

送交部份變更

■ 送交部份變更

● hg record [FILE]■ 用在一次的程式碼變更內含兩個以上修改主題之場合

▸ 一次修了好幾個 bug▸ 一次加了好幾個 feature▸ 順便做了 re-factor▸ 順便更新了文件

■ 相當於 hg commit■ 會提示選擇要加入的變更

■ 需要在設定檔中啓動這個功能

[extensions]record =

Page 33: 版本控制 - Mercurial

33

送交部份變更 (cont.)

$ hg recorddiff --git a/hello.txt b/hello.txt2 hunks, 5 lines changed

examine changes to 'hello.txt'? [Ynesfdaq?] y

@@ -4,11 +4,11 @@ So she was considering in her own mind (as well as she could, for the hot day-made her feel very sleepy and stupid), whether the pleasure of making a-daisy-chain would be worth the trouble of getting up and picking the daisies,+made her feel very sleepy and stupid), whether the pleasure of making a daisy-+chain would be worth the trouble of getting up and picking the daisies, when suddenly a White Rabbit with pink eyes ran close by her.

record change 1/3 to 'hello.txt'? [Ynesfdaq?] y

@@ -9,12 +9,12 @@ she ought to have wondered at this, but at the time it all seemed quite-natural); but when the Rabbit actually TOOK A WATCH OUT OF ITS+natural); but when the Rabbit actually TOOK A WATCH OUT OF ITS WAISTCOAT-...

record change 2/3 to 'hello.txt'? [Ynesfdaq?] s

diff --git a/world.txt b/world.txt1 hunks, 1 lines changed

examine changes to 'world.txt'? [Ynesfdaq?] y

@@ -1,2 +1,2 @@-An apple a day keeps the doctors away.+An Apple A Day Keeps the Doctors Away.

record change 3/3 to 'world.txt'? [Ynesfdaq?] y

Page 34: 版本控制 - Mercurial

34

送交部份變更 (cont.)

■ 動作選項

● Y: 紀錄這個變更

● N: 略過這個變更

● E: 手動編輯這個變更

● S: 目前檢視的檔案中,略過這個以下 ( 含 ) 的變更

● F: 目前檢視的檔案中,紀錄這個以下 ( 含 ) 的變更

● D: 完成,後面的變更與檔案都略過

● A: 紀錄後面所有檔案的所有變更

● Q: 離開,不紀錄任何變更

● ?: 顯示說明訊息

Page 35: 版本控制 - Mercurial

35

合併兩個獨立的檔案庫

■ 偶而會發生● 合併兩個原本不相干的專案

● 在新的檔案庫開相對獨立子功能之更新版本■ 不建議這麼做

■ 建議直接 clone原本的來進行進一步開發

■ 進行合併前建議先調整兩個檔案庫內的檔案配置

● 降低合併 (merge) 時發生衝突 (conflict) 的機會

+ = + =

Page 36: 版本控制 - Mercurial

36

合併兩個獨立的檔案庫 (cont.)

■ 直接使用 hg pull 取得變更集時會被拒絕

● “abort: repository is unrelated”

■ 要加上強制拉取選項

● hg pull -f [TARGET_REPO]

● 後續動作跟一般的 pull-merge 流程相同

Page 37: 版本控制 - Mercurial

37

其他細節

■ 個人識別名稱

■ 共用帳號

Page 38: 版本控制 - Mercurial

38

個人識別名稱

■ 目前常見的有兩種● 以個人名義為主

■ My Name <[email protected]>

● 附加公司簡稱■ My Name, CORP. <[email protected]>

■ 在開放專案中會看到第一種

● 假設合作的人都會看 E-Mail位址來辨識所屬組織

● 個人色彩鮮明

■ 公司專案就比較不一定

■ 總之,選一個寫法然後就避免變動

Page 39: 版本控制 - Mercurial

39

共用帳號

■ 在開發中有時多個開發者得共用主機帳號● 特別是測試或驗證主機

■ 一般來說建議避免把個人化設定放到共用帳號裡● 常見的有環境變數、編輯器設定、版控設定等

■ ~/.bashrc, ~/.profile, ~/.vimrc, ~/.hgrc … etc.

● 避免讓變數非預設值而影響其他人甚至影響程式運作■ 有些設定要怎麼設基本上是信仰的問題

■ 透過建立個人的環境變數檔來兼顧

● 利用 alias 改變預設指令的傳遞參數

● 大部份程式都有參數可以改變預設讀取的設定檔

Page 40: 版本控制 - Mercurial

40

共用帳號 (cont.)

■ 建立 ~/.myrc-myaccount指令稿

● 其他設定檔可建立 ~/.myrc-myaccount-data/ 資料夾來存放

■ 登入時匯入即可完成執行時期客制化

● . ~/.myrc-myaccount

● source ~/.myrc-myaccount

alias hg=”hg –config 'ui.username=Vincent CY Liao, MyCO <[email protected]>'”alias vi='vim -u ~/.myrc-vincentl-data/vimrc'

export EDITOR=/usr/bin/vimexport PATH=/opt/mercurial-2.4/bin:$PATH

Page 41: 版本控制 - Mercurial

41

Thank you !