Java Code Examples for org.eclipse.jgit.api.CheckoutCommand#call()

The following examples show how to use org.eclipse.jgit.api.CheckoutCommand#call() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GitMergeUtil.java    From MergeProcessor with Apache License 2.0 6 votes vote down vote up
/**
 * {@code git checkout}
 */
private void checkout() {
	final String localBranch = getLocalTargetBranch();
	if (localBranch == null) {
		return;
	}
	try {
		final List<Ref> branchListResult = repo.branchList().call();
		final boolean localBranchExists = branchListResult.stream()
				.anyMatch(ref -> ref.getName().endsWith(localBranch));
		CheckoutCommand branchCmd = repo.checkout().setName(localBranch);
		if (!localBranchExists) {
			branchCmd = branchCmd.setCreateBranch(true).setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM)
					.setStartPoint(mergeUnit.getBranchTarget());
		}
		branchCmd.call();
	} catch (GitAPIException e) {
		exception = new MergeUnitException(String.format("Could not checkout branch '%s'", localBranch), e); //$NON-NLS-1$
	}
}
 
Example 2
Source File: GitRepo.java    From spring-cloud-release-tools with Apache License 2.0 6 votes vote down vote up
private Ref checkoutBranch(File projectDir, String branch) throws GitAPIException {
	Git git = this.gitFactory.open(projectDir);
	CheckoutCommand command = git.checkout().setName(branch);
	try {
		if (shouldTrack(git, branch)) {
			trackBranch(command, branch);
		}
		return command.call();
	}
	catch (GitAPIException e) {
		deleteBaseDirIfExists();
		throw e;
	}
	finally {
		git.close();
	}
}
 
Example 3
Source File: RepositoryResource.java    From fabric8-forge with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 4
Source File: GitRepo.java    From spring-cloud-contract with Apache License 2.0 6 votes vote down vote up
private Ref checkoutBranch(File projectDir, String branch) throws GitAPIException {
	Git git = this.gitFactory.open(projectDir);
	CheckoutCommand command = git.checkout().setName(branch);
	try {
		if (shouldTrack(git, branch)) {
			trackBranch(command, branch);
		}
		return command.call();
	}
	catch (GitAPIException e) {
		deleteBaseDirIfExists();
		throw e;
	}
	finally {
		git.close();
	}
}
 
Example 5
Source File: GitConfigurationSource.java    From cfg4j with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example 6
Source File: JGitOperator.java    From verigreen with Apache License 2.0 6 votes vote down vote up
@Override
public String checkout(
        String branchName,
        boolean useBranchNameAsStartPoint,
        boolean createBranch,
        boolean useForce) {
    
    CheckoutCommand command = _git.checkout();
    command.setCreateBranch(createBranch);
    command.setForce(useForce);
    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();
}
 
Example 7
Source File: GitMergeUtil.java    From MergeProcessor with Apache License 2.0 5 votes vote down vote up
/**
 * Evaluate the commit message for the merge unit.
 */
private void evaluteCommitMessage() {
	final String localBranch = getLocalSourceBranch();
	if (localBranch == null) {
		return;
	}
	try {
		final List<Ref> branchListResult = repo.branchList().call();
		final boolean localBranchExists = branchListResult.stream()
				.anyMatch(ref -> ref.getName().endsWith(localBranch));
		CheckoutCommand branchCmd = repo.checkout().setName(localBranch);
		if (!localBranchExists) {
			branchCmd = branchCmd.setCreateBranch(true).setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM)
					.setStartPoint(mergeUnit.getBranchSource());
		}
		branchCmd.call();

		final ObjectId id = ObjectId.fromString(mergeUnit.getRevisionInfo());
		commitMessage = repo.log().add(id).call().iterator().next().getFullMessage();
		/*
		 * Remove merge processor commit information, if the commmit is done by merge
		 * processor e.g. MP [29c64cf540d8f3ea116c3683eade862c3d996dfb] V1.1 -> master:
		 * (2018-03-06 12:17:22)
		 */
		commitMessage = commitMessage.replaceAll("MP \\[[A-Za-z0-9]*\\] .* -> .*: \\(.*\\) ", ""); //$NON-NLS-1$ //$NON-NLS-2$
	} catch (GitAPIException | MissingObjectException | IncorrectObjectTypeException e) {
		exception = new MergeUnitException(String.format("Could not checkout branch '%s'", localBranch), e); //$NON-NLS-1$
	}
}
 
Example 8
Source File: ResourcePackRepository.java    From I18nUpdateMod with MIT License 5 votes vote down vote up
public void sparseCheckout(Collection<String> subPathSet, ProgressMonitor monitor) {
    try {
        // sparse checkout
        CheckoutCommand checkoutCommand = gitRepo.checkout();

        checkoutCommand.setProgressMonitor(monitor)
                .setName(branch)
                .setStartPoint(branch);

        subPathSet.forEach(checkoutCommand::addPath);
        checkoutCommand.call();
    } catch (Exception e) {
        logger.error("Exception caught while checking out: ", e);
    }
}
 
Example 9
Source File: GitRepository.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void checkoutToSelectedBranch(final Git git) throws IOException, GitAPIException {
    boolean createBranch = !ObjectId.isId(branch);
    if (createBranch) {
        Ref ref = git.getRepository().exactRef(R_HEADS + branch);
        if (ref != null) {
            createBranch = false;
        }
    }
    CheckoutCommand checkout = git.checkout().setCreateBranch(createBranch).setName(branch);
    checkout.call();
    if (checkout.getResult().getStatus() == CheckoutResult.Status.ERROR) {
        throw ServerLogger.ROOT_LOGGER.failedToPullRepository(null, defaultRemoteRepository);
    }
}
 
Example 10
Source File: GitRepository.java    From Getaviz with Apache License 2.0 4 votes vote down vote up
public void reset() throws Exception {
	CheckoutCommand checkout = git.checkout();
	checkout.setName(getDefaultBranch().getName());
	checkout.setCreateBranch(false);
	checkout.call();
}
 
Example 11
Source File: JGitEnvironmentRepositoryIntegrationTests.java    From spring-cloud-config with Apache License 2.0 4 votes vote down vote up
private String getCommitID(Git git, String label) throws GitAPIException {
	CheckoutCommand checkout = git.checkout();
	checkout.setName(label);
	Ref localRef = checkout.call();
	return localRef.getObjectId().getName();
}
 
Example 12
Source File: StudioNodeSyncPublishedTask.java    From studio with GNU General Public License v3.0 4 votes vote down vote up
private void updatePublishedBranch(Git git, ClusterMember remoteNode, String branch) throws CryptoException,
        GitAPIException, IOException, ServiceLayerException {
    logger.debug("Update published environment " + branch + " from " + remoteNode.getLocalAddress() +
            " for site " + siteId);
    final Path tempKey = Files.createTempFile(UUID.randomUUID().toString(), ".tmp");

    Repository repo = git.getRepository();
    Ref ref = repo.exactRef(Constants.R_HEADS + branch);
    boolean createBranch = (ref == null);

    logger.debug("Checkout " + branch);
    CheckoutCommand checkoutCommand = git.checkout()
            .setName(branch)
            .setCreateBranch(createBranch);
    if (createBranch) {
        checkoutCommand.setStartPoint(remoteNode.getGitRemoteName() + "/" + branch);
    }
    checkoutCommand.call();

    FetchCommand fetchCommand = git.fetch().setRemote(remoteNode.getGitRemoteName());
    fetchCommand = configureAuthenticationForCommand(remoteNode, fetchCommand, tempKey);
    FetchResult fetchResult = fetchCommand.call();

    ObjectId commitToMerge;
    Ref r;
    if (fetchResult != null) {
        r = fetchResult.getAdvertisedRef(branch);
        if (r == null) {
            r = fetchResult.getAdvertisedRef(Constants.R_HEADS + branch);
        }
        if (r != null) {
            commitToMerge = r.getObjectId();

            MergeCommand mergeCommand = git.merge();
            mergeCommand.setMessage(studioConfiguration.getProperty(REPO_SYNC_DB_COMMIT_MESSAGE_NO_PROCESSING));
            mergeCommand.setCommit(true);
            mergeCommand.include(remoteNode.getGitRemoteName(), commitToMerge);
            mergeCommand.setStrategy(MergeStrategy.THEIRS);
            mergeCommand.call();
        }
    }

    Files.delete(tempKey);
}