Skip to content

Commit

Permalink
Merge pull request #21 from git-chglog/feat/next-tag
Browse files Browse the repository at this point in the history
Add `--next-tag` flag (EXPERIMENTAL)
  • Loading branch information
wadackel committed May 5, 2018
2 parents cb878af + 4046d94 commit e2e3797
Show file tree
Hide file tree
Showing 8 changed files with 301 additions and 72 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ USAGE:

There are the following specification methods for <tag query>.

1. <old>..<new> - Commit contained in <new> tags from <old>.
1. <old>..<new> - Commit contained in <old> tags from <new>.
2. <name>.. - Commit from the <name> to the latest tag.
3. ..<name> - Commit from the oldest tag to <name>.
4. <name> - Commit contained in <name>.
Expand All @@ -163,6 +163,7 @@ OPTIONS:
--init generate the git-chglog configuration file in interactive
--config value, -c value specifies a different configuration file to pick up (default: ".chglog/config.yml")
--output value, -o value output path and filename for the changelogs. If not specified, output to stdout
--next-tag value treat unreleased commits as specified tags (EXPERIMENTAL)
--silent disable stdout output
--no-color disable color output [$NO_COLOR]
--no-emoji disable emoji output [$NO_EMOJI]
Expand Down Expand Up @@ -486,6 +487,25 @@ See godoc [RenderData][doc-render-data] for available variables.
By displaying it on the standard output, it makes it easy to change the contents.
</details>

<details>
<summary>Can I commit CHANGELOG changes before creating tags?</summary>

Yes, it can be solved by using the `--next-tag` flag.

For example, let's say you want to upgrade your project to `2.0.0`.
You can create CHANGELOG containing `2.0.0` as follows.

```bash
$ git-chglog --next-tag 2.0.0 -o CHANGELOG.md
$ git commit -am "release 2.0.0"
$ git tag 2.0.0
```

The point to notice is that before actually creating a tag with git, it is conveying the next version with `--next-tag` :+1:

This is a step that is necessary for project operation in many cases.
</details>




Expand Down
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
Loading

0 comments on commit e2e3797

Please sign in to comment.