Search in sources :

Example 6 with SubmoduleSubscription

use of com.google.gerrit.reviewdb.client.SubmoduleSubscription 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 7 with SubmoduleSubscription

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

the class SubmoduleOp method composeGitlinksCommit.

/** Create a separate gitlink commit */
public CodeReviewCommit composeGitlinksCommit(final Branch.NameKey subscriber) throws IOException, SubmoduleException {
    OpenRepo or;
    try {
        or = orm.getRepo(subscriber.getParentKey());
    } catch (NoSuchProjectException | IOException e) {
        throw new SubmoduleException("Cannot access superproject", e);
    }
    CodeReviewCommit currentCommit;
    if (branchTips.containsKey(subscriber)) {
        currentCommit = branchTips.get(subscriber);
    } else {
        Ref r = or.repo.exactRef(subscriber.get());
        if (r == null) {
            throw new SubmoduleException("The branch was probably deleted from the subscriber repository");
        }
        currentCommit = or.rw.parseCommit(r.getObjectId());
        addBranchTip(subscriber, currentCommit);
    }
    StringBuilder msgbuf = new StringBuilder("");
    PersonIdent author = null;
    DirCache dc = readTree(or.rw, currentCommit);
    DirCacheEditor ed = dc.editor();
    for (SubmoduleSubscription s : targets.get(subscriber)) {
        RevCommit newCommit = updateSubmodule(dc, ed, msgbuf, s);
        if (newCommit != null) {
            if (author == null) {
                author = newCommit.getAuthorIdent();
            } else if (!author.equals(newCommit.getAuthorIdent())) {
                author = myIdent;
            }
        }
    }
    ed.finish();
    ObjectId newTreeId = dc.writeTree(or.ins);
    // Gitlinks are already in the branch, return null
    if (newTreeId.equals(currentCommit.getTree())) {
        return null;
    }
    CommitBuilder commit = new CommitBuilder();
    commit.setTreeId(newTreeId);
    commit.setParentId(currentCommit);
    StringBuilder commitMsg = new StringBuilder("Update git submodules\n\n");
    if (verboseSuperProject != VerboseSuperprojectUpdate.FALSE) {
        commitMsg.append(msgbuf);
    }
    commit.setMessage(commitMsg.toString());
    commit.setAuthor(author);
    commit.setCommitter(myIdent);
    ObjectId id = or.ins.insert(commit);
    return or.rw.parseCommit(id);
}
Also used : NoSuchProjectException(com.google.gerrit.server.project.NoSuchProjectException) ObjectId(org.eclipse.jgit.lib.ObjectId) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) IOException(java.io.IOException) DirCacheEditor(org.eclipse.jgit.dircache.DirCacheEditor) DirCache(org.eclipse.jgit.dircache.DirCache) Ref(org.eclipse.jgit.lib.Ref) PersonIdent(org.eclipse.jgit.lib.PersonIdent) GerritPersonIdent(com.google.gerrit.server.GerritPersonIdent) OpenRepo(com.google.gerrit.server.git.MergeOpRepoManager.OpenRepo) SubmoduleSubscription(com.google.gerrit.reviewdb.client.SubmoduleSubscription) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 8 with SubmoduleSubscription

use of com.google.gerrit.reviewdb.client.SubmoduleSubscription 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)

Example 9 with SubmoduleSubscription

use of com.google.gerrit.reviewdb.client.SubmoduleSubscription 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 10 with SubmoduleSubscription

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

the class SubmoduleSectionParserIT method withSlashesInProjectName.

@Test
public void withSlashesInProjectName() throws Exception {
    Project.NameKey p = createProject("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");
    Branch.NameKey targetBranch = new Branch.NameKey(new Project.NameKey("project"), "master");
    Set<SubmoduleSubscription> res = new SubmoduleSectionParser(cfg, THIS_SERVER, targetBranch).parseAllSections();
    Set<SubmoduleSubscription> expected = Sets.newHashSet(new SubmoduleSubscription(targetBranch, new Branch.NameKey(p, "master"), "a"));
    assertThat(res).containsExactlyElementsIn(expected);
}
Also used : Project(com.google.gerrit.reviewdb.client.Project) Config(org.eclipse.jgit.lib.Config) Branch(com.google.gerrit.reviewdb.client.Branch) SubmoduleSubscription(com.google.gerrit.reviewdb.client.SubmoduleSubscription) SubmoduleSectionParser(com.google.gerrit.server.util.SubmoduleSectionParser) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Aggregations

SubmoduleSubscription (com.google.gerrit.reviewdb.client.SubmoduleSubscription)21 Branch (com.google.gerrit.reviewdb.client.Branch)18 Project (com.google.gerrit.reviewdb.client.Project)17 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)14 SubmoduleSectionParser (com.google.gerrit.server.util.SubmoduleSectionParser)14 Config (org.eclipse.jgit.lib.Config)14 Test (org.junit.Test)14 OpenRepo (com.google.gerrit.server.git.MergeOpRepoManager.OpenRepo)3 NoSuchProjectException (com.google.gerrit.server.project.NoSuchProjectException)3 IOException (java.io.IOException)3 ObjectId (org.eclipse.jgit.lib.ObjectId)3 ArrayList (java.util.ArrayList)2 DirCache (org.eclipse.jgit.dircache.DirCache)2 DirCacheEditor (org.eclipse.jgit.dircache.DirCacheEditor)2 CommitBuilder (org.eclipse.jgit.lib.CommitBuilder)2 SubscribeSection (com.google.gerrit.common.data.SubscribeSection)1 GerritPersonIdent (com.google.gerrit.server.GerritPersonIdent)1 URI (java.net.URI)1 URISyntaxException (java.net.URISyntaxException)1 HashSet (java.util.HashSet)1