Search in sources :

Example 1 with Branch

use of com.google.gerrit.reviewdb.client.Branch in project gerrit by GerritCodeReview.

the class SubmoduleSectionParser method parse.

private SubmoduleSubscription parse(final String id) {
    final String url = bbc.getString("submodule", id, "url");
    final String path = bbc.getString("submodule", id, "path");
    String branch = bbc.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.get();
            }
            // relative URL
            if (url.startsWith("../")) {
                // prefix with a slash for easier relative path walks
                project = '/' + superProjectBranch.getParentKey().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 = new Project.NameKey(project);
            return new SubmoduleSubscription(superProjectBranch, new Branch.NameKey(projectKey, branch), path);
        }
    } catch (URISyntaxException e) {
    // Error in url syntax (in fact it is uri syntax)
    }
    return null;
}
Also used : Project(com.google.gerrit.reviewdb.client.Project) Branch(com.google.gerrit.reviewdb.client.Branch) SubmoduleSubscription(com.google.gerrit.reviewdb.client.SubmoduleSubscription) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI)

Example 2 with Branch

use of com.google.gerrit.reviewdb.client.Branch in project gerrit by GerritCodeReview.

the class CommitIncludedInIT method includedInMergedChange.

@Test
public void includedInMergedChange() throws Exception {
    Result result = createChange();
    gApi.changes().id(result.getChangeId()).revision(result.getCommit().name()).review(ReviewInput.approve());
    gApi.changes().id(result.getChangeId()).revision(result.getCommit().name()).submit();
    assertThat(getIncludedIn(result.getCommit().getId()).branches).containsExactly("master");
    assertThat(getIncludedIn(result.getCommit().getId()).tags).isEmpty();
    grantTagPermissions();
    gApi.projects().name(result.getChange().project().get()).tag("test-tag").create(new TagInput());
    assertThat(getIncludedIn(result.getCommit().getId()).tags).containsExactly("test-tag");
    createBranch(new Branch.NameKey(project.get(), "test-branch"));
    assertThat(getIncludedIn(result.getCommit().getId()).branches).containsExactly("master", "test-branch");
}
Also used : Branch(com.google.gerrit.reviewdb.client.Branch) TagInput(com.google.gerrit.extensions.api.projects.TagInput) Result(com.google.gerrit.acceptance.PushOneCommit.Result) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 3 with Branch

use of com.google.gerrit.reviewdb.client.Branch in project gerrit by GerritCodeReview.

the class SubmoduleOp method addAllSubmoduleProjects.

private void addAllSubmoduleProjects(Project.NameKey project, LinkedHashSet<Project.NameKey> current, LinkedHashSet<Project.NameKey> projects) throws SubmoduleException {
    if (current.contains(project)) {
        throw new SubmoduleException("Project level circular subscriptions detected:  " + printCircularPath(current, project));
    }
    if (projects.contains(project)) {
        return;
    }
    current.add(project);
    Set<Project.NameKey> subprojects = new HashSet<>();
    for (Branch.NameKey branch : branchesByProject.get(project)) {
        Collection<SubmoduleSubscription> subscriptions = targets.get(branch);
        for (SubmoduleSubscription s : subscriptions) {
            subprojects.add(s.getSubmodule().getParentKey());
        }
    }
    for (Project.NameKey p : subprojects) {
        addAllSubmoduleProjects(p, current, projects);
    }
    current.remove(project);
    projects.add(project);
}
Also used : Project(com.google.gerrit.reviewdb.client.Project) Branch(com.google.gerrit.reviewdb.client.Branch) SubmoduleSubscription(com.google.gerrit.reviewdb.client.SubmoduleSubscription) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 4 with Branch

use of com.google.gerrit.reviewdb.client.Branch in project gerrit by GerritCodeReview.

the class SubmoduleOp method updateSuperProjects.

public void updateSuperProjects(BatchUpdate.Factory updateFactory) throws SubmoduleException {
    ImmutableSet<Project.NameKey> projects = getProjectsInOrder();
    if (projects == null) {
        return;
    }
    LinkedHashSet<Project.NameKey> superProjects = new LinkedHashSet<>();
    try {
        for (Project.NameKey project : projects) {
            // only need superprojects
            if (branchesByProject.containsKey(project)) {
                superProjects.add(project);
                // get a new BatchUpdate for the super project
                OpenRepo or = orm.getRepo(project);
                for (Branch.NameKey branch : branchesByProject.get(project)) {
                    addOp(or.getUpdate(updateFactory), branch);
                }
            }
        }
        batchUpdateFactory.execute(orm.batchUpdates(updateFactory, superProjects), BatchUpdateListener.NONE, orm.getSubmissionId(), false);
    } catch (RestApiException | UpdateException | IOException | NoSuchProjectException e) {
        throw new SubmoduleException("Cannot update gitlinks", e);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) NoSuchProjectException(com.google.gerrit.server.project.NoSuchProjectException) IOException(java.io.IOException) Project(com.google.gerrit.reviewdb.client.Project) Branch(com.google.gerrit.reviewdb.client.Branch) OpenRepo(com.google.gerrit.server.git.MergeOpRepoManager.OpenRepo) UpdateException(com.google.gerrit.server.update.UpdateException) RestApiException(com.google.gerrit.extensions.restapi.RestApiException)

Example 5 with Branch

use of com.google.gerrit.reviewdb.client.Branch in project gerrit by GerritCodeReview.

the class SubmoduleOp method superProjectSubscriptionsForSubmoduleBranch.

public Collection<SubmoduleSubscription> superProjectSubscriptionsForSubmoduleBranch(Branch.NameKey srcBranch) throws IOException {
    logDebug("Calculating possible superprojects for " + srcBranch);
    Collection<SubmoduleSubscription> ret = new ArrayList<>();
    Project.NameKey srcProject = srcBranch.getParentKey();
    ProjectConfig cfg = projectCache.get(srcProject).getConfig();
    for (SubscribeSection s : projectStateFactory.create(cfg).getSubscribeSections(srcBranch)) {
        logDebug("Checking subscribe section " + s);
        Collection<Branch.NameKey> branches = getDestinationBranches(srcBranch, s);
        for (Branch.NameKey targetBranch : branches) {
            Project.NameKey targetProject = targetBranch.getParentKey();
            try {
                OpenRepo or = orm.getRepo(targetProject);
                ObjectId id = or.repo.resolve(targetBranch.get());
                if (id == null) {
                    logDebug("The branch " + targetBranch + " doesn't exist.");
                    continue;
                }
            } catch (NoSuchProjectException e) {
                logDebug("The project " + targetProject + " doesn't exist");
                continue;
            }
            GitModules m = branchGitModules.get(targetBranch);
            if (m == null) {
                m = gitmodulesFactory.create(targetBranch, orm);
                branchGitModules.put(targetBranch, m);
            }
            ret.addAll(m.subscribedTo(srcBranch));
        }
    }
    logDebug("Calculated superprojects for " + srcBranch + " are " + ret);
    return ret;
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) NoSuchProjectException(com.google.gerrit.server.project.NoSuchProjectException) ArrayList(java.util.ArrayList) SubscribeSection(com.google.gerrit.common.data.SubscribeSection) Project(com.google.gerrit.reviewdb.client.Project) Branch(com.google.gerrit.reviewdb.client.Branch) OpenRepo(com.google.gerrit.server.git.MergeOpRepoManager.OpenRepo) SubmoduleSubscription(com.google.gerrit.reviewdb.client.SubmoduleSubscription)

Aggregations

Branch (com.google.gerrit.reviewdb.client.Branch)36 Project (com.google.gerrit.reviewdb.client.Project)20 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)18 SubmoduleSubscription (com.google.gerrit.reviewdb.client.SubmoduleSubscription)18 Test (org.junit.Test)18 SubmoduleSectionParser (com.google.gerrit.server.util.SubmoduleSectionParser)14 Config (org.eclipse.jgit.lib.Config)14 IOException (java.io.IOException)9 ObjectId (org.eclipse.jgit.lib.ObjectId)9 Change (com.google.gerrit.reviewdb.client.Change)7 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)5 OpenRepo (com.google.gerrit.server.git.MergeOpRepoManager.OpenRepo)5 ChangeData (com.google.gerrit.server.query.change.ChangeData)5 ArrayList (java.util.ArrayList)5 OrmException (com.google.gwtorm.server.OrmException)4 Repository (org.eclipse.jgit.lib.Repository)4 RevCommit (org.eclipse.jgit.revwalk.RevCommit)4 RevWalk (org.eclipse.jgit.revwalk.RevWalk)4 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)3 IdentifiedUser (com.google.gerrit.server.IdentifiedUser)3