Browse Source

render license info for repo homepage

tags/v1.21.12.1
avadesian 5 years ago
parent
commit
7f317f60f2
2 changed files with 43 additions and 4 deletions
  1. +40
    -3
      routers/repo/view.go
  2. +3
    -1
      templates/repo/home.tmpl

+ 40
- 3
routers/repo/view.go View File

@@ -7,6 +7,7 @@ package repo

import (
"bytes"
"code.gitea.io/gitea/modules/options"
"encoding/base64"
"fmt"
gotemplate "html/template"
@@ -569,8 +570,9 @@ func safeURL(address string) string {

type ContributorInfo struct {
UserInfo *models.User
Email string // for contributor who is not a registered user
Email string // for contributor who is not a registered user
}

// Home render repository home page
func Home(ctx *context.Context) {
if len(ctx.Repo.Units) > 0 {
@@ -579,12 +581,12 @@ func Home(ctx *context.Context) {
if err == nil && contributors != nil {
var contributorInfos []*ContributorInfo
for _, c := range contributors {
user,err := models.GetUserByEmail(c.Email)
user, err := models.GetUserByEmail(c.Email)
if err == nil {
contributorInfos = append(contributorInfos, &ContributorInfo{
user, c.Email,
})
}else{
} else {
contributorInfos = append(contributorInfos, &ContributorInfo{
nil, c.Email,
})
@@ -633,6 +635,39 @@ func Home(ctx *context.Context) {
ctx.NotFound("Home", fmt.Errorf(ctx.Tr("units.error.no_unit_allowed_repo")))
}

func renderLicense(ctx *context.Context) {
entry, err := ctx.Repo.Commit.GetTreeEntryByPath("LICENSE")
if err != nil {
log.Error(err.Error())
return
}
blob := entry.Blob()
dataRc, err := blob.DataAsync()
if err != nil {
log.Error("DataAsync", err)
return
}
defer dataRc.Close()
buf, err := ioutil.ReadAll(dataRc)
if err != nil {
log.Error("DataAsync", err)
return
}
for _, f := range models.Licenses {
license, err := options.License(f)
if err != nil {
log.Error("failed to get license content: %v, err:%v", f, err)
continue
}
if bytes.Compare(buf,license) == 0 {
log.Info("got matched license:%v",f)
ctx.Data["LICENSE"] = f
return
}
}
log.Info("not found matched license,repo:%v",ctx.Repo.Repository.Name)
}

func renderLanguageStats(ctx *context.Context) {
langs, err := ctx.Repo.Repository.GetTopLanguageStats(5)
if err != nil {
@@ -679,6 +714,8 @@ func renderCode(ctx *context.Context) {

// Get Topics of this repo
renderRepoTopics(ctx)
// Get license of this repo
renderLicense(ctx)
if ctx.Written() {
return
}


+ 3
- 1
templates/repo/home.tmpl View File

@@ -170,7 +170,9 @@
<p class="ui">
<i class="clone outline icon"></i>
MIT
{{if .LICENSE}}
{{.LICENSE}}
{{end}}
</p>

<div >


Loading…
Cancel
Save