List of usage examples for org.eclipse.jgit.dircache DirCacheCheckout checkout
public boolean checkout() throws IOException
From source file:com.microsoft.gittf.core.tasks.CloneTask.java
License:Open Source License
@Override public TaskStatus run(final TaskProgressMonitor progressMonitor) throws Exception { final String taskName = Messages.formatString("CloneTask.CloningFormat", //$NON-NLS-1$ tfsPath,//from w w w .j ava 2 s. c om bare ? repository.getDirectory().getAbsolutePath() : repository.getWorkTree().getAbsolutePath()); progressMonitor.beginTask(taskName, 1, TaskProgressDisplay.DISPLAY_PROGRESS.combine(TaskProgressDisplay.DISPLAY_SUBTASK_DETAIL)); /* * Query the changesets. */ /* See if this is an actual server path. */ final Item item = vcClient.getItem(tfsPath, versionSpec, DeletedState.NON_DELETED, GetItemsOptions.NONE); Check.notNull(item, "item"); //$NON-NLS-1$ if (item.getItemType() != ItemType.FOLDER) { return new TaskStatus(TaskStatus.ERROR, Messages.formatString("CloneTask.CannotCloneFileFormat", tfsPath)); //$NON-NLS-1$ } /* Determine the latest changeset on the server. */ final Changeset[] changesets = vcClient.queryHistory(tfsPath, versionSpec, 0, RecursionType.FULL, null, new ChangesetVersionSpec(0), versionSpec, depth, false, false, false, false); /* * Create and configure the repository. */ repository.create(bare); final ConfigureRepositoryTask configureTask = new ConfigureRepositoryTask(repository, serverURI, tfsPath); configureTask.setTag(tag); final TaskStatus configureStatus = new TaskExecutor(new NullTaskProgressMonitor()).execute(configureTask); if (!configureStatus.isOK()) { return configureStatus; } if (changesets.length > 0) { ObjectId lastCommitID = null; ObjectId lastTreeID = null; Item[] previousChangesetItems = null; /* * Download changesets. */ final int numberOfChangesetToDownload = changesets.length; progressMonitor.setWork(numberOfChangesetToDownload); for (int i = numberOfChangesetToDownload; i > 0; i--) { CreateCommitForChangesetVersionSpecTask commitTask = new CreateCommitForChangesetVersionSpecTask( repository, vcClient, changesets[i - 1], previousChangesetItems, lastCommitID, witClient); TaskStatus commitStatus = new TaskExecutor(progressMonitor.newSubTask(1)).execute(commitTask); if (!commitStatus.isOK()) { return commitStatus; } lastCommitID = commitTask.getCommitID(); lastTreeID = commitTask.getCommitTreeID(); previousChangesetItems = commitTask.getCommittedItems(); Check.notNull(lastCommitID, "lastCommitID"); //$NON-NLS-1$ Check.notNull(lastTreeID, "lastTreeID"); //$NON-NLS-1$ new ChangesetCommitMap(repository).setChangesetCommit(changesets[i - 1].getChangesetID(), commitTask.getCommitID()); progressMonitor.displayVerbose(Messages.formatString("CloneTask.ClonedFormat", //$NON-NLS-1$ Integer.toString(changesets[i - 1].getChangesetID()), ObjectIdUtil.abbreviate(repository, lastCommitID))); } progressMonitor.setDetail(Messages.getString("CloneTask.Finalizing")); //$NON-NLS-1$ /* Update master head reference */ RefUpdate ref = repository.updateRef(Constants.R_HEADS + Constants.MASTER); ref.setNewObjectId(lastCommitID); ref.update(); /* Create tfs branch */ TfsBranchUtil.create(repository, Constants.R_HEADS + Constants.MASTER); /* * Check out the cloned commit. */ if (!bare) { DirCache dirCache = repository.lockDirCache(); DirCacheCheckout checkout = new DirCacheCheckout(repository, dirCache, lastTreeID); checkout.checkout(); dirCache.unlock(); RepositoryUtil.fixFileAttributes(repository); } progressMonitor.endTask(); final int finalChangesetID = changesets[0].getChangesetID(); if (numberOfChangesetToDownload == 1) { progressMonitor.displayMessage(Messages.formatString("CloneTask.ClonedFormat", //$NON-NLS-1$ Integer.toString(finalChangesetID), ObjectIdUtil.abbreviate(repository, lastCommitID))); } else { progressMonitor.displayMessage(Messages.formatString("CloneTask.ClonedMultipleFormat", //$NON-NLS-1$ changesets.length, Integer.toString(finalChangesetID), ObjectIdUtil.abbreviate(repository, lastCommitID))); } } else { // the folder exists on the server but is empty progressMonitor.displayMessage(Messages.getString("CloneTask.NothingToDownload")); //$NON-NLS-1$ progressMonitor.displayMessage(Messages.formatString("CloneTask.ClonedFolderEmptyFormat", //$NON-NLS-1$ tfsPath)); } log.info("Clone task completed."); //$NON-NLS-1$ return TaskStatus.OK_STATUS; }
From source file:net.erdfelt.android.sdkfido.git.internal.GitCloneCommand.java
License:Apache License
private void checkoutRef(Ref ref) throws IOException { LOG.info("checkoutRef: ref:" + ref); if (ref == null) { throw new IllegalArgumentException("Unable to checkout from a null ref"); }/*from ww w . j ava 2s . c o m*/ if (!Constants.HEAD.equals(ref.getName())) { RefUpdate u = repo.updateRef(Constants.HEAD); u.disableRefLog(); u.link(ref.getName()); } ObjectId commitId = ref.getObjectId(); RevCommit commit = parseCommit(commitId); RefUpdate u = repo.updateRef(Constants.HEAD); u.setNewObjectId(commit.getId()); u.forceUpdate(); DirCache dc = repo.lockDirCache(); DirCacheCheckout co = new DirCacheCheckout(repo, dc, commit.getTree()); co.checkout(); }
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/* www . j a v a 2s. c o m*/ * @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 {/* w w w . j av a 2s.co 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 w w.j a v a 2 s .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.egit.core.op.ResetOperation.java
License:Open Source License
private void checkoutIndex() throws TeamException { DirCache dc = null;/* w w w.j a va 2 s.c o m*/ try { dc = repository.lockDirCache(); DirCacheCheckout checkout = new DirCacheCheckout(repository, dc, commit.getTree()); checkout.setFailOnConflict(false); checkout.checkout(); } catch (IOException e) { throw new TeamException(CoreText.ResetOperation_mappingTreeForCommit, e); } finally { if (dc != null) dc.unlock(); } }
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//from ww w . j a v a2 s .c o 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;/*from w w w . ja v a 2 s. c o m*/ 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.Clone.java
License:Eclipse Distribution License
private void doCheckout(final Ref branch) throws IOException { if (branch == null) throw new Die(CLIText.get().cannotChekoutNoHeadsAdvertisedByRemote); if (!Constants.HEAD.equals(branch.getName())) { RefUpdate u = db.updateRef(Constants.HEAD); u.disableRefLog();//w w w . j av a 2 s . c o m u.link(branch.getName()); } final RevCommit commit = parseCommit(branch); final RefUpdate u = db.updateRef(Constants.HEAD); u.setNewObjectId(commit); u.forceUpdate(); DirCache dc = db.lockDirCache(); DirCacheCheckout co = new DirCacheCheckout(db, dc, commit.getTree()); co.checkout(); }
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;/* w w w . j a va 2 s . c om*/ 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); }