use of org.eclipse.jgit.diff.DiffEntry in project repairnator by Spirals-Team.
the class TestCheckoutBuggyBuildSourceCode method testCheckoutPreviousBuildSourceCodeWithPR.
@Test
public void testCheckoutPreviousBuildSourceCodeWithPR() throws IOException, GitAPIException, RepairnatorConfigException {
// HubSpot/Singularity
int buildId = 223248816;
int previousBuildId = 222209171;
ScannedBuildStatus status = ScannedBuildStatus.PASSING_AND_PASSING_WITH_TEST_CHANGES;
Build build = BuildHelper.getBuildFromId(buildId, null);
assertThat(build, notNullValue());
assertThat(buildId, is(build.getId()));
assertThat(build.isPullRequest(), is(true));
Build previousBuild = BuildHelper.getBuildFromId(previousBuildId, null);
assertThat(previousBuild, notNullValue());
assertThat(previousBuild.getId(), is(previousBuildId));
assertThat(previousBuild.isPullRequest(), is(true));
Path tmpDirPath = Files.createTempDirectory("test_checkoutprevious");
File tmpDir = tmpDirPath.toFile();
tmpDir.deleteOnExit();
BuildToBeInspected toBeInspected = new BuildToBeInspected(previousBuild, build, status, "");
ProjectInspector inspector = mock(ProjectInspector.class);
when(inspector.getWorkspace()).thenReturn(tmpDir.getAbsolutePath());
when(inspector.getRepoLocalPath()).thenReturn(tmpDir.getAbsolutePath() + "/repo");
when(inspector.getRepoToPushLocalPath()).thenReturn(tmpDir.getAbsolutePath() + "/repotopush");
when(inspector.getBuildToBeInspected()).thenReturn(toBeInspected);
when(inspector.getPatchedBuild()).thenReturn(build);
when(inspector.getBuggyBuild()).thenReturn(previousBuild);
when(inspector.getGitHelper()).thenReturn(new GitHelper());
JobStatus jobStatus = new JobStatus(tmpDir.getAbsolutePath() + "/repo");
when(inspector.getJobStatus()).thenReturn(jobStatus);
CloneRepository cloneStep = new CloneRepository(inspector);
CheckoutBuggyBuildSourceCode checkoutBuild = new CheckoutBuggyBuildSourceCode(inspector);
cloneStep.setNextStep(checkoutBuild);
cloneStep.execute();
assertThat(checkoutBuild.getPipelineState(), is(PipelineState.PREVIOUSBUILDCODECHECKEDOUT));
assertThat(jobStatus.getPipelineState(), is(PipelineState.PREVIOUSBUILDCODECHECKEDOUT));
assertThat(checkoutBuild.isShouldStop(), is(false));
Git gitDir = Git.open(new File(tmpDir, "repo"));
Iterable<RevCommit> logs = gitDir.log().call();
Iterator<RevCommit> iterator = logs.iterator();
boolean foundRightCommitAfterRepairCommits = false;
boolean foundUndoSourceCodeCommit = false;
boolean stopSearch = false;
while (iterator.hasNext() && !stopSearch) {
RevCommit revCommit = iterator.next();
System.out.println(revCommit.getShortMessage());
if (revCommit.getShortMessage().equals("Undo changes on source code")) {
foundUndoSourceCodeCommit = true;
CanonicalTreeParser oldTreeIter = new CanonicalTreeParser();
CanonicalTreeParser newTreeIter = new CanonicalTreeParser();
ObjectReader reader = gitDir.getRepository().newObjectReader();
RevCommit prevCommit = iterator.next();
oldTreeIter.reset(reader, prevCommit.getTree());
newTreeIter.reset(reader, revCommit.getTree());
List<DiffEntry> diff = gitDir.diff().setOldTree(oldTreeIter).setNewTree(newTreeIter).call();
for (DiffEntry entry : diff) {
assertThat(entry.getOldPath(), startsWith("src/main/java/"));
}
revCommit = prevCommit;
}
if (!revCommit.getShortMessage().contains("repairnator") && !revCommit.getShortMessage().contains("merge")) {
stopSearch = true;
}
}
assertThat(foundUndoSourceCodeCommit, is(true));
}
use of org.eclipse.jgit.diff.DiffEntry in project repairnator by Spirals-Team.
the class CommitPatch method computePatchStats.
private void computePatchStats(Git git, RevCommit headRev, RevCommit commit) {
try {
ObjectReader reader = git.getRepository().newObjectReader();
CanonicalTreeParser oldTreeIter = new CanonicalTreeParser();
oldTreeIter.reset(reader, headRev.getTree());
CanonicalTreeParser newTreeIter = new CanonicalTreeParser();
newTreeIter.reset(reader, commit.getTree());
DiffFormatter diffFormatter = new DiffFormatter(DisabledOutputStream.INSTANCE);
diffFormatter.setRepository(git.getRepository());
diffFormatter.setContext(0);
List<DiffEntry> entries = diffFormatter.scan(newTreeIter, oldTreeIter);
int nbLineAdded = 0;
int nbLineDeleted = 0;
Set<String> changedFiles = new HashSet<>();
for (DiffEntry entry : entries) {
String path;
if (entry.getChangeType() == DiffEntry.ChangeType.DELETE) {
path = entry.getOldPath();
} else {
path = entry.getNewPath();
}
if (!path.contains("repairnator")) {
changedFiles.add(path);
FileHeader fileHeader = diffFormatter.toFileHeader(entry);
List<? extends HunkHeader> hunks = fileHeader.getHunks();
for (HunkHeader hunk : hunks) {
nbLineAdded += hunk.getOldImage().getLinesAdded();
nbLineDeleted += hunk.getOldImage().getLinesDeleted();
}
}
}
Metrics metric = this.getInspector().getJobStatus().getMetrics();
metric.setPatchAddedLines(nbLineAdded);
metric.setPatchDeletedLines(nbLineDeleted);
metric.setPatchChangedFiles(changedFiles.size());
} catch (IOException e) {
this.getLogger().error("Error while computing stat on the patch", e);
}
}
use of org.eclipse.jgit.diff.DiffEntry in project gitblit by gitblit.
the class DiffUtils method getCommitPatch.
/**
* Returns the diff between the two commits for the specified file or folder
* formatted as a patch.
*
* @param repository
* @param baseCommit
* if base commit is unspecified, the patch is generated against
* the primary parent of the specified commit.
* @param commit
* @param path
* if path is specified, the patch is generated only for the
* specified file or folder. if unspecified, the patch is
* generated for the entire diff between the two commits.
* @return patch as a string
*/
public static String getCommitPatch(Repository repository, RevCommit baseCommit, RevCommit commit, String path) {
String diff = null;
try {
final ByteArrayOutputStream os = new ByteArrayOutputStream();
RawTextComparator cmp = RawTextComparator.DEFAULT;
PatchFormatter df = new PatchFormatter(os);
df.setRepository(repository);
df.setDiffComparator(cmp);
df.setDetectRenames(true);
RevTree commitTree = commit.getTree();
RevTree baseTree;
if (baseCommit == null) {
if (commit.getParentCount() > 0) {
final RevWalk rw = new RevWalk(repository);
RevCommit parent = rw.parseCommit(commit.getParent(0).getId());
baseTree = parent.getTree();
} else {
// FIXME initial commit. no parent?!
baseTree = commitTree;
}
} else {
baseTree = baseCommit.getTree();
}
List<DiffEntry> diffEntries = df.scan(baseTree, commitTree);
if (path != null && path.length() > 0) {
for (DiffEntry diffEntry : diffEntries) {
if (diffEntry.getNewPath().equalsIgnoreCase(path)) {
df.format(diffEntry);
break;
}
}
} else {
df.format(diffEntries);
}
diff = df.getPatch(commit);
df.flush();
} catch (Throwable t) {
LOGGER.error("failed to generate commit diff!", t);
}
return diff;
}
use of org.eclipse.jgit.diff.DiffEntry in project gitblit by gitblit.
the class GitBlitDiffFormatter method getHtml.
/**
* Workaround function for complex private methods in DiffFormatter. This sets the html for the diff headers.
*
* @return
*/
public String getHtml() {
String html = RawParseUtils.decode(os.toByteArray());
String[] lines = html.split("\n");
StringBuilder sb = new StringBuilder();
for (String line : lines) {
if (line.startsWith("index") || line.startsWith("similarity") || line.startsWith("rename from ") || line.startsWith("rename to ")) {
// skip index lines
} else if (line.startsWith("new file") || line.startsWith("deleted file")) {
// skip new file lines
} else if (line.startsWith("\\ No newline")) {
// skip no new line
} else if (line.startsWith("---") || line.startsWith("+++")) {
// skip --- +++ lines
} else if (line.startsWith("diff")) {
// skip diff lines
} else {
boolean gitLinkDiff = line.length() > 0 && line.substring(1).startsWith("Subproject commit");
if (gitLinkDiff) {
sb.append("<tr><th class='diff-line'></th><th class='diff-line'></th>");
if (line.charAt(0) == '+') {
sb.append("<th class='diff-state diff-state-add'></th><td class=\"diff-cell add2\">");
} else {
sb.append("<th class='diff-state diff-state-sub'></th><td class=\"diff-cell remove2\">");
}
line = StringUtils.escapeForHtml(line.substring(1), CONVERT_TABS, tabLength);
}
sb.append(line);
if (gitLinkDiff) {
sb.append("</td></tr>");
}
sb.append('\n');
}
}
if (truncated) {
sb.append(MessageFormat.format("<div class='header'><div class='diffHeader'>{0}</div></div>", StringUtils.escapeForHtml(getMsg("gb.diffTruncated", "Diff truncated after the above file"), false)));
// List all files not shown. We can be sure we do have at least one path in skipped.
sb.append("<div class='diff'><table cellpadding='0'><tbody><tr><td class='diff-cell' colspan='4'>");
String deletedSuffix = StringUtils.escapeForHtml(getMsg("gb.diffDeletedFileSkipped", "(deleted)"), false);
boolean first = true;
for (DiffEntry entry : skipped) {
if (!first) {
sb.append('\n');
}
if (ChangeType.DELETE.equals(entry.getChangeType())) {
sb.append("<span id=\"n" + entry.getOldId().name() + "\">" + StringUtils.escapeForHtml(entry.getOldPath(), false) + ' ' + deletedSuffix + "</span>");
} else {
sb.append("<span id=\"n" + entry.getNewId().name() + "\">" + StringUtils.escapeForHtml(entry.getNewPath(), false) + "</span>");
}
first = false;
}
skipped.clear();
sb.append("</td></tr></tbody></table></div>");
}
return sb.toString();
}
use of org.eclipse.jgit.diff.DiffEntry in project searchcode-server by boyter.
the class IndexGitRepoJob method updateGitRepository.
/**
* Update a git repository and return if it has changed and the differences
*/
public RepositoryChanged updateGitRepository(RepoResult repoResult, String repoLocations, boolean useCredentials) {
boolean changed = false;
List<String> changedFiles = new ArrayList<>();
List<String> deletedFiles = new ArrayList<>();
this.logger.info(String.format("6cffea0f::attempting to pull latest from %s for %s", repoLocations, repoResult.getName()));
Repository localRepository = null;
Git git = null;
try {
localRepository = new FileRepository(new File(repoLocations + "/" + repoResult.getDirectoryName() + "/.git"));
Ref head = localRepository.getRef("HEAD");
git = new Git(localRepository);
git.reset();
git.clean();
PullCommand pullCmd = git.pull();
if (useCredentials) {
pullCmd.setCredentialsProvider(new UsernamePasswordCredentialsProvider(repoResult.getUsername(), repoResult.getPassword()));
}
pullCmd.call();
Ref newHEAD = localRepository.getRef("HEAD");
if (!head.toString().equals(newHEAD.toString())) {
changed = true;
// Get the differences between the the heads which we updated at
// and use these to just update the differences between them
ObjectId oldHead = localRepository.resolve(head.getObjectId().getName() + "^{tree}");
ObjectId newHead = localRepository.resolve(newHEAD.getObjectId().getName() + "^{tree}");
ObjectReader reader = localRepository.newObjectReader();
CanonicalTreeParser oldTreeIter = new CanonicalTreeParser();
oldTreeIter.reset(reader, oldHead);
CanonicalTreeParser newTreeIter = new CanonicalTreeParser();
newTreeIter.reset(reader, newHead);
List<DiffEntry> entries = git.diff().setNewTree(newTreeIter).setOldTree(oldTreeIter).call();
for (DiffEntry entry : entries) {
if ("DELETE".equals(entry.getChangeType().name())) {
deletedFiles.add(FilenameUtils.separatorsToUnix(entry.getOldPath()));
} else {
changedFiles.add(FilenameUtils.separatorsToUnix(entry.getNewPath()));
}
}
}
} catch (IOException | GitAPIException | InvalidPathException ex) {
changed = false;
String error = String.format("c6646806::error in class %s exception %s repository %s", ex.getClass(), ex.getMessage(), repoResult.getName());
this.logger.severe(error);
repoResult.getData().indexError = error;
Singleton.getRepo().saveRepo(repoResult);
} finally {
Singleton.getHelpers().closeQuietly(localRepository);
Singleton.getHelpers().closeQuietly(git);
}
return new RepositoryChanged(changed, changedFiles, deletedFiles);
}
Aggregations