@Test public void testcomputePatchStats() throws GitAPIException, IOException { JobStatus jobStatus = new JobStatus("fakePomDirPath"); String remoteRepo = "https: String parentCommit = "2d65266f9a52b27f955ec9a74aa9ab4dac5537d7"; String commit = "f267c73200e2ebb9431d6ffe80e507222567696c"; tmpDir = java.nio.file.Files.createTempDirectory("jtravis").toFile(); Git git = Git.cloneRepository().setURI(remoteRepo).setBranch("master").setDirectory(tmpDir).call(); RevWalk revwalk = new RevWalk(git.getRepository()); RevCommit revParentCommit = revwalk.parseCommit(ObjectId.fromString(parentCommit)); RevCommit revCommit = revwalk.parseCommit(ObjectId.fromString(commit)); GitHelper gitHelper = new GitHelper(); gitHelper.computePatchStats(jobStatus, git, revCommit, revParentCommit); PatchDiff patchDiff = jobStatus.getProperties().getPatchDiff(); assertEquals(8, patchDiff.getFiles().getNumberAdded()); assertEquals(1, patchDiff.getFiles().getNumberDeleted()); assertEquals(4, patchDiff.getFiles().getNumberChanged()); assertEquals(405, patchDiff.getLines().getNumberAdded()); assertEquals(104, patchDiff.getLines().getNumberDeleted()); }
public void computePatchStats(JobStatus jobStatus, 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<>(); Set<String> addedFiles = new HashSet<>(); Set<String> deletedFiles = new HashSet<>(); for (DiffEntry entry : entries) { String path; if (entry.getChangeType() == DiffEntry.ChangeType.DELETE) { path = entry.getOldPath(); } else { path = entry.getNewPath(); } if (!jobStatus.isCreatedFileToPush(path) && path.endsWith(".java")) { if (entry.getChangeType() == DiffEntry.ChangeType.MODIFY || entry.getChangeType() == DiffEntry.ChangeType.RENAME) { changedFiles.add(path); } else if (entry.getChangeType() == DiffEntry.ChangeType.ADD || entry.getChangeType() == DiffEntry.ChangeType.COPY) { addedFiles.add(path); } else if (entry.getChangeType() == DiffEntry.ChangeType.DELETE) { deletedFiles.add(path); } FileHeader fileHeader = diffFormatter.toFileHeader(entry); List<? extends HunkHeader> hunks = fileHeader.getHunks(); for (HunkHeader hunk : hunks) { EditList edits = hunk.toEditList(); for (Edit edit : edits) { switch (edit.getType()) { case INSERT: nbLineAdded += edit.getLengthB(); break; case DELETE: nbLineDeleted += edit.getLengthA(); break; case REPLACE: int diff = edit.getLengthA() - edit.getLengthB(); if (diff > 0) { nbLineAdded += edit.getLengthA(); nbLineDeleted += edit.getLengthB(); } else { nbLineDeleted += edit.getLengthA(); nbLineAdded += edit.getLengthB(); } break; case EMPTY: break; } } } } } PatchDiff patchDiff = jobStatus.getProperties().getPatchDiff(); patchDiff.getFiles().setNumberAdded(addedFiles.size()); patchDiff.getFiles().setNumberChanged(changedFiles.size()); patchDiff.getFiles().setNumberDeleted(deletedFiles.size()); patchDiff.getLines().setNumberAdded(nbLineAdded); patchDiff.getLines().setNumberDeleted(nbLineDeleted); } catch (IOException e) { this.getLogger().error("Error while computing stat on the patch.", e); } }
GitHelper { public void computePatchStats(JobStatus jobStatus, 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<>(); Set<String> addedFiles = new HashSet<>(); Set<String> deletedFiles = new HashSet<>(); for (DiffEntry entry : entries) { String path; if (entry.getChangeType() == DiffEntry.ChangeType.DELETE) { path = entry.getOldPath(); } else { path = entry.getNewPath(); } if (!jobStatus.isCreatedFileToPush(path) && path.endsWith(".java")) { if (entry.getChangeType() == DiffEntry.ChangeType.MODIFY || entry.getChangeType() == DiffEntry.ChangeType.RENAME) { changedFiles.add(path); } else if (entry.getChangeType() == DiffEntry.ChangeType.ADD || entry.getChangeType() == DiffEntry.ChangeType.COPY) { addedFiles.add(path); } else if (entry.getChangeType() == DiffEntry.ChangeType.DELETE) { deletedFiles.add(path); } FileHeader fileHeader = diffFormatter.toFileHeader(entry); List<? extends HunkHeader> hunks = fileHeader.getHunks(); for (HunkHeader hunk : hunks) { EditList edits = hunk.toEditList(); for (Edit edit : edits) { switch (edit.getType()) { case INSERT: nbLineAdded += edit.getLengthB(); break; case DELETE: nbLineDeleted += edit.getLengthA(); break; case REPLACE: int diff = edit.getLengthA() - edit.getLengthB(); if (diff > 0) { nbLineAdded += edit.getLengthA(); nbLineDeleted += edit.getLengthB(); } else { nbLineDeleted += edit.getLengthA(); nbLineAdded += edit.getLengthB(); } break; case EMPTY: break; } } } } } PatchDiff patchDiff = jobStatus.getProperties().getPatchDiff(); patchDiff.getFiles().setNumberAdded(addedFiles.size()); patchDiff.getFiles().setNumberChanged(changedFiles.size()); patchDiff.getFiles().setNumberDeleted(deletedFiles.size()); patchDiff.getLines().setNumberAdded(nbLineAdded); patchDiff.getLines().setNumberDeleted(nbLineDeleted); } catch (IOException e) { this.getLogger().error("Error while computing stat on the patch.", e); } } }
GitHelper { public void computePatchStats(JobStatus jobStatus, 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<>(); Set<String> addedFiles = new HashSet<>(); Set<String> deletedFiles = new HashSet<>(); for (DiffEntry entry : entries) { String path; if (entry.getChangeType() == DiffEntry.ChangeType.DELETE) { path = entry.getOldPath(); } else { path = entry.getNewPath(); } if (!jobStatus.isCreatedFileToPush(path) && path.endsWith(".java")) { if (entry.getChangeType() == DiffEntry.ChangeType.MODIFY || entry.getChangeType() == DiffEntry.ChangeType.RENAME) { changedFiles.add(path); } else if (entry.getChangeType() == DiffEntry.ChangeType.ADD || entry.getChangeType() == DiffEntry.ChangeType.COPY) { addedFiles.add(path); } else if (entry.getChangeType() == DiffEntry.ChangeType.DELETE) { deletedFiles.add(path); } FileHeader fileHeader = diffFormatter.toFileHeader(entry); List<? extends HunkHeader> hunks = fileHeader.getHunks(); for (HunkHeader hunk : hunks) { EditList edits = hunk.toEditList(); for (Edit edit : edits) { switch (edit.getType()) { case INSERT: nbLineAdded += edit.getLengthB(); break; case DELETE: nbLineDeleted += edit.getLengthA(); break; case REPLACE: int diff = edit.getLengthA() - edit.getLengthB(); if (diff > 0) { nbLineAdded += edit.getLengthA(); nbLineDeleted += edit.getLengthB(); } else { nbLineDeleted += edit.getLengthA(); nbLineAdded += edit.getLengthB(); } break; case EMPTY: break; } } } } } PatchDiff patchDiff = jobStatus.getProperties().getPatchDiff(); patchDiff.getFiles().setNumberAdded(addedFiles.size()); patchDiff.getFiles().setNumberChanged(changedFiles.size()); patchDiff.getFiles().setNumberDeleted(deletedFiles.size()); patchDiff.getLines().setNumberAdded(nbLineAdded); patchDiff.getLines().setNumberDeleted(nbLineDeleted); } catch (IOException e) { this.getLogger().error("Error while computing stat on the patch.", e); } } GitHelper(); }
GitHelper { public void computePatchStats(JobStatus jobStatus, 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<>(); Set<String> addedFiles = new HashSet<>(); Set<String> deletedFiles = new HashSet<>(); for (DiffEntry entry : entries) { String path; if (entry.getChangeType() == DiffEntry.ChangeType.DELETE) { path = entry.getOldPath(); } else { path = entry.getNewPath(); } if (!jobStatus.isCreatedFileToPush(path) && path.endsWith(".java")) { if (entry.getChangeType() == DiffEntry.ChangeType.MODIFY || entry.getChangeType() == DiffEntry.ChangeType.RENAME) { changedFiles.add(path); } else if (entry.getChangeType() == DiffEntry.ChangeType.ADD || entry.getChangeType() == DiffEntry.ChangeType.COPY) { addedFiles.add(path); } else if (entry.getChangeType() == DiffEntry.ChangeType.DELETE) { deletedFiles.add(path); } FileHeader fileHeader = diffFormatter.toFileHeader(entry); List<? extends HunkHeader> hunks = fileHeader.getHunks(); for (HunkHeader hunk : hunks) { EditList edits = hunk.toEditList(); for (Edit edit : edits) { switch (edit.getType()) { case INSERT: nbLineAdded += edit.getLengthB(); break; case DELETE: nbLineDeleted += edit.getLengthA(); break; case REPLACE: int diff = edit.getLengthA() - edit.getLengthB(); if (diff > 0) { nbLineAdded += edit.getLengthA(); nbLineDeleted += edit.getLengthB(); } else { nbLineDeleted += edit.getLengthA(); nbLineAdded += edit.getLengthB(); } break; case EMPTY: break; } } } } } PatchDiff patchDiff = jobStatus.getProperties().getPatchDiff(); patchDiff.getFiles().setNumberAdded(addedFiles.size()); patchDiff.getFiles().setNumberChanged(changedFiles.size()); patchDiff.getFiles().setNumberDeleted(deletedFiles.size()); patchDiff.getLines().setNumberAdded(nbLineAdded); patchDiff.getLines().setNumberDeleted(nbLineDeleted); } catch (IOException e) { this.getLogger().error("Error while computing stat on the patch.", e); } } GitHelper(); int getNbCommits(); String testCommitExistence(Git git, String oldCommitSha, AbstractStep step, Build build); static PersonIdent getCommitterIdent(); boolean addAndCommitRepairnatorLogAndProperties(JobStatus status, Git git, String commitMsg); void initAllSubmodules(Git git); boolean mergeTwoCommitsForPR(Git git, Build build, PullRequest prInformation, String repository, AbstractStep step, List<String> paths); String forkRepository(String repository, AbstractStep step); void computePatchStats(JobStatus jobStatus, Git git, RevCommit headRev, RevCommit commit); static int gitCreateNewBranchAndCheckoutIt(String path, String branchName); void gitAdd(List<String> files, Git git); void gitResetPaths(String commit, List<String> paths, File gitDirectory); void executeGitCommand(String[] gitCommand, File gitDirectory); List<String> removeDuplicatePaths(List<String> paths); }
GitHelper { public void computePatchStats(JobStatus jobStatus, 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<>(); Set<String> addedFiles = new HashSet<>(); Set<String> deletedFiles = new HashSet<>(); for (DiffEntry entry : entries) { String path; if (entry.getChangeType() == DiffEntry.ChangeType.DELETE) { path = entry.getOldPath(); } else { path = entry.getNewPath(); } if (!jobStatus.isCreatedFileToPush(path) && path.endsWith(".java")) { if (entry.getChangeType() == DiffEntry.ChangeType.MODIFY || entry.getChangeType() == DiffEntry.ChangeType.RENAME) { changedFiles.add(path); } else if (entry.getChangeType() == DiffEntry.ChangeType.ADD || entry.getChangeType() == DiffEntry.ChangeType.COPY) { addedFiles.add(path); } else if (entry.getChangeType() == DiffEntry.ChangeType.DELETE) { deletedFiles.add(path); } FileHeader fileHeader = diffFormatter.toFileHeader(entry); List<? extends HunkHeader> hunks = fileHeader.getHunks(); for (HunkHeader hunk : hunks) { EditList edits = hunk.toEditList(); for (Edit edit : edits) { switch (edit.getType()) { case INSERT: nbLineAdded += edit.getLengthB(); break; case DELETE: nbLineDeleted += edit.getLengthA(); break; case REPLACE: int diff = edit.getLengthA() - edit.getLengthB(); if (diff > 0) { nbLineAdded += edit.getLengthA(); nbLineDeleted += edit.getLengthB(); } else { nbLineDeleted += edit.getLengthA(); nbLineAdded += edit.getLengthB(); } break; case EMPTY: break; } } } } } PatchDiff patchDiff = jobStatus.getProperties().getPatchDiff(); patchDiff.getFiles().setNumberAdded(addedFiles.size()); patchDiff.getFiles().setNumberChanged(changedFiles.size()); patchDiff.getFiles().setNumberDeleted(deletedFiles.size()); patchDiff.getLines().setNumberAdded(nbLineAdded); patchDiff.getLines().setNumberDeleted(nbLineDeleted); } catch (IOException e) { this.getLogger().error("Error while computing stat on the patch.", e); } } GitHelper(); int getNbCommits(); String testCommitExistence(Git git, String oldCommitSha, AbstractStep step, Build build); static PersonIdent getCommitterIdent(); boolean addAndCommitRepairnatorLogAndProperties(JobStatus status, Git git, String commitMsg); void initAllSubmodules(Git git); boolean mergeTwoCommitsForPR(Git git, Build build, PullRequest prInformation, String repository, AbstractStep step, List<String> paths); String forkRepository(String repository, AbstractStep step); void computePatchStats(JobStatus jobStatus, Git git, RevCommit headRev, RevCommit commit); static int gitCreateNewBranchAndCheckoutIt(String path, String branchName); void gitAdd(List<String> files, Git git); void gitResetPaths(String commit, List<String> paths, File gitDirectory); void executeGitCommand(String[] gitCommand, File gitDirectory); List<String> removeDuplicatePaths(List<String> paths);  }