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

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

Introduction

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

Prototype

@Override
public Ref call()
        throws GitAPIException, RefAlreadyExistsException, RefNotFoundException, InvalidRefNameException 

Source Link

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 {//  w  w w.  java  2s  . c om
            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 ww  w. ja va2 s .  c o  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  ww .  j  a v a  2s. com*/
 * 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.tasktop.c2c.server.scm.service.GitServiceBean.java

License:Open Source License

@Secured({ Role.User })
@Override//from   w  ww .j a v  a2  s . c  o m
public String createBranch(String repoName, String branchName) throws EntityNotFoundException {
    try {
        Repository r = findRepositoryByName(repoName);
        Git git = Git.wrap(r);
        CreateBranchCommand command = git.branchCreate();
        command.setName(branchName);

        /*
         * command.setStartPoint(getStartPoint().name()); if (upstreamMode != null)
         * command.setUpstreamMode(upstreamMode); command.call();
         */

        command.call();
        r.close();
        return branchName;
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (GitAPIException e) {
        throw new RuntimeException(e);
    } catch (URISyntaxException e) {
        throw new RuntimeException(e);
    }
}

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 .  ja v a  2 s . 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:com.worldline.easycukes.scm.utils.GitHelper.java

License:Open Source License

/**
* Create a new branch in the local git repository
* (git checkout -b branchname) and finally pushes new branch on the remote repository (git push)
*
* @param directory the directory in which the local git repository is located
* @param username  the username to be used while pushing
* @param password  the password matching with the provided username to be used
*                  for authentication//from   ww  w  .j  a  v a  2 s. co m
* @param message   the commit message to be used    
*/
public static void createBranch(@NonNull File directory, String branchName, String username, String password,
        String message) throws GitAPIException {

    try {
        final Git git = Git.open(directory);

        final UsernamePasswordCredentialsProvider userCredential = new UsernamePasswordCredentialsProvider(
                username, password);

        CreateBranchCommand branchCommand = git.branchCreate();
        branchCommand.setName(branchName);
        branchCommand.call();

        // and then commit
        final PersonIdent author = new PersonIdent(username, "");
        git.commit().setCommitter(author).setMessage(message).setAuthor(author).call();
        log.info(message);

        git.push().setCredentialsProvider(userCredential).call();
        log.info("Pushed the changes in remote Git repository...");
    } catch (final GitAPIException | IOException e) {
        log.error(e.getMessage(), e);
    }
}

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

License:Apache License

/**
 * Execute the creation or update of the branch.
 *///from  w ww .  j av 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  .ja v  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   w w w  .ja v  a2s.  com
    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$
            }/*  w ww.j  a va 2 s  .  c om*/
        }

        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));
}