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

Optionally warn (once) if repository is shallow/sparse #253

Merged
merged 9 commits into from
Mar 21, 2022
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public Map<String, String> getAdditionalProperties(AbstractLicenseMojo mojo, Pro

try {
Map<String, String> result = new HashMap<String, String>(3);
GitLookup gitLookup = getGitLookup(document.getFile(), properties);
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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public Map<String, String> getAdditionalProperties(AbstractLicenseMojo mojo, Pro
}
try {
Map<String, String> result = new HashMap<String, String>(4);
GitLookup gitLookup = getGitLookup(document.getFile(), properties);
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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public enum DateSource {
private final GitPathResolver pathResolver;
private final Repository repository;
private final TimeZone timeZone;
private final boolean shallow;

/**
* Creates a new {@link GitLookup} for a repository that is detected from the supplied {@code anyFile}.
Expand All @@ -76,7 +77,8 @@ public GitLookup(File anyFile, DateSource dateSource, TimeZone timeZone, int che
super();
this.repository = new FileRepositoryBuilder().findGitDir(anyFile).build();
/* A workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=457961 */
this.repository.getObjectDatabase().newReader().getShallowCommits();
// Also contains contents of .git/shallow and can detect shallow repo
this.shallow = !this.repository.getObjectDatabase().newReader().getShallowCommits().isEmpty();
dbwiddis marked this conversation as resolved.
Show resolved Hide resolved

this.pathResolver = new GitPathResolver(repository.getWorkTree().getAbsolutePath());
this.dateSource = dateSource;
Expand Down Expand Up @@ -184,6 +186,10 @@ String getAuthorEmailOfCreation(File file) throws IOException, GitAPIException {
walk.dispose();
return authorEmail;
}

boolean isShallowRepository() {
return this.shallow;
}

private boolean isFileModifiedOrUnstaged(String repoRelativePath) throws GitAPIException {
@SuppressWarnings("resource")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.util.Properties;
import java.util.TimeZone;

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

/**
* @author <a href="mailto:ppalaga@redhat.com">Peter Palaga</a>
*/
Expand All @@ -43,7 +45,7 @@ public GitPropertiesProvider() {}
* @return
* @throws IOException
*/
GitLookup getGitLookup(File file, Properties props) throws IOException {
GitLookup getGitLookup(AbstractLicenseMojo mojo, File file, Properties props) throws IOException {
if (gitLookup == null) {
synchronized (this) {
if (gitLookup == null) {
Expand Down Expand Up @@ -74,6 +76,10 @@ GitLookup getGitLookup(File file, Properties props) throws IOException {
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.");
}
}
}
}
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 com.mycila.maven.plugin.license.LicenseCheckMojo;
import com.mycila.maven.plugin.license.document.Document;

import java.io.File;
Expand Down Expand Up @@ -53,7 +54,7 @@ private void assertAuthor(CopyrightAuthorProvider provider, String path, String
Properties props = new Properties();

Document document = newDocument(path);
Map<String, String> actual = provider.getAdditionalProperties(null, props, document);
Map<String, String> actual = provider.getAdditionalProperties(new LicenseCheckMojo(), props, document);

HashMap<String, String> expected = new HashMap<String, String>();
expected.put(CopyrightAuthorProvider.COPYRIGHT_CREATION_AUTHOR_NAME_KEY, copyrightAuthorName);
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 com.mycila.maven.plugin.license.LicenseCheckMojo;
import com.mycila.maven.plugin.license.document.Document;

import java.io.File;
Expand Down Expand Up @@ -71,7 +72,7 @@ private void assertRange(CopyrightRangeProvider provider, String path, String in
props.put(CopyrightRangeProvider.INCEPTION_YEAR_KEY, inceptionYear);

Document document = newDocument(path);
Map<String, String> actual = provider.getAdditionalProperties(null, props, document);
Map<String, String> actual = provider.getAdditionalProperties(new LicenseCheckMojo(), props, document);

HashMap<String, String> expected = new HashMap<String, String>();
expected.put(CopyrightRangeProvider.COPYRIGHT_CREATION_YEAR_KEY, copyrightStart);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@
import com.mycila.maven.plugin.license.PropertiesProvider;
import com.mycila.maven.plugin.license.document.Document;
import org.tmatesoft.svn.core.ISVNLogEntryHandler;
import org.tmatesoft.svn.core.SVNDepth;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNLogEntry;
import org.tmatesoft.svn.core.internal.wc.DefaultSVNOptions;
import org.tmatesoft.svn.core.wc.SVNClientManager;
import org.tmatesoft.svn.core.wc.SVNInfo;
import org.tmatesoft.svn.core.wc.SVNRevision;

import java.io.File;
Expand Down Expand Up @@ -58,6 +60,8 @@ protected SimpleDateFormat initialValue() {

public static final String SVN_SERVER_ID_PLUGIN_KEY = "license.svn.serverId";

private volatile boolean initialized = false;

/**
* Provides information on the given document. The information is put in the
* returned map using: SVN_COPYRIGHT_LAST_YEAR_KEY the year of the latest
Expand Down Expand Up @@ -104,6 +108,20 @@ public void handleLogEntry(SVNLogEntry logEntry) throws SVNException {
};

try {
if (!this.initialized) {
synchronized (this) {
if (!this.initialized) {
this.initialized = true;
// One-time warning for shallow repo
if (mojo.warnIfShallow) {
SVNInfo info = clientManager.getWCClient().doInfo(documentFile, SVNRevision.HEAD);
if (info.getDepth() != SVNDepth.INFINITY) {
mojo.warn("Sparse svn repository detected. Year and author property values may not be accurate.");
}
}
}
}
}
clientManager.getLogClient().doLog(new File[]{documentFile}, SVNRevision.HEAD, SVNRevision.create(0), true, true, 1, lastChangeDateLogEntryHandler);
} catch (SVNException ex) {
IllegalStateException ise = new IllegalStateException("cannot query SVN latest date information for file: " + documentFile, ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,16 @@ public abstract class AbstractLicenseMojo extends AbstractMojo {
@Parameter(property = "license.skip", defaultValue = "false")
public boolean skip = false;

/**
* Determination of the year and author of the first commit and last change year
* of a file requires a full git or svn history. By default the plugin will log
* warning when using these properties on a shallow or sparse repository. If you
* are certain the repository depth will permit accurate determination of these
* values, you can disable this check.
*/
@Parameter(property = "license.warnIfShallow", defaultValue = "true")
public boolean warnIfShallow = true;

/**
* If you do not want to see the list of file having a missing header, you
* can add the quiet flag that will shorten the output
Expand Down