newren / git-filter-repo

Quickly rewrite git repository history (filter-branch replacement)
Other
8.52k stars 708 forks source link

--tag-callback only executes on annotated tags, not lightweight tags #591

Closed 13steinj closed 1 month ago

13steinj commented 3 months ago

Context on annotated vs lightweight tags.

So, the fuller story here goes:

13steinj commented 3 months ago

For now, to be perfectly honest, I'm just hard-code-changing the content of my installed git-filter-repo's _do_tag_rename function to act upon a python format-string, since it is the easiest path forward to get my minimum requirement (all tags renamed with a suffix, annotated tag messages backreferencing the old annotated tag via --tag-callback).

  @staticmethod
  def _do_tag_rename(rename_str, tagname):
    if not tagname.startswith(b'refs/tags/'):
      return tagname
    tagname = tagname.removeprefix(b'refs/tags/').decode()
    return ("refs/tags/" + rename_str.decode().format(tagname=tagname)).encode()
newren commented 3 months ago

Why not use --refname-callback? e.g.

git filter-repo --refname-callback '
  if not refname.startswith(b'refs/tags/'):
    return refname
  # whatever logic you want to rename the tag, including using f-strings and whatnot
'
13steinj commented 3 months ago

I'll try this out. I assume this will work for renaming the lightweight tags, but in general it's still a bit unexpected that the --tag-callback only works on annotated tags and silently ignores the lightweight ones (especially since it appears as though at least GH decided to use lightweight ones instead of annotated ones for releases).

I'm okay with this as a solution as I assume it gets me to where I need, but I think the docs should be updated to reflect that due to compatibility purposes (or whatever), --tag-callback only acts upon annotated tags.

newren commented 2 months ago

Sorry for the delay.

I'll try this out. I assume this will work for renaming the lightweight tags, but in general it's still a bit unexpected that the --tag-callback only works on annotated tags and silently ignores the lightweight ones (especially since it appears as though at least GH decided to use lightweight ones instead of annotated ones for releases).

I'm okay with this as a solution as I assume it gets me to where I need, but I think the docs should be updated to reflect that due to compatibility purposes (or whatever), --tag-callback only acts upon annotated tags.

The docs actually do already cover this:

--tag-callback <function_body>::
       Python code body for processing tag objects

But I see how the distinction between a tag reference and a tag object is a subtle point that will probably be missed by many users. Most aren't that familiar with git's object model of blobs, trees, commits, and tags, and how tag objects include a tagger and tag message and a pointer to the commit in question. Lightweight tags do not point at tag objects, they instead just point straight to a commit object, and as such, we do not have a tagger and tag message to populate the tag object with. Since the tag callback is supposed to operate on a tag object, there simply isn't anything for the tag callback to be called with or on when dealing with a lightweight tag; --tag-callback is all about operating on tag objects so lightweight tags are naturally excluded.

Anyway, I updated the docs. They now have a few extra sentences to make this clear:

--tag-callback <function_body>::
    Python code body for processing tag objects; see <<CALLBACKS>>.
    Note that lightweight tags have no tag object and thus are not
    handled by this callback.  The only thing you really could do with a
    lightweight tag is rename it, but for that you should see
    --refname-callback instead.