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.uberfire.java.nio.fs.jgit.JGitFileSystemProviderTest.java

License:Apache License

@Test
public void testNewOutputStream() throws Exception {
    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 ww w  .  java  2 s. c  o m*/
                    put("myfile.txt", tempFile("temp\n.origin\n.content"));
                }
            });
    commit(origin, "user_branch", "user", "user@example.com", "commit message", null, null, false,
            new HashMap<String, File>() {
                {
                    put("path/to/some/file/myfile.txt", tempFile("some\n.content\nhere"));
                }
            });

    final URI newRepo = URI.create("git://outstream-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://user_branch@outstream-test-repo/some/path/myfile.txt"));

    final OutputStream outStream = PROVIDER.newOutputStream(path);
    assertThat(outStream).isNotNull();
    outStream.write("my cool content".getBytes());
    outStream.close();

    final InputStream inStream = PROVIDER.newInputStream(path);

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

    inStream.close();

    assertThat(content).isNotNull().isEqualTo("my cool content");

    try {
        PROVIDER.newOutputStream(
                PROVIDER.getPath(URI.create("git://user_branch@outstream-test-repo/some/path/")));
        failBecauseExceptionWasNotThrown(org.uberfire.java.nio.IOException.class);
    } catch (Exception e) {
    }
}

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

License:Apache License

@Test
public void testNewOutputStreamWithJGitOp() throws Exception {
    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  a v a  2s  .c o m
                    put("myfile.txt", tempFile("temp\n.origin\n.content"));
                }
            });
    commit(origin, "user_branch", "user", "user@example.com", "commit message", null, null, false,
            new HashMap<String, File>() {
                {
                    put("path/to/some/file/myfile.txt", tempFile("some\n.content\nhere"));
                }
            });

    final URI newRepo = URI.create("git://outstreamwithop-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 SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");

    final CommentedOption op = new CommentedOption("User Tester", "user.tester@example.com",
            "omg, is it the end?", formatter.parse("31/12/2012"));

    final Path path = PROVIDER
            .getPath(URI.create("git://user_branch@outstreamwithop-test-repo/some/path/myfile.txt"));

    final OutputStream outStream = PROVIDER.newOutputStream(path, op);
    assertThat(outStream).isNotNull();
    outStream.write("my cool content".getBytes());
    outStream.close();

    final InputStream inStream = PROVIDER.newInputStream(path);

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

    inStream.close();

    assertThat(content).isNotNull().isEqualTo("my cool content");
}

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

License:Apache License

@Test
public void testToHTTPMirrorSuccess() throws IOException, GitAPIException {
    final File parentFolder = createTempDirectory();
    final File directory = new File(parentFolder, TARGET_GIT);
    new Clone(directory, ORIGIN, false, CredentialsProvider.getDefault(), null).execute();

    final Git cloned = Git.open(directory);

    assertThat(cloned).isNotNull();/*from w w  w . ja  v  a  2s .  c o  m*/

    assertThat(new ListRefs(cloned.getRepository()).execute()).is(new Condition<List<Ref>>() {
        @Override
        public boolean matches(final List<Ref> refs) {
            return refs.size() > 0;
        }
    });

    assertThat(new ListRefs(cloned.getRepository()).execute().get(0).getName()).isEqualTo("refs/heads/master");

    URIish remoteUri = cloned.remoteList().call().get(0).getURIs().get(0);
    String remoteUrl = remoteUri.getScheme() + "://" + remoteUri.getHost() + remoteUri.getPath();
    assertThat(remoteUrl).isEqualTo(ORIGIN);
}

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

License:Apache License

@Test
public void testEmptyCredentials() throws IOException, GitAPIException {
    final File parentFolder = createTempDirectory();
    final File directory = new File(parentFolder, TARGET_GIT);
    new Clone(directory, ORIGIN, false, null, null).execute();

    final Git cloned = Git.open(directory);

    assertThat(cloned).isNotNull();/*from  w  ww.ja  v a 2  s . c  o m*/

    assertThat(new ListRefs(cloned.getRepository()).execute()).is(new Condition<List<Ref>>() {
        @Override
        public boolean matches(final List<Ref> refs) {
            return refs.size() > 0;
        }
    });

    assertThat(new ListRefs(cloned.getRepository()).execute().get(0).getName()).isEqualTo("refs/heads/master");

    URIish remoteUri = cloned.remoteList().call().get(0).getURIs().get(0);
    String remoteUrl = remoteUri.getScheme() + "://" + remoteUri.getHost() + remoteUri.getPath();
    assertThat(remoteUrl).isEqualTo(ORIGIN);
}

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

License:Apache License

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

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

    commit(origin, "user_branch", "name", "name@example.com", "commit!", null, null, false,
            new HashMap<String, File>() {
                {/*w  w  w. ja  v a2s.  c o m*/
                    put("file2.txt", tempFile("temp2222"));
                }
            });
    commit(origin, "master", "name", "name@example.com", "commit", null, null, false,
            new HashMap<String, File>() {
                {
                    put("file.txt", tempFile("temp"));
                }
            });
    commit(origin, "master", "name", "name@example.com", "commit", null, null, false,
            new HashMap<String, File>() {
                {
                    put("file3.txt", tempFile("temp3"));
                }
            });

    final File gitClonedFolder = new File(parentFolder, "myclone.git");

    final Git git = cloneRepository(gitClonedFolder, origin.getRepository().getDirectory().toString(), true,
            CredentialsProvider.getDefault());

    assertThat(git).isNotNull();

    assertThat(branchList(git, ALL)).hasSize(4);

    assertThat(branchList(git, ALL).get(0).getName()).isEqualTo("refs/heads/master");
    assertThat(branchList(git, ALL).get(1).getName()).isEqualTo("refs/heads/user_branch");
    assertThat(branchList(git, ALL).get(2).getName()).isEqualTo("refs/remotes/origin/master");
    assertThat(branchList(git, ALL).get(3).getName()).isEqualTo("refs/remotes/origin/user_branch");
}

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

License:Apache License

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

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

    commit(origin, "user_branch", "name", "name@example.com", "commit!", null, null, false,
            new HashMap<String, File>() {
                {/*  w  w  w. java  2 s  .c  om*/
                    put("path/to/file2.txt", tempFile("temp2222"));
                }
            });
    commit(origin, "user_branch", "name", "name@example.com", "commit!", null, null, false,
            new HashMap<String, File>() {
                {
                    put("path/to/file3.txt", tempFile("temp2222"));
                }
            });

    final File gitClonedFolder = new File(parentFolder, "myclone.git");

    final Git git = cloneRepository(gitClonedFolder, origin.getRepository().getDirectory().toString(), true,
            CredentialsProvider.getDefault());

    assertThat(JGitUtil.checkPath(git, "user_branch", "pathx/").getK1()).isEqualTo(NOT_FOUND);
    assertThat(JGitUtil.checkPath(git, "user_branch", "path/to/file2.txt").getK1()).isEqualTo(FILE);
    assertThat(JGitUtil.checkPath(git, "user_branch", "path/to").getK1()).isEqualTo(DIRECTORY);
}

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

License:Apache License

@Test
public void testAmend() throws IOException {
    final File parentFolder = createTempDirectory();
    System.out.println("COOL!:" + parentFolder.toString());
    final File gitFolder = new File(parentFolder, "myxxxtest.git");

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

    commit(origin, "master", "name", "name@example.com", "commit!", null, null, false,
            new HashMap<String, File>() {
                {//  www.j  a  v a2  s  .  c om
                    put("path/to/file2.txt", tempFile("tempwdf sdf asdf asd2222"));
                }
            });
    commit(origin, "master", "name", "name@example.com", "commit!", null, null, true,
            new HashMap<String, File>() {
                {
                    put("path/to/file3.txt", tempFile("temp2x d dasdf asdf 222"));
                }
            });

    final File gitClonedFolder = new File(parentFolder, "myclone.git");

    final Git git = cloneRepository(gitClonedFolder, origin.getRepository().getDirectory().toString(), true,
            CredentialsProvider.getDefault());

    assertThat(JGitUtil.checkPath(git, "master", "pathx/").getK1()).isEqualTo(NOT_FOUND);
    assertThat(JGitUtil.checkPath(git, "master", "path/to/file2.txt").getK1()).isEqualTo(FILE);
    assertThat(JGitUtil.checkPath(git, "master", "path/to").getK1()).isEqualTo(DIRECTORY);
}

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

License:Apache License

public static InputStream resolveInputStream(final Git git, final String treeRef, final String path) {
    checkNotNull("git", git);
    checkNotEmpty("treeRef", treeRef);
    checkNotEmpty("path", path);

    final String gitPath = fixPath(path);

    RevWalk rw = null;/*from w ww .  j  a  v a  2s . c  o  m*/
    TreeWalk tw = null;
    try {
        final ObjectId tree = git.getRepository().resolve(treeRef + "^{tree}");
        rw = new RevWalk(git.getRepository());
        tw = new TreeWalk(git.getRepository());
        tw.setFilter(createFromStrings(singleton(gitPath)));
        tw.reset(tree);
        while (tw.next()) {
            if (tw.isSubtree() && !gitPath.equals(tw.getPathString())) {
                tw.enterSubtree();
                continue;
            }
            return new ByteArrayInputStream(
                    git.getRepository().open(tw.getObjectId(0), Constants.OBJ_BLOB).getBytes());
        }
    } catch (final Throwable t) {
        throw new NoSuchFileException("Can't find '" + gitPath + "' in tree '" + treeRef + "'");
    } finally {
        if (rw != null) {
            rw.dispose();
        }
        if (tw != null) {
            tw.release();
        }
    }
    throw new NoSuchFileException("");
}

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

License:Apache License

public static Git cloneRepository(final File repoFolder, final String fromURI, final boolean bare,
        final CredentialsProvider credentialsProvider) {

    if (!repoFolder.getName().endsWith(DOT_GIT_EXT)) {
        throw new RuntimeException("Invalid name");
    }/*ww  w  .  ja  v a 2 s.c  o  m*/

    try {
        final File gitDir = RepositoryCache.FileKey.resolve(repoFolder, DETECTED);
        final Repository repository;
        final Git git;
        if (gitDir != null && gitDir.exists()) {
            repository = new FileRepository(gitDir);
            git = new Git(repository);
        } else {
            git = Git.cloneRepository().setBare(bare).setCloneAllBranches(true).setURI(fromURI)
                    .setDirectory(repoFolder).setCredentialsProvider(credentialsProvider).call();
            repository = git.getRepository();
        }

        fetchRepository(git, credentialsProvider);

        repository.close();

        return git;
    } catch (final Exception ex) {
        try {
            forceDelete(repoFolder);
        } catch (final java.io.IOException e) {
            throw new RuntimeException(e);
        }
        throw new RuntimeException(ex);
    }
}

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

License:Apache License

public static void pushRepository(final Git git, final CredentialsProvider credentialsProvider,
        final String origin, boolean force) throws InvalidRemoteException {

    if (origin != null && !origin.isEmpty()) {

        try {/*from   www .j  ava 2s. com*/
            final StoredConfig config = git.getRepository().getConfig();
            config.setString("remote", "upstream", "url", origin);
            config.save();
        } catch (final Exception ex) {
            throw new RuntimeException(ex);
        }

        final List<RefSpec> specs = new ArrayList<RefSpec>();
        specs.add(new RefSpec("+refs/heads/*:refs/remotes/upstream/*"));
        specs.add(new RefSpec("+refs/tags/*:refs/tags/*"));
        specs.add(new RefSpec("+refs/notes/*:refs/notes/*"));

        try {
            git.push().setCredentialsProvider(credentialsProvider).setRefSpecs(specs).setRemote(origin)
                    .setForce(force).setPushAll().call();

        } catch (final InvalidRemoteException e) {
            throw e;
        } catch (final Exception ex) {
            throw new RuntimeException(ex);
        }
    }
}