Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent distance for non-matching tags #85

Closed
aander80 opened this issue May 3, 2024 · 2 comments
Closed

Inconsistent distance for non-matching tags #85

aander80 opened this issue May 3, 2024 · 2 comments
Labels
bug Something isn't working
Milestone

Comments

@aander80
Copy link

aander80 commented May 3, 2024

Hello,

I have found what I believe to be a bug when calculating the distance in Git when the repo is tagged with a non-matching tag.

To reproduce this issue, run the following commands with dunamai 1.21.0.

cd $(mktemp -d)
git init
git commit -m "Initial commit" --allow-empty
git commit -m Dummy --allow-empty
git commit -m Dummy --allow-empty
dunamai from git # This will print 0.0.0.post3.dev0+<githash>
git tag hello HEAD^
dunamai from git # This will print 0.0.0.post2.dev0+<githash>
git --no-pager log

When HEAD^ is tagged with hello (which dunamai should not react to), the distance calculation changes; the distance is changed from 3 to 2.

From reading the dunamai source code, I gather that if no tags are found, the distance is calculated as the number of commits found in the history including the root commit with the command git rev-list --count HEAD. However, if tags are found but none are matched, then the distance is instead calculated as the number of commits between the root commit and the current commit but excluding the root commit, with the command git rev-list --count {}..HEAD".format(initial_commit). This causes the two dunamai calls above to generate two different versions. I would expect the distance to be the same in the two calls.

In the source code (pasted below), my best guess is that int(msg) should be replaced by int(msg) + 1

if matched_pattern is None:
    distance = 0

    code, msg = _run_cmd("git rev-list --max-parents=0 HEAD", path)
    if msg:
        initial_commit = msg.splitlines()[0].strip()
        code, msg = _run_cmd("git rev-list --count {}..HEAD".format(initial_commit), path)
        distance = int(msg) # Should this be incremented by 1?

As far as I can tell, this is only occurring when there are tags but no matching tags in the repo, otherwise the distance calculation seems to be correct from what I have seen in my (simple) tests.

@mtkennerly mtkennerly added the bug Something isn't working label May 3, 2024
@mtkennerly
Copy link
Owner

Thanks for catching this! It should just use git rev-list --count HEAD like the other cases. I guess I was overthinking when I implemented that 😅

@aander80
Copy link
Author

aander80 commented May 4, 2024

Thanks for the quick fix and the great tool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants