List of usage examples for org.eclipse.jgit.diff DiffFormatter setDiffComparator
public void setDiffComparator(RawTextComparator cmp)
From source file:boa.datagen.scm.GitCommit.java
License:Apache License
private void getChangeFiles(final RevCommit parent, final RevCommit rc, final HashMap<String, String> rChangedPaths, final HashMap<String, String> rRemovedPaths, final HashMap<String, String> rAddedPaths) { final DiffFormatter df = new DiffFormatter(NullOutputStream.INSTANCE); df.setRepository(repository);//w w w .ja v a 2s .c o m df.setDiffComparator(RawTextComparator.DEFAULT); df.setDetectRenames(true); try { final AbstractTreeIterator parentIter; if (parent == null) parentIter = new EmptyTreeIterator(); else parentIter = new CanonicalTreeParser(null, repository.newObjectReader(), parent.getTree()); for (final DiffEntry diff : df.scan(parentIter, new CanonicalTreeParser(null, repository.newObjectReader(), rc.getTree()))) { if (diff.getChangeType() == ChangeType.MODIFY || diff.getChangeType() == ChangeType.COPY || diff.getChangeType() == ChangeType.RENAME) { if (diff.getOldMode().getObjectType() == Constants.OBJ_BLOB && diff.getNewMode().getObjectType() == Constants.OBJ_BLOB) { String path = diff.getNewPath(); rChangedPaths.put(path, diff.getOldPath()); filePathGitObjectIds.put(path, diff.getNewId().toObjectId()); } } else if (diff.getChangeType() == ChangeType.ADD) { if (diff.getNewMode().getObjectType() == Constants.OBJ_BLOB) { String path = diff.getNewPath(); rAddedPaths.put(path, null); filePathGitObjectIds.put(path, diff.getNewId().toObjectId()); } } else if (diff.getChangeType() == ChangeType.DELETE) { if (diff.getOldMode().getObjectType() == Constants.OBJ_BLOB) { rRemovedPaths.put(diff.getOldPath(), diff.getOldPath()); } } } } catch (final IOException e) { if (debug) System.err.println("Git Error getting commit diffs: " + e.getMessage()); } df.close(); }
From source file:br.com.metricminer2.scm.GitRepository.java
License:Apache License
private List<DiffEntry> diffsForTheCommit(Repository repo, RevCommit commit) throws IOException, AmbiguousObjectException, IncorrectObjectTypeException { AnyObjectId currentCommit = repo.resolve(commit.getName()); AnyObjectId parentCommit = commit.getParentCount() > 0 ? repo.resolve(commit.getParent(0).getName()) : null; DiffFormatter df = new DiffFormatter(DisabledOutputStream.INSTANCE); df.setBinaryFileThreshold(2 * 1024); // 2 mb max a file df.setRepository(repo);//from w ww. j av a 2 s .co m df.setDiffComparator(RawTextComparator.DEFAULT); df.setDetectRenames(true); List<DiffEntry> diffs = null; if (parentCommit == null) { RevWalk rw = new RevWalk(repo); diffs = df.scan(new EmptyTreeIterator(), new CanonicalTreeParser(null, rw.getObjectReader(), commit.getTree())); rw.release(); } else { diffs = df.scan(parentCommit, currentCommit); } df.release(); return diffs; }
From source file:co.bledo.gitmin.servlet.Review.java
License:Apache License
public Response commit(Request req) throws IOException, GitAPIException { String repoName = req.getParam("repo", ""); String hash = req.getParam("hash", ""); Git git = Git.open(new File(GitminConfig.getGitRepositoriesPath() + "/" + repoName)); /*// w w w .ja v a 2s .c o m ByteArrayOutputStream baos1 = new ByteArrayOutputStream(); List<DiffEntry> diffTest = git.diff().setOutputStream(baos1) .setOldTree(getTreeIterator(git.getRepository(), hash)) .setNewTree(getTreeIterator(git.getRepository(), hash + "^")) .call(); System.out.println(baos1.toString()); */ RepositoryBuilder builder = new RepositoryBuilder(); Repository repo = builder.setGitDir(new File(GitminConfig.getGitRepositoriesPath() + "/" + repoName)) .readEnvironment().findGitDir().build(); RevWalk rw = new RevWalk(repo); ObjectId hashOid = repo.resolve(hash); RevCommit commit = rw.parseCommit(hashOid); RevCommit parent = rw.parseCommit(commit.getParent(0).getId()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); DiffFormatter df = new DiffFormatter(baos); df.setRepository(repo); df.setDiffComparator(RawTextComparator.DEFAULT); df.setDetectRenames(true); List<DiffEntry> diffs = df.scan(parent, commit); List<CommitInfo> commitInfos = new ArrayList<CommitInfo>(); for (DiffEntry diff : diffs) { CommitInfo nfo = new CommitInfo(); df.format(diff); nfo.diff = baos.toString(); nfo.oldContents = getFileContent(repo, parent, diff.getOldPath()); nfo.newContents = getFileContent(repo, parent, diff.getNewPath()); nfo.newFile = diff.getNewPath(); nfo.oldFile = diff.getOldPath(); commitInfos.add(nfo); } VelocityResponse resp = VelocityResponse.newInstance(req, this); resp.assign("nfolist", commitInfos); return log.exit(resp); }
From source file:com.GitAnalytics.CommitInfo.java
public static List<CommitInfo> getCommits(Repository repo, Iterable<RevCommit> commits, List<Ref> tags) throws Exception { List<CommitInfo> list = new LinkedList<>(); DiffFormatter df = new DiffFormatter(DisabledOutputStream.INSTANCE); df.setRepository(repo);/*from w ww . j a va 2 s .c om*/ df.setDiffComparator(RawTextComparator.DEFAULT); df.setDetectRenames(true); RevWalk rw = new RevWalk(repo); for (RevCommit commit : commits) { List<String> curTags = new LinkedList<>(); tags.stream().filter((tag) -> (tag.getObjectId().equals(ObjectId.fromString(commit.getName())))) .forEachOrdered((tag) -> { curTags.add(tag.getName()); }); list.add(new CommitInfo(df, rw, commit, curTags)); } return list; }
From source file:com.gitblit.utils.DiffUtils.java
License:Apache License
/** * Returns the diff between two commits for the specified file. * * @param repository/*from w ww .j a v a 2s. c om*/ * @param baseCommit * if base commit is null the diff is to the primary parent of * the commit. * @param commit * @param path * if the path is specified, the diff is restricted to that file * or folder. if unspecified, the diff is for the entire commit. * @param comparator * @param outputType * @param handler * to use for rendering binary diffs if {@code outputType} is {@link DiffOutputType#HTML HTML}. * May be {@code null}, resulting in the default behavior. * @param tabLength * @return the diff */ public static DiffOutput getDiff(Repository repository, RevCommit baseCommit, RevCommit commit, String path, DiffComparator comparator, DiffOutputType outputType, final BinaryDiffHandler handler, int tabLength) { DiffStat stat = null; String diff = null; try { ByteArrayOutputStream os = null; DiffFormatter df; switch (outputType) { case HTML: df = new GitBlitDiffFormatter(commit.getName(), repository, path, handler, tabLength); break; case PLAIN: default: os = new ByteArrayOutputStream(); df = new DiffFormatter(os); break; } df.setRepository(repository); df.setDiffComparator((comparator == null ? DiffComparator.SHOW_WHITESPACE : comparator).textComparator); 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()); rw.dispose(); 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); } df.flush(); if (df instanceof GitBlitDiffFormatter) { // workaround for complex private methods in DiffFormatter diff = ((GitBlitDiffFormatter) df).getHtml(); stat = ((GitBlitDiffFormatter) df).getDiffStat(); } else { diff = os.toString(); } } catch (Throwable t) { LOGGER.error("failed to generate commit diff!", t); } return new DiffOutput(outputType, diff, stat); }
From source file:com.gitblit.utils.JGitUtils.java
License:Apache License
/** * Returns the list of files changed in a specified commit. If the * repository does not exist or is empty, an empty list is returned. * * @param repository//from ww w . j a va2 s.c o m * @param startCommit * earliest commit * @param endCommit * most recent commit. if null, HEAD is assumed. * @return list of files changed in a commit range */ public static List<PathChangeModel> getFilesInRange(Repository repository, RevCommit startCommit, RevCommit endCommit) { List<PathChangeModel> list = new ArrayList<PathChangeModel>(); if (!hasCommits(repository)) { return list; } try { DiffFormatter df = new DiffFormatter(null); df.setRepository(repository); df.setDiffComparator(RawTextComparator.DEFAULT); df.setDetectRenames(true); List<DiffEntry> diffEntries = df.scan(startCommit.getTree(), endCommit.getTree()); for (DiffEntry diff : diffEntries) { PathChangeModel pcm = PathChangeModel.from(diff, endCommit.getName(), repository); list.add(pcm); } Collections.sort(list); } catch (Throwable t) { error(t, repository, "{0} failed to determine files in range {1}..{2}!", startCommit, endCommit); } return list; }
From source file:com.tasktop.c2c.server.scm.service.GitServiceBean.java
License:Open Source License
private static List<com.tasktop.c2c.server.scm.domain.DiffEntry> getDiffEntries(final Repository repo, final RevCommit baseCommit, final RevCommit commit, final RawTextComparator cmp, final Integer context) throws IOException { final DiffFormatter df = new DiffFormatter(NullOutputStream.INSTANCE); df.setRepository(repo);// ww w . j a va 2s . c o m df.setDiffComparator(cmp); df.setDetectRenames(true); RevTree baseTree = null; if (baseCommit != null) { baseTree = baseCommit.getTree(); } else { RevCommit parentCommit = getParentCommit(repo, commit); if (parentCommit != null) { baseTree = parentCommit.getTree(); } } final List<com.tasktop.c2c.server.scm.domain.DiffEntry> retval = new ArrayList<com.tasktop.c2c.server.scm.domain.DiffEntry>(); for (DiffEntry de : df.scan(getTreeIterator(repo, baseTree), getTreeIterator(repo, commit.getTree()))) { retval.add(GitBrowseUtil.getDiffEntry(de, repo, context)); } return retval; }
From source file:de.br0tbox.gitfx.ui.controllers.SingleProjectController.java
License:Apache License
private void showHistoryForCommit(final GitFxCommit selectedCommit) { Preconditions.checkNotNull(selectedCommit, "selectedCommit"); final Git git = projectModel.getFxProject().getGit(); final Repository repository = git.getRepository(); ObjectId resolve;/* www. j a v a 2s. co m*/ try { resolve = repository.resolve(selectedCommit.getHash()); final RevWalk revWalk = new RevWalk(repository); final RevCommit commit = revWalk.parseCommit(resolve); RevCommit parent = null; if (commit.getParents().length > 0 && commit.getParent(0) != null) { parent = revWalk.parseCommit(commit.getParent(0).getId()); } final ByteArrayOutputStream out = new ByteArrayOutputStream(); final DiffFormatter df = new DiffFormatter(out); df.setRepository(repository); df.setDiffComparator(RawTextComparator.DEFAULT); df.setDetectRenames(true); List<DiffEntry> diffs = null; if (parent != null) { diffs = df.scan(parent.getTree(), commit.getTree()); } else { diffs = df.scan(new EmptyTreeIterator(), new CanonicalTreeParser(null, revWalk.getObjectReader(), commit.getTree())); } final ObservableList items = commitList.getItems(); items.clear(); for (final DiffEntry diff : diffs) { df.format(diff); final String changes = out.toString("UTF-8"); changesView.setText(changes); if (ChangeType.DELETE.equals(diff.getChangeType())) { items.add(diff.getChangeType().toString().subSequence(0, 1) + " " + diff.getOldPath()); } else { items.add(diff.getChangeType().toString().subSequence(0, 1) + " " + diff.getNewPath()); } } revWalk.release(); df.release(); } catch (final IOException e) { LOGGER.error("Error while showing changes for commit " + selectedCommit.getHash(), e); } }
From source file:de.codesourcery.gittimelapse.GitHelper.java
License:Apache License
private Set<String> getFilesInCommit(RevCommit commit) throws IOException { if (commit == null) { throw new IllegalArgumentException("commit must not be NULL"); }/* w w w.jav a 2s. c om*/ List<PathChangeModel> list = new ArrayList<>(); final RevWalk rw = new RevWalk(repository); try { if (commit.getParentCount() == 0) { TreeWalk tw = new TreeWalk(repository); tw.reset(); tw.setRecursive(true); tw.addTree(commit.getTree()); while (tw.next()) { list.add(new PathChangeModel(tw.getPathString(), tw.getPathString(), 0, tw.getRawMode(0), tw.getObjectId(0).getName(), commit.getId().getName(), ChangeType.ADD)); } tw.release(); } else { RevCommit parent = rw.parseCommit(commit.getParent(0).getId()); final ByteArrayOutputStream out = new ByteArrayOutputStream(); final DiffFormatter df = new DiffFormatter(out); df.setRepository(repository); df.setDiffComparator(RawTextComparator.DEFAULT); df.setDetectRenames(false); final List<DiffEntry> diffs = df.scan(parent.getTree(), commit.getTree()); for (DiffEntry diff : diffs) { // create the path change model PathChangeModel pcm = PathChangeModel.from(diff, commit.getName()); list.add(pcm); } } } finally { rw.dispose(); } Set<String> result = new HashSet<>(); for (PathChangeModel model : list) { if (model.isFile()) { result.add(model.path); } } return result; }
From source file:de.fau.osr.core.vcs.impl.GitVcsClient.java
License:Open Source License
private Stream<CommitFile> getTreeDiffFiles(RevTree a, RevTree b, String commitID) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); DiffFormatter dif = new DiffFormatter(out); dif.setRepository(repo);/*from w w w. ja va 2s .c o m*/ dif.setDiffComparator(RawTextComparator.DEFAULT); dif.setDetectRenames(true); List<DiffEntry> diffs = dif.scan(a, b); return diffs.stream().map(diff -> { CommitState commitState; switch (diff.getChangeType()) { case ADD: commitState = CommitState.ADDED; break; case MODIFY: commitState = CommitState.MODIFIED; break; case RENAME: commitState = CommitState.RENAMED; break; case DELETE: commitState = CommitState.DELETED; break; case COPY: commitState = CommitState.COPIED; break; default: throw new RuntimeException("Encountered an unknown DiffEntry.ChangeType " + diff.getChangeType() + ". Please report a bug."); } try { dif.format(diff); } catch (Exception e1) { throw new Error(e1); } diff.getOldId(); String changedData = ""; try { changedData = out.toString("UTF-8"); out.reset(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } File newPath = new File(diff.getNewPath()); File oldPath = new File(diff.getOldPath()); if (commitState == CommitState.DELETED) newPath = oldPath; CommitFile commitFile = new CommitFile(getWorkingCopy(), new File(diff.getOldPath()), newPath, commitState, commitID, changedData); LoggerFactory.getLogger(getClass()).debug(MessageFormat.format("({0} {1} {2})", diff.getChangeType().name(), diff.getNewMode().getBits(), diff.getNewPath())); return commitFile; }); }