public void postEventMap() throws GitAPIException { Map<DiffEntry, String> diffMap = new HashMap<>(); try (OutputStream stream = new ByteArrayOutputStream()) { List<DiffEntry> list = this.git.diff().setOutputStream(stream).call(); BufferedReader reader = new BufferedReader(new StringReader(stream.toString())); for (DiffEntry entry : list) { String line = reader.readLine(); StringWriter diff = new StringWriter(); while (line != null && !line.startsWith("diff")) { diff.append(line); diff.write('\n'); line = reader.readLine(); } diffMap.put(entry, diff.toString()); } } catch (IOException exc) { LOGGER.trace("Reading of git information failed!", exc); } catch (JGitInternalException gitException) { LOGGER.trace("Could not create Diff!", gitException); } this.eventBus.post(diffMap); }
public DiffScmResult callDiff( Git git, ScmVersion startRevision, ScmVersion endRevision ) throws IOException, GitAPIException, ScmException { AbstractTreeIterator oldTree = null; if ( startRevision != null && StringUtils.isNotEmpty( startRevision.getName().trim() ) ) { String startRev = startRevision.getName().trim(); oldTree = getTreeIterator( git.getRepository(), startRev ); } AbstractTreeIterator newTree = null; if ( endRevision != null && StringUtils.isNotEmpty( endRevision.getName().trim() ) ) { String endRev = endRevision.getName().trim(); newTree = getTreeIterator( git.getRepository(), endRev ); } OutputStream out = new ByteArrayOutputStream(); git.diff().setOutputStream( out ).setOldTree( oldTree ).setNewTree( newTree ).setCached( false ).call(); git.diff().setOutputStream( out ).setOldTree( oldTree ).setNewTree( newTree ).setCached( true ).call(); out.flush(); GitDiffConsumer consumer = new GitDiffConsumer( getLogger(), null ); String fullDiff = out.toString(); out.close(); String[] lines = fullDiff.split( "\n" ); for ( String aLine : lines ) { consumer.consumeLine( aLine ); } return new DiffScmResult( "JGit diff", consumer.getChangedFiles(), consumer.getDifferences(), consumer.getPatch() ); }
public DiffScmResult callDiff( Git git, ScmVersion startRevision, ScmVersion endRevision ) throws IOException, GitAPIException, ScmException { AbstractTreeIterator oldTree = null; if ( startRevision != null && StringUtils.isNotEmpty( startRevision.getName().trim() ) ) { String startRev = startRevision.getName().trim(); oldTree = getTreeIterator( git.getRepository(), startRev ); } AbstractTreeIterator newTree = null; if ( endRevision != null && StringUtils.isNotEmpty( endRevision.getName().trim() ) ) { String endRev = endRevision.getName().trim(); newTree = getTreeIterator( git.getRepository(), endRev ); } OutputStream out = new ByteArrayOutputStream(); git.diff().setOutputStream( out ).setOldTree( oldTree ).setNewTree( newTree ).setCached( false ).call(); git.diff().setOutputStream( out ).setOldTree( oldTree ).setNewTree( newTree ).setCached( true ).call(); out.flush(); GitDiffConsumer consumer = new GitDiffConsumer( getLogger(), null ); String fullDiff = out.toString(); out.close(); String[] lines = fullDiff.split( "\n" ); for ( String aLine : lines ) { consumer.consumeLine( aLine ); } return new DiffScmResult( "JGit diff", consumer.getChangedFiles(), consumer.getDifferences(), consumer.getPatch() ); }
.setOutputStream(computer.receiver()) .setOldTree(prepareTreeParser(repo, targetRef)) .setPathFilter(PathFilter.create(toGitPath(repoRootDir.relativize(path).toString())))
/** * Get the diff of a file against a particular commit * * @param jobId The job config to diff * @param commitId If specified, the commit to compare against; otherwise, compare against the latest revision * @return A string description of the diff, comparable to the output of "git diff filename" * @throws GitAPIException If there is a problem fetching the diff from git * @throws IOException If there is a problem reading from the file */ public String getDiff(String jobId, String commitId) throws GitAPIException, IOException { OutputStream out = new ByteArrayOutputStream(); DiffCommand diff = git.diff() .setPathFilter(PathFilter.create(getPathForJobId(jobId))) .setOutputStream(out) .setSourcePrefix("old:") .setDestinationPrefix("new:"); if (commitId != null) { diff.setOldTree(getTreeIterator(commitId)); diff.setNewTree(getTreeIterator("HEAD")); } diff.call(); return out.toString(); }