getting a list of changed data

This commit is contained in:
Anton Romanov 2021-03-10 14:47:43 +04:00
parent 7b8c22b410
commit 0204547710
6 changed files with 212 additions and 9 deletions

View File

@ -0,0 +1,19 @@
package ru.ulstu.extractor;
import java.util.ArrayList;
import java.util.List;
public class Changes {
private List<FileChange> fileChanges = new ArrayList<>();
public Changes() {
}
public Changes(List<FileChange> fileChanges) {
this.fileChanges = fileChanges;
}
public List<FileChange> getFileChanges() {
return fileChanges;
}
}

View File

@ -6,6 +6,7 @@ public class Commit {
private String message; private String message;
private Date date; private Date date;
private String author; private String author;
private Changes changes;
public Commit(String message, String author, Date date) { public Commit(String message, String author, Date date) {
this.message = message; this.message = message;
@ -25,4 +26,12 @@ public class Commit {
public String getAuthor() { public String getAuthor() {
return author; return author;
} }
public Changes getChanges() {
return changes;
}
public void setChanges(Changes changes) {
this.changes = changes;
}
} }

View File

@ -0,0 +1,50 @@
package ru.ulstu.extractor;
import java.util.ArrayList;
import java.util.List;
public class FileChange {
private String file;
private boolean removed;
private boolean added;
private List<LineChange> lineChanges = new ArrayList<>();
public FileChange() {
}
public FileChange(String file) {
this.file = file;
}
public String getFile() {
return file;
}
public boolean getRemoved() {
return removed;
}
public boolean getAdded() {
return added;
}
public List<LineChange> getLineChanges() {
return lineChanges;
}
public void setFile(String file) {
this.file = file;
}
public void setRemoved(boolean removed) {
this.removed = removed;
}
public void setAdded(boolean added) {
this.added = added;
}
public void setLineChanges(List<LineChange> lineChanges) {
this.lineChanges = lineChanges;
}
}

View File

@ -2,12 +2,14 @@ package ru.ulstu.extractor;
import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.diff.DiffFormatter;
import org.eclipse.jgit.internal.storage.file.FileRepository; import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevCommit;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Path; import java.nio.file.Path;
@ -15,6 +17,7 @@ import java.time.Instant;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Optional;
import static org.apache.logging.log4j.util.Strings.isBlank; import static org.apache.logging.log4j.util.Strings.isBlank;
@ -23,7 +26,6 @@ public class GitRepositoryService {
@Value("${extractor.custom-projects-dir}") @Value("${extractor.custom-projects-dir}")
private String customProjectsDir; private String customProjectsDir;
public void cloneOrUpdateRepo(String url) throws GitAPIException, IOException { public void cloneOrUpdateRepo(String url) throws GitAPIException, IOException {
Git git; Git git;
if (projectDirExists(getProjectDirectoryFile(url))) { if (projectDirExists(getProjectDirectoryFile(url))) {
@ -37,7 +39,7 @@ public class GitRepositoryService {
.call(); .call();
} }
Iterable<RevCommit> commits = git.log().call(); Iterable<RevCommit> commits = git.log().call();
commits.forEach(c -> System.out.println(c.getFullMessage())); //commits.forEach(c -> System.out.println(c.getFullMessage()));
} }
private String getProjectDirectory(String url) { private String getProjectDirectory(String url) {
@ -69,14 +71,58 @@ public class GitRepositoryService {
git.log().call().forEach(commits::add); git.log().call().forEach(commits::add);
List<Commit> list = new ArrayList<>(); List<Commit> list = new ArrayList<>();
for (RevCommit commit : commits) { RevCommit prevCommit = null;
Commit fullMessage = new Commit( for (RevCommit revCommit : commits) {
commit.getFullMessage(), Commit commit = new Commit(
commit.getAuthorIdent().getName(), revCommit.getFullMessage(),
Date.from(Instant.ofEpochSecond(commit.getCommitTime()))); revCommit.getAuthorIdent().getName(),
list.add(fullMessage); Date.from(Instant.ofEpochSecond(revCommit.getCommitTime())));
if (prevCommit != null) {
commit.setChanges(findDiffBetweenTwoRevisions(revCommit, prevCommit, localRepo));
}
list.add(commit);
prevCommit = revCommit;
} }
return list; return list;
} }
public Changes findDiffBetweenTwoRevisions(RevCommit laterCommit, RevCommit earlierCommit, Repository localRepo) {
if (laterCommit == null || earlierCommit == null) {
return null;
}
String output = null;
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
DiffFormatter diffFormatter = new DiffFormatter(out);
diffFormatter.setRepository(localRepo);
diffFormatter.format(earlierCommit.getId(), laterCommit.getId());
output = out.toString();
} catch (IOException e) {
throw new RuntimeException("Error occurred during diff computation. Message: " + e.getMessage());
}
return parseOutputDiff(output);
}
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();
fileChange.setFile(maybeFileName.get());
/// вытащить другие изменения из коммита
changes.getFileChanges().add(fileChange);
}
}
return changes;
}
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/"));
return Optional.of(name);
}
return Optional.empty();
}
} }

View File

@ -0,0 +1,32 @@
package ru.ulstu.extractor;
public class LineChange {
private boolean added;
private boolean removed;
private int lineFrom;
private int lineTo;
public LineChange(boolean added, boolean removed, int lineFrom, int lineTo) {
this.added = added;
this.removed = removed;
this.lineFrom = lineFrom;
this.lineTo = lineTo;
}
public boolean getAdd() {
return added;
}
public boolean getRemoved() {
return removed;
}
public int getLineFrom() {
return lineFrom;
}
public int getLineTo() {
return lineTo;
}
}

View File

@ -0,0 +1,47 @@
package ru.ulstu.extractor;
import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.diff.DiffFormatter;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class NewClass {
private final RevWalk revWalk;
private final DiffFormatter diffFormatter;
private NewClass(RevWalk revWalk, DiffFormatter diffFormatter) {
this.revWalk = revWalk;
this.diffFormatter = diffFormatter;
}
public Set<String> getModified(final RevCommit commit) throws IOException {
final RevCommit[] parents = commit.getParents();
final Set<String> result = new HashSet<>();
if (parents.length == 1) { // merge commit if length > 1
final RevCommit parent = revWalk.parseCommit(parents[0].getId());
// get diff of this commit to its parent, as list of paths
final List<DiffEntry> diffs = getDiffEntries(commit, parent);
for (final DiffEntry diff : diffs) {
final String changePath = diff.getChangeType().equals(DiffEntry.ChangeType.DELETE) ? diff.getOldPath() : diff.getNewPath();
result.add(changePath);
}
}
return result;
}
private List<DiffEntry> getDiffEntries(final RevCommit commit, final RevCommit parent) {
try {
return diffFormatter.scan(parent.getTree(), commit.getTree());
} catch (Exception e) {
e.printStackTrace();
}
return Collections.emptyList();
}
}