private void trackBranch(Git git, CheckoutCommand checkout, String label) { checkout.setCreateBranch(true).setName(label) .setUpstreamMode(SetupUpstreamMode.TRACK) .setStartPoint("origin/" + label); }
command.setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.valueOf(upstreamMode.toString()));
private void trackBranch(CheckoutCommand checkout, String label) { checkout.setCreateBranch(true).setName(label) .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK) .setStartPoint("origin/" + label); }
private void trackBranch(Git git, CheckoutCommand checkout, String branch) { checkout.setCreateBranch(true).setName(branch).setUpstreamMode(TRACK).setStartPoint("origin/" + branch); }
@Override public String readFileToString(String pathAndFile, String startingPoint) { if (!startingPoint.contains("origin")) { startingPoint = "origin/" + startingPoint; } File tempDir = Files.createTempDir(); try { this.gitClone(tempDir); Git git = gitOpen(tempDir); Ref ref = git.checkout(). setCreateBranch(true). setName(branch). setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK). setStartPoint(startingPoint). call(); } catch (GitAPIException e) { log.error(e.getMessage(), e); } try { return IOUtil.toString(new FileReader(new File(tempDir, pathAndFile))); } catch (IOException e) { log.error(e.getMessage(), e); } return ""; }
/** * Executes a checkout -b command using given branch. * * @param git * instance. * @param branch * to create and checkout. * * @return Ref to current branch. */ public Ref createBranchAndCheckout(Git git, String branch) { try { return git.checkout() .setCreateBranch(true) .setName(branch) .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK) .call(); } catch (GitAPIException e) { throw new IllegalStateException(e); } }
private Git checkoutOrCreateBranch(final Git git) { try { if (!branch.equals(git.getRepository().getBranch())) { CheckoutCommand checkoutCommand = git.checkout().setCreateBranch(true).setName(branch).setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK); if (remoteBranchExists(git)) { checkoutCommand.setStartPoint("origin/" + branch); } checkoutCommand.call(); } } catch (Exception e) { log.error(e.getMessage(),e); } return git; }
@Override public Ref checkout(final Git git, final String remote, final boolean createBranch, final SetupUpstreamMode mode, final boolean force) throws GitAPIException { CheckoutCommand checkout = git.checkout(); checkout.setCreateBranch(createBranch); checkout.setName(remote); checkout.setForce(force); checkout.setUpstreamMode(mode); return checkout.call(); }
/** * Checkout existing branch. * * @param git * instance. * @param branch * to move * @param remote * repository name * * @return Ref to current branch */ public Ref checkoutBranch(Git git, String branch, String remote) { try { return git.checkout() .setCreateBranch(true) .setName(branch) .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK) .setStartPoint(remote + "/" + branch) .call(); } catch (GitAPIException e) { throw new IllegalStateException(e); } }
/** * Creates a branch with the passed name and commit id from wich to start the branch. * * @param branchName - The name of the new branch to create. * @param startPoint - The commit from which to start the branch. * @throws GitAPIException - failed. */ public void createBranch(final String branchName, final String startPoint) throws GitAPIException { git.checkout() .setCreateBranch(true) .setName(branchName) .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK) .setStartPoint(startPoint) .call(); }
protected void checkoutBranch(Git git, GitContext context) throws GitAPIException { String current = currentBranch(git); if (Objects.equals(current, branch)) { return; } System.out.println("Checking out branch: " + branch); // lets check if the branch exists CheckoutCommand command = git.checkout().setName(branch); boolean exists = localBranchExists(git, branch); if (!exists) { command = command.setCreateBranch(true).setForce(true). setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK). setStartPoint(getRemote() + "/" + branch); } Ref ref = command.call(); if (LOG.isDebugEnabled()) { LOG.debug("Checked out branch " + branch + " with results " + ref.getName()); } configureBranch(git, branch); }
Git checkoutOrCreateBranch(final Git git) throws GitAPIException, IOException { if (!branch.equals(git.getRepository().getBranch())) { CheckoutCommand checkoutCommand = git.checkout() .setCreateBranch(true) .setName(branch) .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK); if (remoteBranchExists(git)) { checkoutCommand.setStartPoint("origin/" + branch); } checkoutCommand.call(); } return git; }
@Override public Ref checkout(final Git git, final Ref localRef, final SetupUpstreamMode mode, final boolean force) throws GitAPIException { CheckoutCommand checkout = git.checkout(); checkout.setName(Repository.shortenRefName(localRef.getName())); checkout.setForce(force); checkout.setUpstreamMode(mode); return checkout.call(); }
/** * Does not do anything if already on target branch. */ public void checkout(String branch) throws Exception { if (!branch.equals(getBranch())) { createBranchIfNeeded(branch); git.checkout().setName(branch).setStartPoint("origin/" + branch) .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK).call(); // for some reason jgit needs this when branch is switched git.checkout().setName(branch).call(); } }
@Override public Git cloneRepository(Git git, boolean fetchAndCheckout) throws GitAPIException, IOException { File tmpLocation = new File(tmpPatchManagement, TS.format(new Date())); Git fork = Git.init().setBare(false) .setGitDir(new File(tmpLocation, Constants.DOT_GIT)) .setDirectory(tmpLocation).call(); StoredConfig config = fork.getRepository().getConfig(); config.setString("remote", "origin", "url", git.getRepository().getDirectory().getCanonicalPath()); config.setString("remote", "origin", "fetch", "+refs/heads/*:refs/remotes/origin/*"); config.save(); if (fetchAndCheckout) { fork.fetch() .setRemote("origin") .setTagOpt(TagOpt.FETCH_TAGS) .call(); fork.checkout() .setCreateBranch(true) .setName(getMainBranchName()) .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK) .setStartPoint("origin/" + getMainBranchName()) .call(); } return fork; }
private void checkoutToBranch(String branch) throws GitAPIException { CheckoutCommand checkoutCommand = clonedRepo.checkout() .setCreateBranch(false) .setName(branch); List<Ref> refList = clonedRepo.branchList().call(); if (!anyRefMatches(refList, branch)) { checkoutCommand = checkoutCommand .setCreateBranch(true) .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK) .setStartPoint("origin/" + branch); } checkoutCommand .call(); }
@Override public Git cloneRepository(Git git, boolean fetchAndCheckout) throws GitAPIException, IOException { File tmpLocation = new File(tmpPatchManagement, TS.format(new Date())); Git fork = Git.init().setBare(false) .setGitDir(new File(tmpLocation, Constants.DOT_GIT)) .setDirectory(tmpLocation).call(); StoredConfig config = fork.getRepository().getConfig(); config.setString("remote", "origin", "url", git.getRepository().getDirectory().getCanonicalPath()); config.setString("remote", "origin", "fetch", "+refs/heads/*:refs/remotes/origin/*"); config.save(); if (fetchAndCheckout) { fork.fetch() .setRemote("origin") .setTagOpt(TagOpt.FETCH_TAGS) .call(); fork.checkout() .setCreateBranch(true) .setName(getMainBranchName()) .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK) .setStartPoint("origin/" + getMainBranchName()) .call(); } return fork; }
protected void checkoutBranch(Git git, String branch) throws GitAPIException { String current = currentBranch(); if (defaultBranch == null) { defaultBranch = current; } if (Strings.isBlank(branch)) { branch = defaultBranch; } if (Objects.equals(current, branch)) { return; } // lets check if the branch exists CheckoutCommand command = git.checkout().setName(branch); boolean exists = localBranchExists(branch); if (!exists) { command = command.setCreateBranch(true).setForce(true). setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK). setStartPoint(getRemote() + "/" + branch); } Ref ref = command.call(); if (LOG.isDebugEnabled()) { LOG.debug("Checked out branch " + branch + " with results " + ref.getName()); } configureBranch(branch); }
private void cloneTo(GitUtils gitUtils, DirectoryResource projectRoot) throws GitAPIException, IOException { Git git = null; try { git = gitUtils.clone(projectRoot, url.getValue().getFullyQualifiedName()); if (ref.hasValue()) { String refName = ref.getValue(); String currentBranch = git.getRepository().getBranch(); // No need to checkout if the branch name is the same if (!currentBranch.equals(refName)) { git.checkout().setCreateBranch(true).setName(refName).setUpstreamMode(SetupUpstreamMode.TRACK) .setStartPoint("origin/" + refName).call(); } } } finally { gitUtils.close(git); } }
.setName(branch) .setCreateBranch(true) .setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK) .setStartPoint(Constants.DEFAULT_REMOTE_NAME + "/" + branch) .call();