diff --git a/apps/cli/src/git.zig b/apps/cli/src/git.zig index 3c86edd..5999590 100644 --- a/apps/cli/src/git.zig +++ b/apps/cli/src/git.zig @@ -9,3 +9,12 @@ pub fn getStagedChanges(allocator: std.mem.Allocator) ![]u8 { return result.stdout; } + +pub fn commit(allocator: std.mem.Allocator, message: []const u8) !void { + _ = try std.process.Child.run(.{ .allocator = allocator, .argv = &[_][]const u8{ + "git", + "commit", + "-m", + message, + } }); +} diff --git a/apps/cli/src/main.zig b/apps/cli/src/main.zig index eb7f22c..4ecc3a6 100644 --- a/apps/cli/src/main.zig +++ b/apps/cli/src/main.zig @@ -17,7 +17,7 @@ var model = cli.Option{ .value_ref = cli.mkRef(&config.model), }; -fn generate() !void { +fn generate_handler() !void { const diff = try git.getStagedChanges(allocator); if (diff.len == 0) { try std.io.getStdOut().writer().print("No changes to commit\n", .{}); @@ -29,13 +29,36 @@ fn generate() !void { try std.io.getStdOut().writer().print("{s}\n", .{response.choices[0].message.content}); } +fn commit_handler() !void { + const diff = try git.getStagedChanges(allocator); + if (diff.len == 0) { + try std.io.getStdOut().writer().print("No changes to commit\n", .{}); + return; + } + + const response = try openai.getCompletion(allocator, diff, config.model); + const message = response.choices[0].message.content; + + try git.commit(allocator, message); +} + +var generate = cli.Command{ + .name = "generate", + .description = cli.Description{ .one_line = "Generate a commit message based on the current staged changes" }, + .target = cli.CommandTarget{ .action = cli.CommandAction{ .exec = generate_handler } }, +}; + +var commit = cli.Command{ + .name = "commit", + .description = cli.Description{ .one_line = "Generate a commit message & commit for your staged changes" }, + .target = cli.CommandTarget{ .action = cli.CommandAction{ .exec = commit_handler } }, +}; + var app = &cli.App{ .command = cli.Command{ .name = "genmoji", .options = &.{&model}, - .target = cli.CommandTarget{ - .action = cli.CommandAction{ .exec = generate }, - }, + .target = cli.CommandTarget{ .subcommands = &.{ &generate, &commit } }, }, .version = package.version, };