gclient: Search also for URL + '.git' when looking for dependencies.

The URLs for the dependencies in DEPS files might end in '.git', so
search for them too.

R=agable@chromium.org

Bug: 643346
Change-Id: I955c2750179b54fec26423f5123b3fa7eea38d96
Reviewed-on: https://chromium-review.googlesource.com/987630
Commit-Queue: Edward Lesmes <ehmaldonado@chromium.org>
Reviewed-by: Aaron Gable <agable@chromium.org>
diff --git a/gclient.py b/gclient.py
index b6cea2b..b5ee7ba 100755
--- a/gclient.py
+++ b/gclient.py
@@ -934,6 +934,38 @@
           bad_deps.append(dep)
     return bad_deps
 
+  def FuzzyMatchUrl(self, parsed_url, candidates):
+    """Attempts to find this dependency in the list of candidates.
+
+    It looks first for the URL of this dependency (parsed_url) in the list of
+    candidates. If it doesn't succeed, and the URL ends in '.git', it will try
+    looking for the URL minus '.git'. Finally it will try to look for the name
+    of the dependency.
+
+    Args:
+      parsed_url: str. The parsed URL for this dependency. Something like
+          "https://example.com/src.git@revision"
+      candidates: list, dict. The list of candidates in which to look for this
+          dependency. It can contain URLs as above, or dependency names like
+          "src/some/dep".
+
+    Returns:
+      If this dependency is not found in the list of candidates, returns None.
+      Otherwise, it returns under which name did we find this dependency:
+       - Its parsed url: "https://example.com/src.git'
+       - Its parsed url minus '.git': "https://example.com/src"
+       - Its name: "src"
+    """
+    if parsed_url:
+      origin, _ = gclient_utils.SplitUrlRevision(parsed_url)
+      if origin in candidates:
+        return origin
+      if origin.endswith('.git') and origin[:-len('.git')] in candidates:
+        return origin[:-len('.git')]
+    if self.name in candidates:
+      return self.name
+    return None
+
   # Arguments number differs from overridden method
   # pylint: disable=arguments-differ
   def run(self, revision_overrides, command, args, work_queue, options,
@@ -950,10 +982,8 @@
         'flatten', 'runhooks', 'recurse', 'validate', None)
     parsed_url = self.LateOverride(self.url)
     file_list = [] if not options.nohooks else None
-    revision_override = revision_overrides.pop(self.name, None)
-    if parsed_url:
-      revision_override = revision_overrides.pop(
-          parsed_url.split('@')[0], revision_override)
+    revision_override = revision_overrides.pop(
+        self.FuzzyMatchUrl(parsed_url, revision_overrides), None)
     if run_scm and parsed_url:
       # Create a shallow copy to mutate revision.
       options = copy.copy(options)
@@ -966,10 +996,11 @@
                                                      file_list)
 
       patch_repo = parsed_url.split('@')[0]
-      patch_ref = patch_refs.pop(patch_repo, patch_refs.pop(self.name, None))
+      patch_ref = patch_refs.pop(
+          self.FuzzyMatchUrl(parsed_url, patch_refs), None)
       if command == 'update' and patch_ref is not None:
         self._used_scm.apply_patch_ref(patch_repo, patch_ref, options,
-                                        file_list)
+                                       file_list)
 
       if file_list:
         file_list = [os.path.join(self.name, f.strip()) for f in file_list]
@@ -1795,14 +1826,9 @@
         work_queue.enqueue(s)
     work_queue.flush({}, None, [], options=self._options, patch_refs=None)
 
-    def ShouldPrintRevision(path, rev):
-      if not self._options.path and not self._options.url:
-        return True
-      if self._options.path and path in self._options.path:
-        return True
-      if self._options.url and rev and rev.split('@')[0] in self._options.url:
-        return True
-      return False
+    def ShouldPrintRevision(dep, rev):
+      return (not self._options.filter
+              or d.FuzzyMatchUrl(rev, self._options.filter))
 
     def GetURLAndRev(dep):
       """Returns the revision-qualified SCM url for a Dependency."""
@@ -1824,7 +1850,7 @@
           """Recursively grab dependencies."""
           for d in dep.dependencies:
             rev = GetURLAndRev(d)
-            if ShouldPrintRevision(d.name, rev):
+            if ShouldPrintRevision(d, rev):
               entries[d.name] = rev
             GrabDeps(d)
         GrabDeps(d)
@@ -1852,7 +1878,7 @@
           rev = GetURLAndRev(d)
         else:
           rev = d.parsed_url
-        if ShouldPrintRevision(d.name, rev):
+        if ShouldPrintRevision(d, rev):
           entries[d.name] = rev
       if self._options.output_json:
         json_output = {
@@ -2896,12 +2922,9 @@
                     help='creates a snapshot .gclient file of the current '
                          'version of all repositories to reproduce the tree, '
                          'implies -a')
-  parser.add_option('-u', '--url', action='append',
+  parser.add_option('--filter', action='append', dest='filter',
                      help='Display revision information only for the specified '
-                          'URLs.')
-  parser.add_option('-p', '--path', action='append',
-                     help='Display revision information only for the specified '
-                          'paths.')
+                          'dependencies (filtered by URL or path).')
   parser.add_option('--output-json',
                     help='Output a json document to this path containing '
                          'information about the revisions.')