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

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

Introduction

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

Prototype

public Repository getRepository() 

Source Link

Document

Get repository

Usage

From source file:org.springframework.cloud.config.server.JGitEnvironmentRepository.java

License:Apache License

@Override
public Environment findOne(String application, String profile, String label) {
    initialize();//from w  w  w  . j a  v a2 s  .c  om
    Git git = null;
    try {
        git = createGitClient();
        return loadEnvironment(git, application, profile, label);
    } catch (RefNotFoundException e) {
        throw new NoSuchLabelException("No such label: " + label);
    } catch (GitAPIException e) {
        throw new IllegalStateException("Cannot clone or checkout repository", e);
    } catch (Exception e) {
        throw new IllegalStateException("Cannot load environment", e);
    } finally {
        try {
            if (git != null) {
                git.getRepository().close();
            }
        } catch (Exception e) {
            logger.warn("Could not close git repository", e);
        }
    }
}

From source file:org.springframework.cloud.config.server.JGitEnvironmentRepository.java

License:Apache License

private synchronized Environment loadEnvironment(Git git, String application, String profile, String label)
        throws GitAPIException {
    NativeEnvironmentRepository environment = new NativeEnvironmentRepository(getEnvironment());
    git.getRepository().getConfig().setString("branch", label, "merge", label);
    Ref ref = checkout(git, label);
    if (shouldPull(git, ref)) {
        pull(git, label, ref);/*from  w  ww . j  ava2  s .  c  om*/
    }
    environment.setSearchLocations(getSearchLocations(getWorkingDirectory()));
    Environment result = environment.findOne(application, profile, "");
    result.setLabel(label);
    return clean(result);
}

From source file:org.springframework.cloud.config.server.JGitEnvironmentRepository.java

License:Apache License

private boolean shouldPull(Git git, Ref ref) throws GitAPIException {
    return git.status().call().isClean() && ref != null
            && git.getRepository().getConfig().getString("remote", "origin", "url") != null;
}

From source file:org.springframework.cloud.config.server.JGitEnvironmentRepository.java

License:Apache License

/**
 * Assumes we are on a tracking branch (should be safe)
 *///from   w ww.j  a  va  2s.c  o m
private void pull(Git git, String label, Ref ref) {
    PullCommand pull = git.pull();
    try {
        if (hasText(getUsername())) {
            setCredentialsProvider(pull);
        }
        pull.call();
    } catch (Exception e) {
        logger.warn("Could not pull remote for " + label + " (current ref=" + ref + "), remote: "
                + git.getRepository().getConfig().getString("remote", "origin", "url"));
    }
}

From source file:org.springframework.cloud.release.internal.git.GitRepo.java

License:Apache License

/**
 * Clones the project/*from  ww  w . j  a  va  2  s . c o  m*/
 * @param projectUri - URI of the project
 * @return file where the project was cloned
 */
File cloneProject(URI projectUri) {
    try {
        log.info("Cloning repo from [{}] to [{}]", projectUri, this.basedir);
        Git git = cloneToBasedir(projectUri, this.basedir);
        if (git != null) {
            git.close();
        }
        File clonedRepo = git.getRepository().getWorkTree();
        log.info("Cloned repo to [{}]", clonedRepo);
        return clonedRepo;
    } catch (Exception e) {
        throw new IllegalStateException("Exception occurred while cloning repo", e);
    }
}

From source file:org.uberfire.java.nio.fs.jgit.JGitFileSystemProvider.java

License:Apache License

@Override
public FileSystem newFileSystem(final URI uri, final Map<String, ?> env)
        throws IllegalArgumentException, IOException, SecurityException, FileSystemAlreadyExistsException {
    checkNotNull("uri", uri);
    checkCondition("uri scheme not supported",
            uri.getScheme().equals(getScheme()) || uri.getScheme().equals("default"));
    checkURI("uri", uri);
    checkNotNull("env", env);

    final String name = extractRepoName(uri);

    if (fileSystems.containsKey(name)) {
        throw new FileSystemAlreadyExistsException();
    }// www  .  ja  v a 2 s . c om

    ListBranchCommand.ListMode listMode;
    if (env.containsKey(GIT_LIST_ROOT_BRANCH_MODE)) {
        try {
            listMode = ListBranchCommand.ListMode.valueOf((String) env.get(GIT_LIST_ROOT_BRANCH_MODE));
        } catch (Exception ex) {
            listMode = null;
        }
    } else {
        listMode = null;
    }

    final Git git;
    final CredentialsProvider credential;

    boolean bare = true;
    final String outPath = (String) env.get(GIT_ENV_PROP_DEST_PATH);

    final File repoDest;
    if (outPath != null) {
        repoDest = new File(outPath, name + DOT_GIT_EXT);
    } else {
        repoDest = new File(FILE_REPOSITORIES_ROOT, name + DOT_GIT_EXT);
    }

    if (env.containsKey(GIT_DEFAULT_REMOTE_NAME)) {
        final String originURI = env.get(GIT_DEFAULT_REMOTE_NAME).toString();
        credential = buildCredential(env);
        git = cloneRepository(repoDest, originURI, bare, credential);
    } else {
        credential = buildCredential(null);
        git = newRepository(repoDest, bare);
    }

    final JGitFileSystem fs = new JGitFileSystem(this, fullHostNames, git, name, listMode, credential);
    fileSystems.put(name, fs);
    repoIndex.put(fs.gitRepo().getRepository(), fs);

    boolean init = false;

    if (env.containsKey(INIT) && Boolean.valueOf(env.get(INIT).toString())) {
        init = true;
    }

    if (!env.containsKey(GIT_DEFAULT_REMOTE_NAME) && init) {
        try {
            final URI initURI = URI.create(getScheme() + "://master@" + name + "/readme.md");
            final CommentedOption op = setupOp(env);
            final OutputStream stream = newOutputStream(getPath(initURI), op);
            final String _init = "Repository Init Content\n" + "=======================\n" + "\n"
                    + "Your project description here.";
            stream.write(_init.getBytes());
            stream.close();
        } catch (final Exception e) {
        }
        if (!bare) {
            //todo: checkout
        }
    }

    final Object _clusterService = env.get("clusterService");
    if (_clusterService != null && _clusterService instanceof ClusterService) {
        clusterMap.put(git.getRepository(), (ClusterService) _clusterService);
    }

    if (DAEMON_ENABLED && daemonService != null && !daemonService.isRunning()) {
        buildAndStartDaemon();
    }

    return fs;
}

From source file:org.uberfire.java.nio.fs.jgit.JGitFileSystemProviderTest.java

License:Apache License

@Test
public void testInputStream() throws IOException {
    final File parentFolder = createTempDirectory();
    final File gitFolder = new File(parentFolder, "mytest.git");

    final Git origin = JGitUtil.newRepository(gitFolder, true);

    commit(origin, "master", "user", "user@example.com", "commit message", null, null, false,
            new HashMap<String, File>() {
                {//w  ww  .j  a  v a  2  s  .c  o  m
                    put("myfile.txt", tempFile("temp\n.origin\n.content"));
                }
            });

    final URI newRepo = URI.create("git://inputstream-test-repo");

    final Map<String, Object> env = new HashMap<String, Object>() {
        {
            put(JGitFileSystemProvider.GIT_DEFAULT_REMOTE_NAME,
                    origin.getRepository().getDirectory().toString());
        }
    };

    final FileSystem fs = PROVIDER.newFileSystem(newRepo, env);

    assertThat(fs).isNotNull();

    final Path path = PROVIDER.getPath(URI.create("git://origin/master@inputstream-test-repo/myfile.txt"));

    final InputStream inputStream = PROVIDER.newInputStream(path);
    assertThat(inputStream).isNotNull();

    final String content = new Scanner(inputStream).useDelimiter("\\A").next();

    inputStream.close();

    assertThat(content).isNotNull().isEqualTo("temp\n.origin\n.content");
}

From source file:org.uberfire.java.nio.fs.jgit.JGitFileSystemProviderTest.java

License:Apache License

@Test
public void testInputStream2() throws IOException {

    final File parentFolder = createTempDirectory();
    final File gitFolder = new File(parentFolder, "mytest.git");

    final Git origin = JGitUtil.newRepository(gitFolder, true);

    commit(origin, "master", "user", "user@example.com", "commit message", null, null, false,
            new HashMap<String, File>() {
                {//from  w w  w.  j  ava 2  s .  c  o m
                    put("path/to/file/myfile.txt", tempFile("temp\n.origin\n.content"));
                }
            });

    final URI newRepo = URI.create("git://xinputstream-test-repo");

    final Map<String, Object> env = new HashMap<String, Object>() {
        {
            put(JGitFileSystemProvider.GIT_DEFAULT_REMOTE_NAME,
                    origin.getRepository().getDirectory().toString());
        }
    };

    final FileSystem fs = PROVIDER.newFileSystem(newRepo, env);

    assertThat(fs).isNotNull();

    final Path path = PROVIDER
            .getPath(URI.create("git://origin/master@xinputstream-test-repo/path/to/file/myfile.txt"));

    final InputStream inputStream = PROVIDER.newInputStream(path);
    assertThat(inputStream).isNotNull();

    final String content = new Scanner(inputStream).useDelimiter("\\A").next();

    inputStream.close();

    assertThat(content).isNotNull().isEqualTo("temp\n.origin\n.content");
}

From source file:org.uberfire.java.nio.fs.jgit.JGitFileSystemProviderTest.java

License:Apache License

@Test(expected = NoSuchFileException.class)
public void testInputStream3() throws IOException {

    final File parentFolder = createTempDirectory();
    final File gitFolder = new File(parentFolder, "mytest.git");

    final Git origin = JGitUtil.newRepository(gitFolder, true);

    commit(origin, "master", "user", "user@example.com", "commit message", null, null, false,
            new HashMap<String, File>() {
                {// w  w w .  j a  va 2 s. c o  m
                    put("path/to/file/myfile.txt", tempFile("temp\n.origin\n.content"));
                }
            });

    final URI newRepo = URI.create("git://xxinputstream-test-repo");

    final Map<String, Object> env = new HashMap<String, Object>() {
        {
            put(JGitFileSystemProvider.GIT_DEFAULT_REMOTE_NAME,
                    origin.getRepository().getDirectory().toString());
        }
    };

    final FileSystem fs = PROVIDER.newFileSystem(newRepo, env);

    assertThat(fs).isNotNull();

    final Path path = PROVIDER.getPath(URI.create("git://origin/master@xxinputstream-test-repo/path/to"));

    PROVIDER.newInputStream(path);
}

From source file:org.uberfire.java.nio.fs.jgit.JGitFileSystemProviderTest.java

License:Apache License

@Test(expected = NoSuchFileException.class)
public void testInputStreamNoSuchFile() throws IOException {

    final File parentFolder = createTempDirectory();
    final File gitFolder = new File(parentFolder, "mytest.git");

    final Git origin = JGitUtil.newRepository(gitFolder, true);

    commit(origin, "master", "user1", "user1@example.com", "commitx", null, null, false,
            new HashMap<String, File>() {
                {/* w w w .j  a v a 2s .c o  m*/
                    put("file.txt", tempFile("temp.origin.content.2"));
                }
            });

    final URI newRepo = URI.create("git://inputstream-not-exists-test-repo");

    final Map<String, Object> env = new HashMap<String, Object>() {
        {
            put(JGitFileSystemProvider.GIT_DEFAULT_REMOTE_NAME,
                    origin.getRepository().getDirectory().toString());
        }
    };

    final FileSystem fs = PROVIDER.newFileSystem(newRepo, env);

    assertThat(fs).isNotNull();

    final Path path = PROVIDER
            .getPath(URI.create("git://origin/master@inputstream-not-exists-test-repo/temp.txt"));

    PROVIDER.newInputStream(path);
}