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_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) 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_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_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_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_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_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_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_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_TemporaryIndexFS_rev(): 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( message='two', committer='John Doe <*****@*****.**>', commit_time='1216235934 +0300', files=[ dict( path='quux/foo', content='FOO', ), dict( path='bar', content='BAR', mode='100755', ), ], ), ], ) prev = commands.rev_parse(repo=tmp, rev='HEAD~1') t = indexfs.TemporaryIndexFS(repo=tmp, rev='HEAD') eq(t.rev, 'HEAD') t.rev = prev with t as root: eq(list(root), [root.child('quux')]) eq(t.rev, prev) tree = commands.rev_parse(repo=tmp, rev='HEAD~1^{tree}') eq(t.tree, tree)
def test_commit_tree(): 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, ref='refs/heads/import', commits=[ dict( message='one', committer='John Doe <*****@*****.**>', commit_time='1216235872 +0300', files=[ dict( path='foo', content='FOO', ), ], ), ], ) tree = commands.rev_parse( repo=repo, rev='refs/heads/import^{tree}', ) eq(tree, 'd513b699a47153aad2f0cb7ea2cb9fde8c177428') commit = commands.commit_tree( repo=repo, tree=tree, parents=[], message='made some changes', committer_name='John Doe', committer_email='*****@*****.**', committer_date='1216337226 +0300', author_name='Bob Smith', author_email='*****@*****.**', author_date='1216337225 +0300', ) eq(commit, '7a95f3e3276c9704d290375660fb143a3fe0bbcb')
def test_rev_list_reverse(): 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', ), ], ), dict( message='two', committer='Jack Smith <*****@*****.**>', commit_time='1216235940 +0300', files=[ dict( path='foo', content='FOO', ), dict( path='bar', content='BAR', ), ], ), ], ) got = commands.rev_list( repo=tmp, reverse=True, ) got = iter(got) eq(got.next(), 'e1b2f3253b18e7bdbd38db0cf295e6b3b608bb27') eq(got.next(), '27f952fd48ce824454457b9f28bb97091bc5422e') assert_raises(StopIteration, got.next)
def test_rev_parse(): 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', ), ], ), ], ) got = commands.rev_parse(repo=tmp, rev='HEAD') eq(got, 'e1b2f3253b18e7bdbd38db0cf295e6b3b608bb27')
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_read_tree_merge3(): 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='foo', content='FOO', ), ], ), dict( message='two', committer='Jack Smith <*****@*****.**>', commit_time='1216235940 +0300', files=[ dict( path='foo', content='FOO', ), dict( path='bar', content='BAR', ), ], ), ], ) one = commands.rev_parse(repo=repo, rev='HEAD~1') commands.fast_import( repo=repo, ref='refs/heads/bar', commits=[ dict( parent=one, message='three', committer='John Doe <*****@*****.**>', commit_time='1216235942 +0300', files=[ dict( path='foo', content='FOO', ), dict( path='quux', content='QUUX', ), ], ), ], ) index = os.path.join(tmp, 'index') commands.read_tree( repo=repo, treeish='HEAD', index=index, ) commands.read_tree_merge3( repo=repo, index=index, ancestor=one, local='HEAD', remote='refs/heads/bar', trivial=True, ) g = commands.ls_files( repo=repo, index=index, ) eq( g.next(), dict( mode='100644', object='add8373108657cb230a5379a6fcdaab73f330642', path='bar', ), ) eq( g.next(), dict( mode='100644', object='d96c7efbfec2814ae0301ad054dc8d9fc416c9b5', path='foo', ), ) eq( g.next(), dict( mode='100644', object='b9d4f2faa83cc1ad83ef7493ad6cbf08b086e4c1', path='quux', ), ) assert_raises(StopIteration, g.next)
def test_for_each_ref_fields(): 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', ), ], ), dict( message='two', committer='Jack Smith <*****@*****.**>', commit_time='1216235940 +0300', files=[ dict( path='foo', content='FOO', ), dict( path='bar', content='BAR', ), ], ), ], ) commands.update_ref( repo=tmp, ref='refs/heads/quux', newvalue='HEAD~1', oldvalue=40*'0', ) got = commands.for_each_ref( repo=tmp, fields=['refname', 'objecttype', 'objectname'], ) got = iter(got) eq( got.next(), dict( refname='refs/heads/master', objecttype='commit', objectname='27f952fd48ce824454457b9f28bb97091bc5422e', ), ) eq( got.next(), dict( refname='refs/heads/quux', objecttype='commit', objectname='e1b2f3253b18e7bdbd38db0cf295e6b3b608bb27', ), ) assert_raises(StopIteration, got.next)
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')