getting data output

This commit is contained in:
Anton Romanov 2021-03-11 17:04:58 +04:00
parent 0204547710
commit 67d028d6cb
3 changed files with 119 additions and 13 deletions

View File

@ -105,14 +105,36 @@ public class GitRepositoryService {
private Changes parseOutputDiff(String output) {
Changes changes = new Changes();
String[] strings = output.split("\n");
for (String string : strings) {
Optional<String> maybeFileName = getFileName(string);
if (maybeFileName.isPresent()) {
FileChange fileChange = new FileChange();
int stringsLength = strings.length;
for (int i = 0; i < strings.length; i++) {
while (i < stringsLength) {
Optional<String> maybeFileName = getFileName(strings[i]);
if (maybeFileName.isPresent()) {
fileChange = new FileChange();
fileChange.setFile(maybeFileName.get());
/// вытащить другие изменения из коммита
changes.getFileChanges().add(fileChange);
}
LineChange lineChange = new LineChange();
if (strings[i].startsWith("-")) {
while ((i < stringsLength) && strings[i].startsWith("-")) {
lineChange.setLineFrom(lineChange.getLineFrom() + "\n" + strings[i]);
i++;
}
}
if (strings[i].startsWith("+")) {
while ((i < stringsLength) && strings[i].startsWith("+")) {
lineChange.setLineTo(lineChange.getLineTo() + "\n" + strings[i]);
i++;
}
}
if ((lineChange.getLineTo() != null && lineChange.getLineTo().isEmpty())
|| (lineChange.getLineFrom() != null && lineChange.getLineFrom().isEmpty())) {
fileChange.getLineChanges().add(lineChange);
}
i++;
}
}
return changes;
}
@ -120,9 +142,37 @@ public class GitRepositoryService {
private Optional<String> getFileName(String commitString) {
String startString = "diff --git a/";
if (commitString.startsWith(startString)) {
String name = commitString.substring(commitString.indexOf(startString) + startString.length(), commitString.indexOf("b/"));
String name = commitString.substring(commitString.indexOf(startString) + startString.length(), commitString.indexOf(" b/"));
return Optional.of(name);
}
return Optional.empty();
}
private Optional<String> getLineTo(String commitString) {
String startString = "+";
String startPlus = "+++";
if (commitString.startsWith(startString)) {
String lineTo = commitString.substring(commitString.indexOf(startString) + startString.length());
if (commitString.startsWith(startPlus)) {
return Optional.empty();
} else {
return Optional.of(lineTo);
}
}
return Optional.empty();
}
private Optional<String> getLineFrom(String commitString) {
String startString = "-";
String startMinus = "---";
if (commitString.startsWith(startString)) {
String lineFrom = commitString.substring(commitString.indexOf(startString) + startString.length());
if (commitString.startsWith(startMinus)) {
return Optional.empty();
} else {
return Optional.of(lineFrom);
}
}
return Optional.empty();
}
}

View File

@ -3,16 +3,29 @@ package ru.ulstu.extractor;
public class LineChange {
private boolean added;
private boolean removed;
private int lineFrom;
private int lineTo;
private String lineFrom;
private String lineTo;
public LineChange(boolean added, boolean removed, int lineFrom, int lineTo) {
public LineChange() {
}
public LineChange(boolean added, boolean removed, String lineFrom, String lineTo) {
this.added = added;
this.removed = removed;
this.lineFrom = lineFrom;
this.lineTo = lineTo;
}
public void setLineTo(String lineTo) {
this.lineTo = lineTo;
}
public void setLineFrom(String lineFrom) {
this.lineFrom = lineFrom;
}
public boolean getAdd() {
return added;
}
@ -21,11 +34,11 @@ public class LineChange {
return removed;
}
public int getLineFrom() {
public String getLineFrom() {
return lineFrom;
}
public int getLineTo() {
public String getLineTo() {
return lineTo;
}

View File

@ -45,3 +45,46 @@ public class NewClass {
return Collections.emptyList();
}
}
// Optional<String> lineFrom = Optional.empty();
// Optional<String> lineTo = Optional.empty();
// for (String string: strings) {
// Optional<String> maybeFileName = getFileName(string);
// if (maybeFileName.isPresent()) {
// fileChange = new FileChange();
// fileChange.setFile(maybeFileName.get());
// /// вытащить другие изменения из коммита
// changes.getFileChanges().add(fileChange);
// }
//
// Optional<String> maybeLineFrom = getLineFrom(string);
// LineChange lineChange = new LineChange();
// if (maybeLineFrom.isPresent()) {
// lineChange.setLineFrom(maybeLineFrom.get());
// if (lineFrom.isPresent()){
// lineChange.setLineFrom(lineChange.getLineFrom() + "\n" + lineFrom);
// fileChange.getLineChanges().add(lineChange);
// lineFrom = maybeLineFrom;
// }
// else {
// fileChange.getLineChanges().add(lineChange);
// lineFrom = maybeLineFrom;
// }
// }
// Optional<String> maybeLineTo = getLineTo(string);
// if (maybeLineTo.isPresent()) {
// lineChange.setLineTo(maybeLineTo.get());
// if(lineTo.isPresent()){
// lineChange.setLineTo(lineChange.getLineTo() + "\n" + lineTo);
// fileChange.getLineChanges().add(lineChange);
// lineTo = maybeLineTo;
// }
// else {
// fileChange.getLineChanges().add(lineChange);
// lineTo = maybeLineFrom;
// }
// }
// }
// return changes;
// }