»
S
I
D
E
B
A
R
«
使用 git-svn 整合 git 與 svn
Nov 23rd, 2008 by kanru

git 是目前最紅的分散式 VCS 之一,我很喜歡用 git 因為他預設就有許多方便的特性如:

  • git log/diff/help 都有彩色的 pager 可用,不用再自己 pipe 到 less
  • git log 的格式一目了然,清晰易懂
  • git bisect/format-patch/shortlog/stash… 等趁手工具一堆
  • 速度快

而 git 不僅僅是 git,他分成高階的命令如 pull/commit 等,還有低階的命令可以直接操作 ref/object,因此有一些 VCS 就是建立在 git 的檔案系統上,運作起來跟 git 截然不同。

git-svn 是一個可以把 svn 當 git 用的工具,就算專案還在用 svn 也可以享受到 git 的便利。以下簡介一下 git-svn 的使用流程。

首先創造一個實驗用的 svn repository:

bob % cd /tmp/test
bob % svnadmin create hello
bob % svn co file:///tmp/test/hello hello-svn
bob % cd hello-svn
bob % svn mkdir branches tags trunk
bob % svn ci -m'Initial dir setup'

現在我們有一個傳統的 svn repository 了,加點東西進去吧:

bob % cd trunk
bob % echo "printf("hello\n");" > hello.c
bob % svn add hello.c
bob % svn ci -m'My first program :D '
bob % cd ..
bob % svn cp trunk tags/v0.1
bob % svn ci -m'tag v0.1'

這時候 Bob 已經建好他的程式 v0.1 版,Alice 知道了他的計劃也想加入,於是她用 git-svn 來取出 repository

alice % git svn clone --stdlayout file:///tmp/test/hello hello-git
Initialized empty Git repository in /tmp/test/hello-git/.git/
r1 = 80a0fc18653e8bc7d3784567b1b7024b20fd8c11 (trunk)
    A   hello.c
r2 = 4a80acebb6a41c208f3427498225f829950ee39e (trunk)
Found possible branch point: file:///tmp/test/hello/trunk => file:///tmp/test/hello/tags/v0.1, 1
Found branch parent: (tags/v0.1) 80a0fc18653e8bc7d3784567b1b7024b20fd8c11
Following parent with do_switch
    A   hello.c
Successfully followed parent
r3 = 24ca9410e9ca78ffa3bffa1c9ef574068d128935 (tags/v0.1)
Checked out HEAD:
  file:///tmp/test/hello/tags/v0.1 r3
alice % git reset --hard remotes/trunk

因為是使用 stdlayout,所以 tag/branch 等會自動對應到 remote branch

alice % cd hello-git
alice % git branch -r
  tags/v0.1
  trunk

Alice 發現 Bob 原本的程式無法 compile,因此開始修改,改完之後用 git add -p 挑選要 commit 的部份,然後把剩下的部份 stash 起來,最後用 git svn dcommit 提交給 Bob 的 svn repository

alice % edit hello.c
alice % git add -p
alice % git commit -m'Fix everything'
alice % git stash
alice % git svn dcommit

隔了幾天,Alice 發現 Bob 有更新,因此開始做同步。同時,因為她有一個 local branch,所以在更新完 master 之後,還需要把 fancy branch 跟 master rebase。

alice % git svn fetch
    M   hello.c
r6 = c43134a98f43dc6507deaa7495bfcd2c906aaa30 (trunk)
alice % git svn rebase
First, rewinding head to replay your work on top of it...
Fast-forwarded master to refs/remotes/trunk.
alice % git checkout fancy
alice % git rebase master
First, rewinding head to replay your work on top of it...
Applying: Inital fancy output branch

以上就是使用 git-svn 時會用到的大部分指令,其餘的就跟操作一般 git 一樣。

各命令的詳細說明可以參考 git-svn(1)

Display VCS info in prompts
Oct 4th, 2008 by kanru

通常 VCS (Version Control System) 都會在工作目錄底下藏一些特殊目錄用來儲存資訊,如 .svn .git .hg 等,因此我們可以在 shell prompt 上動一些手腳,當我們進到 VCS 工作目錄時,自動顯示相關的訊息在 prompt 上。

而 zsh-beta 4.3.6-dev-0+20080921-1 內建了由 Frank Terbeck 開發的 vcs_info 子系統,可以自動偵測 bzr, cdv, CVS, darcs, git, hg, mtn, p4, svk, svn, tla 等多種 VCS,並設定相關的環境變數可以顯示在 prompt 上。

zsh prompt

我的 prompt 設定如下

    autoload -Uz vcs_info && vcs_info
    zstyle ':vcs_info:' actionformats \
    '%F{5}(%f%s%F{5})%F{3}-%F{5}[%F{2}%b%F{3}|%F{1}%a%F{5}]%f '
    zstyle ':vcs_info:' formats       \
    '%F{5}(%f%s%F{5})%F{3}-%F{5}[%F{2}%b%F{5}]%f '
    zstyle ':vcs_info:(sv[nk]|bzr):' branchformat '%b%F{1}:%F{3}%r'
    precmd () { vcs_info }
    EXITCODE="%(?..%?%1v )"
    JOBS="%(1j.%j .)"
    local BLUE="%{^[[1;34m%}"
    local RED="%{^[[1;31m%}"
    local GREEN="%{^[[1;32m%}"
    local CYAN="%{^[[1;36m%}"
    local YELLOW="%{^[[1;33m%}"
    local NO_COLOUR="%{^[[0m%}"
    PROMPT="${RED}${EXITCODE}${CYAN}${JOBS}${YELLOW}% ${RED}%n${BLUE}@%m:${GREEN}%40<...< %B%~%b%<<"'${vcs_info_msg_0_}'"
    ${YELLOW}%# ${NO_COLOUR}"

詳細的設定請參閱 zshcontrib(3) GATHERING INFORMATION FROM VERSION CONTROL SYSTEMS

AnkhSVN 結合 Visual Studio 與 Subversion
May 31st, 2008 by kanru

最近很多時間都是待在 Visual Studio 的環境下寫程式,而 VCS 則是使用 Windows 下很方便的 TortoiseSVN。對於已經習慣在編輯器裡面修改程式,告一段落後到檔案總管(或是 shell)把修改提交到伺服器上的我來說,使用 TortoiseSVN 算是很習慣了。但是對於第一次接觸 VCS 的同學,可能會忘記要提交修改、要寫 Changelog、開始新的修改前要先 update 等。

AnkhSVN 則提供了一個與 Visual Studio 整合的方案,1.x 版的 AnkhSVN 可以由 VS 內執行 VCS 的命令,而開發中的 2.x 則實做了 Source Control Provider 的介面,與 VS 的環境更緊密的結合,可以完全取代預設的 Sourcesafe 方案,使用上應該與商業化的 Visual SVN 類似,可以直接 checkout 專案、看 changelog、看 diff 等。

solution

測試的時候的小插曲:因為 2.x 是 “stable, but unfinished”,只有提供 nightly build,但是我抓下來的安裝檔都沒辦法在 VS 2005 上面正常執行,後來直接到 #ankhsvn 求救,也很快獲得回應,原來是因為不小心參考到 2008 才有的介面,修正之後,最新的 snapshot 已經可以正常使用 :)

不過要注意的是,2.x 用的是最新版的 svn,所以 working copy 的 format 是第 9 版,而最新的 TortoiseSVN 的 1.4.x release 用的是第 8 版,會沒辦法共同使用,要把 TortoiseSVN 更新到開發中的 1.5 才能讓 working copy 相容。如果沒辦法接受使用兩個開發中軟體的話,還是可以用最新的 release 版本,還是很好用的 :p

»  Substance: WordPress   »  Style: Ahren Ahimsa
© Copyright 2004-2009 Kan-Ru Chen