Example usage for org.eclipse.jgit.lib BlobBasedConfig BlobBasedConfig

List of usage examples for org.eclipse.jgit.lib BlobBasedConfig BlobBasedConfig

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib BlobBasedConfig BlobBasedConfig.

Prototype

public BlobBasedConfig(Config base, Repository db, AnyObjectId treeish, String path)
        throws FileNotFoundException, IOException, ConfigInvalidException 

Source Link

Document

Load a configuration file from a blob stored in a specific commit.

Usage

From source file:com.google.gerrit.server.git.GitModules.java

License:Apache License

void load() throws IOException {
    Project.NameKey project = branch.getParentKey();
    logDebug("Loading .gitmodules of {} for project {}", branch, project);
    try {//w  ww  .  j  a va 2s  .c om
        orm.openRepo(project, false);
    } catch (NoSuchProjectException e) {
        throw new IOException(e);
    }
    OpenRepo or = orm.getRepo(project);

    ObjectId id = or.repo.resolve(branch.get());
    if (id == null) {
        throw new IOException("Cannot open branch " + branch.get());
    }
    RevCommit commit = or.rw.parseCommit(id);

    TreeWalk tw = TreeWalk.forPath(or.repo, GIT_MODULES, commit.getTree());
    if (tw == null || (tw.getRawMode(0) & FileMode.TYPE_MASK) != FileMode.TYPE_FILE) {
        return;
    }
    try {
        BlobBasedConfig bbc = new BlobBasedConfig(null, or.repo, commit, GIT_MODULES);
        subscriptions = subSecParserFactory.create(bbc, thisServer, branch).parseAllSections();
    } catch (ConfigInvalidException e) {
        throw new IOException("Could not read .gitmodule file of super project: " + branch.getParentKey(), e);
    }
}

From source file:com.google.gerrit.server.git.SubmoduleOp.java

License:Apache License

void updateSubmoduleSubscriptions(ReviewDb db, Branch.NameKey destBranch) throws SubmoduleException {
    if (urlProvider.get() == null) {
        logAndThrowSubmoduleException("Cannot establish canonical web url used "
                + "to access gerrit. It should be provided in gerrit.config file.");
    }/*from  w ww. j av a 2s.  com*/
    try (Repository repo = repoManager.openRepository(destBranch.getParentKey());
            RevWalk rw = new RevWalk(repo)) {

        ObjectId id = repo.resolve(destBranch.get());
        if (id == null) {
            logAndThrowSubmoduleException("Cannot resolve submodule destination branch " + destBranch);
        }
        RevCommit commit = rw.parseCommit(id);

        Set<SubmoduleSubscription> oldSubscriptions = Sets
                .newHashSet(db.submoduleSubscriptions().bySuperProject(destBranch));

        Set<SubmoduleSubscription> newSubscriptions;
        TreeWalk tw = TreeWalk.forPath(repo, GIT_MODULES, commit.getTree());
        if (tw != null && (FileMode.REGULAR_FILE.equals(tw.getRawMode(0))
                || FileMode.EXECUTABLE_FILE.equals(tw.getRawMode(0)))) {
            BlobBasedConfig bbc = new BlobBasedConfig(null, repo, commit, GIT_MODULES);

            String thisServer = new URI(urlProvider.get()).getHost();

            newSubscriptions = subSecParserFactory.create(bbc, thisServer, destBranch).parseAllSections();
        } else {
            newSubscriptions = Collections.emptySet();
        }

        Set<SubmoduleSubscription> alreadySubscribeds = new HashSet<>();
        for (SubmoduleSubscription s : newSubscriptions) {
            if (oldSubscriptions.contains(s)) {
                alreadySubscribeds.add(s);
            }
        }

        oldSubscriptions.removeAll(newSubscriptions);
        newSubscriptions.removeAll(alreadySubscribeds);

        if (!oldSubscriptions.isEmpty()) {
            db.submoduleSubscriptions().delete(oldSubscriptions);
        }
        if (!newSubscriptions.isEmpty()) {
            db.submoduleSubscriptions().insert(newSubscriptions);
        }

    } catch (OrmException e) {
        logAndThrowSubmoduleException(
                "Database problem at update of subscriptions table from " + GIT_MODULES + " file.", e);
    } catch (ConfigInvalidException e) {
        logAndThrowSubmoduleException(
                "Problem at update of subscriptions table: " + GIT_MODULES + " config file is invalid.", e);
    } catch (IOException e) {
        logAndThrowSubmoduleException("Problem at update of subscriptions table from " + GIT_MODULES + ".", e);
    } catch (URISyntaxException e) {
        logAndThrowSubmoduleException("Incorrect gerrit canonical web url provided in gerrit.config file.", e);
    }
}

From source file:jetbrains.buildServer.buildTriggers.vcs.git.submodules.SubmoduleResolverImpl.java

License:Apache License

/**
 * Ensure that submodule configuration has been loaded.
 *//*  w  ww .  j a  v  a  2 s.c o m*/
private void ensureConfigLoaded() {
    if (myConfig == null) {
        try {
            myConfig = new SubmodulesConfig(myContext.getConfig(myDb),
                    new BlobBasedConfig(null, myDb, myCommit, ".gitmodules"));
        } catch (FileNotFoundException e) {
            // do nothing
        } catch (Exception e) {
            LOG.error("Unable to load or parse submodule configuration at: " + myCommit.getId().name(), e);
        }
    }
}

From source file:jetbrains.buildServer.buildTriggers.vcs.git.tests.SubmoduleTest.java

License:Apache License

/**
 * Test loading mapping for submodules/*w w w. j  a  v a 2  s . com*/
 *
 * @throws IOException if there is IO problem
 */
@Test
public void testSubmoduleMapping() throws Exception {
    File masterRep = dataFile("repo.git");
    Repository r = new RepositoryBuilder().setGitDir(masterRep).build();
    try {
        SubmodulesConfig s = new SubmodulesConfig(r.getConfig(), new BlobBasedConfig(null, r,
                r.resolve(GitUtils.versionRevision(GitVcsSupportTest.SUBMODULE_ADDED_VERSION)), ".gitmodules"));
        assertTrue(s.isSubmodulePrefix(""));
        assertFalse(s.isSubmodulePrefix("submodule"));
        Submodule m = s.findSubmodule("submodule");
        assertEquals(m.getName(), "submodule");
        assertEquals(m.getPath(), "submodule");
        assertEquals(m.getUrl(), "../submodule.git");
    } finally {
        r.close();
    }
}