libgit2 / pygit2

Python bindings for libgit2
https://www.pygit2.org/
Other
1.59k stars 383 forks source link

Cannot create reference with an initial reflog entry message #1061

Closed pmrowla closed 3 years ago

pmrowla commented 3 years ago

In pygit2, none of the create_reference_... methods support the message parameter, so currently the only way to set a reference with a message is via the Reference.set_target() method. However, set_target will not update the reflog if the ref is already pointing to the specified target. This means that there is no way to create a new reference with a reflog message.

In a new git repo containing 2 commits (fab58a5, 48f4093):

$ pip freeze
cached-property==1.5.2
cffi==1.14.4
pycparser==2.20
pygit2==1.4.0

$ python
Python 3.8.2 (default, Nov  4 2020, 21:23:28)
[Clang 12.0.0 (clang-1200.0.32.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygit2
>>> repo = pygit2.repository(".")
>>> ref = repo.references.create("refs/foo/bar", "fab58a555d62a1fbbbd9c8be8159057ade88715d")
>>> [entry.message for entry in ref.log()]
[]
>>> ref.set_target("fab58a555d62a1fbbbd9c8be8159057ade88715d", message="cannot set message for unchanged ref")
>>> [entry.message for entry in ref.log()]
[]
>>> ref.set_target("48f4093a41f255ed8329eb05d7119902c842edea", message="can set message for changed ref")
>>> [entry.message for entry in ref.log()]
['can set message for changed ref']

To workaround this, if I want to create a ref with an initial reflog message, I would need to create the ref so that it points to any commit in my repo besides the intended ref target, and then call set_target() with a reflog message to point it to the correct target afterwards.

libgit2 does support specifying an initial reflog message on ref creation, so ideally pygit2's create_reference(), create_reference_direct(), and create_reference_symbolic() just need to be updated to accept a message parameter.