chglog icon indicating copy to clipboard operation
chglog copied to clipboard

Tags off branch corrupt the log

Open masonkatz opened this issue 1 year ago • 0 comments

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.

masonkatz avatar Jul 15 '22 22:07 masonkatz