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

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

Introduction

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

Prototype

public CreateBranchCommand setName(String name) 

Source Link

Document

Set the name of the new branch

Usage

From source file:com.door43.translationstudio.tasks.PullTargetTranslationTask.java

private void createBackupBranch(Repo repo) {
    try {// w  w  w .  ja  v  a 2  s.c om
        Git git = repo.getGit();
        DeleteBranchCommand deleteBranchCommand = git.branchDelete();
        deleteBranchCommand.setBranchNames("backup-master").setForce(true).call();
        CreateBranchCommand createBranchCommand = git.branchCreate();
        createBranchCommand.setName("backup-master").setForce(true).call();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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 ww . ja v a2  s . c  o  m*/
            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  . ja  v  a2 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 ww w . java 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.tasktop.c2c.server.scm.service.GitServiceBean.java

License:Open Source License

@Secured({ Role.User })
@Override/*from   w ww  . j a  v a2 s  .com*/
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 .j av  a  2  s  .  c  om
    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.  ja  v a  2s .  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: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  a va 2  s .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.
 *//* w  w w . j av  a2s  .  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.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);
    command.setStartPoint("refs/remotes/" + remoteName + "/" + checkoutBranch);
    command.call();// w w w.  j a  v  a  2s  .  c o m

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