From e644f60718a67ec7249d9d36b2565379224851af Mon Sep 17 00:00:00 2001 From: Craig Gumbley Date: Wed, 20 Apr 2022 12:58:23 +0100 Subject: [PATCH] Fixes root commit reference Prior to this commit, the last tag could potentially be compared with a commit that was not part of the current tree. This caused an invalid comparison. This commit fixes that by using a git log equivalent to get the correct commit history. --- internal/pkg/gitclient/gitclient.go | 42 +++++++++++++++++++---------- internal/pkg/writer/writer.go | 2 +- 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/internal/pkg/gitclient/gitclient.go b/internal/pkg/gitclient/gitclient.go index c303bb2..450f77a 100644 --- a/internal/pkg/gitclient/gitclient.go +++ b/internal/pkg/gitclient/gitclient.go @@ -59,33 +59,47 @@ func (gc *GitClient) GetTags() ([]*Ref, error) { return tags, err } -func (gc *GitClient) GetFirstCommit() (*Ref, error) { - commitObjectss, err := gc.repo.CommitObjects() +func (gc *GitClient) GetLogs(reverse bool) ([]*Ref, error) { + logOpts := git.LogOptions{ + Order: git.LogOrderCommitterTime, + } + + logsRefs, err := gc.repo.Log(&logOpts) if err != nil { return nil, err } - commits := []*object.Commit{} - err = commitObjectss.ForEach(func(commit *object.Commit) error { - commits = append(commits, commit) - return nil - }) + logs := []*Ref{} + err = logsRefs.ForEach(func(c *object.Commit) error { + logs = append(logs, &Ref{ + Sha: c.Hash.String(), + Name: c.Hash.String(), + Date: c.Committer.When, + }) - sort.Slice(commits, func(i, j int) bool { - return commits[i].Committer.When.Before(commits[j].Committer.When) + return nil }) if err != nil { return nil, err } - ref := &Ref{ - Sha: commits[0].Hash.String(), - Name: commits[0].Hash.String(), // Set Name to the commit hash because it is the first commit and doesn't have a name - Date: commits[0].Committer.When, + if reverse { + sort.Slice(logs, func(i, j int) bool { + return logs[i].Date.Before(logs[j].Date) + }) + } + + return logs, nil +} + +func (gc *GitClient) GetFirstCommit() (*Ref, error) { + logs, err := gc.GetLogs(true) + if err != nil { + return nil, err } - return ref, nil + return logs[0], nil } func NewGitClient() (*GitClient, error) { diff --git a/internal/pkg/writer/writer.go b/internal/pkg/writer/writer.go index 057f8a9..6ff699a 100644 --- a/internal/pkg/writer/writer.go +++ b/internal/pkg/writer/writer.go @@ -12,7 +12,7 @@ import ( func Write(changeLog *changelog.ChangeLogProperties) error { var tmplSrc = `# Changelog -All notable changes to this project will be documented in this file. +All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org). {{range .Tags}}