summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Sohn <matthias.sohn@sap.com>2014-04-27 09:34:02 -0400
committerGerrit Code Review @ Eclipse.org <gerrit@eclipse.org>2014-04-27 09:34:02 -0400
commit8e4018390e0d13c89c3d4ab5b08e3ffa9534fd86 (patch)
tree4eb5d324720546e01320509ac83e9d01bffabcb5
parentca207b938dfca824986d60bcbe6f86b46d07cf97 (diff)
parent36f58ef867fc180aaca20dfa58a1b14f6b79e59a (diff)
downloadjgit-8e4018390e0d13c89c3d4ab5b08e3ffa9534fd86.tar.gz
jgit-8e4018390e0d13c89c3d4ab5b08e3ffa9534fd86.zip
Merge changes I2515a6ed,Ib6b24035
* changes: Fix ArrayIndexOutOfBoundsException in CreateBranchCommand Add methods to Repository to handle remote names
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RefTest.java46
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/api/CreateBranchCommand.java11
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java43
3 files changed, 94 insertions, 6 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RefTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RefTest.java
index 26c4d12cc0..f2ed684511 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RefTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/lib/RefTest.java
@@ -58,9 +58,11 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
+import java.util.TreeSet;
import org.eclipse.jgit.lib.Ref.Storage;
import org.eclipse.jgit.lib.RefUpdate.Result;
+import org.eclipse.jgit.storage.file.FileBasedConfig;
import org.eclipse.jgit.test.resources.SampleDataRepositoryTestCase;
import org.junit.Test;
@@ -83,6 +85,50 @@ public class RefTest extends SampleDataRepositoryTestCase {
}
@Test
+ public void testRemoteNames() throws Exception {
+ FileBasedConfig config = db.getConfig();
+ config.setBoolean(ConfigConstants.CONFIG_REMOTE_SECTION,
+ "origin", "dummy", true);
+ config.setBoolean(ConfigConstants.CONFIG_REMOTE_SECTION,
+ "ab/c", "dummy", true);
+ config.save();
+ assertEquals("[ab/c, origin]",
+ new TreeSet<String>(db.getRemoteNames()).toString());
+
+ // one-level deep remote branch
+ assertEquals("master",
+ db.shortenRemoteBranchName("refs/remotes/origin/master"));
+ assertEquals("origin", db.getRemoteName("refs/remotes/origin/master"));
+
+ // two-level deep remote branch
+ assertEquals("masta/r",
+ db.shortenRemoteBranchName("refs/remotes/origin/masta/r"));
+ assertEquals("origin", db.getRemoteName("refs/remotes/origin/masta/r"));
+
+ // Remote with slash and one-level deep branch name
+ assertEquals("xmaster",
+ db.shortenRemoteBranchName("refs/remotes/ab/c/xmaster"));
+ assertEquals("ab/c", db.getRemoteName("refs/remotes/ab/c/xmaster"));
+
+ // Remote with slash and two-level deep branch name
+ assertEquals("xmasta/r",
+ db.shortenRemoteBranchName("refs/remotes/ab/c/xmasta/r"));
+ assertEquals("ab/c", db.getRemoteName("refs/remotes/ab/c/xmasta/r"));
+
+ // no such remote
+ assertNull(db.getRemoteName("refs/remotes/nosuchremote/x"));
+ assertNull(db.shortenRemoteBranchName("refs/remotes/nosuchremote/x"));
+
+ // no such remote too, no branch name either
+ assertNull(db.getRemoteName("refs/remotes/abranch"));
+ assertNull(db.shortenRemoteBranchName("refs/remotes/abranch"));
+
+ // // local branch
+ assertNull(db.getRemoteName("refs/heads/abranch"));
+ assertNull(db.shortenRemoteBranchName("refs/heads/abranch"));
+ }
+
+ @Test
public void testReadAllIncludingSymrefs() throws Exception {
ObjectId masterId = db.resolve("refs/heads/master");
RefUpdate updateRef = db.updateRef("refs/remotes/origin/master");
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/CreateBranchCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/CreateBranchCommand.java
index aa1484c0f9..92e8b466b0 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/api/CreateBranchCommand.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/CreateBranchCommand.java
@@ -246,12 +246,11 @@ public class CreateBranchCommand extends GitCommand<Ref> {
if (doConfigure) {
StoredConfig config = repo.getConfig();
- String[] tokens = baseBranch.split("/", 4); //$NON-NLS-1$
- boolean isRemote = tokens[1].equals("remotes"); //$NON-NLS-1$
- if (isRemote) {
- // refs/remotes/<remote name>/<branch>
- String remoteName = tokens[2];
- String branchName = tokens[3];
+
+ String remoteName = repo.getRemoteName(baseBranch);
+ if (remoteName != null) {
+ String branchName = repo
+ .shortenRemoteBranchName(baseBranch);
config
.setString(ConfigConstants.CONFIG_BRANCH_SECTION,
name, ConfigConstants.CONFIG_KEY_REMOTE,
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java
index c350dddb25..1538d277b1 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java
@@ -1276,6 +1276,40 @@ public abstract class Repository {
/**
* @param refName
+ * @return the remote branch name part of <code>refName</code>, i.e. without
+ * the <code>refs/remotes/&lt;remote&gt;</code> prefix, if
+ * <code>refName</code> represents a remote tracking branch;
+ * otherwise null.
+ * @since 3.4
+ */
+ public String shortenRemoteBranchName(String refName) {
+ for (String remote : getRemoteNames()) {
+ String remotePrefix = Constants.R_REMOTES + remote + "/"; //$NON-NLS-1$
+ if (refName.startsWith(remotePrefix))
+ return refName.substring(remotePrefix.length());
+ }
+ return null;
+ }
+
+ /**
+ * @param refName
+ * @return the remote name part of <code>refName</code>, i.e. without the
+ * <code>refs/remotes/&lt;remote&gt;</code> prefix, if
+ * <code>refName</code> represents a remote tracking branch;
+ * otherwise null.
+ * @since 3.4
+ */
+ public String getRemoteName(String refName) {
+ for (String remote : getRemoteNames()) {
+ String remotePrefix = Constants.R_REMOTES + remote + "/"; //$NON-NLS-1$
+ if (refName.startsWith(remotePrefix))
+ return remote;
+ }
+ return null;
+ }
+
+ /**
+ * @param refName
* @return a {@link ReflogReader} for the supplied refname, or null if the
* named ref does not exist.
* @throws IOException
@@ -1613,4 +1647,13 @@ public abstract class Repository {
throws IOException {
new RebaseTodoFile(this).writeRebaseTodoFile(path, steps, append);
}
+
+ /**
+ * @return the names of all known remotes
+ * @since 3.4
+ */
+ public Set<String> getRemoteNames() {
+ return getConfig()
+ .getSubsections(ConfigConstants.CONFIG_REMOTE_SECTION);
+ }
}