Skip to content

Commit bdbdf4b

Browse files
codexByron
authored andcommitted
Fix rev-parse CI issues
1 parent d7ce6fc commit bdbdf4b

3 files changed

Lines changed: 43 additions & 10 deletions

File tree

git/repo/fun.py

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141

4242
if TYPE_CHECKING:
4343
from git.db import GitCmdObjectDB
44-
from git.objects import Commit, TagObject
44+
from git.objects import Commit
4545
from git.refs.reference import Reference
4646
from git.refs.log import RefLog, RefLogEntry
4747
from git.refs.tag import Tag
@@ -256,13 +256,30 @@ def _object_from_hexsha(repo: "Repo", hexsha: str) -> AnyGitObject:
256256

257257

258258
def _current_reflog_ref(repo: "Repo") -> SymbolicReference:
259-
return repo.head
259+
try:
260+
return repo.head.ref
261+
except TypeError:
262+
return repo.head
263+
# END handle detached head
264+
265+
266+
def _common_reflog_path(repo: "Repo", ref: SymbolicReference) -> Optional[str]:
267+
if repo.common_dir == repo.git_dir:
268+
return None
269+
# END handle normal repository
270+
return SymbolicReference._get_validated_path(osp.join(repo.common_dir, "logs"), ref.path)
260271

261272

262273
def _ref_log(repo: "Repo", ref: SymbolicReference) -> "RefLog":
263274
try:
264275
return ref.log()
265276
except FileNotFoundError:
277+
common_path = _common_reflog_path(repo, ref)
278+
if common_path and osp.isfile(common_path):
279+
from git.refs.log import RefLog
280+
281+
return RefLog.from_file(common_path)
282+
# END handle linked-worktree branch logs
266283
try:
267284
if ref.path == repo.head.ref.path:
268285
return repo.head.log()
@@ -278,6 +295,12 @@ def _ref_log_entry(repo: "Repo", ref: SymbolicReference, index: int) -> "RefLogE
278295
try:
279296
return ref.log_entry(index)
280297
except FileNotFoundError:
298+
common_path = _common_reflog_path(repo, ref)
299+
if common_path and osp.isfile(common_path):
300+
from git.refs.log import RefLog
301+
302+
return RefLog.entry_at(common_path, index)
303+
# END handle linked-worktree branch logs
281304
try:
282305
if ref.path == repo.head.ref.path:
283306
return repo.head.log_entry(index)
@@ -464,7 +487,11 @@ def _find_commit_by_message(
464487
# END handle starting point
465488

466489
for commit in commits:
467-
matches = regex.search(commit.message or "") is not None
490+
message = commit.message
491+
if isinstance(message, bytes):
492+
message = message.decode(commit.encoding, "replace")
493+
# END handle bytes message
494+
matches = regex.search(message or "") is not None
468495
if matches != negated:
469496
return commit
470497
# END found commit
@@ -505,15 +532,15 @@ def _peel(obj: AnyGitObject, output_type: str, repo: "Repo", rev: str) -> AnyGit
505532
if output_type.startswith("/"):
506533
return _find_commit_by_message(repo, obj, output_type[1:], braced=True)
507534
if output_type == "":
508-
return deref_tag(cast("TagObject", obj)) if obj.type == "tag" else obj
535+
return deref_tag(obj) if obj.type == "tag" else obj
509536
if output_type == "object":
510537
return obj
511538
if output_type == "commit":
512539
return to_commit(cast(Object, obj))
513540
if output_type == "tree":
514541
return to_commit(cast(Object, obj)).tree if obj.type != "tree" else obj
515542
if output_type == "blob":
516-
obj = deref_tag(cast("TagObject", obj)) if obj.type == "tag" else obj
543+
obj = deref_tag(obj) if obj.type == "tag" else obj
517544
if obj.type == output_type:
518545
return obj
519546
# END handle matching type
@@ -615,14 +642,14 @@ def rev_parse(repo: "Repo", rev: str) -> AnyGitObject:
615642
# END handle reflog
616643

617644
if token == ":":
618-
return _tree_lookup(cast(AnyGitObject, obj), rev[start + 1 :])
645+
return _tree_lookup(obj, rev[start + 1 :])
619646
# END handle path
620647

621648
start += 1
622649

623650
if token == "^" and start < lr and rev[start] == "{":
624651
end = _find_closing_brace(rev, start)
625-
obj = _peel(cast(AnyGitObject, obj), rev[start + 1 : end], repo, rev)
652+
obj = _peel(obj, rev[start + 1 : end], repo, rev)
626653
ref = None
627654
start = end + 1
628655
continue
@@ -645,7 +672,6 @@ def rev_parse(repo: "Repo", rev: str) -> AnyGitObject:
645672
# END set default num
646673

647674
try:
648-
obj = cast(AnyGitObject, obj)
649675
if token == "~":
650676
obj = to_commit(obj)
651677
for _ in range(num):

test/test_repo.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -865,8 +865,13 @@ def test_rev_parse(self):
865865
# Currently, nothing more is supported.
866866
self.assertRaises(NotImplementedError, rev_parse, "@{1 week ago}")
867867

868-
# The last position.
869-
assert rev_parse("@{1}") != head.commit
868+
# The previous position, if this checkout has enough reflog history.
869+
try:
870+
previous = rev_parse("@{1}")
871+
except IndexError:
872+
pass
873+
else:
874+
self.assertNotEqual(previous, head.commit)
870875

871876
def test_repo_odbtype(self):
872877
target_type = GitCmdObjectDB

test/test_rev_parse.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,11 @@ def test_rev_parse_reflog_selectors(rev_parse_repo):
106106
merge = rev_parse_repo["merge"]
107107
side = rev_parse_repo["side"]
108108
main = rev_parse_repo["main"]
109+
release = rev_parse_repo["release"]
109110

110111
assert repo.rev_parse("@{0}") == merge
111112
assert repo.rev_parse("@{+0}") == merge
113+
assert repo.rev_parse("@{1}") == release
112114
assert repo.rev_parse("%s@{0}" % main.name) == merge
113115
assert repo.rev_parse("@{-1}") == side
114116

0 commit comments

Comments
 (0)