2014-09-26 132 views
0

我使用mercurial來管理我的源代碼。 今天我有12個不同的存儲庫,每個存儲庫有6個分支。 每個存儲庫都包含一個maven項目。 我想創建一個父項目並將我的所有項目聲明爲一個模塊並將所有項目合併到一個存儲庫中。 爲了視覺,今天我有: -Repo1/PROJECT1 -Repo2 /項目2 ... -Repo12/project12Hg:將多個存儲庫合併爲一個存儲庫

而且我想: - globalRepo與 - 模塊1 - 模塊2 - .. - module12

保持分支和歷史(如果可能)。

這樣做的最佳做法是什麼? (Subrepo,合併...)

Thx!

回答

1

這裏是我的解決方案的列表中複雜的順序,從最簡單的。

忘記歷史

最簡單的。將每個項目的默認分支的當前狀態置於新項目結構中。然後開始一個新的回購。

我們沒有被告知如何活躍的分支,但如果他們是積極的,你可以爲每個分支做相同的:

hg up null  # not strictly necessary, but may avoid the impression that a branch started now. 
hg branch <name> 
<populate directory structure with the current state of a branch from each project> 
hg commit 

你仍然將有機會獲得歷史在原有項目的回購協議。你不能輕易做到的是將新的變更集與舊的歷史記錄進行比較。顯然,這種需求會隨着時間而減少,但對您而言可能很重要。

合併

通過合併回購保留歷史是可能的。首先,每個項目回購必須有所有它的文件移動到子目錄,以反映全球回購的預期結構:

for <each project> 
    md projdir 
    hg rename * projdir 
    hg commit 

然後每個項目都可以被納入全球項目:

cd globalrepo 
hg init 
hg pull <first subproject> 
for <other subprojects> 
    hg pull -f <subproject>  # the -f is necessary because the projects are unrelated. 
    hg merge 
    hg commit 

這應該給你一個完整的歷史合併。

當比較具有「舊」歷史記錄的文件時,請記住(或使用別名)爲具有--git選項(即diff和至少導出)的命令使用--git選項。請記住,雖然歷史可能完整,但可能會讓人困惑,因爲每個項目的歷史都是連續的。

使用Subrepos

有可能加入使用沿着你的建議行subrepos回購。儘管如此,subrepos意圖是不太活躍獨立組件。在你的用例中,這些都不是真的。

在你的情況下,這些項目將不會是獨立的,因爲它們只用於新的全局回購的環境中,並且不與其他全球回購共享。

如果subrepos處於活動狀態,就像它們在您的用例中一樣,您必須警惕地記住提交subrepo和父repo。不記得這會導致刺激,因爲推動可能因多個頭部而中止。如果人A將變更集推送到subrepo而不推動父回購,則會發生這種情況;那麼人B更新到父回購(它不拉新的子回報變更集)的提示,並嘗試將他的提交推到相同的子報告上。如果你的團隊中的每個人都有能力處理汞,那麼對你來說可能會很好,但如果沒有,我建議不要在你的用例中使用subrepos。

如果有活躍的分支,該subrepos會留住他們,但全球回購不會看到他們,除非它也被分支:

cd globalrepo 
hg up null  # not strictly necessary, but may avoid the impression that a branch started now. 
hg branch <branch> 
for <each subrepo> 
    cd <project dir> 
    hg up <branch> 
cd globalrepo 
hg commit 

我希望你的想法(未經測試,該位從記憶裏)。

+0

最簡單的方法,在分支之間合併時沒有風險嗎? – 2014-09-29 19:34:16

+0

@MaxenceCramet你將需要歷史來合併分支,所以你不能用'忘記歷史'方法來做到這一點。 [你沒有說你的工作流程需要現有分支之間的後續合併;我認爲他們將永遠是獨立的,就像產品發佈一樣。] – 2014-09-29 23:07:12

1

可以將不相關的存儲庫拉到一起併合並它們。訣竅是在合併之前將文件重命名爲特定的目錄。

例如,以下腳本將在每個項目的根目錄下創建三個單獨的存儲庫,其中包含三個文件。然後將它們合併到一個存儲庫中,將每個項目的文件移至子目錄:

@REM Create three separate repositories 
hg init Repo1 
hg init Repo2 
hg init Repo3 
@REM Add and commit files to each of them. 
cd Repo1 
echo >file1 
echo >file2 
echo >file3 
hg ci -Am r1 
cd ..\Repo2 
echo >file4 
echo >file5 
echo >file6 
hg ci -Am r2 
cd ..\Repo3 
echo >file7 
echo >file8 
echo >file9 
hg ci -Am r3 
cd .. 
@REM Create a global repo. 
hg init globalRepo 
cd globalRepo 
@REM Pull in the first repo and move the files in it to a module1 subdirectory. 
@REM Force the pull since it is an unrelated repo and update to it. 
hg pull ..\Repo1 -f 
hg update tip 
md module1 
hg rename * module1 
hg ci -m "Rename Repo1 files to module1 directory." 
@REM Pull in the second repo and move the files in it to a module2 subdirectory. 
hg pull ..\Repo2 -f 
hg update tip 
md module2 
hg rename * module2 
hg ci -m "Rename Repo2 files to module2 directory." 
@REM Merge the two unrelated branches together. 
hg merge 
hg ci -m "Merge the two unrelated repositories together." 
@REM Pull in the third repo, rename, and merge. 
hg pull ..\Repo3 -f 
hg update tip 
md module3 
hg ren * module3 
hg ci -m "Rename Repo3 files to module3 directory." 
hg merge 
hg ci -m Merge 

結果如下所示。請注意,原始三個項目的整個歷史記錄都將以沒有父項的根節點開始。文件被移動到每個項目的最終目錄中,然後合併在一起。

TortoiseHg graph of the final result

+0

Why * NOT * oneliner with Convert extension instead of many md | ren? – 2014-09-27 23:46:08

+0

@MarkTolonen我合併的一點是一樣的。如果我首先看到這一點,我會錯過這一點。 – 2014-09-28 00:39:16

+0

@LazyBadger,爲什麼不用轉換擴展名顯示答案?我個人發現,在這種情況下,比使用convert更容易拉/重命名/合併。 – 2014-09-28 05:58:21

相關問題