Compare commits

...

4 Commits

Author SHA1 Message Date
c484b41a89 Don't always derefence ref 2024-07-06 19:50:52 +02:00
fe5ed910a3 Add change 34 to instructions 2024-07-06 19:49:59 +02:00
9c53919802 Dereference refs when reading and writing 2024-06-29 18:48:21 +02:00
30ce8c84e4 Add change 33 to instructions 2024-06-29 18:47:23 +02:00
5 changed files with 47 additions and 7 deletions

17
how_to/Change_33.md Normal file
View File

@@ -0,0 +1,17 @@
data: Dereference refs when reading and writing
Now we'll dereference symbolic refs not only when reading them but also when
writing them.
We'll implement a helper function called `_get_ref_internal` which will return
the path and the value of the last ref pointed by a symbolic ref. In simple words:
- When given a non-symbolic ref, `_get_ref_internal` will return the ref name
and value.
- When given a symbolic ref, `_get_ref_internal` will dereference the ref
recursively, and then return the name of the last (non-symbolic) ref that points
to an OID, plus its value.
Now `update_ref` will use `_get_ref_internal` to know which ref it needs to update.
Additionally, we'll use `_get_ref_internal` in `get_ref`.

15
how_to/Change_34.md Normal file
View File

@@ -0,0 +1,15 @@
- data: Don't always dereference refs (for `ugit k`)
Actually, it's not always desirable to dereference a ref all the way. Sometimes
we would like to know at which ref a symbolic ref points, rather than the final
OID. Or we would like to update a ref directly, rather then updating the last
ref in the chain.
One such usecase is `ugit k`. When visualizing refs it would be nice to see
which ref points to which ref. We will see another usecase soon.
To accomodate this, we will add a `deref` option to `get_ref`, `iter_refs` and
`update_ref`. If they will be called with `deref=False`, they will work on the
raw value of a ref and not dereference any symbolic refs.
Then we will update `k` to use `deref=False`.

View File

@@ -161,7 +161,7 @@ def get_oid(name):
f"refs/heads/{name}",
]
for ref in refs_to_try:
if data.get_ref(ref).value:
if data.get_ref(ref, deref=False).value:
return data.get_ref(ref).value
# Name is SHA1

View File

@@ -121,9 +121,10 @@ def k(args):
dot = "digraph commits {\n"
oids = set()
for refname, ref in data.iter_refs():
for refname, ref in data.iter_refs(deref=False):
dot += f"'{refname}' [shape=note]\n"
dot += f"'{refname}' -> '{ref.value}'\n"
if not ref.symbolic:
oids.add(ref.value)
for oid in base.iter_commits_and_parents(oids):

View File

@@ -16,8 +16,9 @@ def init():
RefValue = namedtuple("RefValue", ["symbolic", "value"])
def update_ref(ref, value):
def update_ref(ref, value, deref=True):
assert not value.symbolic
ref = _get_ref_internal(ref, deref)[0]
ref_path = f"{GIT_DIR}/{ref}"
Path.mkdir(ref_path, exist_ok=True)
with open(ref_path, "w") as f:
@@ -25,16 +26,22 @@ def update_ref(ref, value):
def get_ref(ref):
return _get_ref_internal(ref)[1]
def _get_ref_internal(ref):
ref_path = f"{GIT_DIR}/{ref}"
value = None
if Path.is_file(ref_path):
with open(ref_path) as f:
value = f.read().strip()
if value and value.startswith("ref:"):
return get_ref(value.split(":", 1)[1].strip())
symbolic = bool(value) and value.startswith("ref")
if symbolic:
value = value.split(":", 1)[1].strip()
return _get_ref_internal(value)
return RefValue(symbolic=False, value=value)
return ref, RefValue(symbolic=False, value=value)
def iter_refs():