Skip to content

Commit

Permalink
✨ Add commit functionality and refactor generator command in CLI app
Browse files Browse the repository at this point in the history
  • Loading branch information
segersniels committed Apr 5, 2024
1 parent e78d242 commit 88bfd43
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
9 changes: 9 additions & 0 deletions apps/cli/src/git.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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,
} });
}
31 changes: 27 additions & 4 deletions apps/cli/src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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", .{});
Expand All @@ -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,
};
Expand Down

0 comments on commit 88bfd43

Please sign in to comment.