2011-10-24 30 views
2

我有一個包含多個分支的git存儲庫。該存儲庫具有分支之間相同的makefile。我想創建一個可以定期運行的bash腳本,該腳本從遠程存儲庫中提取所有分支,然後爲每個已更改或已添加的分支運行make。然後將創建的可執行文件移動到服務器上的另一個位置,然後在下一個要處理的分支上開始執行make clean。如何在每個git分支上運行makefile

我會跑會

make 
cp executable /some/other/directory/branch_name 
make clean 

指令的順序,這甚至有可能做一個shell腳本,如果是我怎麼可能去實現這一點。如果一個shell腳本不適合這個任務,我還有什麼可以去實現相同的結果?

+0

附註:如果你不是想測試你的'使clean'規則,'混帳clean'的完整性(可能帶有選項'-x -d -f';請參閱聯機幫助頁)可用於清除未跟蹤的所有內容,以確保您的構建真正處於清理狀態。 – Cascabel

+0

感謝你的這一點,我認爲git clean可能在這裏有很大的意義。 –

回答

3

我會做這樣的事情:

# fetch updates, but don't merge yet 
git fetch # origin is default 

# list all branches 
git for-each-ref --format='%(refname:short)' refs/heads | 
while read branch; do 
    # if the branch is not the same as the remote branch... 
    if [ "$(git rev-parse $branch)" != "$(git rev-parse origin/$branch)" ]; then 
     # only continue to the next step on successful results 
     git checkout $branch && 
     git pull && 
     (make && 
     cp executable /somewhere/else; 
     make clean) 
     # except always make clean as long as we tried to make 
     # 
     # you might also consider a hard reset and clean, to guarantee things 
     # are safe for the next checkout 
    fi 
done 

約先取出的好處是,你不必不必要的檢查分支,沒有變化;如果你的回購很大,這可以節省時間。

顯然有處理錯誤的選擇;我只是用最簡單的東西去做,但仍然有意義。對於更復雜的處理,你可能反而想做的形式if make; then ...; else ...; fi,例如事情:

# die hard on unexpected checkout/pull failures 
if ! (git checkout $branch && git pull); then 
    exit 1 
fi 
if make; then 
    cp executable /somewhere/else 
else 
    # report a build failure? 
fi 
# clean up no matter what 
git clean -xdf 
git reset --hard 
1

那麼,git pull確實會生成輸出結果,可能是grep'd來確定哪些refs已更新。之後,循環播放grepfor ... in ...循環之間的匹配是一件簡單的事情。

相關問題