Example usage for org.eclipse.jgit.api Git open

List of usage examples for org.eclipse.jgit.api Git open

Introduction

In this page you can find the example usage for org.eclipse.jgit.api Git open.

Prototype

public static Git open(File dir) throws IOException 

Source Link

Document

Open repository

Usage

From source file:com.stormcloud.ide.api.git.GitManager.java

License:Open Source License

@Override
public void commit(String repository, String message, String[] files, boolean all) throws GitManagerException {

    try {/*from   ww  w.  j av  a 2  s.  c  o  m*/

        Git git = Git.open(new File(repository));

        CommitCommand commit = git.commit();

        commit.setMessage(message);

        if (all) {

            commit.setAll(true);

        } else {

            for (String file : files) {

                commit.setOnly(file);
            }

        }

        RevCommit result = commit.call();

        // result....

    } catch (IOException e) {
        LOG.error(e);
        throw new GitManagerException(e);
    } catch (GitAPIException e) {
        LOG.error(e);
        throw new GitManagerException(e);
    }
}

From source file:com.stormcloud.ide.api.git.GitManager.java

License:Open Source License

@Override
public String getStatus(String file, String userHome) throws GitManagerException {

    String tmpRelativePath = new File(file).getAbsolutePath().replaceFirst(userHome + "/projects", "")
            .replaceFirst("/", "");

    String project;//from  w  w w  .  j  av  a2s .  c  o m

    if (tmpRelativePath.contains("/")) {

        project = tmpRelativePath.substring(0, tmpRelativePath.indexOf('/'));

    } else {

        project = tmpRelativePath;
    }

    String repository = userHome + "/projects/" + project;
    String relativePath = tmpRelativePath.replaceFirst(project, "").replaceFirst("/", "");

    String status = "unmodified";

    try {

        Git git = Git.open(new File(repository));

        IndexDiff diff = new IndexDiff(git.getRepository(), "HEAD", new FileTreeIterator(git.getRepository()));

        diff.setFilter(PathFilterGroup.createFromStrings(relativePath));

        diff.diff();

        if (!diff.getModified().isEmpty() || !diff.getChanged().isEmpty()) {
            return "modified";
        }

        if (!diff.getMissing().isEmpty() || !diff.getRemoved().isEmpty()) {
            return "missing";
        }

        if (!diff.getUntracked().isEmpty() || !diff.getUntrackedFolders().isEmpty()
                || !diff.getAdded().isEmpty()) {

            return "untracked";
        }

        if (!diff.getConflicting().isEmpty()) {
            return "conflict";
        }

        return status;

    } catch (IOException e) {
        LOG.error(e);
        throw new GitManagerException(e);
    }
}

From source file:com.tasktop.c2c.server.scm.service.GitServiceBean.java

License:Open Source License

protected List<ScmRepository> getHostedRepositories() throws IOException {

    List<ScmRepository> result = new ArrayList<ScmRepository>();

    File hostedDir = repositoryProvider.getTenantHostedBaseDir();
    if (hostedDir != null && hostedDir.exists()) {

        for (String repoName : hostedDir.list()) {
            // if the fileName starts with a '.' then it's not a git repo
            if (!repoName.startsWith(".")) {
                ScmRepository repo = new ScmRepository();
                repo.setName(repoName);/*from   www  .ja v a 2s.  com*/
                repo.setScmLocation(ScmLocation.CODE2CLOUD);
                repo.setType(ScmType.GIT);
                repo.setUrl(profileServiceConfiguration.getHostedScmUrlPrefix(
                        TenancyUtil.getCurrentTenantProjectIdentifer()) + repo.getName()); // FIXME
                repo.setAlternateUrl(computeSshUrl(repo));
                File descriptionFile = new File(hostedDir.getPath() + File.separator + repoName, "description");
                if (descriptionFile.exists()) {
                    String description = org.apache.commons.io.FileUtils.readFileToString(descriptionFile);
                    repo.setDescription(description);
                }
                result.add(repo);

                Git git = Git.open(new File(hostedDir, repoName));

                setBranchesAndTags(repo, git);
            }
        }
    }

    return result;
}

From source file:com.tasktop.c2c.server.scm.service.GitServiceBean.java

License:Open Source License

private List<ScmRepository> getExternalRepositories() throws IOException, URISyntaxException {
    List<ScmRepository> result = new ArrayList<ScmRepository>();

    File hostedDir = repositoryProvider.getTenantMirroredBaseDir();
    if (hostedDir != null && hostedDir.exists()) {
        for (String repoName : hostedDir.list()) {
            ScmRepository repo = new ScmRepository();
            repo.setName(repoName);/*from w ww. ja  v a 2 s  . c  om*/
            repo.setScmLocation(ScmLocation.EXTERNAL);
            repo.setType(ScmType.GIT);

            Git git = Git.open(new File(hostedDir, repoName));
            RemoteConfig config = new RemoteConfig(git.getRepository().getConfig(),
                    Constants.DEFAULT_REMOTE_NAME);
            repo.setUrl(config.getURIs().get(0).toString());

            setBranchesAndTags(repo, git);

            result.add(repo);
        }
    }

    return result;
}

From source file:com.tenxdev.ovcs.command.InitCommand.java

License:Open Source License

private void initGitRepo(final Path workingDir, final String remoteUri, final String connectionString)
        throws OvcsException {
    try {//from w ww .j  a  v  a 2s .  co  m
        System.out.println("Cloning git repo");
        Git.cloneRepository().setDirectory(workingDir.toFile()).setCloneAllBranches(true).setRemote("origin")
                .setURI(remoteUri).setProgressMonitor(new TextProgressMonitor()).call();
        final Git git = Git.open(workingDir.toFile());
        final StoredConfig config = git.getRepository().getConfig();
        config.setString("database", null, "connectionString", connectionString);
        config.save();
        git.checkout().setName("master").setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM);
    } catch (GitAPIException | IOException e) {
        throw new OvcsException("Unable to initialize Git repo: " + e.getMessage(), e);
    }
}

From source file:com.tnd.eso.integration.scm.scripts.repository.GitParser.java

License:Open Source License

protected GitParser() {
    try {/*w w  w.j  a  va  2  s .  co  m*/
        gitWorkDir = new File(dir);
        git = Git.open(gitWorkDir);
        repo = git.getRepository();

        // get head commit hash
        ObjectId lastCommitId = repo.resolve(Constants.HEAD);
        RevWalk revWalk = new RevWalk(repo);
        RevCommit commit = revWalk.parseCommit(lastCommitId);
        head = commit.getName().substring(0, 7);
        revWalk.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.worldline.easycukes.scm.utils.GitHelper.java

License:Open Source License

/**
 * Adds all the files of the specified directory in the local git repository
 * (git add .), then commits the changes (git commit .), and finally pushes
 * the changes 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  w  w  w  . j ava2s. com
 * @param message   the commit message to be used
 * @throws GitAPIException if something's going wrong while interacting with Git
 * @throws IOException     if something's going wrong while manipulating the local
 *                         repository
 */
public static void commitAndPush(@NonNull File directory, String username, String password, String message)
        throws GitAPIException, IOException {
    try {
        final Git git = Git.open(directory);
        // run the add
        final AddCommand addCommand = git.add();
        for (final String filePath : directory.list())
            if (!".git".equals(filePath))
                addCommand.addFilepattern(filePath);
        addCommand.call();
        log.info("Added content of the directory" + directory + " in the Git repository located in "
                + directory.toString());
        // and then commit
        final PersonIdent author = new PersonIdent(username, "");
        git.commit().setCommitter(author).setMessage(message).setAuthor(author).call();
        log.info("Commited the changes in the Git repository...");
        // and finally push
        final UsernamePasswordCredentialsProvider userCredential = new UsernamePasswordCredentialsProvider(
                username, password);
        git.push().setCredentialsProvider(userCredential).call();
        log.info("Pushed the changes in remote Git repository...");
    } catch (final GitAPIException e) {
        log.error(e.getMessage(), e);
        throw e;
    }
}

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   w w  w .ja  va 2s.  c  o 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:com.worldline.easycukes.scm.utils.GitHelper.java

License:Open Source License

/**
 * Delete a branch in the local git repository
 * (git branch -d 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  w w w.ja v a2s .  com
 * @param message   the commit message to be used    
 */
public static void deleteBranch(@NonNull File directory, String branchName, String username, String password,
        String message) {

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

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

        DeleteBranchCommand deleteBranchCommand = git.branchDelete();

        deleteBranchCommand.setBranchNames(branchName);
        deleteBranchCommand.call();
        log.info("Develop branch deleted");

        // 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:com.worldline.easycukes.scm.utils.GitHelper.java

License:Open Source License

/**
 * Create a new tag in the local git repository
 * (git checkout tagname) 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 ava 2 s .co m*/
 * @param message   the commit message to be used    
 */
public static void createTag(@NonNull File directory, String tagName, String username, String password,
        String message) {

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

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

        TagCommand tagCommand = git.tag();
        tagCommand.setName(tagName);
        tagCommand.setMessage(message);
        tagCommand.call();
        log.info("Tag created");

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