format: Keep on going in case of error

The previous behavior would stop on the first file in dry-run, or on first
failure otherwise.

Print out the incorrectly formatted files, makes the try jobs more useful.

Bug: 211
Change-Id: Ic64f754ca76a9f0b9bf87a9be02600cca82595a2
Reviewed-on: https://gn-review.googlesource.com/c/gn/+/10520
Reviewed-by: Brett Wilson <brettw@chromium.org>
Commit-Queue: Brett Wilson <brettw@chromium.org>
diff --git a/src/gn/command_format.cc b/src/gn/command_format.cc
index 249c2ae..20b3ccb 100644
--- a/src/gn/command_format.cc
+++ b/src/gn/command_format.cc
@@ -59,9 +59,8 @@
 Arguments
 
   --dry-run
-      Does not change or output anything, but sets the process exit code based
-      on whether output would be different than what's on disk. This is useful
-      for presubmit/lint-type checks.
+      Prints the list of files that would be reformatted but does not write
+      anything to disk. This is useful for presubmit/lint-type checks.
       - Exit code 0: successful format, matches on disk.
       - Exit code 1: general failure (parse error, etc.)
       - Exit code 2: successful format, but differs from on disk.
@@ -1320,12 +1319,14 @@
 
   // TODO(scottmg): Eventually, this list of files should be processed in
   // parallel.
+  int exit_code = 0;
   for (const auto& arg : args) {
     Err err;
     SourceFile file = source_dir.ResolveRelativeFile(Value(nullptr, arg), &err);
     if (err.has_error()) {
       err.PrintToStdout();
-      return 1;
+      exit_code = 1;
+      continue;
     }
 
     base::FilePath to_format = setup.build_settings().GetFullPath(file);
@@ -1334,17 +1335,24 @@
       Err(Location(),
           std::string("Couldn't read \"") + FilePathToUTF8(to_format))
           .PrintToStdout();
-      return 1;
+      exit_code = 1;
+      continue;
     }
 
     std::string output_string;
     if (!FormatStringToString(original_contents, dump_tree, &output_string)) {
-      return 1;
+      exit_code = 1;
+      continue;
     }
     if (dump_tree == TreeDumpMode::kInactive) {
+      if (dry_run) {
+        if (original_contents != output_string) {
+          printf("%s\n", arg.c_str());
+          exit_code = 2;
+        }
+        continue;
+      }
       // Update the file in-place.
-      if (dry_run)
-        return original_contents == output_string ? 0 : 2;
       if (original_contents != output_string) {
         if (base::WriteFile(to_format, output_string.data(),
                             static_cast<int>(output_string.size())) == -1) {
@@ -1352,17 +1360,18 @@
               std::string("Failed to write formatted output back to \"") +
                   FilePathToUTF8(to_format) + std::string("\"."))
               .PrintToStdout();
-          return 1;
+          exit_code = 1;
+          continue;
         }
         if (!quiet) {
           printf("Wrote formatted to '%s'.\n",
-                 FilePathToUTF8(to_format).c_str());
+                FilePathToUTF8(to_format).c_str());
         }
       }
     }
   }
 
-  return 0;
+  return exit_code;
 }
 
 }  // namespace commands