Skip to content

Commit

Permalink
Detect shallow repo by testing .git/shallow existence
Browse files Browse the repository at this point in the history
  • Loading branch information
dbwiddis committed Jan 16, 2022
1 parent 1d181ae commit a588eeb
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 166 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ public Map<String, String> getAdditionalProperties(AbstractLicenseMojo mojo, Pro
try {
Map<String, String> result = new HashMap<String, String>(3);
GitLookup gitLookup = getGitLookup(document.getFile(), properties);


if (mojo.failIfShallow && gitLookup.isShallowRepository()) {
throw new RuntimeException("Shallow repository detected.");
}
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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ public Map<String, String> getAdditionalProperties(AbstractLicenseMojo mojo, Pro
try {
Map<String, String> result = new HashMap<String, String>(4);
GitLookup gitLookup = getGitLookup(document.getFile(), properties);
if (mojo.failIfShallow && gitLookup.isShallowRepository()) {
throw new RuntimeException("Shallow repository detected.");
}
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 @@ -184,6 +184,12 @@ String getAuthorEmailOfCreation(File file) throws IOException, GitAPIException {
walk.dispose();
return authorEmail;
}

boolean isShallowRepository() {
File dotGit = repository.getDirectory();
File shallow = new File(dotGit.getPath() + File.separator + "shallow");
return shallow.exists();
}

private boolean isFileModifiedOrUnstaged(String repoRelativePath) throws GitAPIException {
Status status = new Git(repository).status().addPath(repoRelativePath).call();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.mycila.maven.plugin.license.header.HeaderDefinition;
import com.mycila.maven.plugin.license.header.HeaderSource;
import com.mycila.maven.plugin.license.header.HeaderType;
import com.mycila.maven.plugin.license.util.ExecutingCommand;
import com.mycila.maven.plugin.license.util.Selection;
import com.mycila.maven.plugin.license.util.resource.ResourceFinder;
import com.mycila.xmltool.XMLDoc;
Expand Down Expand Up @@ -297,13 +296,11 @@ public abstract class AbstractLicenseMojo extends AbstractMojo {
public boolean skip = false;

/**
* Whether to skip the plugin execution on a shallow git clone.
* <p>
* We recommend setting this to {@code true} if you are generating copyright
* ranges from file creation dates in git history.
* Whether to fail plugin execution on a shallow git clone if you are using
* git history for initial author or copyright ranges.
*/
@Parameter(property = "license.skipIfShallow", defaultValue = "false")
public boolean skipIfShallow;
@Parameter(property = "license.failIfShallow", defaultValue = "true")
public boolean failIfShallow = true;

/**
* If you do not want to see the list of file having a missing header, you
Expand Down Expand Up @@ -484,15 +481,6 @@ public void checkUnknown() throws MojoExecutionException {
@SuppressWarnings({"unchecked"})
protected final void execute(final Callback callback) throws MojoExecutionException, MojoFailureException {
if (!skip) {
// skip if shallow clone detected
if (skipIfShallow) {
List<String> output = ExecutingCommand.runNative("git rev-parse --is-shallow-repository");
if (!output.isEmpty() && "true".equals(output.get(0))) {
warn("Shallow repository detected, skipping.");
return;
}
}

// make default base dir canonical
this.defaultBasedir = this.getCanonicalFile(this.defaultBasedir, "license.basedir");

Expand Down

This file was deleted.

0 comments on commit a588eeb

Please sign in to comment.