Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,19 @@ pub struct Args {
help = "Set custom line width for text wrapping (default: terminal width)"
)]
pub wrap_width: Option<usize>,

#[clap(
short = 'c',
long = "commit",
help = "Automatically run git commit after generating the message"
)]
pub commit: bool,

#[clap(
long = "commit-args",
help = "Extra arguments to pass to git commit (can be specified multiple times)",
num_args = 1,
allow_hyphen_values = true,
)]
pub commit_args: Vec<String>,
}
8 changes: 8 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ pub struct Config {
/// Text wrapping configuration
#[serde(default)]
pub text_wrap: TextWrapConfig,
/// Automatically run git commit after generating the message
#[serde(default)]
pub auto_commit: bool,
/// Extra arguments to pass to git commit
#[serde(default)]
pub commit_args: Vec<String>,
}

impl Config {
Expand Down Expand Up @@ -175,6 +181,8 @@ impl Default for Config {
sanitize_secrets: true,
custom_sanitize_patterns: Vec::new(),
text_wrap: TextWrapConfig::default(),
auto_commit: false,
commit_args: Vec::new(),
}
}
}
Expand Down
21 changes: 21 additions & 0 deletions src/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,24 @@ pub async fn generate_both(args: &cli::Args, config: &Config) -> anyhow::Result<
let commit_message = generate_commit_message(&diff, config, args.prompt.as_deref()).await?;
Ok((branch_name, commit_message))
}

/// 执行 git commit,将生成的 message 作为 commit message
pub fn execute_git_commit(message: &str, extra_args: &[String]) -> anyhow::Result<()> {
let mut cmd = Command::new("git");
cmd.args(["commit", "-m", message]);
for arg in extra_args {
cmd.arg(arg);
}
let output = cmd.output()?;
if output.status.success() {
let stdout = String::from_utf8_lossy(&output.stdout);
if !stdout.trim().is_empty() {
eprintln!("{}", stdout.trim());
}
eprintln!("\x1b[32mSuccessfully committed!\x1b[0m");
Ok(())
} else {
let stderr = String::from_utf8_lossy(&output.stderr);
Err(anyhow::anyhow!("git commit failed:\n{}", stderr.trim()))
}
}
14 changes: 14 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ async fn main() -> anyhow::Result<()> {
config.sanitize_secrets = false;
}

// 合并 auto_commit 参数
let auto_commit = args.commit || config.auto_commit;
let commit_args = if args.commit_args.is_empty() {
&config.commit_args
} else {
&args.commit_args
};

// 确定是否启用文本包装 (CLI 参数优先级高于配置)
let enable_wrapping = !args.no_wrap && config.text_wrap.enabled;

Expand Down Expand Up @@ -68,6 +76,9 @@ async fn main() -> anyhow::Result<()> {
spinner.finish();
print_wrapped_content(&wrapper, &branch_name, Some("Generated branch name:"));
print_wrapped_content(&commit_wrapper, &msg, None);
if auto_commit {
generate::execute_git_commit(&msg, commit_args)?;
}
} else if args.generate_branch {
// 仅生成分支名
let branch_name = generate::generate_branch(&args, &config).await?;
Expand All @@ -78,6 +89,9 @@ async fn main() -> anyhow::Result<()> {
let msg = generate::generate(&args, &config).await?;
spinner.finish();
print_wrapped_content(&commit_wrapper, &msg, None);
if auto_commit {
generate::execute_git_commit(&msg, commit_args)?;
}
}
Ok(())
}
Expand Down