@@ -72,21 +72,22 @@ def test_init_repo_object(self, rw_dir):
7272 # heads, tags and references
7373 # heads are branches in git-speak
7474 # [8-test_init_repo_object]
75- self .assertEqual (
76- repo .head .ref ,
77- repo .heads .master , # head is a sym-ref pointing to master.
78- "It's ok if TC not running from `master`." ,
79- )
75+ active_branch = repo .active_branch
76+ self .assertEqual (repo .head .ref , active_branch ) # HEAD is a sym-ref pointing to the active branch.
8077 self .assertEqual (repo .tags ["0.3.5" ], repo .tag ("refs/tags/0.3.5" )) # You can access tags in various ways too.
81- self .assertEqual (repo .refs . master , repo .heads ["master" ]) # .refs provides all refs, i.e. heads ...
78+ self .assertEqual (repo .refs [ active_branch . name ] , repo .heads [active_branch . name ]) # .refs provides all refs...
8279
8380 if "TRAVIS" not in os .environ :
84- self .assertEqual (repo .refs ["origin/master" ], repo .remotes .origin .refs .master ) # ... remotes ...
81+ remote_head = repo .remotes .origin .refs .HEAD .reference
82+ self .assertEqual (
83+ repo .refs [remote_head .name ], repo .remotes .origin .refs [remote_head .remote_head ]
84+ ) # ...remotes...
8585 self .assertEqual (repo .refs ["0.3.5" ], repo .tags ["0.3.5" ]) # ... and tags.
8686 # ![8-test_init_repo_object]
8787
8888 # Create a new head/branch.
8989 # [9-test_init_repo_object]
90+ original_branch = cloned_repo .active_branch
9091 new_branch = cloned_repo .create_head ("feature" ) # Create a new branch ...
9192 assert cloned_repo .active_branch != new_branch # which wasn't checked out yet ...
9293 self .assertEqual (new_branch .commit , cloned_repo .active_branch .commit ) # pointing to the checked-out commit.
@@ -146,10 +147,10 @@ def update(self, op_code, cur_count, max_count=None, message=""):
146147 assert origin .exists ()
147148 for fetch_info in origin .fetch (progress = MyProgressPrinter ()):
148149 print ("Updated %s to %s" % (fetch_info .ref , fetch_info .commit ))
149- # Create a local branch at the latest fetched master. We specify the name
150- # statically, but you have all information to do it programmatically as well.
151- bare_master = bare_repo .create_head ("master" , origin . refs . master )
152- bare_repo .head .set_reference (bare_master )
150+ # Create a local branch at the remote's default branch.
151+ remote_default_branch = origin . refs . HEAD . reference
152+ bare_branch = bare_repo .create_head (remote_default_branch . remote_head , remote_default_branch )
153+ bare_repo .head .set_reference (bare_branch )
153154 assert not bare_repo .delete_remote (origin ).exists ()
154155 # push and pull behave very similarly.
155156 # ![12-test_init_repo_object]
@@ -162,35 +163,39 @@ def update(self, op_code, cur_count, max_count=None, message=""):
162163 new_file_path = os .path .join (cloned_repo .working_tree_dir , "my-new-file" )
163164 open (new_file_path , "wb" ).close () # Create new file in working tree.
164165 cloned_repo .index .add ([new_file_path ]) # Add it to the index.
165- # Commit the changes to deviate masters history.
166+ # Commit the changes to deviate from the original branch's history.
166167 cloned_repo .index .commit ("Added a new file in the past - for later merge" )
167168
168169 # Prepare a merge.
169- master = cloned_repo .heads .master # Right-hand side is ahead of us, in the future.
170- merge_base = cloned_repo .merge_base (new_branch , master ) # Allows for a three-way merge.
171- cloned_repo .index .merge_tree (master , base = merge_base ) # Write the merge result into index.
170+ merge_base = cloned_repo .merge_base (new_branch , original_branch ) # Allows for a three-way merge.
171+ cloned_repo .index .merge_tree (original_branch , base = merge_base ) # Write the merge result into index.
172172 cloned_repo .index .commit (
173173 "Merged past and now into future ;)" ,
174- parent_commits = (new_branch .commit , master .commit ),
174+ parent_commits = (new_branch .commit , original_branch .commit ),
175175 )
176176
177- # Now new_branch is ahead of master , which probably should be checked out and reset softly.
177+ # Now new_branch is ahead of the original branch , which probably should be checked out and reset softly.
178178 # Note that all these operations didn't touch the working tree, as we managed it ourselves.
179179 # This definitely requires you to know what you are doing! :)
180180 assert os .path .basename (new_file_path ) in new_branch .commit .tree # New file is now in tree.
181- master .commit = new_branch .commit # Let master point to most recent commit.
182- cloned_repo .head .reference = master # We adjusted just the reference, not the working tree or index.
181+ original_branch .commit = new_branch .commit # Let the original branch point to the most recent commit.
182+ cloned_repo .head .reference = original_branch # We adjusted just the reference, not the working tree or index.
183183 # ![13-test_init_repo_object]
184184
185185 # submodules
186186
187187 # [14-test_init_repo_object]
188- # Create a new submodule and check it out on the spot, setup to track master
189- # branch of `bare_repo`. As our GitPython repository has submodules already that
190- # point to GitHub, make sure we don't interact with them.
188+ # Create a new submodule and check it out on the spot, set up to track the
189+ # default branch of `bare_repo`. As our GitPython repository has submodules
190+ # already that point to GitHub, make sure we don't interact with them.
191191 for sm in cloned_repo .submodules :
192192 assert not sm .remove ().exists () # after removal, the sm doesn't exist anymore
193- sm = cloned_repo .create_submodule ("mysubrepo" , "path/to/subrepo" , url = bare_repo .git_dir , branch = "master" )
193+ sm = cloned_repo .create_submodule (
194+ "mysubrepo" ,
195+ "path/to/subrepo" ,
196+ url = bare_repo .git_dir ,
197+ branch = bare_branch .name ,
198+ )
194199
195200 # .gitmodules was written and added to the index, which is now being committed.
196201 cloned_repo .index .commit ("Added submodule" )
0 commit comments