summaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit
diff options
context:
space:
mode:
authorMykola Nikishov <mn@mn.com.ua>2009-12-18 01:03:51 +0200
committerRobin Rosenberg <robin.rosenberg@dewire.com>2009-12-19 00:47:06 +0100
commit21b6f3434ebc84f493405d890a3768d55375a721 (patch)
tree053f332f953824628686addd90b415d9a9650395 /org.eclipse.jgit
parentf8f75f8a52a30cf2a7d793ba15cd74410ba64534 (diff)
downloadjgit-21b6f3434ebc84f493405d890a3768d55375a721.tar.gz
jgit-21b6f3434ebc84f493405d890a3768d55375a721.zip
Method to get a 'humanish' name from a path
Change-Id: Iec0688232bd59d4626111d77633109918e8e1df3 Signed-off-by: Mykola Nikishov <mn@mn.com.ua> Signed-off-by: Robin Rosenberg <robin.rosenberg@dewire.com>
Diffstat (limited to 'org.eclipse.jgit')
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/transport/URIish.java52
1 files changed, 52 insertions, 0 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/URIish.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/URIish.java
index e1df85248e..1f7bad1838 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/URIish.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/URIish.java
@@ -1,4 +1,5 @@
/*
+ * Copyright (C) 2009, Mykola Nikishov <mn@mn.com.ua>
* Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
* Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
* and other copyright owners as documented in the project's IP log.
@@ -56,6 +57,8 @@ import java.util.regex.Pattern;
* any special character is written as-is.
*/
public class URIish {
+ private static final String DOT_GIT = ".git";
+
private static final Pattern FULL_URI = Pattern
.compile("^(?:([a-z][a-z0-9+-]+)://(?:([^/]+?)(?::([^/]+?))?@)?(?:([^/]+?))?(?::(\\d+))?)?((?:[A-Za-z]:)?/.+)$");
@@ -363,4 +366,53 @@ public class URIish {
return r.toString();
}
+
+ /**
+ * Get the "humanish" part of the path. Some examples of a 'humanish' part
+ * for a full path:
+ * <table>
+ * <tr>
+ * <th>Path</th>
+ * <th>Humanish part</th>
+ * </tr>
+ * <tr>
+ * <td><code>/path/to/repo.git</code></td>
+ * <td rowspan="4"><code>repo</code></td>
+ * </tr>
+ * <tr>
+ * <td><code>/path/to/repo.git/</code></td>
+ * </tr>
+ * <tr>
+ * <td><code>/path/to/repo/.git</code></td>
+ * </tr>
+ * <tr>
+ * <td><code>/path/to/repo/</code></td>
+ * </tr>
+ * <tr>
+ * <td><code>/path//to</code></td>
+ * <td>an empty string</td>
+ * </tr>
+ * </table>
+ *
+ * @return the "humanish" part of the path. May be an empty string. Never
+ * {@code null}.
+ * @throws IllegalArgumentException
+ * if it's impossible to determine a humanish part, or path is
+ * {@code null} or empty
+ * @see #getPath
+ */
+ public String getHumanishName() throws IllegalArgumentException {
+ if ("".equals(getPath()) || getPath() == null)
+ throw new IllegalArgumentException();
+ String[] elements = getPath().split("/");
+ if (elements.length == 0)
+ throw new IllegalArgumentException();
+ String result = elements[elements.length - 1];
+ if (DOT_GIT.equals(result))
+ result = elements[elements.length - 2];
+ else if (result.endsWith(DOT_GIT))
+ result = result.substring(0, result.length() - DOT_GIT.length());
+ return result;
+ }
+
}