List of usage examples for org.eclipse.jgit.api Git diff
public DiffCommand diff()
From source file:at.bitandart.zoubek.mervin.gerrit.GerritReviewRepositoryService.java
License:Open Source License
/** * loads all patches of from the given list of {@link DiffEntry}s. * //from ww w. j av a 2 s. com * @param patchSet * the patchSet to add the patches to. * @param ref * the ref to the commit of the patch set. * @param repository * the git repository instance * @throws RepositoryIOException */ private void loadPatches(PatchSet patchSet, Ref ref, Git git) throws RepositoryIOException { EList<Patch> patches = patchSet.getPatches(); Repository repository = git.getRepository(); try { RevWalk revWalk = new RevWalk(repository); RevCommit newCommit = revWalk.parseCommit(ref.getObjectId()); RevCommit oldCommit = newCommit.getParent(0); revWalk.parseHeaders(oldCommit); ObjectReader objectReader = repository.newObjectReader(); revWalk.close(); CanonicalTreeParser newTreeIterator = new CanonicalTreeParser(); newTreeIterator.reset(objectReader, newCommit.getTree().getId()); CanonicalTreeParser oldTreeIterator = new CanonicalTreeParser(); oldTreeIterator.reset(objectReader, oldCommit.getTree().getId()); List<DiffEntry> diffs = git.diff().setOldTree(oldTreeIterator).setNewTree(newTreeIterator).call(); for (DiffEntry diff : diffs) { String newPath = diff.getNewPath(); String oldPath = diff.getOldPath(); Patch patch = null; /* * only papyrus diagrams are supported for now, so models are in * .uml files, diagrams in .notation files */ if (diff.getChangeType() != ChangeType.DELETE) { if (newPath.endsWith(".uml")) { patch = modelReviewFactory.createModelPatch(); } else if (newPath.endsWith(".notation")) { patch = modelReviewFactory.createDiagramPatch(); } else { patch = modelReviewFactory.createPatch(); } } else { if (oldPath.endsWith(".uml")) { patch = modelReviewFactory.createModelPatch(); } else if (oldPath.endsWith(".notation")) { patch = modelReviewFactory.createDiagramPatch(); } else { patch = modelReviewFactory.createPatch(); } } switch (diff.getChangeType()) { case ADD: patch.setChangeType(PatchChangeType.ADD); break; case COPY: patch.setChangeType(PatchChangeType.COPY); break; case DELETE: patch.setChangeType(PatchChangeType.DELETE); break; case MODIFY: patch.setChangeType(PatchChangeType.MODIFY); break; case RENAME: patch.setChangeType(PatchChangeType.RENAME); break; } patch.setNewPath(newPath); patch.setOldPath(oldPath); if (diff.getChangeType() != ChangeType.DELETE) { ObjectLoader objectLoader = repository.open(diff.getNewId().toObjectId()); patch.setNewContent(objectLoader.getBytes()); } if (diff.getChangeType() != ChangeType.ADD) { ObjectLoader objectLoader = repository.open(diff.getOldId().toObjectId()); patch.setOldContent(objectLoader.getBytes()); } patches.add(patch); } } catch (IOException e) { throw new RepositoryIOException(MessageFormat.format( "An IO error occured during loading the patches for patch set #{0}", patchSet.getId()), e); } catch (GitAPIException e) { throw new RepositoryIOException( MessageFormat.format("An JGit API error occured during loading the patches for patch set #{0}", patchSet.getId()), e); } }
From source file:br.edu.ifpb.scm.api.loads.Loader.java
public static void showFileDiffs(Git gitRepository) { Repository repo = gitRepository.getRepository(); try {/*from w w w. j av a 2s . c om*/ ObjectId head = ObjectId.fromString("c24af304077e4a6d1925db7cd35d0cd1ed488d6a"); ObjectId previousHead = ObjectId.fromString("c16be41e77bb53a4b639cb864c9a6e4d0f8df7c2"); ObjectReader reader = repo.newObjectReader(); CanonicalTreeParser oldTreeIter = new CanonicalTreeParser(); oldTreeIter.reset(reader, previousHead); CanonicalTreeParser newTreeIter = new CanonicalTreeParser(); newTreeIter.reset(reader, head); List<DiffEntry> listDiffs = gitRepository.diff().setOldTree(oldTreeIter).setNewTree(newTreeIter).call(); listDiffs.stream().forEach((DiffEntry diff) -> { DiffFormatter formatter = new DiffFormatter(System.out); formatter.setRepository(repo); System.out.println(diff); try { formatter.format(diff); } catch (IOException ex) { Logger.getLogger(Loader.class.getName()).log(Level.SEVERE, null, ex); } }); } catch (IOException | GitAPIException ex) { Logger.getLogger(Loader.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:com.github.checkstyle.regression.git.DiffParser.java
License:Open Source License
/** * Parses the diff between a given branch and the master in the give repository path. * @param repositoryPath the path of checkstyle repository * @param branchName the name of the branch to be compared with master * @return a list of {@link GitChange} to represent the changes * @throws IOException JGit library exception * @throws GitAPIException JGit library exception *///from w w w .ja v a 2 s . co m public static List<GitChange> parse(String repositoryPath, String branchName) throws IOException, GitAPIException { final List<GitChange> returnValue = new LinkedList<>(); final File gitDir = new File(repositoryPath, ".git"); final Repository repository = new FileRepositoryBuilder().setGitDir(gitDir).readEnvironment().findGitDir() .build(); try { final TreeParserPair pair = getTreeParserPair(repository, branchName); final Git git = new Git(repository); final DiffFormatter formatter = new DiffFormatter(DisabledOutputStream.INSTANCE); formatter.setRepository(repository); try { final List<DiffEntry> diffs = git.diff().setOldTree(pair.commonAncestorTreeParser) .setNewTree(pair.prTreeParser).call().stream() .filter(entry -> entry.getChangeType() != DiffEntry.ChangeType.DELETE) .collect(Collectors.toList()); for (DiffEntry diff : diffs) { returnValue.add(convertDiffEntryToGitChange(diff, formatter)); } } finally { git.close(); } } finally { repository.close(); } return returnValue; }
From source file:com.google.appraise.eclipse.core.client.git.AppraiseGitReviewClient.java
License:Open Source License
/** * Gets the diff between heads on two branches. * See/*from ww w . j a v a 2 s . c o m*/ * https://github.com/centic9/jgit-cookbook/blob/master/src/main/java/org/dstadler/jgit/porcelain/ShowBranchDiff.java. */ private List<DiffEntry> calculateBranchDiffs(Git git, String targetRef, String reviewRef) throws IOException, GitAPIException { AbstractTreeIterator oldTreeParser = prepareTreeParser(targetRef); AbstractTreeIterator newTreeParser = prepareTreeParser(reviewRef); return git.diff().setOldTree(oldTreeParser).setNewTree(newTreeParser).call(); }
From source file:com.google.appraise.eclipse.core.client.git.AppraiseGitReviewClient.java
License:Open Source License
/** * Gets the diff between two commits./*from w w w . ja v a 2s . c o m*/ */ private List<DiffEntry> calculateCommitDiffs(Git git, RevCommit first, RevCommit last) throws IOException, GitAPIException { AbstractTreeIterator oldTreeParser = prepareTreeParser(first); AbstractTreeIterator newTreeParser = prepareTreeParser(last); return git.diff().setOldTree(oldTreeParser).setNewTree(newTreeParser).call(); }
From source file:com.maiereni.synchronizer.git.utils.GitDownloaderImpl.java
License:Apache License
private List<Change> checkDifferences(final Git git, final Ref localBranch, final Ref head) throws Exception { List<Change> ret = new ArrayList<Change>(); Repository repository = git.getRepository(); AbstractTreeIterator oldTreeParser = prepareTreeParser(repository, localBranch); AbstractTreeIterator newTreeParser = prepareTreeParser(repository, head); List<DiffEntry> diffs = git.diff().setOldTree(oldTreeParser).setNewTree(newTreeParser).call(); for (DiffEntry diff : diffs) { Change change = new Change(); change.setPathNew(diff.getNewPath()); change.setPathOld(diff.getOldPath()); change.setChangeType(ChangeType.valueOf(diff.getChangeType().name())); ret.add(change);// www . j a v a 2s . co m } return ret; }
From source file:com.osbitools.ws.shared.prj.utils.GitUtils.java
License:Open Source License
public static String getDiff(Git git, String base, String name, String ext) throws WsSrvException { checkFile(base, name, ext).getAbsolutePath(); // Prepare path for git save String fp = getLocalPath(name); List<DiffEntry> diff;//from ww w. j a v a 2 s . co m try { diff = git.diff().setPathFilter(PathFilter.create(fp)).call(); } catch (GitAPIException e) { throw new WsSrvException(260, "Unable retrieve git diff", e); } ByteArrayOutputStream res = new ByteArrayOutputStream(); for (DiffEntry entry : diff) { DiffFormatter formatter = new DiffFormatter(res); formatter.setRepository(git.getRepository()); try { formatter.format(entry); } catch (IOException e) { throw new WsSrvException(261, "Unable format diff", e); } } return res.toString(); }
From source file:com.osbitools.ws.shared.prj.utils.GitUtils.java
License:Open Source License
public static boolean hasDiff(Git git, String base, String name, String ext) throws WsSrvException { checkFile(base, name, ext).getAbsolutePath(); // Prepare path for git save String fp = getLocalPath(name); List<DiffEntry> diff;/*from w ww. ja v a 2 s . com*/ try { diff = git.diff().setPathFilter(PathFilter.create(fp)).call(); } catch (GitAPIException e) { throw new WsSrvException(260, "Unable retrieve git diff", e); } return diff.size() > 0; }
From source file:com.searchcode.app.jobs.IndexGitRepoJob.java
private RepositoryChanged updateGitRepository(String repoName, String repoRemoteLocation, String repoUserName, String repoPassword, String repoLocations, String branch, boolean useCredentials) { boolean changed = false; List<String> changedFiles = new ArrayList<>(); List<String> deletedFiles = new ArrayList<>(); Singleton.getLogger().info("Attempting to pull latest from " + repoRemoteLocation + " for " + repoName); try {/*from ww w.j a v a 2 s . com*/ Repository localRepository = new FileRepository(new File(repoLocations + "/" + repoName + "/.git")); Ref head = localRepository.getRef("HEAD"); Git git = new Git(localRepository); git.reset(); git.clean(); PullCommand pullCmd = git.pull(); if (useCredentials) { pullCmd.setCredentialsProvider(new UsernamePasswordCredentialsProvider(repoUserName, repoPassword)); } 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; Singleton.getLogger().warning("ERROR - caught a " + ex.getClass() + " in " + this.getClass() + "\n with message: " + ex.getMessage()); } return new RepositoryChanged(changed, changedFiles, deletedFiles); }
From source file:com.searchcode.app.jobs.repository.IndexGitHistoryJob.java
public void getRevisionChanges(Repository localRepository, Git git, GitChangeSet oldRevison, GitChangeSet newRevision) throws IOException, GitAPIException { ObjectId oldHead = localRepository.resolve(oldRevison.getRevision() + "^{tree}"); ObjectId newHead = localRepository.resolve(newRevision.getRevision() + "^{tree}"); ObjectReader reader = localRepository.newObjectReader(); CanonicalTreeParser oldTreeIter = new CanonicalTreeParser(); oldTreeIter.reset(reader, oldHead);/*www . j av a 2 s. c o m*/ CanonicalTreeParser newTreeIter = new CanonicalTreeParser(); newTreeIter.reset(reader, newHead); List<DiffEntry> entries = git.diff().setNewTree(newTreeIter).setOldTree(oldTreeIter).call(); GitService gs = new GitService(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); for (DiffEntry entry : entries) { if ("DELETE".equals(entry.getChangeType().name())) { System.out.println("DEL " + entry.getOldPath()); String contents = gs.fetchFileRevision(localRepository.getWorkTree().toString() + "/.git", oldRevison.getRevision(), entry.getOldPath()); CodeIndexDocument cd = new CodeIndexDocument(entry.getNewPath(), "thumbor", entry.getOldPath(), entry.getOldPath(), entry.getOldPath(), "md5hash", "Java", contents.split("\\r?\\n").length, contents, "", oldRevison.getAuthor()); cd.setRevision(oldRevison.getRevision()); cd.setYearMonthDay(sdf.format(oldRevison.getExpiry())); cd.setYearMonth(cd.getYearMonthDay().substring(0, 6)); cd.setYear(cd.getYearMonthDay().substring(0, 4)); cd.setMessage(oldRevison.getMessage()); cd.setDeleted("TRUE"); Singleton.getCodeIndexer().indexTimeDocument(cd); } else { System.out.println("ADD " + entry.getNewPath()); String contents = gs.fetchFileRevision(localRepository.getWorkTree().toString() + "/.git", newRevision.getRevision(), entry.getNewPath()); CodeIndexDocument cd = new CodeIndexDocument(entry.getNewPath(), "thumbor", entry.getNewPath(), entry.getNewPath(), entry.getNewPath(), "md5hash", "Java", contents.split("\\r?\\n").length, contents, "", newRevision.getAuthor()); cd.setRevision(newRevision.getRevision()); cd.setYearMonthDay(sdf.format(oldRevison.getExpiry())); cd.setYearMonth(cd.getYearMonthDay().substring(0, 6)); cd.setYear(cd.getYearMonthDay().substring(0, 4)); cd.setMessage(newRevision.getMessage()); cd.setDeleted("FALSE"); Singleton.getCodeIndexer().indexTimeDocument(cd); } } }