Skip to content

Commit f913337

Browse files
committed
Test if it is possible to add files to the index of a bare repo.
Adding using a path should still require a non-bare repository.
1 parent 660bdca commit f913337

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

git/test/test_index.py

+34
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020
import time
2121
from stat import *
2222

23+
from StringIO import StringIO
24+
from gitdb.base import IStream
25+
from git.objects import Blob
26+
from git.index.typ import BaseIndexEntry
27+
28+
2329
class TestIndex(TestBase):
2430

2531
def __init__(self, *args):
@@ -646,6 +652,34 @@ def make_paths():
646652
for absfile in absfiles:
647653
assert os.path.isfile(absfile)
648654

655+
@with_rw_repo('HEAD', bare=True)
656+
def test_index_bare_add(self, rw_bare_repo):
657+
# Something is wrong after cloning to a bare repo, reading the
658+
# property rw_bare_repo.working_tree_dir will return '/tmp'
659+
# instead of throwing the Exception we are expecting. This is
660+
# a quick hack to make this test fail when expected.
661+
rw_bare_repo._working_tree_dir = None
662+
contents = 'This is a StringIO file'
663+
filesize = len(contents)
664+
fileobj = StringIO(contents)
665+
filename = 'my-imaginary-file'
666+
istream = rw_bare_repo.odb.store(
667+
IStream(Blob.type, filesize, fileobj))
668+
entry = BaseIndexEntry((100644, istream.binsha, 0, filename))
669+
try:
670+
rw_bare_repo.index.add([entry])
671+
except AssertionError, e:
672+
self.fail("Adding to the index of a bare repo is not allowed.")
673+
674+
# Adding using a path should still require a non-bare repository.
675+
asserted = False
676+
path = os.path.join('git', 'test', 'test_index.py')
677+
try:
678+
rw_bare_repo.index.add([path])
679+
except Exception, e:
680+
asserted = "does not have a working tree" in e.message
681+
assert asserted, "Adding using a filename is not correctly asserted."
682+
649683

650684
@with_rw_repo('HEAD')
651685
def test_compare_write_tree(self, rw_repo):

0 commit comments

Comments
 (0)