GN: Correct Git Bash removal of leading "/" of "//" labels

Git bash on Windows will remove the leading slash for "//" labels,
changing absolute labels from //foo to /foo. This CL fixes these labels
back to //foo, file system absolute labels are not "fixed".

This modification by Git Bash may be a bug, but looking at the source,
the removal seems to be intentional.

Change-Id: I16d0ca796ce4c1187717f97ab9c3b1c14a6c6a73
Reviewed-on: https://chromium-review.googlesource.com/957084
Reviewed-by: Dirk Pranke <dpranke@chromium.org>
Commit-Queue: Yngve Pettersen <yngve@vivaldi.com>
Cr-Original-Commit-Position: refs/heads/master@{#544203}
Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src
Cr-Mirrored-Commit: 3325da9a7b57fc48650ada6839c74f96d22f3a7c
diff --git a/tools/gn/commands.cc b/tools/gn/commands.cc
index e9691aa..c47f1b1 100644
--- a/tools/gn/commands.cc
+++ b/tools/gn/commands.cc
@@ -5,8 +5,10 @@
 #include "tools/gn/commands.h"
 
 #include "base/command_line.h"
+#include "base/environment.h"
 #include "base/strings/string_split.h"
 #include "base/values.h"
+#include "build/build_config.h"
 #include "tools/gn/builder.h"
 #include "tools/gn/filesystem_utils.h"
 #include "tools/gn/item.h"
@@ -381,8 +383,22 @@
     Setup* setup,
     const std::string& label_string) {
   // Need to resolve the label after we know the default toolchain.
+  std::string temp_label = label_string;
+#if defined(OS_WIN)
+  // Git bash will remove the first "/" in "//" paths
+  // Fix "//" paths, but not absolute and relative paths
+  std::unique_ptr<base::Environment> env(base::Environment::Create());
+
+  if (env->HasVar("MSYSTEM") && // Only for MinGW based shells like Git Bash
+      temp_label[0] == '/' && // Only fix for //foo paths, not /f:oo paths
+      (temp_label.length() < 2 ||
+        (temp_label[1] != '/' && (temp_label.length() < 3 ||
+                                  temp_label[1] != ':' ))))
+    temp_label.insert(0, "/");
+#endif
+
   Label default_toolchain = setup->loader()->default_toolchain_label();
-  Value arg_value(nullptr, label_string);
+  Value arg_value(nullptr, temp_label);
   Err err;
   Label label = Label::Resolve(SourceDirForCurrentDirectory(
                                    setup->build_settings().root_path()),