Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added -e flag to exclude repair of a file containing DO NOT ALTER OR REMOVE COPYRIGHT NOTICES #10

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions src/main/java/org/glassfish/copyright/AbstractCopyright.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public abstract class AbstractCopyright {
"permission notice:\n" +
"\n";

private static final String DONT_ALTER_HEADER = "DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.";

private static final String DEFAULT_CORRECT = "epl-copyright.txt";
private static final String DEFAULT_ALTERNATE = "apache-copyright.txt";
private static final String DEFAULT_BSD = "edl-copyright.txt";
Expand All @@ -93,6 +95,8 @@ public abstract class AbstractCopyright {
private static Pattern bsdpat = Pattern.compile(
"(THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS)"+
"|(SPDX-License-Identifier: BSD-3-Clause)", Pattern.MULTILINE);
// pattern to detect DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
protected static Pattern doNotAlterPattern = Pattern.compile(DONT_ALTER_HEADER);

protected static final String allrights = "All rights reserved.";

Expand Down Expand Up @@ -215,8 +219,17 @@ protected void checkCopyright(File file) throws IOException {
System.out.println(comment);
System.out.println("---");
}
if (c.warn && !c.quiet)
warnCopyright(file, r);
if (c.warn && !c.quiet) {
warnCopyright(file, r);
}

if (c.explicitExclude) {
if (isEditableCopyright(file, r)) {
err(file + ": EXCLUDED FROM REPAIR: contains: " + DONT_ALTER_HEADER);
return;
}
}

} finally {
if (r != null)
r.close();
Expand Down Expand Up @@ -333,6 +346,24 @@ else if (lc == null)
}

/**
* Verifies if the file contains the message:
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* @param file
* @return
*/
protected boolean isEditableCopyright(final File file, BufferedReader in) throws IOException {
String line;
while ((line = in.readLine()) != null) {
Matcher m2 = doNotAlterPattern.matcher(line);
if (m2.find()) {
return false;
}
}
return true;
}

/**
* Does the string match the pattern?
*/
protected boolean matches(Pattern pat, String s) {
Expand Down Expand Up @@ -511,6 +542,11 @@ protected void warnCopyright(File file, BufferedReader in)
System.out.println(file +
": WARNING: extra copyright: " + line);
}

Matcher m2 = doNotAlterPattern.matcher(line);
if (m2.find()) {
System.out.println(file + ": WARNING: contains: " + line);
}
/*
* XXX - too many false positives for this one
else if (line.indexOf("Copyright") >= 0)
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/org/glassfish/copyright/Copyright.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*
* Usage: java -jar copyright.jar
* [-w] -[y] [-r] [-n] [-s] [-h] [-m] [-g] [-S] [-c] [-q] [-j] [-x]
* [-p] [-t] [-N] [-D] [-X pat] [-C file] [-A file] [-B file] [-P]
* [-p] [-t] [-e] [-N] [-D] [-X pat] [-C file] [-A file] [-B file] [-P]
* [-v] [-V] [files ...]
*
* Options:
Expand All @@ -39,6 +39,7 @@
* -x check XML syntax files
* -p check properties syntax files
* -t check other text files
* -e exclude files that contains DO NOT ALTER OR REMOVE COPYRIGHT NOTICES
* -N normalize format of repaired copyright to match template
* -D use dash instead of comma in years when repairing files
* -X exclude files matching pat (substring only)
Expand Down Expand Up @@ -79,6 +80,7 @@ public class Copyright {
public boolean doText = false;
public boolean preserveCopyrights = false;
public boolean verbose = false;
public boolean explicitExclude = false;
public File correctTemplate;
public List<File> alternateTemplates = new ArrayList<File>();
public File correctBSDTemplate;
Expand Down Expand Up @@ -343,6 +345,8 @@ public static void main(String[] argv) throws Exception {
c.doProps = true;
} else if (argv[optind].equals("-t")) {
c.doText = true;
} else if (argv[optind].equals("-e")) {
c.explicitExclude = true;
} else if (argv[optind].equals("-X")) {
String ex = argv[++optind];
if (ex.startsWith("@"))
Expand Down