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_get_object_size(): tmp = maketemp() commands.init_bare(tmp) sha1 = commands.write_object(repo=tmp, content='FOO') eq(sha1, 'd96c7efbfec2814ae0301ad054dc8d9fc416c9b5') got = commands.get_object_size(repo=tmp, object=sha1) eq(got, 3)
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 test_update_ref_oldvalue_bad(): 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='foo', content='FOO', ), ], ), ], ) head = commands.rev_parse(repo=tmp, rev='HEAD') eq(head, 'e1b2f3253b18e7bdbd38db0cf295e6b3b608bb27') e = assert_raises( # TODO better exception for this RuntimeError, commands.update_ref, repo=tmp, ref='HEAD', newvalue=head, oldvalue='deadbeefdeadbeefdeadbeefdeadbeefdeadbeef', ) eq(str(e), 'git update-ref failed') got = commands.rev_parse(repo=tmp, rev='HEAD') eq(got, head)
def test_for_each_ref_sort(): 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='foo', content='FOO', ), ], ), ], ) commands.update_ref( repo=tmp, ref='refs/heads/quux', newvalue='HEAD', oldvalue=40*'0', ) got = commands.for_each_ref( repo=tmp, sort='-refname', ) got = iter(got) eq(got.next(), dict(refname='refs/heads/quux')) eq(got.next(), dict(refname='refs/heads/master')) assert_raises(StopIteration, got.next)
def test_update_ref_delete(): 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='foo', content='FOO', ), ], ), ], ) head = commands.rev_parse(repo=tmp, rev='HEAD') eq(head, 'e1b2f3253b18e7bdbd38db0cf295e6b3b608bb27') commands.update_ref( repo=tmp, ref='refs/heads/master', newvalue=None, ) got = commands.rev_parse(repo=tmp, rev='refs/heads/master') eq(got, None)
def test_is_commit_needed_no_change(): 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') commands.update_index( repo=repo, index=index, files=[ dict( object=sha_foo, path='quux/foo', ), ], ) tree = commands.write_tree( repo=repo, index=index, ) got = commands.is_commit_needed( repo=repo, tree=tree, parents=[], ) eq(got, True)
def test_is_commit_needed_no_change(): tmp = maketemp() repo = os.path.join(tmp, 'repo') os.mkdir(repo) commands.init_bare(repo) commands.fast_import( repo=tmp, commits=[ dict( message='one', committer='John Doe <*****@*****.**>', commit_time='1216235872 +0300', files=[ dict( path='foo', content='FOO', ), ], ), ], ) head = commands.rev_parse(repo=tmp, rev='HEAD') tree = commands.rev_parse(repo=tmp, rev='HEAD^{tree}') got = commands.is_commit_needed( repo=repo, tree=tree, parents=[head], ) eq(got, False)
def test_is_commit_needed_has_change(): 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='foo', content='FOO', ), ], ), ], ) head = commands.rev_parse(repo=tmp, rev='HEAD') tree = commands.rev_parse(repo=tmp, rev='HEAD^{tree}') got = commands.is_commit_needed( repo=tmp, tree='deadbeefdeadbeefdeadbeefdeadbeefdeadbeef', parents=[head], ) eq(got, True)
def test_index_path(): tmp = maketemp() repo_path = os.path.join(tmp, 'repo') commands.init_bare(repo_path) commands.fast_import( repo=repo_path, commits=[ dict( message='one', committer='John Doe <*****@*****.**>', commit_time='1216235872 +0300', files=[ dict( path='quux/foo', content='FOO', ), ], ), ], ) index_path = os.path.join(tmp, '1nd3x') r = repo.Repository(path=repo_path) with r.transaction(index=index_path) as p: assert os.path.exists(index_path) assert not os.path.exists(os.path.join(repo_path, 'pygitfs'))
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_no_empty_commits(): 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', ), ], ), ], ) orig = commands.rev_parse( repo=tmp, rev='HEAD', ) r = repo.Repository(path=tmp) with r.transaction() as p: # nothing pass got = commands.rev_parse( repo=tmp, rev='HEAD', ) eq(got, orig)
def test_update_index(): 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') assert not os.path.exists(index) commands.update_index( repo=repo, index=index, files=[ dict( object=sha_foo, path='quux/foo', ), dict( object=sha_bar, path='bar', ), ], ) assert os.path.isfile(index) st = os.stat(index) assert st.st_size > 0
def test_read_tree(): tmp = maketemp() repo = os.path.join(tmp, 'repo') os.mkdir(repo) commands.init_bare(repo) commands.fast_import( repo=repo, commits=[ dict( message='one', committer='John Doe <*****@*****.**>', commit_time='1216235872 +0300', files=[ dict( path='quux/foo', content='FOO', ), ], ), ], ) index = os.path.join(tmp, 'index') assert not os.path.exists(index) commands.read_tree( repo=repo, treeish='HEAD', index=index, ) assert os.path.isfile(index) st = os.stat(index) assert st.st_size > 0
def test_set_sha1_mass(): tmp = maketemp() repo = os.path.join(tmp, 'repo') index = os.path.join(tmp, 'index') commands.init_bare(repo) root = indexfs.IndexFS( repo=repo, index=index, ) one = root.child('one') with one.open('w') as f: f.write('one') one_sha = one.git_get_sha1() two = root.child('two') with two.open('w') as f: f.write('two') two_sha = two.git_get_sha1() bar = root.child('bar') quux = root.child('quux') thud = root.child('thud') root.git_mass_set_sha1([ (bar, one_sha), (quux, two_sha), (thud, one_sha), ]) with bar.open() as f: got = f.read() eq(got, 'one') with quux.open() as f: got = f.read() eq(got, 'two') with thud.open() as f: got = f.read() eq(got, 'one')
def test_init_atomic(): tmp = maketemp() commands.init_bare_atomic( repo=os.path.join(tmp, 'repo'), tmp=os.path.join(tmp, 'repo.42.tmp'), ) eq(os.listdir(tmp), ['repo']) check_repository(repo=os.path.join(tmp, 'repo'))
def setUp(self): tmp = maketemp() repo = os.path.join(tmp, 'repo') index = os.path.join(tmp, 'index') commands.init_bare(repo) self.path = indexfs.IndexFS( repo=repo, index=index, )
def test_enter_no_initial_commit(): tmp = maketemp() commands.init_bare(tmp) with readonly.ReadOnlyGitFS( repo=tmp, rev='HEAD', ) as root: eq(sorted(root), []) # well-known empty tree sha eq(root.rev, '4b825dc642cb6eb9a060e54bf8d69288fbee4904')
def test_get_object_size_bad_notfound(): tmp = maketemp() commands.init_bare(tmp) e = assert_raises( # TODO better exception for this RuntimeError, commands.get_object_size, repo=tmp, object='deadbeefdeadbeefdeadbeefdeadbeefdeadbeef', ) eq(str(e), 'git cat-file failed')
def test_symbolic_ref_bad_nonsymbolic(): tmp = maketemp() commands.init_bare(tmp) e = assert_raises( # TODO better exception for this RuntimeError, commands.get_symbolic_ref, repo=tmp, ref='refs/heads/master', ) eq(str(e), 'git symbolic-ref failed')
def test_name(): tmp = maketemp() commands.init_bare(tmp) r = repo.Repository(tmp) with readonly.ReadOnlyGitFS( repo=tmp, rev='HEAD', ) as root: p1 = root.child('foo') p2 = p1.child('bar') eq(p1.name(), 'foo') eq(p2.name(), 'bar')
def test_ls_files(): tmp = maketemp() repo = os.path.join(tmp, 'repo') os.mkdir(repo) commands.init_bare(repo) index = os.path.join(tmp, 'index') commands.fast_import( repo=repo, 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', ), ], ), ], ) commands.read_tree( repo=repo, treeish='HEAD', index=index, ) g = commands.ls_files( repo=repo, index=index, ) eq( g.next(), dict( mode='100755', object='add8373108657cb230a5379a6fcdaab73f330642', path='bar', ), ) eq( g.next(), dict( mode='100644', object='d96c7efbfec2814ae0301ad054dc8d9fc416c9b5', path='quux/foo', ), ) assert_raises(StopIteration, g.next)
def test_no_empty_commits_initial(): tmp = maketemp() commands.init_bare(tmp) r = repo.Repository(path=tmp) with r.transaction() as p: # nothing pass got = commands.rev_parse( repo=tmp, rev='HEAD', ) eq(got, None)
def test_enter(): tmp = maketemp() commands.init_bare(tmp) r = repo.Repository(tmp) with r.transaction() as root: with root.child('foo').open('w') as f: f.write('FOO') head = commands.rev_parse(repo=tmp, rev='HEAD') assert head is not None with readonly.ReadOnlyGitFS( repo=tmp, rev='HEAD', ) as root: eq(root.rev, head)
def test_open(): tmp = maketemp() commands.init_bare(tmp) r = repo.Repository(tmp) with r.transaction() as root: with root.child('foo').open('w') as f: f.write('FOO') with readonly.ReadOnlyGitFS( repo=tmp, rev='HEAD', ) as root: with root.child('foo').open() as f: got = f.read() eq(got, 'FOO')
def test_TemporaryIndexFS_simple(): tmp = maketemp() repo = os.path.join(tmp, 'repo') commands.init_bare(repo) t = indexfs.TemporaryIndexFS(repo=repo) with t as root: with root.child('bar').open('w') as f: f.write('hello') eq(t.tree, '4d9fa708931786c374d879e71f89f97a68e73f94') got = commands.cat_file( repo=repo, object='4d9fa708931786c374d879e71f89f97a68e73f94:bar', ) eq(got, 'hello')
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 test_open_write(): tmp = maketemp() commands.init_bare(tmp) with readonly.ReadOnlyGitFS( repo=tmp, rev='HEAD', ) as root: p = root.child('foo') e = assert_raises( IOError, p.open, 'w', ) eq(e.errno, errno.EROFS) eq(str(e), '[Errno %d] Read-only file system' % errno.EROFS)
def test_merge_base_disconnected(): 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='foo', content='FOO', ), ], ), ], ) one = commands.rev_parse(repo=tmp, rev='HEAD') commands.fast_import( repo=tmp, ref='refs/heads/another', commits=[ dict( message='two', committer='Jack Smith <*****@*****.**>', commit_time='1216235940 +0300', files=[ dict( path='foo', content='FOO', ), dict( path='bar', content='BAR', ), ], ), ], ) two = commands.rev_parse(repo=tmp, rev='refs/heads/another') got = commands.merge_base( repo=tmp, rev1=one, rev2=two, ) eq(got, None)
def test_get_sha1_nonexistent(): tmp = maketemp() repo = os.path.join(tmp, 'repo') index = os.path.join(tmp, 'index') commands.init_bare(repo) root = indexfs.IndexFS( repo=repo, index=index, ) foo = root.child('foo') e = assert_raises( OSError, foo.git_get_sha1, ) eq(e.errno, errno.ENOENT) eq(str(e), '[Errno %d] No such file or directory' % errno.ENOENT)