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

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

Introduction

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

Prototype

public static InitCommand init() 

Source Link

Document

Return a command object to execute a init command

Usage

From source file:org.jboss.forge.rest.main.GitCommandCompletePostProcessor.java

License:Apache License

@Override
public void firePostCompleteActions(String name, ExecutionRequest executionRequest, RestUIContext context,
        CommandController controller, ExecutionResult results) {
    if (name.equals("project-new")) {
        String targetLocation = null;
        String named = null;/* w w  w .j a  v  a 2 s .c  o m*/
        List<Map<String, String>> inputList = executionRequest.getInputList();
        for (Map<String, String> map : inputList) {
            if (Strings.isNullOrEmpty(targetLocation)) {
                targetLocation = map.get("targetLocation");
            }
            if (Strings.isNullOrEmpty(named)) {
                named = map.get("named");
            }
        }
        if (Strings.isNullOrEmpty(targetLocation)) {
            LOG.warn("No targetLocation could be found!");
        } else if (Strings.isNullOrEmpty(named)) {
            LOG.warn("No named could be found!");
        } else {
            File basedir = new File(targetLocation, named);
            if (!basedir.isDirectory() || !basedir.exists()) {
                LOG.warn("Generated project folder does not exist: " + basedir.getAbsolutePath());
            } else {
                // lets git init...
                System.out.println("About to git init folder " + basedir.getAbsolutePath());
                InitCommand initCommand = Git.init();
                initCommand.setDirectory(basedir);
                try {
                    Git git = initCommand.call();
                    LOG.info("Initialised an empty git configuration repo at {}", basedir.getAbsolutePath());

                    String address = gogsUrl.toString();
                    int idx = address.indexOf("://");
                    if (idx > 0) {
                        address = "http" + address.substring(idx);
                    }
                    if (!address.endsWith("/")) {
                        address += "/";
                    }

                    // TODO take these from the request?
                    String user = gitUser;
                    String password = gitPassword;
                    String authorEmail = "dummy@gmail.com";

                    // lets create the repository
                    GitRepoClient repoClient = new GitRepoClient(address, user, password);
                    CreateRepositoryDTO createRepository = new CreateRepositoryDTO();
                    createRepository.setName(named);

                    String fullName = null;
                    RepositoryDTO repository = repoClient.createRepository(createRepository);
                    if (repository != null) {
                        System.out.println("Got repository: " + JsonHelper.toJson(repository));
                        fullName = repository.getFullName();
                    }
                    if (Strings.isNullOrEmpty(fullName)) {
                        fullName = user + "/" + named;
                    }
                    String htmlUrl = address + user + "/" + named;
                    String remote = address + user + "/" + named + ".git";
                    //results.appendOut("Created git repository " + fullName + " at: " + htmlUrl);

                    results.setOutputProperty("fullName", fullName);
                    results.setOutputProperty("cloneUrl", remote);
                    results.setOutputProperty("htmlUrl", htmlUrl);

                    // now lets import the code and publish
                    LOG.info("Using remote: " + remote);
                    String branch = "master";
                    configureBranch(git, branch, remote);

                    CredentialsProvider credentials = new UsernamePasswordCredentialsProvider(user, password);
                    PersonIdent personIdent = new PersonIdent(user, authorEmail);

                    doAddCommitAndPushFiles(git, credentials, personIdent, remote, branch);
                } catch (Exception e) {
                    handleException(e);
                }

            }
        }
    } else {
        File basedir = context.getInitialSelectionFile();
        String absolutePath = basedir != null ? basedir.getAbsolutePath() : null;
        System.out.println("===== added or mutated files in folder: " + absolutePath);
        if (basedir != null) {
            File gitFolder = new File(basedir, ".git");
            if (gitFolder.exists() && gitFolder.isDirectory()) {
                System.out.println("======== has .git folder so lets add/commit files then push!");

            }
        }
    }
}

From source file:org.jboss.tools.feedhenry.ui.internal.util.GitUtil.java

License:Open Source License

public static Repository createRepository(IProject project, IProgressMonitor monitor) throws CoreException {
    try {/* w  w  w  . j a  va 2 s.c  o m*/
        InitCommand init = Git.init();
        init.setBare(false).setDirectory(project.getLocation().toFile());
        Git git = init.call();
        return git.getRepository();
    } catch (JGitInternalException | GitAPIException e) {
        throw new CoreException(new Status(IStatus.ERROR, FHPlugin.PLUGIN_ID,
                NLS.bind("Could not initialize a git repository at {0}: {1}", getRepositoryPathFor(project),
                        e.getMessage()),
                e));
    }
}

From source file:org.jboss.tools.openshift.egit.core.EGitUtils.java

License:Open Source License

/**
 * Creates a repository for the given project. The repository is created in
 * the .git directory within the given project. The project is not connected
 * with the new repository//w  ww  .j  a v  a2 s. c o  m
 * 
 * @param project
 *            the project to create the repository for.
 * @param monitor
 *            the monitor to report the progress to
 * @return
 * @throws CoreException
 * 
 * @see #connect(IProject, Repository, IProgressMonitor)
 */
public static Repository createRepository(IProject project, IProgressMonitor monitor) throws CoreException {
    try {
        InitCommand init = Git.init();
        init.setBare(false).setDirectory(project.getLocation().toFile());
        Git git = init.call();
        return git.getRepository();
    } catch (JGitInternalException e) {
        throw new CoreException(EGitCoreActivator
                .createErrorStatus(NLS.bind("Could not initialize a git repository at {0}: {1}",
                        getRepositoryPathFor(project), e.getMessage()), e));
    } catch (GitAPIException e) {
        throw new CoreException(EGitCoreActivator
                .createErrorStatus(NLS.bind("Could not initialize a git repository at {0}: {1}",
                        getRepositoryPathFor(project), e.getMessage()), e));
    }
}

From source file:org.jboss.tools.openshift.ui.bot.test.application.v3.adapter.ImportApplicationWizardGitTest.java

License:Open Source License

private Git createRepo() {
    try {/*  ww w  .  ja v  a  2  s  . com*/
        return Git.init().setDirectory(projectFolder).call();
    } catch (IllegalStateException | GitAPIException e) {
        e.printStackTrace();
        fail();
        return null;
    }
}

From source file:org.jenkinsci.git.GitHelper.java

License:Open Source License

/**
 * Initialize a new repo in a new directory
 * // w w w  .j  av  a2 s .  com
 * @return created .git folder
 */
public File initRepo() {
    File dir = tempDirectory();

    Git.init().setDirectory(dir).setBare(false).call();
    File repo = new File(dir, Constants.DOT_GIT);
    assertTrue(repo.exists());
    return repo;
}

From source file:org.jenkinsci.git.InitOperation.java

License:Open Source License

public Repository invoke(File file, VirtualChannel channel) throws IOException {
    String directory = repo.getDirectory();
    File gitDir;/*from   w w w .ja v  a 2s .com*/
    if (directory == null || directory.length() == 0 || ".".equals(directory))
        gitDir = file;
    else
        gitDir = new File(file, directory);
    if (Constants.DOT_GIT.equals(gitDir.getName()))
        gitDir = new File(gitDir, Constants.DOT_GIT);
    InitCommand init = Git.init();
    init.setBare(false);
    init.setDirectory(gitDir);
    return init.call().getRepository();
}

From source file:org.kercoin.magrit.core.user.UserIdentityServiceImplTest.java

License:Open Source License

@BeforeClass
public static void loadRepo() {
    { // repo//  w w w  . j  a  va  2 s .  co m
        File where = new File("target/tmp/repos/test3");
        where.mkdirs();

        try {
            repo = tests.GitTestsUtils.inflate(
                    new File(GitUtilsTest.class.getClassLoader().getResource("archives/test3.zip").toURI()),
                    where);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    { // empty
        File where = new File("target/tmp/repos/empty");
        where.mkdirs();

        try {
            empty = Git.init().setDirectory(where).setBare(false).call().getRepository();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:org.kercoin.magrit.core.utils.GitUtilsTest.java

License:Open Source License

@BeforeClass
public static void setUpClass() {
    File where = new File("target/tmp/repos/test");
    where.mkdirs();/* w w w  .  ja  v a 2  s. co m*/

    File clonePath = new File("target/tmp/repos/clone");
    clonePath.mkdirs();
    Git.init().setDirectory(clonePath).call();

    try {
        test = tests.GitTestsUtils.inflate(
                new File(GitUtilsTest.class.getClassLoader().getResource("archives/test1.zip").toURI()), where);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.kie.commons.java.nio.fs.jgit.AbstractTestInfra.java

License:Apache License

protected Git setupGit(final File tempDir) throws IOException, GitAPIException {

    final Git git = Git.init().setBare(true).setDirectory(tempDir).call();

    commit(git, "master", "name", "name@example.com", "cool1", null, null, new HashMap<String, File>() {
        {/*from  w w  w .j av  a  2s . c om*/
            put("file1.txt", tempFile("content"));
            put("file2.txt", tempFile("content2"));
        }
    });

    return git;
}

From source file:org.kie.commons.java.nio.fs.jgit.util.JGitUtil.java

License:Apache License

public static Git newRepository(final File repoFolder, final boolean bare) throws IOException {
    checkNotNull("repoFolder", repoFolder);

    try {//from w  ww . j  a va  2 s.  co  m
        return Git.init().setBare(bare).setDirectory(repoFolder).call();
    } catch (GitAPIException e) {
        throw new IOException(e);
    }
}