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

Fix incorrect commit range #75

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion internal/git/tag.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package git

import (
"errors"
"fmt"
"io"
"time"
Expand Down Expand Up @@ -41,12 +42,24 @@ func CommitsBetween(repoPath string, cfg Range) ([]string, error) {
}
}

var sinceTime time.Time
if sinceHash != nil {
c, err := r.CommitObject(*sinceHash)
if err != nil {
return nil, fmt.Errorf("unable to find since git commit=%q: %w", sinceHash, err)
}
sinceTime = c.Committer.When
}

untilHash, err := r.ResolveRevision(plumbing.Revision(cfg.UntilRef))
if err != nil {
return nil, fmt.Errorf("unable to find until git ref=%q: %w", cfg.UntilRef, err)
}

iter, err := r.Log(&git.LogOptions{From: *untilHash})
iter, err := r.Log(&git.LogOptions{
From: *untilHash,
})
// BRANCH ------> 0 ---> Tip of Main
if err != nil {
return nil, fmt.Errorf("unable to find until git log for ref=%q: %w", cfg.UntilRef, err)
}
Expand All @@ -67,6 +80,9 @@ func CommitsBetween(repoPath string, cfg Range) ([]string, error) {
if cfg.IncludeStart {
commits = append(commits, hash)
}
case sinceTime.Before(c.Committer.When):
log.Errorf("found commit=%q before finding commit=%q at time=%q", hash, sinceHash, sinceTime)
retErr = errors.New("the previous release is probably on a different branch")
default:
commits = append(commits, hash)
}
Expand Down
13 changes: 12 additions & 1 deletion internal/git/tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,24 @@ func TestCommitsBetween(t *testing.T) {
},
count: 5,
},
{
name: "include start and end; filter by time",
path: "test-fixtures/repos/tag-range-repo",
config: Range{
SinceRef: "v0.1.1",
UntilRef: "v0.2.0",
IncludeStart: true,
IncludeEnd: true,
},
count: 4,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
actual, err := CommitsBetween(test.path, test.config)
require.NoError(t, err)

// the answer is based off the the current (dynamically created) git log test fixture
// the answer is based off the current (dynamically created) git log test fixture
expected := gitLogRange(t, test.path, test.config.SinceRef, test.config.UntilRef)
require.NotEmpty(t, expected)

Expand Down