authorgravatar for topolarity@tapscott.meCody Tapscott <topolarity@tapscott.me> 2022-03-23 23:05:33-07:00
committergravatar for topolarity@tapscott.meCody Tapscott <topolarity@tapscott.me> 2022-03-25 12:27:46-07:00
log1de63ad79331aec9f53a7a9d95069fd0ca5f66f4
tree5644ee454239ef76cb51fea58d8678673f1a6ead
parent7f64f7c9259ba5f67adb386ba55473d4b2b74607

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.

2 files changed, 21 insertions(+), 1 deletions(-)

ci/zinc/linux_test.sh+1-1
...@@ -45,7 +45,7 @@ cd $WORKSPACE...@@ -45,7 +45,7 @@ cd $WORKSPACE
4545
46# Look for non-conforming code formatting.46# Look for non-conforming code formatting.
47# Formatting errors can be fixed by running `zig fmt` on the files printed here.47# Formatting errors can be fixed by running `zig fmt` on the files printed here.
48$ZIG fmt --check .48$ZIG fmt --check . --exclude test/compile_errors/
4949
50# Build stage2 standalone so that we can test stage2 against stage2 compiler-rt.50# Build stage2 standalone so that we can test stage2 against stage2 compiler-rt.
51$ZIG build -p stage2 -Denable-llvm -Duse-zig-libcxx51$ZIG build -p stage2 -Denable-llvm -Duse-zig-libcxx
src/main.zig+20
...@@ -3792,6 +3792,7 @@ pub const usage_fmt =...@@ -3792,6 +3792,7 @@ pub const usage_fmt =
3792 \\ --check List non-conforming files and exit with an error3792 \\ --check List non-conforming files and exit with an error
3793 \\ if the list is non-empty3793 \\ if the list is non-empty
3794 \\ --ast-check Run zig ast-check on every file3794 \\ --ast-check Run zig ast-check on every file
3795 \\ --exclude [file] Exclude file or directory from formatting
3795 \\3796 \\
3796 \\3797 \\
3797;3798;
...@@ -3815,6 +3816,8 @@ pub fn cmdFmt(gpa: Allocator, arena: Allocator, args: []const []const u8) !void...@@ -3815,6 +3816,8 @@ pub fn cmdFmt(gpa: Allocator, arena: Allocator, args: []const []const u8) !void
3815 var check_ast_flag: bool = false;3816 var check_ast_flag: bool = false;
3816 var input_files = ArrayList([]const u8).init(gpa);3817 var input_files = ArrayList([]const u8).init(gpa);
3817 defer input_files.deinit();3818 defer input_files.deinit();
3819 var excluded_files = ArrayList([]const u8).init(gpa);
3820 defer excluded_files.deinit();
38183821
3819 {3822 {
3820 var i: usize = 0;3823 var i: usize = 0;
...@@ -3840,6 +3843,13 @@ pub fn cmdFmt(gpa: Allocator, arena: Allocator, args: []const []const u8) !void...@@ -3840,6 +3843,13 @@ pub fn cmdFmt(gpa: Allocator, arena: Allocator, args: []const []const u8) !void
3840 check_flag = true;3843 check_flag = true;
3841 } else if (mem.eql(u8, arg, "--ast-check")) {3844 } else if (mem.eql(u8, arg, "--ast-check")) {
3842 check_ast_flag = true;3845 check_ast_flag = true;
3846 } else if (mem.eql(u8, arg, "--exclude")) {
3847 if (i + 1 >= args.len) {
3848 fatal("expected parameter after --exclude", .{});
3849 }
3850 i += 1;
3851 const next_arg = args[i];
3852 try excluded_files.append(next_arg);
3843 } else {3853 } else {
3844 fatal("unrecognized parameter: '{s}'", .{arg});3854 fatal("unrecognized parameter: '{s}'", .{arg});
3845 }3855 }
...@@ -3940,6 +3950,16 @@ pub fn cmdFmt(gpa: Allocator, arena: Allocator, args: []const []const u8) !void...@@ -3940,6 +3950,16 @@ pub fn cmdFmt(gpa: Allocator, arena: Allocator, args: []const []const u8) !void
3940 defer fmt.seen.deinit();3950 defer fmt.seen.deinit();
3941 defer fmt.out_buffer.deinit();3951 defer fmt.out_buffer.deinit();
39423952
3953 // Mark any excluded files/directories as already seen,
3954 // so that they are skipped later during actual processing
3955 for (excluded_files.items) |file_path| {
3956 var dir = try fs.cwd().openDir(file_path, .{});
3957 defer dir.close();
3958
3959 const stat = try dir.stat();
3960 try fmt.seen.put(stat.inode, {});
3961 }
3962
3943 for (input_files.items) |file_path| {3963 for (input_files.items) |file_path| {
3944 try fmtPath(&fmt, file_path, check_flag, fs.cwd(), file_path);3964 try fmtPath(&fmt, file_path, check_flag, fs.cwd(), file_path);
3945 }3965 }