Save hash object
This commit is contained in:
@@ -18,9 +18,18 @@ def parse_args():
|
|||||||
init_parser = commands.add_parser("init")
|
init_parser = commands.add_parser("init")
|
||||||
init_parser.set_defaults(func=init)
|
init_parser.set_defaults(func=init)
|
||||||
|
|
||||||
|
hash_object_parser = commands.add_parser("hash-object")
|
||||||
|
hash_object_parser.set_defaults(func=hash_object)
|
||||||
|
hash_object_parser.add_argument("file")
|
||||||
|
|
||||||
return parser.parse_args()
|
return parser.parse_args()
|
||||||
|
|
||||||
|
|
||||||
def init(args):
|
def init(args):
|
||||||
data.init()
|
data.init()
|
||||||
print(f"Initialized empty ugit repository in {Path.cwd()}/{data.GIT_DIR}")
|
print(f"Initialized empty ugit repository in {Path.cwd()}/{data.GIT_DIR}")
|
||||||
|
|
||||||
|
|
||||||
|
def hash_object(args):
|
||||||
|
with open(args.file, "rb") as f:
|
||||||
|
print(data.hash_object(f.read()))
|
||||||
|
|||||||
10
ugit/data.py
10
ugit/data.py
@@ -1,7 +1,17 @@
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
import hashlib
|
||||||
|
|
||||||
GIT_DIR = ".ugit"
|
GIT_DIR = ".ugit"
|
||||||
|
|
||||||
|
|
||||||
def init():
|
def init():
|
||||||
Path.mkdir(GIT_DIR)
|
Path.mkdir(GIT_DIR)
|
||||||
|
Path.mkdir(f"{GIT_DIR}/objects")
|
||||||
|
|
||||||
|
|
||||||
|
def hash_object(data):
|
||||||
|
oid = hashlib.sha1(data).hexdigest()
|
||||||
|
with open(f"{GIT_DIR}/objects/{oid}", "wb") as out:
|
||||||
|
out.write(data)
|
||||||
|
return oid
|
||||||
|
|||||||
Reference in New Issue
Block a user