2013-12-18 25 views
1

我目前在大型目錄樹中建立了大量的git項目。結構看起來是這樣的如何從現有的目錄結構中生成git超級項目

projects 
projects/stuff -> this is a git repo 
projects/frontend/frontendone -> this is also a git repo 
projects/frontend/frontendtwo -> this is also a git repo 
projects/something -> this is a git repo 
... 

這整個樹包含了很多的git回購協議(如50-100)的,他們可以在樹內的任何地方,他們可以從不同的服務器,不同的configs。

我想在projects目錄內創建一個新的超級項目,該目錄將所有存儲庫作爲子模塊存在。

我可以在git子模塊上找到的大多數例子都是從沒有git存儲庫開始,然後用git submodule add一個一個地重新添加它們,但是我已經很好地設置了我的目錄結構,逐一做所有這些似乎是太多的努力。

所以基本上我只想要projects目錄成爲一個超級項目,並保持一切完好,因爲它們已經爲我設置得很好。

什麼是創建超級項目最簡單的方法?

+0

我不認爲你沒有'git submodule add'就能相處。不過,你可以用'git submodule foreach'切割一個角落。然而,我發現在這個問題中缺少的是你想保留子模塊當前所在的位置,還是希望將它們移動到新的位置。 – raina77ow

+0

@ raina77ow我想讓他們在那裏(如果可能),因爲這似乎是最簡單的(對我來說)。雖然如果不可能的話,重新創建外部整體結構然後將其移回這裏也是一種選擇,但是我希望能夠在不需要太多努力的情況下重新創建結構。 – SztupY

回答

1

我需要相同的答案,所以我爲Bash編寫了一個腳本。如果你在不同的平臺上,希望這可以說明你需要做什麼。乾杯!

#!/bin/bash 
# add all the git folders below current folder as git submodules. 
# NOTE: does not recursively nest submodules, but falls back to 
# git submodule add's behavior of failing those. 
# Workaround is to run this script in each affected directory 
# from the bottom up. 
# For example: 
# a/.git 
# b/.git 
# b/b1/.git 
# b/b2/.git 
# c/.git 
# 
# run this script twice - first in b (adds b1 & b2 as submodules to b), 
# then in root (adds a, b, c to root) 
# 

# if any options specified, treat as a test run and display only 
if [ -z $1 ]; then 
    GITSMADD="git submodule add -f" 
    if [ ! -d ./.git ]; then 
     git init 
    fi 
else 
    GITSMADD="echo git submodule add -f" 
    echo running in DISPLAY mode 
fi 

find . -name '.git' -type d -exec dirname {} \; | sort | while read LINE 
do 
    if [ "$LINE" != "." ]; then 
     pushd $LINE > /dev/null 
     ORIGIN=$(git remote -v | grep fetch | head -1 | awk '{print $1}') 
     URL=$(git remote -v | grep fetch | head -1 | awk '{print $2}') 
     popd > /dev/null 
     if [ -z $ORIGIN ]; then 
      echo "Adding local folder $LINE as submodule." 
      $GITSMADD "$LINE" 
     else 
      echo "Adding remote $URL as submodule in folder $LINE" 
      $GITSMADD "$URL" "$LINE" 
     fi 

    fi 
done