build: gn_check_autofix should retain blank lines, comments and # keep tagged lines

Bug: webrtc:467294026
Change-Id: I613ef701e678563a3f9e41e7a008b42540137916
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/456141
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Reviewed-by: Jeremy Leconte <jleconte@google.com>
Commit-Queue: Philipp Hancke <philipp.hancke@googlemail.com>
Cr-Commit-Position: refs/heads/main@{#47139}
diff --git a/api/BUILD.gn b/api/BUILD.gn
index 8c62fe2..9df13cc 100644
--- a/api/BUILD.gn
+++ b/api/BUILD.gn
@@ -662,10 +662,10 @@
     # Basically, don't add stuff here. You might break sensitive downstream
     # targets like pnacl. API should not depend on anything outside of this
     # file, really. All these should arguably go away in time.
-    "../media:rtc_media_config",
-    "../rtc_base:checks",
-    "../rtc_base:threading",
-    "../rtc_base/system:rtc_export",
+    "../media:rtc_media_config",  # keep
+    "../rtc_base:checks",  # keep
+    "../rtc_base:threading",  # keep
+    "../rtc_base/system:rtc_export",  # keep
   ]
 }
 
diff --git a/tools_webrtc/gn_check_autofix.py b/tools_webrtc/gn_check_autofix.py
index 99d57df..0732f4f 100755
--- a/tools_webrtc/gn_check_autofix.py
+++ b/tools_webrtc/gn_check_autofix.py
@@ -166,20 +166,25 @@
 
 def _update_deps(new_lines, current_deps_lines_content, block_type):
     """Helper to finalize and append a processed deps block to new_lines."""
-    deps_to_keep = []
-    for dep_line in current_deps_lines_content:
-        # Keep absolute dependencies.
-        deps_to_keep.extend(re.findall(r'"(//[^"]*)"', dep_line))
-        # Keep test targets.
-        deps_to_keep.extend(re.findall(r'"([^"]*_unittests)"', dep_line))
-
     if block_type == 'deps_set':
         new_lines.append('deps = [\n')
     elif block_type == 'deps_add':
         new_lines.append('deps += [\n')
-
-    for dep in deps_to_keep:
-        new_lines.append('"' + dep + '",\n')
+    for dep_line in current_deps_lines_content:
+        line = dep_line.strip()
+        # Keep blank lines (inserted for formatting), comments and
+        # lines tagged with "  # keep".
+        if not line or line.startswith('#') or line.endswith(' # keep'):
+            new_lines.append(dep_line)
+            continue
+        deps_to_keep = []
+        # Keep absolute dependencies.
+        deps_to_keep.extend(re.findall(r'"(//[^"]*)"', dep_line))
+        # Keep _unittest and _unittests target.
+        deps_to_keep.extend(
+            re.findall(r'"([^"]*[_:](?:unit)?tests?)"', dep_line))
+        for dep in deps_to_keep:
+            new_lines.append('"' + dep + '",\n')
     new_lines.append(']\n')