Skip to content
Snippets Groups Projects
Verified Commit 1c89edd8 authored by Volker Schukai's avatar Volker Schukai :alien:
Browse files

fix: the tags are now processed branch-related #15

parent ee709a36
No related branches found
No related tags found
No related merge requests found
...@@ -101,7 +101,6 @@ version predict ...@@ -101,7 +101,6 @@ version predict
**Makefile** **Makefile**
```makefile ```makefile
build: build:
version patch --path $(PROJECT_ROOT)version.json --selector "version" version patch --path $(PROJECT_ROOT)version.json --selector "version"
$(eval VERSION := $(shell cat version.json | jq -r .version)) $(eval VERSION := $(shell cat version.json | jq -r .version))
......
...@@ -264,6 +264,13 @@ func executeCommand() { ...@@ -264,6 +264,13 @@ func executeCommand() {
err = writeVersion(newVersion) err = writeVersion(newVersion)
if err != nil { if err != nil {
if activeCommand.Name == "auto" || activeCommand.Name == "predict" {
if arguments.Auto.ExitCode || arguments.Predict.ExitCode {
os.Exit(10)
}
}
_, err := fmt.Fprintf(os.Stderr, "Error: %s\n", err) _, err := fmt.Fprintf(os.Stderr, "Error: %s\n", err)
if err != nil { if err != nil {
fmt.Printf("Error: %s\n", err) fmt.Printf("Error: %s\n", err)
......
...@@ -90,7 +90,7 @@ func GetCommitType() (CommitType, error) { ...@@ -90,7 +90,7 @@ func GetCommitType() (CommitType, error) {
fmt.Println("tag commit:", tagCommit) fmt.Println("tag commit:", tagCommit)
} }
commitType, err := getCommitTypeSinceTag(tagCommit) commitType, err := findNextTagType(gitRepo, tagCommit, verbosity)
if err != nil { if err != nil {
return OtherCommit, err return OtherCommit, err
} }
...@@ -103,7 +103,7 @@ func GetCommitType() (CommitType, error) { ...@@ -103,7 +103,7 @@ func GetCommitType() (CommitType, error) {
} }
func getLatestSemanticTag() (SemanticVersion, error) { func getLatestSemanticTag() (SemanticVersion, error) {
tagList, err := getSemanticTags() tagList, err := getSemanticTags(gitRepo)
if err != nil { if err != nil {
return SemanticVersion{}, err return SemanticVersion{}, err
} }
...@@ -124,51 +124,66 @@ func getTagCommit(tag string) (*object.Commit, error) { ...@@ -124,51 +124,66 @@ func getTagCommit(tag string) (*object.Commit, error) {
return nil, fmt.Errorf("failed to get tags: %v", err) return nil, fmt.Errorf("failed to get tags: %v", err)
} }
var tagCommit *object.Commit var commitHash *object.Commit
err = tags.ForEach(func(t *plumbing.Reference) error { err = tags.ForEach(func(t *plumbing.Reference) error {
if t.Name().Short() == tag { if t.Name().Short() != tag {
return nil
tagObj, err := r.TagObject(t.Hash())
if err == plumbing.ErrObjectNotFound {
commit, err := r.CommitObject(t.Hash())
if err != nil {
return err
} }
tagCommit = commit
} else if err != nil {
return err
} else {
tagCommit, err = tagObj.Commit() // Resolve the tag to a commit
if err != nil { obj, err := r.TagObject(t.Hash())
return err var hash plumbing.Hash
} if err == nil {
// This is an annotated tag
hash = obj.Target
} else {
// This is a lightweight tag or an error occurred
hash = t.Hash()
} }
commitHash, err = r.CommitObject(hash)
return ErrStopIteration return ErrStopIteration
}
return nil
}) })
if err != nil && err != ErrStopIteration { if err != nil && err != ErrStopIteration {
return nil, fmt.Errorf("failed to iterate over tags: %v", err) return nil, fmt.Errorf("failed to iterate over tags: %v", err)
} }
if tagCommit == nil { if commitHash == nil {
return nil, fmt.Errorf("tag '%s' not found in commit log", tag) return nil, fmt.Errorf("tag '%s' not found in commit log", tag)
} }
return tagCommit, nil return commitHash, nil
} }
func getSemanticTags() ([]SemanticVersion, error) { func getSemanticTags(r *git.Repository) ([]SemanticVersion, error) {
r := gitRepo
tags, err := r.Tags() tags, err := r.Tags()
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to get tags: %v", err) return nil, fmt.Errorf("failed to get tags: %v", err)
} }
headRef, err := r.Head()
if err != nil {
return nil, fmt.Errorf("failed to get branch: %v", err)
}
commitIter, err := r.Log(&git.LogOptions{From: headRef.Hash()})
if err != nil {
return nil, fmt.Errorf("failed to get commit history: %v", err)
}
commitHistory := make(map[plumbing.Hash]bool)
err = commitIter.ForEach(func(c *object.Commit) error {
commitHistory[c.Hash] = true
return nil
})
if err != nil {
return nil, fmt.Errorf("failed to iterate over commits: %v", err)
}
var tagList []SemanticVersion var tagList []SemanticVersion
err = tags.ForEach(func(tag *plumbing.Reference) error { err = tags.ForEach(func(tag *plumbing.Reference) error {
tagName := tag.Name().Short() tagName := tag.Name().Short()
...@@ -177,6 +192,26 @@ func getSemanticTags() ([]SemanticVersion, error) { ...@@ -177,6 +192,26 @@ func getSemanticTags() ([]SemanticVersion, error) {
fmt.Println("found tag: ", tagName) fmt.Println("found tag: ", tagName)
} }
// Resolve the tag to a commit
var commitHash plumbing.Hash
obj, err := r.TagObject(tag.Hash())
if err == nil {
// This is an annotated tag
commitHash = obj.Target
} else {
// This is a lightweight tag or an error occurred
commitHash = tag.Hash()
if verbosity {
fmt.Println("tag " + tagName + " is a lightweight tag, you should use an annotated tag")
}
}
if _, exists := commitHistory[commitHash]; !exists {
return nil
}
if versionRegex.MatchString(tagName) { if versionRegex.MatchString(tagName) {
tagList = append(tagList, ParseSemanticVersion(tagName)) tagList = append(tagList, ParseSemanticVersion(tagName))
} }
...@@ -199,69 +234,45 @@ func getSemanticTags() ([]SemanticVersion, error) { ...@@ -199,69 +234,45 @@ func getSemanticTags() ([]SemanticVersion, error) {
return tagList, nil return tagList, nil
} }
func getCommitTypeSinceTag(tagCommit *object.Commit) (CommitType, error) { func findNextTagType(r *git.Repository, tagCommit *object.Commit, verbosity bool) (CommitType, error) {
r := gitRepo
cIter, err := r.Log(&git.LogOptions{}) cIter, err := r.Log(&git.LogOptions{})
if err != nil { if err != nil {
return OtherCommit, fmt.Errorf("failed to get commit log: %v", err) return OtherCommit, fmt.Errorf("failed to get commit log from tag: %v", err)
} }
var commitType CommitType var commitType CommitType = OtherCommit
found := false
counter := 0 counter := 0
err = cIter.ForEach(func(commit *object.Commit) error { err = cIter.ForEach(func(commit *object.Commit) error {
counter++ counter++
if commit.Hash == tagCommit.Hash { if commit.Hash == tagCommit.Hash {
found = true return storer.ErrStop
if verbosity {
fmt.Println("found tag commit after", counter, "commits")
}
return storer.ErrStop // stop iteration
} }
message := strings.TrimSpace(commit.Message) message := strings.TrimSpace(commit.Message)
if strings.HasPrefix(message, "feat") { if strings.HasPrefix(message, "feat") {
commitType = FeatCommit commitType = FeatCommit
found = true
if verbosity { if verbosity {
fmt.Println("found feat commit after", counter, "commits") fmt.Printf("Found 'feat' commit after %d commits\n", counter)
} }
return storer.ErrStop
} else if strings.HasPrefix(message, "fix") { } else if strings.HasPrefix(message, "fix") {
if commitType < FixCommit {
// if we already found a feat or breaking commit, we don't care about fix commits
if commitType < FixCommit && commitType > OtherCommit {
return nil
}
commitType = FixCommit commitType = FixCommit
if verbosity { if verbosity {
fmt.Println("found fix commit after", counter, "commits") fmt.Printf("Found 'fix' commit after %d commits\n", counter)
}
} }
} else if containsBreakingChangeFooter(message) { } else if containsBreakingChangeFooter(message) {
commitType = BreakingCommit commitType = BreakingCommit
found = true
if verbosity { if verbosity {
fmt.Println("found breaking commit after", counter, "commits") fmt.Printf("Found breaking change after %d commits\n", counter)
} }
return storer.ErrStop
return storer.ErrStop // stop iteration
} }
return nil return nil
}) })
...@@ -269,10 +280,6 @@ func getCommitTypeSinceTag(tagCommit *object.Commit) (CommitType, error) { ...@@ -269,10 +280,6 @@ func getCommitTypeSinceTag(tagCommit *object.Commit) (CommitType, error) {
return OtherCommit, fmt.Errorf("failed to iterate over commit log: %v", err) return OtherCommit, fmt.Errorf("failed to iterate over commit log: %v", err)
} }
if !found {
return OtherCommit, fmt.Errorf("tag commit not found in commit log")
}
return commitType, nil return commitType, nil
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment