Skip to content

Commit

Permalink
Correctly close the repository
Browse files Browse the repository at this point in the history
  • Loading branch information
mathieucarbou committed Mar 28, 2022
1 parent feb0daa commit 1d7e972
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,8 @@ public CopyrightAuthorProvider() {
public Map<String, String> getAdditionalProperties(AbstractLicenseMojo mojo, Properties properties,
Document document) {

try {
try (GitLookup gitLookup = getGitLookup(mojo, document.getFile(), properties)) {
Map<String, String> result = new HashMap<>(3);
GitLookup gitLookup = getGitLookup(mojo, document.getFile(), properties);

result.put(COPYRIGHT_CREATION_AUTHOR_NAME_KEY, gitLookup.getAuthorNameOfCreation(document.getFile()));
result.put(COPYRIGHT_CREATION_AUTHOR_EMAIL_KEY, gitLookup.getAuthorEmailOfCreation(document.getFile()));
return Collections.unmodifiableMap(result);
Expand All @@ -64,6 +62,4 @@ public Map<String, String> getAdditionalProperties(AbstractLicenseMojo mojo, Pro
+ document.getFile().getAbsolutePath(), e);
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public CopyrightRangeProvider() {
* 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}.</li>
* <li>{@value #COPYRIGHT_EXISTENCE_YEARS_KEY} key stores the range from {@value #COPYRIGHT_CREATION_YEAR_KEY} value to
* <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>
Expand All @@ -75,9 +75,9 @@ public Map<String, String> getAdditionalProperties(AbstractLicenseMojo mojo, Pro
throw new RuntimeException("'" + INCEPTION_YEAR_KEY + "' must be an integer ; found = " + inceptionYear + " file: "
+ document.getFile().getAbsolutePath());
}
try {
try (GitLookup gitLookup = getGitLookup(mojo, document.getFile(), properties)) {
Map<String, String> result = new HashMap<>(4);
GitLookup gitLookup = getGitLookup(mojo, document.getFile(), properties);

int copyrightEnd = gitLookup.getYearOfLastChange(document.getFile());
result.put(COPYRIGHT_LAST_YEAR_KEY, Integer.toString(copyrightEnd));
final String copyrightYears;
Expand All @@ -90,15 +90,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);
copyrightExistenceYears = Integer.toString(copyrightStart);
} else {
copyrightExistenceYears = copyrightStart + "-" + copyrightEnd;
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 @@ -15,6 +15,7 @@
*/
package com.mycila.maven.plugin.license.git;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
Expand Down Expand Up @@ -44,7 +45,7 @@
*
* @author <a href="mailto:[email protected]">Peter Palaga</a>
*/
public class GitLookup {
public class GitLookup implements Closeable {
public static final TimeZone DEFAULT_ZONE = TimeZone.getTimeZone("GMT");

public enum DateSource {
Expand All @@ -71,10 +72,13 @@ public enum DateSource {
* @throws IOException
*/
public GitLookup(File anyFile, DateSource dateSource, TimeZone timeZone, int checkCommitsCount) throws IOException {
super();
this.repository = new FileRepositoryBuilder().findGitDir(anyFile).build();
/* A workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=457961 */
// Also contains contents of .git/shallow and can detect shallow repo
// the line below reads and caches the entries in the FileObjectDatabase of the repository to
// avoid concurrent modifications during RevWalk
// Closing the repository will close the FileObjectDatabase.
// Here the newReader() is a WindowCursor which delegates the getShallowCommits() to the FileObjectDatabase.
this.shallow = !this.repository.getObjectDatabase().newReader().getShallowCommits().isEmpty();

this.pathResolver = new GitPathResolver(repository.getWorkTree().getAbsolutePath());
Expand Down Expand Up @@ -148,12 +152,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 getCurrentYear();
}

return commitYear;
}

Expand Down Expand Up @@ -182,7 +186,7 @@ String getAuthorEmailOfCreation(File file) throws IOException {
walk.dispose();
return authorEmail;
}

boolean isShallowRepository() {
return this.shallow;
}
Expand Down Expand Up @@ -245,4 +249,9 @@ private String getAuthorEmailFromCommit(RevCommit commit) {
PersonIdent id = commit.getAuthorIdent();
return id.getEmailAddress();
}

@Override
public void close() {
repository.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.mycila.maven.plugin.license.git;

import com.mycila.maven.plugin.license.AbstractLicenseMojo;

import java.io.File;
import java.io.IOException;
import java.util.Locale;
Expand All @@ -27,66 +28,51 @@
*/
public class GitPropertiesProvider {

private GitLookup gitLookup;
public static final String MAX_COMMITS_LOOKUP_KEY = "license.git.maxCommitsLookup";
// keep for compatibility
private static final String COPYRIGHT_LAST_YEAR_MAX_COMMITS_LOOKUP_KEY = "license.git.copyrightLastYearMaxCommitsLookup";
public static final String COPYRIGHT_LAST_YEAR_SOURCE_KEY = "license.git.copyrightLastYearSource";
public static final String COPYRIGHT_LAST_YEAR_TIME_ZONE_KEY = "license.git.copyrightLastYearTimeZone";

public GitPropertiesProvider() {}

;

/**
* Lazily initializes #gitLookup assuming that all subsequent calls to this method will be related to the same
* git repository.
*
* @param file
* @return
* @throws IOException
*/
GitLookup getGitLookup(AbstractLicenseMojo mojo, File file, Properties props) throws IOException {
if (gitLookup == null) {
synchronized (this) {
if (gitLookup == null) {
String dateSourceString = props.getProperty(COPYRIGHT_LAST_YEAR_SOURCE_KEY,
GitLookup.DateSource.AUTHOR.name());
GitLookup.DateSource dateSource = GitLookup.DateSource.valueOf(dateSourceString.toUpperCase(Locale.US));
String checkCommitsCountString = props.getProperty(MAX_COMMITS_LOOKUP_KEY);
// Backwads compatibility
if (checkCommitsCountString == null) {
checkCommitsCountString = props.getProperty(COPYRIGHT_LAST_YEAR_MAX_COMMITS_LOOKUP_KEY);
}
int checkCommitsCount = Integer.MAX_VALUE;
if (checkCommitsCountString != null) {
checkCommitsCountString = checkCommitsCountString.trim();
checkCommitsCount = Integer.parseInt(checkCommitsCountString);
}
final TimeZone timeZone;
String tzString = props.getProperty(COPYRIGHT_LAST_YEAR_TIME_ZONE_KEY);
switch (dateSource) {
case COMMITER:
timeZone = tzString == null ? GitLookup.DEFAULT_ZONE : TimeZone.getTimeZone(tzString);
break;
case AUTHOR:
if (tzString != null) {
throw new RuntimeException(COPYRIGHT_LAST_YEAR_TIME_ZONE_KEY + " must not be set with "
+ COPYRIGHT_LAST_YEAR_SOURCE_KEY + " = " + GitLookup.DateSource.AUTHOR.name()
+ " because git author name already contrains time zone information.");
}
timeZone = null;
break;
default:
throw new IllegalStateException("Unexpected " + GitLookup.DateSource.class.getName() + " " + dateSource);
}
gitLookup = new GitLookup(file, dateSource, timeZone, checkCommitsCount);
// One-time warning for shallow repo
if (mojo.warnIfShallow && gitLookup.isShallowRepository()) {
mojo.warn("Shallow git repository detected. Year and author property values may not be accurate.");
}
static GitLookup getGitLookup(AbstractLicenseMojo mojo, File file, Properties props) throws IOException {
String dateSourceString = props.getProperty(COPYRIGHT_LAST_YEAR_SOURCE_KEY,
GitLookup.DateSource.AUTHOR.name());
GitLookup.DateSource dateSource = GitLookup.DateSource.valueOf(dateSourceString.toUpperCase(Locale.US));
String checkCommitsCountString = props.getProperty(MAX_COMMITS_LOOKUP_KEY);
// Backwads compatibility
if (checkCommitsCountString == null) {
checkCommitsCountString = props.getProperty(COPYRIGHT_LAST_YEAR_MAX_COMMITS_LOOKUP_KEY);
}
int checkCommitsCount = Integer.MAX_VALUE;
if (checkCommitsCountString != null) {
checkCommitsCountString = checkCommitsCountString.trim();
checkCommitsCount = Integer.parseInt(checkCommitsCountString);
}
final TimeZone timeZone;
String tzString = props.getProperty(COPYRIGHT_LAST_YEAR_TIME_ZONE_KEY);
switch (dateSource) {
case COMMITER:
timeZone = tzString == null ? GitLookup.DEFAULT_ZONE : TimeZone.getTimeZone(tzString);
break;
case AUTHOR:
if (tzString != null) {
throw new RuntimeException(COPYRIGHT_LAST_YEAR_TIME_ZONE_KEY + " must not be set with "
+ COPYRIGHT_LAST_YEAR_SOURCE_KEY + " = " + GitLookup.DateSource.AUTHOR.name()
+ " because git author name already contrains time zone information.");
}
}
timeZone = null;
break;
default:
throw new IllegalStateException("Unexpected " + GitLookup.DateSource.class.getName() + " " + dateSource);
}
GitLookup gitLookup = new GitLookup(file, dateSource, timeZone, checkCommitsCount);
// One-time warning for shallow repo
if (mojo.warnIfShallow && gitLookup.isShallowRepository()) {
mojo.warn("Shallow git repository detected. Year and author property values may not be accurate.");
}
return gitLookup;
}
Expand Down

0 comments on commit 1d7e972

Please sign in to comment.