Skip to content

Commit d8a4958

Browse files
CopilotByron
andauthored
Fix rev parsing for refs starting with at
Agent-Logs-Url: https://github.com/gitpython-developers/GitPython/sessions/0273e08f-ead0-4b1e-b148-b103851caca9 Co-authored-by: Byron <63622+Byron@users.noreply.github.com>
1 parent 5a15361 commit d8a4958

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

git/repo/fun.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,15 @@ def rev_parse(repo: "Repo", rev: str) -> AnyGitObject:
269269
if rev[start] not in "^~:@":
270270
start += 1
271271
continue
272+
if rev[start] == "@":
273+
next_char = rev[start + 1] if start + 1 < lr else None
274+
if start == 0:
275+
if next_char not in (None, "{", "^", "~", ":"):
276+
start += 1
277+
continue
278+
elif next_char != "{":
279+
start += 1
280+
continue
272281
# END handle start
273282

274283
token = rev[start]
@@ -292,6 +301,12 @@ def rev_parse(repo: "Repo", rev: str) -> AnyGitObject:
292301

293302
start += 1
294303

304+
if token == "@" and (start >= lr or rev[start] != "{"):
305+
obj = cast(AnyGitObject, ref.commit)
306+
ref = None
307+
parsed_to = start
308+
continue
309+
295310
# Try to parse {type}.
296311
if start < lr and rev[start] == "{":
297312
end = rev.find("}", start)

test/test_repo.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,22 @@ def test_commit_from_revision(self):
146146
self.assertEqual(commit.type, "commit")
147147
self.assertEqual(self.rorepo.commit(commit), commit)
148148

149+
@with_rw_directory
150+
def test_commit_from_tag_starting_with_at(self, rw_dir):
151+
repo = Repo.init(rw_dir)
152+
with repo.config_writer() as writer:
153+
writer.set_value("user", "name", "GitPython Tests")
154+
writer.set_value("user", "email", "gitpython@example.com")
155+
156+
tracked_file = Path(rw_dir) / "hello.txt"
157+
tracked_file.write_text("hello")
158+
repo.index.add([str(tracked_file)])
159+
commit = repo.index.commit("init")
160+
repo.create_tag("@foo")
161+
162+
self.assertEqual(repo.commit("@"), commit)
163+
self.assertEqual(repo.commit("@foo"), commit)
164+
149165
def test_commits(self):
150166
mc = 10
151167
commits = list(self.rorepo.iter_commits("0.1.6", max_count=mc))

0 commit comments

Comments
 (0)