<s> from git . test . lib import ( <EOL> TestBase , <EOL> with_rw_repo <EOL> ) <EOL> from git import ( <EOL> Reference , <EOL> Head , <EOL> TagReference , <EOL> RemoteReference , <EOL> Commit , <EOL> SymbolicReference , <EOL> GitCommandError , <EOL> RefLog <EOL> ) <EOL> import git . refs as refs <EOL> from git . util import Actor <EOL> from git . objects . tag import TagObject <EOL> from itertools import chain <EOL> import os <EOL> class TestRefs ( TestBase ) : <EOL> def test_from_path ( self ) : <EOL> for ref_type in ( Reference , Head , TagReference , RemoteReference ) : <EOL> for name in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> full_path = ref_type . to_full_path ( name ) <EOL> instance = ref_type . from_path ( self . rorepo , full_path ) <EOL> assert isinstance ( instance , ref_type ) <EOL> self . failUnlessRaises ( ValueError , TagReference , self . rorepo , "<STR_LIT>" ) <EOL> TagReference ( self . rorepo , "<STR_LIT>" , check_path = False ) <EOL> def test_tag_base ( self ) : <EOL> tag_object_refs = list ( ) <EOL> for tag in self . rorepo . tags : <EOL> assert "<STR_LIT>" in tag . path <EOL> assert tag . name <EOL> assert isinstance ( tag . commit , Commit ) <EOL> if tag . tag is not None : <EOL> tag_object_refs . append ( tag ) <EOL> tagobj = tag . tag <EOL> self . failUnlessRaises ( AttributeError , setattr , tagobj , '<STR_LIT>' , <NUM_LIT:1> ) <EOL> assert isinstance ( tagobj , TagObject ) <EOL> assert tagobj . tag == tag . name <EOL> assert isinstance ( tagobj . tagger , Actor ) <EOL> assert isinstance ( tagobj . tagged_date , int ) <EOL> assert isinstance ( tagobj . tagger_tz_offset , int ) <EOL> assert tagobj . message <EOL> assert tag . object == tagobj <EOL> self . failUnlessRaises ( AttributeError , setattr , tag , '<STR_LIT:object>' , tagobj ) <EOL> assert tag_object_refs <EOL> assert isinstance ( self . rorepo . tags [ '<STR_LIT>' ] , TagReference ) <EOL> def test_tags_author ( self ) : <EOL> tag = self . rorepo . tags [ <NUM_LIT:0> ] <EOL> tagobj = tag . tag <EOL> assert isinstance ( tagobj . tagger , Actor ) <EOL> tagger_name = tagobj . tagger . name <EOL> assert tagger_name == '<STR_LIT>' <EOL> def test_tags ( self ) : <EOL> s = set ( ) <EOL> ref_count = <NUM_LIT:0> <EOL> for ref in chain ( self . rorepo . tags , self . rorepo . heads ) : <EOL> ref_count += <NUM_LIT:1> <EOL> assert isinstance ( ref , refs . Reference ) <EOL> assert str ( ref ) == ref . name <EOL> assert repr ( ref ) <EOL> assert ref == ref <EOL> assert not ref != ref <EOL> s . add ( ref ) <EOL> assert len ( s ) == ref_count <EOL> assert len ( s | s ) == ref_count <EOL> @ with_rw_repo ( '<STR_LIT>' , bare = False ) <EOL> def test_heads ( self , rwrepo ) : <EOL> for head in rwrepo . heads : <EOL> assert head . name <EOL> assert head . path <EOL> assert "<STR_LIT>" in head . path <EOL> prev_object = head . object <EOL> cur_object = head . object <EOL> assert prev_object == cur_object <EOL> assert prev_object is not cur_object <EOL> writer = head . config_writer ( ) <EOL> tv = "<STR_LIT>" <EOL> writer . set_value ( tv , <NUM_LIT:1> ) <EOL> assert writer . get_value ( tv ) == <NUM_LIT:1> <EOL> writer . release ( ) <EOL> assert head . config_reader ( ) . get_value ( tv ) == <NUM_LIT:1> <EOL> writer = head . config_writer ( ) <EOL> writer . remove_option ( tv ) <EOL> writer . release ( ) <EOL> head . set_tracking_branch ( None ) <EOL> assert head . tracking_branch ( ) is None <EOL> remote_ref = rwrepo . remotes [ <NUM_LIT:0> ] . refs [ <NUM_LIT:0> ] <EOL> assert head . set_tracking_branch ( remote_ref ) is head <EOL> assert head . tracking_branch ( ) == remote_ref <EOL> head . set_tracking_branch ( None ) <EOL> assert head . tracking_branch ( ) is None <EOL> head = rwrepo . head <EOL> cur_head = head . ref <EOL> cur_commit = cur_head . commit <EOL> pcommit = cur_head . commit . parents [ <NUM_LIT:0> ] . parents [ <NUM_LIT:0> ] <EOL> hlog_len = len ( head . log ( ) ) <EOL> blog_len = len ( cur_head . log ( ) ) <EOL> assert head . set_reference ( pcommit , '<STR_LIT>' ) is head <EOL> thlog = head . log ( ) <EOL> assert len ( thlog ) == hlog_len + <NUM_LIT:1> <EOL> assert thlog [ - <NUM_LIT:1> ] . oldhexsha == cur_commit . hexsha <EOL> assert thlog [ - <NUM_LIT:1> ] . newhexsha == pcommit . hexsha <EOL> assert len ( cur_head . log ( ) ) == blog_len <EOL> head . set_reference ( cur_head , '<STR_LIT>' ) <EOL> assert len ( head . log ( ) ) == hlog_len + <NUM_LIT:2> <EOL> assert len ( cur_head . log ( ) ) == blog_len <EOL> cur_head . set_commit ( pcommit , '<STR_LIT>' ) <EOL> assert len ( cur_head . log ( ) ) == blog_len + <NUM_LIT:1> <EOL> assert len ( head . log ( ) ) == hlog_len + <NUM_LIT:3> <EOL> assert head . set_commit ( cur_commit , '<STR_LIT>' ) is head <EOL> assert len ( head . log ( ) ) == hlog_len + <NUM_LIT:4> <EOL> assert len ( cur_head . log ( ) ) == blog_len + <NUM_LIT:2> <EOL> other_head = Head . create ( rwrepo , '<STR_LIT>' , pcommit , logmsg = '<STR_LIT>' ) <EOL> log = other_head . log ( ) <EOL> assert len ( log ) == <NUM_LIT:1> <EOL> assert log [ <NUM_LIT:0> ] . oldhexsha == pcommit . NULL_HEX_SHA <EOL> assert log [ <NUM_LIT:0> ] . newhexsha == pcommit . hexsha <EOL> def test_refs ( self ) : <EOL> types_found = set ( ) <EOL> for ref in self . rorepo . refs : <EOL> types_found . add ( type ( ref ) ) <EOL> assert len ( types_found ) >= <NUM_LIT:3> <EOL> def test_is_valid ( self ) : <EOL> assert not Reference ( self . rorepo , '<STR_LIT>' ) . is_valid ( ) <EOL> assert self . rorepo . head . is_valid ( ) <EOL> assert self . rorepo . head . reference . is_valid ( ) <EOL> assert not SymbolicReference ( self . rorepo , '<STR_LIT>' ) . is_valid ( ) <EOL> def test_orig_head ( self ) : <EOL> assert type ( self . rorepo . head . orig_head ( ) ) == SymbolicReference <EOL> @ with_rw_repo ( '<STR_LIT>' ) <EOL> def test_head_reset ( self , rw_repo ) : <EOL> cur_head = rw_repo . head <EOL> old_head_commit = cur_head . commit <EOL> new_head_commit = cur_head . ref . commit . parents [ <NUM_LIT:0> ] <EOL> cur_head . reset ( new_head_commit , index = True ) <EOL> assert cur_head . reference . commit == new_head_commit <EOL> self . failUnlessRaises ( ValueError , cur_head . reset , new_head_commit , index = False , working_tree = True ) <EOL> new_head_commit = new_head_commit . parents [ <NUM_LIT:0> ] <EOL> cur_head . reset ( new_head_commit , index = True , working_tree = True ) <EOL> assert cur_head . reference . commit == new_head_commit <EOL> rw_repo . index . reset ( old_head_commit . parents [ <NUM_LIT:0> ] ) <EOL> cur_head . reset ( cur_head , paths = "<STR_LIT:test>" ) <EOL> cur_head . reset ( new_head_commit , paths = "<STR_LIT>" ) <EOL> self . failUnlessRaises ( GitCommandError , cur_head . reset , new_head_commit , working_tree = True , paths = "<STR_LIT>" ) <EOL> cur_head . reset ( new_head_commit ) <EOL> rw_repo . index . checkout ( [ "<STR_LIT>" ] , force = True ) <EOL> heads = rw_repo . heads <EOL> assert heads <EOL> for head in heads : <EOL> cur_head . reference = head <EOL> assert cur_head . reference == head <EOL> assert isinstance ( cur_head . reference , Head ) <EOL> assert cur_head . commit == head . commit <EOL> assert not cur_head . is_detached <EOL> active_head = heads [ <NUM_LIT:0> ] <EOL> curhead_commit = active_head . commit <EOL> cur_head . reference = curhead_commit <EOL> assert cur_head . commit == curhead_commit <EOL> assert cur_head . is_detached <EOL> self . failUnlessRaises ( TypeError , getattr , cur_head , "<STR_LIT>" ) <EOL> some_tag = rw_repo . tags [ <NUM_LIT:0> ] <EOL> cur_head . reference = some_tag <EOL> assert not cur_head . is_detached <EOL> assert cur_head . commit == some_tag . commit <EOL> assert isinstance ( cur_head . reference , TagReference ) <EOL> cur_head . reference = active_head <EOL> self . failUnlessRaises ( ValueError , setattr , cur_head , "<STR_LIT>" , "<STR_LIT>" ) <EOL> commit = '<STR_LIT>' <EOL> prev_head_commit = cur_head . commit <EOL> for count , new_name in enumerate ( ( "<STR_LIT>" , "<STR_LIT>" ) ) : <EOL> actual_commit = commit + "<STR_LIT>" * count <EOL> new_head = Head . create ( rw_repo , new_name , actual_commit ) <EOL> assert new_head . is_detached <EOL> assert cur_head . commit == prev_head_commit <EOL> assert isinstance ( new_head , Head ) <EOL> Head . create ( rw_repo , new_name , new_head . commit ) <EOL> self . failUnlessRaises ( OSError , Head . create , rw_repo , new_name , new_head . commit . parents [ <NUM_LIT:0> ] ) <EOL> new_head = Head . create ( rw_repo , new_name , actual_commit , force = True ) <EOL> old_path = new_head . path <EOL> old_name = new_head . name <EOL> assert new_head . rename ( "<STR_LIT:hello>" ) . name == "<STR_LIT:hello>" <EOL> assert new_head . rename ( "<STR_LIT>" ) . name == "<STR_LIT>" <EOL> assert new_head . rename ( old_name ) . name == old_name and new_head . path == old_path <EOL> tmp_head = Head . create ( rw_repo , "<STR_LIT>" ) <EOL> self . failUnlessRaises ( GitCommandError , tmp_head . rename , new_head ) <EOL> tmp_head . rename ( new_head , force = True ) <EOL> assert tmp_head == new_head and tmp_head . object == new_head . object <EOL> logfile = RefLog . path ( tmp_head ) <EOL> assert os . path . isfile ( logfile ) <EOL> Head . delete ( rw_repo , tmp_head ) <EOL> assert not os . path . isfile ( logfile ) <EOL> heads = rw_repo . heads <EOL> assert tmp_head not in heads and new_head not in heads <EOL> self . failUnlessRaises ( TypeError , RemoteReference . create , rw_repo , "<STR_LIT>" ) <EOL> tag_name = "<STR_LIT>" <EOL> light_tag = TagReference . create ( rw_repo , tag_name ) <EOL> self . failUnlessRaises ( GitCommandError , TagReference . create , rw_repo , tag_name ) <EOL> light_tag = TagReference . create ( rw_repo , tag_name , "<STR_LIT>" , force = True ) <EOL> assert isinstance ( light_tag , TagReference ) <EOL> assert light_tag . name == tag_name <EOL> assert light_tag . commit == cur_head . commit . parents [ <NUM_LIT:0> ] <EOL> assert light_tag . tag is None <EOL> other_tag_name = "<STR_LIT>" <EOL> msg = "<STR_LIT>" <EOL> obj_tag = TagReference . create ( rw_repo , other_tag_name , message = msg ) <EOL> assert isinstance ( obj_tag , TagReference ) <EOL> assert obj_tag . name == other_tag_name <EOL> assert obj_tag . commit == cur_head . commit <EOL> assert obj_tag . tag is not None <EOL> TagReference . delete ( rw_repo , light_tag , obj_tag ) <EOL> tags = rw_repo . tags <EOL> assert light_tag not in tags and obj_tag not in tags <EOL> remote_refs_so_far = <NUM_LIT:0> <EOL> remotes = rw_repo . remotes <EOL> assert remotes <EOL> for remote in remotes : <EOL> refs = remote . refs <EOL> remote_head_name = "<STR_LIT>" <EOL> if remote_head_name in refs : <EOL> RemoteReference . delete ( rw_repo , refs [ remote_head_name ] ) <EOL> del ( refs [ remote_head_name ] ) <EOL> RemoteReference . delete ( rw_repo , * refs ) <EOL> remote_refs_so_far += len ( refs ) <EOL> for ref in refs : <EOL> assert ref . remote_name == remote . name <EOL> assert remote_refs_so_far <EOL> for remote in remotes : <EOL> self . failUnlessRaises ( AssertionError , getattr , remote , '<STR_LIT>' ) <EOL> if cur_head . is_detached : <EOL> cur_head . reference = rw_repo . heads [ <NUM_LIT:0> ] <EOL> head = cur_head . reference <EOL> old_commit = head . commit <EOL> head . commit = old_commit . parents [ <NUM_LIT:0> ] <EOL> assert head . commit == old_commit . parents [ <NUM_LIT:0> ] <EOL> assert head . commit == cur_head . commit <EOL> head . commit = old_commit <EOL> head_tree = head . commit . tree <EOL> self . failUnlessRaises ( ValueError , setattr , head , '<STR_LIT>' , head_tree ) <EOL> assert head . commit == old_commit <EOL> head . object = head_tree <EOL> assert head . object == head_tree <EOL> self . failUnlessRaises ( TypeError , getattr , head , '<STR_LIT>' ) <EOL> assert not cur_head . is_detached <EOL> head . object = old_commit <EOL> cur_head . reference = head . commit <EOL> assert cur_head . is_detached <EOL> parent_commit = head . commit . parents [ <NUM_LIT:0> ] <EOL> assert cur_head . is_detached <EOL> cur_head . commit = parent_commit <EOL> assert cur_head . is_detached and cur_head . commit == parent_commit <EOL> cur_head . reference = head <EOL> assert not cur_head . is_detached <EOL> cur_head . commit = parent_commit <EOL> assert not cur_head . is_detached <EOL> assert head . commit == parent_commit <EOL> active_branch = rw_repo . active_branch <EOL> for head in rw_repo . heads : <EOL> checked_out_head = head . checkout ( ) <EOL> assert checked_out_head == head <EOL> new_head = active_branch . checkout ( b = "<STR_LIT>" ) <EOL> assert active_branch != rw_repo . active_branch <EOL> assert new_head == rw_repo . active_branch <EOL> open ( new_head . commit . tree . blobs [ - <NUM_LIT:1> ] . abspath , '<STR_LIT:w>' ) . close ( ) <EOL> assert len ( new_head . commit . diff ( None ) ) <EOL> far_away_head = rw_repo . create_head ( "<STR_LIT>" , '<STR_LIT>' ) <EOL> self . failUnlessRaises ( GitCommandError , far_away_head . checkout ) <EOL> assert active_branch == active_branch . checkout ( force = True ) <EOL> assert rw_repo . head . reference != far_away_head <EOL> partial_ref = '<STR_LIT>' <EOL> full_ref = '<STR_LIT>' % partial_ref <EOL> ref = Reference . create ( rw_repo , partial_ref ) <EOL> assert ref . path == full_ref <EOL> assert ref . object == rw_repo . head . commit <EOL> self . failUnlessRaises ( OSError , Reference . create , rw_repo , full_ref , '<STR_LIT>' ) <EOL> assert Reference . create ( rw_repo , full_ref , '<STR_LIT>' ) . path == full_ref <EOL> Reference . delete ( rw_repo , full_ref ) <EOL> ref = Reference . create ( rw_repo , full_ref ) <EOL> assert ref . path == full_ref <EOL> assert ref . object == rw_repo . head . commit <EOL> ref = Reference . create ( rw_repo , partial_ref , '<STR_LIT>' , force = True ) <EOL> assert ref . path == full_ref <EOL> assert ref . object == rw_repo . head . commit . parents [ <NUM_LIT:0> ] <EOL> orig_obj = ref . object <EOL> for name in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> ref_new_name = ref . rename ( name ) <EOL> assert isinstance ( ref_new_name , Reference ) <EOL> assert name in ref_new_name . path <EOL> assert ref_new_name . object == orig_obj <EOL> assert ref_new_name == ref <EOL> self . failUnlessRaises ( ValueError , getattr , Reference ( rw_repo , "<STR_LIT>" ) , '<STR_LIT>' ) <EOL> ex_ref_path = far_away_head . path <EOL> self . failUnlessRaises ( OSError , ref . rename , ex_ref_path ) <EOL> far_away_head . commit = ref . commit <EOL> ref . rename ( ex_ref_path ) <EOL> assert ref . path == ex_ref_path and ref . object == orig_obj <EOL> assert ref . rename ( ref . path ) . path == ex_ref_path <EOL> symref_path = "<STR_LIT>" <EOL> symref = SymbolicReference . create ( rw_repo , symref_path , cur_head . reference ) <EOL> assert symref . path == symref_path <EOL> assert symref . reference == cur_head . reference <EOL> self . failUnlessRaises ( OSError , SymbolicReference . create , rw_repo , symref_path , cur_head . reference . commit ) <EOL> SymbolicReference . create ( rw_repo , symref . path , symref . reference ) . path == symref . path <EOL> SymbolicReference . delete ( rw_repo , symref ) <EOL> symref = SymbolicReference . create ( rw_repo , symref_path , cur_head . reference ) <EOL> symbol_ref_path = "<STR_LIT>" <EOL> symref = SymbolicReference ( rw_repo , symbol_ref_path ) <EOL> assert symref . path == symbol_ref_path <EOL> symbol_ref_abspath = os . path . join ( rw_repo . git_dir , symref . path ) <EOL> symref . reference = new_head <EOL> assert symref . reference == new_head <EOL> assert os . path . isfile ( symbol_ref_abspath ) <EOL> assert symref . commit == new_head . commit <EOL> for name in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> symref_new_name = symref . rename ( name ) <EOL> assert isinstance ( symref_new_name , SymbolicReference ) <EOL> assert name in symref_new_name . path <EOL> assert symref_new_name . reference == new_head <EOL> assert symref_new_name == symref <EOL> assert not symref . is_detached <EOL> Reference . create ( rw_repo , full_ref ) <EOL> rw_repo . git . pack_refs ( all = True , prune = True ) <EOL> heads = rw_repo . heads <EOL> assert heads <EOL> assert new_head in heads <EOL> assert active_branch in heads <EOL> assert rw_repo . tags <EOL> for symref in SymbolicReference . iter_items ( rw_repo ) : <EOL> assert not symref . is_detached <EOL> rw_repo . head . reference = rw_repo . head . commit <EOL> deleted_refs = set ( ) <EOL> for ref in Reference . iter_items ( rw_repo ) : <EOL> if ref . is_detached : <EOL> ref . delete ( rw_repo , ref ) <EOL> deleted_refs . add ( ref ) <EOL> assert deleted_refs <EOL> for ref in Reference . iter_items ( rw_repo ) : <EOL> if ref . is_detached : <EOL> assert ref not in deleted_refs <EOL> rw_repo . head . reference = Head . create ( rw_repo , "<STR_LIT>" ) <EOL> assert os . path . isfile ( os . path . join ( rw_repo . git_dir , '<STR_LIT>' ) ) <EOL> refs = list ( SymbolicReference . iter_items ( rw_repo ) ) <EOL> assert len ( refs ) == <NUM_LIT:1> <EOL> for path in ( "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ) : <EOL> fpath = Reference . to_full_path ( path ) <EOL> ref_fp = Reference . from_path ( rw_repo , fpath ) <EOL> assert not ref_fp . is_valid ( ) <EOL> ref = Reference ( rw_repo , fpath ) <EOL> assert ref == ref_fp <EOL> ref . commit = rw_repo . head . commit <EOL> assert ref . is_valid ( ) <EOL> Reference . delete ( ref . repo , ref . path ) <EOL> assert not ref . is_valid ( ) <EOL> self . failUnlessRaises ( ValueError , setattr , ref , '<STR_LIT>' , "<STR_LIT>" ) <EOL> assert not ref . is_valid ( ) <EOL> Reference . delete ( ref . repo , ref . path ) <EOL> assert not ref . is_valid ( ) <EOL> ref . object = rw_repo . head . commit <EOL> assert ref . is_valid ( ) <EOL> Reference . delete ( ref . repo , ref . path ) <EOL> assert not ref . is_valid ( ) <EOL> self . failUnlessRaises ( ValueError , setattr , ref , '<STR_LIT:object>' , "<STR_LIT>" ) <EOL> assert not ref . is_valid ( ) <EOL> def test_dereference_recursive ( self ) : <EOL> assert SymbolicReference . dereference_recursive ( self . rorepo , '<STR_LIT>' ) <EOL> def test_reflog ( self ) : <EOL> assert isinstance ( self . rorepo . heads . master . log ( ) , RefLog ) </s>
