Compare commits
2 Commits
6797bcfabe
...
1f947e6343
Author | SHA1 | Date |
---|---|---|
David Doblas Jiménez | 1f947e6343 | |
David Doblas Jiménez | cb8e744794 |
|
@ -0,0 +1,23 @@
|
|||
- tag: Generalize HEAD to refs
|
||||
|
||||
As part of implementing `tag`, we'll generalize the way we handle HEAD. If you
|
||||
think about it, HEAD and tags are similar. They are both ways for ugit to attach
|
||||
a name to an OID. In case of HEAD, the name is hardcoded by ugit; in case of
|
||||
tags, the name will be provided by the user. It makes sense to handle them
|
||||
similarly in *data.py*.
|
||||
|
||||
In *data.py*, let's extend the function `set_HEAD` and `get_HEAD` to
|
||||
`update_ref` and `get_ref`. "Ref" is a short for reference, and that's the name
|
||||
Git uses. The function will now accept the name of the ref and write/read it as
|
||||
a file under *.ugit* directory. Logically, a ref is a named pointer to an object.
|
||||
|
||||
The important change is in *data.py*. The rest of the changes just rename some
|
||||
functions:
|
||||
|
||||
```
|
||||
- get_HEAD() -> get_ref('HEAD')
|
||||
- set_HEAD(oid) -> update_ref('HEAD', oid)
|
||||
```
|
||||
|
||||
Note that we didn't change any behaviour of ugit here, this is purely
|
||||
refactoring.
|
|
@ -83,7 +83,7 @@ def read_tree(tree_oid):
|
|||
def commit(message):
|
||||
commit = f"tree {write_tree()}\n"
|
||||
|
||||
HEAD = data.get_HEAD()
|
||||
HEAD = data.get_ref("HEAD")
|
||||
if HEAD:
|
||||
commit += f"parent {HEAD}\n"
|
||||
|
||||
|
@ -92,7 +92,7 @@ def commit(message):
|
|||
|
||||
oid = data.hash_object(commit.encode(), "commit")
|
||||
|
||||
data.set_HEAD(oid)
|
||||
data.update_ref("HEAD", oid)
|
||||
|
||||
return oid
|
||||
|
||||
|
@ -100,7 +100,7 @@ def commit(message):
|
|||
def checkout(oid):
|
||||
commit = get_commit(oid)
|
||||
read_tree(commit.tree)
|
||||
data.set_HEAD(oid)
|
||||
data.update_ref("HEAD", oid)
|
||||
|
||||
|
||||
Commit = namedtuple("Commit", ["tree", "parent", "message"])
|
||||
|
|
|
@ -85,7 +85,7 @@ def commit(args):
|
|||
|
||||
|
||||
def log(args):
|
||||
oid = args.oid or data.get_HEAD()
|
||||
oid = args.oid or data.get_ref("HEAD")
|
||||
while oid:
|
||||
commit = base.get_commit(oid)
|
||||
|
||||
|
@ -101,5 +101,5 @@ def checkout(args):
|
|||
|
||||
|
||||
def tag(args):
|
||||
oid = args.oid or data.get_HEAD()
|
||||
oid = args.oid or data.get_ref("HEAD")
|
||||
base.create_tag(args.name, oid)
|
||||
|
|
|
@ -10,13 +10,13 @@ def init():
|
|||
Path.mkdir(f"{GIT_DIR}/objects")
|
||||
|
||||
|
||||
def set_HEAD(oid):
|
||||
with open(f"{GIT_DIR}/HEAD", "w") as f:
|
||||
def update_ref(ref, oid):
|
||||
with open(f"{GIT_DIR}/{ref}", "w") as f:
|
||||
f.write(oid)
|
||||
|
||||
|
||||
def get_HEAD():
|
||||
if Path.is_file(f"{GIT_DIR}/HEAD"):
|
||||
def get_ref(ref):
|
||||
if Path.is_file(f"{GIT_DIR}/{ref}"):
|
||||
with open(f"{GIT_DIR}/HEAD") as f:
|
||||
return f.read().strip()
|
||||
|
||||
|
|
Loading…
Reference in New Issue