Example usage for org.eclipse.jgit.transport URIish getPath

List of usage examples for org.eclipse.jgit.transport URIish getPath

Introduction

In this page you can find the example usage for org.eclipse.jgit.transport URIish getPath.

Prototype

public String getPath() 

Source Link

Document

Get path name component

Usage

From source file:com.docmd.behavior.GitManager.java

public boolean isValidRemoteRepository(URIish repoUri) {
    boolean result;

    if (repoUri.getScheme().toLowerCase().startsWith("http")) {
        String path = repoUri.getPath();
        String newPath = path.endsWith("/") ? path + INFO_REFS_PATH : path + "/" + INFO_REFS_PATH;
        URIish checkUri = repoUri.setPath(newPath);
        InputStream ins = null;/*www.  j  a  v a  2s. c  o  m*/
        try {
            URLConnection conn = new URL(checkUri.toString()).openConnection();
            //conn.setReadTimeout(5000);
            ins = conn.getInputStream();
            result = true;
        } catch (Exception e) {
            if (e.getMessage().contains("403"))
                result = true;
            else
                result = false;
        } finally {
            try {
                ins.close();
            } catch (Exception e) {
                /* ignore */ }
        }
    } else {
        if (repoUri.getScheme().toLowerCase().startsWith("ssh")) {
            RemoteSession ssh = null;
            Process exec = null;
            try {
                ssh = SshSessionFactory.getInstance().getSession(repoUri, null, FS.detect(), 5000);
                exec = ssh.exec("cd " + repoUri.getPath() + "; git rev-parse --git-dir", 5000);
                Integer exitValue = null;
                do {
                    try {
                        exitValue = exec.exitValue();
                    } catch (Exception e) {
                        try {
                            Thread.sleep(1000);
                        } catch (Exception ee) {
                        }
                    }
                } while (exitValue == null);
                result = exitValue == 0;
            } catch (Exception e) {
                result = false;
            } finally {
                try {
                    exec.destroy();
                } catch (Exception e) {
                    /* ignore */ }
                try {
                    ssh.disconnect();
                } catch (Exception e) {
                    /* ignore */ }
            }
        } else {
            result = true;
        }
    }
    return result;
}

From source file:com.genuitec.eclipse.gerrit.tools.utils.GerritUtils.java

License:Open Source License

public static String getGerritProjectName(Repository repository) {
    try {//w ww .  j  a  v a  2  s .c  om
        RemoteConfig config = new RemoteConfig(repository.getConfig(), "origin"); //$NON-NLS-1$

        List<URIish> urls = new ArrayList<URIish>(config.getPushURIs());
        urls.addAll(config.getURIs());

        for (URIish uri : urls) {
            if (uri.getPort() == 29418) { //Gerrit refspec
                String path = uri.getPath();
                while (path.startsWith("/")) { //$NON-NLS-1$
                    path = path.substring(1);
                }
                return path;
            }
            break;
        }
    } catch (Exception e) {
        GerritToolsPlugin.getDefault().log(e);
    }
    return null;
}

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

License:Apache License

private List<ReplicationConfig> allConfigs(final SitePaths site) throws ConfigInvalidException, IOException {
    final FileBasedConfig cfg = new FileBasedConfig(site.replication_config, FS.DETECTED);

    if (!cfg.getFile().exists()) {
        log.warn("No " + cfg.getFile() + "; not replicating");
        return Collections.emptyList();
    }//from   w  ww  .  j av  a  2 s .c o m
    if (cfg.getFile().length() == 0) {
        log.info("Empty " + cfg.getFile() + "; not replicating");
        return Collections.emptyList();
    }

    try {
        cfg.load();
    } catch (ConfigInvalidException e) {
        throw new ConfigInvalidException("Config file " + cfg.getFile() + " is invalid: " + e.getMessage(), e);
    } catch (IOException e) {
        throw new IOException("Cannot read " + cfg.getFile() + ": " + e.getMessage(), e);
    }

    final List<ReplicationConfig> r = new ArrayList<ReplicationConfig>();
    for (final RemoteConfig c : allRemotes(cfg)) {
        if (c.getURIs().isEmpty()) {
            continue;
        }

        for (final URIish u : c.getURIs()) {
            if (u.getPath() == null || !u.getPath().contains("${name}")) {
                throw new ConfigInvalidException("remote." + c.getName() + ".url" + " \"" + u
                        + "\" lacks ${name} placeholder in " + cfg.getFile());
            }
        }

        // In case if refspec destination for push is not set then we assume it is
        // equal to source
        for (RefSpec ref : c.getPushRefSpecs()) {
            if (ref.getDestination() == null) {
                ref.setDestination(ref.getSource());
            }
        }

        if (c.getPushRefSpecs().isEmpty()) {
            RefSpec spec = new RefSpec();
            spec = spec.setSourceDestination("refs/*", "refs/*");
            spec = spec.setForceUpdate(true);
            c.addPushRefSpec(spec);
        }

        r.add(new ReplicationConfig(injector, workQueue, c, cfg, database, replicationUserFactory));
    }
    return Collections.unmodifiableList(r);
}

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

License:Apache License

@Override
public void replicateNewProject(Project.NameKey projectName, String head) {
    if (!isEnabled()) {
        return;//from www.j a v a 2  s. co m
    }

    for (ReplicationConfig config : configs) {
        List<URIish> uriList = config.getURIs(projectName, "*");
        String[] adminUrls = config.getAdminUrls();
        boolean adminURLUsed = false;

        for (String url : adminUrls) {
            URIish adminURI = null;
            try {
                if (url != null && !url.isEmpty()) {
                    adminURI = new URIish(url);
                }
            } catch (URISyntaxException e) {
                log.error("The URL '" + url + "' is invalid");
            }

            if (adminURI != null) {
                final String replacedPath = replace(adminURI.getPath(), "name", projectName.get());
                if (replacedPath != null) {
                    adminURI = adminURI.setPath(replacedPath);
                    if (usingSSH(adminURI)) {
                        replicateProject(adminURI, head);
                        adminURLUsed = true;
                    } else {
                        log.error("The adminURL '" + url + "' is non-SSH which is not allowed");
                    }
                }
            }
        }

        if (!adminURLUsed) {
            for (URIish uri : uriList) {
                replicateProject(uri, head);
            }
        }
    }
}

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

License:Apache License

private void replicateProject(final URIish replicateURI, final String head) {
    SshSessionFactory sshFactory = SshSessionFactory.getInstance();
    RemoteSession sshSession;//from  w  w w .  j a va  2 s  .c o  m
    String projectPath = QuotedString.BOURNE.quote(replicateURI.getPath());

    if (!usingSSH(replicateURI)) {
        log.warn("Cannot create new project on remote site since the connection " + "method is not SSH: "
                + replicateURI.toString());
        return;
    }

    OutputStream errStream = createErrStream();
    String cmd = "mkdir -p " + projectPath + "&& cd " + projectPath + "&& git init --bare"
            + "&& git symbolic-ref HEAD " + QuotedString.BOURNE.quote(head);

    try {
        sshSession = sshFactory.getSession(replicateURI, null, FS.DETECTED, 0);
        Process proc = sshSession.exec(cmd, 0);
        proc.getOutputStream().close();
        StreamCopyThread out = new StreamCopyThread(proc.getInputStream(), errStream);
        StreamCopyThread err = new StreamCopyThread(proc.getErrorStream(), errStream);
        out.start();
        err.start();
        try {
            proc.waitFor();
            out.halt();
            err.halt();
        } catch (InterruptedException interrupted) {
            // Don't wait, drop out immediately.
        }
        sshSession.disconnect();
    } catch (IOException e) {
        log.error("Communication error when trying to replicate to: " + replicateURI.toString() + "\n"
                + "Error reported: " + e.getMessage() + "\n" + "Error in communication: "
                + errStream.toString());
    }
}

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

License:Apache License

private boolean usingSSH(final URIish uri) {
    final String scheme = uri.getScheme();
    if (!uri.isRemote())
        return false;
    if (scheme != null && scheme.toLowerCase().contains("ssh"))
        return true;
    if (scheme == null && uri.getHost() != null && uri.getPath() != null)
        return true;
    return false;
}

From source file:com.googlesource.gerrit.plugins.github.replication.Destination.java

License:Apache License

List<URIish> getURIs(Project.NameKey project, String urlMatch) {
    List<URIish> r = Lists.newArrayListWithCapacity(remote.getURIs().size());
    for (URIish uri : remote.getURIs()) {
        if (matches(uri, urlMatch)) {
            String name = project.get();
            if (needsUrlEncoding(uri)) {
                name = encode(name);//from w ww .  j  a  va2s . co m
            }
            if (remoteNameStyle.equals("dash")) {
                name = name.replace("/", "-");
            } else if (remoteNameStyle.equals("underscore")) {
                name = name.replace("/", "_");
            } else if (!remoteNameStyle.equals("slash")) {
                GitHubDestinations.log.debug(
                        String.format("Unknown remoteNameStyle: %s, falling back to slash", remoteNameStyle));
            }
            String replacedPath = GitHubDestinations.replaceName(uri.getPath(), name);
            if (replacedPath != null) {
                uri = uri.setPath(replacedPath);
                r.add(uri);
            }
        }
    }
    return r;
}

From source file:com.googlesource.gerrit.plugins.github.replication.GitHubDestinations.java

License:Apache License

private List<String> getOrganisations(List<Destination> destinations) {
    ArrayList<String> organisations = new ArrayList<String>();
    for (Destination destination : destinations) {
        for (URIish urish : destination.getRemote().getURIs()) {
            String[] uriPathParts = urish.getPath().split("/");
            organisations.add(uriPathParts[0]);
        }//  w  w  w.j a v  a2 s .com
    }
    return organisations;
}

From source file:com.googlesource.gerrit.plugins.github.replication.GitHubDestinations.java

License:Apache License

private List<Destination> getDestinations(File cfgPath) throws ConfigInvalidException, IOException {
    FileBasedConfig cfg = new FileBasedConfig(cfgPath, FS.DETECTED);
    if (!cfg.getFile().exists() || cfg.getFile().length() == 0) {
        return Collections.emptyList();
    }//from  w  w  w  .  j  av  a 2  s  . c  o m

    try {
        cfg.load();
    } catch (ConfigInvalidException e) {
        throw new ConfigInvalidException(
                String.format("Config file %s is invalid: %s", cfg.getFile(), e.getMessage()), e);
    } catch (IOException e) {
        throw new IOException(String.format("Cannot read %s: %s", cfg.getFile(), e.getMessage()), e);
    }

    ImmutableList.Builder<Destination> dest = ImmutableList.builder();
    for (RemoteConfig c : allRemotes(cfg)) {
        if (c.getURIs().isEmpty()) {
            continue;
        }

        for (URIish u : c.getURIs()) {
            if (u.getPath() == null || !u.getPath().contains("${name}")) {
                throw new ConfigInvalidException(String.format(
                        "remote.%s.url \"%s\" lacks ${name} placeholder in %s", c.getName(), u, cfg.getFile()));
            }
        }

        // If destination for push is not set assume equal to source.
        for (RefSpec ref : c.getPushRefSpecs()) {
            if (ref.getDestination() == null) {
                ref.setDestination(ref.getSource());
            }
        }

        if (c.getPushRefSpecs().isEmpty()) {
            c.addPushRefSpec(new RefSpec().setSourceDestination("refs/*", "refs/*").setForceUpdate(true));
        }

        dest.add(new Destination(injector, c, cfg, database, replicationUserFactory, pluginUser,
                gitRepositoryManager, groupBackend));
    }
    return dest.build();
}

From source file:com.googlesource.gerrit.plugins.replication.Destination.java

License:Apache License

List<URIish> getURIs(Project.NameKey project, String urlMatch) {
    List<URIish> r = Lists.newArrayListWithCapacity(config.getRemoteConfig().getURIs().size());
    for (URIish uri : config.getRemoteConfig().getURIs()) {
        if (matches(uri, urlMatch)) {
            String name = project.get();
            if (needsUrlEncoding(uri)) {
                name = encode(name);//from w  w  w .ja va  2 s  .  co  m
            }
            String remoteNameStyle = config.getRemoteNameStyle();
            if (remoteNameStyle.equals("dash")) {
                name = name.replace("/", "-");
            } else if (remoteNameStyle.equals("underscore")) {
                name = name.replace("/", "_");
            } else if (remoteNameStyle.equals("basenameOnly")) {
                name = FilenameUtils.getBaseName(name);
            } else if (!remoteNameStyle.equals("slash")) {
                repLog.debug(
                        String.format("Unknown remoteNameStyle: %s, falling back to slash", remoteNameStyle));
            }
            String replacedPath = ReplicationQueue.replaceName(uri.getPath(), name, isSingleProjectMatch());
            if (replacedPath != null) {
                uri = uri.setPath(replacedPath);
                r.add(uri);
            }
        }
    }
    return r;
}