Skip to content

Commit

Permalink
Merge pull request #437 from fo-code/delta-changes
Browse files Browse the repository at this point in the history
Provide affected lines for code delta
  • Loading branch information
uhafner authored Mar 9, 2022
2 parents a1bf677 + 07363b1 commit 8340a8b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 24 deletions.
4 changes: 2 additions & 2 deletions plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
<echarts-api.version>5.3.0-2</echarts-api.version>
<font-awesome-api.version>6.0.0-1</font-awesome-api.version>
<plugin-util-api.version>2.14.0</plugin-util-api.version>
<data-tables-api.version>1.11.4-1</data-tables-api.version>
<forensics-api-plugin.version>1.8.0</forensics-api-plugin.version>
<data-tables-api.version>1.11.4-3</data-tables-api.version>
<forensics-api-plugin.version>1.9.0</forensics-api-plugin.version>
<bootstrap5-api.version>5.1.3-6</bootstrap5-api.version>
<streamex.version>0.8.1</streamex.version>
</properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.diff.DiffEntry.ChangeType;
Expand Down Expand Up @@ -105,12 +106,15 @@ private RemoteResultWrapper<Delta> calculateDelta(final Repository repository) t
fileChangesMap.put(diffEntry.getNewId().name(), fileChanges);
}

log.logInfo("Creating the Git diff file");
String diffFile = new String(diffStream.toByteArray(), StandardCharsets.UTF_8);

GitDelta delta = new GitDelta(currentCommitId, referenceCommitId, fileChangesMap, diffFile);
RemoteResultWrapper<Delta> wrapper = new RemoteResultWrapper<>(delta, "Errors from Git Delta:");
wrapper.merge(log);

log.logInfo("Git code delta successfully calculated");

return wrapper;
}
}
Expand Down Expand Up @@ -153,8 +157,7 @@ private FileChanges createFileChanges(final FileEditType fileEditType, final Dif
FileChanges fileChanges = new FileChanges(filePath, fileContent, fileEditType, new HashMap<>());

for (Edit edit : diffFormatter.toFileHeader(diffEntry).toEditList()) {
Change change = createChange(edit);
fileChanges.addChange(change);
createChange(edit).ifPresent(fileChanges::addChange);
}

return fileChanges;
Expand Down Expand Up @@ -232,19 +235,27 @@ private ChangeEditType getChangeEditType(final Edit.Type type) {
* @param edit
* The edit to be processed
*
* @return the created change
* @return the created change as an Optional if there is a edit which is not empty
*/
private Change createChange(final Edit edit) {
private Optional<Change> createChange(final Edit edit) {
ChangeEditType changeEditType = getChangeEditType(edit.getType());
// add 1 to the 'begin' since it is included and the index is zero based
// 'end' does not need this because the value is excluded anyway
if (changeEditType == ChangeEditType.DELETE) {
// get the changed line indices from the old file version
return new Change(changeEditType, edit.getBeginA() + 1, edit.getEndA());
// add 1 to the 'begin' of the interval which is relevant for determining the made change since the begin is
// included and the index is zero based ('end' does not need this because the value is excluded anyway)
if (changeEditType.equals(ChangeEditType.DELETE)) {
return Optional.of(new Change(changeEditType,
edit.getBeginA() + 1, edit.getEndA(),
edit.getBeginB(), edit.getEndB()));
}
else {
// get the changed line indices from the new file version
return new Change(changeEditType, edit.getBeginB() + 1, edit.getEndB());
else if (changeEditType.equals(ChangeEditType.INSERT)) {
return Optional.of(new Change(changeEditType,
edit.getBeginA(), edit.getEndA(),
edit.getBeginB() + 1, edit.getEndB()));
}
else if (changeEditType.equals(ChangeEditType.REPLACE)) {
return Optional.of(new Change(changeEditType,
edit.getBeginA() + 1, edit.getEndA(),
edit.getBeginB() + 1, edit.getEndB()));
}
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,10 @@ public void shouldDetermineAddedLines() {
FilteredLog log = createLog();

final String content = "Test\nTest\n";
final String insertedContent = "Test\nInsert1\nInsert2\nTest\n";
commitFile(content);
final String referenceCommit = getHead();
commitFile(content + content);
commitFile(insertedContent);
final String currentCommit = getHead();

Optional<Delta> result = deltaCalculator.calculateDelta(currentCommit, referenceCommit, log);
Expand All @@ -172,8 +173,10 @@ public void shouldDetermineAddedLines() {
FileChanges fileChanges = getSingleFileChanges(delta);
Change change = getSingleChangeOfType(fileChanges, ChangeEditType.INSERT);
assertThat(change.getEditType()).isEqualTo(ChangeEditType.INSERT);
assertThat(change.getFromLine()).isEqualTo(3);
assertThat(change.getToLine()).isEqualTo(4);
assertThat(change.getChangedFromLine()).isEqualTo(1);
assertThat(change.getChangedToLine()).isEqualTo(1);
assertThat(change.getFromLine()).isEqualTo(2);
assertThat(change.getToLine()).isEqualTo(3);
}

/**
Expand All @@ -184,8 +187,8 @@ public void shouldDetermineModifiedLines() {
GitDeltaCalculator deltaCalculator = createDeltaCalculator();
FilteredLog log = createLog();

final String content = "Test\nTest\n";
final String modified = "Test\nModified\n";
final String content = "Test\nTest\nTest\nTest";
final String modified = "Test\nModified\nModified2\nTest";
commitFile(content);
final String referenceCommit = getHead();
commitFile(modified);
Expand All @@ -198,8 +201,10 @@ public void shouldDetermineModifiedLines() {
FileChanges fileChanges = getSingleFileChanges(delta);
Change change = getSingleChangeOfType(fileChanges, ChangeEditType.REPLACE);
assertThat(change.getEditType()).isEqualTo(ChangeEditType.REPLACE);
assertThat(change.getChangedFromLine()).isEqualTo(2);
assertThat(change.getChangedToLine()).isEqualTo(3);
assertThat(change.getFromLine()).isEqualTo(2);
assertThat(change.getToLine()).isEqualTo(2);
assertThat(change.getToLine()).isEqualTo(3);
}

/**
Expand All @@ -210,8 +215,8 @@ public void shouldDetermineDeletedLines() {
GitDeltaCalculator deltaCalculator = createDeltaCalculator();
FilteredLog log = createLog();

final String content = "Test\nTest\n";
final String modified = "Test\n";
final String content = "Test\nTest3\nTest";
final String modified = "Test\nTest";
commitFile(content);
final String referenceCommit = getHead();
commitFile(modified);
Expand All @@ -224,8 +229,10 @@ public void shouldDetermineDeletedLines() {
FileChanges fileChanges = getSingleFileChanges(delta);
Change change = getSingleChangeOfType(fileChanges, ChangeEditType.DELETE);
assertThat(change.getEditType()).isEqualTo(ChangeEditType.DELETE);
assertThat(change.getFromLine()).isEqualTo(2);
assertThat(change.getToLine()).isEqualTo(2);
assertThat(change.getChangedFromLine()).isEqualTo(2);
assertThat(change.getChangedToLine()).isEqualTo(2);
assertThat(change.getFromLine()).isEqualTo(1);
assertThat(change.getToLine()).isEqualTo(1);
}

/**
Expand All @@ -251,16 +258,22 @@ public void shouldDetermineAllChangeTypesTogether() {

Change replace = getSingleChangeOfType(fileChanges, ChangeEditType.REPLACE);
assertThat(replace.getEditType()).isEqualTo(ChangeEditType.REPLACE);
assertThat(replace.getChangedFromLine()).isEqualTo(1);
assertThat(replace.getChangedToLine()).isEqualTo(1);
assertThat(replace.getFromLine()).isEqualTo(1);
assertThat(replace.getToLine()).isEqualTo(1);

Change insert = getSingleChangeOfType(fileChanges, ChangeEditType.INSERT);
assertThat(insert.getEditType()).isEqualTo(ChangeEditType.INSERT);
assertThat(insert.getChangedFromLine()).isEqualTo(2);
assertThat(insert.getChangedToLine()).isEqualTo(2);
assertThat(insert.getFromLine()).isEqualTo(3);
assertThat(insert.getToLine()).isEqualTo(3);

Change delete = getSingleChangeOfType(fileChanges, ChangeEditType.DELETE);
assertThat(delete.getEditType()).isEqualTo(ChangeEditType.DELETE);
assertThat(delete.getChangedFromLine()).isEqualTo(4);
assertThat(delete.getChangedToLine()).isEqualTo(4);
assertThat(delete.getFromLine()).isEqualTo(4);
assertThat(delete.getToLine()).isEqualTo(4);
}
Expand Down

0 comments on commit 8340a8b

Please sign in to comment.