Compare commits

...

2 Commits

Author SHA1 Message Date
edae32dc86 Create the tag ref 2024-04-17 19:33:54 +02:00
e85766f671 Add change 22 instructions 2024-04-17 19:33:24 +02:00
3 changed files with 36 additions and 3 deletions

28
how_to/Change_22.md Normal file
View File

@@ -0,0 +1,28 @@
- tag: Create the tag ref
After we've implemented refs in the previous change, it's time to create a ref
when the user creates a tag.
`create_tag` now calls update_ref with the tag name to actually create the tag.
For namespacing purposes, we'll put all tags under *refs/tags/*. That is, if the
user creates *my-cool-commit* tag, we'll create *refs/tags/my-cool-commit* ref
to point to the desired OID.
Then we'll update *data.py* to handle this "namespaced" ref. Since we can't have
a / in the file name, we'll create directories for it. Now if a ref
*refs/tags/sometag* is created, it will be placed under *.ugit/refs/tags* in a
file named *sometag*.
To verify that this code works, you can run:
```
$ ugit tag test
```
And make sure that the tag points to HEAD:
```
$ cat .ugit/refs/tags/test
$ cat .ugit/HEAD
```
The last two commands should give the same output.

View File

@@ -96,6 +96,8 @@ def commit(message):
return oid
def create_tag(name, oid):
data.update_ref(f"refs/tags/{name}", oid)
def checkout(oid):
commit = get_commit(oid)

View File

@@ -11,13 +11,16 @@ def init():
def update_ref(ref, oid):
with open(f"{GIT_DIR}/{ref}", "w") as f:
ref_path = f"{GIT_DIR}/{ref}"
Path.mkdir(ref_path, exist_ok=True)
with open(ref_path, "w") as f:
f.write(oid)
def get_ref(ref):
if Path.is_file(f"{GIT_DIR}/{ref}"):
with open(f"{GIT_DIR}/HEAD") as f:
ref_path = f"{GIT_DIR}/{ref}"
if Path.is_file(ref_path):
with open(ref_path) as f:
return f.read().strip()