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

Tags off branch corrupt the log #99

Closed
masonkatz opened this issue Jul 15, 2022 · 0 comments
Closed

Tags off branch corrupt the log #99

masonkatz opened this issue Jul 15, 2022 · 0 comments

Comments

@masonkatz
Copy link
Contributor

masonkatz commented Jul 15, 2022

Summary

Support branches often have tags that do not get merged back to the master/trunk. If one of these tags is older than the most recent tag on the current branch things break.

Steps to Reproduce

mkdir repo; cd repo; git init
for i in 0 2 10; do touch file$i; git add file$i; git commit -m "commit $i"; git tag v1.$i.0; done
git branch support; git checkout support
for i in 11; do touch file$i; git add file$i; git commit -m "commit $i"; git tag v1.$i.0; done
git checkout master
for i in 20; do touch file$i; git add file$i; git commit -m "commit $i"; git tag v1.$i.0; done

Then

~/chglog/chglog init; ~/chglog/chglog format -t rep

Produces

1.20.0
=============
2022-07-15

* commit 20 (71c9aa2e)
* commit 10 (c6093ede)
* commit 2 (d39e052b)
* commit 0 (ac171453)

1.11.0
=============
2022-07-15

* commit 11 (9cfcda51)

1.10.0
=============
2022-07-15

* commit 10 (c6093ede)

1.2.0
=============
2022-07-15

* commit 2 (d39e052b)

1.0.0
=============
2022-07-15

* commit 0 (ac171453)

Commit 1.11.0 is from the support branch so when walking the commits from 1.20.0 back to the previous version it ends up walking all the way back to the first commit.

The following from init.go looks like it is trying to protect against this:

		if commitObject, err = gitRepo.CommitObject(start); err != nil {
			// This ignores objects that are off branch which happens when tagging on multiple branches happens.
			if errors.Is(err, plumbing.ErrObjectNotFound) {
				continue
			}
			return nil, fmt.Errorf("unable to fetch commit from tag %v: %w", tagName, err)
		}

But that is checking if the commit is not in the repo, it is, just on a different branch.

Possible Fix

Walk the Log once with a map of all the tags, and toss out any tags that don't have any commits. Use this as a semver sorted list that we iterate over.

Doing this would produce:

1.20.0
=============
2022-07-15

* commit 20 (71c9aa2e)

1.10.0
=============
2022-07-15

* commit 10 (c6093ede)

1.2.0
=============
2022-07-15

* commit 2 (d39e052b)

1.0.0
=============
2022-07-15

* commit 0 (ac171453)

Code

I've got a solution coded up as both a test and fix. I was thinking of first doing a pull request of just the test to document this better. And then add the potential fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant