git-filter-repo icon indicating copy to clipboard operation
git-filter-repo copied to clipboard

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

Open 13steinj opened this issue 1 year ago • 4 comments

Context on annotated vs lightweight tags.

So, the fuller story here goes:

  • We use GitHub Enterprise internally
  • We use tags that start with v to indicate a release. For example, v1.2.3. Sometimes v1.2.3.4 depending on the use case. Can have as many parts as one wants. Can also have a suffix, as well as a numerical one. E.g. v.1.2.3-suffix is less than v1.2.4-suffix and both of these are incommensurable with
  • Our CI system interacts with GHES via the API to make releases, which makes tags when they don't exist.
    • Apparently, any time this happens / is done on the GHES web UI, it's a lightweight tag. GitHub doesn't care about the above link. ¯\_(ツ)_/¯
  • I want to rename all the tags to have a suffix, since I'm merging repos, and I want to reference the old tag at least if the tag is a "full" (e.g. annotated) tag.
    • I can't use --tag-rename since that only does prefixes (maybe I should do another feature request that expands --tag-rename to a full callback, so one can just f-string rather than be limited to prefixes and suffixes)?
    • I can't use --tag-callback because it only acts upon annotated tags.
      • Even on the annotated tags, changing the ref here only creates a new tag.
    • It would be nice, but not a "dealbreaker", to be able to reference the tag information in --commit-callback if it exists, and add that information to the commit, in the case of lightweight and/or annotated tags (if annotated, would be nice to be able to access more than just the tag-ref-name.

13steinj avatar Aug 03 '24 06:08 13steinj

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()

13steinj avatar Aug 03 '24 06:08 13steinj

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
'

newren avatar Aug 05 '24 17:08 newren

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.

13steinj avatar Aug 05 '24 18:08 13steinj

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.

newren avatar Sep 05 '24 02:09 newren