Blob Blame History Raw
From 8a0e94a64e0716f6f91ce835a50f59b62c3b1da1 Mon Sep 17 00:00:00 2001
From: Georgy Dyuldin <g.dyuldin@gmail.com>
Date: Fri, 25 Apr 2014 13:26:33 -0700
Subject: [PATCH 7/8] Switch to fuzzy search in setup-repo to match repository
 path.

Sometimes repository path on reviewboard server and on local copy of repository
may contains slight difference (trailing slash, user credentials, etc.) Now rbt
setup-repo offers repository with closest path to local path.

Testing done: Ran unit tests.

Reviewed at https://reviews.reviewboard.org/r/5724/
---
 rbtools/commands/setup_repo.py | 38 ++++++++++++++++++++++++--------------
 1 file changed, 24 insertions(+), 14 deletions(-)

diff --git a/rbtools/commands/setup_repo.py b/rbtools/commands/setup_repo.py
index 7cd988c3e789e87ffce5f6e7b143d747e233bdac..46b3b32d4c188b8eef53b747e47cbd8ba6310fe2 100644
--- a/rbtools/commands/setup_repo.py
+++ b/rbtools/commands/setup_repo.py
@@ -1,8 +1,9 @@
 import os
 
-from rbtools.commands import Command, CommandError, Option
+from rbtools.commands import Command, CommandError
 from rbtools.utils.console import confirm
 from rbtools.utils.filesystem import CONFIG_FILE
+import difflib
 
 
 class SetupRepo(Command):
@@ -28,6 +29,7 @@ class SetupRepo(Command):
     args = ""
     option_list = [
         Command.server_options,
+        Command.perforce_options,
     ]
 
     def prompt_rb_repository(self, tool_name, repository_info, api_root):
@@ -42,19 +44,27 @@ class SetupRepo(Command):
         # selection is made, immediately return the selected repo.
         try:
             while True:
-                for repo in repositories:
-                    is_match = (
-                        tool_name == repo.tool and
-                        repository_info.path in
-                        (repo['path'], getattr(repo, 'mirror_path', '')))
-
-                    if is_match:
-                        question = (
-                            "Use the %s repository '%s' (%s)?"
-                            % (tool_name, repo['name'], repo['path']))
-
-                        if confirm(question):
-                            return repo
+                repo_paths = {}
+                for repository in repositories:
+                    if repository.tool != tool_name:
+                        continue
+
+                    repo_paths[repository['path']] = repository
+                    if 'mirror_path' in repository:
+                        repo_paths[repository['mirror_path']] = repository
+
+                closest_path = difflib.get_close_matches(repository_info.path,
+                                                         repo_paths.keys(),
+                                                         n=1)
+
+                for path in closest_path:
+                    repo = repo_paths[path]
+                    question = (
+                        "Use the %s repository '%s' (%s)?"
+                        % (tool_name, repo['name'], repo['path']))
+
+                    if confirm(question):
+                        return repo
 
                 repositories = repositories.get_next()
         except StopIteration:
-- 
1.9.0