Commit 1de63ad793

Cody Tapscott <topolarity@tapscott.me>
2022-03-24 07:05:33
zig fmt: Add `--exclude` argument to skip dir/file
This change adds a "--exclude" parameter to zig format, which can be used to make sure that it does not process certain files or folders when recursively walking a directory. To do this, we simply piggy-back on the existing "seen" logic in zig fmt and mark these files/folders as seen before processing begins.
1 parent 7f64f7c
Changed files (2)
ci/zinc/linux_test.sh
@@ -45,7 +45,7 @@ cd $WORKSPACE
 
 # Look for non-conforming code formatting.
 # Formatting errors can be fixed by running `zig fmt` on the files printed here.
-$ZIG fmt --check .
+$ZIG fmt --check . --exclude test/compile_errors/
 
 # Build stage2 standalone so that we can test stage2 against stage2 compiler-rt.
 $ZIG build -p stage2 -Denable-llvm -Duse-zig-libcxx
src/main.zig
@@ -3792,6 +3792,7 @@ pub const usage_fmt =
     \\   --check                List non-conforming files and exit with an error
     \\                          if the list is non-empty
     \\   --ast-check            Run zig ast-check on every file
+    \\   --exclude [file]       Exclude file or directory from formatting
     \\
     \\
 ;
@@ -3815,6 +3816,8 @@ pub fn cmdFmt(gpa: Allocator, arena: Allocator, args: []const []const u8) !void
     var check_ast_flag: bool = false;
     var input_files = ArrayList([]const u8).init(gpa);
     defer input_files.deinit();
+    var excluded_files = ArrayList([]const u8).init(gpa);
+    defer excluded_files.deinit();
 
     {
         var i: usize = 0;
@@ -3840,6 +3843,13 @@ pub fn cmdFmt(gpa: Allocator, arena: Allocator, args: []const []const u8) !void
                     check_flag = true;
                 } else if (mem.eql(u8, arg, "--ast-check")) {
                     check_ast_flag = true;
+                } else if (mem.eql(u8, arg, "--exclude")) {
+                    if (i + 1 >= args.len) {
+                        fatal("expected parameter after --exclude", .{});
+                    }
+                    i += 1;
+                    const next_arg = args[i];
+                    try excluded_files.append(next_arg);
                 } else {
                     fatal("unrecognized parameter: '{s}'", .{arg});
                 }
@@ -3940,6 +3950,16 @@ pub fn cmdFmt(gpa: Allocator, arena: Allocator, args: []const []const u8) !void
     defer fmt.seen.deinit();
     defer fmt.out_buffer.deinit();
 
+    // Mark any excluded files/directories as already seen,
+    // so that they are skipped later during actual processing
+    for (excluded_files.items) |file_path| {
+        var dir = try fs.cwd().openDir(file_path, .{});
+        defer dir.close();
+
+        const stat = try dir.stat();
+        try fmt.seen.put(stat.inode, {});
+    }
+
     for (input_files.items) |file_path| {
         try fmtPath(&fmt, file_path, check_flag, fs.cwd(), file_path);
     }