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

Support injecting Copyright Years based on file existence #240

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class CopyrightRangeProvider extends GitPropertiesProvider implements Pro

public static final String COPYRIGHT_LAST_YEAR_KEY = "license.git.copyrightLastYear";
public static final String COPYRIGHT_CREATION_YEAR_KEY = "license.git.copyrightCreationYear";
public static final String COPYRIGHT_EXISTENCE_YEARS_KEY = "license.git.copyrightExistenceYears";
public static final String COPYRIGHT_YEARS_KEY = "license.git.copyrightYears";
public static final String INCEPTION_YEAR_KEY = "project.inceptionYear";

Expand All @@ -44,17 +45,19 @@ public CopyrightRangeProvider() {
}

/**
* Returns an unmodifiable map containing the three entries {@value #COPYRIGHT_LAST_YEAR_KEY}, {@value #COPYRIGHT_YEARS_KEY},
* and {@value #COPYRIGHT_CREATION_YEAR_KEY}, whose values are set based on inspecting git history.
* Returns an unmodifiable map containing the following entries, whose values are set based on inspecting git history.
*
* <ul>
* <li>{@value #COPYRIGHT_LAST_YEAR_KEY} key stores the year from the committer date of the last git commit that has
* modified the supplied {@code document}.
* modified the supplied {@code document}.</li>
* <li>{@value #COPYRIGHT_YEARS_KEY} key stores the range from {@value #INCEPTION_YEAR_KEY} value to
* {@value #COPYRIGHT_LAST_YEAR_KEY} value. If both values a equal, only the {@value #INCEPTION_YEAR_KEY} value is
* returned; otherwise, the two values are combined using dash, so that the result is e.g. {@code "2000 - 2010"}.
* returned; otherwise, the two values are combined using dash, so that the result is e.g. {@code "2000-2010"}.</li>
* <li>{@value #COPYRIGHT_CREATION_YEAR_KEY} key stores the year from the committer date of the first git commit for
* the supplied {@code document}.
* the supplied {@code document}.</li>
* <li>{@value #COPYRIGHT_EXISTENCE_YEARS_KEY} key stores the range from {@value #COPYRIGHT_CREATION_YEAR_KEY} value to
* {@value #COPYRIGHT_LAST_YEAR_KEY} value. If both values are equal only the {@value #COPYRIGHT_CREATION_YEAR_KEY} is returned;
* otherwise, the two values are combined using dash, so that the result is e.g. {@code "2005-2010"}.</li>
* </ul>
* The {@value #INCEPTION_YEAR_KEY} value is read from the supplied properties and it must available. Otherwise a
* {@link RuntimeException} is thrown.
Expand Down Expand Up @@ -88,6 +91,15 @@ public Map<String, String> getAdditionalProperties(AbstractLicenseMojo mojo, Pro

int copyrightStart = gitLookup.getYearOfCreation(document.getFile());
result.put(COPYRIGHT_CREATION_YEAR_KEY, Integer.toString(copyrightStart));

final String copyrightExistenceYears;
if (copyrightStart >= copyrightEnd) {
copyrightExistenceYears = Integer.toString(copyrightStart);
} else {
copyrightExistenceYears = copyrightStart + "-" + copyrightEnd;
}
result.put(COPYRIGHT_EXISTENCE_YEARS_KEY, copyrightExistenceYears);

return Collections.unmodifiableMap(result);
} catch (Exception e) {
throw new RuntimeException("Could not compute the year of the last git commit for file "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,6 @@ int getYearOfLastChange(File file) throws NoHeadException, GitAPIException, IOEx
int getYearOfCreation(File file) throws IOException, GitAPIException {
String repoRelativePath = pathResolver.relativize(file);

if (isFileModifiedOrUnstaged(repoRelativePath)) {
return getCurrentYear();
}

int commitYear = 0;
RevWalk walk = getGitRevWalk(repoRelativePath, true);
Iterator<RevCommit> iterator = walk.iterator();
Expand All @@ -154,6 +150,12 @@ int getYearOfCreation(File file) throws IOException, GitAPIException {
commitYear = getYearFromCommit(commit);
}
walk.dispose();

// If we couldn't find a creation year from Git assume newly created file
if (commitYear == 0) {
return getCurrentYear();
}

return commitYear;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,27 @@ public class CopyrightRangeProviderTest {
public void copyrightRange() {
CopyrightRangeProvider provider = new CopyrightRangeProvider();

assertRange(provider, "dir1/file1.txt", "2000", "2006", "1999-2006");
assertRange(provider, "dir2/file2.txt", "2007", "2007", "1999-2007");
assertRange(provider, "dir1/file3.txt", "2009", "2009", "1999-2009");
assertRange(provider, "dir2/file4.txt", "1999", "1999", "1999");
assertRange(provider, "dir1/file1.txt", "2000", "2006", "1999-2006", "2000-2006");
assertRange(provider, "dir2/file2.txt", "2007", "2007", "1999-2007", "2007");
assertRange(provider, "dir1/file3.txt", "2009", "2009", "1999-2009", "2009");
assertRange(provider, "dir2/file4.txt", "1999", "1999", "1999", "1999");

/* The last change of file4.txt in git history is in 1999
* but the inception year is 2000
* and we do not want the range to go back (2000-1999)
* so in this case we expect just 2000 */
assertRange(provider, "dir2/file4.txt", "2000", "1999", "1999", "2000");
* so in this case we expect just 2000
* However for existence years always report the actual year regardless
* of the inception year so expect 1999 for that */
assertRange(provider, "dir2/file4.txt", "2000", "1999", "1999", "2000", "1999");

}

private void assertRange(CopyrightRangeProvider provider, String path, String copyrightStart, String copyrightEnd, String copyrightRange) {
assertRange(provider, path, "1999", copyrightStart, copyrightEnd, copyrightRange);
private void assertRange(CopyrightRangeProvider provider, String path, String copyrightStart, String copyrightEnd, String copyrightRange, String copyrightExistence) {
assertRange(provider, path, "1999", copyrightStart, copyrightEnd, copyrightRange, copyrightExistence);
}

private void assertRange(CopyrightRangeProvider provider, String path, String inceptionYear,
String copyrightStart, String copyrightEnd, String copyrightRange) {
String copyrightStart, String copyrightEnd, String copyrightRange, String copyrightExistence) {
Properties props = new Properties();
props.put(CopyrightRangeProvider.INCEPTION_YEAR_KEY, inceptionYear);

Expand All @@ -70,6 +72,7 @@ private void assertRange(CopyrightRangeProvider provider, String path, String in
expected.put(CopyrightRangeProvider.COPYRIGHT_CREATION_YEAR_KEY, copyrightStart);
expected.put(CopyrightRangeProvider.COPYRIGHT_LAST_YEAR_KEY, copyrightEnd);
expected.put(CopyrightRangeProvider.COPYRIGHT_YEARS_KEY, copyrightRange);
expected.put(CopyrightRangeProvider.COPYRIGHT_EXISTENCE_YEARS_KEY, copyrightExistence);
Assert.assertEquals("for file '" + path + "': ", expected, actual);

}
Expand Down