Compare commits
2 Commits
1f947e6343
...
edae32dc86
| Author | SHA1 | Date | |
|---|---|---|---|
| edae32dc86 | |||
| e85766f671 |
28
how_to/Change_22.md
Normal file
28
how_to/Change_22.md
Normal 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.
|
||||
@@ -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)
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user