From 16038ce2adf5f06f75230150f75d110a804414c1 Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Sat, 22 Oct 2011 09:48:17 -0400
Subject: [PATCH] Fixed collision error on rename repository or user

---
 src/com/gitblit/GitBlit.java                     |   18 ++++++++++++++++++
 docs/04_releases.mkd                             |    1 +
 src/com/gitblit/client/GitblitPanel.java         |    2 ++
 src/com/gitblit/client/EditRepositoryDialog.java |   18 ++++++++++++++++--
 src/com/gitblit/client/EditUserDialog.java       |   13 +++++++++++++
 docs/00_index.mkd                                |    1 +
 6 files changed, 51 insertions(+), 2 deletions(-)

diff --git a/docs/00_index.mkd b/docs/00_index.mkd
index 6a2d0d1..f34fd55 100644
--- a/docs/00_index.mkd
+++ b/docs/00_index.mkd
@@ -40,6 +40,7 @@
 - added: Gitblit Manager (Java/Swing Application) for remote administration of a Gitblit server.
 - fixed: federation protocol timestamps.  dates are now serialized to the [iso8601](http://en.wikipedia.org/wiki/ISO_8601) standard.  
     **This breaks 0.6.0 federation clients/servers.**
+- fixed: collision on rename for repositories and users
 - fixed: Gitblit can now browse the Linux kernel repository (issue 25)
 - fixed: Gitblit now runs on Servlet 3.0 webservers (e.g. Tomcat 7, Jetty 8) (issue 23)
 - fixed: Set the RSS content type for Firefox 4 (issue 22)
diff --git a/docs/04_releases.mkd b/docs/04_releases.mkd
index ba09075..4b538ba 100644
--- a/docs/04_releases.mkd
+++ b/docs/04_releases.mkd
@@ -14,6 +14,7 @@
 - added: Gitblit Manager (Java/Swing Application) for remote administration of a Gitblit server.
 - fixed: federation protocol timestamps.  dates are now serialized to the [iso8601](http://en.wikipedia.org/wiki/ISO_8601) standard.  
     **This breaks 0.6.0 federation clients/servers.**
+- fixed: collision on rename for repositories and users
 - fixed: Gitblit can now browse the Linux kernel repository (issue 25)
 - fixed: Gitblit now runs on Servlet 3.0 webservers (e.g. Tomcat 7, Jetty 8) (issue 23)
 - fixed: Set the RSS content type for Firefox 4 (issue 22)
diff --git a/src/com/gitblit/GitBlit.java b/src/com/gitblit/GitBlit.java
index eaf6a95..6f537ec 100644
--- a/src/com/gitblit/GitBlit.java
+++ b/src/com/gitblit/GitBlit.java
@@ -440,6 +440,14 @@
 	 */
 	public void updateUserModel(String username, UserModel user, boolean isCreate)
 			throws GitBlitException {
+		if (!username.equalsIgnoreCase(user.username)) {
+			if (userService.getUserModel(user.username) != null) {
+				throw new GitBlitException(
+						MessageFormat
+								.format("Failed to rename ''{0}'' because ''{1}'' already exists.",
+										username, user.username));
+			}
+		}
 		if (!userService.updateUserModel(username, user)) {
 			throw new GitBlitException(isCreate ? "Failed to add user!" : "Failed to update user!");
 		}
@@ -722,6 +730,16 @@
 		} else {
 			// rename repository
 			if (!repositoryName.equalsIgnoreCase(repository.name)) {
+				if (!repository.name.toLowerCase().endsWith(
+						org.eclipse.jgit.lib.Constants.DOT_GIT_EXT)) {
+					repository.name += org.eclipse.jgit.lib.Constants.DOT_GIT_EXT;
+				}
+				if (new File(repositoriesFolder, repository.name).exists()) {
+					throw new GitBlitException(
+							MessageFormat
+									.format("Failed to rename ''{0}'' because ''{1}'' already exists.",
+											repositoryName, repository.name));
+				}
 				closeRepository(repositoryName);
 				File folder = new File(repositoriesFolder, repositoryName);
 				File destFolder = new File(repositoriesFolder, repository.name);
diff --git a/src/com/gitblit/client/EditRepositoryDialog.java b/src/com/gitblit/client/EditRepositoryDialog.java
index 93330cd..c2d8f67 100644
--- a/src/com/gitblit/client/EditRepositoryDialog.java
+++ b/src/com/gitblit/client/EditRepositoryDialog.java
@@ -63,6 +63,8 @@
 
 	private static final long serialVersionUID = 1L;
 
+	private final String repositoryName;
+
 	private final RepositoryModel repository;
 
 	private boolean isCreate;
@@ -105,6 +107,7 @@
 
 	public EditRepositoryDialog(RepositoryModel aRepository) {
 		super();
+		this.repositoryName = aRepository.name;
 		this.repository = new RepositoryModel();
 		this.repositoryNames = new HashSet<String>();
 		this.isCreate = false;
@@ -114,7 +117,7 @@
 		setTitle(Translation.get("gb.edit") + ": " + aRepository.name);
 		setIconImage(new ImageIcon(getClass().getResource("/gitblt-favicon.png")).getImage());
 	}
-	
+
 	@Override
 	protected JRootPane createRootPane() {
 		KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
@@ -175,7 +178,8 @@
 		fieldsPanel
 				.add(newFieldPanel(Translation.get("gb.showRemoteBranches"), showRemoteBranches));
 		fieldsPanel.add(newFieldPanel(Translation.get("gb.showReadme"), showReadme));
-		fieldsPanel.add(newFieldPanel(Translation.get("gb.skipSizeCalculation"), skipSizeCalculation));
+		fieldsPanel.add(newFieldPanel(Translation.get("gb.skipSizeCalculation"),
+				skipSizeCalculation));
 		fieldsPanel.add(newFieldPanel(Translation.get("gb.isFrozen"), isFrozen));
 
 		usersPalette = new JPalette<String>();
@@ -292,6 +296,16 @@
 						"Can not create repository ''{0}'' because it already exists.", rname));
 				return false;
 			}
+		} else {
+			// check rename collision
+			if (!repositoryName.equalsIgnoreCase(rname)) {
+				if (repositoryNames.contains(rname.toLowerCase())) {
+					error(MessageFormat.format(
+							"Failed to rename ''{0}'' because ''{1}'' already exists.",
+							repositoryName, rname));
+					return false;
+				}
+			}
 		}
 
 		if (accessRestriction.getSelectedItem() == null) {
diff --git a/src/com/gitblit/client/EditUserDialog.java b/src/com/gitblit/client/EditUserDialog.java
index 988f8fb..c77713a 100644
--- a/src/com/gitblit/client/EditUserDialog.java
+++ b/src/com/gitblit/client/EditUserDialog.java
@@ -55,6 +55,8 @@
 
 	private static final long serialVersionUID = 1L;
 
+	private final String username;
+
 	private final UserModel user;
 
 	private final ServerSettings settings;
@@ -85,6 +87,7 @@
 
 	public EditUserDialog(UserModel anUser, ServerSettings settings) {
 		super();
+		this.username = anUser.username;
 		this.user = new UserModel("");
 		this.settings = settings;
 		this.usernames = new HashSet<String>();
@@ -194,6 +197,16 @@
 				error(MessageFormat.format("Username ''{0}'' is unavailable.", uname));
 				return false;
 			}
+		} else {
+			// check rename collision
+			if (!username.equalsIgnoreCase(uname)) {
+				if (usernames.contains(uname.toLowerCase())) {
+					error(MessageFormat.format(
+							"Failed to rename ''{0}'' because ''{1}'' already exists.", username,
+							uname));
+					return false;
+				}
+			}
 		}
 
 		int minLength = settings.get(Keys.realm.minPasswordLength).getInteger(5);
diff --git a/src/com/gitblit/client/GitblitPanel.java b/src/com/gitblit/client/GitblitPanel.java
index e4c6e76..5737c15 100644
--- a/src/com/gitblit/client/GitblitPanel.java
+++ b/src/com/gitblit/client/GitblitPanel.java
@@ -656,6 +656,7 @@
 		List<String> usernames = gitblit.getUsernames();
 		List<String> members = gitblit.getPermittedUsernames(repository);
 		dialog.setUsers(repository.owner, usernames, members);
+		dialog.setRepositories(gitblit.getRepositories());
 		dialog.setFederationSets(gitblit.getFederationSets(), repository.federationSets);
 		dialog.setVisible(true);
 		final RepositoryModel revisedRepository = dialog.getRepository();
@@ -798,6 +799,7 @@
 	protected void editUser(final UserModel user) {
 		EditUserDialog dialog = new EditUserDialog(user, gitblit.getSettings());
 		dialog.setLocationRelativeTo(GitblitPanel.this);
+		dialog.setUsers(gitblit.getUsers());
 		dialog.setRepositories(gitblit.getRepositories(), new ArrayList<String>(user.repositories));
 		dialog.setVisible(true);
 		final UserModel revisedUser = dialog.getUser();

--
Gitblit v1.9.1