List of usage examples for org.eclipse.jgit.dircache DirCacheCheckout DirCacheCheckout
public DirCacheCheckout(Repository repo, DirCache dc, ObjectId mergeCommitTree, WorkingTreeIterator workingTree) throws IOException
From source file:org.craftercms.studio.impl.v1.util.git.CherryPickCommandEx.java
License:Eclipse Distribution License
/** * Executes the {@code Cherry-Pick} command with all the options and * parameters collected by the setter methods (e.g. {@link #include(Ref)} of * this class. Each instance of this class should only be used for one * invocation of the command. Don't call this method twice on an instance. * * @return the result of the cherry-pick * @throws GitAPIException//from ww w. ja va 2 s.com * @throws WrongRepositoryStateException * @throws ConcurrentRefUpdateException * @throws UnmergedPathsException * @throws NoMessageException * @throws NoHeadException */ @Override public CherryPickResult call() throws GitAPIException { RevCommit newHead = null; List<Ref> cherryPickedRefs = new LinkedList<>(); checkCallable(); try (RevWalk revWalk = new RevWalk(repo)) { // get the head commit Ref headRef = repo.exactRef(Constants.HEAD); if (headRef == null) throw new NoHeadException(JGitText.get().commitOnRepoWithoutHEADCurrentlyNotSupported); newHead = revWalk.parseCommit(headRef.getObjectId()); // loop through all refs to be cherry-picked for (Ref src : commits) { // get the commit to be cherry-picked // handle annotated tags ObjectId srcObjectId = src.getPeeledObjectId(); if (srcObjectId == null) srcObjectId = src.getObjectId(); RevCommit srcCommit = revWalk.parseCommit(srcObjectId); Merger merger = strategy.newMerger(repo); if (merger.merge(newHead, srcCommit)) { if (AnyObjectId.equals(newHead.getTree().getId(), merger.getResultTreeId())) continue; DirCacheCheckout dco = new DirCacheCheckout(repo, newHead.getTree(), repo.lockDirCache(), merger.getResultTreeId()); dco.setFailOnConflict(true); dco.checkout(); if (!noCommit) newHead = new Git(getRepository()).commit().setMessage(srcCommit.getFullMessage()) .setReflogComment(reflogPrefix + " " //$NON-NLS-1$ + srcCommit.getShortMessage()) .setAuthor(srcCommit.getAuthorIdent()).setNoVerify(true).call(); cherryPickedRefs.add(src); } else { return CherryPickResult.CONFLICT; } } } catch (IOException e) { throw new JGitInternalException( MessageFormat.format(JGitText.get().exceptionCaughtDuringExecutionOfCherryPickCommand, e), e); } return new CherryPickResult(newHead, cherryPickedRefs); }
From source file:org.eclipse.egit.core.op.BranchOperation.java
License:Open Source License
private void checkoutTree() throws TeamException { try {/*from w ww . j a v a2s .c o m*/ DirCacheCheckout dirCacheCheckout = new DirCacheCheckout(repository, oldTree, repository.lockDirCache(), newTree); dirCacheCheckout.setFailOnConflict(true); boolean result = dirCacheCheckout.checkout(); if (!result) retryDelete(dirCacheCheckout); } catch (CheckoutConflictException e) { TeamException teamException = new TeamException(e.getMessage()); throw teamException; } catch (IOException e) { throw new TeamException(CoreText.BranchOperation_checkoutProblem, e); } }
From source file:org.eclipse.egit.core.op.CloneOperation.java
License:Open Source License
private void doCheckout(final IProgressMonitor monitor) throws IOException { final Ref head = fetchResult.getAdvertisedRef(branch); if (head == null || head.getObjectId() == null) return;/*w ww. j a va 2s.c o m*/ final RevWalk rw = new RevWalk(local); final RevCommit mapCommit; try { mapCommit = rw.parseCommit(head.getObjectId()); } finally { rw.release(); } final RefUpdate u; u = local.updateRef(Constants.HEAD); u.setNewObjectId(mapCommit.getId()); u.forceUpdate(); monitor.setTaskName(CoreText.CloneOperation_checkingOutFiles); DirCacheCheckout dirCacheCheckout = new DirCacheCheckout(local, null, local.lockDirCache(), mapCommit.getTree()); dirCacheCheckout.setFailOnConflict(true); boolean result = dirCacheCheckout.checkout(); if (!result) // this should never happen when writing in an empty folder throw new IOException("Internal error occured on checking out files"); //$NON-NLS-1$ monitor.setTaskName(CoreText.CloneOperation_writingIndex); }
From source file:org.eclipse.orion.server.git.jobs.StashApplyCommand.java
License:Eclipse Distribution License
/** * Apply the changes in a stashed commit to the working directory and index * * @return id of stashed commit that was applied TODO: Does anyone depend on this, or could we make it more like Merge/CherryPick/Revert? * @throws GitAPIException/* w w w . j av a 2s .co m*/ * @throws WrongRepositoryStateException * @throws NoHeadException * @throws StashApplyFailureException */ @Override public ObjectId call() throws GitAPIException, WrongRepositoryStateException, NoHeadException, StashApplyFailureException { checkCallable(); if (!ignoreRepositoryState && repo.getRepositoryState() != RepositoryState.SAFE) throw new WrongRepositoryStateException( MessageFormat.format(JGitText.get().stashApplyOnUnsafeRepository, repo.getRepositoryState())); ObjectReader reader = repo.newObjectReader(); try { RevWalk revWalk = new RevWalk(reader); ObjectId headCommit = repo.resolve(Constants.HEAD); if (headCommit == null) throw new NoHeadException(JGitText.get().stashApplyWithoutHead); final ObjectId stashId = getStashId(); RevCommit stashCommit = revWalk.parseCommit(stashId); if (stashCommit.getParentCount() < 2 || stashCommit.getParentCount() > 3) throw new JGitInternalException( MessageFormat.format(JGitText.get().stashCommitIncorrectNumberOfParents, stashId.name(), Integer.valueOf(stashCommit.getParentCount()))); ObjectId headTree = repo.resolve(Constants.HEAD + "^{tree}"); //$NON-NLS-1$ ObjectId stashIndexCommit = revWalk.parseCommit(stashCommit.getParent(1)); ObjectId stashHeadCommit = stashCommit.getParent(0); ObjectId untrackedCommit = null; if (applyUntracked && stashCommit.getParentCount() == 3) untrackedCommit = revWalk.parseCommit(stashCommit.getParent(2)); ResolveMerger merger = (ResolveMerger) strategy.newMerger(repo); merger.setCommitNames(new String[] { "stashed HEAD", "HEAD", "stash" }); merger.setBase(stashHeadCommit); merger.setWorkingTreeIterator(new FileTreeIterator(repo)); if (merger.merge(headCommit, stashCommit)) { DirCache dc = repo.lockDirCache(); DirCacheCheckout dco = new DirCacheCheckout(repo, headTree, dc, merger.getResultTreeId()); dco.setFailOnConflict(true); dco.checkout(); // Ignoring failed deletes.... if (applyIndex) { ResolveMerger ixMerger = (ResolveMerger) strategy.newMerger(repo, true); ixMerger.setCommitNames(new String[] { "stashed HEAD", "HEAD", "stashed index" }); ixMerger.setBase(stashHeadCommit); boolean ok = ixMerger.merge(headCommit, stashIndexCommit); if (ok) { resetIndex(revWalk.parseTree(ixMerger.getResultTreeId())); } else { throw new StashApplyFailureException(JGitText.get().stashApplyConflict); } } if (untrackedCommit != null) { ResolveMerger untrackedMerger = (ResolveMerger) strategy.newMerger(repo, true); untrackedMerger.setCommitNames(new String[] { "stashed HEAD", "HEAD", "untracked files" }); //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ untrackedMerger.setBase(stashHeadCommit); boolean ok = untrackedMerger.merge(stashHeadCommit, untrackedCommit); if (ok) try { RevTree untrackedTree = revWalk.parseTree(untrackedMerger.getResultTreeId()); resetUntracked(untrackedTree); } catch (CheckoutConflictException e) { throw new StashApplyFailureException(JGitText.get().stashApplyConflict); } else throw new StashApplyFailureException(JGitText.get().stashApplyConflict); } } else { throw new StashApplyFailureException(JGitText.get().stashApplyConflict); } return stashId; } catch (JGitInternalException e) { throw e; } catch (IOException e) { throw new JGitInternalException(JGitText.get().stashApplyFailed, e); } finally { reader.release(); } }
From source file:org.jboss.forge.addon.git.GitUtilsImpl.java
License:Open Source License
@Override public CherryPickResult cherryPickNoMerge(final Git git, Ref src) throws GitAPIException, CantMergeCommitException { // Does the same as the original git-cherryPick // except commiting after running merger Repository repo = git.getRepository(); RevCommit newHead = null;/*w ww. j av a 2s.c om*/ List<Ref> cherryPickedRefs = new LinkedList<Ref>(); try (RevWalk revWalk = new RevWalk(repo)) { // get the head commit Ref headRef = repo.findRef(Constants.HEAD); if (headRef == null) throw new NoHeadException(JGitText.get().commitOnRepoWithoutHEADCurrentlyNotSupported); RevCommit headCommit = revWalk.parseCommit(headRef.getObjectId()); newHead = headCommit; // get the commit to be cherry-picked // handle annotated tags ObjectId srcObjectId = src.getPeeledObjectId(); if (srcObjectId == null) srcObjectId = src.getObjectId(); RevCommit srcCommit = revWalk.parseCommit(srcObjectId); // get the parent of the commit to cherry-pick if (srcCommit.getParentCount() == 0) throw new CantMergeCommitException("Commit with zero parents cannot be merged"); if (srcCommit.getParentCount() > 1) throw new MultipleParentsNotAllowedException( MessageFormat.format(JGitText.get().canOnlyCherryPickCommitsWithOneParent, srcCommit.name(), Integer.valueOf(srcCommit.getParentCount()))); RevCommit srcParent = srcCommit.getParent(0); revWalk.parseHeaders(srcParent); ResolveMerger merger = (ResolveMerger) MergeStrategy.RESOLVE.newMerger(repo); merger.setWorkingTreeIterator(new FileTreeIterator(repo)); merger.setBase(srcParent.getTree()); if (merger.merge(headCommit, srcCommit)) { DirCacheCheckout dco = new DirCacheCheckout(repo, headCommit.getTree(), repo.lockDirCache(), merger.getResultTreeId()); dco.setFailOnConflict(true); dco.checkout(); cherryPickedRefs.add(src); } else { if (merger.failed()) return new CherryPickResult(merger.getFailingPaths()); // there are merge conflicts String message = new MergeMessageFormatter().formatWithConflicts(srcCommit.getFullMessage(), merger.getUnmergedPaths()); repo.writeCherryPickHead(srcCommit.getId()); repo.writeMergeCommitMsg(message); return CherryPickResult.CONFLICT; } } catch (IOException e) { throw new JGitInternalException( MessageFormat.format(JGitText.get().exceptionCaughtDuringExecutionOfCherryPickCommand, e), e); } return new CherryPickResult(newHead, cherryPickedRefs); }
From source file:org.jboss.forge.git.GitUtils.java
License:Open Source License
public static CherryPickResult cherryPickNoMerge(final Git git, Ref src) throws GitAPIException, CantMergeCommitWithZeroParentsException { // Does the same as the original git-cherryPick // except commiting after running merger Repository repo = git.getRepository(); RevCommit newHead = null;/*from w w w . j av a 2 s . c o m*/ List<Ref> cherryPickedRefs = new LinkedList<Ref>(); RevWalk revWalk = new RevWalk(repo); try { // get the head commit Ref headRef = repo.getRef(Constants.HEAD); if (headRef == null) throw new NoHeadException(JGitText.get().commitOnRepoWithoutHEADCurrentlyNotSupported); RevCommit headCommit = revWalk.parseCommit(headRef.getObjectId()); newHead = headCommit; // get the commit to be cherry-picked // handle annotated tags ObjectId srcObjectId = src.getPeeledObjectId(); if (srcObjectId == null) srcObjectId = src.getObjectId(); RevCommit srcCommit = revWalk.parseCommit(srcObjectId); // get the parent of the commit to cherry-pick if (srcCommit.getParentCount() == 0) throw new CantMergeCommitWithZeroParentsException("Commit with zero parents cannot be merged"); if (srcCommit.getParentCount() > 1) throw new MultipleParentsNotAllowedException( MessageFormat.format(JGitText.get().canOnlyCherryPickCommitsWithOneParent, srcCommit.name(), Integer.valueOf(srcCommit.getParentCount()))); RevCommit srcParent = srcCommit.getParent(0); revWalk.parseHeaders(srcParent); ResolveMerger merger = (ResolveMerger) MergeStrategy.RESOLVE.newMerger(repo); merger.setWorkingTreeIterator(new FileTreeIterator(repo)); merger.setBase(srcParent.getTree()); if (merger.merge(headCommit, srcCommit)) { DirCacheCheckout dco = new DirCacheCheckout(repo, headCommit.getTree(), repo.lockDirCache(), merger.getResultTreeId()); dco.setFailOnConflict(true); dco.checkout(); cherryPickedRefs.add(src); } else { if (merger.failed()) return new CherryPickResult(merger.getFailingPaths()); // there are merge conflicts String message = new MergeMessageFormatter().formatWithConflicts(srcCommit.getFullMessage(), merger.getUnmergedPaths()); repo.writeCherryPickHead(srcCommit.getId()); repo.writeMergeCommitMsg(message); return CherryPickResult.CONFLICT; } } catch (IOException e) { throw new JGitInternalException( MessageFormat.format(JGitText.get().exceptionCaughtDuringExecutionOfCherryPickCommand, e), e); } finally { revWalk.release(); } return new CherryPickResult(newHead, cherryPickedRefs); }
From source file:org.openengsb.connector.git.internal.GitServiceImpl.java
License:Apache License
protected void doCheckout(FetchResult fetchResult) throws IOException { final Ref head = fetchResult.getAdvertisedRef(Constants.R_HEADS + watchBranch); final RevWalk rw = new RevWalk(repository); final RevCommit mapCommit; try {//www .j av a2 s .com LOGGER.debug("Mapping received reference to respective commit."); mapCommit = rw.parseCommit(head.getObjectId()); } finally { rw.release(); } final RefUpdate u; boolean detached = !head.getName().startsWith(Constants.R_HEADS); LOGGER.debug("Updating HEAD reference to revision [{}]", mapCommit.getId().name()); u = repository.updateRef(Constants.HEAD, detached); u.setNewObjectId(mapCommit.getId()); u.forceUpdate(); DirCacheCheckout dirCacheCheckout = new DirCacheCheckout(repository, null, repository.lockDirCache(), mapCommit.getTree()); dirCacheCheckout.setFailOnConflict(true); boolean checkoutResult = dirCacheCheckout.checkout(); LOGGER.debug("Checked out new repository revision to working directory"); if (!checkoutResult) { throw new IOException("Internal error occured on checking out files"); } }
From source file:util.ChkoutCmd.java
License:Eclipse Distribution License
/** * @throws RefAlreadyExistsException/* w ww. ja v a2 s. c o m*/ * when trying to create (without force) a branch with a name * that already exists * @throws RefNotFoundException * if the start point or branch can not be found * @throws InvalidRefNameException * if the provided name is <code>null</code> or otherwise * invalid * @throws CheckoutConflictException * if the checkout results in a conflict * @return the newly created branch */ public Ref call() throws GitAPIException, RefAlreadyExistsException, RefNotFoundException, InvalidRefNameException, CheckoutConflictException { checkCallable(); processOptions(); try { if (checkoutAllPaths || !paths.isEmpty()) { checkoutPaths(); status = new CheckoutResult(Status.OK, paths); setCallable(false); return null; } if (createBranch) { Git git = new Git(repo); CreateBranchCommand command = git.branchCreate(); command.setName(name); command.setStartPoint(getStartPoint().name()); if (upstreamMode != null) command.setUpstreamMode(upstreamMode); command.call(); } Ref headRef = repo.getRef(Constants.HEAD); String shortHeadRef = getShortBranchName(headRef); String refLogMessage = "checkout: moving from " + shortHeadRef; //$NON-NLS-1$ ObjectId branch = repo.resolve(name); if (branch == null) throw new RefNotFoundException(MessageFormat.format(JGitText.get().refNotResolved, name)); RevWalk revWalk = new RevWalk(repo); AnyObjectId headId = headRef.getObjectId(); RevCommit headCommit = headId == null ? null : revWalk.parseCommit(headId); RevCommit newCommit = revWalk.parseCommit(branch); RevTree headTree = headCommit == null ? null : headCommit.getTree(); DirCacheCheckout dco; DirCache dc = repo.lockDirCache(); try { dco = new DirCacheCheckout(repo, headTree, dc, newCommit.getTree()); dco.setFailOnConflict(false); try { dco.checkout(); } catch (org.eclipse.jgit.errors.CheckoutConflictException e) { status = new CheckoutResult(Status.CONFLICTS, dco.getConflicts()); throw new CheckoutConflictException(dco.getConflicts(), e); } } finally { dc.unlock(); } Ref ref = repo.getRef(name); if (ref != null && !ref.getName().startsWith(Constants.R_HEADS)) ref = null; String toName = Repository.shortenRefName(name); RefUpdate refUpdate = repo.updateRef(Constants.HEAD, ref == null); refUpdate.setForceUpdate(force); refUpdate.setRefLogMessage(refLogMessage + " to " + toName, false); //$NON-NLS-1$ Result updateResult; if (ref != null) updateResult = refUpdate.link(ref.getName()); else { refUpdate.setNewObjectId(newCommit); updateResult = refUpdate.forceUpdate(); } setCallable(false); boolean ok = false; switch (updateResult) { case NEW: ok = true; break; case NO_CHANGE: case FAST_FORWARD: case FORCED: ok = true; break; default: break; } if (!ok) throw new JGitInternalException( MessageFormat.format(JGitText.get().checkoutUnexpectedResult, updateResult.name())); if (!dco.getToBeDeleted().isEmpty()) { status = new CheckoutResult(Status.NONDELETED, dco.getToBeDeleted()); } else status = new CheckoutResult(new ArrayList<String>(dco.getUpdated().keySet()), dco.getRemoved()); return ref; } catch (IOException ioe) { throw new JGitInternalException(ioe.getMessage(), ioe); } finally { if (status == null) status = CheckoutResult.ERROR_RESULT; } }