Compare commits
2 Commits
b0d8cab498
...
cd91f18da6
Author | SHA1 | Date |
---|---|---|
David Doblas Jiménez | cd91f18da6 | |
David Doblas Jiménez | 78044a877a |
|
@ -0,0 +1,12 @@
|
|||
- log: Implement
|
||||
|
||||
`log` will walk the list of commits and print them.
|
||||
|
||||
We will start by implementing `get_commit()` that will parse a commit object by
|
||||
OID.
|
||||
|
||||
Then in the CLI module we will start from the HEAD commit and walk its parents
|
||||
until we reach a commit without a parent.
|
||||
|
||||
The result is that the entire commit history is printed to the screen once we
|
||||
run `ugit log`.
|
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