Have icon for "reset" entry in reflog
[egit/eclipse.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / clone / GitUrlChecker.java
blob9f401115b86ddfa3de4731815d3cbb5b9a853432
1 /*******************************************************************************
2 * Copyright (c) 2011, 2017 The Eclipse Foundation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License 2.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-2.0/
8 * SPDX-License-Identifier: EPL-2.0
10 * Contributors:
11 * The Eclipse Foundation - initial API and implementation
12 * Ian Pun - factored out of RepositorySelectionPage
13 *******************************************************************************/
14 package org.eclipse.egit.ui.internal.clone;
16 import java.net.URISyntaxException;
18 import org.eclipse.egit.ui.internal.KnownHosts;
19 import org.eclipse.egit.ui.internal.components.RepositorySelectionPage.Protocol;
20 import org.eclipse.jgit.lib.Constants;
21 import org.eclipse.jgit.transport.Transport;
22 import org.eclipse.jgit.transport.TransportProtocol;
23 import org.eclipse.jgit.transport.URIish;
25 /**
26 * Utility class for checking strings for being valid git URLs, and for
27 * sanitizing arbitrary string input.
29 public abstract class GitUrlChecker {
31 private static final String GIT_CLONE_COMMAND_PREFIX = "git clone "; //$NON-NLS-1$
33 /**
34 * Checks if the incoming string is a valid git URL. It is recommended to
35 * {@link #sanitizeAsGitUrl(String) sanitize} the String first if coming
36 * from an untrustworthy source.
38 * @param url
39 * to check
40 * @return {@code true} if the {@code url} is a valid git URL, {@code false}
41 * otherwise
43 public static boolean isValidGitUrl(String url) {
44 try {
45 if (url != null) {
46 URIish u = new URIish(url);
47 if (canHandleProtocol(u)) {
48 if (Protocol.GIT.handles(u) || Protocol.SSH.handles(u)
49 || (Protocol.HTTP.handles(u)
50 || Protocol.HTTPS.handles(u))
51 && KnownHosts.isKnownHost(u.getHost())
52 || url.endsWith(Constants.DOT_GIT_EXT)) {
53 return true;
57 } catch (URISyntaxException e) {
58 // Ignore. This is used to check arbitrary input for being a
59 // possibly valid git URL; we don't want to flood the log here.
61 return false;
64 /**
65 * Sanitize a string for use as a git URL. Strips the Git Clone command if
66 * needed and reduces remaining the input to anything before the first
67 * whitespace.
69 * @param input
70 * String to be sanitized
71 * @return sanitized string; if the input came from an untrustworthy source,
72 * is should still be checked using {@link #isValidGitUrl(String)}
73 * before being used for real as a git URL
75 public static String sanitizeAsGitUrl(String input) {
76 String sanitized = input.trim();
77 if (sanitized.startsWith(GIT_CLONE_COMMAND_PREFIX)) {
78 sanitized = sanitized.substring(GIT_CLONE_COMMAND_PREFIX.length())
79 .trim();
81 // For file URLs, take everything up to the first vertical space
82 try {
83 URIish uri = new URIish(sanitized);
84 if (Protocol.FILE.handles(uri)) {
85 return sanitized.split("\\v", 2)[0]; //$NON-NLS-1$
87 } catch (URISyntaxException e) {
88 // Ignore here; error will be reported later where this method is
89 // used.
91 // Take only the part up to the first whitespace character
92 return sanitized.split("[\\h|\\v]", 2)[0]; //$NON-NLS-1$
95 private static boolean canHandleProtocol(URIish u) {
96 for (TransportProtocol proto : Transport.getTransportProtocols()) {
97 if (proto.canHandle(u)) {
98 return true;
101 return false;