Compare commits

...

2 Commits

Author SHA1 Message Date
David Doblas Jiménez 73eb89d397 Write-tree for listing files 2024-02-26 18:59:58 +01:00
David Doblas Jiménez d666efcbd3 Add change 08 instructions 2024-02-26 18:58:52 +01:00
3 changed files with 46 additions and 0 deletions

26
how_to/Change_08.md Normal file
View File

@ -0,0 +1,26 @@
- write-tree: List files
The next command is `write-tree`. This command will take the current working
directory and store it to the object database. If `hash-object` was for storing
an individual file, then `write-tree` is for storing a whole directory.
Like `hash-object`, `write-tree` is going to give us an OID after it's done and
we'll be able to use the OID in order to retrieve the directory at a later time.
In Git's lingo a "tree" means a directory.
We'll get into the details in later changes, in this change we'll only prepare
the code around the feature:
+ Create a `write-tree` CLI command
+ Create a `write_tree()` function in base module. Why in base module and not
in data module? Because `write_tree()` is not going to write to disk directly
but use the object database provided by data to store the directory. Hence it
belongs to the higher-level base module.
+ Add code to `write_tree()` to print a directory recursively. For now nothing
is written anywhere, but we just coded the boilerplate to recursively scan a
directory.
We continue in the next change.

View File

@ -1 +1,14 @@
from pathlib import Path
from . import data
def write_tree(directory="."):
with Path.iterdir(directory) as it:
for entry in it:
full = f"{directory}/{entry.name}"
if entry.is_file(follow_symlinks=False):
# TODO write the file to object store
print(full)
elif entry.is_dir(follow_symlinks=False):
write_tree(full)

View File

@ -25,6 +25,9 @@ def parse_args():
cat_file_parser.set_defaults(func=cat_file)
cat_file_parser.add_argument("object")
write_tree_parser = commands.add_parser("write-tree")
write_tree_parser.set_defaults(func=write_tree)
hash_object_parser = commands.add_parser("hash-object")
hash_object_parser.set_defaults(func=hash_object)
hash_object_parser.add_argument("file")
@ -45,3 +48,7 @@ def hash_object(args):
def cat_file(args):
sys.stdout.flush()
sys.stdout.buffer.write(data.get_object(args.object), expected=None)
def write_tree(args):
base.write_tree()