2013-02-14 26 views
2

我讀過how to remove a git submodule myself刪除git submodules - 如何自動刪除拉?

# Delete the relevant section from the .gitmodules file. 
git config -f .gitmodules --remove-section submodule.$submodulepath 
# Delete the relevant section from .git/config 
git config -f .git/config --remove-section submodule.$submodulepath 
git rm --cached path_to_submodule # no trailing slash 
git commit 
rm -rf path_to_submodule 

我能做到這一點。但是當別人做了git pull我們如何確保子模塊從系統中移除?據我所知,.gitmodules更改與拉,但沒有其他更多。所以拉的人將仍然需要運行

git config -f .git/config --remove-section submodule.$submodulepath 
rm -rf path_to_submodule 

這是正確的嗎?在一個小型開發團隊中,我想你可以告訴每個人都運行這些命令,但這並不理想。

是否有一些神奇的命令可以自動執行此操作?特別是我希望在部署腳本中使用一些標準的自動化方法。關於我的頭頂,我不確定劇本怎麼會知道有一個比以前少的子模塊。發生在我(不是特別有吸引力)的選項有:

  • .gitmodules做的diff之前和拉
  • 刪除所有子模塊,然後運行git submodule update --init每一個部署後。
  • 該子模塊確實在拉動後最終成爲未跟蹤文件,所以一個可行的選項是在拉動後刪除包含.git子目錄的所有未跟蹤目錄,但是您可能會刪除想要保留的東西那樣。

任何更好的選項讚賞。

+0

應該不會命令你執行rm'卸下拉子模型的'git的? – 2013-02-14 15:01:44

+0

@DanielHilgarth:最近我們做了它並沒有被刪除,但也許我沒有做到上面的確切步驟。也許我應該測試一下。 – 2013-02-14 15:09:54

+0

@DanielHilgarth:剛剛檢查過並且'git rm'實際上並沒有刪除子模塊文件,因此在第一組指令結束時會刪除'rm -rf path_to_submodule'。克隆副本還需要手動刪除步驟。這對你來說有什麼不同?如果是這樣,你使用的是什麼版本的git? – 2013-02-14 15:17:42

回答

1

爲了將代碼部署到具有git clean後拉(例如通過post-checkout掛鉤)運行的服務器是一個安全選項。

至於更新開發人員的回購,其中運行git clean是危險的,我知道除了手工修剪之外別無他法。

但是,以下(未經測試)腳本會自動完成,將其命名爲git prune-submodules。它涉及到git pull之後,刪除的子模塊仍然在.git/config中列出,所以你可以找出哪一個要歸檔。

我是shell腳本的新手,所以請仔細檢查。

#!/bin/sh 
#get the list of submodules specified in .git/config 
#http://stackoverflow.com/questions/1260748/how-do-i-remove-a-git-submodule#comment9411108_7646931 
submodules_cfg=`git config -f .git/config -l | cut -d'=' -f1 | grep "submodule.$MODPATH" | sed 's/^submodule\.//' | sed 's/\.url$//'` 

#get the list of current submodules 
submodules=`git submodule | cut -b 2- | cut -d' ' -f 2` 

#archive submodule if listed in .git/config but doesn't exist anymore 
for submodule_cfg in $submodules_cfg; do 
    submodule_cfg_exists=0 
    for submodule in $submodules; do 
     if [ "$submodule" == "$submodule_cfg" ]; then 
       submodule_cfg_exists=1 
     fi 
    done 

    if ["$submodule_cfg_exists" == 0]; then 
     mkdir -p archived-submodules 
     mv $submodule_cfg archived-submodules/ 
     git config -f .git/config --remove-section submodule.$submodule_cfg 
    fi 
done 
+0

看起來不錯 - 我還沒有機會測試它,但如果它需要任何編輯,我會回饋他們。 – 2013-02-25 11:56:03