»
S
I
D
E
B
A
R
«
Git branch in bash prompt
Dec 13th, 2008 by kanru

之前提過 zsh 有支援在 prompt 上顯示多種 VCS 資訊的功能,不知道 bash 上是否有人寫好同樣支援這麼多種 VCS 的 library,不過目前 git 附帶的 bash completion script 中有提供自訂 PS1 的函式。

只要呼叫 __git_ps1 即可得到目前的 git branch 的格式化輸出,可以自訂 prompt 如下

GITPS1='$(__git_ps1 ":%s ")'
export PS1="\w ${GITPS1}\$ "

就會顯示如

/usr/src/awesome :next $

這樣的 prompt

為什麼 Git 比 X 好?
Dec 6th, 2008 by kanru

當初一看到這個標題心裡想的是 Git 怎麼拿來跟 X 比?原來這個 X 是指其他 SCM…

http://whygitisbetterthanx.com/

很多人都注意到了,但是現在有

http://zh-tw.whygitisbetterthanx.com/

囉,小小替 Git 宣傳一下 :p

使用 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

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