Compare commits

...

2 Commits

Author SHA1 Message Date
7fbf6640f6 Iterate commits and parents 2024-05-16 11:54:52 +02:00
dad9077515 Add change 27 to instructions 2024-05-16 11:54:28 +02:00
2 changed files with 29 additions and 0 deletions

21
how_to/Change_27.md Normal file
View File

@@ -0,0 +1,21 @@
- k: Iterate commits and parents
In addition to printing the refs, we'll also print all OIDs that are reachable
from those refs. We'll create `iter_commits_and_parents`, which is a generator
that returns all commits that it can reach from a given set of OIDs.
Note that `iter_commits_and_parents` will return an OID once, even if it's
reachable from multiple refs. Here, for example:
```
o<----o<----o<----o<----@<----@<----@
^ \ ^
first commit -<--$<----$ refs/tags/tag1
^
refs/tags/tag2
```
We can reach the first commit by following the parents of *tag1* or by following
the parents of *tag2*. Yet if we call `iter_commits_and_parents({tag1, tag2})`,
the first commit will be yielded only once. This property will be useful later.
(Note that nothing is visualized yet, we're preparing for that.)

View File

@@ -110,6 +110,14 @@ def tag(args):
def k(args):
oids = set()
for refname, ref in data.iter_refs():
print(refname, ref)
oids.add(ref)
for oid in base.iter_commits_and_parents(oids):
commit = base.get_commit(oid)
print(oid)
if commit.parent:
print("Parent", commit.parent)
# TODO visualize refs