Search in sources :

Example 1 with SubmoduleSubscription

use of com.google.gerrit.entities.SubmoduleSubscription in project gerrit by GerritCodeReview.

the class SubmoduleSectionParser method parse.

private SubmoduleSubscription parse(String id) {
    final String url = config.getString("submodule", id, "url");
    final String path = config.getString("submodule", id, "path");
    String branch = config.getString("submodule", id, "branch");
    try {
        if (url != null && url.length() > 0 && path != null && path.length() > 0 && branch != null && branch.length() > 0) {
            // All required fields filled.
            String project;
            if (branch.equals(".")) {
                branch = superProjectBranch.branch();
            }
            // relative URL
            if (url.startsWith("../")) {
                // prefix with a slash for easier relative path walks
                project = '/' + superProjectBranch.project().get();
                String hostPart = url;
                while (hostPart.startsWith("../")) {
                    int lastSlash = project.lastIndexOf('/');
                    if (lastSlash < 0) {
                        // too many levels up, ignore for now
                        return null;
                    }
                    project = project.substring(0, lastSlash);
                    hostPart = hostPart.substring(3);
                }
                project = project + "/" + hostPart;
                // remove leading '/'
                project = project.substring(1);
            } else {
                // It is actually an URI. It could be ssh://localhost/project-a.
                URI targetServerURI = new URI(url);
                URI thisServerURI = new URI(canonicalWebUrl);
                String thisHost = thisServerURI.getHost();
                String targetHost = targetServerURI.getHost();
                if (thisHost == null || targetHost == null || !targetHost.equalsIgnoreCase(thisHost)) {
                    return null;
                }
                String p1 = targetServerURI.getPath();
                String p2 = thisServerURI.getPath();
                if (!p1.startsWith(p2)) {
                    // http://server/other-teams-gerrit/
                    return null;
                }
                // skip common part
                project = p1.substring(p2.length());
            }
            while (project.startsWith("/")) {
                project = project.substring(1);
            }
            if (project.endsWith(Constants.DOT_GIT_EXT)) {
                project = project.substring(// 
                0, project.length() - Constants.DOT_GIT_EXT.length());
            }
            Project.NameKey projectKey = Project.nameKey(project);
            return new SubmoduleSubscription(superProjectBranch, BranchNameKey.create(projectKey, branch), path);
        }
    } catch (URISyntaxException e) {
    // Error in url syntax (in fact it is uri syntax)
    }
    return null;
}
Also used : Project(com.google.gerrit.entities.Project) SubmoduleSubscription(com.google.gerrit.entities.SubmoduleSubscription) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 2 with SubmoduleSubscription

use of com.google.gerrit.entities.SubmoduleSubscription in project gerrit by GerritCodeReview.

the class SubmoduleCommitsTest method createGitlinksCommit_subprojectMoved.

@Test
public void createGitlinksCommit_subprojectMoved() throws Exception {
    createRepo(subProject, MASTER);
    createRepo(superProject, MASTER);
    when(mockProjectCache.get(any())).thenReturn(Optional.of(mockProjectState));
    mergeOpRepoManager = new MergeOpRepoManager(repoManager, mockProjectCache, null, null);
    ObjectId subprojectCommit = getTip(subProject, MASTER);
    RevCommit superprojectTip = directUpdateSubmodule(superProject, MASTER, Project.nameKey("dir-x"), subprojectCommit);
    assertThat(readGitLink(superProject, superprojectTip, "dir-x")).isEqualTo(subprojectCommit);
    RevCommit newSubprojectCommit = addCommit(subProject, MASTER);
    BranchNameKey superBranch = BranchNameKey.create(superProject, MASTER);
    BranchNameKey subBranch = BranchNameKey.create(subProject, MASTER);
    SubmoduleSubscription ss = new SubmoduleSubscription(superBranch, subBranch, "dir-x");
    SubmoduleCommits helper = new SubmoduleCommits(mergeOpRepoManager, ident, new Config());
    Optional<CodeReviewCommit> newGitLinksCommit = helper.composeGitlinksCommit(BranchNameKey.create(superProject, MASTER), ImmutableList.of(ss));
    assertThat(newGitLinksCommit).isPresent();
    assertThat(newGitLinksCommit.get().getParent(0)).isEqualTo(superprojectTip);
    assertThat(readGitLink(superProject, newGitLinksCommit.get(), "dir-x")).isEqualTo(newSubprojectCommit);
}
Also used : BranchNameKey(com.google.gerrit.entities.BranchNameKey) AnyObjectId(org.eclipse.jgit.lib.AnyObjectId) ObjectId(org.eclipse.jgit.lib.ObjectId) Config(org.eclipse.jgit.lib.Config) SubmoduleSubscription(com.google.gerrit.entities.SubmoduleSubscription) CodeReviewCommit(com.google.gerrit.server.git.CodeReviewCommit) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

Example 3 with SubmoduleSubscription

use of com.google.gerrit.entities.SubmoduleSubscription in project gerrit by GerritCodeReview.

the class SubscriptionGraphTest method multipleSuperprojectsToMultipleSubmodules.

@Test
public void multipleSuperprojectsToMultipleSubmodules() throws Exception {
    // Create superprojects and subprojects.
    Project.NameKey superProject1 = Project.nameKey("superproject1");
    Project.NameKey superProject2 = Project.nameKey("superproject2");
    Project.NameKey subProject1 = Project.nameKey("subproject1");
    Project.NameKey subProject2 = Project.nameKey("subproject2");
    TestRepository<Repository> superProjectRepo1 = createRepo(superProject1);
    TestRepository<Repository> superProjectRepo2 = createRepo(superProject2);
    TestRepository<Repository> submoduleRepo1 = createRepo(subProject1);
    TestRepository<Repository> submoduleRepo2 = createRepo(subProject2);
    // Initialize super branches.
    BranchNameKey superBranch1 = BranchNameKey.create(superProject1, "refs/heads/one");
    BranchNameKey superBranch2 = BranchNameKey.create(superProject2, "refs/heads/one");
    createBranch(superProjectRepo1, superBranch1, superProjectRepo1.commit().message("Initial commit").create());
    createBranch(superProjectRepo2, superBranch2, superProjectRepo2.commit().message("Initial commit").create());
    // Initialize sub branches.
    BranchNameKey submoduleBranch1 = BranchNameKey.create(subProject1, "refs/heads/one");
    BranchNameKey submoduleBranch2 = BranchNameKey.create(subProject1, "refs/heads/two");
    BranchNameKey submoduleBranch3 = BranchNameKey.create(subProject2, "refs/heads/one");
    createBranch(submoduleRepo1, submoduleBranch1, submoduleRepo1.commit().message("Commit1").create());
    createBranch(submoduleRepo1, submoduleBranch2, submoduleRepo1.commit().message("Commit2").create());
    createBranch(submoduleRepo2, submoduleBranch3, submoduleRepo2.commit().message("Commit1").create());
    allowSubscription(submoduleBranch1);
    allowSubscription(submoduleBranch2);
    allowSubscription(submoduleBranch3);
    // Initialize subscriptions.
    setSubscription(submoduleBranch1, ImmutableList.of(superBranch1, superBranch2));
    setSubscription(submoduleBranch2, ImmutableList.of(superBranch1));
    setSubscription(submoduleBranch3, ImmutableList.of(superBranch1, superBranch2));
    SubscriptionGraph.Factory factory = new DefaultFactory(mockGitModulesFactory, mockProjectCache);
    SubscriptionGraph subscriptionGraph = factory.compute(ImmutableSet.of(submoduleBranch1, submoduleBranch2), mergeOpRepoManager);
    assertThat(subscriptionGraph.getAffectedSuperProjects()).containsExactly(superProject1, superProject2);
    assertThat(subscriptionGraph.getAffectedSuperBranches(superProject1)).containsExactly(superBranch1);
    assertThat(subscriptionGraph.getAffectedSuperBranches(superProject2)).containsExactly(superBranch2);
    assertThat(subscriptionGraph.getSubscriptions(superBranch1)).containsExactly(new SubmoduleSubscription(superBranch1, submoduleBranch1, TEST_PATH), new SubmoduleSubscription(superBranch1, submoduleBranch2, TEST_PATH));
    assertThat(subscriptionGraph.getSubscriptions(superBranch2)).containsExactly(new SubmoduleSubscription(superBranch2, submoduleBranch1, TEST_PATH));
    assertThat(subscriptionGraph.hasSuperproject(submoduleBranch1)).isTrue();
    assertThat(subscriptionGraph.hasSuperproject(submoduleBranch2)).isTrue();
    assertThat(subscriptionGraph.hasSuperproject(submoduleBranch3)).isFalse();
    assertThat(subscriptionGraph.getSortedSuperprojectAndSubmoduleBranches()).containsExactly(submoduleBranch2, submoduleBranch1, superBranch2, superBranch1).inOrder();
}
Also used : Project(com.google.gerrit.entities.Project) TestRepository(org.eclipse.jgit.junit.TestRepository) Repository(org.eclipse.jgit.lib.Repository) BranchNameKey(com.google.gerrit.entities.BranchNameKey) SubmoduleSubscription(com.google.gerrit.entities.SubmoduleSubscription) DefaultFactory(com.google.gerrit.server.submit.SubscriptionGraph.DefaultFactory) Test(org.junit.Test)

Example 4 with SubmoduleSubscription

use of com.google.gerrit.entities.SubmoduleSubscription in project gerrit by GerritCodeReview.

the class SubscriptionGraphTest method oneSuperprojectOneSubmodule.

@Test
public void oneSuperprojectOneSubmodule() throws Exception {
    SubscriptionGraph.Factory factory = new DefaultFactory(mockGitModulesFactory, mockProjectCache);
    SubscriptionGraph subscriptionGraph = factory.compute(ImmutableSet.of(SUB_BRANCH), mergeOpRepoManager);
    assertThat(subscriptionGraph.getAffectedSuperProjects()).containsExactly(SUPER_PROJECT);
    assertThat(subscriptionGraph.getAffectedSuperBranches(SUPER_PROJECT)).containsExactly(SUPER_BRANCH);
    assertThat(subscriptionGraph.getSubscriptions(SUPER_BRANCH)).containsExactly(new SubmoduleSubscription(SUPER_BRANCH, SUB_BRANCH, TEST_PATH));
    assertThat(subscriptionGraph.hasSuperproject(SUB_BRANCH)).isTrue();
    assertThat(subscriptionGraph.getSortedSuperprojectAndSubmoduleBranches()).containsExactly(SUB_BRANCH, SUPER_BRANCH).inOrder();
}
Also used : SubmoduleSubscription(com.google.gerrit.entities.SubmoduleSubscription) DefaultFactory(com.google.gerrit.server.submit.SubscriptionGraph.DefaultFactory) Test(org.junit.Test)

Example 5 with SubmoduleSubscription

use of com.google.gerrit.entities.SubmoduleSubscription in project gerrit by GerritCodeReview.

the class SubmoduleSectionParserTest method withSlashesInProjectName.

@Test
public void withSlashesInProjectName() throws Exception {
    Project.NameKey p = Project.nameKey("project/with/slashes/a");
    Config cfg = new Config();
    cfg.fromText("" + "[submodule \"project/with/slashes/a\"]\n" + "path = a\n" + "url = http://localhost:80/" + p.get() + "\n" + "branch = master\n");
    BranchNameKey targetBranch = BranchNameKey.create(Project.nameKey("project"), "master");
    Set<SubmoduleSubscription> res = new SubmoduleSectionParser(cfg, THIS_SERVER, targetBranch).parseAllSections();
    Set<SubmoduleSubscription> expected = Sets.newHashSet(new SubmoduleSubscription(targetBranch, BranchNameKey.create(p, "master"), "a"));
    assertThat(res).containsExactlyElementsIn(expected);
}
Also used : Project(com.google.gerrit.entities.Project) BranchNameKey(com.google.gerrit.entities.BranchNameKey) Config(org.eclipse.jgit.lib.Config) SubmoduleSubscription(com.google.gerrit.entities.SubmoduleSubscription) Test(org.junit.Test)

Aggregations

SubmoduleSubscription (com.google.gerrit.entities.SubmoduleSubscription)22 BranchNameKey (com.google.gerrit.entities.BranchNameKey)18 Test (org.junit.Test)18 Project (com.google.gerrit.entities.Project)16 Config (org.eclipse.jgit.lib.Config)16 CodeReviewCommit (com.google.gerrit.server.git.CodeReviewCommit)4 ObjectId (org.eclipse.jgit.lib.ObjectId)4 RevCommit (org.eclipse.jgit.revwalk.RevCommit)3 StorageException (com.google.gerrit.exceptions.StorageException)2 NoSuchProjectException (com.google.gerrit.server.project.NoSuchProjectException)2 OpenRepo (com.google.gerrit.server.submit.MergeOpRepoManager.OpenRepo)2 DefaultFactory (com.google.gerrit.server.submit.SubscriptionGraph.DefaultFactory)2 IOException (java.io.IOException)2 DirCache (org.eclipse.jgit.dircache.DirCache)2 DirCacheEditor (org.eclipse.jgit.dircache.DirCacheEditor)2 AnyObjectId (org.eclipse.jgit.lib.AnyObjectId)2 CommitBuilder (org.eclipse.jgit.lib.CommitBuilder)2 GerritPersonIdent (com.google.gerrit.server.GerritPersonIdent)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1