Introduce remoteNameStyle = basenameOnly

In order to replicate repositories below a certain namespace (e.g. foo/bar/),
the projects parameter allows to filter repositories (projects = foo/bar/*).

With the current implementation, the path on the target server contains the full
path to these repositories (foo/bar/myrepo), which can only be modified in terms
of replacing slashes with dash or underscore using the remoteNameStyle option.

This patch adds a new value "basenameOnly", which only takes the basename of the
repository path (e.g. myrepo when applied to foo/bar/myrepo), which, for example
enables Gerrit user to replicate whole namespaces to a special Github
organization.

Change-Id: Id12780948a4841c054eb76e359f02dae0816d576
diff --git a/BUCK b/BUCK
index 6610330..7c1da50 100644
--- a/BUCK
+++ b/BUCK
@@ -5,7 +5,10 @@
   manifest_entries = [
     'Gerrit-Module: com.googlesource.gerrit.plugins.replication.ReplicationModule',
     'Gerrit-SshModule: com.googlesource.gerrit.plugins.replication.SshModule'
-  ]
+  ],
+  deps = [
+    '//lib/commons:io',
+  ],
 )
 
 java_test(
diff --git a/pom.xml b/pom.xml
index 695a73e..da069fa 100644
--- a/pom.xml
+++ b/pom.xml
@@ -82,6 +82,12 @@
       <version>4.8.1</version>
       <scope>test</scope>
     </dependency>
+
+    <dependency>
+      <groupId>commons-io</groupId>
+      <artifactId>commons-io</artifactId>
+      <version>2.4</version>
+    </dependency>
   </dependencies>
 
   <repositories>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/Destination.java b/src/main/java/com/googlesource/gerrit/plugins/replication/Destination.java
index fe5969c..4271dc9 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/replication/Destination.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/replication/Destination.java
@@ -44,6 +44,7 @@
 import com.google.inject.assistedinject.FactoryModuleBuilder;
 import com.google.inject.servlet.RequestScoped;
 
+import org.apache.commons.io.FilenameUtils;
 import org.eclipse.jgit.lib.Config;
 import org.eclipse.jgit.lib.Constants;
 import org.eclipse.jgit.lib.Ref;
@@ -429,6 +430,8 @@
           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")) {
             ReplicationQueue.log.debug(String.format(
                 "Unknown remoteNameStyle: %s, falling back to slash", remoteNameStyle));
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md
index acc1ec2..09ef7f6 100644
--- a/src/main/resources/Documentation/config.md
+++ b/src/main/resources/Documentation/config.md
@@ -203,16 +203,22 @@
 	By default, false, do not remove remote branches.
 
 remote.NAME.remoteNameStyle
-:	Slashes in the `${name}` placeholder are replaced with either
-	dashes or underscores.
+:	Provides possibilities to influence the name of the target
+	repository, e.g. by replacing slashes in the `${name}`
+	placeholder.
 
 	Github and Gitorious do not permit slashes "/" in repository
 	names and changes this to dashes "-" at repository creation
-	time. If set to "dash," this changes slashes to dashes in the
+	time. If set to "dash", this changes slashes to dashes in the
 	repository name. If set to "underscore", this changes slashes
 	to underscores in the repository name.
 
-	By default, "slash," remote name will contain slashes as they
+	Option "basenameOnly" makes `${name}` to be only the basename
+	(the part after the last slash) of the repository path on the
+	Gerrit server, e.g. `${name}` of `foo/bar/my-repo.git` would
+	be `my-repo`.
+
+	By default, "slash" remote name will contain slashes as they
 	do in Gerrit.
 
 <a name="remote.NAME.projects">remote.NAME.projects</a>