Skip to content
This repository has been archived by the owner on Aug 12, 2020. It is now read-only.

Commit

Permalink
Fix cross-platform support - #45
Browse files Browse the repository at this point in the history
 * Added enforce unix stye paths to FilePathUtils
 * Enfoce unix style paths in RefactoringAnalyzer, Yes and
 ProcessMetricscollector
  • Loading branch information
jan-gerling committed Feb 18, 2020
1 parent 181dd7b commit b8f46ad
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.stream.Collectors;

import static refactoringml.util.CKUtils.cleanClassName;
import static refactoringml.util.FilePathUtils.enforceUnixPaths;
import static refactoringml.util.JGitUtils.readFileFromGit;
import static refactoringml.util.RefactoringUtils.cleanMethodName;

Expand All @@ -46,7 +47,7 @@ public ProcessMetricsCollector(Project project, Database db, Repository reposito
this.db = db;
this.repository = repository;
this.branch = branch;
this.fileStoragePath = FilePathUtils.lastSlashDir(fileStoragePath);
this.fileStoragePath = enforceUnixPaths(FilePathUtils.lastSlashDir(fileStoragePath));
this.lastCommitToProcess = lastCommitToProcess;
pmDatabase = new PMDatabase(commitThreshold);
}
Expand Down Expand Up @@ -119,7 +120,7 @@ private void updateProcessMetrics(RevCommit commit, RevCommit commitParent) thro
diffFormatter.setDetectRenames(true);

for (DiffEntry entry : diffFormatter.scan(commitParent, commit)) {
String fileName = entry.getPath(null);
String fileName = enforceUnixPaths(entry.getNewPath());

if(TrackDebugMode.ACTIVE && fileName.equals(TrackDebugMode.FILE_TO_TRACK)) {
log.info("[TRACK] File was changed in commit " + commit.getId().getName() + ", updating process metrics");
Expand All @@ -136,7 +137,8 @@ private void updateProcessMetrics(RevCommit commit, RevCommit commitParent) thro
// this is a TTV as we can't correctly trace all renames and etc. But this doesn't affect the overall result,
// as this is basically exceptional when compared to thousands of commits and changes.
if(entry.getChangeType() == DiffEntry.ChangeType.DELETE || entry.getChangeType() == DiffEntry.ChangeType.RENAME) {
pmDatabase.remove(entry.getOldPath());
String oldFileName = enforceUnixPaths(entry.getOldPath());
pmDatabase.remove(oldFileName);

if(entry.getChangeType() == DiffEntry.ChangeType.DELETE)
continue;
Expand Down Expand Up @@ -355,7 +357,7 @@ private List<No> codeMetrics(CommitMetaData commitMetaData) {
No no = new No(
project,
commitMetaData,
ck.getFile().replace(tempDir, ""),
enforceUnixPaths(ck.getFile()).replace(tempDir, ""),
cleanedCkClassName,
classMetric,
null,
Expand Down Expand Up @@ -396,7 +398,7 @@ private List<No> codeMetrics(CommitMetaData commitMetaData) {
No noM = new No(
project,
commitMetaData,
ck.getFile().replace(tempDir, ""),
enforceUnixPaths(ck.getFile()).replace(tempDir, ""),
cleanedCkClassName,
classMetric,
methodMetrics,
Expand All @@ -412,7 +414,7 @@ private List<No> codeMetrics(CommitMetaData commitMetaData) {
No noV = new No(
project,
commitMetaData,
ck.getFile().replace(tempDir, ""),
enforceUnixPaths(ck.getFile()).replace(tempDir, ""),
cleanedCkClassName,
classMetric,
methodMetrics,
Expand All @@ -438,7 +440,7 @@ private List<No> codeMetrics(CommitMetaData commitMetaData) {
No noF = new No(
project,
commitMetaData,
ck.getFile().replace(tempDir, ""),
enforceUnixPaths(ck.getFile()).replace(tempDir, ""),
cleanedCkClassName,
classMetric,
null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import refactoringml.util.CKUtils;
import refactoringml.util.RefactoringUtils;
import refactoringml.util.SourceCodeUtils;
import refactoringml.util.*;

import java.io.*;
import java.util.*;
Expand Down Expand Up @@ -89,17 +90,17 @@ public Set<Long> collectCommitData(RevCommit commit, Refactoring refactoring, Co

// this should not happen...
if(!refactoredEntry.isPresent()) {
log.info("old classes in DiffEntry: " + entries.stream().map(x -> x.getOldPath()).collect(Collectors.toList()));
log.info("new classes in DiffEntry: " + entries.stream().map(x -> x.getNewPath()).collect(Collectors.toList()));
log.info("old classes in DiffEntry: " + entries.stream().map(x -> enforceUnixPaths(x.getOldPath())).collect(Collectors.toList()));
log.info("new classes in DiffEntry: " + entries.stream().map(x -> enforceUnixPaths(x.getNewPath())).collect(Collectors.toList()));
throw new RuntimeException("RefactoringMiner finds a refactoring for class '" + refactoredClassName + "', but we can't find it in DiffEntry: '" + refactoring.getRefactoringType() + "'. Check RefactoringAnalyzer.java for reasons why this can happen.");
}

// we found the file, let's get its metrics!
DiffEntry entry = refactoredEntry.get();
diffFormatter.toFileHeader(entry);

String oldFileName = entry.getOldPath();
String currentFileName = entry.getNewPath();
String oldFileName = enforceUnixPaths(entry.getOldPath());
String currentFileName = enforceUnixPaths(entry.getNewPath());

if(TrackDebugMode.ACTIVE && (oldFileName.equals(TrackDebugMode.FILE_TO_TRACK) || currentFileName.equals(TrackDebugMode.FILE_TO_TRACK))) {
log.info("[TRACK] Refactoring '" + refactoring.getName() +"' detected, commit " + commit.getId().getName());
Expand Down Expand Up @@ -317,7 +318,6 @@ private Yes calculateCkMetrics(String refactoredClass, CommitMetaData commitMeta
variableMetrics = new VariableMetric(refactoredVariable, appearances);
}
}

}

// finally, if it's a field refactoring, we then only have class + field
Expand All @@ -337,7 +337,7 @@ private Yes calculateCkMetrics(String refactoredClass, CommitMetaData commitMeta
Yes yes = new Yes(
project,
commitMetaData,
ck.getFile().replace(tempDir, ""),
enforceUnixPaths(ck.getFile()).replace(tempDir, ""),
cleanedCkClassName,
refactoring.getRefactoringType().getDisplayName(),
refactoringTypeInNumber(refactoring),
Expand All @@ -355,6 +355,7 @@ private Yes calculateCkMetrics(String refactoredClass, CommitMetaData commitMeta
return list.isEmpty() ? null : list.get(0);
}

//TODO: on my Windows computer the tempDir is not always deleted
private void cleanTmpDir() throws IOException {
if(tempDir != null) {
FileUtils.deleteDirectory(new File(tempDir));
Expand All @@ -363,6 +364,7 @@ private void cleanTmpDir() throws IOException {
}

private void createTmpDir() {
tempDir = lastSlashDir(com.google.common.io.Files.createTempDir().getAbsolutePath());
String unixTmpDir = enforceUnixPaths(com.google.common.io.Files.createTempDir().getAbsolutePath());
tempDir = lastSlashDir(unixTmpDir);
}
}
4 changes: 2 additions & 2 deletions data-collection/src/main/java/refactoringml/db/Yes.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package refactoringml.db;

import org.hibernate.annotations.Type;
import refactoringml.util.FilePathUtils;
import refactoringml.util.RefactoringUtils;
import javax.persistence.*;

Expand Down Expand Up @@ -57,7 +57,7 @@ public Yes() {}
public Yes(Project project, CommitMetaData commitMetaData, String filePath, String className, String refactoring, int refactoringLevel,
String refactoringSummary, ClassMetric classMetrics, MethodMetric methodMetrics, VariableMetric variableMetrics, FieldMetric fieldMetrics) {
this.project = project;
this.filePath = filePath;
this.filePath = FilePathUtils.enforceUnixPaths(filePath);
this.className = className;
this.refactoring = refactoring;
this.refactoringLevel = refactoringLevel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

public class FilePathUtils {
public static String classFromFileName (String fileName) {
fileName = fileName.replace("\\", "/");
String[] splittedFile = fileName.split("/");
String[] splittedFile = enforceUnixPaths(fileName).split("/");
return splittedFile[splittedFile.length-1].replace(".java", "");
}

Expand All @@ -27,6 +26,15 @@ public static boolean createAllDirs (String base, String fileName) {
}

public static String lastSlashDir (String path) {
//TODO: causes problems on Windows machines: C:\Users\test is made to C:\Users\test/
// Solution: enforce unix-style paths are only unix OSs
return path + (path.endsWith("/")?"":"/");
}
}

/*
Enforce uniform path formatting for cross-platform support.
On Windows jgit.Diffentry.getPath() and ck.getFile use different file separator e.g.
yes.filePath: ...\Temp\1581608730366-0/ and diffEntry.filePath: .../Temp/1581608730366-0/
*/
public static String enforceUnixPaths(String filePath){ return filePath.replace(File.separator, "/"); }
}

0 comments on commit b8f46ad

Please sign in to comment.