Compare commits

...

2 Commits

Author SHA1 Message Date
David Doblas Jiménez cd91f18da6 Implement log 2024-03-20 19:43:58 +01:00
David Doblas Jiménez 78044a877a Add change 17 to instructions 2024-03-20 19:43:29 +01:00
3 changed files with 56 additions and 3 deletions

12
how_to/Change_17.md Normal file
View File

@ -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`.

View File

@ -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("/")

View File

@ -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