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.kie.wb.test.rest.RestTestBase.java

License:Apache License

@BeforeClass
public static void createGitRepository() throws GitAPIException, IOException {
    gitRepository = new File(System.getProperty("user.dir"), "target/git-repository/");
    Git git = Git.init().setDirectory(gitRepository).call();

    URL pomUrl = RestTestBase.class.getResource("/pom.xml");
    File pomFile = new File(gitRepository, "pom.xml");
    FileUtils.copyURLToFile(pomUrl, pomFile);

    git.add().addFilepattern("pom.xml").call();
    git.commit().setMessage("Add pom.xml").call();
}

From source file:org.kie.workbench.common.screens.examples.backend.server.ExamplesServiceImpl.java

License:Apache License

@PostConstruct
public void initPlaygroundRepository() {
    try {//w  ww .  j  a v  a  2  s.c om
        String userDir = System.getProperty("user.dir");

        File playgroundDirectory = new File(userDir, ".kie-wb-playground");
        if (playgroundDirectory.exists()) {
            cleanPlaygroundDirectory(playgroundDirectory.toPath());
        }
        playgroundDirectory.mkdirs();

        URL resource = getClass().getClassLoader().getResource(KIE_WB_PLAYGROUND_ZIP);
        if (resource == null) {
            logger.warn("Playground repository jar not found on classpath.");
            return;
        }

        try (ZipInputStream inputStream = new ZipInputStream(resource.openStream())) {
            ZipEntry zipEntry = null;
            while ((zipEntry = inputStream.getNextEntry()) != null) {
                byte[] buffer = new byte[1024];
                File file = new File(playgroundDirectory, zipEntry.getName());
                if (zipEntry.isDirectory()) {
                    file.mkdirs();
                } else {
                    try (FileOutputStream fos = new FileOutputStream(file)) {
                        int read = -1;
                        while ((read = inputStream.read(buffer)) != -1) {
                            fos.write(buffer, 0, read);
                        }
                    }
                }
            }

            final Git git = Git.init().setBare(false).setDirectory(playgroundDirectory).call();
            git.add().addFilepattern(".").call();
            git.commit().setMessage("Initial commit").call();

            String repositoryUrl = resolveRepositoryUrl(playgroundDirectory.getAbsolutePath());
            playgroundRepository = new ExampleRepository(repositoryUrl);
        }
    } catch (java.io.IOException | GitAPIException e) {
        logger.error(
                "Unable to initialize playground git repository. Only custom repository definition will be available in the Workbench.",
                e);
    }
}

From source file:org.komodo.rest.service.KomodoImportExportServiceTest.java

License:Open Source License

private void initGitRepository() throws Exception {
    String tmpDirPath = System.getProperty("java.io.tmpdir");
    File tmpDir = new File(tmpDirPath);

    long timestamp = System.currentTimeMillis();
    myGitDir = new File(tmpDir, "mygit-" + timestamp);
    assertTrue(myGitDir.mkdir());/*w  w  w . j  av  a2 s  .  c o m*/

    myGit = Git.init().setDirectory(myGitDir).setBare(true).call();
    assertNotNull(myGit);

    // Need to initialise the master branch
    // as jgit does not automatically create on
    File seedDir = new File(tmpDir, "seedDir-" + timestamp);
    Git seedGit = Git.cloneRepository().setURI(myGitDir.getAbsolutePath()).setDirectory(seedDir).call();

    File tweetVdbFile = new File(seedDir,
            TestUtilities.TWEET_EXAMPLE_NAME + TestUtilities.TWEET_EXAMPLE_SUFFIX);
    assertTrue(tweetVdbFile.createNewFile());
    FileUtils.write(TestUtilities.tweetExample(), tweetVdbFile);
    assertTrue(tweetVdbFile.length() > 0);

    File usStatesZipFile = new File(tmpDir, TestUtilities.US_STATES_DATA_SERVICE_NAME + ZIP_SUFFIX);
    FileUtils.write(TestUtilities.usStatesDataserviceExample(), usStatesZipFile);
    try (FileInputStream fis = new FileInputStream(usStatesZipFile)) {
        File usStatesDir = new File(seedDir, TestUtilities.US_STATES_DATA_SERVICE_NAME);
        usStatesDir.mkdir();
        FileUtils.zipExtract(fis, usStatesDir);
        usStatesZipFile.delete();
    }

    seedGit.add().addFilepattern(DOT).call();
    seedGit.commit().setMessage("First Commit").call();
    seedGit.push().call();

    FileUtils.removeDirectoryAndChildren(seedDir);

    //
    // Local git repository
    //
    String dirName = System.currentTimeMillis() + HYPHEN;
    gitRepoDest = new File(FileUtils.tempDirectory(), dirName);
    gitRepoDest.mkdir();
}

From source file:org.komodo.storage.git.TestGitStorageConnector.java

License:Open Source License

@Before
public void setup() throws Exception {
    String tmpDirPath = System.getProperty("java.io.tmpdir");
    tmpDir = new File(tmpDirPath);

    timestamp = System.currentTimeMillis();
    myGitDir = new File(tmpDir, "mygit-" + timestamp);
    assertTrue(myGitDir.mkdir());/*from ww  w . ja  va2  s  . c  o m*/

    myGit = Git.init().setDirectory(myGitDir).setBare(true).call();
    assertNotNull(myGit);

    //
    // Create a clone to seed the repository with a file
    //
    File seedDir = new File(tmpDir, "seedDir-" + timestamp);
    Git seedGit = Git.cloneRepository().setURI(myGitDir.getAbsolutePath()).setDirectory(seedDir).call();

    // Adds a file into the seed repository
    File vdbFile = new File(seedDir, TEST_VDB_XML);
    assertTrue(vdbFile.createNewFile());
    FileUtils.write(TestUtilities.tweetExample(), vdbFile);
    assertTrue(vdbFile.length() > 0);

    File subDir = new File(seedDir, SUB_DIR);
    assertTrue(subDir.mkdir());

    File subDirVdbFile = new File(subDir, TEST_VDB_3_XML);
    assertTrue(subDirVdbFile.createNewFile());
    FileUtils.write(TestUtilities.tweetExample(), subDirVdbFile);
    assertTrue(subDirVdbFile.length() > 0);

    File usStatesZipFile = new File(tmpDir, TestUtilities.US_STATES_VDB_NAME + ZIP_SUFFIX);
    FileUtils.write(TestUtilities.usStatesDataserviceExample(), usStatesZipFile);
    try (FileInputStream fis = new FileInputStream(usStatesZipFile)) {
        File usStatesDir = new File(seedDir, TestUtilities.US_STATES_VDB_NAME);
        usStatesDir.mkdir();
        FileUtils.zipExtract(fis, usStatesDir);
        usStatesZipFile.delete();
    }

    seedGit.add().addFilepattern(DOT).call();
    seedGit.commit().setMessage("Adds Test Files").call();
    seedGit.push().call();

    FileUtils.removeDirectoryAndChildren(seedDir);
}

From source file:org.moxie.ant.Main.java

License:Apache License

private void initGit() throws GitAPIException {
    // create the repository
    InitCommand init = Git.init();
    init.setBare(false);//from ww w  . j  a v a  2  s .  c o  m
    init.setDirectory(newProject.dir);
    Git git = init.call();

    if (!StringUtils.isEmpty(newProject.gitOrigin)) {
        // set the origin and configure the master branch for merging 
        StoredConfig config = git.getRepository().getConfig();
        config.setString("remote", "origin", "url", getGitUrl());
        config.setString("remote", "origin", "fetch", "+refs/heads/*:refs/remotes/origin/*");
        config.setString("branch", "master", "remote", "origin");
        config.setString("branch", "master", "merge", "refs/heads/master");
        try {
            config.save();
        } catch (IOException e) {
            throw new MoxieException(e);
        }
    }

    // prepare a common gitignore file
    StringBuilder sb = new StringBuilder();
    sb.append("/.directory\n");
    sb.append("/.DS_STORE\n");
    sb.append("/.DS_Store\n");
    sb.append("/.settings\n");
    sb.append("/bin\n");
    sb.append("/build\n");
    sb.append("/ext\n");
    sb.append("/target\n");
    sb.append("/tmp\n");
    sb.append("/temp\n");
    if (!newProject.eclipse.includeClasspath()) {
        // ignore hard-coded .classpath
        sb.append("/.classpath\n");
    }
    FileUtils.writeContent(new File(newProject.dir, ".gitignore"), sb.toString());

    AddCommand add = git.add();
    add.addFilepattern("build.xml");
    add.addFilepattern("build.moxie");
    add.addFilepattern(".gitignore");
    if (newProject.eclipse.includeProject()) {
        add.addFilepattern(".project");
    }
    if (newProject.eclipse.includeClasspath()) {
        // MOXIE_HOME relative dependencies in .classpath
        add.addFilepattern(".classpath");
    }
    if (newProject.idea.includeProject()) {
        add.addFilepattern(".project");
    }
    if (newProject.idea.includeClasspath()) {
        // MOXIE_HOME relative dependencies in .iml
        add.addFilepattern("*.iml");
    }
    try {
        add.call();
        CommitCommand commit = git.commit();
        PersonIdent moxie = new PersonIdent("Moxie", "moxie@localhost");
        commit.setAuthor(moxie);
        commit.setCommitter(moxie);
        commit.setMessage("Project structure created");
        commit.call();
    } catch (Exception e) {
        throw new MoxieException(e);
    }
}

From source file:org.ms123.common.git.GitServiceImpl.java

License:Open Source License

@RequiresRoles("admin")
public void createRepository(@PName("name") String name) throws RpcException {
    try {//from   ww w  .j av a2  s  .  com
        String gitSpace = System.getProperty("git.repos");
        File dir = new File(gitSpace, name);
        if (dir.exists()) {
            throw new RpcException(ERROR_FROM_METHOD, 100,
                    "GitService.createRepository:Repo(" + name + ") exists");
        }
        InitCommand ic = Git.init();
        ic.setDirectory(dir);
        ic.call();
        FS fs = FS.detect();
        FileBasedConfig fbc = new FileBasedConfig(new File(gitSpace + "/" + name + "/.git/config"), fs);
        fbc.load();
        SimpleDateFormat sdf = new SimpleDateFormat(m_datePattern);
        sdf.setTimeZone(TimeZone.getTimeZone("Europe/Berlin"));
        String created = sdf.format(new Date());
        fbc.setString("sw", null, "created", created);
        fbc.save();
        if (!isDataRepo(name)) {
            createStoreFile(gitSpace, name);
        }
        return;
    } catch (Exception e) {
        throw new RpcException(ERROR_FROM_METHOD, INTERNAL_SERVER_ERROR, "GitService.createRepository:", e);
    } finally {
    }
}

From source file:org.n52.wps.repository.git.GitAlgorithmRepositoryTest.java

License:Open Source License

private Path initRepository() throws GitAPIException, IOException {
    File repository = testRoot.newFolder("repository");
    Git.init().setDirectory(repository).call();
    return repository.toPath();
}

From source file:org.nmrfx.project.GUIProject.java

public void createProject(Path projectDir) throws IOException {
    try {/*from   ww w.j  av  a2s.  c o m*/
        super.createProject(projectDir);
        PreferencesController.saveRecentProjects(projectDir.toString());
        git = Git.init().setDirectory(projectDir.toFile()).call();
    } catch (GitAPIException ex) {
        Logger.getLogger(GUIProject.class.getName()).log(Level.SEVERE, null, ex);
    }
    if (git != null) {
        writeIgnore();
    }
}

From source file:org.nmrfx.project.GUIProject.java

boolean gitCommit() {
    boolean didSomething = false;
    commitActive = true;//from w  w w  .ja  va2 s  .c o m
    try {
        if (git == null) {
            try {
                git = Git.open(projectDir.toFile());
            } catch (IOException ioE) {
                git = Git.init().setDirectory(projectDir.toFile()).call();
                writeIgnore();
                System.out.println("gitinited");
            }
        }

        DirCache index = git.add().addFilepattern(".").call();
        Status status = git.status().call();
        System.out.println("status " + status.isClean() + " " + status.hasUncommittedChanges());
        StringBuilder sBuilder = new StringBuilder();
        Set<String> actionMap = new HashSet<>();
        if (!status.isClean() || status.hasUncommittedChanges()) {
            Set<String> addedFiles = status.getAdded();
            for (String addedFile : addedFiles) {
                String action = "add:" + Paths.get(addedFile).getName(0);
                actionMap.add(action);
                System.out.println("added " + addedFile);
            }
            Set<String> changedFiles = status.getChanged();
            for (String changedFile : changedFiles) {
                String action = "change:" + Paths.get(changedFile).getName(0);
                actionMap.add(action);
                System.out.println("changed " + changedFile);
            }
            Set<String> removedFiles = status.getRemoved();
            for (String removedFile : removedFiles) {
                System.out.println("removed " + removedFile);
                String action = "remove:" + Paths.get(removedFile).getName(0);
                actionMap.add(action);
                git.rm().addFilepattern(removedFile).call();
            }
            Set<String> missingFiles = status.getMissing();
            for (String missingFile : missingFiles) {
                System.out.println("missing " + missingFile);
                String action = "missing:" + Paths.get(missingFile).getName(0);
                actionMap.add(action);
                git.rm().addFilepattern(missingFile).call();
            }
            actionMap.stream().forEach(action -> sBuilder.append(action).append(","));
            RevCommit commit = git.commit().setMessage(sBuilder.toString()).call();
            didSomething = true;
        }
    } catch (GitAPIException ex) {
        Logger.getLogger(GUIProject.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        // fixme, should we do this after each commit, or leave git open
        git.close();
        git = null;
        commitActive = false;
    }
    return didSomething;
}

From source file:org.ocpsoft.redoculous.tests.asciidoc.AsciidocIncludeTest.java

License:Apache License

@Before
public void before() throws IOException, GitAPIException {
    repository = File.createTempFile("redoc", "ulous-test");
    repository.delete();//from w  ww. j ava 2s. c o  m
    repository.mkdirs();
    File document = new File(repository, "document.asciidoc");
    document.createNewFile();
    File master = new File(repository, "master.asciidoc");
    master.createNewFile();

    Files.write(document, getClass().getClassLoader().getResourceAsStream("asciidoc/toc.asciidoc"));
    Files.write(master, "Somethingn, then...\n" + "include::document.asciidoc[]\n" + ASDF123);

    Git repo = Git.init().setDirectory(repository).call();
    repo.add().addFilepattern("document.asciidoc").call();
    repo.add().addFilepattern("master.asciidoc").call();
    repo.commit().setMessage("Initial commit.").call();
}