2016-03-14 101 views
0

使用裸回購當我嘗試添加文件到裸回購:用git-蟒蛇

import git 
r = git.Repo("./bare-repo") 
r.working_dir("/tmp/f") 
print(r.bare) # True 
r.index.add(["/tmp/f/foo"]) # Exception, can't use bare repo <...> 

我只明白,我只能通過Repo.index.add添加文件。

使用裸回購與git-python模塊甚至可能嗎?或者我需要使用subprocess.callgit --work-tree=... --git-dir=... add

回答

0

您不能將文件添加到裸存儲庫。他們是爲了分享,而不是爲了工作。您應該克隆裸存儲庫以使用它。有一個很好的職位看:www.saintsjd.com/2011/01/what-is-a-bare-git-repository/

更新(2016年6月16日)

代碼示例的要求:

import git 
    import os, shutil 
    test_folder = "temp_folder" 
    # This is your bare repository 
    bare_repo_folder = os.path.join(test_folder, "bare-repo") 
    repo = git.Repo.init(bare_repo_folder, bare=True) 
    assert repo.bare 
    del repo 

    # This is non-bare repository where you can make your commits 
    non_bare_repo_folder = os.path.join(test_folder, "non-bare-repo") 
    # Clone bare repo into non-bare 
    cloned_repo = git.Repo.clone_from(bare_repo_folder, non_bare_repo_folder) 
    assert not cloned_repo.bare 

    # Make changes (e.g. create .gitignore file) 
    tmp_file = os.path.join(non_bare_repo_folder, ".gitignore") 
    with open(tmp_file, 'w') as f: 
     f.write("*.pyc") 

    # Run git regular operations (I use cmd commands, but you could use wrappers from git module) 
    cmd = cloned_repo.git 
    cmd.add(all=True) 
    cmd.commit(m=".gitignore was added") 

    # Push changes to bare repo 
    cmd.push("origin", "master", u=True) 

    del cloned_repo # Close Repo object and cmd associated with it 
    # Remove non-bare cloned repo 
    shutil.rmtree(non_bare_repo_folder) 
+0

請問您能解釋一下如何做galarius嗎?也許在一個最小的Python腳本使用「導入git」會很好。該帖子很有趣,但沒有回答這個問題。 – alemol

+0

我已經用代碼示例更新了我的答案。 – galarius

+0

根據GitPython文檔,「我們的索引實現允許將日期流入索引,這對於沒有工作樹的裸倉庫非常有用。」但我找不到有關如何執行此操作的任何文檔。我的用例稍有不同;我不想將文件放在裸倉庫中,而是放置與我的工作樹中的版本不同的文件版本。 – Tom