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.webcat.core.git.GitCloner.java

License:Open Source License

private Repository createWorkingCopyRepositoryIfNecessary(File location, File remoteDir) throws IOException {
    Repository wcRepository;/*from w w  w . j  a v  a  2 s.  c  o m*/

    try {
        wcRepository = RepositoryCache.open(FileKey.lenient(location, FS.DETECTED));
    } catch (RepositoryNotFoundException e) {
        // Create the repository from scratch.

        if (!location.exists()) {
            location.mkdirs();
        }

        InitCommand init = Git.init();
        init.setDirectory(location);
        init.setBare(false);
        wcRepository = init.call().getRepository();

        StoredConfig config = wcRepository.getConfig();
        config.setBoolean("core", null, "bare", false);

        try {
            RefSpec refSpec = new RefSpec().setForceUpdate(true).setSourceDestination(Constants.R_HEADS + "*",
                    Constants.R_REMOTES + "origin" + "/*");

            RemoteConfig remoteConfig = new RemoteConfig(config, "origin");
            remoteConfig.addURI(new URIish(remoteDir.toString()));
            remoteConfig.addFetchRefSpec(refSpec);
            remoteConfig.update(config);
        } catch (URISyntaxException e2) {
            // Do nothing.
        }

        config.save();
    }

    return wcRepository;
}

From source file:org.wso2.carbon.deployment.synchronizer.git.util.GitUtilities.java

License:Open Source License

/**
 * Initialize local git repository/*from   ww  w. ja va 2 s .  c o m*/
 *
 * @param gitRepoDir directory in the local file system
 */
public static void InitGitRepository(File gitRepoDir) {

    try {
        Git.init().setDirectory(gitRepoDir).setBare(false).call();

    } catch (GitAPIException e) {
        String errorMsg = "Initializing local repo at " + gitRepoDir.getPath() + " failed";
        handleError(errorMsg, e);
    }
}

From source file:org.z2env.gitcr.test.TestGitTools.java

License:Apache License

@Test
public void testEmptyBareRepo() throws Exception {
    File tmpFile = null;//from  ww  w  .  j  a  v a  2  s.  c o  m
    try {
        tmpFile = File.createTempFile("TestGitTools_", ".tmp");
        tmpFile.delete();
        Git.init().setBare(true).setDirectory(tmpFile).call();
        assertValid(isValidRepository(new URIish(tmpFile.getCanonicalPath())));
    } finally {
        if (tmpFile != null) {
            FileUtils.delete(tmpFile);
        }
    }
}

From source file:org.z2env.impl.helper.GitTools.java

License:Apache License

/**
 * Clones the given remote repository into the given destination folder. The method clones all branches but doesn't perform a checkout.
 *   //  w ww  . ja  v a2s.co m
 * @param remoteUri URI of the remote repository
 * @param destFolder local destination folder
 * @param credentials user credentials
 * @return the cloned repository
 * @throws IOException if something went wrong
 */
public static Repository cloneRepository(URIish remoteUri, File destFolder, CredentialsProvider credentials,
        int timeout) throws IOException {

    // workaround for http://redmine.z2-environment.net/issues/902:
    // split clone into its piece in order to get the chance to set "core.autocrlf"
    Git gitResult;
    try {
        gitResult = Git.init().setBare(false).setDirectory(destFolder).call();
    } catch (Exception e) {
        throw new IOException("Failed to initialize a new Git repository at " + destFolder.getAbsolutePath(),
                e);
    }

    Repository repo = gitResult.getRepository();

    // setting "core.autocrlf=false" helps to solve http://redmine.z2-environment.net/issues/902
    StoredConfig config = repo.getConfig();
    config.setString(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_AUTOCRLF,
            String.valueOf(false));

    // add origin - clone all branches
    RemoteConfig remoteCfg = null;
    try {
        remoteCfg = new RemoteConfig(config, "origin");
    } catch (URISyntaxException e) {
        throw new IOException("Failed to configure origin repository", e);
    }
    remoteCfg.addURI(remoteUri);
    remoteCfg.addFetchRefSpec(new RefSpec("+refs/heads/*:refs/remotes/origin/*"));
    remoteCfg.update(config);
    config.save();

    // fetch all branches from origin
    try {
        gitResult.fetch().setRemote("origin").setCredentialsProvider(credentials).setTimeout(timeout).call();
    } catch (Exception e) {
        throw new IOException("Failed to fetch from origin!", e);
    }

    return repo;
}