navid/veinsforge / commit
bc4e64585748
Navid M <navidmomtahen@gmail.com> · 2026-11-28
parents: d0b6604355dd
Browse files at this commit · Download source
--- a/go.mod+++ b/go.mod@@ -4,6 +4,7 @@ require ( github.com/bytesparadise/libasciidoc v0.8.0+ github.com/google/licensecheck v0.3.1 github.com/microcosm-cc/bluemonday v1.0.27 github.com/sirupsen/logrus v1.7.0 github.com/yuin/goldmark v1.7.4--- a/go.sum+++ b/go.sum@@ -39,6 +39,8 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=+github.com/google/licensecheck v0.3.1 h1:QoxgoDkaeC4nFrtGN1jV7IPmDCHFNIVh54e5hSt6sPs=+github.com/google/licensecheck v0.3.1/go.mod h1:ORkR35t/JjW+emNKtfJDII0zlciG9JgbT7SmsohlHmY= github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE= github.com/google/pprof v0.0.0-20240409012703-83162a5b38cd h1:gbpYu9NMq8jhDVbvlGkMFWCjLFlqqEZjEmObmhUy6Vo= github.com/google/pprof v0.0.0-20240409012703-83162a5b38cd/go.mod h1:kf6iHlnVGwgKolg33glAes7Yg/8iWP8ukqeldJSO7jw=--- /dev/null+++ b/internal/web/license.go@@ -0,0 +1,37 @@+package web++import (+ "strings"++ "github.com/google/licensecheck"+)++func firstLicense(files []string) string {+ for _, want := range []string{+ "license", "licence", "license.md", "licence.md", "license.txt", "licence.txt",+ "copying", "copying.md", "copying.txt", "copyright", "copyright.md", "copyright.txt",+ } {+ for _, file := range files {+ if !strings.Contains(file, "/") && strings.EqualFold(file, want) {+ return file+ }+ }+ }+ return ""+}++func licenseName(data []byte) string {+ coverage := licensecheck.Scan(data)+ best := ""+ bestLength := 0+ for _, match := range coverage.Match {+ if match.ID == "" {+ continue+ }+ if length := match.End - match.Start; length > bestLength {+ best = match.ID+ bestLength = length+ }+ }+ return best+}--- /dev/null+++ b/internal/web/license_test.go@@ -0,0 +1,40 @@+package web++import (+ "testing"+)++const mitLicense = `MIT License++Copyright (c) 2026 Example++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in all+copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE+SOFTWARE.+`++func TestLicenseDetection(t *testing.T) {+ if got := licenseName([]byte(mitLicense)); got != "MIT" {+ t.Fatalf("license = %q, want MIT", got)+ }+ if got := firstLicense([]string{"README.md", "LICENSE"}); got != "LICENSE" {+ t.Fatalf("license file = %q", got)+ }+ if got := firstLicense([]string{"README.md", "src/LICENSE"}); got != "" {+ t.Fatalf("nested license = %q, want empty", got)+ }+}--- a/internal/web/repo_browse.go+++ b/internal/web/repo_browse.go@@ -156,6 +156,13 @@ _ = lines dataMap["Readme"] = RenderRepoDocument(string(f.Data), readme, repo, rev, "") dataMap["ReadmePath"] = readme+ }+ }+ }+ if licenseFile := firstLicense(files); licenseFile != "" {+ if f, err := s.vn.ReadFile(r.Context(), repo.ID, central, rev, licenseFile, 1<<20); err == nil {+ if name := licenseName(f.Data); name != "" {+ dataMap["License"] = name } } }--- a/internal/web/templates/repo.html+++ b/internal/web/templates/repo.html@@ -8,6 +8,7 @@ {{if .Data.LatestTag}} · latest tag <code>{{.Data.LatestTag}}</code>{{end}} {{if .Data.Perms.CanAdmin}} · <a href="{{repoURL .Data.Repo}}/settings">settings</a>{{end}} </p>+{{if .Data.License}}<p class="small license"><span aria-hidden="true">⚖️</span> {{.Data.License}}</p>{{end}} {{with .Data.Languages}} <div class="language-bar" role="img" aria-label="Language breakdown"> <svg viewBox="0 0 100 10" preserveAspectRatio="none" aria-hidden="true" focusable="false">