Skip to content

Commit

Permalink
Merge f8f4ccb into cb878af
Browse files Browse the repository at this point in the history
  • Loading branch information
wadackel committed May 5, 2018
2 parents cb878af + f8f4ccb commit 6615d12
Show file tree
Hide file tree
Showing 7 changed files with 279 additions and 70 deletions.
63 changes: 56 additions & 7 deletions chglog.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
// Options is an option used to process commits
type Options struct {
Processor Processor
NextTag string // Treat unreleased commits as specified tags (EXPERIMENTAL)
CommitFilters map[string][]string // Filter by using `Commit` properties and values. Filtering is not done by specifying an empty value
CommitSortBy string // Property name to use for sorting `Commit` (e.g. `Scope`)
CommitGroupBy string // Property name of `Commit` to be grouped into `CommitGroup` (e.g. `Type`)
Expand Down Expand Up @@ -150,18 +151,30 @@ func (gen *Generator) Generate(w io.Writer, query string) error {
}

func (gen *Generator) readVersions(tags []*Tag, first string) ([]*Version, error) {
next := gen.config.Options.NextTag
versions := []*Version{}

for i, tag := range tags {
var rev string

if i+1 < len(tags) {
rev = tags[i+1].Name + ".." + tag.Name
var (
isNext = next == tag.Name
rev string
)

if isNext {
if tag.Previous != nil {
rev = tag.Previous.Name + "..HEAD"
} else {
rev = "HEAD"
}
} else {
if first != "" {
rev = first + ".." + tag.Name
if i+1 < len(tags) {
rev = tags[i+1].Name + ".." + tag.Name
} else {
rev = tag.Name
if first != "" {
rev = first + ".." + tag.Name
} else {
rev = tag.Name
}
}
}

Expand All @@ -180,12 +193,21 @@ func (gen *Generator) readVersions(tags []*Tag, first string) ([]*Version, error
RevertCommits: revertCommits,
NoteGroups: noteGroups,
})

// Instead of `getTags()`, assign the date to the tag
if isNext {
tag.Date = commits[0].Author.Date
}
}

return versions, nil
}

func (gen *Generator) readUnreleased(tags []*Tag) (*Unreleased, error) {
if gen.config.Options.NextTag != "" {
return &Unreleased{}, nil
}

rev := "HEAD"

if len(tags) > 0 {
Expand Down Expand Up @@ -216,6 +238,33 @@ func (gen *Generator) getTags(query string) ([]*Tag, string, error) {
return nil, "", err
}

next := gen.config.Options.NextTag
if next != "" {
for _, tag := range tags {
if next == tag.Name {
return nil, "", fmt.Errorf("\"%s\" tag already exists", next)
}
}

var previous *RelateTag
if len(tags) > 0 {
previous = &RelateTag{
Name: tags[0].Name,
Subject: tags[0].Subject,
Date: tags[0].Date,
}
}

// Assign the date with `readVersions()`
tags = append([]*Tag{
&Tag{
Name: next,
Subject: next,
Previous: previous,
},
}, tags...)
}

if len(tags) == 0 {
return nil, "", errors.New("git-tag does not exist")
}
Expand Down
203 changes: 161 additions & 42 deletions chglog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ import (
)

var (
cwd string
testRepoRoot = ".tmp"
cwd string
testRepoRoot = ".tmp"
internalTimeFormat = "2006-01-02 15:04:05"
)

type commitFunc = func(date, subject, body string)
type tagFunc = func(name string)

func TestMain(m *testing.M) {
cwd, _ = os.Getwd()
cleanup()
Expand All @@ -25,9 +29,10 @@ func TestMain(m *testing.M) {
os.Exit(code)
}

func setup(dir string, setupRepo func(gitcmd.Client)) {
func setup(dir string, setupRepo func(commitFunc, tagFunc, gitcmd.Client)) {
testDir := filepath.Join(cwd, testRepoRoot, dir)

os.RemoveAll(testDir)
os.MkdirAll(testDir, os.ModePerm)
os.Chdir(testDir)

Expand All @@ -39,7 +44,21 @@ func setup(dir string, setupRepo func(gitcmd.Client)) {
git.Exec("config", "user.name", "test_user")
git.Exec("config", "user.email", "test@example.com")

setupRepo(git)
var commit = func(date, subject, body string) {
msg := subject
if body != "" {
msg += "\n\n" + body
}
t, _ := time.Parse(internalTimeFormat, date)
d := t.Format("Mon Jan 2 15:04:05 2006 +0000")
git.Exec("commit", "--allow-empty", "--date", d, "-m", msg)
}

var tag = func(name string) {
git.Exec("tag", name)
}

setupRepo(commit, tag, git)

os.Chdir(cwd)
}
Expand All @@ -53,8 +72,8 @@ func TestGeneratorNotFoundTags(t *testing.T) {
assert := assert.New(t)
testName := "not_found"

setup(testName, func(git gitcmd.Client) {
git.Exec("commit", "--allow-empty", "--date", "Mon Jan 1 00:00:00 2018 +0000", "-m", "feat(*): New feature")
setup(testName, func(commit commitFunc, _ tagFunc, _ gitcmd.Client) {
commit("2018-01-01 00:00:00", "feat(*): New feature", "")
})

gen := NewGenerator(&Config{
Expand All @@ -78,9 +97,9 @@ func TestGeneratorNotFoundCommits(t *testing.T) {
assert := assert.New(t)
testName := "not_found"

setup(testName, func(git gitcmd.Client) {
git.Exec("commit", "--allow-empty", "--date", "Mon Jan 1 00:00:00 2018 +0000", "-m", "feat(*): New feature")
git.Exec("tag", "1.0.0")
setup(testName, func(commit commitFunc, tag tagFunc, _ gitcmd.Client) {
commit("2018-01-01 00:00:00", "feat(*): New feature", "")
tag("1.0.0")
})

gen := NewGenerator(&Config{
Expand All @@ -103,9 +122,9 @@ func TestGeneratorNotFoundCommitsOne(t *testing.T) {
assert := assert.New(t)
testName := "not_found"

setup(testName, func(git gitcmd.Client) {
git.Exec("commit", "--allow-empty", "--date", "Mon Jan 1 00:00:00 2018 +0000", "-m", "chore(*): First commit")
git.Exec("tag", "1.0.0")
setup(testName, func(commit commitFunc, tag tagFunc, _ gitcmd.Client) {
commit("2018-01-01 00:00:00", "chore(*): First commit", "")
tag("1.0.0")
})

gen := NewGenerator(&Config{
Expand Down Expand Up @@ -158,24 +177,29 @@ func TestGeneratorWithTypeScopeSubject(t *testing.T) {
assert := assert.New(t)
testName := "type_scope_subject"

setup(testName, func(git gitcmd.Client) {
git.Exec("commit", "--allow-empty", "--date", "Mon Jan 1 00:00:00 2018 +0000", "-m", "chore(*): First commit")
git.Exec("commit", "--allow-empty", "--date", "Mon Jan 1 00:01:00 2018 +0000", "-m", "feat(core): Add foo bar")
git.Exec("commit", "--allow-empty", "--date", "Mon Jan 1 00:02:00 2018 +0000", "-m", "docs(readme): Update usage #123")
setup(testName, func(commit commitFunc, tag tagFunc, _ gitcmd.Client) {
commit("2018-01-01 00:00:00", "chore(*): First commit", "")
commit("2018-01-01 00:01:00", "feat(core): Add foo bar", "")
commit("2018-01-01 00:02:00", "docs(readme): Update usage #123", "")
tag("1.0.0")

git.Exec("tag", "1.0.0")
git.Exec("commit", "--allow-empty", "--date", "Tue Jan 2 00:00:00 2018 +0000", "-m", "feat(parser): New some super options #333")
git.Exec("commit", "--allow-empty", "--date", "Tue Jan 2 00:01:00 2018 +0000", "-m", "Merge pull request #999 from tsuyoshiwada/patch-1")
git.Exec("commit", "--allow-empty", "--date", "Tue Jan 2 00:02:00 2018 +0000", "-m", "Merge pull request #1000 from tsuyoshiwada/patch-1")
git.Exec("commit", "--allow-empty", "--date", "Tue Jan 2 00:03:00 2018 +0000", "-m", "Revert \"feat(core): Add foo bar @mention and issue #987\"")
commit("2018-01-02 00:00:00", "feat(parser): New some super options #333", "")
commit("2018-01-02 00:01:00", "Merge pull request #999 from tsuyoshiwada/patch-1", "")
commit("2018-01-02 00:02:00", "Merge pull request #1000 from tsuyoshiwada/patch-1", "")
commit("2018-01-02 00:03:00", "Revert \"feat(core): Add foo bar @mention and issue #987\"", "")
tag("1.1.0")

git.Exec("tag", "1.1.0")
git.Exec("commit", "--allow-empty", "--date", "Wed Jan 3 00:00:00 2018 +0000", "-m", "feat(context): Online breaking change\n\nBREAKING CHANGE: Online breaking change message.")
git.Exec("commit", "--allow-empty", "--date", "Wed Jan 3 00:01:00 2018 +0000", "-m", "feat(router): Muliple breaking change\n\nThis is body,\n\nBREAKING CHANGE:\nMultiple\nbreaking\nchange message.")
commit("2018-01-03 00:00:00", "feat(context): Online breaking change", "BREAKING CHANGE: Online breaking change message.")
commit("2018-01-03 00:01:00", "feat(router): Muliple breaking change", `This is body,
git.Exec("tag", "2.0.0-beta.0")
git.Exec("commit", "--allow-empty", "--date", "Thu Jan 4 00:00:00 2018 +0000", "-m", "refactor(context): gofmt")
git.Exec("commit", "--allow-empty", "--date", "Thu Jan 4 00:01:00 2018 +0000", "-m", "fix(core): Fix commit\n\nThis is body message.")
BREAKING CHANGE:
Multiple
breaking
change message.`)
tag("2.0.0-beta.0")

commit("2018-01-04 00:00:00", "refactor(context): gofmt", "")
commit("2018-01-04 00:01:00", "fix(core): Fix commit\n\nThis is body message.", "")
})

gen := NewGenerator(&Config{
Expand Down Expand Up @@ -230,13 +254,18 @@ func TestGeneratorWithTypeScopeSubject(t *testing.T) {
err := gen.Generate(buf, "")

assert.Nil(err)
assert.Equal(`<a name="2.0.0-beta.0"></a>
## 2.0.0-beta.0 (2018-01-03)
assert.Equal(`<a name="unreleased"></a>
## [Unreleased]
### Features
### Bug Fixes
- **core:** Fix commit
* **context:** Online breaking change
* **router:** Muliple breaking change
<a name="2.0.0-beta.0"></a>
## [2.0.0-beta.0] - 2018-01-03
### Features
- **context:** Online breaking change
- **router:** Muliple breaking change
### BREAKING CHANGE
Expand All @@ -247,28 +276,118 @@ change message.
Online breaking change message.
<a name="1.1.0"></a>
## 1.1.0 (2018-01-02)
## [1.1.0] - 2018-01-02
### Features
- **parser:** New some super options #333
### Reverts
- feat(core): Add foo bar @mention and issue #987
### Pull Requests
- Merge pull request #1000 from tsuyoshiwada/patch-1
- Merge pull request #999 from tsuyoshiwada/patch-1
<a name="1.0.0"></a>
## 1.0.0 - 2018-01-01
### Features
- **core:** Add foo bar
* **parser:** New some super options #333
### Reverts
[Unreleased]: https://github.com/git-chglog/git-chglog/compare/2.0.0-beta.0...HEAD
[2.0.0-beta.0]: https://github.com/git-chglog/git-chglog/compare/1.1.0...2.0.0-beta.0
[1.1.0]: https://github.com/git-chglog/git-chglog/compare/1.0.0...1.1.0`, strings.TrimSpace(buf.String()))
}

* feat(core): Add foo bar @mention and issue #987
func TestGeneratorWithNextTag(t *testing.T) {
assert := assert.New(t)
testName := "type_scope_subject"

### Pull Requests
setup(testName, func(commit commitFunc, tag tagFunc, _ gitcmd.Client) {
commit("2018-01-01 00:00:00", "feat(core): version 1.0.0", "")
tag("1.0.0")

commit("2018-02-01 00:00:00", "feat(core): version 2.0.0", "")
tag("2.0.0")

commit("2018-03-01 00:00:00", "feat(core): version 3.0.0", "")
})

gen := NewGenerator(&Config{
Bin: "git",
WorkingDir: filepath.Join(testRepoRoot, testName),
Template: filepath.Join(cwd, "testdata", testName+".md"),
Info: &Info{
Title: "CHANGELOG Example",
RepositoryURL: "https://github.com/git-chglog/git-chglog",
},
Options: &Options{
NextTag: "3.0.0",
CommitFilters: map[string][]string{
"Type": []string{
"feat",
},
},
CommitSortBy: "Scope",
CommitGroupBy: "Type",
CommitGroupSortBy: "Title",
CommitGroupTitleMaps: map[string]string{
"feat": "Features",
},
HeaderPattern: "^(\\w*)(?:\\(([\\w\\$\\.\\-\\*\\s]*)\\))?\\:\\s(.*)$",
HeaderPatternMaps: []string{
"Type",
"Scope",
"Subject",
},
},
})

* Merge pull request #1000 from tsuyoshiwada/patch-1
* Merge pull request #999 from tsuyoshiwada/patch-1
buf := &bytes.Buffer{}
err := gen.Generate(buf, "")

assert.Nil(err)
assert.Equal(`<a name="unreleased"></a>
## [Unreleased]
<a name="3.0.0"></a>
## [3.0.0] - 2018-03-01
### Features
- **core:** version 3.0.0
<a name="2.0.0"></a>
## [2.0.0] - 2018-02-01
### Features
- **core:** version 2.0.0
<a name="1.0.0"></a>
## 1.0.0 (2018-01-01)
## 1.0.0 - 2018-01-01
### Features
- **core:** version 1.0.0
[Unreleased]: https://github.com/git-chglog/git-chglog/compare/3.0.0...HEAD
[3.0.0]: https://github.com/git-chglog/git-chglog/compare/2.0.0...3.0.0
[2.0.0]: https://github.com/git-chglog/git-chglog/compare/1.0.0...2.0.0`, strings.TrimSpace(buf.String()))

buf = &bytes.Buffer{}
err = gen.Generate(buf, "3.0.0")

assert.Nil(err)
assert.Equal(`<a name="unreleased"></a>
## [Unreleased]
<a name="3.0.0"></a>
## [3.0.0] - 2018-03-01
### Features
- **core:** version 3.0.0
* **core:** Add foo bar`, strings.TrimSpace(buf.String()))
[Unreleased]: https://github.com/git-chglog/git-chglog/compare/3.0.0...HEAD
[3.0.0]: https://github.com/git-chglog/git-chglog/compare/2.0.0...3.0.0`, strings.TrimSpace(buf.String()))
}
1 change: 1 addition & 0 deletions cmd/git-chglog/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ func (config *Config) Convert(ctx *CLIContext) *chglog.Config {
RepositoryURL: info.RepositoryURL,
},
Options: &chglog.Options{
NextTag: ctx.NextTag,
CommitFilters: opts.Commits.Filters,
CommitSortBy: opts.Commits.SortBy,
CommitGroupBy: opts.CommitGroups.GroupBy,
Expand Down
Loading

0 comments on commit 6615d12

Please sign in to comment.