Skip to content

Commit b38445e

Browse files
committed
feat: Return git-semver log results in historically descanding order
* Code will fall back to time-based order if history is not fully available
1 parent 8eee48c commit b38445e

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

git_utils/sort_commits_desc.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,17 @@ package git_utils
22

33
import "gopkg.in/src-d/go-git.v4/plumbing/object"
44

5-
type ByCommitTimeDesc []*object.Commit
5+
type ByHistoryDesc []*object.Commit
66

7-
func (a ByCommitTimeDesc) Len() int { return len(a) }
8-
func (a ByCommitTimeDesc) Less(i, j int) bool { return a[i].Committer.When.After(a[j].Committer.When) }
9-
func (a ByCommitTimeDesc) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
7+
func (a ByHistoryDesc) Len() int { return len(a) }
8+
func (a ByHistoryDesc) Less(i, j int) bool {
9+
// Swap i and j as we want to sort descanding
10+
isAncestor, err := a[j].IsAncestor(a[i])
11+
12+
if err != nil {
13+
return a[j].Committer.When.Before(a[i].Committer.When)
14+
}
15+
16+
return isAncestor
17+
}
18+
func (a ByHistoryDesc) Swap(i, j int) { a[i], a[j] = a[j], a[i] }

integration_tests/src/test/java/de/psanetra/gitsemver/LogCmdTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,6 @@ public void shouldPrintLogAsConventionalCommits() {
239239

240240
assertThat(container.exec("git", "semver", "log", "--conventional-commits"))
241241
.isEqualTo("[\n"
242-
+ " {\n"
243-
+ " \"type\": \"feat\",\n"
244-
+ " \"description\": \"Add feature\"\n"
245-
+ " },\n"
246242
+ " {\n"
247243
+ " \"type\": \"fix\",\n"
248244
+ " \"scope\": \"some_component\",\n"
@@ -257,6 +253,10 @@ public void shouldPrintLogAsConventionalCommits() {
257253
+ " \"http://issues.example.com/123\"\n"
258254
+ " ]\n"
259255
+ " }\n"
256+
+ " },\n"
257+
+ " {\n"
258+
+ " \"type\": \"feat\",\n"
259+
+ " \"description\": \"Add feature\"\n"
260260
+ " }\n"
261261
+ "]\n"
262262
);

version_log/version_log.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func VersionLog(options VersionLogOptions) ([]*object.Commit, error) {
111111
commits = append(commits, commit)
112112
}
113113

114-
sort.Sort(git_utils.ByCommitTimeDesc(commits))
114+
sort.Sort(git_utils.ByHistoryDesc(commits))
115115

116116
// Return most recent commits first
117117
return commits, nil

0 commit comments

Comments
 (0)