Compare commits

...

2 Commits

Author SHA1 Message Date
db7d608010 Print refs for k (visualization tool) 2024-05-15 11:01:19 +02:00
c9d8b443ed Add change 26 to instructions 2024-05-15 10:59:54 +02:00
3 changed files with 35 additions and 1 deletions

14
how_to/Change_26.md Normal file
View File

@@ -0,0 +1,14 @@
- k: Print refs
Now that we have refs and a potentially branching commit history, it's a good
idea to create a visualization tool to see all the mess that we've created.
The visualization tool will draw all refs and all the commits pointed by the refs.
Our command to run the tool will be called `ugit k`, similar to `gitk` (which is
a graphical visualization tool for Git).
We'll create a new `k` command in *cli.py*. We'll create `iter_refs` which is a
generator which will iterate on all available refs (it will return HEAD from the
ugit root directory and everything under *.ugit/refs*). As a first step, let's
just print all refs when running `k`.

View File

@@ -56,6 +56,9 @@ def parse_args():
tag_parser.add_argument("name")
tag_parser.add_argument("oid", default="@", type=oid, nargs="?")
k_parser = commands.add_parser("k")
k_parser.set_defaults(func=k)
return parser.parse_args()
@@ -104,3 +107,9 @@ def checkout(args):
def tag(args):
base.create_tag(args.name, args.oid)
def k(args):
for refname, ref in data.iter_refs():
print(refname, ref)
# TODO visualize refs

View File

@@ -1,6 +1,7 @@
from pathlib import Path
from pathlib import Path, PurePath
import hashlib
import os
GIT_DIR = ".ugit"
@@ -24,6 +25,16 @@ def get_ref(ref):
return f.read().strip()
def iter_refs():
refs = ["HEAD"]
for root, _, filenames in Path.walk(f"{GIT_DIR}/refs"):
root = PurePath.relative_to(root, GIT_DIR)
refs.extend(f"{root}/{name}" for name in filenames)
for refname in refs:
yield refname, get_ref(refname)
def hash_object(data, type_="blob"):
obj = type_.encode() + b"\x00" + data
oid = hashlib.sha1(obj).hexdigest()