Skip to content

simplify tag check logic to revisions #10

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

Merged
merged 11 commits into from
Dec 11, 2021
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/changelog.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Generate Change Log
id: generate_log
run: |
curl -sf https://gobinaries.com/barelyhuman/commitlog | sh
curl -sSL https://bina.egoist.sh/barelyhuman/commitlog | sh
commitlog > CHANGELOG.md
- uses: ncipollo/release-action@v1
with:
Expand Down
22 changes: 22 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Test

on:
- push
- pull_request

jobs:
build_and_test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16

- name: Build
run: go build -v ./...

- name: Test
run: go test -v ./...
4 changes: 3 additions & 1 deletion cmd/commitlog/commitlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ func Run(args []string) {
log.Fatalln(err)
}

changelog, clogErr := clog.CommitLog(*repoPath, *startCommit, *endCommit, *inclusionFlags, *skipClassification)
currentRepository := clog.OpenRepository(*repoPath)

changelog, clogErr := clog.CommitLog(currentRepository, *startCommit, *endCommit, *inclusionFlags, *skipClassification)

if clogErr.Err != nil {
log.Fatal(clogErr.Message, clogErr.Err)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ go 1.13
require (
github.com/AlecAivazis/survey/v2 v2.2.8
github.com/fatih/color v1.12.0
github.com/go-git/go-billy/v5 v5.0.0 // indirect
github.com/go-git/go-git/v5 v5.2.0
)
101 changes: 8 additions & 93 deletions log/gitutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,105 +11,20 @@ import (
"github.com/go-git/go-git/v5/plumbing/object"
)

var (
latestTag *plumbing.Reference
previousTag *plumbing.Reference
)

// GetLatestTagFromRepository - Get the latest Tag reference from the repo
func GetLatestTagFromRepository(repository *git.Repository) (*plumbing.Reference, *plumbing.Reference, error) {
tagRefs, err := repository.Tags()
if err != nil {
return nil, nil, err
}

var latestTagCommit *object.Commit
var latestTagName *plumbing.Reference
var previousTag *plumbing.Reference
var previousTagReturn *plumbing.Reference

err = tagRefs.ForEach(func(tagRef *plumbing.Reference) error {
revision := plumbing.Revision(tagRef.Name())

tagCommitHash, err := repository.ResolveRevision(revision)
if err != nil {
return err
}

commit, err := repository.CommitObject(*tagCommitHash)
func IsHashATag(currentRepository *git.Repository, hash plumbing.Hash) bool {
isTag := false
tags, _ := currentRepository.Tags()
tags.ForEach(func(tagRef *plumbing.Reference) error {
revHash, err := currentRepository.ResolveRevision(plumbing.Revision(tagRef.Name()))
if err != nil {
return err
}

if latestTagCommit == nil {
latestTagCommit = commit
latestTagName = tagRef
previousTagReturn = previousTag
}

if commit.Committer.When.After(latestTagCommit.Committer.When) {
latestTagCommit = commit
latestTagName = tagRef
previousTagReturn = previousTag
if *revHash == hash {
isTag = true
}

previousTag = tagRef

return nil
})

if err != nil {
return nil, nil, err
}

return latestTagName, previousTagReturn, nil
}

// isCommitToNearestTag - go through git revisions to find the latest tag and the nearest next tag
func isCommitToNearestTag(repo *git.Repository, commit *object.Commit) bool {
if latestTag == nil || previousTag == nil {
var err error
latestTag, previousTag, err = GetLatestTagFromRepository(repo)
if err != nil {
log.Fatal("Error getting latest tags from repository")
}
}

ref, err := repo.Head()

if err != nil {
log.Fatal("Unable to get repository HEAD:", err)
}

tillLatest := latestTag != nil && latestTag.Hash().String() != ref.Hash().String()

if err != nil {
log.Fatal("Couldn't get latest tag...", err)
}

if latestTag == nil || previousTag == nil {
return false
}

// Ignore errors as these are to be optionally checked
followedTagReferenceLatest, err := repo.ResolveRevision(plumbing.Revision(latestTag.Name()))

if err != nil {
log.Fatal("Failed to get referenced commit hash for latestTag's revision")
}

followedTagReferencePrev, err := repo.ResolveRevision(plumbing.Revision(previousTag.Name()))

if err != nil {
log.Fatal("Failed to get referenced commit hash for previous's revision")
}

if tillLatest {
return *followedTagReferenceLatest == commit.Hash
}

return *followedTagReferencePrev == commit.Hash

return isTag
}

// normalizeCommit - reduces the commit message to the first line and ignore the description text of the commit
Expand Down
41 changes: 28 additions & 13 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,29 +133,26 @@ func (container *logContainer) canAddToContainer(skip bool) bool {
return true
}

/*
TODO:
- [] if the current start is also a tag then get data till prev tag
- [] add in option to include the description, if the commit has a description
*/

// CommitLog - Generate commit log
func CommitLog(path string, startCommitString string, endCommitString string, inclusionFlags string, skipClassification bool) (string, ErrMessage) {
currentRepository := OpenRepository(path)
func CommitLog(currentRepository *git.Repository, startCommitString string, endCommitString string, inclusionFlags string, skipClassification bool) (string, ErrMessage) {
baseCommitReference, err := currentRepository.Head()
var startHash, endHash *object.Commit
var cIter object.CommitIter

if err != nil {
return "", ErrMessage{"Unable to get repository HEAD:", err}
return "", ErrMessage{"Unable to get repository HEAD, are you sure you are in a git repository? Error:", err}
}

startHash = GetCommitFromString(startCommitString, currentRepository)
endHash = GetCommitFromString(endCommitString, currentRepository)
isHeadTag := false

if startHash != nil {
cIter, err = currentRepository.Log(&git.LogOptions{From: startHash.Hash})
} else {
if IsHashATag(currentRepository, baseCommitReference.Hash()) {
isHeadTag = true
}
cIter, err = currentRepository.Log(&git.LogOptions{From: baseCommitReference.Hash()})
}

Expand All @@ -165,8 +162,26 @@ func CommitLog(path string, startCommitString string, endCommitString string, in

var commits []*object.Commit

var latestTag *object.Commit
var previousTag *object.Commit
tagAssignment := 0

err = cIter.ForEach(func(c *object.Commit) error {
commits = append(commits, c)
if isHeadTag && tagAssignment == 0 && latestTag == nil && c.Hash == baseCommitReference.Hash() {
latestTag = c
tagAssignment += 1

} else if IsHashATag(currentRepository, c.Hash) {
if latestTag == nil && tagAssignment == 0 {
latestTag = c
tagAssignment += 1
}
if previousTag == nil && tagAssignment == 1 {
previousTag = c
tagAssignment += 1
}
}
return nil
})

Expand Down Expand Up @@ -195,15 +210,15 @@ func CommitLog(path string, startCommitString string, endCommitString string, in
key = strings.SplitN(strings.TrimSpace(key), ":", 2)[0]
normalizedHash := c.Hash.String() + " - " + normalizeCommit(c.Message, scopedKey)

logContainer.AddCommit(key, normalizedHash, skipClassification)

if endHash == nil && isCommitToNearestTag(currentRepository, c) {
if endHash == nil && previousTag != nil && previousTag.Hash == c.Hash {
break
} else if endHash != nil && c.Hash == endHash.Hash {
break
}
}

logContainer.AddCommit(key, normalizedHash, skipClassification)

}
return logContainer.ToMarkdown(skipClassification), ErrMessage{}
}

Expand Down
Loading