0

When I'm trying to add files to bare repo:

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 <...>

I only understood that I can add files only by Repo.index.add.

Is using bare repo with git-python module even possible? Or I need to use subprocess.call with git --work-tree=... --git-dir=... add ?

mikroskeem
  • 37
  • 1
  • 9

1 Answers1

2

You can not add files into bare repositories. They are for sharing, not for working. You should clone bare repository to work with it. There is a nice post about it: www.saintsjd.com/2011/01/what-is-a-bare-git-repository/

UPDATE (16.06.2016)

Code sample as requested:

    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)
galarius
  • 108
  • 9
  • Could you, please explain with some code how to do it galarius? maybe in a minimal python script using "import git" would be nice. The post is interesting but does not answer the question. – alemol Jun 09 '16 at 17:49
  • 1
    According to the GitPython documentation, "Our index implementation allows to stream date into the index, which is useful for bare repositories that do not have a working tree." But I can't find any documentation on how to do this. My use case is slightly different; I don't want to stage files in a bare repository, but rather stage a version of a file that is different to the version in my working tree. – Tom Feb 22 '18 at 10:08