Skip to content

Commit d570372

Browse files
committed
reference: iter_item method add a parameter to return the ref's destination
1 parent 32da7fe commit d570372

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

git/refs/reference.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ def name(self):
9494
return '/'.join(tokens[2:])
9595

9696
@classmethod
97-
def iter_items(cls, repo, common_path=None):
97+
def iter_items(cls, repo, common_path=None, with_destination=False):
9898
"""Equivalent to SymbolicReference.iter_items, but will return non-detached
9999
references as well."""
100-
return cls._iter_items(repo, common_path)
100+
return cls._iter_items(repo, common_path, with_destination)
101101

102102
#}END interface
103103

git/refs/symbolic.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -566,10 +566,11 @@ def rename(self, new_path, force=False):
566566
return self
567567

568568
@classmethod
569-
def _iter_items(cls, repo, common_path=None):
569+
def _iter_items(cls, repo, common_path=None, with_destination=False):
570570
if common_path is None:
571571
common_path = cls._common_path_default
572572
rela_paths = set()
573+
rela_paths_dest = {}
573574

574575
# walk loose refs
575576
# Currently we do not follow links
@@ -584,21 +585,31 @@ def _iter_items(cls, repo, common_path=None):
584585
if f == 'packed-refs':
585586
continue
586587
abs_path = to_native_path_linux(join_path(root, f))
587-
rela_paths.add(abs_path.replace(to_native_path_linux(repo.git_dir) + '/', ""))
588+
rela_path = abs_path.replace(to_native_path_linux(repo.git_dir) + '/', "")
589+
rela_paths.add(rela_path)
590+
if with_destination:
591+
with open(abs_path, 'r') as fp:
592+
dest = fp.read().strip('\n')
593+
rela_paths_dest[rela_path] = dest
588594
# END for each file in root directory
589595
# END for each directory to walk
590596

591597
# read packed refs
592-
for sha, rela_path in cls._iter_packed_refs(repo): # @UnusedVariable
598+
for dest, rela_path in cls._iter_packed_refs(repo): # @UnusedVariable
593599
if rela_path.startswith(common_path):
594600
rela_paths.add(rela_path)
601+
if with_destination and rela_path not in rela_paths_dest:
602+
rela_paths_dest[rela_path] = dest
595603
# END relative path matches common path
596604
# END packed refs reading
597605

598606
# return paths in sorted order
599607
for path in sorted(rela_paths):
600608
try:
601-
yield cls.from_path(repo, path)
609+
if with_destination:
610+
yield cls.from_path(repo, path), rela_paths_dest[path] if path in rela_paths_dest else None
611+
else:
612+
yield cls.from_path(repo, path)
602613
except ValueError:
603614
continue
604615
# END for each sorted relative refpath

git/test/test_refs.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ def test_tags(self):
9292
assert len(s) == ref_count
9393
assert len(s | s) == ref_count
9494

95+
@with_rw_repo('HEAD')
96+
def test_iter_item_with_destination(self, rwrepo):
97+
for ref, dest in Reference.iter_items(rwrepo, None, True):
98+
if not dest.startswith('ref:'):
99+
assert len(dest) == 40
100+
assert ref.commit == rwrepo.commit(dest)
101+
102+
95103
@with_rw_repo('HEAD', bare=False)
96104
def test_heads(self, rwrepo):
97105
for head in rwrepo.heads:

0 commit comments

Comments
 (0)