Skip to main content

git_hunk/
cli.rs

1use std::path::PathBuf;
2
3use clap::{Args, Parser, Subcommand, ValueEnum};
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Parser)]
7#[command(name = "git-hunk")]
8#[command(about = "Non-interactive hunk staging for AI agents")]
9pub struct Cli {
10    #[command(subcommand)]
11    pub command: Command,
12}
13
14impl Cli {
15    pub fn json(&self) -> bool {
16        match &self.command {
17            Command::Scan(args) => args.json,
18            Command::Show(args) => args.json,
19            Command::Stage(args) => args.json,
20            Command::Unstage(args) => args.json,
21            Command::Commit(args) => args.json,
22        }
23    }
24}
25
26#[derive(Debug, Subcommand)]
27pub enum Command {
28    Scan(ScanArgs),
29    Show(ShowArgs),
30    Stage(MutateArgs),
31    Unstage(MutateArgs),
32    Commit(CommitArgs),
33}
34
35#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, ValueEnum)]
36#[serde(rename_all = "snake_case")]
37pub enum Mode {
38    Stage,
39    Unstage,
40}
41
42impl Mode {
43    pub fn as_str(self) -> &'static str {
44        match self {
45            Mode::Stage => "stage",
46            Mode::Unstage => "unstage",
47        }
48    }
49}
50
51#[derive(Debug, Args)]
52pub struct ScanArgs {
53    #[arg(long, value_enum)]
54    pub mode: Mode,
55    #[arg(long)]
56    pub json: bool,
57}
58
59#[derive(Debug, Args)]
60pub struct ShowArgs {
61    #[arg(long, value_enum)]
62    pub mode: Mode,
63    pub id: String,
64    #[arg(long)]
65    pub json: bool,
66}
67
68#[derive(Debug, Args)]
69pub struct MutateArgs {
70    #[arg(long)]
71    pub snapshot: Option<String>,
72    #[arg(long)]
73    pub plan: Option<PathBuf>,
74    #[arg(long = "hunk")]
75    pub hunks: Vec<String>,
76    #[arg(long = "change")]
77    pub changes: Vec<String>,
78    #[arg(long)]
79    pub json: bool,
80}
81
82#[derive(Debug, Args)]
83pub struct CommitArgs {
84    #[arg(short = 'm', long = "message", required = true)]
85    pub messages: Vec<String>,
86    #[arg(long)]
87    pub snapshot: Option<String>,
88    #[arg(long)]
89    pub plan: Option<PathBuf>,
90    #[arg(long = "hunk")]
91    pub hunks: Vec<String>,
92    #[arg(long = "change")]
93    pub changes: Vec<String>,
94    #[arg(long)]
95    pub allow_empty: bool,
96    #[arg(long)]
97    pub json: bool,
98}