Skip to content
Merged
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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fastcommit"
version = "0.5.2"
version = "0.6.0"
description = "AI-based command line tool to quickly generate standardized commit messages."
edition = "2021"
authors = ["longjin <fslongjin@vip.qq.com>"]
Expand Down
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ You can install `fastcommit` using the following method:

```bash
# Install using cargo
cargo install --git https://github.com/fslongjin/fastcommit --tag v0.5.2
cargo install --git https://github.com/fslongjin/fastcommit --tag v0.6.0
```


Expand Down Expand Up @@ -38,6 +38,8 @@ NOTE: All common config can be configured via `~/.fastcommit/config.toml`
- `-r, --range <RANGE>`: Specify diff range for generating commit message (e.g. HEAD~1, abc123..def456).
- `--no-wrap`: Disable text wrapping for long lines.
- `--wrap-width <WIDTH>`: Set custom line width for text wrapping (default: config file setting or 80).
- `-c, --commit`: Automatically run `git commit` after generating the message.
- `--commit-args <ARG>`: Extra arguments to pass to `git commit` (can be specified multiple times, e.g. `--commit-args "-s" --commit-args "--no-verify"`).
- `-h, --help`: Print help information.
- `-V, --version`: Print version information.

Expand Down Expand Up @@ -102,6 +104,16 @@ NOTE: All common config can be configured via `~/.fastcommit/config.toml`
fastcommit -b -m --wrap-width 100
```

9. Auto-commit after generating the message:

```bash
# Generate and auto-commit
fastcommit -c

# Auto-commit with signoff and skip hooks
fastcommit -c --commit-args "-s" --commit-args "--no-verify"
```

## Contributing

Contributions of code or suggestions are welcome! Please read the [Contributing Guide](CONTRIBUTING.md) first.
Expand Down
14 changes: 13 additions & 1 deletion README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

```bash
# 使用 cargo 安装
cargo install --git https://github.com/fslongjin/fastcommit --tag v0.5.1
cargo install --git https://github.com/fslongjin/fastcommit --tag v0.6.0
```

## 使用
Expand All @@ -35,6 +35,8 @@ NOTE: All common config can be configured via `~/.fastcommit/config.toml`
- `-r, --range <RANGE>`: 指定差异范围以生成提交信息(例如:HEAD~1, abc123..def456)。
- `--no-wrap`: 禁用长行文本换行。
- `--wrap-width <WIDTH>`: 设置文本换行的自定义行宽度(默认:配置文件设置或 80)。
- `-c, --commit`: 生成提交信息后自动执行 `git commit`。
- `--commit-args <ARG>`: 传递给 `git commit` 的额外参数(可多次指定,例如 `--commit-args "-s" --commit-args "--no-verify"`)。
- `-h, --help`: 打印帮助信息。
- `-V, --version`: 打印版本信息。

Expand Down Expand Up @@ -99,6 +101,16 @@ NOTE: All common config can be configured via `~/.fastcommit/config.toml`
fastcommit -b -m --wrap-width 100
```

9. 生成提交信息后自动提交:

```bash
# 生成并自动提交
fastcommit -c

# 自动提交并签名、跳过 hook
fastcommit -c --commit-args "-s" --commit-args "--no-verify"
```

## 贡献

欢迎贡献代码或提出建议!请先阅读 [贡献指南](CONTRIBUTING.md)。
Expand Down
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