Example usage for org.eclipse.jgit.api CreateBranchCommand setStartPoint

List of usage examples for org.eclipse.jgit.api CreateBranchCommand setStartPoint

Introduction

In this page you can find the example usage for org.eclipse.jgit.api CreateBranchCommand setStartPoint.

Prototype

public CreateBranchCommand setStartPoint(RevCommit startPoint) 

Source Link

Document

Set the start point

Usage

From source file:com.meltmedia.cadmium.core.git.GitService.java

License:Apache License

public void switchBranch(String branchName) throws RefNotFoundException, Exception {
    Repository repository = git.getRepository();
    if (branchName != null && !repository.getBranch().equals(branchName)) {
        log.info("Switching branch from {} to {}", repository.getBranch(), branchName);
        CheckoutCommand checkout = git.checkout();
        if (isTag(branchName)) {
            checkout.setName(branchName);
        } else {/*from   w w  w  . j  a v a 2  s  .  com*/
            checkout.setName("refs/heads/" + branchName);
            if (repository.getRef("refs/heads/" + branchName) == null) {
                CreateBranchCommand create = git.branchCreate();
                create.setName(branchName);
                create.setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM);
                create.setStartPoint("origin/" + branchName);
                create.call();
            }
        }
        checkout.call();
    }
}

From source file:com.microsoft.gittf.core.util.TfsBranchUtil.java

License:Open Source License

/**
 * //from   w w  w  .j a v  a  2 s . co  m
 * Creates a remote tracking ref for tfs. If the repo is bare a regular
 * branch is created too.
 * 
 * @param repository
 * @param startPoint
 * @throws RefAlreadyExistsException
 * @throws RefNotFoundException
 * @throws InvalidRefNameException
 * @throws GitAPIException
 * @throws IOException
 */
public static void create(Repository repository, String startPoint) throws RefAlreadyExistsException,
        RefNotFoundException, InvalidRefNameException, GitAPIException, IOException {
    if (repository.isBare()) {
        CreateBranchCommand createBranch = new Git(repository).branchCreate();
        createBranch.setName(GitTFConstants.GIT_TF_BRANCHNAME);
        createBranch.setForce(true);
        if (startPoint != null && startPoint.length() > 0) {
            createBranch.setStartPoint(startPoint);
        }
        createBranch.call();
    }

    TfsRemoteReferenceUpdate remoteRefUpdate = new TfsRemoteReferenceUpdate(repository, startPoint);
    remoteRefUpdate.update();
}

From source file:com.sap.dirigible.ide.jgit.connector.JGitConnector.java

License:Open Source License

/**
 * //from  w w  w .  j  ava 2s  .  c o m
 * Creates new branch from a particular start point
 * 
 * @param name
 *            the branch name
 * @param startPoint
 *            valid tree-ish object example: "5c15e8", "master", "HEAD",
 *            "21d5a96070353d01c0f30bc0559ab4de4f5e3ca0"
 * @throws RefAlreadyExistsException
 * @throws RefNotFoundException
 * @throws InvalidRefNameException
 * @throws GitAPIException
 */
public void createBranch(String name, String startPoint)
        throws RefAlreadyExistsException, RefNotFoundException, InvalidRefNameException, GitAPIException {
    repository.getConfig().setString(BRANCH, name, MERGE, REFS_HEADS_MASTER);
    CreateBranchCommand createBranchCommand = git.branchCreate();
    createBranchCommand.setName(name);
    createBranchCommand.setStartPoint(startPoint);
    createBranchCommand.setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM);
    createBranchCommand.call();
}

From source file:com.verigreen.jgit.JGitOperator.java

License:Apache License

@Override
public String createBranch(String commitId, String branchName) {

    Ref result = null;//from w  w  w  .j  ava2s.co  m
    CreateBranchCommand branchCreate = _git.branchCreate();
    branchCreate.setName(branchName);
    branchCreate.setStartPoint(commitId);
    try {
        result = branchCreate.call();
    } catch (Throwable e) {
        throw new RuntimeException(
                String.format("Failed creating branch: %s for commit [%s]", branchName, commitId), e);
    }

    return result.getName();
}

From source file:de.blizzy.documentr.repository.ProjectRepositoryManager.java

License:Open Source License

ILockedRepository createBranchRepository(String branchName, String startingBranch)
        throws IOException, GitAPIException {
    Assert.hasLength(branchName);/*from   w w w .j  ava  2s . com*/
    if (startingBranch != null) {
        Assert.hasLength(startingBranch);
    }

    File repoDir = new File(reposDir, branchName);
    if (repoDir.isDirectory()) {
        throw new IllegalStateException("repository already exists: " + repoDir.getAbsolutePath()); //$NON-NLS-1$
    }

    List<String> branches = listBranches();
    if (branches.contains(branchName)) {
        throw new IllegalArgumentException("branch already exists: " + branchName); //$NON-NLS-1$
    }

    if ((startingBranch == null) && !branches.isEmpty()) {
        throw new IllegalArgumentException("must specify a starting branch"); //$NON-NLS-1$
    }

    ILock lock = lockManager.lockAll();
    try {
        Repository centralRepo = null;
        File centralRepoGitDir;
        try {
            centralRepo = getCentralRepositoryInternal(true);
            centralRepoGitDir = centralRepo.getDirectory();
        } finally {
            RepositoryUtil.closeQuietly(centralRepo);
            centralRepo = null;
        }

        Repository repo = null;
        try {
            repo = Git.cloneRepository().setURI(centralRepoGitDir.toURI().toString()).setDirectory(repoDir)
                    .call().getRepository();

            try {
                centralRepo = getCentralRepositoryInternal(true);
                if (!RepositoryUtils.getBranches(centralRepo).contains(branchName)) {
                    CreateBranchCommand createBranchCommand = Git.wrap(centralRepo).branchCreate();
                    if (startingBranch != null) {
                        createBranchCommand.setStartPoint(startingBranch);
                    }
                    createBranchCommand.setName(branchName).call();
                }
            } finally {
                RepositoryUtil.closeQuietly(centralRepo);
            }

            Git git = Git.wrap(repo);
            RefSpec refSpec = new RefSpec("refs/heads/" + branchName + ":refs/remotes/origin/" + branchName); //$NON-NLS-1$ //$NON-NLS-2$
            git.fetch().setRemote("origin").setRefSpecs(refSpec).call(); //$NON-NLS-1$
            git.branchCreate().setName(branchName).setStartPoint("origin/" + branchName).call(); //$NON-NLS-1$
            git.checkout().setName(branchName).call();
        } finally {
            RepositoryUtil.closeQuietly(repo);
        }
    } finally {
        lockManager.unlock(lock);
    }

    return getBranchRepository(branchName);
}

From source file:org.ajoberstar.gradle.git.tasks.GitBranchCreate.java

License:Apache License

/**
 * Execute the creation or update of the branch.
 *//*from  w  w  w  . ja  v a 2  s.c  o m*/
@TaskAction
void branchCreate() {
    CreateBranchCommand cmd = getGit().branchCreate();
    cmd.setName(getBranchName());
    cmd.setStartPoint(getStartPoint());
    cmd.setForce(getForce());

    if (getMode() != null) {
        switch (getMode()) {
        case NO_TRACK:
            cmd.setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.NOTRACK);
            break;
        case TRACK:
            cmd.setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK);
            break;
        case SET_UPSTREAM:
            cmd.setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.SET_UPSTREAM);
            break;
        default:
            throw new AssertionError("Illegal mode: " + getMode());
        }
    }

    try {
        cmd.call();
    } catch (InvalidRefNameException e) {
        throw new GradleException("Invalid branch name: " + getName(), e);
    } catch (RefNotFoundException e) {
        throw new GradleException("Can't find start point: " + getStartPoint(), e);
    } catch (RefAlreadyExistsException e) {
        throw new GradleException("Branch " + getName() + " already exists. Use force=true to modify.", e);
    } catch (GitAPIException e) {
        throw new GradleException("Problem creating or updating branch " + getName(), e);
    }
}

From source file:org.eclipse.che.git.impl.jgit.JGitConnection.java

License:Open Source License

@Override
public Branch branchCreate(BranchCreateRequest request) throws GitException {
    CreateBranchCommand createBranchCommand = getGit().branchCreate().setName(request.getName());
    String start = request.getStartPoint();
    if (start != null) {
        createBranchCommand.setStartPoint(start);
    }//  w  w  w  .  j av  a  2  s .  c  o m
    try {
        Ref brRef = createBranchCommand.call();
        String refName = brRef.getName();
        String displayName = Repository.shortenRefName(refName);
        return newDto(Branch.class).withName(refName).withDisplayName(displayName).withActive(false)
                .withRemote(false);
    } catch (GitAPIException exception) {
        throw new GitException(exception.getMessage(), exception);
    }
}

From source file:org.eclipse.oomph.setup.git.impl.GitCloneTaskImpl.java

License:Open Source License

private static void createBranch(SetupTaskContext context, Git git, String checkoutBranch, String remoteName)
        throws Exception {
    context.log("Creating local branch " + checkoutBranch);

    CreateBranchCommand command = git.branchCreate();
    command.setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM);
    command.setName(checkoutBranch);//from ww  w.  j a  v  a  2 s  . c om
    command.setStartPoint("refs/remotes/" + remoteName + "/" + checkoutBranch);
    command.call();

    StoredConfig config = git.getRepository().getConfig();
    config.setBoolean(ConfigConstants.CONFIG_BRANCH_SECTION, checkoutBranch, ConfigConstants.CONFIG_KEY_REBASE,
            true);
    config.save();
}

From source file:org.eclipse.orion.server.git.servlets.GitBranchHandlerV1.java

License:Open Source License

private boolean handlePost(HttpServletRequest request, HttpServletResponse response, String path)
        throws IOException, JSONException, ServletException, URISyntaxException, CoreException,
        JGitInternalException, GitAPIException {
    Path p = new Path(path);
    // expected path /gitapi/branch/file/{path}
    if (p.segment(0).equals("file")) { //$NON-NLS-1$

        JSONObject toCreate = OrionServlet.readJSONRequest(request);
        String branchName = toCreate.optString(ProtocolConstants.KEY_NAME, null);
        String startPoint = toCreate.optString(GitConstants.KEY_BRANCH_NAME, null);

        if (branchName == null || branchName.isEmpty()) {
            if (startPoint == null || startPoint.isEmpty())
                return statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR,
                        HttpServletResponse.SC_BAD_REQUEST, "Branch name must be provided", null));
            else {
                String shortName = Repository.shortenRefName(startPoint);
                branchName = shortName.substring(shortName.lastIndexOf("/") + 1); //$NON-NLS-1$
            }/*www  .  j a v a  2 s .co m*/
        }

        File gitDir = GitUtils.getGitDir(p);
        Repository db = new FileRepository(gitDir);
        Git git = new Git(db);

        CreateBranchCommand cc = git.branchCreate();
        cc.setName(branchName);

        if (startPoint != null && !startPoint.isEmpty()) {
            cc.setStartPoint(startPoint);
            cc.setUpstreamMode(SetupUpstreamMode.TRACK);
        }

        Ref ref = cc.call();

        // TODO: what if something went wrong, handle exception
        JSONObject result = BranchToJSONConverter.toJSON(ref, db, getURI(request), 2);
        OrionServlet.writeJSONResponse(request, response, result);
        response.setHeader(ProtocolConstants.HEADER_LOCATION, result.getString(ProtocolConstants.KEY_LOCATION));
        response.setStatus(HttpServletResponse.SC_CREATED);
        return true;
    }
    String msg = NLS.bind("Failed to create a branch for {0}", path); //$NON-NLS-1$
    return statusHandler.handleRequest(request, response,
            new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, msg, null));
}

From source file:org.fedoraproject.eclipse.packager.git.FedoraPackagerGitCloneOperation.java

License:Open Source License

/**
 * Create local branches based on existing remotes (uses the JGit API).
 * /*  w  ww. ja  v a2s  . com*/
 * @param monitor
 * @throws CoreException
 */
private void createLocalBranches(Git git, IProgressMonitor monitor) throws CoreException {
    monitor.beginTask(FedoraPackagerGitText.FedoraPackagerGitCloneWizard_createLocalBranchesJob,
            IProgressMonitor.UNKNOWN);

    try {
        // get a list of remote branches
        ListBranchCommand branchList = git.branchList();
        branchList.setListMode(ListMode.REMOTE); // want all remote branches
        List<Ref> remoteRefs = branchList.call();
        for (Ref remoteRef : remoteRefs) {
            String name = remoteRef.getName();
            int index = (Constants.R_REMOTES + "origin/").length(); //$NON-NLS-1$
            // Remove "refs/remotes/origin/" part in branch name
            name = name.substring(index);
            // Use "f14"-like branch naming
            if (name.endsWith("/" + Constants.MASTER)) { //$NON-NLS-1$
                index = name.indexOf("/" + Constants.MASTER); //$NON-NLS-1$
                name = name.substring(0, index);
            }
            // Create all remote branches, except "master"
            if (!name.equals(Constants.MASTER)) {
                CreateBranchCommand branchCreateCmd = git.branchCreate();
                branchCreateCmd.setName(name);
                // Need to set starting point this way in order for tracking
                // to work properly. See: https://bugs.eclipse.org/bugs/show_bug.cgi?id=333899
                branchCreateCmd.setStartPoint(remoteRef.getName());
                // Add remote tracking config in order to not confuse
                // fedpkg
                branchCreateCmd.setUpstreamMode(SetupUpstreamMode.TRACK);
                branchCreateCmd.call();
            }
        }
    } catch (JGitInternalException e) {
        e.printStackTrace();
    } catch (RefAlreadyExistsException e) {
        e.printStackTrace();
    } catch (RefNotFoundException e) {
        e.printStackTrace();
    } catch (InvalidRefNameException e) {
        e.printStackTrace();
    }
}