Skip to content

Commit

Permalink
feat: Update version to 0.1.66 and refactor commit logic
Browse files Browse the repository at this point in the history
This commit updates the version of the `aicommit` tool to 0.1.66 in `Cargo.toml`, `package.json`,
  • Loading branch information
suenot committed Jan 31, 2025
1 parent b00b2fb commit 34aeced
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 102 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "aicommit"
version = "0.1.65"
version = "0.1.66"
edition = "2021"
authors = ["Eugen Soloviov <[email protected]>"]
description = "A CLI tool that generates concise and descriptive git commit messages using LLMs"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@
"postinstall": "node -e \"process.exit(0)\"",
"test": "echo \"Error: no test specified\" && exit 1"
},
"version": "0.1.65"
"version": "0.1.66"
}
139 changes: 40 additions & 99 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1057,107 +1057,35 @@ async fn main() -> Result<(), String> {
}

async fn run_commit(config: &Config, cli: &Cli) -> Result<(), String> {
// Stage changes if --add flag is set
if cli.add {
let add_output = Command::new("sh")
.arg("-c")
.arg("git add .")
.output()
.map_err(|e| format!("Failed to execute git add: {}", e))?;

if !add_output.status.success() {
return Err(String::from_utf8_lossy(&add_output.stderr).to_string());
}
}

// Обновляем версии если указаны соответствующие параметры
let mut new_version = String::new();

// Обновляем версию в файле версии
if let Some(version_file) = cli.version_file.as_ref() {
if cli.version_iterate {
update_version_file(version_file).await?;
}
new_version = tokio::fs::read_to_string(version_file)
.await
.map_err(|e| format!("Failed to read version file: {}", e))?
.trim()
.to_string();
}

// Обновляем версию в Cargo.toml
if cli.version_cargo {
if new_version.is_empty() {
return Err("Error: --version-file must be specified when using --version-cargo".to_string());
}
update_cargo_version(&new_version).await?;
}

// Обновляем версию в package.json
if cli.version_npm {
if new_version.is_empty() {
return Err("Error: --version-file must be specified when using --version-npm".to_string());
}
update_npm_version(&new_version).await?;
}

// Обновляем версию на GitHub
if cli.version_github {
if new_version.is_empty() {
return Err("Error: --version-file must be specified when using --version-github".to_string());
}
update_github_version(&new_version)?;
}

// Stage version changes if any version flags were used
if cli.version_iterate || cli.version_cargo || cli.version_npm || cli.version_github {
let add_output = Command::new("sh")
.arg("-c")
.arg("git add .")
.output()
.map_err(|e| format!("Failed to execute git add: {}", e))?;
// Get the diff (will handle git add if needed)
let diff = get_git_diff(cli)?;

if !add_output.status.success() {
return Err(String::from_utf8_lossy(&add_output.stderr).to_string());
// Generate commit message based on the active provider
let (message, usage_info) = match &config.active_provider {
_ => {
let active_provider = config.providers.iter().find(|p| match p {
ProviderConfig::OpenRouter(c) => c.id == &config.active_provider,

Check failure on line 1067 in src/main.rs

View workflow job for this annotation

GitHub Actions / publish-crates

can't compare `std::string::String` with `&std::string::String`

Check failure on line 1067 in src/main.rs

View workflow job for this annotation

GitHub Actions / build-linux-x86

can't compare `std::string::String` with `&std::string::String`

Check failure on line 1067 in src/main.rs

View workflow job for this annotation

GitHub Actions / build-macos-x86

can't compare `std::string::String` with `&std::string::String`

Check failure on line 1067 in src/main.rs

View workflow job for this annotation

GitHub Actions / build-macos-arm

can't compare `std::string::String` with `&std::string::String`

Check failure on line 1067 in src/main.rs

View workflow job for this annotation

GitHub Actions / build-windows-x86

can't compare `std::string::String` with `&std::string::String`

Check failure on line 1067 in src/main.rs

View workflow job for this annotation

GitHub Actions / build-windows-arm

can't compare `std::string::String` with `&std::string::String`

Check failure on line 1067 in src/main.rs

View workflow job for this annotation

GitHub Actions / publish-crates

can't compare `std::string::String` with `&std::string::String`

Check failure on line 1067 in src/main.rs

View workflow job for this annotation

GitHub Actions / build-linux-x86

can't compare `std::string::String` with `&std::string::String`

Check failure on line 1067 in src/main.rs

View workflow job for this annotation

GitHub Actions / build-macos-arm

can't compare `std::string::String` with `&std::string::String`

Check failure on line 1067 in src/main.rs

View workflow job for this annotation

GitHub Actions / build-macos-x86

can't compare `std::string::String` with `&std::string::String`

Check failure on line 1067 in src/main.rs

View workflow job for this annotation

GitHub Actions / build-windows-x86

can't compare `std::string::String` with `&std::string::String`

Check failure on line 1067 in src/main.rs

View workflow job for this annotation

GitHub Actions / build-windows-arm

can't compare `std::string::String` with `&std::string::String`
ProviderConfig::Ollama(c) => c.id == &config.active_provider,

Check failure on line 1068 in src/main.rs

View workflow job for this annotation

GitHub Actions / publish-crates

can't compare `std::string::String` with `&std::string::String`

Check failure on line 1068 in src/main.rs

View workflow job for this annotation

GitHub Actions / build-linux-x86

can't compare `std::string::String` with `&std::string::String`

Check failure on line 1068 in src/main.rs

View workflow job for this annotation

GitHub Actions / build-macos-x86

can't compare `std::string::String` with `&std::string::String`

Check failure on line 1068 in src/main.rs

View workflow job for this annotation

GitHub Actions / build-macos-arm

can't compare `std::string::String` with `&std::string::String`

Check failure on line 1068 in src/main.rs

View workflow job for this annotation

GitHub Actions / build-windows-x86

can't compare `std::string::String` with `&std::string::String`

Check failure on line 1068 in src/main.rs

View workflow job for this annotation

GitHub Actions / build-windows-arm

can't compare `std::string::String` with `&std::string::String`

Check failure on line 1068 in src/main.rs

View workflow job for this annotation

GitHub Actions / publish-crates

can't compare `std::string::String` with `&std::string::String`

Check failure on line 1068 in src/main.rs

View workflow job for this annotation

GitHub Actions / build-linux-x86

can't compare `std::string::String` with `&std::string::String`

Check failure on line 1068 in src/main.rs

View workflow job for this annotation

GitHub Actions / build-macos-arm

can't compare `std::string::String` with `&std::string::String`

Check failure on line 1068 in src/main.rs

View workflow job for this annotation

GitHub Actions / build-macos-x86

can't compare `std::string::String` with `&std::string::String`

Check failure on line 1068 in src/main.rs

View workflow job for this annotation

GitHub Actions / build-windows-x86

can't compare `std::string::String` with `&std::string::String`

Check failure on line 1068 in src/main.rs

View workflow job for this annotation

GitHub Actions / build-windows-arm

can't compare `std::string::String` with `&std::string::String`
ProviderConfig::OpenAICompatible(c) => c.id == &config.active_provider,

Check failure on line 1069 in src/main.rs

View workflow job for this annotation

GitHub Actions / publish-crates

can't compare `std::string::String` with `&std::string::String`

Check failure on line 1069 in src/main.rs

View workflow job for this annotation

GitHub Actions / build-linux-x86

can't compare `std::string::String` with `&std::string::String`

Check failure on line 1069 in src/main.rs

View workflow job for this annotation

GitHub Actions / build-macos-x86

can't compare `std::string::String` with `&std::string::String`

Check failure on line 1069 in src/main.rs

View workflow job for this annotation

GitHub Actions / build-macos-arm

can't compare `std::string::String` with `&std::string::String`

Check failure on line 1069 in src/main.rs

View workflow job for this annotation

GitHub Actions / build-windows-x86

can't compare `std::string::String` with `&std::string::String`

Check failure on line 1069 in src/main.rs

View workflow job for this annotation

GitHub Actions / build-windows-arm

can't compare `std::string::String` with `&std::string::String`

Check failure on line 1069 in src/main.rs

View workflow job for this annotation

GitHub Actions / publish-crates

can't compare `std::string::String` with `&std::string::String`

Check failure on line 1069 in src/main.rs

View workflow job for this annotation

GitHub Actions / build-linux-x86

can't compare `std::string::String` with `&std::string::String`

Check failure on line 1069 in src/main.rs

View workflow job for this annotation

GitHub Actions / build-macos-arm

can't compare `std::string::String` with `&std::string::String`

Check failure on line 1069 in src/main.rs

View workflow job for this annotation

GitHub Actions / build-macos-x86

can't compare `std::string::String` with `&std::string::String`

Check failure on line 1069 in src/main.rs

View workflow job for this annotation

GitHub Actions / build-windows-x86

can't compare `std::string::String` with `&std::string::String`

Check failure on line 1069 in src/main.rs

View workflow job for this annotation

GitHub Actions / build-windows-arm

can't compare `std::string::String` with `&std::string::String`
}).ok_or("No active provider found")?;

match active_provider {
ProviderConfig::OpenRouter(c) => generate_openrouter_commit_message(c, &diff).await?,
ProviderConfig::Ollama(c) => generate_ollama_commit_message(c, &diff).await?,
ProviderConfig::OpenAICompatible(c) => generate_openai_compatible_commit_message(c, &diff).await?,
}
}
}

// Get active provider
let active_provider = config.providers.iter().find(|p| match p {
ProviderConfig::OpenRouter(c) => c.id == config.active_provider,
ProviderConfig::Ollama(c) => c.id == config.active_provider,
ProviderConfig::OpenAICompatible(c) => c.id == config.active_provider,
}).ok_or("No active provider found")?;

// Get git diff
let diff = get_git_diff(cli)?;
if diff.is_empty() {
return Err("No changes to commit".to_string());
}
};

let mut commit_applied = false;
println!("Generated commit message: \"{}\"\n", message);
println!("Tokens: {}↑ {}↓", usage_info.input_tokens, usage_info.output_tokens);
println!("API Cost: ${:.4}", usage_info.total_cost);

// Generate and apply commit
if cli.dry_run {
// ... dry run logic ...
} else {
let (message, usage) = match active_provider {
ProviderConfig::OpenRouter(c) => generate_openrouter_commit_message(c, &diff).await?,
ProviderConfig::Ollama(c) => generate_ollama_commit_message(c, &diff).await?,
ProviderConfig::OpenAICompatible(c) => generate_openai_compatible_commit_message(c, &diff).await?,
};

println!("Generated commit message: \"{}\"\n", message);
println!("Tokens: {}↑ {}↓", usage.input_tokens, usage.output_tokens);
println!("API Cost: ${:.4}", usage.total_cost);

create_git_commit(&message)?;
println!("Commit successfully created.");
commit_applied = true;
}
create_git_commit(&message)?;
println!("Commit successfully created.");

// Pull changes if --pull flag is set
if cli.pull && commit_applied {
if cli.pull {
let pull_output = Command::new("sh")
.arg("-c")
.arg("git pull --no-rebase --no-edit")
Expand All @@ -1174,8 +1102,8 @@ async fn run_commit(config: &Config, cli: &Cli) -> Result<(), String> {
println!("Successfully pulled changes.");
}

// Push changes if --push flag is set and commit was applied
if cli.push && commit_applied {
// Push changes if --push flag is set
if cli.push {
let push_output = Command::new("sh")
.arg("-c")
.arg("git push")
Expand All @@ -1193,8 +1121,21 @@ async fn run_commit(config: &Config, cli: &Cli) -> Result<(), String> {
}

fn get_git_diff(cli: &Cli) -> Result<String, String> {
// Stage all changes if --add flag is set
if cli.add {
// Check for unstaged changes first
let status_output = Command::new("sh")
.arg("-c")
.arg("git status --porcelain")
.output()
.map_err(|e| format!("Failed to execute git status: {}", e))?;

let status = String::from_utf8_lossy(&status_output.stdout).to_string();

// If --add flag is set and there are unstaged changes, add them
if cli.add && status.lines().any(|line| {
line.starts_with(" M") || // Modified but not staged
line.starts_with("MM") || // Modified and staged with new modifications
line.starts_with("??") // Untracked files
}) {
let add_output = Command::new("sh")
.arg("-c")
.arg("git add .")
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.65
0.1.66

0 comments on commit 34aeced

Please sign in to comment.