Compare commits
2 Commits
41333f06bc
...
db7d608010
| Author | SHA1 | Date | |
|---|---|---|---|
| db7d608010 | |||
| c9d8b443ed |
14
how_to/Change_26.md
Normal file
14
how_to/Change_26.md
Normal 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`.
|
||||||
@@ -56,6 +56,9 @@ def parse_args():
|
|||||||
tag_parser.add_argument("name")
|
tag_parser.add_argument("name")
|
||||||
tag_parser.add_argument("oid", default="@", type=oid, nargs="?")
|
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()
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
@@ -104,3 +107,9 @@ def checkout(args):
|
|||||||
|
|
||||||
def tag(args):
|
def tag(args):
|
||||||
base.create_tag(args.name, args.oid)
|
base.create_tag(args.name, args.oid)
|
||||||
|
|
||||||
|
|
||||||
|
def k(args):
|
||||||
|
for refname, ref in data.iter_refs():
|
||||||
|
print(refname, ref)
|
||||||
|
# TODO visualize refs
|
||||||
|
|||||||
13
ugit/data.py
13
ugit/data.py
@@ -1,6 +1,7 @@
|
|||||||
from pathlib import Path
|
from pathlib import Path, PurePath
|
||||||
|
|
||||||
import hashlib
|
import hashlib
|
||||||
|
import os
|
||||||
|
|
||||||
GIT_DIR = ".ugit"
|
GIT_DIR = ".ugit"
|
||||||
|
|
||||||
@@ -24,6 +25,16 @@ def get_ref(ref):
|
|||||||
return f.read().strip()
|
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"):
|
def hash_object(data, type_="blob"):
|
||||||
obj = type_.encode() + b"\x00" + data
|
obj = type_.encode() + b"\x00" + data
|
||||||
oid = hashlib.sha1(obj).hexdigest()
|
oid = hashlib.sha1(obj).hexdigest()
|
||||||
|
|||||||
Reference in New Issue
Block a user