Files
git-hooks/hooks/updates/notes/deletion.py
Joel Brobecker 9db584955e pass the RefKind and object_type when creating Update objects
As part of determining which Update class it needs to instantiate
for the reference udpate being evaluated, the udpate factory
computes the following information:
  - The kind of reference being updated (branch? notes? tag?)
  - What operation on the branch is being performed (create? delete?
    update?)
  - The type of object the commit targeted by the reference
    (commit? tag?)

In the context of enhancing the git-hooks to allow projects to provide
custom-checks on commits, the first and third items seem like these
could be useful information to pass to those hooks. In order to allow
this, without recomputing that information, this commit enhances
the AbstractUpdate class __init__ method to add those as additional
required paramaters, and then stores them as two new attributes.

As a bonus side-effect of this change, the new_rev_type attribute
is no longer necessary, which allows us to save one external call
to Git.

In the meantime, having these new attributes means that we can use
those to cross-check, within each AbstractUpdate child class'
self_sanity_check method, that the ref_kind and object_type values
correspond to each class' expectation (in other words, we cross-check
that the factory instantiated the correct class).

One other side-effect of that change is that we are no longer calling
the get_object_type function with a null SHA1 anymore. We could
modify the function's implementation to only accept non-null SHA1
revisions, but this would denature the function, in my opinion.
There is a genuine chance that perhaps, one day, we'll need that
again. So, instead, we cover that function by adding a new unit test
instead.

Change-Id: I8fd1ce180a6e17c0401b4dee07b9bc07d2abfdda
TN: T209-005
2020-07-19 19:30:55 -07:00

19 lines
656 B
Python

"""Handling of Git Notes deletion."""
from errors import InvalidUpdate
from updates import AbstractUpdate, RefKind
class NotesDeletion(AbstractUpdate):
"""Update object for Git Notes deletion."""
def self_sanity_check(self):
"""See AbstractUpdate.self_sanity_check."""
assert self.ref_kind == RefKind.notes_ref \
and self.object_type == 'commit'
assert self.ref_name == 'refs/notes/commits'
def validate_ref_update(self):
"""See AbstractUpdate.validate_ref_update."""
# Deleting the Git Notes branch is never allowed.
raise InvalidUpdate('Deleting the Git Notes is not allowed.')