From 4de0e379024e201cf3a155ed978b3fab06770786 Mon Sep 17 00:00:00 2001 From: alexandra Date: Mon, 21 Nov 2022 13:42:15 +0400 Subject: [PATCH] #78 - Add FileTS --- .../service/GitRepositoryService.java | 187 +++++++++++------- .../ru/ulstu/extractor/ts/creator/FileTS.java | 53 +++-- 2 files changed, 136 insertions(+), 104 deletions(-) diff --git a/src/main/java/ru/ulstu/extractor/gitrepository/service/GitRepositoryService.java b/src/main/java/ru/ulstu/extractor/gitrepository/service/GitRepositoryService.java index b08f1d9..fb0a3eb 100644 --- a/src/main/java/ru/ulstu/extractor/gitrepository/service/GitRepositoryService.java +++ b/src/main/java/ru/ulstu/extractor/gitrepository/service/GitRepositoryService.java @@ -30,6 +30,7 @@ import ru.ulstu.extractor.heuristic.model.ResourceUnit; import ru.ulstu.extractor.heuristic.service.StructuralUnitService; import ru.ulstu.extractor.model.Commit; +import javax.swing.text.html.parser.Entity; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; @@ -39,10 +40,7 @@ import java.net.URL; import java.nio.charset.StandardCharsets; import java.nio.file.Path; import java.time.Instant; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import java.util.Optional; +import java.util.*; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -280,80 +278,22 @@ public class GitRepositoryService { private List parseOutputDiff(String output, org.eclipse.jgit.lib.Repository repository, RevCommit commit) { List changes = new ArrayList<>(); String[] strings = output.split("\n"); - FileChange fileChange = new FileChange(); - boolean isAdded = false; - boolean isRemoved = false; - boolean isFileChanged = false; - int stringsLength = strings.length - 1; - for (int i = 0; i < strings.length; i++) { - while (i < stringsLength) { - Optional maybeFileName = getFileName(strings[i]); - if (maybeFileName.isPresent()) { - fileChange = new FileChange(); - fileChange.setFile(maybeFileName.get()); - Future futureEntity = executorService.submit(() -> structuralUnitService.containsEntity(getContent(repository, commit, maybeFileName.get()))); - Future futureBL = executorService.submit(() -> structuralUnitService.containsBusinessLogic(getContent(repository, commit, maybeFileName.get()))); - fileChange.setAdded(isAdded); - fileChange.setRemoved(isRemoved); - try { - fileChange.setContainsBusinessLogic(futureBL.get()); - fileChange.setContainsEntity(futureEntity.get()); - } catch (Exception ex) { - LOG.warn(ex.getMessage()); - } - /// вытащить другие изменения из коммита - changes.add(fileChange); - } - LineChange lineChange = new LineChange(); - if (strings[i].startsWith("-")) { - while ((i < stringsLength) && strings[i].startsWith("-")) { - if (strings[i].startsWith("---")) { - i++; - } else { - if (lineChange.getLineFrom() == null) { - lineChange.setLineFrom(strings[i]); - i++; - } else { - lineChange.setLineFrom(lineChange.getLineFrom() + "\n" + strings[i]); - i++; - } - isRemoved = true; - } - } - lineChange.setRemoved(true); - } - if (strings[i].startsWith("+")) { - while ((i < stringsLength) && strings[i].startsWith("+")) { - if (strings[i].startsWith("+++")) { - i++; - } else { - if (lineChange.getLineTo() == null) { - lineChange.setLineTo(strings[i]); - i++; - } else { - lineChange.setLineTo(lineChange.getLineTo() + "\n" + strings[i]); - i++; - } - isAdded = true; - } - } - lineChange.setAdded(true); - } - if ((lineChange.getLineTo() != null) || (lineChange.getLineFrom() != null) - || ((lineChange.getLineTo() != null) && (lineChange.getLineFrom() != null))) { - fileChange.getLineChanges().add(lineChange); - } else { - i++; - } + Map> filesContent = getFilesContent(strings); + System.out.println(filesContent); + for(Map.Entry> fileSterings: filesContent.entrySet()) { + FileChange fileChange = new FileChange(); + fileChange.setFile(fileSterings.getKey()); + Future futureEntity = executorService.submit(() -> structuralUnitService.containsEntity(getContent(repository, commit, fileSterings.getKey()))); + Future futureBL = executorService.submit(() -> structuralUnitService.containsBusinessLogic(getContent(repository, commit, fileSterings.getKey()))); + try { + fileChange.setContainsBusinessLogic(futureBL.get()); + fileChange.setContainsEntity(futureEntity.get()); + } catch (Exception ex) { + LOG.warn(ex.getMessage()); } + fileChange = getChange(fileChange, fileSterings.getValue()); + changes.add(fileChange); } - if ((isAdded) && (isRemoved)) { - isAdded = false; - isRemoved = false; - isFileChanged = true; - } - fileChange.setAdded(isAdded); - fileChange.setRemoved(isRemoved); return changes; } @@ -382,6 +322,101 @@ public class GitRepositoryService { return Optional.empty(); } + private Map> getFilesContent(String[] commitStrings) { + int i = 0; + Map> result = new HashMap<>(); + while (i < commitStrings.length) { + Optional maybeFileName = getFileName(commitStrings[i]); + if (maybeFileName.isEmpty()) { + i++; + } else { + i = skipLinesForNextFile(commitStrings, i); + result.put(maybeFileName.get(), getFileContent(commitStrings, i)); + i += getFileContent(commitStrings, i).size() - 1; + } + } + return result; + } + + private List getFileContent(String[] commitsString, int i) { + List result = new ArrayList<>(); + while (i < commitsString.length && getFileName(commitsString[i]).isEmpty()) { + result.add(commitsString[i]); + i++; + } + return result; + } + + private int skipLinesForNextFile(String[] commitStrings, int index) { + boolean isFilePrefixSkipped = false; + while (!isFilePrefixSkipped && (index < commitStrings.length)) { + if ((commitStrings[index].startsWith("diff --git a/")) + || (commitStrings[index].startsWith("delete")) + || (commitStrings[index].startsWith("new")) + || (commitStrings[index].startsWith("index")) + || (commitStrings[index].startsWith("@@")) + || (commitStrings[index].startsWith("---")) + || (commitStrings[index].startsWith("+++"))) { + index++; + } else { + isFilePrefixSkipped = true; + } + } + return index; + } + + private FileChange getChange(FileChange fileChange, List fileContent){ + int addedLine = 0; + int removedLine = 0; + StringBuilder builder = new StringBuilder(); + boolean isRemoved = false; + boolean isAdded = false; + for (String line : fileContent){ + LineChange lineChange = new LineChange(); + if (line.startsWith("-")){ + isRemoved = true; + if (isAdded) { + isAdded = false; + lineChange = setAdded(lineChange, builder); + builder.setLength(0); + } + builder.append(line).append("\n"); + removedLine++; + } else if(line.startsWith("+")){ + isAdded = true; + if (isRemoved) { + isRemoved = false; + lineChange = setRemoved(lineChange, builder); + builder.setLength(0); + } + builder.append(line).append("\n"); + addedLine++; + } else { + if (isRemoved) { + lineChange = setRemoved(lineChange, builder); + builder.setLength(0); + } else if (isAdded){ + lineChange = setAdded(lineChange, builder); + builder.setLength(0); + } + } + } + fileChange.setRemoved(removedLine == fileContent.size() - 1); + fileChange.setAdded(addedLine == fileContent.size() - 1); + return fileChange; + } + + private LineChange setRemoved(LineChange lineChange, StringBuilder builder){ + lineChange.setLineFrom(builder.toString()); + lineChange.setRemoved(true); + return lineChange; + } + + private LineChange setAdded(LineChange lineChange, StringBuilder builder){ + lineChange.setLineTo(builder.toString()); + lineChange.setAdded(true); + return lineChange; + } public Page findAll(Pageable pageable) { return gitRepositoryRepository.findAll(pageable); } diff --git a/src/main/java/ru/ulstu/extractor/ts/creator/FileTS.java b/src/main/java/ru/ulstu/extractor/ts/creator/FileTS.java index 27e3453..8e6a934 100644 --- a/src/main/java/ru/ulstu/extractor/ts/creator/FileTS.java +++ b/src/main/java/ru/ulstu/extractor/ts/creator/FileTS.java @@ -7,6 +7,7 @@ import ru.ulstu.extractor.gitrepository.service.GitRepositoryService; import ru.ulstu.extractor.model.Commit; import ru.ulstu.extractor.ts.model.TimeSeries; import ru.ulstu.extractor.ts.model.TimeSeriesType; +import ru.ulstu.extractor.ts.model.TimeSeriesValue; import ru.ulstu.extractor.ts.service.TimeSeriesService; import java.util.ArrayList; @@ -26,18 +27,30 @@ public class FileTS extends AbstractTimeSeriesCreator { @Override public List getTimeSeries(Integer repositoryId, String branchName) { -// findSum(repositoryId,branchName); - return new ArrayList<>(); -// return Collections.singletonList( -// new TimeSeries( -// String.format("", -// gitRepositoryService.findById(repositoryId).getName(), -// branchName, -// getTimeSeriesType().getDescription()), -// getTimeSeriesType(), -// ) -// ) -// ) + List timeSeriesResult = new ArrayList<>(); + List commits = new ArrayList<>(commitService.findByRepositoryIdAndName(repositoryId, branchName)); + double value = 0; + TimeSeries timeSeries = new TimeSeries( + String.format("%s %s %s", + gitRepositoryService.findById(repositoryId).getName(), + branchName, + getTimeSeriesType().getDescription()), + getTimeSeriesType()); + for (Commit commit : commits) { + for (FileChange fileChange : commit.getFileChanges()) { + if ((fileChange.getAdded() != null) && (fileChange.getAdded())) { + value += 1; + } + if ((fileChange.getRemoved() != null) && (fileChange.getRemoved())) { + value -= 1; + } + } + timeSeries.getValues().add(new TimeSeriesValue(commit.getDate(), value)); + } + if (!timeSeries.getValues().isEmpty()) { + timeSeriesResult.add(timeSeries); + } + return timeSeriesResult; } @Override @@ -49,20 +62,4 @@ public class FileTS extends AbstractTimeSeriesCreator { public TimeSeriesType getTimeSeriesType() { return TimeSeriesType.FILES; } - - public void findSum(Integer repositoryId, String branchName) { - List commits = new ArrayList<>(commitService.findByRepositoryIdAndName(repositoryId, branchName)); - int value = 0; - for (Commit commit : commits) { - for (FileChange fileChange : commit.getFileChanges()) { - if ((fileChange.getAdded() != null) && (fileChange.getAdded())) { - value = +1; - } - if ((fileChange.getRemoved() != null) && (fileChange.getRemoved())) { - value = -1; - } - } - System.out.println(value); - } - } }