清出一個乾淨的 Git Repository

自動化機制中,要確保可以拿到最新的 Git Repository 最保險的方法是 rm -rf 掉再重新 clone 一份,但對於稍微大一點的 repository 速度就太慢了,所以得想辦法保留 repository 加快速度。

目標是想要處理 submodule 的變更,並且仍然可以處理 force push 時的災難,避免需要人工介入... 這在建各種自動化機制時常常會用到,像是用 Fortify SCA 白箱掃描,或是 CI & CD 機制上。

BRANCH=master
git fetch --force
git reset --hard origin/"${BRANCH}"
git submodule sync --recursive
git submodule update --init --force
git clean --force --force -d -x
git checkout "${BRANCH}" --force

用了很多 --force 主要是要處理 force push 後的收拾 :o

其實如果想要處理的更好的話,可以在發生錯誤時改走 mv (保留屍體) + 重新 clone 的機制,並且發警報出來讓管理者事後研究,這樣發生問題當下還是可以先提供服務。

不過這個想法還只在腦袋裡還沒寫... XD

Git 2.8 的平行下載 submodule 加速

Git 推出新版的時候,幾家 Git Hosting 都會撰文寫一些重要的進展,像是 GitHub 這次的內容:「Git 2.8 has been released」。

GitHub 這次說明平行下載的範例直接清楚表示出來功能:

git fetch --recurse-submodules --jobs=4

用 Google 找了一個 .gitmodules 裡面有很多筆的 repository 測了一下,的確是快了不少...

Git 2.5 的 worktree

GitHubGit 2.5 寫的介紹:「Git 2.5, including multiple worktrees and triangular workflows」。

Please note that the worktree feature is experimental. It may have some bugs, and its interface may change based on user feedback. It's not recommended to use git worktree with a repository that contains submodules.

另外效能上也有改善:

Git 2.5 includes performance improvements aimed at large working trees and working trees stored on networked filesystems (e.g., NFS):

從 pathogen.vim 換成 Vundle...

pathogen.vimgit submodule 用到有點煩了... 改用 Vundle 避開 pathogen.vim 使用 git submodule 帶來的維護問題...

Vundle 的安裝方法很簡單 (網路上其實也有不少說明了),先 clone 或是 submodule add 下來:

$ git clone https://github.com/gmarik/vundle.git ~/.vim/bundle/vundle
$ git submodule add https://github.com/gmarik/vundle.git .vim/bundle/vundle

然後在 .vimrc 內加入:

filetype off
set rtp+=~/.vim/bundle/vundle/
call vundle#rc()
Bundle 'gmarik/vundle'
Bundle '其他的 github 位置...'
filetype plugin indent on

接下來開 vim,跑 :BundleInstall 他就會自己安裝其他的套件了。要增加套件就加到 .vimrc 內再跑一次 :BundleInstall 就可以了。