summaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit
diff options
context:
space:
mode:
authorRobin Stocker <robin@nibor.org>2013-01-12 15:20:32 +0100
committerMatthias Sohn <matthias.sohn@sap.com>2013-02-06 08:48:54 +0100
commit60d538fe5142dc1ce547a77391e4142c4fb8a251 (patch)
tree1e53171fe47cf4f018acf920c4049870993aecdd /org.eclipse.jgit
parent767be14f34b07e5acb8f332032fd97dd3e19a059 (diff)
downloadjgit-60d538fe5142dc1ce547a77391e4142c4fb8a251.tar.gz
jgit-60d538fe5142dc1ce547a77391e4142c4fb8a251.zip
Add getConflictingNames to RefDatabase
This has the same logic as isNameConflicting, but instead of only returning a boolean, it returns a collection of names that conflict. It will be used in EGit to provide a better message to the user when validating a ref name, see Ibea9984121ae88c488858b8a8e73b593195b15e0. Existing implementations of isNameConflicting could be rewritten like this: return !getConflictingNames(name).isEmpty(); But I'm not sure about that, as isNameConflicting can be implemented in a faster way than getConflictingNames. Change-Id: I11e0ba2f300adb8b3612943c304ba68bbe73db8a Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
Diffstat (limited to 'org.eclipse.jgit')
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/lib/RefDatabase.java41
1 files changed, 40 insertions, 1 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/RefDatabase.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/RefDatabase.java
index 7e7b779bc9..2ee63f18ea 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/RefDatabase.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/RefDatabase.java
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2010, Google Inc.
+ * Copyright (C) 2010, 2013 Google Inc.
* and other copyright owners as documented in the project's IP log.
*
* This program and the accompanying materials are made available
@@ -44,6 +44,9 @@
package org.eclipse.jgit.lib;
import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
import java.util.List;
import java.util.Map;
@@ -111,10 +114,46 @@ public abstract class RefDatabase {
* using this name right now would be safe.
* @throws IOException
* the database could not be read to check for conflicts.
+ * @see #getConflictingNames(String)
*/
public abstract boolean isNameConflicting(String name) throws IOException;
/**
+ * Determine if a proposed reference cannot coexist with existing ones. If
+ * the passed name already exists, it's not considered a conflict.
+ *
+ * @param name
+ * proposed name to check for conflicts against
+ * @return a collection of full names of existing refs which would conflict
+ * with the passed ref name; empty collection when there are no
+ * conflicts
+ * @throws IOException
+ * @since 2.3
+ * @see #isNameConflicting(String)
+ */
+ public Collection<String> getConflictingNames(String name)
+ throws IOException {
+ Map<String, Ref> allRefs = getRefs(ALL);
+ // Cannot be nested within an existing reference.
+ int lastSlash = name.lastIndexOf('/');
+ while (0 < lastSlash) {
+ String needle = name.substring(0, lastSlash);
+ if (allRefs.containsKey(needle))
+ return Collections.singletonList(needle);
+ lastSlash = name.lastIndexOf('/', lastSlash - 1);
+ }
+
+ List<String> conflicting = new ArrayList<String>();
+ // Cannot be the container of an existing reference.
+ String prefix = name + '/';
+ for (String existing : allRefs.keySet())
+ if (existing.startsWith(prefix))
+ conflicting.add(existing);
+
+ return conflicting;
+ }
+
+ /**
* Create a new update command to create, modify or delete a reference.
*
* @param name