0
0
mirror of https://github.com/thegeeklab/git-sv.git synced 2024-11-21 22:10:39 +00:00

feat: add range and date at commit log action

related to #7
This commit is contained in:
Beatriz Vieira 2021-01-25 02:51:42 -03:00
parent 02fce48717
commit 41be35e83d
3 changed files with 119 additions and 28 deletions

View File

@ -36,7 +36,7 @@ func nextVersionHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor) f
return fmt.Errorf("error parsing version: %s from describe, message: %v", describe, err)
}
commits, err := git.Log(describe, "")
commits, err := git.Log(sv.NewLogRange(sv.TagRange, describe, ""))
if err != nil {
return fmt.Errorf("error getting git log, message: %v", err)
}
@ -51,11 +51,23 @@ func commitLogHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor) fun
return func(c *cli.Context) error {
var commits []sv.GitCommitLog
var err error
tagFlag := c.String("t")
rangeFlag := c.String("r")
startFlag := c.String("s")
endFlag := c.String("e")
if tagFlag != "" && (rangeFlag != "" || startFlag != "" || endFlag != "") {
return fmt.Errorf("cannot define tag flag with range, start or end flags")
}
if tag := c.String("t"); tag != "" {
commits, err = getTagCommits(git, tag)
if tagFlag != "" {
commits, err = getTagCommits(git, tagFlag)
} else {
commits, err = git.Log(git.Describe(), "")
// commits, err = git.Log(sv.NewLogRange(sv.TagRange, git.Describe(), ""))
r, rerr := logRange(git, rangeFlag, startFlag, endFlag)
if rerr != nil {
return rerr
}
commits, err = git.Log(r)
}
if err != nil {
return fmt.Errorf("error getting git log, message: %v", err)
@ -77,7 +89,20 @@ func getTagCommits(git sv.Git, tag string) ([]sv.GitCommitLog, error) {
if err != nil {
return nil, err
}
return git.Log(prev, tag)
return git.Log(sv.NewLogRange(sv.TagRange, prev, tag))
}
func logRange(git sv.Git, rangeFlag, startFlag, endFlag string) (sv.LogRange, error) {
switch rangeFlag {
case "", string(sv.TagRange):
return sv.NewLogRange(sv.TagRange, str(startFlag, git.Describe()), endFlag), nil
case string(sv.DateRange):
return sv.NewLogRange(sv.DateRange, startFlag, endFlag), nil
case string(sv.HashRange):
return sv.NewLogRange(sv.HashRange, startFlag, endFlag), nil
default:
return sv.LogRange{}, fmt.Errorf("invalid range: %s, expected: %s, %s or %s", rangeFlag, sv.TagRange, sv.DateRange, sv.HashRange)
}
}
func releaseNotesHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor, rnProcessor sv.ReleaseNoteProcessor, outputFormatter sv.OutputFormatter) func(c *cli.Context) error {
@ -114,7 +139,7 @@ func getTagVersionInfo(git sv.Git, semverProcessor sv.SemVerCommitsProcessor, ta
return semver.Version{}, time.Time{}, nil, fmt.Errorf("error listing tags, message: %v", err)
}
commits, err := git.Log(previousTag, tag)
commits, err := git.Log(sv.NewLogRange(sv.TagRange, previousTag, tag))
if err != nil {
return semver.Version{}, time.Time{}, nil, fmt.Errorf("error getting git log from tag: %s, message: %v", tag, err)
}
@ -157,7 +182,7 @@ func getNextVersionInfo(git sv.Git, semverProcessor sv.SemVerCommitsProcessor) (
return semver.Version{}, time.Time{}, nil, fmt.Errorf("error parsing version: %s from describe, message: %v", describe, err)
}
commits, err := git.Log(describe, "")
commits, err := git.Log(sv.NewLogRange(sv.TagRange, describe, ""))
if err != nil {
return semver.Version{}, time.Time{}, nil, fmt.Errorf("error getting git log, message: %v", err)
}
@ -174,7 +199,7 @@ func tagHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor) func(c *c
return fmt.Errorf("error parsing version: %s from describe, message: %v", describe, err)
}
commits, err := git.Log(describe, "")
commits, err := git.Log(sv.NewLogRange(sv.TagRange, describe, ""))
if err != nil {
return fmt.Errorf("error getting git log, message: %v", err)
}
@ -276,7 +301,7 @@ func changelogHandler(git sv.Git, semverProcessor sv.SemVerCommitsProcessor, rnP
previousTag = tags[i+1].Name
}
commits, err := git.Log(previousTag, tag.Name)
commits, err := git.Log(sv.NewLogRange(sv.TagRange, previousTag, tag.Name))
if err != nil {
return fmt.Errorf("error getting git log from tag: %s, message: %v", tag.Name, err)
}
@ -348,3 +373,10 @@ func appendOnFile(message, filepath string) error {
_, err = f.WriteString(message)
return err
}
func str(value, defaultValue string) string {
if value != "" {
return value
}
return defaultValue
}

View File

@ -44,7 +44,12 @@ func main() {
Aliases: []string{"cl"},
Usage: "list all commit logs since last version as jsons",
Action: commitLogHandler(git, semverProcessor),
Flags: []cli.Flag{&cli.StringFlag{Name: "t", Aliases: []string{"tag"}, Usage: "get commit log from tag"}},
Flags: []cli.Flag{
&cli.StringFlag{Name: "t", Aliases: []string{"tag"}, Usage: "get commit log from a specific tag"},
&cli.StringFlag{Name: "r", Aliases: []string{"range"}, Usage: "type of range of commits, use: tag, date or hash"},
&cli.StringFlag{Name: "s", Aliases: []string{"start"}, Usage: "start range"},
&cli.StringFlag{Name: "e", Aliases: []string{"end"}, Usage: "end range"},
},
},
{
Name: "release-notes",

View File

@ -26,7 +26,7 @@ const (
// Git commands
type Git interface {
Describe() string
Log(initialTag, endTag string) ([]GitCommitLog, error)
Log(lr LogRange) ([]GitCommitLog, error)
Commit(header, body, footer string) error
Tag(version semver.Version) error
Tags() ([]GitTag, error)
@ -35,6 +35,7 @@ type Git interface {
// GitCommitLog description of a single commit log
type GitCommitLog struct {
Date string `json:"date,omitempty"`
Hash string `json:"hash,omitempty"`
Type string `json:"type,omitempty"`
Scope string `json:"scope,omitempty"`
@ -49,6 +50,28 @@ type GitTag struct {
Date time.Time
}
// LogRangeType type of log range
type LogRangeType string
// constants for log range type
const (
TagRange LogRangeType = "tag"
DateRange = "date"
HashRange = "hash"
)
// LogRange git log range
type LogRange struct {
rangeType LogRangeType
start string
end string
}
// NewLogRange LogRange constructor
func NewLogRange(t LogRangeType, start, end string) LogRange {
return LogRange{rangeType: t, start: start, end: end}
}
// GitImpl git command implementation
type GitImpl struct {
messageMetadata map[string][]string
@ -74,22 +97,27 @@ func (GitImpl) Describe() string {
}
// Log return git log
func (g GitImpl) Log(initialTag, endTag string) ([]GitCommitLog, error) {
format := "--pretty=format:\"%h" + logSeparator + "%s" + logSeparator + "%b" + endLine + "\""
var cmd *exec.Cmd
if initialTag == "" && endTag == "" {
cmd = exec.Command("git", "log", format)
} else if endTag == "" {
cmd = exec.Command("git", "log", initialTag+"..HEAD", format)
} else if initialTag == "" {
cmd = exec.Command("git", "log", endTag, format)
func (g GitImpl) Log(lr LogRange) ([]GitCommitLog, error) {
format := "--pretty=format:\"%ad" + logSeparator + "%h" + logSeparator + "%s" + logSeparator + "%b" + endLine + "\""
params := []string{"log", "--date=short", format}
if lr.start != "" || lr.end != "" {
switch lr.rangeType {
case DateRange:
params = append(params, "--since", lr.start, "--until", addDay(lr.end))
default:
if lr.start == "" {
params = append(params, lr.end)
} else {
cmd = exec.Command("git", "log", initialTag+".."+endTag, format)
params = append(params, lr.start+".."+str(lr.end, "HEAD"))
}
}
}
cmd := exec.Command("git", params...)
out, err := cmd.CombinedOutput()
if err != nil {
return nil, err
return nil, combinedOutputErr(err, out)
}
return parseLogOutput(g.messageMetadata, string(out)), nil
}
@ -121,7 +149,7 @@ func (g GitImpl) Tags() ([]GitTag, error) {
cmd := exec.Command("git", "tag", "-l", "--format", "%(taggerdate:iso8601)#%(refname:short)")
out, err := cmd.CombinedOutput()
if err != nil {
return nil, err
return nil, combinedOutputErr(err, out)
}
return parseTagsOutput(string(out))
}
@ -163,12 +191,12 @@ func parseLogOutput(messageMetadata map[string][]string, log string) []GitCommit
func parseCommitLog(messageMetadata map[string][]string, commit string) GitCommitLog {
content := strings.Split(strings.Trim(commit, "\""), logSeparator)
commitType, scope, subject := parseCommitLogMessage(content[1])
commitType, scope, subject := parseCommitLogMessage(content[2])
metadata := make(map[string]string)
for key, prefixes := range messageMetadata {
for _, prefix := range prefixes {
if tagValue := extractTag(prefix, content[2]); tagValue != "" {
if tagValue := extractTag(prefix, content[3]); tagValue != "" {
metadata[key] = tagValue
break
}
@ -176,11 +204,12 @@ func parseCommitLog(messageMetadata map[string][]string, commit string) GitCommi
}
return GitCommitLog{
Hash: content[0],
Date: content[0],
Hash: content[1],
Type: commitType,
Scope: scope,
Subject: subject,
Body: content[2],
Body: content[3],
Metadata: metadata,
}
}
@ -222,3 +251,28 @@ func splitAt(b []byte) func(data []byte, atEOF bool) (advance int, token []byte,
return 0, nil, nil
}
}
func addDay(value string) string {
if value == "" {
return value
}
t, err := time.Parse("2006-01-02", value)
if err != nil { // keep original value if is not date format
return value
}
return t.AddDate(0, 0, 1).Format("2006-01-02")
}
func str(value, defaultValue string) string {
if value != "" {
return value
}
return defaultValue
}
func combinedOutputErr(err error, out []byte) error {
msg := strings.Split(string(out), "\n")
return fmt.Errorf("%v - %s", err, msg[0])
}