Skip to content

Commit

Permalink
Clarify why the max(key=len) logic works
Browse files Browse the repository at this point in the history
  • Loading branch information
uranusjr committed Jan 14, 2020
1 parent 85654a9 commit 62ffc7b
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/pip/_internal/vcs/versioncontrol.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,20 +232,24 @@ def get_backend_for_dir(self, location):
Return a VersionControl object if a repository of that type is found
at the given directory.
"""
candidates = {}
vcs_backends = {}
for vcs_backend in self._registry.values():
root = vcs_backend.get_repository_root(location)
if not root:
repo_path = vcs_backend.get_repository_root(location)
if not repo_path:
continue
logger.debug('Determine that %s uses VCS: %s',
location, vcs_backend.name)
candidates[root] = vcs_backend
vcs_backends[repo_path] = vcs_backend

# Choose the VCS in the inner-most directory (i.e. path to root
# is longest).
if not candidates:
if not vcs_backends:
return None
return candidates[max(candidates, key=len)]

# Choose the VCS in the inner-most directory. Since all repository
# roots found here would be either ``location``` or one of its
# parents, the longest path should have the most path components,
# i.e. the backend representing the inner-most repository.
inner_most_repo_path = max(vcs_backends, key=len)
return vcs_backends[inner_most_repo_path]

def get_backend_for_scheme(self, scheme):
# type: (str) -> Optional[VersionControl]
Expand Down

0 comments on commit 62ffc7b

Please sign in to comment.