Skip to content

Commit

Permalink
fix: initial changelog should include first tag.
Browse files Browse the repository at this point in the history
  • Loading branch information
outofcoffee committed Sep 3, 2023
1 parent a45e7f8 commit 5700e11
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 59 deletions.
20 changes: 7 additions & 13 deletions changelog/changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func RenderCommits(commits *[]vcs.TagCommits, groupIntoSections bool, unreleased
if len(output) > 0 {
output += "\n\n"
}
output += "## [" + versionName + "] - " + tagCommits.Date.Format("2006-01-02") + "\n"
output += "## [" + versionName + "] - " + tagCommits.Date.Format("2006-01-02") + "\n\n"
categorised := convcommits.CategoriseByType(tagCommits.Commits)
if groupIntoSections {
categorised = groupBySection(categorised)
Expand Down Expand Up @@ -116,13 +116,12 @@ func SplitIntoSections(lines []string) (Sections, error) {
}
}
}
if firstH2 == 0 {
return Sections{}, fmt.Errorf("could not find h2 in changelog")
}

var body string
for _, line := range lines[firstH2:] {
body += line + "\n"
if firstH2 > 0 {
for _, line := range lines[firstH2:] {
body += line + "\n"
}
}
sections := Sections{
Boilerplate: boilerplate,
Expand Down Expand Up @@ -175,7 +174,7 @@ func GetUpdatedChangelog(
afterTag string,
unique bool,
) (metadata vcs.ReleaseMetadata, updatedChangelog string) {
commits, err := vcs.FetchCommitsByTag(config, repoPath, beforeTag, afterTag, orderBy, unique)
commits, err := vcs.FetchCommitsByTag(config, repoPath, beforeTag, afterTag, unique)
if err != nil {
panic(fmt.Errorf("failed to fetch commit messages from repo: %s: %v", repoPath, err))
}
Expand Down Expand Up @@ -236,17 +235,12 @@ func InitChangelog(
return "", fmt.Errorf("failed to initialise changelog: %s: %v", changelogFile, err)
}

earliestTag, err := vcs.GetEarliestTag(repoPath, orderBy)
if err != nil {
return "", fmt.Errorf("failed to get earliest tag: %v", err)
}

latestTag, err := vcs.GetLatestTag(repoPath, orderBy)
if err != nil {
return "", fmt.Errorf("failed to get latest tag: %v", err)
}

_, updated := GetUpdatedChangelog(config, changelogFile, orderBy, repoPath, latestTag, earliestTag, unique)
_, updated := GetUpdatedChangelog(config, changelogFile, orderBy, repoPath, latestTag, "", unique)

return updated, nil
}
6 changes: 0 additions & 6 deletions changelog/templates/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,3 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.1.0] - 2023-01-01

### Added

- Initial release.
8 changes: 7 additions & 1 deletion cmd/changelog_generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ func generateChangelog(
if err != nil {
panic(err)
}
_, updated := changelog.GetUpdatedChangelog(config, changelogFile, orderBy, repoPath, "", "", unique)

latestTag, err := vcs.GetLatestTag(repoPath, orderBy)
if err != nil {
panic(err)
}

_, updated := changelog.GetUpdatedChangelog(config, changelogFile, orderBy, repoPath, "", latestTag, unique)
fmt.Println(updated)
}
7 changes: 6 additions & 1 deletion cmd/changelog_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ func updateChangelog(
panic(err)
}

_, updated := changelog.GetUpdatedChangelog(config, changelogFile, orderBy, repoPath, "", "", unique)
latestTag, err := vcs.GetLatestTag(repoPath, orderBy)
if err != nil {
panic(err)
}

_, updated := changelog.GetUpdatedChangelog(config, changelogFile, orderBy, repoPath, "", latestTag, unique)
err = changelog.WriteChangelog(changelogFile, updated)
if err != nil {
panic(fmt.Errorf("failed to update changelog: %w", err))
Expand Down
13 changes: 12 additions & 1 deletion cmd/project_changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,18 @@ func listCommits(
panic(err)
}

commits, err := vcs.FetchCommitsByTag(config, repoPath, "", tag, orderBy, unique)
var afterTag string
if tag == "" {
latestTag, err := vcs.GetLatestTag(repoPath, orderBy)
if err != nil {
panic(err)
}
afterTag = latestTag
} else {
afterTag = tag
}

commits, err := vcs.FetchCommitsByTag(config, repoPath, "", afterTag, unique)
if err != nil {
return "", err
}
Expand Down
7 changes: 6 additions & 1 deletion cmd/project_release.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ func release(
panic(err)
}

metadata, updatedChangelog := changelog.GetUpdatedChangelog(config, changelogFile, orderBy, repoPath, "", "", unique)
latestTag, err := vcs.GetLatestTag(repoPath, orderBy)
if err != nil {
panic(err)
}

metadata, updatedChangelog := changelog.GetUpdatedChangelog(config, changelogFile, orderBy, repoPath, "", latestTag, unique)
version := metadata.NewVersion
if metadata.VPrefix {
version = "v" + version
Expand Down
13 changes: 12 additions & 1 deletion cmd/project_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,18 @@ func printVersion(
panic(err)
}

commits, err := vcs.FetchCommitMessages(config, repoPath, "", tag, orderBy, unique)
var afterTag string
if tag == "" {
latestTag, err := vcs.GetLatestTag(repoPath, orderBy)
if err != nil {
panic(err)
}
afterTag = latestTag
} else {
afterTag = tag
}

commits, err := vcs.FetchCommitMessages(config, repoPath, "", afterTag, unique)
if err != nil {
panic(err)
}
Expand Down
72 changes: 37 additions & 35 deletions vcs/commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,15 @@ const UnreleasedVersionName = "Unreleased"

// FetchCommitMessages returns a slice of commit messages between the given tags.
// If beforeTag is empty, then HEAD is used.
// If afterTag is empty, the most recent tag is used.
// If afterTag is empty, the oldest commit is used.
func FetchCommitMessages(
config cfg.SinceConfig,
repoPath string,
beforeTag string,
afterTag string,
orderBy TagOrderBy,
unique bool,
) ([]string, error) {
commits, err := FetchCommitsByTag(config, repoPath, beforeTag, afterTag, orderBy, unique)
commits, err := FetchCommitsByTag(config, repoPath, beforeTag, afterTag, unique)
if err != nil {
return nil, err
}
Expand All @@ -52,23 +51,14 @@ func FetchCommitMessages(
// FetchCommitsByTag returns a map of commit messages between the given tags.
// The key is the tag metadata, and the value is a slice of commit messages.
// If beforeTag is empty, then HEAD is used.
// If afterTag is empty, the most recent tag is used.
// If afterTag is empty, the oldest commit is used.
func FetchCommitsByTag(
config cfg.SinceConfig,
repoPath string,
beforeTag string,
afterTag string,
orderBy TagOrderBy,
unique bool,
) (*[]TagCommits, error) {
if afterTag == "" {
latestTag, err := GetLatestTag(repoPath, orderBy)
if err != nil {
return nil, err
}
logrus.Debugf("most recent tag: %s", latestTag)
afterTag = latestTag
}
commits, err := fetchCommitsBetween(config, repoPath, beforeTag, afterTag, unique)
if err != nil {
return nil, err
Expand All @@ -92,7 +82,7 @@ func FlattenCommits(tags *[]TagCommits) []string {

// fetchCommitsBetween returns a slice of commit messages between the given tags.
// If beforeTag is empty, then HEAD is used.
// If afterTag is empty, the most recent tag is used.
// If afterTag is empty, the oldest commit is used.
func fetchCommitsBetween(
config cfg.SinceConfig,
repoPath string,
Expand All @@ -110,25 +100,28 @@ func fetchCommitsBetween(
return nil, err
}

var beforeTagCommit *object.Commit
var beforeCommit *object.Commit
if beforeTag != "" {
beforeTagMeta, err := r.Tag(beforeTag)
if err != nil {
return nil, err
}
beforeTagCommit, err = r.CommitObject(beforeTagMeta.Hash())
beforeCommit, err = r.CommitObject(beforeTagMeta.Hash())
if err != nil {
return nil, err
}
}

afterTagMeta, err := r.Tag(afterTag)
if err != nil {
return nil, err
}
afterTagCommit, err := r.CommitObject(afterTagMeta.Hash())
if err != nil {
return nil, err
var afterCommit *object.Commit
if afterTag != "" {
afterTagMeta, err := r.Tag(afterTag)
if err != nil {
return nil, err
}
afterCommit, err = r.CommitObject(afterTagMeta.Hash())
if err != nil {
return nil, err
}
}

allTags, err := listAllTags(r)
Expand All @@ -145,12 +138,24 @@ func fetchCommitsBetween(
Date: time.Now(),
}

var commitMessages []string

appendCurrentTag := func() {
if len(commitMessages) > 0 {
tag := TagCommits{
TagMeta: currentTag,
Commits: commitMessages,
}
tagCommits = append(tagCommits, tag)
commitMessages = nil
}
}

// skip commits until reaching beforeTag
skip := beforeTagCommit != nil
skip := beforeCommit != nil

var commitMessages []string
err = commits.ForEach(func(c *object.Commit) error {
if beforeTagCommit != nil && c.Hash == beforeTagCommit.Hash {
if beforeCommit != nil && c.Hash == beforeCommit.Hash {
skip = false
}
if skip {
Expand All @@ -159,19 +164,12 @@ func fetchCommitsBetween(

tagCommit := allTags[c.Hash.String()]
if tagCommit != nil {
if len(commitMessages) > 0 {
tag := TagCommits{
TagMeta: currentTag,
Commits: commitMessages,
}
tagCommits = append(tagCommits, tag)
commitMessages = nil
}
appendCurrentTag()
currentTag = *tagCommit
}

// stop after appending tag commits for previous tag
if c.Hash == afterTagCommit.Hash {
if afterCommit != nil && c.Hash == afterCommit.Hash {
return storer.ErrStop
}

Expand All @@ -187,6 +185,10 @@ func fetchCommitsBetween(
if err != nil {
return nil, err
}

// final tag
appendCurrentTag()

if unique {
commitMessages = stringutil.Unique(commitMessages)
}
Expand Down
1 change: 1 addition & 0 deletions vcs/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func GetLatestTag(repoPath string, orderBy TagOrderBy) (string, error) {
return "", err
}
latestTag = tag
logrus.Debugf("most recent tag: %s", latestTag)
}
return latestTag, nil
}
Expand Down

0 comments on commit 5700e11

Please sign in to comment.