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

[#1601] Convert datetime functionalities to java.time package #1625

Merged
Merged
Show file tree
Hide file tree
Changes from 53 commits
Commits
Show all changes
59 commits
Select commit Hold shift + click to select a range
ab60526
Merge branch 'master' of https://github.com/reposense/RepoSense
yhtMinceraft1010X Aug 26, 2021
1f3cb05
Merge branch 'master' of https://github.com/reposense/RepoSense
yhtMinceraft1010X Sep 24, 2021
4142873
Merge branch 'master' of https://github.com/reposense/RepoSense
yhtMinceraft1010X Dec 23, 2021
6ba4885
Merge branch 'master' of https://github.com/reposense/RepoSense
yhtMinceraft1010X Jan 19, 2022
3a2c5a4
Replace SimpleDateFormat with DateTimeFormatter
yhtMinceraft1010X Jan 21, 2022
79b2096
Fix failing tests
yhtMinceraft1010X Jan 22, 2022
5403b80
Merge branch 'master' of https://github.com/reposense/RepoSense
yhtMinceraft1010X Jan 22, 2022
3469386
Merge branch 'master' into 1601-fix-intermittent-exceptions
yhtMinceraft1010X Jan 22, 2022
241c0df
Fix zoneIds used
yhtMinceraft1010X Jan 22, 2022
8db40b0
Remove withZone() in CustomLogFormatter
yhtMinceraft1010X Jan 24, 2022
5c31f0a
Simplify DateTimeFormatter parsing
yhtMinceraft1010X Jan 24, 2022
3a2d291
Remove unused exception
yhtMinceraft1010X Jan 25, 2022
c7f7d5c
Revert uuuu to yyyy
yhtMinceraft1010X Jan 25, 2022
61e0a92
Merge branch 'master' of https://github.com/reposense/RepoSense
yhtMinceraft1010X Jan 26, 2022
675bad0
Merge branch 'master' into 1601-fix-intermittent-exceptions
yhtMinceraft1010X Jan 31, 2022
00ff308
Fix git log timezone
yhtMinceraft1010X Jan 31, 2022
b8da945
Redo conversion of SimpleDateFormat
yhtMinceraft1010X Jan 31, 2022
9b6a5db
Change datetime APIs in main code
yhtMinceraft1010X Jan 31, 2022
7505e20
Change datetime APIs for test code
yhtMinceraft1010X Jan 31, 2022
de6ecf7
Fix import errors
yhtMinceraft1010X Jan 31, 2022
7b5b374
Fix NullPointerExceptions and wrong month numbering
yhtMinceraft1010X Jan 31, 2022
00f67c2
Fix since date typo
yhtMinceraft1010X Jan 31, 2022
70b6cf8
Fix month typo
yhtMinceraft1010X Jan 31, 2022
4c64a9b
Fix GitShowTest and TimeUtilTest
yhtMinceraft1010X Jan 31, 2022
bae1fdb
Fix ArgsParserTest
yhtMinceraft1010X Jan 31, 2022
0b68df6
Fix Json Formatting
yhtMinceraft1010X Feb 1, 2022
7873c3f
Fix checkstyle
yhtMinceraft1010X Feb 1, 2022
54bda60
Update javadoc
yhtMinceraft1010X Feb 1, 2022
940da71
Merge branch 'switch-to-java-time-package' into 1601-fix-intermittent…
yhtMinceraft1010X Feb 1, 2022
7282f40
Update docs to reflect new code
yhtMinceraft1010X Feb 1, 2022
4d65889
Fix systemtest
yhtMinceraft1010X Feb 1, 2022
8a09956
Remove unused code
yhtMinceraft1010X Feb 1, 2022
ffe79e5
Remove last Date usages
yhtMinceraft1010X Feb 1, 2022
5a249ec
Update doc to show new code
yhtMinceraft1010X Feb 1, 2022
db426a0
Improve code quality
yhtMinceraft1010X Feb 3, 2022
86cb35b
Add rationale for 'uuuu' parsing
yhtMinceraft1010X Feb 3, 2022
8e2c91e
Fix tests
yhtMinceraft1010X Feb 3, 2022
24fd106
Change timezone for expected dates
yhtMinceraft1010X Feb 4, 2022
066c912
Adjust timezone for commits
yhtMinceraft1010X Feb 4, 2022
b7e700a
Add test for different timezone
yhtMinceraft1010X Feb 4, 2022
8b80cdb
Merge branch 'master' of https://github.com/reposense/RepoSense
yhtMinceraft1010X Feb 4, 2022
c48da72
Merge branch 'master' into 1601-fix-intermittent-exceptions
yhtMinceraft1010X Feb 4, 2022
5ee6184
Rename actual time variables for SG time
yhtMinceraft1010X Feb 4, 2022
62dc6e1
Merge branch 'master' of https://github.com/reposense/RepoSense
yhtMinceraft1010X Feb 7, 2022
93339e2
Merge branch 'master' into 1601-fix-intermittent-exceptions
yhtMinceraft1010X Feb 7, 2022
dbfb94c
Fix wrong timezoning for 'd1' with no commitInfos
yhtMinceraft1010X Feb 7, 2022
8be61b8
Add test cases for timezones with different dates
yhtMinceraft1010X Feb 7, 2022
f6addc7
Remove redundant test
yhtMinceraft1010X Feb 7, 2022
428eb9c
Remove unused import
yhtMinceraft1010X Feb 7, 2022
23f01c8
Fix arbitrary datetime checking in getStartOfDate
yhtMinceraft1010X Feb 7, 2022
5e961ea
Replace month numbers with constants and getValue()
yhtMinceraft1010X Feb 8, 2022
1c6bdf5
Merge lines for SummaryJson constructor
yhtMinceraft1010X Feb 8, 2022
73c3780
Add comments for boolean
yhtMinceraft1010X Feb 8, 2022
3d1f4ed
Merge branch 'master' of https://github.com/reposense/RepoSense
yhtMinceraft1010X Feb 13, 2022
0e93b61
Merge branch 'master' into 1601-fix-intermittent-exceptions
yhtMinceraft1010X Feb 13, 2022
f08cba4
Merge branch 'master' of https://github.com/reposense/RepoSense
yhtMinceraft1010X Feb 16, 2022
acbff02
Merge branch 'master' into 1601-fix-intermittent-exceptions
yhtMinceraft1010X Feb 16, 2022
1bb44a7
Merge branch 'master' into 1601-fix-intermittent-exceptions
yhtMinceraft1010X Feb 19, 2022
1d7f047
Merge branch 'master' into 1601-fix-intermittent-exceptions
dcshzj Feb 21, 2022
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
3 changes: 2 additions & 1 deletion docs/dg/learningBasics.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ Here are some small tasks for you to gain some basic knowledge of the code relat

```
GsonBuilder gsonBuilder = new GsonBuilder()
.setDateFormat(GITHUB_API_DATE_FORMAT)
.registerTypeAdapter(LocalDateTime.class, (JsonSerializer<LocalDateTime>) (date, typeOfSrc, context)
-> new JsonPrimitive(date.format(DateTimeFormatter.ofPattern(GITHUB_API_DATE_FORMAT))))
.registerTypeAdapter(FileType.class, new FileType.FileTypeSerializer());
Gson gson;
if (isPrettyPrintingUsed) {
Expand Down
18 changes: 8 additions & 10 deletions src/main/java/reposense/authorship/FileInfoAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;

import java.util.HashSet;
Expand All @@ -22,7 +23,6 @@
import reposense.model.RepoConfiguration;
import reposense.system.LogsManager;
import reposense.util.FileUtil;
import reposense.util.TimeUtil;

/**
* Analyzes the target and information given in the {@code FileInfo}.
Expand Down Expand Up @@ -148,21 +148,22 @@ private static void aggregateBlameAuthorModifiedAndDateInfo(RepoConfiguration co

String[] blameResultLines = blameResults.split("\n");
Path filePath = Paths.get(fileInfo.getPath());
Long sinceDateInMs = config.getSinceDate().getTime();
Long untilDateInMs = config.getUntilDate().getTime();
LocalDateTime sinceDate = config.getSinceDate();
LocalDateTime untilDate = config.getUntilDate();

for (int lineCount = 0; lineCount < blameResultLines.length; lineCount += 5) {
String commitHash = blameResultLines[lineCount].substring(0, FULL_COMMIT_HASH_LENGTH);
String authorName = blameResultLines[lineCount + 1].substring(AUTHOR_NAME_OFFSET);
String authorEmail = blameResultLines[lineCount + 2]
.substring(AUTHOR_EMAIL_OFFSET).replaceAll("<|>", "");
Long commitDateInMs = Long.parseLong(blameResultLines[lineCount + 3].substring(AUTHOR_TIME_OFFSET)) * 1000;
String authorTimeZone = blameResultLines[lineCount + 4].substring(AUTHOR_TIMEZONE_OFFSET);
LocalDateTime commitDate = LocalDateTime.ofInstant(Instant.ofEpochMilli(commitDateInMs),
ZoneId.of(config.getZoneId()));
Author author = config.getAuthor(authorName, authorEmail);

if (!fileInfo.isFileLineTracked(lineCount / 5) || author.isIgnoringFile(filePath)
|| CommitHash.isInsideCommitList(commitHash, config.getIgnoreCommitList())
|| commitDateInMs < sinceDateInMs || commitDateInMs > untilDateInMs) {
|| commitDate.compareTo(sinceDate) < 0 || commitDate.compareTo(untilDate) > 0) {
author = Author.UNKNOWN_AUTHOR;
}

Expand All @@ -171,11 +172,8 @@ private static void aggregateBlameAuthorModifiedAndDateInfo(RepoConfiguration co
logger.warning(String.format(
MESSAGE_SHALLOW_CLONING_LAST_MODIFIED_DATE_CONFLICT, config.getRepoName()));
}
// convert the commit date from the system default time zone to cli-specified timezone
Date convertedCommitDate = TimeUtil.getZonedDateFromSystemDate(new Date(commitDateInMs),
ZoneId.of(config.getZoneId()));

fileInfo.setLineLastModifiedDate(lineCount / 5, convertedCommitDate);
fileInfo.setLineLastModifiedDate(lineCount / 5, commitDate);
}
fileInfo.setLineAuthor(lineCount / 5, author);
}
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/reposense/authorship/FileInfoExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
Expand Down Expand Up @@ -62,12 +63,13 @@ public static List<FileInfo> extractTextFileInfos(RepoConfiguration config) {
// checks out to the latest commit of the date range to ensure the FileInfo generated correspond to the
// git blame file analyze output
try {
GitCheckout.checkoutDate(config.getRepoRoot(), config.getBranch(), config.getUntilDate());
GitCheckout.checkoutDate(config.getRepoRoot(), config.getBranch(), config.getUntilDate(),
ZoneId.of(config.getZoneId()));
} catch (CommitNotFoundException cnfe) {
return fileInfos;
}
String lastCommitHash = GitRevList.getCommitHashBeforeDate(
config.getRepoRoot(), config.getBranch(), config.getSinceDate());
config.getRepoRoot(), config.getBranch(), config.getSinceDate(), ZoneId.of(config.getZoneId()));

fileInfos = (lastCommitHash.isEmpty())
? getAllFileInfo(config, false)
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/reposense/authorship/model/FileInfo.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package reposense.authorship.model;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import reposense.model.Author;
Expand Down Expand Up @@ -72,7 +72,7 @@ public void setLineAuthor(int lineNumber, Author author) {
/**
* Sets the {@code lastModifiedDate} of the {@code LineInfo} in {@code lineNumber} for this {@code FileInfo}.
*/
public void setLineLastModifiedDate(int lineNumber, Date lastModifiedDate) {
public void setLineLastModifiedDate(int lineNumber, LocalDateTime lastModifiedDate) {
lines.get(lineNumber).setLastModifiedDate(lastModifiedDate);
}

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/reposense/authorship/model/LineInfo.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package reposense.authorship.model;

import java.util.Date;
import java.time.LocalDateTime;
import java.util.Objects;

import reposense.model.Author;
Expand All @@ -12,7 +12,7 @@ public class LineInfo {
private int lineNumber;
private Author author;
private String content;
private Date lastModifiedDate;
private LocalDateTime lastModifiedDate;

private transient boolean isTracked;

Expand Down Expand Up @@ -43,11 +43,11 @@ public void setTracked(boolean isTracked) {
this.isTracked = isTracked;
}

public Date getLastModifiedDate() {
public LocalDateTime getLastModifiedDate() {
return lastModifiedDate;
}

public void setLastModifiedDate(Date lastModifiedDate) {
public void setLastModifiedDate(LocalDateTime lastModifiedDate) {
this.lastModifiedDate = lastModifiedDate;
}

Expand Down
26 changes: 16 additions & 10 deletions src/main/java/reposense/commits/CommitInfoAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import static reposense.util.StringsUtil.removeQuote;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -30,7 +31,8 @@
* Analyzes commit information found in the git log.
*/
public class CommitInfoAnalyzer {
public static final DateFormat GIT_STRICT_ISO_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
public static final DateTimeFormatter GIT_STRICT_ISO_DATE_FORMAT =
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssz");

private static final String TAB_SPLITTER = "\t";
private static final String MOVED_FILE_INDICATION = "=> ";
Expand Down Expand Up @@ -87,13 +89,16 @@ public static CommitResult analyzeCommit(CommitInfo commitInfo, RepoConfiguratio
String hash = elements[COMMIT_HASH_INDEX];
Author author = config.getAuthor(elements[AUTHOR_INDEX], elements[EMAIL_INDEX]);

Date date = null;
ZonedDateTime date = null;
try {
date = GIT_STRICT_ISO_DATE_FORMAT.parse(elements[DATE_INDEX]);
} catch (ParseException pe) {
date = ZonedDateTime.parse(elements[DATE_INDEX], GIT_STRICT_ISO_DATE_FORMAT);
} catch (DateTimeParseException pe) {
logger.log(Level.WARNING, "Unable to parse the date from git log result for commit.", pe);
}

// Commit date may be in a timezone different from the one given in the config.
LocalDateTime adjustedDate = date.withZoneSameInstant(ZoneId.of(config.getZoneId())).toLocalDateTime();

String messageTitle = (elements.length > MESSAGE_TITLE_INDEX) ? elements[MESSAGE_TITLE_INDEX] : "";
String messageBody = (elements.length > MESSAGE_BODY_INDEX)
? getCommitMessageBody(elements[MESSAGE_BODY_INDEX]) : "";
Expand All @@ -109,15 +114,16 @@ public static CommitResult analyzeCommit(CommitInfo commitInfo, RepoConfiguratio
}

if (statLine.isEmpty()) { // empty commit, no files changed
return new CommitResult(author, hash, date, messageTitle, messageBody, tags);
return new CommitResult(author, hash, adjustedDate, messageTitle, messageBody, tags);
}

String[] statInfos = statLine.split(NEW_LINE_SPLITTER);
String[] fileTypeContributions = Arrays.copyOfRange(statInfos, 0, statInfos.length - 1);
Map<FileType, ContributionPair> fileTypeAndContributionMap =
getFileTypesAndContribution(fileTypeContributions, config);

return new CommitResult(author, hash, date, messageTitle, messageBody, tags, fileTypeAndContributionMap);
return new CommitResult(author, hash, adjustedDate, messageTitle, messageBody, tags,
fileTypeAndContributionMap);
}

/**
Expand Down
Loading