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
4545
4646# Look for non-conforming code formatting.
4747# 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
5050# Build stage2 standalone so that we can test stage2 against stage2 compiler-rt.
5151$ZIG build -p stage2 -Denable-llvm -Duse-zig-libcxx
src/main.zig+20
......@@ -3792,6 +3792,7 @@ pub const usage_fmt =
37923792 \\ --check List non-conforming files and exit with an error
37933793 \\ if the list is non-empty
37943794 \\ --ast-check Run zig ast-check on every file
3795 \\ --exclude [file] Exclude file or directory from formatting
37953796 \\
37963797 \\
37973798;
......@@ -3815,6 +3816,8 @@ pub fn cmdFmt(gpa: Allocator, arena: Allocator, args: []const []const u8) !void
38153816 var check_ast_flag: bool = false;
38163817 var input_files = ArrayList([]const u8).init(gpa);
38173818 defer input_files.deinit();
3819 var excluded_files = ArrayList([]const u8).init(gpa);
3820 defer excluded_files.deinit();
38183821
38193822 {
38203823 var i: usize = 0;
......@@ -3840,6 +3843,13 @@ pub fn cmdFmt(gpa: Allocator, arena: Allocator, args: []const []const u8) !void
38403843 check_flag = true;
38413844 } else if (mem.eql(u8, arg, "--ast-check")) {
38423845 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);
38433853 } else {
38443854 fatal("unrecognized parameter: '{s}'", .{arg});
38453855 }
......@@ -3940,6 +3950,16 @@ pub fn cmdFmt(gpa: Allocator, arena: Allocator, args: []const []const u8) !void
39403950 defer fmt.seen.deinit();
39413951 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
39433963 for (input_files.items) |file_path| {
39443964 try fmtPath(&fmt, file_path, check_flag, fs.cwd(), file_path);
39453965 }