Skip to content

Commit d40b356

Browse files
committed
Remove default-branch assumptions from tests
1 parent 04960cf commit d40b356

4 files changed

Lines changed: 42 additions & 36 deletions

File tree

test/test_commit.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,14 +276,14 @@ def test_iteration(self):
276276
assert ltd_commits and len(ltd_commits) < len(all_commits)
277277

278278
# Show commits of multiple paths, resulting in a union of commits.
279-
less_ltd_commits = list(Commit.iter_items(self.rorepo, "master", paths=("CHANGES", "AUTHORS")))
279+
less_ltd_commits = list(Commit.iter_items(self.rorepo, "HEAD", paths=("CHANGES", "AUTHORS")))
280280
assert len(ltd_commits) < len(less_ltd_commits)
281281

282282
class Child(Commit):
283283
def __init__(self, *args, **kwargs):
284284
super().__init__(*args, **kwargs)
285285

286-
child_commits = list(Child.iter_items(self.rorepo, "master", paths=("CHANGES", "AUTHORS")))
286+
child_commits = list(Child.iter_items(self.rorepo, "HEAD", paths=("CHANGES", "AUTHORS")))
287287
assert type(child_commits[0]) is Child
288288

289289
def test_iter_items(self):
@@ -536,7 +536,7 @@ def test_trailers(self):
536536
),
537537
]
538538
for msg in msgs:
539-
commit = copy.copy(self.rorepo.commit("master"))
539+
commit = copy.copy(self.rorepo.commit("HEAD"))
540540
commit.message = msg
541541
assert commit.trailers_list == [
542542
(KEY_1, VALUE_1_1),
@@ -559,13 +559,13 @@ def test_trailers(self):
559559
]
560560

561561
for msg in msgs:
562-
commit = copy.copy(self.rorepo.commit("master"))
562+
commit = copy.copy(self.rorepo.commit("HEAD"))
563563
commit.message = msg
564564
assert commit.trailers_list == []
565565
assert commit.trailers_dict == {}
566566

567567
# Check that only the last key value paragraph is evaluated.
568-
commit = copy.copy(self.rorepo.commit("master"))
568+
commit = copy.copy(self.rorepo.commit("HEAD"))
569569
commit.message = f"Subject\n\nMultiline\nBody\n\n{KEY_1}: {VALUE_1_1}\n\n{KEY_2}: {VALUE_2}\n"
570570
assert commit.trailers_list == [(KEY_2, VALUE_2)]
571571
assert commit.trailers_dict == {KEY_2: [VALUE_2]}

test/test_docs.py

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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")

test/test_refs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ def test_orig_head(self):
258258

259259
@with_rw_repo("0.1.6")
260260
def test_head_checkout_detached_head(self, rw_repo):
261-
res = rw_repo.remotes.origin.refs.master.checkout()
261+
res = rw_repo.remotes.origin.refs.HEAD.reference.checkout()
262262
assert isinstance(res, SymbolicReference)
263263
assert res.name == "HEAD"
264264

@@ -661,7 +661,7 @@ def test_dereference_recursive(self):
661661
assert SymbolicReference.dereference_recursive(self.rorepo, "HEAD")
662662

663663
def test_reflog(self):
664-
assert isinstance(self.rorepo.heads.master.log(), RefLog)
664+
assert isinstance(self.rorepo.active_branch.log(), RefLog)
665665

666666
def test_refs_outside_repo(self):
667667
# Create a file containing a valid reference outside the repository. Attempting

test/test_repo.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,9 @@ def test_heads_should_populate_head_data(self):
128128
self.assertIsInstance(head.commit, Commit)
129129
# END for each head
130130

131-
self.assertIsInstance(self.rorepo.heads.master, Head)
132-
self.assertIsInstance(self.rorepo.heads["master"], Head)
131+
active_branch = self.rorepo.active_branch
132+
self.assertIsInstance(self.rorepo.heads[active_branch.name], Head)
133+
self.assertEqual(self.rorepo.heads[active_branch.name], active_branch)
133134

134135
def test_tree_from_revision(self):
135136
tree = self.rorepo.tree("0.1.6")
@@ -1008,7 +1009,7 @@ def last_commit(repo, rev, path):
10081009
for _ in range(64):
10091010
for repo_type in (GitCmdObjectDB, GitDB):
10101011
repo = Repo(self.rorepo.working_tree_dir, odbt=repo_type)
1011-
last_commit(repo, "master", "test/test_base.py")
1012+
last_commit(repo, "HEAD", "test/test_base.py")
10121013
# END for each repository type
10131014
# END for each iteration
10141015

@@ -1125,11 +1126,11 @@ def test_is_ancestor(self):
11251126
c1 = "f6aa8d1"
11261127
c2 = "763ef75"
11271128
self.assertTrue(repo.is_ancestor(c1, c1))
1128-
self.assertTrue(repo.is_ancestor("master", "master"))
1129+
self.assertTrue(repo.is_ancestor("HEAD", "HEAD"))
11291130
self.assertTrue(repo.is_ancestor(c1, c2))
1130-
self.assertTrue(repo.is_ancestor(c1, "master"))
1131+
self.assertTrue(repo.is_ancestor(c1, "HEAD"))
11311132
self.assertFalse(repo.is_ancestor(c2, c1))
1132-
self.assertFalse(repo.is_ancestor("master", c1))
1133+
self.assertFalse(repo.is_ancestor("HEAD", c1))
11331134
for i, j in itertools.permutations([c1, "ffffff", ""], r=2):
11341135
self.assertRaises(GitCommandError, repo.is_ancestor, i, j)
11351136

0 commit comments

Comments
 (0)