Skip to content

Commit

Permalink
Removed unnecessary failure regarding TZ, filtering out empty strings…
Browse files Browse the repository at this point in the history
… and cleanup property parsing
  • Loading branch information
mathieucarbou committed Mar 30, 2022
1 parent 6f6a551 commit 7f8396b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,23 @@
*/
package com.mycila.maven.plugin.license.git;

import static java.util.Objects.requireNonNull;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.TimeZone;
import java.util.stream.Collectors;

import java.util.stream.Stream;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.Status;
import org.eclipse.jgit.api.errors.GitAPIException;
Expand Down Expand Up @@ -81,49 +83,36 @@ public enum DateSource {
* to the same git repository.
*/
public static GitLookup create(File file, Map<String, String> props) {
String dateSourceString = props.get(COPYRIGHT_LAST_YEAR_SOURCE_KEY);
if (dateSourceString == null) {
dateSourceString = GitLookup.DateSource.AUTHOR.name();
}
GitLookup.DateSource dateSource = GitLookup.DateSource.valueOf(
dateSourceString.toUpperCase(Locale.US));
String checkCommitsCountString = props.get(MAX_COMMITS_LOOKUP_KEY);
// Backwards compatibility
if (checkCommitsCountString == null) {
checkCommitsCountString = props.get(COPYRIGHT_LAST_YEAR_MAX_COMMITS_LOOKUP_KEY);
}
int checkCommitsCount = Integer.MAX_VALUE;
if (checkCommitsCountString != null) {
checkCommitsCountString = checkCommitsCountString.trim();
checkCommitsCount = Integer.parseInt(checkCommitsCountString);
}
String commitsToIgnoreString = props.get(COMMITS_TO_IGNORE_KEY);
Set<ObjectId> commitsToIgnore = Collections.emptySet();
if (commitsToIgnoreString != null) {
commitsToIgnoreString = commitsToIgnoreString.trim();
commitsToIgnore = Arrays.stream(commitsToIgnoreString.split(","))
.map(String::trim)
.map(ObjectId::fromString)
.collect(Collectors.toSet());
}
final TimeZone timeZone;
String tzString = props.get(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 contains time zone information.");
}
timeZone = null;
break;
default:
throw new IllegalStateException(
"Unexpected " + GitLookup.DateSource.class.getName() + " " + dateSource);
}
final GitLookup.DateSource dateSource = Optional.ofNullable(props.get(COPYRIGHT_LAST_YEAR_SOURCE_KEY))
.map(String::trim)
.map(String::toUpperCase)
.map(GitLookup.DateSource::valueOf)
.orElse(GitLookup.DateSource.AUTHOR);

final int checkCommitsCount = Stream.of(
MAX_COMMITS_LOOKUP_KEY,
COPYRIGHT_LAST_YEAR_MAX_COMMITS_LOOKUP_KEY) // Backwards compatibility
.map(props::get)
.filter(Objects::nonNull)
.map(String::trim)
.map(Integer::parseInt)
.findFirst()
.orElse(Integer.MAX_VALUE);

final Set<ObjectId> commitsToIgnore = Stream.of(COMMITS_TO_IGNORE_KEY)
.map(props::get)
.filter(Objects::nonNull)
.flatMap(s -> Stream.of(s.split(",")))
.map(String::trim)
.filter(s -> !s.isEmpty())
.map(ObjectId::fromString)
.collect(Collectors.toSet());

final TimeZone timeZone = Optional.ofNullable(props.get(COPYRIGHT_LAST_YEAR_TIME_ZONE_KEY))
.map(String::trim)
.map(TimeZone::getTimeZone)
.orElse(DEFAULT_ZONE);

return new GitLookup(file, dateSource, timeZone, checkCommitsCount, commitsToIgnore);
}

Expand All @@ -143,6 +132,11 @@ public static GitLookup create(File file, Map<String, String> props) {
* @throws IOException
*/
private GitLookup(File anyFile, DateSource dateSource, TimeZone timeZone, int checkCommitsCount, Set<ObjectId> commitsToIgnore) {
requireNonNull(anyFile);
requireNonNull(dateSource);
requireNonNull(timeZone);
requireNonNull(commitsToIgnore);

try {
this.repository = new FileRepositoryBuilder().findGitDir(anyFile).build();
/* A workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=457961 */
Expand All @@ -152,25 +146,9 @@ private GitLookup(File anyFile, DateSource dateSource, TimeZone timeZone, int ch
// 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());
this.dateSource = dateSource;
switch (dateSource) {
case COMMITER:
this.timeZone = timeZone == null ? DEFAULT_ZONE : timeZone;
break;
case AUTHOR:
if (timeZone != null) {
throw new IllegalArgumentException(
"Time zone must be null with dateSource " + DateSource.AUTHOR.name()
+ " because git author name already contains time zone information.");
}
this.timeZone = null;
break;
default:
throw new IllegalStateException(
"Unexpected " + DateSource.class.getName() + " " + dateSource);
}
this.timeZone = timeZone;
this.checkCommitsCount = checkCommitsCount;
this.commitsToIgnore = commitsToIgnore;
} catch (IOException e) {
Expand Down Expand Up @@ -298,7 +276,7 @@ private RevWalk getGitRevWalk(String repoRelativePath, boolean oldestCommitsFirs
}

private int getCurrentYear() {
return toYear(System.currentTimeMillis(), timeZone != null ? timeZone : DEFAULT_ZONE);
return toYear(System.currentTimeMillis(), timeZone);
}

private int getYearFromCommit(RevCommit commit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,17 +156,9 @@ void reuseProvider() throws GitAPIException, IOException {

@Test
void timezone() throws GitAPIException, IOException {
try {
GitLookup.create(gitRepoRoot.toFile(), buildProps(DateSource.AUTHOR, "GMT", "10", null));
Assertions.fail("RuntimeException expected");
} catch (RuntimeException e) {
if (e.getMessage().contains(
"license.git.copyrightLastYearTimeZone must not be set with license.git.copyrightLastYearSource = AUTHOR because git author name already contains time zone information.")) {
/* expected */
} else {
throw e;
}
}
// do not fail if a tz is./mv set, it will just be unused
// it allows parent poms to pre-defined properties and let sub modules use them if needed
GitLookup.create(gitRepoRoot.toFile(), buildProps(DateSource.AUTHOR, "GMT", "10", null));

/* null is GMT */
GitLookup nullTzLookup = GitLookup.create(gitRepoRoot.toFile(),
Expand Down

0 comments on commit 7f8396b

Please sign in to comment.