未验证 提交 8c972f9d 编写于 作者: P Phodal Huang

fix: use reverse for git history change

上级 947cb814
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
GitLogs GitLogs
``` ```
git log --all --numstat --date=short --pretty="format:[%h] %aN %ad %s" --numstat git log --all --numstat --date=short --pretty="format:[%h] %aN %ad %s" --numstat --reverse
``` ```
Related Projects: [https://github.com/bast/gitink](https://github.com/bast/gitink) Related Projects: [https://github.com/bast/gitink](https://github.com/bast/gitink)
......
...@@ -18,7 +18,6 @@ var currentCommitMessage CommitMessage ...@@ -18,7 +18,6 @@ var currentCommitMessage CommitMessage
var currentFileChanges []FileChange var currentFileChanges []FileChange
var commitMessages []CommitMessage var commitMessages []CommitMessage
var ( var (
rev = `\[([\d|a-f]{5,12})\]` rev = `\[([\d|a-f]{5,12})\]`
author = `(.*?)\s\d{4}-\d{2}-\d{2}` author = `(.*?)\s\d{4}-\d{2}-\d{2}`
...@@ -34,7 +33,7 @@ var ( ...@@ -34,7 +33,7 @@ var (
) )
func BuildCommitMessage() []CommitMessage { func BuildCommitMessage() []CommitMessage {
historyArgs := []string{"log", "--pretty=format:[%h] %aN %ad %s", "--date=short", "--numstat"} historyArgs := []string{"log", "--pretty=format:[%h] %aN %ad %s", "--date=short", "--numstat", "--reverse"}
cmd := exec.Command("git", historyArgs...) cmd := exec.Command("git", historyArgs...)
out, err := cmd.CombinedOutput() out, err := cmd.CombinedOutput()
if err != nil { if err != nil {
...@@ -91,20 +90,21 @@ func GetTeamSummary(messages []CommitMessage) []TeamSummary { ...@@ -91,20 +90,21 @@ func GetTeamSummary(messages []CommitMessage) []TeamSummary {
infos := make(map[string]TeamInformation) infos := make(map[string]TeamInformation)
for _, commitMessage := range messages { for _, commitMessage := range messages {
for _, change := range commitMessage.Changes { for _, change := range commitMessage.Changes {
if moveReg.MatchString(change.File) { fileName := change.File
infos = switchFile(infos, change.File) if moveReg.MatchString(fileName) {
infos, fileName = switchFile(infos, fileName)
} }
if infos[change.File].EntityName == "" { if infos[fileName].EntityName == "" {
authors := make(map[string]string) authors := make(map[string]string)
authors[commitMessage.Author] = commitMessage.Author authors[commitMessage.Author] = commitMessage.Author
revs := make(map[string]string) revs := make(map[string]string)
revs[commitMessage.Rev] = commitMessage.Rev revs[commitMessage.Rev] = commitMessage.Rev
infos[change.File] = *&TeamInformation{change.File, authors, revs} infos[fileName] = *&TeamInformation{fileName, authors, revs}
} else { } else {
infos[change.File].Authors[commitMessage.Author] = commitMessage.Author infos[fileName].Authors[commitMessage.Author] = commitMessage.Author
infos[change.File].Revs[commitMessage.Rev] = commitMessage.Rev infos[fileName].Revs[commitMessage.Rev] = commitMessage.Rev
} }
} }
} }
...@@ -122,22 +122,24 @@ func GetTeamSummary(messages []CommitMessage) []TeamSummary { ...@@ -122,22 +122,24 @@ func GetTeamSummary(messages []CommitMessage) []TeamSummary {
} }
// 反向查询 // 反向查询
func switchFile(infos map[string]TeamInformation, changedFile string) map[string]TeamInformation { func switchFile(infos map[string]TeamInformation, changedFile string) (map[string]TeamInformation, string) {
changed := moveReg.FindStringSubmatch(changedFile) changed := moveReg.FindStringSubmatch(changedFile)
// examples: cmd/{call_graph.go => call.go} // examples: cmd/{call_graph.go => call.go}
if len(changed) >= 5 { if len(changed) >= 5 {
oldFileName := changed[1] + changed[2] + changed[4] oldFileName := changed[1] + changed[2] + changed[4]
newFileName := changed[1] + changed[3] + changed[4] newFileName := changed[1] + changed[3] + changed[4]
fmt.Println(infos, oldFileName, newFileName)
if _, ok := infos[oldFileName]; ok { if _, ok := infos[oldFileName]; ok {
oldInfo := infos[oldFileName] oldInfo := infos[oldFileName]
delete(infos, oldFileName) delete(infos, oldFileName)
oldInfo.EntityName = newFileName
infos[newFileName] = oldInfo infos[newFileName] = oldInfo
changedFile = newFileName
} }
} }
return infos return infos, changedFile
} }
type TopAuthor struct { type TopAuthor struct {
......
...@@ -2,7 +2,6 @@ package test_test ...@@ -2,7 +2,6 @@ package test_test
import ( import (
"coca/core/domain/gitt" "coca/core/domain/gitt"
"fmt"
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
) )
...@@ -30,25 +29,39 @@ var _ = Describe("Git Parser", func() { ...@@ -30,25 +29,39 @@ var _ = Describe("Git Parser", func() {
Context("Test for Move file", func() { Context("Test for Move file", func() {
It("should have a current file move update", func() { It("should have a current file move update", func() {
messages := gitt.BuildMessageByInput(` messages := gitt.BuildMessageByInput(`
[d00f01214b] Phodal Huang 2019-12-19 update files [d00f0124d] Phodal Huang 2019-12-19 update files
0 0 core/domain/bs/BadSmellApp.go
[1d00f0124b] Phodal Huang 2019-12-19 update files
1 1 cmd/bs.go 1 1 cmd/bs.go
0 0 core/adapter/bs/BadSmellApp.go 0 0 core/domain/bs/BadSmellApp.go
[d00f04111b] Phodal Huang 2019-12-18 refactor: move bs to adapter [d00f04111b] Phodal Huang 2019-12-18 refactor: move bs to adapter
1 1 cmd/bs.go 1 1 cmd/bs.go
5 5 core/{domain => adapter}/bs/BadSmellApp.go 5 5 core/{domain => adapter}/bs/BadSmellApp.go
[1d00f0124b] Phodal Huang 2019-12-19 update files [d00f01214b] Phodal Huang 2019-12-19 update files
1 1 cmd/bs.go 1 1 cmd/bs.go
0 0 core/domain/bs/BadSmellApp.go 0 0 core/adapter/bs/BadSmellApp.go
`)
summary := gitt.GetTeamSummary(messages)
Expect(summary[0].EntityName).To(Equal("core/adapter/bs/BadSmellApp.go"))
Expect(summary[1].EntityName).To(Equal("cmd/bs.go"))
Expect(len(summary)).To(Equal(2))
})
It("support for first path change", func() {
messages := gitt.BuildMessageByInput(`
[333] Phodal Huang 2019-12-19 update files
0 0 src/domain/gitt/README.md
[d00f0124d] Phodal Huang 2019-12-19 update files [d00f0124d] Phodal Huang 2019-12-19 update files
0 0 core/domain/bs/BadSmellApp.go 0 0 {src => core}/domain/gitt/README.md
`) `)
summary := gitt.GetTeamSummary(messages) summary := gitt.GetTeamSummary(messages)
fmt.Println(summary) Expect(summary[0].EntityName).To(Equal("core/domain/gitt/README.md"))
Expect(summary[0].EntityName).To(Equal("cmd/bs.go")) Expect(len(summary)).To(Equal(1))
Expect(summary[1].EntityName).To(Equal("core/domain/bs/BadSmellApp.go"))
}) })
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册