Search in sources :

Example 1 with ConfigRepository

use of com.peterphi.configuration.service.git.ConfigRepository in project stdlib by petergeneric.

the class ConfigGuiceModule method getRepository.

@Provides
@Singleton
@Named("config")
public ConfigRepository getRepository(@Named("repo.config.path") final File workingDirectory, @Named("repo.config.force-reclone-on-startup") final boolean reclone, @Named("repo.config.remote") final String remote) throws IOException, URISyntaxException, GitAPIException {
    log.info("Repo path " + workingDirectory + ", reclone: " + reclone);
    if (reclone && workingDirectory.exists()) {
        log.info("Recloning " + workingDirectory);
        File[] files = workingDirectory.listFiles();
        if (files != null)
            for (File file : files) {
                FileUtils.deleteQuietly(file);
                if (!file.exists()) {
                    throw new RuntimeException("Tried to delete local checkout contents but did not succeed. File was: " + file);
                }
            }
    }
    final File gitDir = new File(workingDirectory, ".git");
    final boolean newlyCreated;
    // Create the git repository if it doesn't already exist
    if (!gitDir.exists()) {
        log.info("Initialising new git dir at: " + workingDirectory);
        FileUtils.forceMkdir(workingDirectory);
        InitCommand init = new InitCommand();
        init.setBare(false).setDirectory(workingDirectory).setGitDir(gitDir).call();
        newlyCreated = true;
    } else {
        newlyCreated = false;
    }
    FileRepositoryBuilder frb = new FileRepositoryBuilder();
    Repository repo = frb.setGitDir(gitDir).readEnvironment().findGitDir().build();
    final boolean hasRemote = !remote.equalsIgnoreCase("none");
    final CredentialsProvider credentials;
    if (hasRemote) {
        // Try to extract username/password from the remote URL
        final URIish uri = new URIish(remote);
        if (uri.getUser() != null && uri.getPass() != null)
            credentials = new UsernamePasswordCredentialsProvider(uri.getUser(), uri.getPass());
        else
            credentials = null;
    } else {
        credentials = null;
    }
    if (newlyCreated) {
        // Add the remote and pull from it
        if (hasRemote) {
            RepoHelper.addRemote(repo, "origin", remote);
            RepoHelper.pull(repo, "origin", credentials);
        }
        Git git = new Git(repo);
        // If there are no commits in this repository, create one
        if (!git.log().setMaxCount(1).call().iterator().hasNext()) {
            git.commit().setAll(true).setAuthor("system", "system@localhost").setMessage("initial commit").call();
            if (hasRemote)
                RepoHelper.push(repo, "origin", credentials);
        }
    }
    return new ConfigRepository(repo, hasRemote, credentials);
}
Also used : URIish(org.eclipse.jgit.transport.URIish) ConfigRepository(com.peterphi.configuration.service.git.ConfigRepository) Repository(org.eclipse.jgit.lib.Repository) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) Git(org.eclipse.jgit.api.Git) InitCommand(org.eclipse.jgit.api.InitCommand) ConfigRepository(com.peterphi.configuration.service.git.ConfigRepository) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) CredentialsProvider(org.eclipse.jgit.transport.CredentialsProvider) File(java.io.File) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) Named(com.google.inject.name.Named) Singleton(com.google.inject.Singleton) Provides(com.google.inject.Provides)

Aggregations

Provides (com.google.inject.Provides)1 Singleton (com.google.inject.Singleton)1 Named (com.google.inject.name.Named)1 ConfigRepository (com.peterphi.configuration.service.git.ConfigRepository)1 File (java.io.File)1 Git (org.eclipse.jgit.api.Git)1 InitCommand (org.eclipse.jgit.api.InitCommand)1 Repository (org.eclipse.jgit.lib.Repository)1 FileRepositoryBuilder (org.eclipse.jgit.storage.file.FileRepositoryBuilder)1 CredentialsProvider (org.eclipse.jgit.transport.CredentialsProvider)1 URIish (org.eclipse.jgit.transport.URIish)1 UsernamePasswordCredentialsProvider (org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider)1