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.kie.commons.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();
    }/*from  w w  w.  j ava  2s .c om*/

    final Git git;
    final ListBranchCommand.ListMode listMode;
    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);
    } 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);
        listMode = ALL;
    } else {
        credential = buildCredential(null);
        git = newRepository(repoDest, bare);
        listMode = null;
    }

    final JGitFileSystem fs = new JGitFileSystem(this, fullHostName, 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 (DEAMON_ENABLED && deamonService != null && !deamonService.isRunning()) {
        buildAndStartDeamon();
    }

    return fs;
}

From source file:org.kie.commons.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,
            new HashMap<String, File>() {
                {//from  ww  w  . j av 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.kie.commons.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,
            new HashMap<String, File>() {
                {/*from   w w w.j a v  a2s. com*/
                    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.kie.commons.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,
            new HashMap<String, File>() {
                {//from w  ww. j  a  v  a 2s  .  co 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.kie.commons.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, new HashMap<String, File>() {
        {/*w  ww . ja v a2s  . c  om*/
            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);
}

From source file:org.kie.commons.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,
            new HashMap<String, File>() {
                {/*from  ww w .  j a v a2  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,
            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.kie.commons.java.nio.IOException.class);
    } catch (Exception e) {
    }
}

From source file:org.kie.commons.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,
            new HashMap<String, File>() {
                {/*from  ww  w .  ja v  a 2  s. c  om*/
                    put("myfile.txt", tempFile("temp\n.origin\n.content"));
                }
            });
    commit(origin, "user_branch", "user", "user@example.com", "commit message", null, null,
            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.kie.commons.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,
            new HashMap<String, File>() {
                {/* w w  w.j  av a 2  s .  c  o  m*/
                    put("file2.txt", tempFile("temp2222"));
                }
            });
    commit(origin, "master", "name", "name@example.com", "commit", null, null, new HashMap<String, File>() {
        {
            put("file.txt", tempFile("temp"));
        }
    });
    commit(origin, "master", "name", "name@example.com", "commit", null, null, 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.kie.commons.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,
            new HashMap<String, File>() {
                {//from  ww w.jav  a  2  s.c o m
                    put("path/to/file2.txt", tempFile("temp2222"));
                }
            });
    commit(origin, "user_branch", "name", "name@example.com", "commit!", null, null,
            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.kie.commons.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;/*  ww w . j a  v  a 2s . com*/
    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;
            }
            final ObjectId entid = tw.getObjectId(0);
            final FileMode entmode = tw.getFileMode(0);
            final RevObject ro = rw.lookupAny(entid, entmode.getObjectType());
            rw.parseBody(ro);
            final ObjectLoader ldr = git.getRepository().open(ro.getId(), Constants.OBJ_BLOB);
            return ldr.openStream();
        }
    } 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("");
}