Improve codereview.settings file lookup.

Remove unnecessary os.listdir call and fix infinite loop on Windows if
no file was found in a tree.

Change-Id: I82a8763e807bbc0ce6fcae6b35a370ffe3b34943
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/tools/depot_tools/+/3687234
Reviewed-by: Josip Sokcevic <sokcevic@google.com>
Commit-Queue: Josip Sokcevic <sokcevic@google.com>
Auto-Submit: Aleksey Khoroshilov <akhoroshilov@brave.com>
diff --git a/git_cl.py b/git_cl.py
index 806dad2..c22c682 100755
--- a/git_cl.py
+++ b/git_cl.py
@@ -3003,14 +3003,18 @@
   cwd = os.getcwd()
   root = settings.GetRoot()
   if os.path.isfile(os.path.join(root, inherit_ok_file)):
-    root = '/'
+    root = None
   while True:
-    if filename in os.listdir(cwd):
-      if os.path.isfile(os.path.join(cwd, filename)):
-        return open(os.path.join(cwd, filename))
+    if os.path.isfile(os.path.join(cwd, filename)):
+      return open(os.path.join(cwd, filename))
     if cwd == root:
       break
-    cwd = os.path.dirname(cwd)
+    parent_dir = os.path.dirname(cwd)
+    if parent_dir == cwd:
+      # We hit the system root directory.
+      break
+    cwd = parent_dir
+  return None
 
 
 def LoadCodereviewSettingsFromFile(fileobj):