Skip to content

Commit

Permalink
feat: support tag ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Nov 23, 2020
1 parent 44ce3cd commit 3d14c9c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 17 deletions.
18 changes: 18 additions & 0 deletions 1_parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package parser
import (
"fmt"
"regexp"
"strconv"
"strings"

"github.com/axetroy/changelog/internal/client"
Expand All @@ -24,6 +25,7 @@ type Scope struct {
var (
regShortHash = regexp.MustCompile(`^[a-z\d]{7}$`)
regLongHash = regexp.MustCompile(`^[a-z\d]{40}$`)
regTag = regexp.MustCompile(`^tag:(\d+)$`)
)

func getCommitFromHEAD(git *client.GitClient) (*object.Commit, error) {
Expand Down Expand Up @@ -69,6 +71,22 @@ func getCommit(git *client.GitClient, version string, isStart bool) (*object.Com
}

return commit, nil
} else if regTag.MatchString(version) {
matcher := regTag.FindStringSubmatch(version)

tagIndex, err := strconv.ParseInt(matcher[1], 10, 10)

if err != nil {
return nil, errors.WithStack(err)
}

tag, err := git.TagN(int(tagIndex))

if err != nil {
return nil, errors.WithStack(err)
}

return tag.Commit, nil
} else if regShortHash.MatchString(version) {
return git.CommitByShort(version)
} else if regLongHash.MatchString(version) {
Expand Down
59 changes: 48 additions & 11 deletions 2_extractor/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ func Extract(g *client.GitClient, scope *parser.Scope) ([]*ExtractSplice, error)

index := 0

splice := &ExtractSplice{
Name: "Unreleased",
}

for {
if index == len(commits) {
break
Expand All @@ -84,19 +80,60 @@ func Extract(g *client.GitClient, scope *parser.Scope) ([]*ExtractSplice, error)
commit := commits[index]

if tag := getTagOfCommit(scope.Tags, commit); tag != nil {
if splice.Commit != nil {
splices = append(splices, splice)
}
splice = &ExtractSplice{
splice := &ExtractSplice{
Name: tag.Name,
Tag: tag,
}
index++
internalLoop:
for {
if index == len(commits) {
break internalLoop
}

nextCommit := commits[index]

if t := getTagOfCommit(scope.Tags, nextCommit); t != nil {
break internalLoop
}

if splice.Commit == nil {
splice.Commit = make([]*object.Commit, 0)
}

splice.Commit = append(splice.Commit, nextCommit)

index++
}

splices = append(splices, splice)
}
} else {
splice := &ExtractSplice{
Name: "Unreleased",
}

internalLoop2:
for {
if index == len(commits) {
break internalLoop2
}

nextCommit := commits[index]

splice.Commit = append(splice.Commit, commit)
if t := getTagOfCommit(scope.Tags, nextCommit); t != nil {
break internalLoop2
}

index++
if splice.Commit == nil {
splice.Commit = make([]*object.Commit, 0)
}

splice.Commit = append(splice.Commit, nextCommit)

index++
}

}
}

return splices, nil
Expand Down
15 changes: 9 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,27 @@ OPTIONS:
--tpl Specify the directory to be generated.
EXAMPLES:
# generate changelog from HEAD to <latest version>
$ changelog
# generate changelog from HEAD to <latest version>. equivalent to 'changelog HEAD~tag:0'
$ changelog
# generate changelog of the specified version
$ changelog v1.2.0
# generate changelog within the specified range
$ changelog v1.3.0~v1.2.0
$ changelog v1.3.0~v1.2.0
# generate changelog from HEAD to <Nth tag>
$ changelog ~tag:0
# generate changelog from <0th tag> to <2th tag>
$ changelog tag:0~tag:2
# generate changelog from HEAD to specified version
$ changelog HEAD~v1.3.0
# generate all changelog
$ changelog HEAD~
# generate all changelog
$ changelog HEAD~
# generate changelog from two commit hashes
$ changelog 770ed02~585445d
Expand Down

0 comments on commit 3d14c9c

Please sign in to comment.