Make rebase_path("//foo", "//foo") resolve to "." (not "../foo")

BUG=647679

Review-Url: https://codereview.chromium.org/2346913003
Cr-Original-Commit-Position: refs/heads/master@{#419229}
Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src
Cr-Mirrored-Commit: 3d56c98e60572a809a47918ef98e7b3d9138c5ad
diff --git a/tools/gn/function_rebase_path.cc b/tools/gn/function_rebase_path.cc
index ef5ef40..f3b14bb 100644
--- a/tools/gn/function_rebase_path.cc
+++ b/tools/gn/function_rebase_path.cc
@@ -98,13 +98,23 @@
         scope->settings()->build_settings()->root_path_utf8());
     MakeSlashEndingMatchInput(string_value, &result.string_value());
   } else {
-    result.string_value() = RebasePath(
+    SourceFile resolved_file =
         from_dir.ResolveRelativeFile(value, err,
-            scope->settings()->build_settings()->root_path_utf8()).value(),
-        to_dir,
-        scope->settings()->build_settings()->root_path_utf8());
+            scope->settings()->build_settings()->root_path_utf8());
     if (err->has_error())
       return Value();
+    // Special case:
+    //   rebase_path("//foo", "//bar") ==> "../foo"
+    //   rebase_path("//foo", "//foo") ==> "." and not "../foo"
+    if (resolved_file.value() ==
+        to_dir.value().substr(0, to_dir.value().size() - 1)) {
+      result.string_value() = ".";
+    } else {
+      result.string_value() = RebasePath(
+          resolved_file.value(),
+          to_dir,
+          scope->settings()->build_settings()->root_path_utf8());
+    }
   }
 
   return result;
diff --git a/tools/gn/function_rebase_path_unittest.cc b/tools/gn/function_rebase_path_unittest.cc
index 4b09790..d89b56f 100644
--- a/tools/gn/function_rebase_path_unittest.cc
+++ b/tools/gn/function_rebase_path_unittest.cc
@@ -51,9 +51,7 @@
   EXPECT_EQ("foo/", RebaseOne(scope, "//foo/", "//", "//"));
   EXPECT_EQ("../../foo/bar", RebaseOne(scope, "//foo/bar", "//out/Debug", "."));
   EXPECT_EQ("./", RebaseOne(scope, "//foo/", "//foo/", "//"));
-  // Thie one is technically correct but could be simplified to "." if
-  // necessary.
-  EXPECT_EQ("../foo", RebaseOne(scope, "//foo", "//foo", "//"));
+  EXPECT_EQ(".", RebaseOne(scope, "//foo", "//foo", "//"));
 
   // Test slash conversion.
   EXPECT_EQ("foo/bar", RebaseOne(scope, "foo/bar", ".", "."));