List of usage examples for org.eclipse.jgit.api CheckoutCommand setCreateBranch
public CheckoutCommand setCreateBranch(boolean createBranch)
From source file:com.barchart.jenkins.cascade.PluginScmGit.java
License:BSD License
/** * See {@link Git#checkout()}/*from www .j a va 2s . co m*/ */ public static CheckoutResult doCheckout(final File workspace, final String localBranch, final String remoteName, final String remoteBranch) { try { final Git git = Git.open(workspace); final CheckoutCommand command = git.checkout().setName(localBranch).setForce(true); if (findRef(workspace, localBranch) == null) { command.setCreateBranch(true).setUpstreamMode(SetupUpstreamMode.TRACK) .setStartPoint(remote(remoteName, remoteBranch)).call(); } else { command.call(); } return command.getResult(); } catch (final Throwable e) { throw new RuntimeException(e); } }
From source file:com.ejwa.gitdepmavenplugin.DownloaderMojo.java
License:Open Source License
private void checkout(Git git, Pom pom, GitDependency dependency) throws MojoExecutionException { final GitDependencyHandler dependencyHandler = new GitDependencyHandler(dependency); final String version = dependencyHandler.getDependencyVersion(pom); try {// w w w . j av a 2 s . c o m final Repository repository = git.getRepository(); final ObjectId rev = repository.resolve(version); final RevCommit rc = new RevWalk(repository).parseCommit(rev); final CheckoutCommand checkout = git.checkout(); checkout.setName("maven-gitdep-branch-" + rc.getCommitTime()); checkout.setStartPoint(rc); checkout.setCreateBranch(true); checkout.call(); final Status status = checkout.getResult().getStatus(); if (status != Status.OK) { throw new MojoExecutionException( String.format("Invalid checkout state (%s) of dependency.", status)); } } catch (IOException | InvalidRefNameException | RefAlreadyExistsException | RefNotFoundException ex) { throw new MojoExecutionException(String.format("Failed to check out dependency for %s.%s", dependency.getGroupId(), dependency.getArtifactId()), ex); } }
From source file:com.ejwa.mavengitdepplugin.DownloaderMojo.java
License:Open Source License
private void checkout(Git git, POM pom, GitDependency dependency) { final GitDependencyHandler dependencyHandler = new GitDependencyHandler(dependency); final String version = dependencyHandler.getDependencyVersion(pom); try {/*from w w w.j a v a 2 s .c o m*/ final ObjectId rev = git.getRepository().resolve(version); final RevCommit rc = new RevWalk(git.getRepository()).parseCommit(rev); final CheckoutCommand checkout = git.checkout(); checkout.setName("maven-gitdep-branch-" + rc.getCommitTime()); checkout.setStartPoint(rc); checkout.setCreateBranch(true); checkout.call(); final Status status = checkout.getResult().getStatus(); if (!status.equals(Status.OK)) { getLog().error("Status of checkout: " + status); throw new IllegalStateException("Invalid checkout state of dependency."); } } catch (Exception ex) { getLog().error(ex); throw new IllegalStateException("Failed to check out dependency."); } }
From source file:com.mortardata.git.GitUtil.java
License:Apache License
/** * Checkout a branch, tracking remote branch if it exists. * /*from ww w . ja va2 s .com*/ * @param git Git repository * @param branch Name of branch to checkout * @throws GitAPIException if error running git command */ public void checkout(Git git, String branch) throws GitAPIException { List<Ref> branches = getBranches(git); CheckoutCommand checkout = git.checkout().setName(branch); String localBranchRefName = "refs/heads/" + branch; String remoteBranchName = "origin/" + branch; String remoteBranchRefName = "refs/remotes/" + remoteBranchName; if (containsRef(branches, localBranchRefName)) { logger.debug("git checkout " + branch); // local branch exists: no create branch checkout.setCreateBranch(false); } else if (containsRef(branches, remoteBranchRefName)) { logger.debug("git checkout --set-upstream -b " + branch + " " + remoteBranchName); // remote branch exists: track existing branch checkout.setCreateBranch(true).setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM) .setStartPoint(remoteBranchName); } else { // no remote branch exists: create a new branch, no tracking logger.debug("git checkout -b " + branch); checkout.setCreateBranch(true); } checkout.call(); }
From source file:com.rimerosolutions.ant.git.tasks.CheckoutTask.java
License:Apache License
@Override protected void doExecute() throws BuildException { try {// w w w. ja v a2 s .c o m CheckoutCommand checkoutCommand = git.checkout(); if (createBranch) { checkoutCommand.setCreateBranch(true); if (trackBranchOnCreate) { checkoutCommand.setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK); } } if (!GitTaskUtils.isNullOrBlankString(branchName)) { checkoutCommand.setName(branchName); } if (!GitTaskUtils.isNullOrBlankString(startPoint)) { checkoutCommand.setStartPoint(Constants.DEFAULT_REMOTE_NAME + "/" + startPoint); } checkoutCommand.call(); CheckoutResult checkoutResult = checkoutCommand.getResult(); if (checkoutResult.getStatus().equals(CheckoutResult.Status.CONFLICTS)) { String conflicts = checkoutResult.getConflictList().toString(); throw new GitBuildException(String.format("Conflicts were found:%s.", conflicts)); } else if (checkoutResult.getStatus().equals(CheckoutResult.Status.NONDELETED)) { String undeleted = checkoutResult.getUndeletedList().toString(); throw new GitBuildException(String.format("Some files could not be deleted:%s.", undeleted)); } } catch (RefAlreadyExistsException e) { throw new GitBuildException( String.format("Cannot create branch '%s', as it already exists!", branchName), e); } catch (RefNotFoundException e) { throw new GitBuildException(String.format("The branch '%s' was not found.", branchName), e); } catch (InvalidRefNameException e) { throw new GitBuildException("An invalid branch name was specified.", e); } catch (CheckoutConflictException e) { throw new GitBuildException("Some checkout conflicts were found.", e); } catch (GitAPIException e) { throw new GitBuildException(String.format("Could not checkout branch '%s'.", branchName), e); } }
From source file:com.verigreen.jgit.JGitOperator.java
License:Apache License
@Override public String checkout(String branchName, boolean useBranchNameAsStartPoint, boolean createBranch, boolean useForce) { CheckoutCommand command = _git.checkout(); command.setCreateBranch(createBranch); command.setForce(useForce);/*from w w w . j a v a 2 s . c om*/ command.setName(branchName); if (useBranchNameAsStartPoint) { command.setStartPoint(REFS_REMOTES + branchName); } Ref ref = null; try { ref = command.call(); } catch (Throwable e) { throw new RuntimeException(String.format("Failed to checkout branch [%s]", branchName), e); } return ref.getName(); }
From source file:io.fabric8.forge.rest.git.RepositoryResource.java
License:Apache License
protected void checkoutBranch(Git git, GitContext context) throws GitAPIException { String current = currentBranch(git); if (Objects.equals(current, branch)) { return;//from w ww .jav a2 s. co m } 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); }
From source file:io.jenkins.blueocean.blueocean_git_pipeline.GitCacheCloneReadSaveRequest.java
License:Open Source License
private @Nonnull Git getActiveRepository(Repository repository) throws IOException { try {/*from w w w. ja v a 2s .c o m*/ // Clone the bare repository File cloneDir = File.createTempFile("clone", ""); if (cloneDir.exists()) { if (cloneDir.isDirectory()) { FileUtils.deleteDirectory(cloneDir); } else { if (!cloneDir.delete()) { throw new ServiceException.UnexpectedErrorException("Unable to delete repository clone"); } } } if (!cloneDir.mkdirs()) { throw new ServiceException.UnexpectedErrorException("Unable to create repository clone directory"); } String url = repository.getConfig().getString("remote", "origin", "url"); Git gitClient = Git.cloneRepository().setCloneAllBranches(false) .setProgressMonitor(new CloneProgressMonitor(url)) .setURI(repository.getDirectory().getCanonicalPath()).setDirectory(cloneDir).call(); RemoteRemoveCommand remove = gitClient.remoteRemove(); remove.setName("origin"); remove.call(); RemoteAddCommand add = gitClient.remoteAdd(); add.setName("origin"); add.setUri(new URIish(gitSource.getRemote())); add.call(); if (GitUtils.isSshUrl(gitSource.getRemote())) { // Get committer info and credentials User user = User.current(); if (user == null) { throw new ServiceException.UnauthorizedException("Not authenticated"); } BasicSSHUserPrivateKey privateKey = UserSSHKeyManager.getOrCreate(user); // Make sure up-to-date and credentials work GitUtils.fetch(repository, privateKey); } else { FetchCommand fetch = gitClient.fetch(); fetch.call(); } if (!StringUtils.isEmpty(sourceBranch) && !sourceBranch.equals(branch)) { CheckoutCommand checkout = gitClient.checkout(); checkout.setStartPoint("origin/" + sourceBranch); checkout.setName(sourceBranch); checkout.setCreateBranch(true); // to create a new local branch checkout.setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.NOTRACK); checkout.call(); checkout = gitClient.checkout(); checkout.setName(branch); checkout.setCreateBranch(true); // this *should* be a new branch checkout.setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.NOTRACK); checkout.call(); } else { CheckoutCommand checkout = gitClient.checkout(); checkout.setStartPoint("origin/" + branch); checkout.setName(branch); checkout.setCreateBranch(true); // to create a new local branch checkout.setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.NOTRACK); checkout.call(); } return gitClient; } catch (GitAPIException | URISyntaxException ex) { throw new ServiceException.UnexpectedErrorException("Unable to get working repository directory", ex); } }
From source file:net.erdfelt.android.sdkfido.git.internal.InternalGit.java
License:Apache License
@Override public void checkoutBranch(String branchName) throws GitException { try {// w w w . ja v a 2 s . c o m CheckoutCommand command = new Git(repo).checkout(); command.setCreateBranch(false); command.setName(branchName); command.setForce(false); command.call(); } catch (Throwable t) { throw new GitException(t.getMessage(), t); } }
From source file:net.mobid.codetraq.runnables.GitChecker.java
License:Open Source License
private void attachHead(Git g, String branch) { LogService.writeMessage("Trying to attach HEAD to " + g.getRepository().toString()); try {/* w w w . ja v a2 s . c om*/ CheckoutCommand temp = g.checkout(); temp.setCreateBranch(true); temp.setName("temp"); Ref tRef = temp.call(); CheckoutCommand b = g.checkout(); b.setName(branch); b.setCreateBranch(true); b.call(); MergeCommand merge = g.merge(); merge.include(tRef); merge.call(); } catch (Exception ex) { LogService.getLogger(GitChecker.class.getName()).log(Level.SEVERE, null, ex); LogService.writeLog(Level.SEVERE, ex); } }