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.craftercms.commons.git.impl.GitRepositoryImplTest.java

License:Open Source License

@Test
public void testCommit() throws Exception {
    Git git = Git.init().setDirectory(tmpDir.getRoot()).call();
    GitRepositoryImpl repository = new GitRepositoryImpl(git);

    File testFile = new File(tmpDir.getRoot(), "test");
    testFile.createNewFile();/*from   ww w .ja  v a  2 s.co  m*/

    git.add().addFilepattern("test").call();

    repository.commit("Test message");

    List<RevCommit> commits = IterableUtils.toList(git.log().all().call());

    assertNotNull(commits);
    assertEquals(1, commits.size());
    assertEquals("Test message", commits.get(0).getFullMessage());
}

From source file:org.craftercms.commons.git.impl.GitRepositoryImplTest.java

License:Open Source License

@Test
public void testPush() throws Exception {
    File masterRepoDir = tmpDir.newFolder("master.git");
    File cloneRepoDir = tmpDir.newFolder("clone");

    Git masterGit = Git.init().setDirectory(masterRepoDir).setBare(true).call();
    Git cloneGit = Git.cloneRepository().setURI(masterRepoDir.getCanonicalPath()).setDirectory(cloneRepoDir)
            .call();//from w  ww.j av a  2  s .  c  om
    GitRepositoryImpl cloneRepo = new GitRepositoryImpl(cloneGit);

    File testFile = new File(cloneRepoDir, "test");
    testFile.createNewFile();

    cloneGit.add().addFilepattern("test").call();
    cloneGit.commit().setMessage("Test message").call();

    cloneRepo.push();

    List<RevCommit> commits = IterableUtils.toList(masterGit.log().all().call());

    assertNotNull(commits);
    assertEquals(1, commits.size());
    assertEquals("Test message", commits.get(0).getFullMessage());

    List<String> committedFiles = getCommittedFiles(masterGit.getRepository(), commits.get(0));

    assertNotNull(committedFiles);
    assertEquals(1, committedFiles.size());
    assertEquals("test", committedFiles.get(0));
}

From source file:org.craftercms.commons.git.impl.GitRepositoryImplTest.java

License:Open Source License

@Test
public void testPull() throws Exception {
    File masterRepoDir = tmpDir.newFolder("master");
    File cloneRepoDir = tmpDir.newFolder("clone");

    Git masterGit = Git.init().setDirectory(masterRepoDir).call();
    Git cloneGit = Git.cloneRepository().setURI(masterRepoDir.getCanonicalPath()).setDirectory(cloneRepoDir)
            .call();/*w  w  w  .  ja va  2 s.  c  om*/
    GitRepositoryImpl cloneRepo = new GitRepositoryImpl(cloneGit);

    File testFile = new File(masterRepoDir, "test");
    testFile.createNewFile();

    masterGit.add().addFilepattern("test").call();
    masterGit.commit().setMessage("Test message").call();

    cloneRepo.pull();

    List<RevCommit> commits = IterableUtils.toList(cloneGit.log().all().call());

    assertNotNull(commits);
    assertEquals(1, commits.size());
    assertEquals("Test message", commits.get(0).getFullMessage());

    List<String> committedFiles = getCommittedFiles(cloneGit.getRepository(), commits.get(0));

    assertNotNull(committedFiles);
    assertEquals(1, committedFiles.size());
    assertEquals("test", committedFiles.get(0));
}

From source file:org.debian.dependency.sources.JGitSource.java

License:Apache License

private void openRepo(final File location, final String origin) throws IOException, GitAPIException {
    try {//from ww  w .ja va  2s .c  om
        git = Git.open(location);

        if (git.getRepository().isBare()) {
            throw new IllegalArgumentException("Cannot work with bare git repos, convert to a non-bare repo");
        } else if (git.getRepository().getRef(Constants.HEAD).getObjectId() == null) {
            throw new IllegalArgumentException(
                    "Repository must have " + Constants.HEAD + " setup and pointing to a valid commit");
        }
    } catch (RepositoryNotFoundException e) {
        getLogger().debug("No existing git repository found, creating from " + location);
        git = Git.init().setDirectory(location).setBare(false).call();

        git.add().addFilepattern(".").call();

        // we don't want to track other SCM metadata files
        RmCommand removeAction = git.rm();
        for (String pattern : FileUtils.getDefaultExcludes()) {
            if (pattern.startsWith(".git")) {
                continue;
            }
            removeAction.addFilepattern(pattern);
        }
        removeAction.call();

        git.commit().setMessage(COMMIT_MESSAGE_PREFIX + " Import upstream from " + origin).call();
    }
}

From source file:org.debian.dependency.sources.TestJGitSource.java

License:Apache License

@Before
public void setUp() throws Exception {
    directory = tempFolder.getRoot();//w ww  .java2s . co m
    git = Git.init().setDirectory(directory).call();

    new File(git.getRepository().getWorkTree(), "a").createNewFile();
    git.add().addFilepattern(".").call();

    git.commit().setMessage("Initial commit").call();
}

From source file:org.debian.dependency.sources.TestJGitSource.java

License:Apache License

/** An existing empty git repository is rare, but we should treat it as an existing repository. */
@Test(expected = IllegalArgumentException.class)
public void testInitExistingEmptyGit() throws Exception {
    File emptyDir = tempFolder.newFolder();
    Git.init().setDirectory(emptyDir).call();

    source.initialize(emptyDir, ORIGIN);
}

From source file:org.debian.dependency.sources.TestJGitSource.java

License:Apache License

/** We need a working tree in order to operate on the source. */
@Test(expected = IllegalArgumentException.class)
public void testInitExistingBareGit() throws Exception {
    File bareRepo = tempFolder.newFolder();
    Git.init().setBare(true).setDirectory(bareRepo).call();

    source.initialize(bareRepo, ORIGIN);
}

From source file:org.dstadler.jgit.CreateNewRepository.java

License:Apache License

public static void main(String[] args) throws IOException, IllegalStateException, GitAPIException {
    // prepare a new folder
    File localPath = File.createTempFile("TestGitRepository", "");
    if (!localPath.delete()) {
        throw new IOException("Could not delete temporary file " + localPath);
    }//from w w  w . ja v a 2s.  c o m

    // create the directory
    try (Git git = Git.init().setDirectory(localPath).call()) {
        System.out.println("Having repository: " + git.getRepository().getDirectory());
    }

    FileUtils.deleteDirectory(localPath);
}

From source file:org.dstadler.jgit.unfinished.TestSubmodules.java

License:Apache License

private static File createRepository() throws IOException, GitAPIException {
    File dir = File.createTempFile("gitinit", ".test");
    if (!dir.delete()) {
        throw new IOException("Could not delete temporary file " + dir);
    }//from   w w  w .  j a  v a  2s.  c o  m

    Git.init().setDirectory(dir).call();

    try (Repository repository = FileRepositoryBuilder.create(new File(dir.getAbsolutePath(), ".git"))) {
        System.out.println("Created a new repository at " + repository.getDirectory());
    }

    return dir;
}

From source file:org.eclipse.emf.compare.git.pgm.AbstractApplicationTest.java

License:Open Source License

@Before
public void before() throws Exception {
    // Creates a local git repository for test purpose
    testTmpFolder = Files.createTempDirectory(TMP_DIRECTORY_PREFIX, new FileAttribute<?>[] {});
    outputStream = new ByteArrayOutputStream();
    errStream = new ByteArrayOutputStream();
    sysout = System.out;/*w  w  w . j ava2s .  c  o m*/
    syserr = System.err;

    // Redirects out and err in order to test outputs.
    System.setOut(new PrintStream(outputStream));
    System.setErr(new PrintStream(errStream));

    app = buildApp();
    setContext(new MockedApplicationContext());

    setRepositoryPath(Files.createTempDirectory(testTmpFolder, REPO_PREFIX, new FileAttribute<?>[] {}));
    setGitFolderPath(new File(getRepositoryPath().toFile(), Constants.DOT_GIT));
    git = Git.init().setDirectory(getRepositoryPath().toFile()).call();
    // Saves the user.dire property to be able to restore it.( some tests can modify it)
    userDir = System.getProperty("user.dir"); //$NON-NLS-1$

}