Example usage for org.eclipse.jgit.storage.file FileRepositoryBuilder FileRepositoryBuilder

List of usage examples for org.eclipse.jgit.storage.file FileRepositoryBuilder FileRepositoryBuilder

Introduction

In this page you can find the example usage for org.eclipse.jgit.storage.file FileRepositoryBuilder FileRepositoryBuilder.

Prototype

FileRepositoryBuilder

Source Link

Usage

From source file:org.eclipse.winery.repository.backend.filebased.GitBasedRepository.java

License:Open Source License

/**
 * @param gitBasedRepositoryConfiguration the configuration of the repository
 * @throws IOException         thrown if repository does not exist
 * @throws GitAPIException     thrown if there was an error while checking the status of the repository
 * @throws NoWorkTreeException thrown if the directory is not a git work tree
 *//*from  ww  w . j  ava2  s.c  om*/
public GitBasedRepository(GitBasedRepositoryConfiguration gitBasedRepositoryConfiguration)
        throws IOException, NoWorkTreeException, GitAPIException {
    super(Objects.requireNonNull(gitBasedRepositoryConfiguration));
    this.gitBasedRepositoryConfiguration = gitBasedRepositoryConfiguration;

    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    Repository gitRepo = builder.setWorkTree(this.repositoryRoot.toFile()).setMustExist(false).build();

    String repoUrl = gitBasedRepositoryConfiguration.getRepositoryUrl();
    String branch = gitBasedRepositoryConfiguration.getBranch();

    if (!Files.exists(this.repositoryRoot.resolve(".git"))) {
        if (repoUrl != null && !repoUrl.isEmpty()) {
            this.git = cloneRepository(repoUrl, branch);
        } else {
            gitRepo.create();
            this.git = new Git(gitRepo);
        }
    } else {
        this.git = new Git(gitRepo);
    }

    if (this.repositoryRoot.resolve(Constants.DEFAULT_LOCAL_REPO_NAME).toFile().exists()) {
        if (!(this instanceof MultiRepository)) {
            this.workingRepositoryRoot = this.repositoryRoot.resolve(Constants.DEFAULT_LOCAL_REPO_NAME);
        } else {
            this.workingRepositoryRoot = repositoryDep;
        }
    } else {
        this.workingRepositoryRoot = repositoryDep;
    }

    this.eventBus = new EventBus();

    // explicitly enable longpaths to ensure proper handling of long pathss
    gitRepo.getConfig().setBoolean("core", null, "longpaths", true);
    gitRepo.getConfig().save();

    if (gitBasedRepositoryConfiguration.isAutoCommit() && !this.git.status().call().isClean()) {
        this.addCommit("Files changed externally.");
    }
}

From source file:org.eclipse.winery.repository.PrefsTestEnabledGitBackedRepository.java

License:Open Source License

public PrefsTestEnabledGitBackedRepository() throws Exception {
    super(false);

    Path repositoryPath = Paths.get(System.getProperty("java.io.tmpdir")).resolve("test-repository");
    if (!Files.exists(repositoryPath)) {
        Files.createDirectory(repositoryPath);
    }//w ww.jav  a2s .  c o  m

    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    if (!Files.exists(repositoryPath.resolve(".git"))) {
        this.git = Git.cloneRepository().setURI("https://github.com/winery/test-repository.git").setBare(false)
                .setCloneAllBranches(true).setDirectory(repositoryPath.toFile()).call();
    } else {
        Repository gitRepo = builder.setWorkTree(repositoryPath.toFile()).setMustExist(false).build();
        this.git = new Git(gitRepo);
        try {
            this.git.fetch().call();
        } catch (TransportException e) {
            // we ignore it to enable offline testing
            LOGGER.debug("Working in offline mode", e);
        }
    }

    this.repository = new GitBasedRepository(repositoryPath.toString());
}

From source file:org.eclipse.winery.repository.TestWithGitBackedRepository.java

License:Open Source License

/**
 * Initializes the git repository from https://github.com/winery/test-repository into %TEMP%/test-repository
 *
 * @throws RuntimeException wraps an Exception
 *//*from  w  ww . j  a v a 2  s .  c  om*/
public TestWithGitBackedRepository() {
    try {
        Path repositoryPath = Paths.get(System.getProperty("java.io.tmpdir")).resolve("test-repository");
        LOGGER.debug("Testing with repository directory {}", repositoryPath);

        if (!Files.exists(repositoryPath)) {
            Files.createDirectory(repositoryPath);
        }

        FileRepositoryBuilder builder = new FileRepositoryBuilder();
        if (!Files.exists(repositoryPath.resolve(".git"))) {
            this.git = Git.cloneRepository().setURI("https://github.com/winery/test-repository.git")
                    .setBare(false).setCloneAllBranches(true).setDirectory(repositoryPath.toFile()).call();
        } else {
            Repository gitRepo = builder.setWorkTree(repositoryPath.toFile()).setMustExist(false).build();
            this.git = new Git(gitRepo);
            try {
                this.git.fetch().call();
            } catch (TransportException e) {
                // we ignore it to enable offline testing
                LOGGER.debug("Working in offline mode", e);
            }
        }

        // inject the current path to the repository factory
        FileBasedRepositoryConfiguration fileBasedRepositoryConfiguration = new FileBasedRepositoryConfiguration(
                repositoryPath);
        GitBasedRepositoryConfiguration gitBasedRepositoryConfiguration = new GitBasedRepositoryConfiguration(
                false, fileBasedRepositoryConfiguration);
        RepositoryFactory.reconfigure(gitBasedRepositoryConfiguration);

        this.repository = RepositoryFactory.getRepository();
        LOGGER.debug("Initialized test repository");
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.gitcontrib.dataset.GitRepository.java

License:Apache License

protected Repository open() throws Exception {
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    Repository repository = builder.setGitDir(new File(directory, ".git")).build();

    return repository;
}

From source file:org.jabylon.team.git.GitTeamProvider.java

License:Open Source License

private Repository createRepository(ProjectVersion project) throws IOException {
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    File gitDir = new File(project.absoluteFilePath().path());
    Repository repository = builder.setGitDir(new File(gitDir, ".git")).build();
    return repository;
}

From source file:org.jboss.as.controller.persistence.GitPersistenceResourceTestCase.java

License:Apache License

@Before
public void createDirectoriesAndFiles() throws Exception {
    root = new File("target", "standalone").toPath();
    Files.createDirectories(root);
    File baseDir = root.toAbsolutePath().toFile();
    File gitDir = new File(baseDir, Constants.DOT_GIT);
    if (!gitDir.exists()) {
        try (Git git = Git.init().setDirectory(baseDir).setGitDir(gitDir).call()) {
            git.commit().setMessage("Repository initialized").call();
        }//  w ww.  jav a2s  . c om
    }
    repository = new FileRepositoryBuilder().setWorkTree(baseDir).setGitDir(gitDir).setup().build();
}

From source file:org.jboss.as.controller.persistence.RemoteGitPersistenceResourceTestCase.java

License:Apache License

@Before
public void createDirectoriesAndFiles() throws Exception {
    root = new File("target", "standalone").toPath();
    remoteRoot = new File("target", "remote").toPath().resolve("standalone");
    Files.createDirectories(remoteRoot);
    File baseDir = remoteRoot.toAbsolutePath().toFile();
    createFile(remoteRoot, "standard.xml", "std");
    File gitDir = new File(baseDir, Constants.DOT_GIT);
    if (!gitDir.exists()) {
        try (Git git = Git.init().setDirectory(baseDir).call()) {
            git.add().addFilepattern("standard.xml").call();
            git.commit().setMessage("Repository initialized").call();
        }//from www  . j  a  v  a 2  s  .co m
    }
    remoteRepository = new FileRepositoryBuilder().setWorkTree(baseDir).setGitDir(gitDir).setup().build();
    repository = new FileRepositoryBuilder().setWorkTree(root.toAbsolutePath().toFile())
            .setGitDir(new File(root.toAbsolutePath().toFile(), Constants.DOT_GIT)).setup().build();
}

From source file:org.jboss.as.server.controller.git.GitRepository.java

License:Apache License

public GitRepository(GitRepositoryConfiguration gitConfig)
        throws IllegalArgumentException, IOException, ConfigXMLParseException, GeneralSecurityException {
    this.basePath = gitConfig.getBasePath();
    this.branch = gitConfig.getBranch();
    this.ignored = gitConfig.getIgnored();
    this.defaultRemoteRepository = gitConfig.getRepository();
    File baseDir = basePath.toFile();
    File gitDir = new File(baseDir, DOT_GIT);
    if (gitConfig.getAuthenticationConfig() != null) {
        CredentialsProvider//w  w  w .j a  v  a2  s  .  com
                .setDefault(new ElytronClientCredentialsProvider(gitConfig.getAuthenticationConfig()));
    }
    if (gitDir.exists()) {
        try {
            repository = new FileRepositoryBuilder().setWorkTree(baseDir).setGitDir(gitDir).setup().build();
        } catch (IOException ex) {
            throw ServerLogger.ROOT_LOGGER.failedToPullRepository(ex, gitConfig.getRepository());
        }
        try (Git git = Git.wrap(repository)) {
            git.clean();
            if (!isLocalGitRepository(gitConfig.getRepository())) {
                String remote = getRemoteName(gitConfig.getRepository());
                checkoutToSelectedBranch(git);
                PullResult result = git.pull().setRemote(remote).setRemoteBranchName(branch)
                        .setStrategy(MergeStrategy.RESOLVE).call();
                if (!result.isSuccessful()) {
                    throw ServerLogger.ROOT_LOGGER.failedToPullRepository(null, gitConfig.getRepository());
                }
            } else {
                if (!this.branch.equals(repository.getBranch())) {
                    CheckoutCommand checkout = git.checkout().setName(branch);
                    checkout.call();
                    if (checkout.getResult().getStatus() == CheckoutResult.Status.ERROR) {
                        throw ServerLogger.ROOT_LOGGER.failedToPullRepository(null, gitConfig.getRepository());
                    }
                }
            }
        } catch (GitAPIException ex) {
            throw ServerLogger.ROOT_LOGGER.failedToPullRepository(ex, gitConfig.getRepository());
        }
    } else {
        if (isLocalGitRepository(gitConfig.getRepository())) {
            try (Git git = Git.init().setDirectory(baseDir).call()) {
                final AddCommand addCommand = git.add();
                addCommand.addFilepattern("data/content/");
                Path configurationDir = basePath.resolve("configuration");
                try (Stream<Path> files = Files.list(configurationDir)) {
                    files.filter(configFile -> !"logging.properties".equals(configFile.getFileName().toString())
                            && Files.isRegularFile(configFile))
                            .forEach(configFile -> addCommand.addFilepattern(getPattern(configFile)));
                }
                addCommand.call();
                createGitIgnore(git, basePath);
                git.commit().setMessage(ServerLogger.ROOT_LOGGER.repositoryInitialized()).call();
            } catch (GitAPIException | IOException ex) {
                throw ServerLogger.ROOT_LOGGER.failedToInitRepository(ex, gitConfig.getRepository());
            }
        } else {
            clearExistingFiles(basePath, gitConfig.getRepository());
            try (Git git = Git.init().setDirectory(baseDir).call()) {
                String remoteName = UUID.randomUUID().toString();
                StoredConfig config = git.getRepository().getConfig();
                config.setString("remote", remoteName, "url", gitConfig.getRepository());
                config.setString("remote", remoteName, "fetch",
                        "+" + R_HEADS + "*:" + R_REMOTES + remoteName + "/*");
                config.save();
                git.clean();
                git.pull().setRemote(remoteName).setRemoteBranchName(branch).setStrategy(MergeStrategy.RESOLVE)
                        .call();
                checkoutToSelectedBranch(git);
                if (createGitIgnore(git, basePath)) {
                    git.commit().setMessage(ServerLogger.ROOT_LOGGER.addingIgnored()).call();
                }
            } catch (GitAPIException ex) {
                throw ServerLogger.ROOT_LOGGER.failedToInitRepository(ex, gitConfig.getRepository());
            }
        }
        repository = new FileRepositoryBuilder().setWorkTree(baseDir).setGitDir(gitDir).setup().build();
    }
    ServerLogger.ROOT_LOGGER.usingGit();
}

From source file:org.jboss.as.test.manualmode.management.persistence.AbstractGitRepositoryTestCase.java

License:Apache License

@Before
public void prepareEmptyRemoteRepository() throws Exception {
    emptyRemoteRoot = new File("target", "empty-remote").toPath();
    Files.createDirectories(emptyRemoteRoot);
    File gitDir = new File(emptyRemoteRoot.toFile(), Constants.DOT_GIT);
    if (!gitDir.exists()) {
        try (Git git = Git.init().setDirectory(emptyRemoteRoot.toFile()).call()) {
        }/*from  w ww  .j  a  v a  2  s .  c  o  m*/
    }
    Assert.assertTrue(gitDir.exists());
    emptyRemoteRepository = new FileRepositoryBuilder().setWorkTree(emptyRemoteRoot.toFile()).setGitDir(gitDir)
            .setup().build();
}

From source file:org.jboss.as.test.manualmode.management.persistence.AbstractGitRepositoryTestCase.java

License:Apache License

protected Repository createRepository() throws IOException {
    Repository repo = new FileRepositoryBuilder().setWorkTree(getJbossServerBaseDir().toFile())
            .setGitDir(getDotGitDir().toFile()).setup().build();
    StoredConfig config = repo.getConfig();
    config.setString("remote", "empty", "url",
            "file://" + emptyRemoteRoot.resolve(Constants.DOT_GIT).toAbsolutePath().toString());
    config.save();//w w w .  j av  a 2 s.c o  m
    return repo;
}