Skip to content

Commit

Permalink
fix: the incorrect range generated when there are no parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
axetroy committed Nov 27, 2020
1 parent 76eb777 commit bbf648e
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
29 changes: 28 additions & 1 deletion 1_parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,34 @@ func Parse(git *client.GitClient, ranges string) (*Scope, error) {
}

if latestTag != nil {
versions = append(versions, latestTag.Commit.Hash.String())
commit, err := git.GetPrevCommitOfTag(latestTag)

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 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, "")
}
} else {
versions = append(versions, commit.Hash.String())
}

} else {
versions = append(versions, "")
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/whatchanged/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ OPTIONS:
--fmt=md.
EXAMPLES:
# generate changelog from HEAD to <latest version>. equivalent to 'whatchanged HEAD~@0'
# generate changelog from HEAD to <latest version>.
# if HEAD is not the latest tag. then this should be a unreleased version
# otherwise it should be the latest version
$ whatchanged
# generate changelog of the specified version
Expand Down
60 changes: 60 additions & 0 deletions internal/client/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,27 @@ func (g *GitClient) TagN(offset int) (*Tag, error) {
return tag, nil
}

// Get next tag from the specified tag
func (g *GitClient) PrevTag(target *Tag) (*Tag, error) {
tags, err := g.Tags()

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

for index, tag := range tags {
if tag.Hash == target.Hash {
i := index - 1
if i < 0 {
return nil, nil
}
return tags[i], nil
}
}

return nil, nil
}

// Get next tag from the specified tag
func (g *GitClient) NextTag(target *Tag) (*Tag, error) {
tags, err := g.Tags()
Expand Down Expand Up @@ -478,6 +499,45 @@ func (g *GitClient) GetLastCommitOfTag(tag *Tag) (*object.Commit, error) {
}
}

func (g *GitClient) GetPrevCommitOfTag(tag *Tag) (*object.Commit, error) {
prevTag, err := g.PrevTag(tag)

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

options := git.LogOptions{}

if prevTag != nil {
options.From = prevTag.Commit.Hash
}

cIter, err := g.Repository.Log(&options)

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

var prevCommit *object.Commit

for {
if commit, err := cIter.Next(); err == io.EOF {
break
} else if err != nil {
return nil, errors.WithStack(err)
} else if commit == nil {
break
} else {
if commit.Hash.String() == tag.Commit.Hash.String() {
return prevCommit, nil
}
prevCommit = commit
}
}

return prevCommit, nil
}

func (g *GitClient) GetRemote() (*config.RemoteConfig, error) {
repoConfig, err := g.Repository.Config()

Expand Down

0 comments on commit bbf648e

Please sign in to comment.