getting a list of changed data
This commit is contained in:
parent
7b8c22b410
commit
0204547710
19
src/main/java/ru/ulstu/extractor/Changes.java
Normal file
19
src/main/java/ru/ulstu/extractor/Changes.java
Normal 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;
|
||||
}
|
||||
}
|
@ -6,6 +6,7 @@ public class Commit {
|
||||
private String message;
|
||||
private Date date;
|
||||
private String author;
|
||||
private Changes changes;
|
||||
|
||||
public Commit(String message, String author, Date date) {
|
||||
this.message = message;
|
||||
@ -25,4 +26,12 @@ public class Commit {
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public Changes getChanges() {
|
||||
return changes;
|
||||
}
|
||||
|
||||
public void setChanges(Changes changes) {
|
||||
this.changes = changes;
|
||||
}
|
||||
}
|
||||
|
50
src/main/java/ru/ulstu/extractor/FileChange.java
Normal file
50
src/main/java/ru/ulstu/extractor/FileChange.java
Normal 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;
|
||||
}
|
||||
}
|
@ -2,12 +2,14 @@ package ru.ulstu.extractor;
|
||||
|
||||
import org.eclipse.jgit.api.Git;
|
||||
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.lib.Repository;
|
||||
import org.eclipse.jgit.revwalk.RevCommit;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
@ -15,6 +17,7 @@ import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.apache.logging.log4j.util.Strings.isBlank;
|
||||
|
||||
@ -23,7 +26,6 @@ public class GitRepositoryService {
|
||||
@Value("${extractor.custom-projects-dir}")
|
||||
private String customProjectsDir;
|
||||
|
||||
|
||||
public void cloneOrUpdateRepo(String url) throws GitAPIException, IOException {
|
||||
Git git;
|
||||
if (projectDirExists(getProjectDirectoryFile(url))) {
|
||||
@ -37,7 +39,7 @@ public class GitRepositoryService {
|
||||
.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) {
|
||||
@ -69,14 +71,58 @@ public class GitRepositoryService {
|
||||
git.log().call().forEach(commits::add);
|
||||
|
||||
List<Commit> list = new ArrayList<>();
|
||||
for (RevCommit commit : commits) {
|
||||
Commit fullMessage = new Commit(
|
||||
commit.getFullMessage(),
|
||||
commit.getAuthorIdent().getName(),
|
||||
Date.from(Instant.ofEpochSecond(commit.getCommitTime())));
|
||||
list.add(fullMessage);
|
||||
RevCommit prevCommit = null;
|
||||
for (RevCommit revCommit : commits) {
|
||||
Commit commit = new Commit(
|
||||
revCommit.getFullMessage(),
|
||||
revCommit.getAuthorIdent().getName(),
|
||||
Date.from(Instant.ofEpochSecond(revCommit.getCommitTime())));
|
||||
if (prevCommit != null) {
|
||||
commit.setChanges(findDiffBetweenTwoRevisions(revCommit, prevCommit, localRepo));
|
||||
}
|
||||
list.add(commit);
|
||||
prevCommit = revCommit;
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
32
src/main/java/ru/ulstu/extractor/LineChange.java
Normal file
32
src/main/java/ru/ulstu/extractor/LineChange.java
Normal 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;
|
||||
}
|
||||
|
||||
}
|
47
src/main/java/ru/ulstu/extractor/NewClass.java
Normal file
47
src/main/java/ru/ulstu/extractor/NewClass.java
Normal 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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user