Implement log
This commit is contained in:
parent
78044a877a
commit
cd91f18da6
27
ugit/base.py
27
ugit/base.py
@ -1,6 +1,10 @@
|
||||
from pathlib import Path, PurePath
|
||||
import itertools
|
||||
import operator
|
||||
import os
|
||||
|
||||
from collections import namedtuple
|
||||
from pathlib import Path, PurePath
|
||||
|
||||
from . import data
|
||||
|
||||
|
||||
@ -93,5 +97,26 @@ def commit(message):
|
||||
return oid
|
||||
|
||||
|
||||
Commit = namedtuple("Commit", ["tree", "parent", "message"])
|
||||
|
||||
|
||||
def get_commit(oid):
|
||||
parent = None
|
||||
|
||||
commit = data.get_object(oid, "commit").decode()
|
||||
lines = iter(commit.splitlines())
|
||||
for line in itertools.takewhile(operator.truth, lines):
|
||||
key, value = line.split(" ", 1)
|
||||
if key == "tree":
|
||||
tree = value
|
||||
elif key == "parent":
|
||||
parent = value
|
||||
else:
|
||||
assert False, f"Unknown field {key}"
|
||||
|
||||
message = "\n".join(lines)
|
||||
return Commit(tree=tree, parent=parent, message=message)
|
||||
|
||||
|
||||
def is_ignored(path):
|
||||
return ".ugit" in path.split("/")
|
||||
|
20
ugit/cli.py
20
ugit/cli.py
@ -1,7 +1,8 @@
|
||||
from pathlib import Path
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
import textwrap
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from . import base
|
||||
from . import data
|
||||
@ -40,6 +41,9 @@ def parse_args():
|
||||
commit_parser.set_defaults(func=commit)
|
||||
commit_parser.add_argument("-m", "--message", required=True)
|
||||
|
||||
log_parser = commands.add_parser("log")
|
||||
log_parser.set_defaults(func=log)
|
||||
|
||||
return parser.parse_args()
|
||||
|
||||
|
||||
@ -68,3 +72,15 @@ def read_tree(args):
|
||||
|
||||
def commit(args):
|
||||
print(base.commit(args.message))
|
||||
|
||||
|
||||
def log(args):
|
||||
oid = data.get_HEAD()
|
||||
while oid:
|
||||
commit = base.get_commit(oid)
|
||||
|
||||
print(f"commit {oid}\n")
|
||||
print(textwrap.indent(commit.message, " "))
|
||||
print("")
|
||||
|
||||
oid = commit.parent
|
||||
|
Loading…
Reference in New Issue
Block a user