Skip to content

Commit

Permalink
feat: refactor multiple version generation
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Nov 27, 2020
1 parent dde4ece commit 65c50c6
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 105 deletions.
181 changes: 90 additions & 91 deletions 1_parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,146 +111,145 @@ func getCommit(git *client.GitClient, version string, isStart bool) (*object.Com
}
}

func Parse(git *client.GitClient, ranges string) ([]*Scope, error) {
ranges = strings.TrimSpace(ranges)
func Parse(git *client.GitClient, rangesSlice []string) ([]*Scope, error) {
var (
startCommit *object.Commit
endCommit *object.Commit
scopeRange = make([]*Scope, 0)
scope = &Scope{}
err error
scopeRange = make([]*Scope, 0)
err error
)

versions := strings.Split(ranges, "~")
if len(rangesSlice) == 0 {
rangesSlice = []string{""}
}

if ranges == "" {
versions[0] = "HEAD"
latestTag, err := git.TagN(0)
for _, ranges := range rangesSlice {
var (
startCommit *object.Commit
endCommit *object.Commit
scope = &Scope{}
)

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

if latestTag != nil {
commit, err := git.GetPrevCommitOfTag(latestTag)
versions := strings.Split(ranges, "~")

if ranges == "" {
versions[0] = "HEAD"
latestTag, err := git.TagN(0)

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

// if there is no prev tag
// tag is the HEAD
if commit == nil {
nextTag, err := git.NextTag(latestTag)
if latestTag != nil {
commit, err := git.GetPrevCommitOfTag(latestTag)

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

if nextTag != nil {
if commit, err := git.GetPrevCommitOfTag(nextTag); err != nil {
// if there is no prev tag
// tag is the HEAD
if commit == nil {
nextTag, err := git.NextTag(latestTag)

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

if nextTag != nil {
if commit, err := git.GetPrevCommitOfTag(nextTag); err != nil {
return nil, errors.WithStack(err)
} else {
versions = append(versions, commit.Hash.String())
}
} else {
versions = append(versions, commit.Hash.String())
versions = append(versions, "")
}
} else {
versions = append(versions, "")
versions = append(versions, commit.Hash.String())
}

} else {
versions = append(versions, commit.Hash.String())
versions = append(versions, "")
}
} else if len(versions) == 1 {
var tag *client.Tag

} else {
versions = append(versions, "")
}
} else if len(versions) == 1 {
var tag *client.Tag
if regTag.MatchString(ranges) {
matcher := regTag.FindStringSubmatch(ranges)

multipleVersions := strings.Split(versions[0], ",")
tagIndex, err := strconv.ParseInt(matcher[1], 10, 10)

if len(multipleVersions) > 1 {
for _, v := range multipleVersions {
if ss, err := Parse(git, v); err != nil {
if err != nil {
return nil, errors.WithStack(err)
}

if tag, err = git.TagN(int(tagIndex)); err != nil {
return nil, errors.WithStack(err)
}
} else {
if tag, err = git.TagName(ranges); err != nil {
return nil, errors.WithStack(err)
} else {
scopeRange = append(scopeRange, ss...)
}
}
return scopeRange, nil
} else if regTag.MatchString(ranges) {
matcher := regTag.FindStringSubmatch(ranges)

tagIndex, err := strconv.ParseInt(matcher[1], 10, 10)
if tag != nil {
commit, err := git.GetLastCommitOfTag(tag)

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

if tag, err = git.TagN(int(tagIndex)); err != nil {
return nil, errors.WithStack(err)
}
} else {
if tag, err = git.TagName(ranges); err != nil {
return nil, errors.WithStack(err)
versions = append(versions, commit.Hash.String())
} else {
versions = append(versions, "")
}
}

if tag != nil {
commit, err := git.GetLastCommitOfTag(tag)

if err != nil {
if startCommit, err = getCommit(git, versions[0], true); err != nil {
return nil, errors.WithStack(err)
} else if startCommit == nil {
return nil, errors.New(fmt.Sprintf("invalid version '%s'", versions[0]))
} else {
scope.Start = Range{Commit: startCommit}
if tag, err := git.GetTagByCommit(startCommit); err != nil {
return nil, errors.WithStack(err)
} else if tag != nil {
scope.Start.Tag = tag
}

versions = append(versions, commit.Hash.String())
} else {
versions = append(versions, "")
}
}

if startCommit, err = getCommit(git, versions[0], true); err != nil {
return nil, errors.WithStack(err)
} else if startCommit == nil {
return nil, errors.New(fmt.Sprintf("invalid version '%s'", versions[0]))
} else {
scope.Start = Range{Commit: startCommit}
if tag, err := git.GetTagByCommit(startCommit); err != nil {
if endCommit, err = getCommit(git, versions[1], false); err != nil {
return nil, errors.WithStack(err)
} else if tag != nil {
scope.Start.Tag = tag
}
}
} else if endCommit == nil {
return nil, errors.New(fmt.Sprintf("invalid version '%s'", versions[1]))
} else {
scope.End = Range{Commit: endCommit}
if tag, err := git.GetTagByCommit(endCommit); err != nil {
return nil, errors.WithStack(err)
} else if tag != nil {
scope.End.Tag = tag

if endCommit, err = getCommit(git, versions[1], false); err != nil {
return nil, errors.WithStack(err)
} else if endCommit == nil {
return nil, errors.New(fmt.Sprintf("invalid version '%s'", versions[1]))
} else {
scope.End = Range{Commit: endCommit}
if tag, err := git.GetTagByCommit(endCommit); err != nil {
return nil, errors.WithStack(err)
} else if tag != nil {
scope.End.Tag = tag
lastCommitOfTag, err := git.GetLastCommitOfTag(tag)

lastCommitOfTag, err := git.GetLastCommitOfTag(tag)
if err != nil {
return nil, errors.WithStack(err)
}

if err != nil {
return nil, errors.WithStack(err)
scope.End.Commit = lastCommitOfTag
}

scope.End.Commit = lastCommitOfTag
}
}

tags, err := git.GetTagRangesByCommit(startCommit, endCommit)
tags, err := git.GetTagRangesByCommit(startCommit, endCommit)

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

scope.Tags = tags
scope.Tags = tags

scopeRange = append(scopeRange, scope)
scopeRange = append(scopeRange, scope)
}

return scopeRange, nil
}
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,18 @@ $ whatchanged --help
whatchanged - a cli to generate changelog from git project

USAGE:
whatchanged [OPTIONS] [version]
whatchanged [OPTIONS] [version...]

ARGUMENTS:
[version] Optional version or version range.
[version...] Optional version or version range.
1.null.
If you do not specify the version, then it will automatically
generate a change log from "HEAD~<latest version>" or
"HEAD~<earliest commit>" or "<latest version>-<last version>"
2.single version. eg. v1.2.0
2.single version. eg. "v1.2.0"
Generate a specific version of the changelog.
3.version range. eg v1.3.0~v1.2.0
3.multiple versions. eg. "v2.0.0 v1.0.0"
4.version range. eg v1.3.0~v1.2.0
Generate changelog within the specified range.
For more details, please check the following examples.

Expand Down Expand Up @@ -77,7 +78,10 @@ EXAMPLES:
# Generate the specified two versions
# Separate by a comma, and only generate these two versions
# the middle version will not be generated
$ whatchanged v2.0.0,v1.0.0
$ whatchanged v2.0.0 v1.0.0

# generate HEAD to latest tag and <Nth tag>
$ whatchanged HEAD~@0 @1 @2

# generate changelog within the specified range
$ whatchanged v1.3.0~v1.2.0
Expand Down
3 changes: 2 additions & 1 deletion cmd/whatchanged-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"net/http"
"os"
"regexp"

"github.com/axetroy/whatchanged"
"github.com/axetroy/whatchanged/option"
Expand Down Expand Up @@ -55,7 +56,7 @@ func handler(w http.ResponseWriter, r *http.Request) {
url := fmt.Sprintf("https://github.com/%s/%s", username, repo)

if err = whatchanged.Generate(url, output, &option.Options{
Version: version,
Version: regexp.MustCompile(`\s+`).Split(version, -1),
Template: template,
Preset: option.Preset(preset),
}); err != nil {
Expand Down
18 changes: 11 additions & 7 deletions cmd/whatchanged/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,18 @@ func printHelp() {
println(`whatchanged - a cli to generate changelog from git project
USAGE:
whatchanged [OPTIONS] [version]
whatchanged [OPTIONS] [version...]
ARGUMENTS:
[version] Optional version or version range.
[version...] Optional version or version range.
1.null.
If you do not specify the version, then it will automatically
generate a change log from "HEAD~<latest version>" or
"HEAD~<earliest commit>" or "<latest version>-<last version>"
2.single version. eg. v1.2.0
2.single version. eg. "v1.2.0"
Generate a specific version of the changelog.
3.version range. eg v1.3.0~v1.2.0
3.multiple versions. eg. "v2.0.0 v1.0.0"
4.version range. eg v1.3.0~v1.2.0
Generate changelog within the specified range.
For more details, please check the following examples.
Expand Down Expand Up @@ -69,7 +70,10 @@ EXAMPLES:
# Generate the specified two versions
# Separate by a comma, and only generate these two versions
# the middle version will not be generated
$ whatchanged v2.0.0,v1.0.0
$ whatchanged v2.0.0 v1.0.0
# generate HEAD to latest tag and <Nth tag>
$ whatchanged HEAD~@0 @1 @2
# generate changelog within the specified range
$ whatchanged v1.3.0~v1.2.0
Expand Down Expand Up @@ -138,7 +142,7 @@ func run() error {
os.Exit(0)
}

version := flag.Arg(0)
ranges := flag.Args()

var output = os.Stdout

Expand All @@ -155,7 +159,7 @@ func run() error {
}

if err := whatchanged.Generate(project, output, &option.Options{
Version: version,
Version: ranges,
Preset: option.Preset(preset),
Format: option.Format(format),
TemplateFile: templateFile,
Expand Down
2 changes: 1 addition & 1 deletion option/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const (
)

type Options struct {
Version string
Version []string
Format Format
Preset Preset // Priority: 1 The higher the level, the more priority
TemplateFile string // Priority: 2
Expand Down

0 comments on commit 65c50c6

Please sign in to comment.