Search in sources :

Example 41 with FileBasedConfig

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

the class RepositoryManager method getRepositoryModel.

/**
 * Returns the repository model for the specified repository. This method
 * does not consider user access permissions.
 *
 * @param name
 * @return repository model or null
 */
@Override
public RepositoryModel getRepositoryModel(String name) {
    String repositoryName = fixRepositoryName(name);
    String repositoryKey = getRepositoryKey(repositoryName);
    if (!repositoryListCache.containsKey(repositoryKey)) {
        RepositoryModel model = loadRepositoryModel(repositoryName);
        if (model == null) {
            return null;
        }
        addToCachedRepositoryList(model);
        return DeepCopier.copy(model);
    }
    // cached model
    RepositoryModel model = repositoryListCache.get(repositoryKey);
    if (isCollectingGarbage(model.name)) {
        // Gitblit is busy collecting garbage, use our cached model
        RepositoryModel rm = DeepCopier.copy(model);
        rm.isCollectingGarbage = true;
        return rm;
    }
    // check for updates
    Repository r = getRepository(model.name);
    if (r == null) {
        // repository is missing
        removeFromCachedRepositoryList(repositoryName);
        logger.error(MessageFormat.format("Repository \"{0}\" is missing! Removing from cache.", repositoryName));
        return null;
    }
    FileBasedConfig config = (FileBasedConfig) getRepositoryConfig(r);
    if (config.isOutdated()) {
        // reload model
        logger.debug(MessageFormat.format("Config for \"{0}\" has changed. Reloading model and updating cache.", repositoryName));
        model = loadRepositoryModel(model.name);
        removeFromCachedRepositoryList(model.name);
        addToCachedRepositoryList(model);
    } else {
        // update a few repository parameters
        if (!model.hasCommits) {
            // update hasCommits, assume a repository only gains commits :)
            model.hasCommits = JGitUtils.hasCommits(r);
        }
        updateLastChangeFields(r, model);
    }
    r.close();
    // return a copy of the cached model
    return DeepCopier.copy(model);
}
Also used : Repository(org.eclipse.jgit.lib.Repository) RepositoryModel(com.gitblit.models.RepositoryModel) FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig)

Example 42 with FileBasedConfig

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

the class ProjectCacheIT method allProjectsProjectsConfig_ChangeInFileInvalidatesPersistedCache.

@Test
public void allProjectsProjectsConfig_ChangeInFileInvalidatesPersistedCache() throws Exception {
    assertThat(projectCache.getAllProjects().getConfig().getCheckReceivedObjects()).isTrue();
    // Change etc/All-Projects-project.config
    FileBasedConfig fileBasedConfig = new FileBasedConfig(sitePaths.etc_dir.resolve(allProjects.get()).resolve(ProjectConfig.PROJECT_CONFIG).toFile(), FS.DETECTED);
    fileBasedConfig.setString("receive", null, "checkReceivedObjects", "false");
    fileBasedConfig.save();
    // Invalidate only the in-memory cache
    inMemoryProjectCache.invalidate(allProjects);
    assertThat(projectCache.getAllProjects().getConfig().getCheckReceivedObjects()).isFalse();
}
Also used : FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig) Test(org.junit.Test) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest)

Example 43 with FileBasedConfig

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

the class AllProjectsConfigTest method setUp.

@Before
public void setUp() throws Exception {
    sitePaths = new SitePaths(temporaryFolder.newFolder().toPath());
    Files.createDirectories(sitePaths.etc_dir);
    Path gitPath = sitePaths.resolve("git");
    StoredConfig gerritConfig = new FileBasedConfig(sitePaths.resolve("etc").resolve("gerrit.config").toFile(), FS.DETECTED);
    gerritConfig.load();
    gerritConfig.setString("gerrit", null, "basePath", gitPath.toAbsolutePath().toString());
    gerritConfig.setString("gerrit", null, "allProjects", ALL_PROJECTS);
    gerritConfig.save();
    Files.createDirectories(sitePaths.resolve("git"));
    allProjectsRepoFile = gitPath.resolve("All-The-Projects.git").toFile();
    try (Repository repo = new FileRepository(allProjectsRepoFile)) {
        repo.create(true);
    }
    InMemorySecureStore secureStore = new InMemorySecureStore();
    InitFlags flags = new InitFlags(sitePaths, secureStore, ImmutableList.of(), false);
    Section.Factory sections = (name, subsection) -> new Section(flags, sitePaths, secureStore, ui, name, subsection);
    AllProjectsConfigProvider configProvider = new FileBasedAllProjectsConfigProvider(sitePaths);
    allProjectsConfig = new AllProjectsConfig(new AllProjectsNameOnInitProvider(sections), configProvider, sitePaths, flags);
}
Also used : Path(java.nio.file.Path) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) InMemorySecureStore(com.google.gerrit.server.securestore.testing.InMemorySecureStore) FileBasedAllProjectsConfigProvider(com.google.gerrit.server.config.FileBasedAllProjectsConfigProvider) Config(org.eclipse.jgit.lib.Config) ImmutableList(com.google.common.collect.ImmutableList) Path(java.nio.file.Path) Before(org.junit.Before) TestRepository(org.eclipse.jgit.junit.TestRepository) Files(java.nio.file.Files) FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) IOException(java.io.IOException) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) File(java.io.File) FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig) Rule(org.junit.Rule) SitePaths(com.google.gerrit.server.config.SitePaths) StoredConfig(org.eclipse.jgit.lib.StoredConfig) FS(org.eclipse.jgit.util.FS) AllProjectsConfigProvider(com.google.gerrit.server.config.AllProjectsConfigProvider) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Repository(org.eclipse.jgit.lib.Repository) TemporaryFolder(org.junit.rules.TemporaryFolder) FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) SitePaths(com.google.gerrit.server.config.SitePaths) StoredConfig(org.eclipse.jgit.lib.StoredConfig) TestRepository(org.eclipse.jgit.junit.TestRepository) FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) Repository(org.eclipse.jgit.lib.Repository) FileBasedAllProjectsConfigProvider(com.google.gerrit.server.config.FileBasedAllProjectsConfigProvider) InMemorySecureStore(com.google.gerrit.server.securestore.testing.InMemorySecureStore) FileBasedAllProjectsConfigProvider(com.google.gerrit.server.config.FileBasedAllProjectsConfigProvider) AllProjectsConfigProvider(com.google.gerrit.server.config.AllProjectsConfigProvider) FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig) Before(org.junit.Before)

Example 44 with FileBasedConfig

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

the class JGitConfigTest method openSystemConfigReturnsDifferentInstances.

@Test
public void openSystemConfigReturnsDifferentInstances() throws Exception {
    FileBasedConfig system1 = SystemReader.getInstance().openSystemConfig(null, FS.DETECTED);
    system1.load();
    assertThat(system1.getString("core", null, "trustFolderStat")).isEqualTo("false");
    FileBasedConfig system2 = SystemReader.getInstance().openSystemConfig(null, FS.DETECTED);
    system2.load();
    assertThat(system2.getString("core", null, "trustFolderStat")).isEqualTo("false");
    system1.setString("core", null, "trustFolderStat", "true");
    assertThat(system1.getString("core", null, "trustFolderStat")).isEqualTo("true");
    assertThat(system2.getString("core", null, "trustFolderStat")).isEqualTo("false");
}
Also used : FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig) Test(org.junit.Test)

Example 45 with FileBasedConfig

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

the class JGitConfigTest method openSystemConfigRespectsParent.

@Test
public void openSystemConfigRespectsParent() throws Exception {
    Config parent = new Config();
    parent.setString("foo", null, "bar", "value");
    FileBasedConfig system = SystemReader.getInstance().openSystemConfig(parent, FS.DETECTED);
    system.load();
    assertThat(system.getString("core", null, "trustFolderStat")).isEqualTo("false");
    assertThat(system.getString("foo", null, "bar")).isEqualTo("value");
}
Also used : Config(org.eclipse.jgit.lib.Config) FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig) FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig) Test(org.junit.Test)

Aggregations

FileBasedConfig (org.eclipse.jgit.storage.file.FileBasedConfig)47 File (java.io.File)19 IOException (java.io.IOException)15 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)12 Config (org.eclipse.jgit.lib.Config)10 FS (org.eclipse.jgit.util.FS)7 Test (org.junit.Test)7 StoredConfig (org.eclipse.jgit.lib.StoredConfig)6 Path (java.nio.file.Path)5 Repository (org.eclipse.jgit.lib.Repository)5 SitePaths (com.google.gerrit.server.config.SitePaths)4 HashMap (java.util.HashMap)4 UserModel (com.gitblit.models.UserModel)3 FileBasedAllProjectsConfigProvider (com.google.gerrit.server.config.FileBasedAllProjectsConfigProvider)3 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 ObjectId (org.eclipse.jgit.lib.ObjectId)3 RevCommit (org.eclipse.jgit.revwalk.RevCommit)3 SystemReader (org.eclipse.jgit.util.SystemReader)3 Before (org.junit.Before)3