2013-07-22 65 views
1

我有按日期保存在標籤目錄發佈的舊的遺留代碼庫...如何將一系列目錄轉換爲Git提交?

... 
../20110819/ 
../20120105/ 
... 

我們搬到以前的git幾個月,我拍了幾張提交從舊版本作爲起點,並有在頂部發展。

我有兩個問題,其中第一個更重要: 如何將此係列目錄轉換爲git提交?優選地,這將在腳本中自動化。

其次,我怎樣才能將我已經做出的提交添加到頂端?

回答

0

這個擴展了賈斯汀的答案。之後的rebase程序可能會很複雜,所以我沒有包含它。它相當於git rebase -i newroot master,但就合併/添加/刪除而言存在細微之處。例如,我必須清理已刪除的文件(這些文件不會被拾取,因爲我們只是添加文件)。

如果你是,因爲我是,希望這些傳統進口在當前回購協議提交「之前」,你會想要做這樣的事情git checkout --orphan newroot

#!/usr/bin/python 

import os 
import sys 
import datetime as dt 

LEGACY_PATH = '/path/to/legacy' 
REPO_PATH = '/path/to/repo' 
AUTHOR_NAME = 'Your Author <[email protected]>' 

def main(): 
    os.chdir(REPO_PATH) 
    #We assume you are on the branch where you want to import 
    #Otherwise the following line will do 
    #os.system('git checkout --orphan newroot') 
    subdirs = sorted(os.listdir(LEGACY_PATH)) 
    print subdirs 

    for d in subdirs: 
     fullpath = os.path.join(LEGACY_PATH, d) 
     legacy_date = dt.datetime.strptime(d, '%Y%m%d').isoformat() 

     cmd = 'git --git-dir=%s/.git --work-tree=%s add .' \ 
       % (REPO_PATH, fullpath) 
     print '\n', cmd 
     os.system(cmd) 

     os.chdir(REPO_PATH) 
     cmd = 'git commit -m "Import changes from legacy version %s" \ 
       --date=%s \ 
       --author="%s"' \ 
       % (d, legacy_date, AUTHOR_NAME) 
     print '\n', cmd 
     os.system(cmd) 

if __name__ == "__main__": 
    main() 
1

僞代碼:

git init newrepo 
for each directory in the proper order (looks like you can just do a numeric sort there, unless there are exceptions to the naming convention) 
do 
    remove everything in newrepo except for .git 
    copy entire contents of tree into newrepo, without the <date>/ prefix 
    git add -A . 
    git commit -m "some suitable message mentioning the date/version" 
done 

您可能能夠跳過與git --git-dir=... --work-tree=...選項的正確使用複製,但我通常只是做了上述類似的情況(當然,在情況下,我已經做到了,所有的「版本」都是從一系列的檔案中解開,而不是生活在一系列的目錄中)。

1

我沒有明確地測試過這個,但它應該可以工作。製作你的git repo的副本並對其進行測試,以便在出現問題時不會丟失任何重要內容。

#!/bin/bash 

LEGACY_PATH=/path/to/legacy/versions 
REPO_PATH=/path/to/git/repo 

cd ${REPO_PATH} 
git checkout -b import-legacy 
for V in $(ls -v ${LEGACY_PATH}/*); do 
    cd ${V} 
    git --git-dir=${REPO_PATH}/.git --work-tree=${REPO_PATH} add -A . 
    git commit -m "Import changes from legacy version ${V}" 
done 
git rebase --interactive master 
(reorder your changes, if needed, on top of the last legacy import) 
git checkout master 
+0

這就是我要找的。我會嘗試一下並讓你知道。 –

+0

這並不完全正確(將編輯錯誤),並且錯過了我希望提交給正確日期的一些功能。我將發佈一個我熟悉的Python版本。 –

相關問題