Search in sources :

Example 21 with FileBasedConfig

use of org.eclipse.jgit.storage.file.FileBasedConfig in project gerrit by GerritCodeReview.

the class GerritServerConfigModule method getSecureStoreFromGerritConfig.

private static String getSecureStoreFromGerritConfig(final Path sitePath) {
    AbstractModule m = new AbstractModule() {

        @Override
        protected void configure() {
            bind(Path.class).annotatedWith(SitePath.class).toInstance(sitePath);
            bind(SitePaths.class);
        }
    };
    Injector injector = Guice.createInjector(m);
    SitePaths site = injector.getInstance(SitePaths.class);
    FileBasedConfig cfg = new FileBasedConfig(site.gerrit_config.toFile(), FS.DETECTED);
    if (!cfg.getFile().exists()) {
        return DefaultSecureStore.class.getName();
    }
    try {
        cfg.load();
        String className = cfg.getString("gerrit", null, "secureStoreClass");
        return nullToDefault(className);
    } catch (IOException | ConfigInvalidException e) {
        throw new ProvisionException(e.getMessage(), e);
    }
}
Also used : ProvisionException(com.google.inject.ProvisionException) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) Injector(com.google.inject.Injector) IOException(java.io.IOException) FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig) AbstractModule(com.google.inject.AbstractModule)

Example 22 with FileBasedConfig

use of org.eclipse.jgit.storage.file.FileBasedConfig in project gerrit by GerritCodeReview.

the class UpgradeFrom2_0_xTest method upgrade.

@Test
public void upgrade() throws IOException, ConfigInvalidException {
    final Path p = newSitePath();
    final SitePaths site = new SitePaths(p);
    assertTrue(site.isNew);
    FileUtil.mkdirsOrDie(site.etc_dir, "Failed to create");
    for (String n : UpgradeFrom2_0_x.etcFiles) {
        Files.write(p.resolve(n), ("# " + n + "\n").getBytes(UTF_8));
    }
    FileBasedConfig old = new FileBasedConfig(p.resolve("gerrit.config").toFile(), FS.DETECTED);
    old.setString("ldap", null, "username", "ldap.user");
    old.setString("ldap", null, "password", "ldap.s3kr3t");
    old.setString("sendemail", null, "smtpUser", "email.user");
    old.setString("sendemail", null, "smtpPass", "email.s3kr3t");
    old.save();
    final InMemorySecureStore secureStore = new InMemorySecureStore();
    final InitFlags flags = new InitFlags(site, secureStore, Collections.<String>emptyList(), false);
    final ConsoleUI ui = createStrictMock(ConsoleUI.class);
    Section.Factory sections = new Section.Factory() {

        @Override
        public Section get(String name, String subsection) {
            return new Section(flags, site, secureStore, ui, name, subsection);
        }
    };
    expect(ui.yesno(eq(true), eq("Upgrade '%s'"), eq(p.toAbsolutePath().normalize()))).andReturn(true);
    replay(ui);
    UpgradeFrom2_0_x u = new UpgradeFrom2_0_x(site, flags, ui, sections);
    assertTrue(u.isNeedUpgrade());
    u.run();
    assertFalse(u.isNeedUpgrade());
    verify(ui);
    for (String n : UpgradeFrom2_0_x.etcFiles) {
        if ("gerrit.config".equals(n) || "secure.config".equals(n)) {
            continue;
        }
        try (InputStream in = Files.newInputStream(site.etc_dir.resolve(n))) {
            assertEquals("# " + n + "\n", new String(ByteStreams.toByteArray(in), UTF_8));
        }
    }
    FileBasedConfig cfg = new FileBasedConfig(site.gerrit_config.toFile(), FS.DETECTED);
    cfg.load();
    assertEquals("email.user", cfg.getString("sendemail", null, "smtpUser"));
    assertNull(cfg.getString("sendemail", null, "smtpPass"));
    assertEquals("email.s3kr3t", secureStore.get("sendemail", null, "smtpPass"));
    assertEquals("ldap.user", cfg.getString("ldap", null, "username"));
    assertNull(cfg.getString("ldap", null, "password"));
    assertEquals("ldap.s3kr3t", secureStore.get("ldap", null, "password"));
    u.run();
}
Also used : Path(java.nio.file.Path) InitFlags(com.google.gerrit.pgm.init.api.InitFlags) InputStream(java.io.InputStream) SitePaths(com.google.gerrit.server.config.SitePaths) FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig) Section(com.google.gerrit.pgm.init.api.Section) ConsoleUI(com.google.gerrit.pgm.init.api.ConsoleUI) Test(org.junit.Test)

Example 23 with FileBasedConfig

use of org.eclipse.jgit.storage.file.FileBasedConfig in project bndtools by bndtools.

the class GitUtils method getRepository.

public static synchronized Repository getRepository(File gitRoot, String branch, String gitUrl, String gitPushUrl) throws IOException, ConfigInvalidException, JGitInternalException, GitAPIException {
    File dotGit;
    if (gitRoot.getName().equals(Constants.DOT_GIT)) {
        dotGit = gitRoot;
    } else {
        dotGit = new File(gitRoot, Constants.DOT_GIT);
    }
    Repository repository = localRepos.get(dotGit);
    if (repository != null && dotGit.exists()) {
        return repository;
    }
    if (!dotGit.exists()) {
        Git.cloneRepository().setDirectory(gitRoot).setCloneAllBranches(true).setURI(gitUrl).call();
        FileBasedConfig config = new FileBasedConfig(new File(dotGit, "config"), FS.DETECTED);
        config.load();
        if (gitPushUrl != null) {
            config.setString(ConfigConstants.CONFIG_REMOTE_SECTION, "origin", "pushurl", gitPushUrl);
        }
        config.save();
    }
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    repository = builder.setGitDir(dotGit).readEnvironment().findGitDir().build();
    localRepos.put(dotGit, repository);
    try {
        repository.incrementOpen();
        Git git = Git.wrap(repository);
        // Check branch
        boolean pull = true;
        String currentBranch = repository.getBranch();
        if (branch != null && !branch.equals(currentBranch)) {
            CheckoutCommand checkout = git.checkout();
            if (!branchExists(git, branch)) {
                checkout.setCreateBranch(true);
                pull = false;
            }
            checkout.setName(branch);
            checkout.call();
        }
        if (pull) {
            git.pull().call();
        } else {
            git.fetch().call();
        }
    } catch (Exception e) {
        if (!(e.getCause() instanceof TransportException)) {
            throw new RuntimeException(e);
        }
    } finally {
        if (repository != null) {
            repository.close();
        }
    }
    return repository;
}
Also used : Repository(org.eclipse.jgit.lib.Repository) CheckoutCommand(org.eclipse.jgit.api.CheckoutCommand) Git(org.eclipse.jgit.api.Git) FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig) File(java.io.File) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder) TransportException(org.eclipse.jgit.errors.TransportException) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) IOException(java.io.IOException) TransportException(org.eclipse.jgit.errors.TransportException) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException)

Aggregations

FileBasedConfig (org.eclipse.jgit.storage.file.FileBasedConfig)23 IOException (java.io.IOException)12 File (java.io.File)10 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)8 UserModel (com.gitblit.models.UserModel)4 HashMap (java.util.HashMap)4 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ObjectId (org.eclipse.jgit.lib.ObjectId)3 RevCommit (org.eclipse.jgit.revwalk.RevCommit)3 RefModel (com.gitblit.models.RefModel)2 TeamModel (com.gitblit.models.TeamModel)2 X509Metadata (com.gitblit.utils.X509Utils.X509Metadata)2 Date (java.util.Date)2 List (java.util.List)2 Map (java.util.Map)2 TreeSet (java.util.TreeSet)2 IndexWriter (org.apache.lucene.index.IndexWriter)2 Config (org.eclipse.jgit.lib.Config)2