List of usage examples for org.eclipse.jgit.lib RefUpdate link
public Result link(String target) throws IOException
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"); }// w ww . j a va 2 s . co 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.commonjava.gitwrap.BareGitRepository.java
License:Open Source License
protected final void doClone(final String remoteUrl, final String remoteName, final String branch) throws GitWrapException { final FileRepository repository = getRepository(); final String branchRef = Constants.R_HEADS + (branch == null ? Constants.MASTER : branch); try {// w w w . j a v a 2 s . co m final RefUpdate head = repository.updateRef(Constants.HEAD); head.disableRefLog(); head.link(branchRef); final RemoteConfig remoteConfig = new RemoteConfig(repository.getConfig(), remoteName); remoteConfig.addURI(new URIish(remoteUrl)); final String remoteRef = Constants.R_REMOTES + remoteName; RefSpec spec = new RefSpec(); spec = spec.setForceUpdate(true); spec = spec.setSourceDestination(Constants.R_HEADS + "*", remoteRef + "/*"); remoteConfig.addFetchRefSpec(spec); remoteConfig.update(repository.getConfig()); repository.getConfig().setString("branch", branch, "remote", remoteName); repository.getConfig().setString("branch", branch, "merge", branchRef); repository.getConfig().save(); fetch(remoteName); postClone(remoteUrl, branchRef); } catch (final IOException e) { throw new GitWrapException("Failed to clone from: %s. Reason: %s", e, remoteUrl, e.getMessage()); } catch (final URISyntaxException e) { throw new GitWrapException("Failed to clone from: %s. Reason: %s", e, remoteUrl, e.getMessage()); } }
From source file:org.commonjava.gitwrap.GitRepository.java
License:Open Source License
public GitRepository checkoutBranch(final String name) throws GitWrapException { final String refName; if (name == null) { refName = Constants.HEAD;/*from w ww . ja v a 2 s . c o m*/ } else if (name.startsWith(Constants.R_HEADS) || name.startsWith(Constants.R_TAGS)) { refName = name; } else { refName = Constants.R_HEADS + name; } if (hasBranch(refName)) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("Checking out: " + refName); } final FileRepository repository = getRepository(); final boolean detach = !refName.startsWith(Constants.R_HEADS); try { final RevWalk walk = new RevWalk(repository); final RevCommit newCommit = walk.parseCommit(repository.resolve(refName)); final RevCommit oldCommit = walk.parseCommit(repository.resolve(Constants.HEAD)); final GitIndex index = repository.getIndex(); final RevTree newTree = newCommit.getTree(); final RevTree oldTree = oldCommit.getTree(); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Checking out: " + newCommit + " (resolved from: " + refName + ")"); } final WorkDirCheckout checkout = new WorkDirCheckout(repository, repository.getWorkTree(), repository.mapTree(oldTree), index, repository.mapTree(newTree)); checkout.checkout(); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Writing index..."); } index.write(); final RefUpdate u = repository.updateRef(Constants.HEAD, detach); Result result; if (detach) { u.setNewObjectId(newCommit.getId()); u.setRefLogMessage("Switching to detached branch: " + refName, false); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Updating head ref to point to: " + newCommit.getId()); } result = u.forceUpdate(); } else { u.setRefLogMessage("Switching to linked branch: " + refName, false); if (LOGGER.isDebugEnabled()) { LOGGER.debug("Linking head ref to: " + refName); } result = u.link(refName); } switch (result) { case NEW: case FORCED: case NO_CHANGE: case FAST_FORWARD: break; default: throw new GitWrapException("Error checking out branch: %s. Result: %s", refName, u.getResult().name()); } } catch (final IOException e) { throw new GitWrapException("Failed to checkout branch: %s.\nReason: %s", e, refName, e.getMessage()); } } else { throw new GitWrapException( "Cannot checkout non-existent branch: %s. Perhaps you meant to call createBranch(..)?", refName); } return this; }
From source file:org.eclipse.egit.core.op.BranchOperation.java
License:Open Source License
private void updateHeadRef() throws TeamException { boolean detach = false; // in case of a non-local branch or a tag, // we "detach" HEAD, i.e. point it to the // underlying commit instead of to the Ref if (refName == null || !refName.startsWith(Constants.R_HEADS)) detach = true;//from w w w . ja v a2s.c o m try { RefUpdate u = repository.updateRef(Constants.HEAD, detach); Result res; if (detach) { u.setNewObjectId(newCommit.getId()); // using forceUpdate instead of update avoids // the merge tests which would otherwise make // this fail u.setRefLogMessage(NLS.bind(CoreText.BranchOperation_checkoutMovingTo, newCommit.getId().name()), false); res = u.forceUpdate(); } else { u.setRefLogMessage(NLS.bind(CoreText.BranchOperation_checkoutMovingTo, refName), false); res = u.link(refName); } switch (res) { case NEW: case FORCED: case NO_CHANGE: case FAST_FORWARD: break; default: throw new IOException(u.getResult().name()); } } catch (IOException e) { throw new TeamException(NLS.bind(CoreText.BranchOperation_updatingHeadToRef, refName), e); } }
From source file:org.eclipse.egit.core.op.CloneOperation.java
License:Open Source License
private void doInit(final IProgressMonitor monitor) throws URISyntaxException, IOException { monitor.setTaskName(CoreText.CloneOperation_initializingRepository); local = new FileRepository(gitdir); local.create();/*from w w w . j av a 2 s .co m*/ final RefUpdate head = local.updateRef(Constants.HEAD); head.disableRefLog(); head.link(branch); remoteConfig = new RemoteConfig(local.getConfig(), remoteName); remoteConfig.addURI(uri); final String dst = Constants.R_REMOTES + remoteConfig.getName(); RefSpec wcrs = new RefSpec(); wcrs = wcrs.setForceUpdate(true); wcrs = wcrs.setSourceDestination(Constants.R_HEADS + "*", dst + "/*"); //$NON-NLS-1$ //$NON-NLS-2$ if (allSelected) { remoteConfig.addFetchRefSpec(wcrs); } else { for (final Ref ref : selectedBranches) if (wcrs.matchSource(ref)) remoteConfig.addFetchRefSpec(wcrs.expandFromSource(ref)); } // we're setting up for a clone with a checkout local.getConfig().setBoolean("core", null, "bare", false); //$NON-NLS-1$ //$NON-NLS-2$ remoteConfig.update(local.getConfig()); // branch is like 'Constants.R_HEADS + branchName', we need only // the 'branchName' part String branchName = branch.substring(Constants.R_HEADS.length()); // setup the default remote branch for branchName local.getConfig().setString("branch", branchName, "remote", remoteName); //$NON-NLS-1$ //$NON-NLS-2$ local.getConfig().setString("branch", branchName, "merge", branch); //$NON-NLS-1$ //$NON-NLS-2$ local.getConfig().save(); }
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 . java 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.webcat.core.git.GitCloner.java
License:Open Source License
private void doCheckout(Repository repository, Ref branch) throws IOException { if (!Constants.HEAD.equals(branch.getName())) { RefUpdate refUpdate = repository.updateRef(Constants.HEAD); refUpdate.disableRefLog();/* ww w. j av a2 s . c o m*/ refUpdate.link(branch.getName()); } RevCommit commit = parseCommit(repository, branch); RefUpdate refUpdate = repository.updateRef(Constants.HEAD); refUpdate.setNewObjectId(commit); refUpdate.forceUpdate(); DirCache dirCache = repository.lockDirCache(); DirCacheCheckout checkout = new DirCacheCheckout(repository, dirCache, commit.getTree()); checkout.checkout(); }
From source file:playRepository.GitRepository.java
License:Apache License
/** * Clones a local repository./* w ww . j a v a 2 s. c o m*/ * * This doesn't copy Git objects but hardlink them to save disk space. * * @param originalProject * @param forkProject * @throws IOException */ protected static void cloneHardLinkedRepository(Project originalProject, Project forkProject) throws IOException { Repository origin = GitRepository.buildGitRepository(originalProject); Repository forked = GitRepository.buildGitRepository(forkProject); forked.create(); final Path originObjectsPath = Paths.get(new File(origin.getDirectory(), "objects").getAbsolutePath()); final Path forkedObjectsPath = Paths.get(new File(forked.getDirectory(), "objects").getAbsolutePath()); // Hardlink files .git/objects/ directory to save disk space, // but copy .git/info/alternates because the file can be modified. SimpleFileVisitor<Path> visitor = new SimpleFileVisitor<Path>() { public FileVisitResult visitFile(Path file, BasicFileAttributes attr) throws IOException { Path newPath = forkedObjectsPath.resolve(originObjectsPath.relativize(file.toAbsolutePath())); if (file.equals(forkedObjectsPath.resolve("/info/alternates"))) { Files.copy(file, newPath); } else { FileUtils.mkdirs(newPath.getParent().toFile(), true); Files.createLink(newPath, file); } return java.nio.file.FileVisitResult.CONTINUE; } }; Files.walkFileTree(originObjectsPath, visitor); // Import refs. for (Map.Entry<String, Ref> entry : origin.getAllRefs().entrySet()) { RefUpdate updateRef = forked.updateRef(entry.getKey()); Ref ref = entry.getValue(); if (ref.isSymbolic()) { updateRef.link(ref.getTarget().getName()); } else { updateRef.setNewObjectId(ref.getObjectId()); updateRef.update(); } } }
From source file:util.ChkoutCmd.java
License:Eclipse Distribution License
/** * @throws RefAlreadyExistsException/* w w w . j a va 2 s. c om*/ * 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; } }