View Javadoc
1   /*
2    * Copyright (C) 2009, Mykola Nikishov <mn@mn.com.ua>
3    * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
4    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
5    * Copyright (C) 2010, Christian Halstrick <christian.halstrick@sap.com>
6    * Copyright (C) 2013, Robin Stocker <robin@nibor.org>
7    * Copyright (C) 2015, Patrick Steinhardt <ps@pks.im> and others
8    *
9    * This program and the accompanying materials are made available under the
10   * terms of the Eclipse Distribution License v. 1.0 which is available at
11   * https://www.eclipse.org/org/documents/edl-v10.php.
12   *
13   * SPDX-License-Identifier: BSD-3-Clause
14   */
15  
16  package org.eclipse.jgit.transport;
17  
18  import static java.nio.charset.StandardCharsets.UTF_8;
19  
20  import java.io.ByteArrayOutputStream;
21  import java.io.File;
22  import java.io.Serializable;
23  import java.net.URISyntaxException;
24  import java.net.URL;
25  import java.util.BitSet;
26  import java.util.regex.Matcher;
27  import java.util.regex.Pattern;
28  
29  import org.eclipse.jgit.internal.JGitText;
30  import org.eclipse.jgit.lib.Constants;
31  import org.eclipse.jgit.util.RawParseUtils;
32  import org.eclipse.jgit.util.References;
33  import org.eclipse.jgit.util.StringUtils;
34  
35  /**
36   * This URI like construct used for referencing Git archives over the net, as
37   * well as locally stored archives. It is similar to RFC 2396 URI's, but also
38   * support SCP and the malformed file://&lt;path&gt; syntax (as opposed to the correct
39   * file:&lt;path&gt; syntax.
40   */
41  public class URIish implements Serializable {
42  	/**
43  	 * Part of a pattern which matches the scheme part (git, http, ...) of an
44  	 * URI. Defines one capturing group containing the scheme without the
45  	 * trailing colon and slashes
46  	 */
47  	private static final String SCHEME_P = "([a-z][a-z0-9+-]+)://"; //$NON-NLS-1$
48  
49  	/**
50  	 * Part of a pattern which matches the optional user/password part (e.g.
51  	 * root:pwd@ in git://root:pwd@host.xyz/a.git) of URIs. Defines two
52  	 * capturing groups: the first containing the user and the second containing
53  	 * the password
54  	 */
55  	private static final String OPT_USER_PWD_P = "(?:([^/:]+)(?::([^\\\\/]+))?@)?"; //$NON-NLS-1$
56  
57  	/**
58  	 * Part of a pattern which matches the host part of URIs. Defines one
59  	 * capturing group containing the host name.
60  	 */
61  	private static final String HOST_P = "((?:[^\\\\/:]+)|(?:\\[[0-9a-f:]+\\]))"; //$NON-NLS-1$
62  
63  	/**
64  	 * Part of a pattern which matches the optional port part of URIs. Defines
65  	 * one capturing group containing the port without the preceding colon.
66  	 */
67  	private static final String OPT_PORT_P = "(?::(\\d*))?"; //$NON-NLS-1$
68  
69  	/**
70  	 * Part of a pattern which matches the ~username part (e.g. /~root in
71  	 * git://host.xyz/~root/a.git) of URIs. Defines no capturing group.
72  	 */
73  	private static final String USER_HOME_P = "(?:/~(?:[^\\\\/]+))"; //$NON-NLS-1$
74  
75  	/**
76  	 * Part of a pattern which matches the optional drive letter in paths (e.g.
77  	 * D: in file:///D:/a.txt). Defines no capturing group.
78  	 */
79  	private static final String OPT_DRIVE_LETTER_P = "(?:[A-Za-z]:)?"; //$NON-NLS-1$
80  
81  	/**
82  	 * Part of a pattern which matches a relative path. Relative paths don't
83  	 * start with slash or drive letters. Defines no capturing group.
84  	 */
85  	private static final String RELATIVE_PATH_P = "(?:(?:[^\\\\/]+[\\\\/]+)*[^\\\\/]+[\\\\/]*)"; //$NON-NLS-1$
86  
87  	/**
88  	 * Part of a pattern which matches a relative or absolute path. Defines no
89  	 * capturing group.
90  	 */
91  	private static final String PATH_P = "(" + OPT_DRIVE_LETTER_P + "[\\\\/]?" //$NON-NLS-1$ //$NON-NLS-2$
92  			+ RELATIVE_PATH_P + ")"; //$NON-NLS-1$
93  
94  	private static final long serialVersionUID = 1L;
95  
96  	/**
97  	 * A pattern matching standard URI: </br>
98  	 * <code>scheme "://" user_password? hostname? portnumber? path</code>
99  	 */
100 	private static final Pattern FULL_URI = Pattern.compile("^" // //$NON-NLS-1$
101 			+ SCHEME_P //
102 			+ "(?:" // start a group containing hostname and all options only //$NON-NLS-1$
103 					// availabe when a hostname is there
104 			+ OPT_USER_PWD_P //
105 			+ HOST_P //
106 			+ OPT_PORT_P //
107 			+ "(" // open a group capturing the user-home-dir-part //$NON-NLS-1$
108 			+ (USER_HOME_P + "?") //$NON-NLS-1$
109 			+ "(?:" // start non capturing group for host //$NON-NLS-1$
110 					// separator or end of line
111 			+ "[\\\\/])|$" //$NON-NLS-1$
112 			+ ")" // close non capturing group for the host//$NON-NLS-1$
113 					// separator or end of line
114 			+ ")?" // close the optional group containing hostname //$NON-NLS-1$
115 			+ "(.+)?" //$NON-NLS-1$
116 			+ "$"); //$NON-NLS-1$
117 
118 	/**
119 	 * A pattern matching the reference to a local file. This may be an absolute
120 	 * path (maybe even containing windows drive-letters) or a relative path.
121 	 */
122 	private static final Pattern LOCAL_FILE = Pattern.compile("^" // //$NON-NLS-1$
123 			+ "([\\\\/]?" + PATH_P + ")" // //$NON-NLS-1$ //$NON-NLS-2$
124 			+ "$"); //$NON-NLS-1$
125 
126 	/**
127 	 * A pattern matching a URI for the scheme 'file' which has only ':/' as
128 	 * separator between scheme and path. Standard file URIs have '://' as
129 	 * separator, but java.io.File.toURI() constructs those URIs.
130 	 */
131 	private static final Pattern SINGLE_SLASH_FILE_URI = Pattern.compile("^" // //$NON-NLS-1$
132 			+ "(file):([\\\\/](?![\\\\/])" // //$NON-NLS-1$
133 			+ PATH_P //
134 			+ ")$"); //$NON-NLS-1$
135 
136 	/**
137 	 * A pattern matching a SCP URI's of the form user@host:path/to/repo.git
138 	 */
139 	private static final Pattern RELATIVE_SCP_URI = Pattern.compile("^" // //$NON-NLS-1$
140 			+ OPT_USER_PWD_P //
141 			+ HOST_P //
142 			+ ":(" // //$NON-NLS-1$
143 			+ ("(?:" + USER_HOME_P + "[\\\\/])?") // //$NON-NLS-1$ //$NON-NLS-2$
144 			+ RELATIVE_PATH_P //
145 			+ ")$"); //$NON-NLS-1$
146 
147 	/**
148 	 * A pattern matching a SCP URI's of the form user@host:/path/to/repo.git
149 	 */
150 	private static final Pattern ABSOLUTE_SCP_URI = Pattern.compile("^" // //$NON-NLS-1$
151 			+ OPT_USER_PWD_P //
152 			+ "([^\\\\/:]{2,})" // //$NON-NLS-1$
153 			+ ":(" // //$NON-NLS-1$
154 			+ "[\\\\/]" + RELATIVE_PATH_P // //$NON-NLS-1$
155 			+ ")$"); //$NON-NLS-1$
156 
157 	private String scheme;
158 
159 	private String path;
160 
161 	private String rawPath;
162 
163 	private String user;
164 
165 	private String pass;
166 
167 	private int port = -1;
168 
169 	private String host;
170 
171 	/**
172 	 * Parse and construct an {@link org.eclipse.jgit.transport.URIish} from a
173 	 * string
174 	 *
175 	 * @param s
176 	 *            a {@link java.lang.String} object.
177 	 * @throws java.net.URISyntaxException
178 	 */
179 	public URIish(String s) throws URISyntaxException {
180 		if (StringUtils.isEmptyOrNull(s)) {
181 			throw new URISyntaxException("The uri was empty or null", //$NON-NLS-1$
182 					JGitText.get().cannotParseGitURIish);
183 		}
184 		Matcher matcher = SINGLE_SLASH_FILE_URI.matcher(s);
185 		if (matcher.matches()) {
186 			scheme = matcher.group(1);
187 			rawPath = cleanLeadingSlashes(matcher.group(2), scheme);
188 			path = unescape(rawPath);
189 			return;
190 		}
191 		matcher = FULL_URI.matcher(s);
192 		if (matcher.matches()) {
193 			scheme = matcher.group(1);
194 			user = unescape(matcher.group(2));
195 			pass = unescape(matcher.group(3));
196 			// empty ports are in general allowed, except for URLs like
197 			// file://D:/path for which it is more desirable to parse with
198 			// host=null and path=D:/path
199 			String portString = matcher.group(5);
200 			if ("file".equals(scheme) && "".equals(portString)) { //$NON-NLS-1$ //$NON-NLS-2$
201 				rawPath = cleanLeadingSlashes(
202 						n2e(matcher.group(4)) + ":" + portString //$NON-NLS-1$
203 								+ n2e(matcher.group(6)) + n2e(matcher.group(7)),
204 						scheme);
205 			} else {
206 				host = unescape(matcher.group(4));
207 				if (portString != null && portString.length() > 0) {
208 					port = Integer.parseInt(portString);
209 				}
210 				rawPath = cleanLeadingSlashes(
211 						n2e(matcher.group(6)) + n2e(matcher.group(7)), scheme);
212 			}
213 			path = unescape(rawPath);
214 			return;
215 		}
216 		matcher = RELATIVE_SCP_URI.matcher(s);
217 		if (matcher.matches()) {
218 			user = matcher.group(1);
219 			pass = matcher.group(2);
220 			host = matcher.group(3);
221 			rawPath = matcher.group(4);
222 			path = rawPath;
223 			return;
224 		}
225 		matcher = ABSOLUTE_SCP_URI.matcher(s);
226 		if (matcher.matches()) {
227 			user = matcher.group(1);
228 			pass = matcher.group(2);
229 			host = matcher.group(3);
230 			rawPath = matcher.group(4);
231 			path = rawPath;
232 			return;
233 		}
234 		matcher = LOCAL_FILE.matcher(s);
235 		if (matcher.matches()) {
236 			rawPath = matcher.group(1);
237 			path = rawPath;
238 			return;
239 		}
240 		throw new URISyntaxException(s, JGitText.get().cannotParseGitURIish);
241 	}
242 
243 	private static int parseHexByte(byte c1, byte c2) {
244 			return ((RawParseUtils.parseHexInt4(c1) << 4)
245 					| RawParseUtils.parseHexInt4(c2));
246 	}
247 
248 	private static String unescape(String s) throws URISyntaxException {
249 		if (s == null)
250 			return null;
251 		if (s.indexOf('%') < 0)
252 			return s;
253 
254 		byte[] bytes = s.getBytes(UTF_8);
255 
256 		byte[] os = new byte[bytes.length];
257 		int j = 0;
258 		for (int i = 0; i < bytes.length; ++i) {
259 			byte c = bytes[i];
260 			if (c == '%') {
261 				if (i + 2 >= bytes.length)
262 					throw new URISyntaxException(s, JGitText.get().cannotParseGitURIish);
263 				byte c1 = bytes[i + 1];
264 				byte c2 = bytes[i + 2];
265 				int val;
266 				try {
267 					val = parseHexByte(c1, c2);
268 				} catch (ArrayIndexOutOfBoundsException e) {
269 					URISyntaxException use = new URISyntaxException(s,
270 							JGitText.get().cannotParseGitURIish);
271 					use.initCause(e);
272 					throw use;
273 				}
274 				os[j++] = (byte) val;
275 				i += 2;
276 			} else
277 				os[j++] = c;
278 		}
279 		return RawParseUtils.decode(os, 0, j);
280 	}
281 
282 	private static final BitSet reservedChars = new BitSet(127);
283 
284 	static {
285 		for (byte b : Constants.encodeASCII("!*'();:@&=+$,/?#[]")) //$NON-NLS-1$
286 			reservedChars.set(b);
287 	}
288 
289 	/**
290 	 * Escape unprintable characters optionally URI-reserved characters
291 	 *
292 	 * @param s
293 	 *            The Java String to encode (may contain any character)
294 	 * @param escapeReservedChars
295 	 *            true to escape URI reserved characters
296 	 * @param encodeNonAscii
297 	 *            encode any non-ASCII characters
298 	 * @return a URI-encoded string
299 	 */
300 	private static String escape(String s, boolean escapeReservedChars,
301 			boolean encodeNonAscii) {
302 		if (s == null)
303 			return null;
304 		ByteArrayOutputStream os = new ByteArrayOutputStream(s.length());
305 		byte[] bytes = s.getBytes(UTF_8);
306 		for (byte c : bytes) {
307 			int b = c & 0xFF;
308 			if (b <= 32 || (encodeNonAscii && b > 127) || b == '%'
309 					|| (escapeReservedChars && reservedChars.get(b))) {
310 				os.write('%');
311 				byte[] tmp = Constants.encodeASCII(String.format("%02x", //$NON-NLS-1$
312 						Integer.valueOf(b)));
313 				os.write(tmp[0]);
314 				os.write(tmp[1]);
315 			} else {
316 				os.write(b);
317 			}
318 		}
319 		byte[] buf = os.toByteArray();
320 		return RawParseUtils.decode(buf, 0, buf.length);
321 	}
322 
323 	private String n2e(String s) {
324 		return s == null ? "" : s; //$NON-NLS-1$
325 	}
326 
327 	// takes care to cut of a leading slash if a windows drive letter or a
328 	// user-home-dir specifications are
329 	private String cleanLeadingSlashes(String p, String s) {
330 		if (p.length() >= 3
331 				&& p.charAt(0) == '/'
332 				&& p.charAt(2) == ':'
333 				&& ((p.charAt(1) >= 'A' && p.charAt(1) <= 'Z')
334 						|| (p.charAt(1) >= 'a' && p.charAt(1) <= 'z')))
335 			return p.substring(1);
336 		else if (s != null && p.length() >= 2 && p.charAt(0) == '/'
337 				&& p.charAt(1) == '~')
338 			return p.substring(1);
339 		else
340 			return p;
341 	}
342 
343 	/**
344 	 * Construct a URIish from a standard URL.
345 	 *
346 	 * @param u
347 	 *            the source URL to convert from.
348 	 */
349 	public URIish(URL u) {
350 		scheme = u.getProtocol();
351 		path = u.getPath();
352 		path = cleanLeadingSlashes(path, scheme);
353 		try {
354 			rawPath = u.toURI().getRawPath();
355 			rawPath = cleanLeadingSlashes(rawPath, scheme);
356 		} catch (URISyntaxException e) {
357 			throw new RuntimeException(e); // Impossible
358 		}
359 
360 		final String ui = u.getUserInfo();
361 		if (ui != null) {
362 			final int d = ui.indexOf(':');
363 			user = d < 0 ? ui : ui.substring(0, d);
364 			pass = d < 0 ? null : ui.substring(d + 1);
365 		}
366 
367 		port = u.getPort();
368 		host = u.getHost();
369 	}
370 
371 	/**
372 	 * Create an empty, non-configured URI.
373 	 */
374 	public URIish() {
375 		// Configure nothing.
376 	}
377 
378 	private URIishsh" href="../../../../org/eclipse/jgit/transport/URIish.html#URIish">URIish(URIish u) {
379 		this.scheme = u.scheme;
380 		this.rawPath = u.rawPath;
381 		this.path = u.path;
382 		this.user = u.user;
383 		this.pass = u.pass;
384 		this.port = u.port;
385 		this.host = u.host;
386 	}
387 
388 	/**
389 	 * Whether this URI references a repository on another system.
390 	 *
391 	 * @return true if this URI references a repository on another system.
392 	 */
393 	public boolean isRemote() {
394 		return getHost() != null;
395 	}
396 
397 	/**
398 	 * Get host name part.
399 	 *
400 	 * @return host name part or null
401 	 */
402 	public String getHost() {
403 		return host;
404 	}
405 
406 	/**
407 	 * Return a new URI matching this one, but with a different host.
408 	 *
409 	 * @param n
410 	 *            the new value for host.
411 	 * @return a new URI with the updated value.
412 	 */
413 	public URIish setHost(String n) {
414 		final URIishort/URIish.html#URIish">URIish r = new URIish(this);
415 		r.host = n;
416 		return r;
417 	}
418 
419 	/**
420 	 * Get protocol name
421 	 *
422 	 * @return protocol name or null for local references
423 	 */
424 	public String getScheme() {
425 		return scheme;
426 	}
427 
428 	/**
429 	 * Return a new URI matching this one, but with a different scheme.
430 	 *
431 	 * @param n
432 	 *            the new value for scheme.
433 	 * @return a new URI with the updated value.
434 	 */
435 	public URIish setScheme(String n) {
436 		final URIishort/URIish.html#URIish">URIish r = new URIish(this);
437 		r.scheme = n;
438 		return r;
439 	}
440 
441 	/**
442 	 * Get path name component
443 	 *
444 	 * @return path name component
445 	 */
446 	public String getPath() {
447 		return path;
448 	}
449 
450 	/**
451 	 * Get path name component
452 	 *
453 	 * @return path name component
454 	 */
455 	public String getRawPath() {
456 		return rawPath;
457 	}
458 
459 	/**
460 	 * Return a new URI matching this one, but with a different path.
461 	 *
462 	 * @param n
463 	 *            the new value for path.
464 	 * @return a new URI with the updated value.
465 	 */
466 	public URIish setPath(String n) {
467 		final URIishort/URIish.html#URIish">URIish r = new URIish(this);
468 		r.path = n;
469 		r.rawPath = n;
470 		return r;
471 	}
472 
473 	/**
474 	 * Return a new URI matching this one, but with a different (raw) path.
475 	 *
476 	 * @param n
477 	 *            the new value for path.
478 	 * @return a new URI with the updated value.
479 	 * @throws java.net.URISyntaxException
480 	 */
481 	public URIish setRawPath(String n) throws URISyntaxException {
482 		final URIishort/URIish.html#URIish">URIish r = new URIish(this);
483 		r.path = unescape(n);
484 		r.rawPath = n;
485 		return r;
486 	}
487 
488 	/**
489 	 * Get user name requested for transfer
490 	 *
491 	 * @return user name requested for transfer or null
492 	 */
493 	public String getUser() {
494 		return user;
495 	}
496 
497 	/**
498 	 * Return a new URI matching this one, but with a different user.
499 	 *
500 	 * @param n
501 	 *            the new value for user.
502 	 * @return a new URI with the updated value.
503 	 */
504 	public URIish setUser(String n) {
505 		final URIishort/URIish.html#URIish">URIish r = new URIish(this);
506 		r.user = n;
507 		return r;
508 	}
509 
510 	/**
511 	 * Get password requested for transfer
512 	 *
513 	 * @return password requested for transfer or null
514 	 */
515 	public String getPass() {
516 		return pass;
517 	}
518 
519 	/**
520 	 * Return a new URI matching this one, but with a different password.
521 	 *
522 	 * @param n
523 	 *            the new value for password.
524 	 * @return a new URI with the updated value.
525 	 */
526 	public URIish setPass(String n) {
527 		final URIishort/URIish.html#URIish">URIish r = new URIish(this);
528 		r.pass = n;
529 		return r;
530 	}
531 
532 	/**
533 	 * Get port number requested for transfer or -1 if not explicit
534 	 *
535 	 * @return port number requested for transfer or -1 if not explicit
536 	 */
537 	public int getPort() {
538 		return port;
539 	}
540 
541 	/**
542 	 * Return a new URI matching this one, but with a different port.
543 	 *
544 	 * @param n
545 	 *            the new value for port.
546 	 * @return a new URI with the updated value.
547 	 */
548 	public URIish setPort(int n) {
549 		final URIishort/URIish.html#URIish">URIish r = new URIish(this);
550 		r.port = n > 0 ? n : -1;
551 		return r;
552 	}
553 
554 	/** {@inheritDoc} */
555 	@Override
556 	public int hashCode() {
557 		int hc = 0;
558 		if (getScheme() != null)
559 			hc = hc * 31 + getScheme().hashCode();
560 		if (getUser() != null)
561 			hc = hc * 31 + getUser().hashCode();
562 		if (getPass() != null)
563 			hc = hc * 31 + getPass().hashCode();
564 		if (getHost() != null)
565 			hc = hc * 31 + getHost().hashCode();
566 		if (getPort() > 0)
567 			hc = hc * 31 + getPort();
568 		if (getPath() != null)
569 			hc = hc * 31 + getPath().hashCode();
570 		return hc;
571 	}
572 
573 	/** {@inheritDoc} */
574 	@Override
575 	public boolean equals(Object obj) {
576 		if (!(obj instanceof URIish))
577 			return false;
578 		final URIishref="../../../../org/eclipse/jgit/transport/URIish.html#URIish">URIish b = (URIish) obj;
579 		if (!eq(getScheme(), b.getScheme()))
580 			return false;
581 		if (!eq(getUser(), b.getUser()))
582 			return false;
583 		if (!eq(getPass(), b.getPass()))
584 			return false;
585 		if (!eq(getHost(), b.getHost()))
586 			return false;
587 		if (getPort() != b.getPort())
588 			return false;
589 		if (!eq(getPath(), b.getPath()))
590 			return false;
591 		return true;
592 	}
593 
594 	private static boolean eq(String a, String b) {
595 		if (References.isSameObject(a, b)) {
596 			return true;
597 		}
598 		if (StringUtils.isEmptyOrNull(a) && StringUtils.isEmptyOrNull(b))
599 			return true;
600 		if (a == null || b == null)
601 			return false;
602 		return a.equals(b);
603 	}
604 
605 	/**
606 	 * Obtain the string form of the URI, with the password included.
607 	 *
608 	 * @return the URI, including its password field, if any.
609 	 */
610 	public String toPrivateString() {
611 		return format(true, false);
612 	}
613 
614 	/** {@inheritDoc} */
615 	@Override
616 	public String toString() {
617 		return format(false, false);
618 	}
619 
620 	private String format(boolean includePassword, boolean escapeNonAscii) {
621 		final StringBuilder r = new StringBuilder();
622 		if (getScheme() != null) {
623 			r.append(getScheme());
624 			r.append("://"); //$NON-NLS-1$
625 		}
626 
627 		if (getUser() != null) {
628 			r.append(escape(getUser(), true, escapeNonAscii));
629 			if (includePassword && getPass() != null) {
630 				r.append(':');
631 				r.append(escape(getPass(), true, escapeNonAscii));
632 			}
633 		}
634 
635 		if (getHost() != null) {
636 			if (getUser() != null && getUser().length() > 0)
637 				r.append('@');
638 			r.append(escape(getHost(), false, escapeNonAscii));
639 			if (getScheme() != null && getPort() > 0) {
640 				r.append(':');
641 				r.append(getPort());
642 			}
643 		}
644 
645 		if (getPath() != null) {
646 			if (getScheme() != null) {
647 				if (!getPath().startsWith("/") && !getPath().isEmpty()) //$NON-NLS-1$
648 					r.append('/');
649 			} else if (getHost() != null)
650 				r.append(':');
651 			if (getScheme() != null)
652 				if (escapeNonAscii)
653 					r.append(escape(getPath(), false, escapeNonAscii));
654 				else
655 					r.append(getRawPath());
656 			else
657 				r.append(getPath());
658 		}
659 
660 		return r.toString();
661 	}
662 
663 	/**
664 	 * Get the URI as an ASCII string.
665 	 *
666 	 * @return the URI as an ASCII string. Password is not included.
667 	 */
668 	public String toASCIIString() {
669 		return format(false, true);
670 	}
671 
672 	/**
673 	 * Convert the URI including password, formatted with only ASCII characters
674 	 * such that it will be valid for use over the network.
675 	 *
676 	 * @return the URI including password, formatted with only ASCII characters
677 	 *         such that it will be valid for use over the network.
678 	 */
679 	public String toPrivateASCIIString() {
680 		return format(true, true);
681 	}
682 
683 	/**
684 	 * Get the "humanish" part of the path. Some examples of a 'humanish' part
685 	 * for a full path:
686 	 * <table summary="path vs humanish path" border="1">
687 	 * <tr>
688 	 * <th>Path</th>
689 	 * <th>Humanish part</th>
690 	 * </tr>
691 	 * <tr>
692 	 * <td><code>/path/to/repo.git</code></td>
693 	 * <td rowspan="4"><code>repo</code></td>
694 	 * </tr>
695 	 * <tr>
696 	 * <td><code>/path/to/repo.git/</code></td>
697 	 * </tr>
698 	 * <tr>
699 	 * <td><code>/path/to/repo/.git</code></td>
700 	 * </tr>
701 	 * <tr>
702 	 * <td><code>/path/to/repo/</code></td>
703 	 * </tr>
704 	 * <tr>
705 	 * <td><code>localhost</code></td>
706 	 * <td><code>ssh://localhost/</code></td>
707 	 * </tr>
708 	 * <tr>
709 	 * <td><code>/path//to</code></td>
710 	 * <td>an empty string</td>
711 	 * </tr>
712 	 * </table>
713 	 *
714 	 * @return the "humanish" part of the path. May be an empty string. Never
715 	 *         {@code null}.
716 	 * @throws java.lang.IllegalArgumentException
717 	 *             if it's impossible to determine a humanish part, or path is
718 	 *             {@code null} or empty
719 	 * @see #getPath
720 	 */
721 	public String getHumanishName() throws IllegalArgumentException {
722 		String s = getPath();
723 		if ("/".equals(s) || "".equals(s)) //$NON-NLS-1$ //$NON-NLS-2$
724 			s = getHost();
725 		if (s == null) // $NON-NLS-1$
726 			throw new IllegalArgumentException();
727 
728 		String[] elements;
729 		if ("file".equals(scheme) || LOCAL_FILE.matcher(s).matches()) //$NON-NLS-1$
730 			elements = s.split("[\\" + File.separatorChar + "/]"); //$NON-NLS-1$ //$NON-NLS-2$
731 		else
732 			elements = s.split("/+"); //$NON-NLS-1$
733 		if (elements.length == 0)
734 			throw new IllegalArgumentException();
735 		String result = elements[elements.length - 1];
736 		if (Constants.DOT_GIT.equals(result))
737 			result = elements[elements.length - 2];
738 		else if (result.endsWith(Constants.DOT_GIT_EXT))
739 			result = result.substring(0, result.length()
740 					- Constants.DOT_GIT_EXT.length());
741 		if (("file".equals(scheme) || LOCAL_FILE.matcher(s) //$NON-NLS-1$
742 				.matches())
743 				&& result.endsWith(Constants.DOT_BUNDLE_EXT)) {
744 			result = result.substring(0,
745 					result.length() - Constants.DOT_BUNDLE_EXT.length());
746 		}
747 		return result;
748 	}
749 
750 }