def test_ls_tree_children_true(): tmp = maketemp() commands.init_bare(tmp) commands.fast_import( repo=tmp, commits=[ dict( message='one', committer='John Doe <*****@*****.**>', commit_time='1216235872 +0300', files=[ dict( path='quux/foo', content='FOO', ), dict( path='bar', content='BAR', mode='100755', ), ], ), ], ) g = commands.ls_tree(repo=tmp, path='quux', children=True) eq( g.next(), dict( type='blob', mode='100644', object='d96c7efbfec2814ae0301ad054dc8d9fc416c9b5', path='quux/foo', ), ) assert_raises(StopIteration, g.next)
def test_no_initial_commit(): tmp = maketemp() commands.init_bare(tmp) r = repo.Repository(path=tmp) with r.transaction() as p: eq(list(p), []) with p.child('bar').open('w') as f: f.write('THUD') # transaction committed, now the content has changed got = commands.ls_tree( repo=tmp, path='bar', ) got = list(got) eq(len(got), 1) got = got[0] object = got['object'] content = commands.cat_file( repo=tmp, object=object, ) eq(content, 'THUD')
def test_ls_tree_children_false(): tmp = maketemp() commands.init_bare(tmp) commands.fast_import( repo=tmp, commits=[ dict( message='one', committer='John Doe <*****@*****.**>', commit_time='1216235872 +0300', files=[ dict( path='quux/foo', content='FOO', ), dict( path='bar', content='BAR', mode='100755', ), ], ), ], ) g = commands.ls_tree(repo=tmp, path='quux', children=False) eq( g.next(), dict( mode='040000', type='tree', object='d513b699a47153aad2f0cb7ea2cb9fde8c177428', path='quux', ), ) assert_raises(StopIteration, g.next)
def isdir(self): for data in commands.ls_tree( repo=self.repo, path=self.path, treeish=self.rev, children=True, ): return True # i have no children, therefore i am not a directory return False
def exists(self): if self.path == '': # root directory always exists return True for data in commands.ls_tree( repo=self.repo, path=self.path, treeish=self.rev, children=False, ): return True return False
def islink(self): if self.path == '': # root directory is never a link return False for data in commands.ls_tree( repo=self.repo, path=self.path, treeish=self.rev, children=False, ): return data['mode'] == '120000' # didn't match anything -> don't even exist return False
def test_write_tree(): tmp = maketemp() repo = os.path.join(tmp, 'repo') os.mkdir(repo) commands.init_bare(repo) index = os.path.join(tmp, 'index') sha_foo = commands.write_object(repo=repo, content='FOO') sha_bar = commands.write_object(repo=repo, content='BAR') commands.update_index( repo=repo, index=index, files=[ dict( object=sha_foo, path='quux/foo', ), dict( object=sha_bar, path='bar', ), ], ) tree = commands.write_tree( repo=repo, index=index, ) eq(tree, 'e8abc9dd8c483a4e27598521674ae7a357d2c825') g = commands.ls_tree(repo=repo, treeish=tree) eq( g.next(), dict( mode='100644', type='blob', object='add8373108657cb230a5379a6fcdaab73f330642', path='bar', ), ) eq( g.next(), dict( mode='040000', type='tree', object='d513b699a47153aad2f0cb7ea2cb9fde8c177428', path='quux', ), ) assert_raises(StopIteration, g.next)
def __iter__(self): for data in commands.ls_tree( repo=self.repo, path=self.path, treeish=self.rev, children=True, ): if self.path == '': prefix = '' else: prefix = self.path + '/' assert data['path'][:len(prefix)] == prefix relative = data['path'][len(prefix):] if relative == '.gitfs-placeholder': # hide the magic continue yield self.child(relative)
def size(self): if self.path == '': object = commands.rev_parse( repo=self.repo, rev='%s^{tree}' % self.rev, ) for data in commands.ls_tree( repo=self.repo, path=self.path, treeish=self.rev, children=False, ): object = data['object'] break return commands.get_object_size( repo=self.repo, object=object, )
def test_commit_race(): tmp = maketemp() commands.init_bare(tmp) commands.fast_import( repo=tmp, commits=[ dict( message='one', committer='John Doe <*****@*****.**>', commit_time='1216235872 +0300', files=[ dict( path='quux/foo', content='FOO', ), dict( path='bar', content='orig\n', mode='100755', ), ], ), ], ) r = repo.Repository(path=tmp) racecount = 0 tries = 1 MAX_RETRIES = 3 success = False while tries <= MAX_RETRIES: try: with r.transaction() as p: if racecount < 2: racecount += 1 r2 = repo.Repository(path=tmp) with r2.transaction() as p2: with p2.child('bar').open('a') as f: f.write('racer %d\n' % racecount) with p.child('bar').open('a') as f: f.write('loser %d\n' % tries) except repo.TransactionRaceLostError: tries += 1 else: success = True break assert success # transaction committed, now the content has changed got = commands.ls_tree( repo=tmp, path='bar', ) got = list(got) eq(len(got), 1) got = got[0] object = got['object'] content = commands.cat_file( repo=tmp, object=object, ) eq(content, 'orig\nracer 1\nracer 2\nloser 3\n')
def test_simple(): tmp = maketemp() commands.init_bare(tmp) commands.fast_import( repo=tmp, commits=[ dict( message='one', committer='John Doe <*****@*****.**>', commit_time='1216235872 +0300', files=[ dict( path='quux/foo', content='FOO', ), dict( path='bar', content='BAR', mode='100755', ), ], ), ], ) r = repo.Repository(path=tmp) with r.transaction() as p: assert type(p) == indexfs.IndexFS eq(p.path, '') eq(list(p), [p.child('bar'), p.child('quux')]) with p.child('bar').open('w') as f: f.write('THUD') # the committed tree has not changed yet, because transaction # is still open got = commands.ls_tree( repo=tmp, path='bar', ) got = list(got) eq(len(got), 1) got = got[0] object = got['object'] content = commands.cat_file( repo=tmp, object=object, ) eq(content, 'BAR') # transaction committed, now the content has changed got = commands.ls_tree( repo=tmp, path='bar', ) got = list(got) eq(len(got), 1) got = got[0] object = got['object'] content = commands.cat_file( repo=tmp, object=object, ) eq(content, 'THUD')