支持Git Log

This commit is contained in:
marvzhang
2020-03-11 14:58:51 +08:00
parent 03d74ddd14
commit 23301a50ad
3 changed files with 93 additions and 25 deletions

View File

@@ -10,7 +10,7 @@ import (
func GetGitRemoteBranches(c *gin.Context) {
url := c.Query("url")
branches, err := services.GetGitRemoteBranches(url)
branches, err := services.GetGitRemoteBranchesPlain(url)
if err != nil {
HandleError(http.StatusInternalServerError, c, err)
return

View File

@@ -44,15 +44,16 @@ type GitTag struct {
}
type GitCommit struct {
Hash string `json:"hash"`
TreeHash string `json:"tree_hash"`
Author string `json:"author"`
Email string `json:"email"`
Message string `json:"message"`
IsHead bool `json:"is_head"`
Ts time.Time `json:"ts"`
Branches []GitBranch `json:"branches"`
Tags []GitTag `json:"tags"`
Hash string `json:"hash"`
TreeHash string `json:"tree_hash"`
Author string `json:"author"`
Email string `json:"email"`
Message string `json:"message"`
IsHead bool `json:"is_head"`
Ts time.Time `json:"ts"`
Branches []GitBranch `json:"branches"`
RemoteBranches []GitBranch `json:"remote_branches"`
Tags []GitTag `json:"tags"`
}
func (g *GitCronScheduler) Start() error {
@@ -137,7 +138,7 @@ func SaveSpiderGitSyncError(s model.Spider, errMsg string) {
}
// 获得Git分支
func GetGitRemoteBranches(url string) (branches []string, err error) {
func GetGitRemoteBranchesPlain(url string) (branches []string, err error) {
var stdout bytes.Buffer
var stderr bytes.Buffer
@@ -418,6 +419,40 @@ func GetHeadHash(repo *git.Repository) string {
return head.Hash().String()
}
func GetGitRemoteBranches(s model.Spider) (branches []GitBranch, err error) {
// 打开 repo
repo, err := git.PlainOpen(s.Src)
if err != nil {
log.Error(err.Error())
debug.PrintStack()
return branches, err
}
iter, err := repo.References()
if err != nil {
log.Error(err.Error())
debug.PrintStack()
return branches, err
}
if err := iter.ForEach(func(reference *plumbing.Reference) error {
if reference.Name().IsRemote() {
log.Infof(reference.Hash().String())
log.Infof(reference.Name().String())
branches = append(branches, GitBranch{
Hash: reference.Hash().String(),
Name: reference.Name().String(),
Label: reference.Name().Short(),
})
}
return nil
}); err != nil {
log.Error(err.Error())
debug.PrintStack()
return branches, err
}
return branches, err
}
func GetGitCommits(s model.Spider) (commits []GitCommit, err error) {
// 打开 repo
repo, err := git.PlainOpen(s.Src)
@@ -429,14 +464,21 @@ func GetGitCommits(s model.Spider) (commits []GitCommit, err error) {
// 获取分支列表
branches, err := GetGitBranches(s)
branchesDict := make(map[string][]GitBranch)
branchesDict := map[string][]GitBranch{}
for _, b := range branches {
branchesDict[b.Hash] = append(branchesDict[b.Hash], b)
}
// 获取分支列表
remoteBranches, err := GetGitRemoteBranches(s)
remoteBranchesDict := map[string][]GitBranch{}
for _, b := range remoteBranches {
remoteBranchesDict[b.Hash] = append(remoteBranchesDict[b.Hash], b)
}
// 获取标签列表
tags, err := GetGitTags(s)
tagsDict := make(map[string][]GitTag)
tagsDict := map[string][]GitTag{}
for _, t := range tags {
tagsDict[t.Hash] = append(tagsDict[t.Hash], t)
}
@@ -454,15 +496,16 @@ func GetGitCommits(s model.Spider) (commits []GitCommit, err error) {
// 遍历日志
if err := iter.ForEach(func(commit *object.Commit) error {
gc := GitCommit{
Hash: commit.Hash.String(),
TreeHash: commit.TreeHash.String(),
Message: commit.Message,
Author: commit.Author.Name,
Email: commit.Author.Email,
Ts: commit.Author.When,
IsHead: commit.Hash.String() == GetHeadHash(repo),
Branches: branchesDict[commit.Hash.String()],
Tags: tagsDict[commit.Hash.String()],
Hash: commit.Hash.String(),
TreeHash: commit.TreeHash.String(),
Message: commit.Message,
Author: commit.Author.Name,
Email: commit.Author.Email,
Ts: commit.Author.When,
IsHead: commit.Hash.String() == GetHeadHash(repo),
Branches: branchesDict[commit.Hash.String()],
RemoteBranches: remoteBranchesDict[commit.Hash.String()],
Tags: tagsDict[commit.Hash.String()],
}
commits = append(commits, gc)
return nil

View File

@@ -162,7 +162,7 @@
v-for="c in commits"
:key="c.hash"
:timestamp="c.ts"
:type="c.is_head ? 'primary' : ''"
:type="getCommitType(c)"
>
<div class="commit">
<div class="row">
@@ -192,6 +192,15 @@
<i class="fa fa-tag"></i>
{{b.label}}
</el-tag>
<el-tag
v-for="b in c.remote_branches"
:key="b.name"
type="info"
size="mini"
>
<i class="fa fa-tag"></i>
{{b.label}}
</el-tag>
<el-tag
v-for="t in c.tags"
:key="t.name"
@@ -277,7 +286,7 @@ export default {
}
} finally {
this.isGitSyncLoading = false
this.updateGit()
await this.updateGit()
await this.$store.dispatch('spider/getSpiderData', this.$route.params.id)
}
this.$st.sendEv('爬虫详情', 'Git 设置', '同步')
@@ -301,7 +310,7 @@ export default {
}
} finally {
this.isGitResetLoading = false
this.updateGit()
await this.updateGit()
}
})
this.$st.sendEv('爬虫详情', 'Git 设置', '点击重置')
@@ -327,6 +336,22 @@ export default {
},
async updateGit () {
this.getCommits()
},
getCommitType (c) {
if (c.is_head) return 'primary'
if (c.branches && c.branches.length) {
if (c.branches.map(d => d.name).includes('master')) {
return 'danger'
} else {
return 'warning'
}
}
if (c.tags && c.tags.length) {
return 'success'
}
if (c.remote_branches && c.remote_branches.length) {
return 'info'
}
}
},
async created () {