internal/control/license.go
50 lines · 1714 bytes
2 symbols in this file
1package control
2
3import (
4 "strings"
5
6 "gitbay.org/gitbay/internal/gitutil"
7)
8
9// licenseNames maps content markers to display names, checked in order.
10var licenseChecks = []struct{ marker, name string }{
11 {"MIT License", "MIT"},
12 {"Permission is hereby granted, free of charge", "MIT"},
13 {"Apache License", "Apache-2.0"},
14 {"GNU AFFERO GENERAL PUBLIC LICENSE", "AGPL-3.0"},
15 {"GNU GENERAL PUBLIC LICENSE", "GPL"},
16 {"GNU LESSER GENERAL PUBLIC LICENSE", "LGPL"},
17 {"Mozilla Public License", "MPL-2.0"},
18 {"BSD 3-Clause", "BSD-3-Clause"},
19 {"BSD 2-Clause", "BSD-2-Clause"},
20 {"Redistribution and use in source and binary forms", "BSD"},
21 {"BSD Zero Clause License", "0BSD"},
22 {"Zero Clause BSD", "0BSD"},
23 // ISC is the 0BSD grant plus the notice-retention clause; match on the
24 // clause so title-less 0BSD files don't read as ISC.
25 {"hereby granted, provided that the above copyright notice", "ISC"},
26 {"Permission to use, copy, modify, and/or distribute", "0BSD"},
27 {"This is free and unencumbered software", "Unlicense"},
28 {"CC0", "CC0"},
29}
30
31// DetectLicense reports the repo's license name from a conventional file
32// at the ref root, or "". It lives here because profile show and the
33// web's repo listings both report it.
34func DetectLicense(dir, ref string) string {
35 for _, name := range []string{"LICENSE", "LICENSE.md", "LICENSE.txt", "COPYING", "UNLICENSE"} {
36 raw, err := gitutil.ReadBlob(dir, ref, name, 2048)
37 if err != nil {
38 continue
39 }
40 // Collapse whitespace so markers match across line wraps.
41 text := strings.Join(strings.Fields(string(raw)), " ")
42 for _, c := range licenseChecks {
43 if strings.Contains(text, c.marker) {
44 return c.name
45 }
46 }
47 return "license"
48 }
49 return ""
50}