Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: google/addlicense
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v1.0.0
Choose a base ref
...
head repository: google/addlicense
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v1.1.0
Choose a head ref
  • 14 commits
  • 8 files changed
  • 9 contributors

Commits on Aug 17, 2021

  1. Copy the full SHA
    944049f View commit details

Commits on Oct 2, 2021

  1. docs: update go install instructions

    Fixes #96
    willnorris committed Oct 2, 2021
    Copy the full SHA
    d1984d0 View commit details

Commits on Oct 28, 2021

  1. Added .pl to the list of supported files (#98)

    * Added .pl to the list of supported files
    
    Includes support for Perl (.pl) file, treated in the same way as shell scripts.
    
    * Adding .pl to the relevant test case
    cboneti authored Oct 28, 2021
    Copy the full SHA
    fc41b40 View commit details

Commits on Nov 15, 2021

  1. refactor: prep for additional licenseHeader logic

    this refactors licenseHeader to make it easier to add additional logic
    to determine prefix, beyond just file extension.
    willnorris committed Nov 15, 2021
    Copy the full SHA
    90da58c View commit details
  2. feat: add support for cmake files

    Co-authored-by: Sergii Baitala <[email protected]>
    willnorris and srgbtl committed Nov 15, 2021
    Copy the full SHA
    2b44b36 View commit details

Commits on Apr 20, 2022

  1. Copy the full SHA
    758687b View commit details

Commits on Apr 21, 2022

  1. chore: fix govet error on trailing newline

    conversation in golang/go#18085, but basically a single trailing newline
    is a govet error, but two are not.
    willnorris committed Apr 21, 2022
    Copy the full SHA
    8cfc1b5 View commit details
  2. chore: bump action versions

    I think this should fix the unicode import error in https://github.com/google/addlicense/actions/runs/2198105433
    willnorris committed Apr 21, 2022
    Copy the full SHA
    13e73a7 View commit details

Commits on Jul 22, 2022

  1. Update README.md

    Previous documented command was providing docker folder mount option to addlicense as arguments... that was incorrect. Folder should be mounted before specifying the docker image to run.
    bryanloz-xilinx authored and willnorris committed Jul 22, 2022
    Copy the full SHA
    d40d757 View commit details

Commits on Jul 30, 2022

  1. Copy the full SHA
    6c3d250 View commit details

Commits on Aug 26, 2022

  1. Only log skipped files in verbose mode

    talal authored and willnorris committed Aug 26, 2022
    Copy the full SHA
    eadb73a View commit details
  2. README: update usage options

    talal authored and willnorris committed Aug 26, 2022
    Copy the full SHA
    09bb508 View commit details
  3. Copy the full SHA
    04bfe4e View commit details

Commits on Oct 11, 2022

  1. Copy the full SHA
    53d978a View commit details
Showing with 171 additions and 50 deletions.
  1. +6 −3 .github/workflows/linter.yaml
  2. +4 −4 .github/workflows/release.yaml
  3. +4 −4 .github/workflows/tests.yaml
  4. +31 −14 README.md
  5. +22 −9 main.go
  6. +11 −1 main_test.go
  7. +4 −4 tmpl.go
  8. +89 −11 tmpl_test.go
9 changes: 6 additions & 3 deletions .github/workflows/linter.yaml
Original file line number Diff line number Diff line change
@@ -10,9 +10,12 @@ jobs:
runs-on: ${{ matrix.platform }}

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}

- name: golangci-lint
uses: golangci/golangci-lint-action@v2
uses: golangci/golangci-lint-action@v3
with:
version: v1.41
version: v1.45
8 changes: 4 additions & 4 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -15,14 +15,14 @@ jobs:
steps:
-
name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0
-
name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v3
with:
go-version: 1.16
go-version: 1.18
-
name: Run GoReleaser
uses: goreleaser/goreleaser-action@5a54d7e660bda43b405e8463261b3d25631ffe86 #v2.7.0
@@ -45,7 +45,7 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Log in to the Container registry
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9 #v1.10.0
8 changes: 4 additions & 4 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
@@ -24,13 +24,13 @@ jobs:
with:
access_token: ${{ github.token }}

- uses: actions/setup-go@v2
- uses: actions/setup-go@v3
with:
go-version: ${{ matrix.go-version }}
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Cache go modules
uses: actions/cache@v2
uses: actions/cache@v3
with:
path: |
~/.cache/go-build
@@ -43,4 +43,4 @@ jobs:

- name: Upload coverage to Codecov
if: ${{ matrix.update-coverage }}
uses: codecov/codecov-action@v2
uses: codecov/codecov-action@v3
45 changes: 31 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -10,41 +10,58 @@ addlicense requires go 1.16 or later.

## install

go get -u github.com/google/addlicense
go install github.com/google/addlicense@latest

## usage

addlicense [flags] pattern [pattern ...]

-c copyright holder (defaults to "Google LLC")
-f custom license file (no default)
-l license type: apache, bsd, mit, mpl (defaults to "apache")
-y year (defaults to current year)
-check check only mode: verify presence of license headers and exit with non-zero code if missing
-c copyright holder (default "Google LLC")
-check check only mode: verify presence of license headers and exit with non-zero code if missing
-f license file
-ignore file patterns to ignore, for example: -ignore **/*.go -ignore vendor/**
-l license type: apache, bsd, mit, mpl (default "apache")
-s Include SPDX identifier in license header. Set -s=only to only include SPDX identifier.
-v verbose mode: print the name of the files that are modified
-y copyright year(s) (default "2022")

The pattern argument can be provided multiple times, and may also refer
to single files.
to single files. Directories are processed recursively.

For example, to run addlicense across everything in the current directory and
all subdirectories:

addlicense .

The `-ignore` flag can use any pattern [supported by
doublestar](https://github.com/bmatcuk/doublestar#patterns).

## Running in a Docker Container

- Clone the repository using `git clone https://github.com/google/addlicense.git`
- Build your docker container
The simplest way to get the addlicense docker image is to pull from GitHub
Container Registry:

```bash
docker build -t google/addlicense .
docker pull ghcr.io/google/addlicense:latest
```

- Test the image
Alternately, you can build it from source yourself:

```bash
docker run -it google/addlicense -h
docker build -t ghcr.io/google/addlicense .
```

- Usage example
Once you have the image, you can test that it works by running:

```bash
docker run -it ghcr.io/google/addlicense -h
```

Finally, to run it, mount the directory you want to scan to `/src` and pass the
appropriate addlicense flags:

```bash
docker run -v ${PWD}:/src -it google/addlicense -c "Google LLC" *.go
docker run -it -v ${PWD}:/src ghcr.io/google/addlicense -c "Google LLC" *.go
```

## license
31 changes: 22 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
@@ -46,6 +46,7 @@ The pattern argument can be provided multiple times, and may also refer
to single files.
Flags:
`

var (
@@ -57,13 +58,13 @@ var (
license = flag.String("l", "apache", "license type: apache, bsd, mit, mpl")
licensef = flag.String("f", "", "license file")
year = flag.String("y", fmt.Sprint(time.Now().Year()), "copyright year(s)")
verbose = flag.Bool("v", false, "verbose mode: print the name of the files that are modified")
verbose = flag.Bool("v", false, "verbose mode: print the name of the files that are modified or were skipped")
checkonly = flag.Bool("check", false, "check only mode: verify presence of license headers and exit with non-zero code if missing")
)

func init() {
flag.Usage = func() {
fmt.Fprintln(os.Stderr, helpText)
fmt.Fprint(os.Stderr, helpText)
flag.PrintDefaults()
}
flag.Var(&skipExtensionFlags, "skip", "[deprecated: see -ignore] file extensions to skip, for example: -skip rb -skip go")
@@ -216,7 +217,9 @@ func walk(ch chan<- *file, start string) error {
return nil
}
if fileMatches(path, ignorePatterns) {
log.Printf("skipping: %s", path)
if *verbose {
log.Printf("skipping: %s", path)
}
return nil
}
ch <- &file{path, fi.Mode()}
@@ -277,19 +280,22 @@ func fileHasLicense(path string) (bool, error) {
return hasLicense(b) || isGenerated(b), nil
}

// licenseHeader populates the provided license template with data, and returns
// it with the proper prefix for the file type specified by path. The file does
// not need to actually exist, only its name is used to determine the prefix.
func licenseHeader(path string, tmpl *template.Template, data licenseData) ([]byte, error) {
var lic []byte
var err error
switch fileExtension(path) {
default:
return nil, nil
base := strings.ToLower(filepath.Base(path))

switch fileExtension(base) {
case ".c", ".h", ".gv", ".java", ".scala", ".kt", ".kts":
lic, err = executeTemplate(tmpl, data, "/*", " * ", " */")
case ".js", ".mjs", ".cjs", ".jsx", ".tsx", ".css", ".scss", ".sass", ".tf", ".ts":
lic, err = executeTemplate(tmpl, data, "/**", " * ", " */")
case ".cc", ".cpp", ".cs", ".go", ".hcl", ".hh", ".hpp", ".m", ".mm", ".proto", ".rs", ".swift", ".dart", ".groovy", ".v", ".sv":
lic, err = executeTemplate(tmpl, data, "", "// ", "")
case ".py", ".sh", ".yaml", ".yml", ".dockerfile", "dockerfile", ".rb", "gemfile", ".tcl", ".bzl":
case ".py", ".sh", ".yaml", ".yml", ".dockerfile", "dockerfile", ".rb", "gemfile", ".tcl", ".bzl", ".pl", ".pp", "build":
lic, err = executeTemplate(tmpl, data, "", "# ", "")
case ".el", ".lisp":
lic, err = executeTemplate(tmpl, data, "", ";; ", "")
@@ -303,15 +309,22 @@ func licenseHeader(path string, tmpl *template.Template, data licenseData) ([]by
lic, err = executeTemplate(tmpl, data, "", "// ", "")
case ".ml", ".mli", ".mll", ".mly":
lic, err = executeTemplate(tmpl, data, "(**", " ", "*)")
default:
// handle various cmake files
if base == "cmakelists.txt" || strings.HasSuffix(base, ".cmake.in") || strings.HasSuffix(base, ".cmake") {
lic, err = executeTemplate(tmpl, data, "", "# ", "")
}
}
return lic, err
}

// fileExtension returns the file extension of name, or the full name if there
// is no extension.
func fileExtension(name string) string {
if v := filepath.Ext(name); v != "" {
return strings.ToLower(v)
return v
}
return strings.ToLower(filepath.Base(name))
return name
}

var head = []string{
12 changes: 11 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
@@ -316,7 +316,7 @@ func TestLicenseHeader(t *testing.T) {
"// HYS\n\n",
},
{
[]string{"f.py", "f.sh", "f.yaml", "f.yml", "f.dockerfile", "dockerfile", "f.rb", "gemfile", "f.tcl", "f.bzl"},
[]string{"f.py", "f.sh", "f.yaml", "f.yml", "f.dockerfile", "dockerfile", "f.rb", "gemfile", "f.tcl", "f.bzl", "f.pl", "f.pp", "build"},
"# HYS\n\n",
},
{
@@ -339,6 +339,16 @@ func TestLicenseHeader(t *testing.T) {
[]string{"f.ml", "f.mli", "f.mll", "f.mly"},
"(**\n HYS\n*)\n\n",
},
{
[]string{"cmakelists.txt", "f.cmake", "f.cmake.in"},
"# HYS\n\n",
},

// ensure matches are case insenstive
{
[]string{"F.PY", "DoCkErFiLe"},
"# HYS\n\n",
},
}

for _, tt := range tests {
8 changes: 4 additions & 4 deletions tmpl.go
Original file line number Diff line number Diff line change
@@ -101,7 +101,7 @@ func executeTemplate(t *template.Template, d licenseData, top, mid, bot string)
return out.Bytes(), nil
}

const tmplApache = `Copyright {{.Year}} {{.Holder}}
const tmplApache = `Copyright{{ if .Year }} {{.Year}}{{ end }} {{.Holder}}
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@@ -115,11 +115,11 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.`

const tmplBSD = `Copyright (c) {{.Year}} {{.Holder}} All rights reserved.
const tmplBSD = `Copyright (c){{ if .Year }} {{.Year}}{{ end }} {{.Holder}} All rights reserved.
Use of this source code is governed by a BSD-style
license that can be found in the LICENSE file.`

const tmplMIT = `Copyright (c) {{.Year}} {{.Holder}}
const tmplMIT = `Copyright (c){{ if .Year }} {{.Year}}{{ end }} {{.Holder}}
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
@@ -142,7 +142,7 @@ const tmplMPL = `This Source Code Form is subject to the terms of the Mozilla Pu
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at https://mozilla.org/MPL/2.0/.`

const tmplSPDX = `{{ if and .Year .Holder }}Copyright {{.Year}} {{.Holder}}
const tmplSPDX = `{{ if .Holder }}Copyright{{ if .Year }} {{.Year}}{{ end }} {{.Holder}}
{{ end }}SPDX-License-Identifier: {{.SPDXID}}`

const spdxSuffix = "\n\nSPDX-License-Identifier: {{.SPDXID}}"
Loading