Skip to content

Commit 023f816

Browse files
committed
feat: fix
1 parent 0e19b0e commit 023f816

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

src/main.zig

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -96,21 +96,36 @@ pub fn main() !void {
9696
defer if (needs_to_free_custom_type) allocator.free(custom_type);
9797

9898
// Execute git commands using process.Child
99-
// Add files to staging
100-
var add_cmd_args = if (path_arg) |path|
101-
[_][]const u8{ "git", "add", path }
102-
else
103-
[_][]const u8{ "git", "add", "." };
99+
// First check if there are any staged files
100+
var has_staged_files = false;
101+
{
102+
var diff_cmd_args = [_][]const u8{ "git", "diff", "--cached", "--quiet" };
103+
var diff_cmd = std.process.Child.init(&diff_cmd_args, allocator);
104+
diff_cmd.stderr_behavior = .Ignore;
105+
diff_cmd.stdout_behavior = .Ignore;
106+
107+
const diff_term = try diff_cmd.spawnAndWait();
108+
// Exit code 1 means there are staged changes, 0 means no staged changes
109+
has_staged_files = diff_term.Exited == 1;
110+
}
104111

105-
var add_cmd = std.process.Child.init(&add_cmd_args, allocator);
106-
add_cmd.stderr_behavior = .Inherit;
107-
add_cmd.stdout_behavior = .Inherit;
112+
// Only add files if there are no staged files
113+
if (!has_staged_files) {
114+
var add_cmd_args = if (path_arg) |path|
115+
[_][]const u8{ "git", "add", path }
116+
else
117+
[_][]const u8{ "git", "add", "." };
108118

109-
const add_term = try add_cmd.spawnAndWait();
119+
var add_cmd = std.process.Child.init(&add_cmd_args, allocator);
120+
add_cmd.stderr_behavior = .Inherit;
121+
add_cmd.stdout_behavior = .Inherit;
110122

111-
if (add_term.Exited != 0) {
112-
try out.writeAll("Error adding files to git\n");
113-
return;
123+
const add_term = try add_cmd.spawnAndWait();
124+
125+
if (add_term.Exited != 0) {
126+
try out.writeAll("Error adding files to git\n");
127+
return;
128+
}
114129
}
115130

116131
// Create commit

0 commit comments

Comments
 (0)