2016-03-14 104 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