Skip to content

Commit 26596c7

Browse files
committed
🔒 Updated dependencies and refactored command utilities.
1 parent 1accefb commit 26596c7

File tree

5 files changed

+46
-86
lines changed

5 files changed

+46
-86
lines changed

‎Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "gpt-commit-rust"
3-
version = "0.3.8"
3+
version = "0.3.9"
44
edition = "2021"
55

66
[dependencies]

‎src/command_utils.rs

Lines changed: 29 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,41 @@
11
use colored::*;
2-
use std::process::{Child, Command};
2+
use std::process::Command;
33

4-
pub fn run_commands(commands: &str) {
5-
if commands.contains(" && ") {
6-
return run_and_commands(commands);
7-
}
8-
9-
if commands.contains(" | ") {
10-
return run_pipe_commands(commands);
11-
}
12-
13-
let commands = commands.split('\n');
14-
for c in commands {
15-
if c.trim() == "" {
16-
continue;
17-
}
18-
run_command(c).unwrap().wait().unwrap();
19-
}
20-
}
21-
22-
fn run_and_commands(commands: &str) {
23-
let commands = commands.split(" && ");
24-
let mut last_command = None;
25-
for c in commands {
26-
if c.trim() == "" {
27-
continue;
28-
}
29-
let mut command = run_command(c).unwrap();
30-
command.wait().unwrap();
31-
last_command = Some(command);
32-
}
33-
if let Some(mut command) = last_command {
34-
command.wait().unwrap();
35-
}
36-
}
37-
38-
fn run_pipe_commands(commands: &str) {
39-
let commands = commands.split(" | ");
40-
let mut last_command: Option<Child> = None;
4+
pub fn run_commands(commands: &Vec<Vec<String>>) {
415
for c in commands {
42-
if c.trim() == "" {
43-
continue;
6+
let mut command = Command::new(&c[0]);
7+
for arg in &c[1..] {
8+
command.arg(arg);
449
}
45-
let mut command = run_command(c).unwrap();
46-
if let Some(mut last_command) = last_command {
47-
last_command.stdout = command.stdout.take();
48-
last_command.wait().unwrap();
49-
}
50-
last_command = Some(command);
51-
}
52-
if let Some(mut command) = last_command {
53-
command.wait().unwrap();
10+
command.spawn().unwrap().wait().unwrap();
5411
}
5512
}
5613

57-
fn run_command(command: &str) -> Result<Child, std::io::Error> {
58-
let mut parts = command.split_whitespace();
59-
let cmd = parts.next().unwrap();
60-
let args = parts.collect::<Vec<_>>();
61-
let args = args
14+
pub fn parse_commands(commands: &Vec<Vec<String>>, new_lines: bool) -> String {
15+
return commands
6216
.into_iter()
63-
.fold::<Vec<String>, _>(vec![], |mut args, arg| match args.last_mut() {
64-
Some(last) if last.starts_with('"') => {
65-
*last = format!("{} {}", last, arg);
66-
args
67-
}
68-
_ => {
69-
args.push(arg.to_owned());
70-
args
71-
}
17+
.map(|command| {
18+
colorize_command(
19+
command
20+
.into_iter()
21+
.map(|arg| {
22+
if arg.contains(' ') {
23+
format!("\"{}\"", arg).to_owned()
24+
} else {
25+
arg.to_owned()
26+
}
27+
})
28+
.collect::<Vec<String>>()
29+
.join(" ")
30+
.as_str(),
31+
)
7232
})
73-
.into_iter()
74-
.map(|arg| arg.replace("\"", ""))
75-
.collect::<Vec<_>>();
76-
Command::new(cmd).args(args).spawn()
33+
.collect::<Vec<_>>()
34+
.join(&format!(
35+
"{}{}",
36+
" &&".bright_black(),
37+
if new_lines { "\n" } else { " " }
38+
));
7739
}
7840

7941
pub fn parse_command(line: &str, new_lines: bool) -> String {

‎src/git.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,24 @@ use git2::{DiffFormat, DiffOptions, Repository, StatusOptions};
33
use normpath::{BasePathBuf, PathExt};
44
use std::{path::Path, str};
55

6-
pub fn build_commands(commit_message: String, include_push: bool) -> String {
6+
pub fn build_commands(commit_message: String, include_push: bool) -> Vec<Vec<String>> {
77
let mut commit_message = commit_message;
88

99
if commit_message.starts_with("\"") && commit_message.ends_with("\"") {
1010
commit_message = commit_message[1..commit_message.len() - 1].to_owned();
1111
}
1212

13-
let mut commands = String::new();
14-
commands.push_str("git add .");
15-
commands.push_str(" && ");
16-
commands.push_str("git commit -m");
17-
commands.push(' ');
18-
commands.push_str(format!("\"{}\"", commit_message).as_str());
13+
let mut commands: Vec<Vec<String>> = Vec::new();
14+
commands.push("git add .".split(' ').map(|s| s.to_owned()).collect());
15+
let mut commit_command: Vec<String> = "git commit -m"
16+
.to_owned()
17+
.split(' ')
18+
.map(|s| s.to_owned())
19+
.collect();
20+
commit_command.push(commit_message);
21+
commands.push(commit_command);
1922
if include_push {
20-
commands.push_str(" && ");
21-
commands.push_str("git push");
23+
commands.push("git push".split(' ').map(|s| s.to_owned()).collect());
2224
}
2325

2426
commands

‎src/main.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use colored::Colorize;
1212
use gpt_api::query;
1313

1414
use crate::{
15-
command_utils::{parse_command, run_commands},
15+
command_utils::{parse_command, parse_commands, run_commands},
1616
git::{build_commands, Git},
1717
utils::{check_for_update, get_executable_name},
1818
};
@@ -170,19 +170,15 @@ async fn main() {
170170

171171
loader.stop();
172172

173-
let mut result = match result {
174-
Ok(result) => build_commands(result, false),
173+
let result = match result {
174+
Ok(result) => build_commands(result, push),
175175
Err(err) => {
176176
println!("Error: {}", err);
177177
return;
178178
}
179179
};
180180

181-
if push {
182-
result.push_str(" && git push");
183-
}
184-
185-
let parsed_command = parse_command(&result, true);
181+
let parsed_command = parse_commands(&result, true);
186182

187183
println!("{}\n{}", "Commands:".bright_magenta(), parsed_command);
188184
print!("\n{} {}: ", "Confirm".green(), "(Y/n)");

0 commit comments

Comments
 (0)