Skip to content

Commit cd9d94a

Browse files
committed
🚀 Improved Git command building, added add_all and commit methods.
1 parent 5b8f937 commit cd9d94a

File tree

1 file changed

+68
-4
lines changed

1 file changed

+68
-4
lines changed

‎src/git.rs

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,29 @@
11
use colored::Colorize;
2-
use git2::{DiffFormat, DiffOptions, Repository, StatusOptions};
2+
use git2::{DiffFormat, DiffOptions, Oid, 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) -> Vec<Vec<String>> {
7-
let mut commit_message = commit_message;
6+
use crate::command_utils::run_commands;
7+
8+
pub fn build_commands(
9+
commit_message: &String,
10+
include_push: bool,
11+
files: &Vec<String>,
12+
) -> Vec<Vec<String>> {
13+
let mut commit_message = commit_message.clone();
814

915
if commit_message.starts_with("\"") && commit_message.ends_with("\"") {
1016
commit_message = commit_message[1..commit_message.len() - 1].to_owned();
1117
}
1218

1319
let mut commands: Vec<Vec<String>> = Vec::new();
14-
commands.push("git add .".split(' ').map(|s| s.to_owned()).collect());
20+
let mut add_command: Vec<String> = "git add"
21+
.to_owned()
22+
.split(' ')
23+
.map(|s| s.to_owned())
24+
.collect();
25+
add_command.extend(paths_to_git_paths(&files));
26+
commands.push(add_command);
1527
let mut commit_command: Vec<String> = "git commit -m"
1628
.to_owned()
1729
.split(' ')
@@ -23,6 +35,8 @@ pub fn build_commands(commit_message: String, include_push: bool) -> Vec<Vec<Str
2335
commands.push("git push".split(' ').map(|s| s.to_owned()).collect());
2436
}
2537

38+
println!("Commands: {:?}", commands);
39+
2640
commands
2741
}
2842

@@ -148,4 +162,54 @@ impl Git {
148162

149163
Ok(status_value)
150164
}
165+
166+
pub fn add_all(self: &Self, files: Option<&Vec<String>>) {
167+
self.repo
168+
.index()
169+
.unwrap()
170+
.add_all(
171+
paths_to_git_paths(&files.unwrap_or(&vec![])).iter(),
172+
git2::IndexAddOption::DEFAULT,
173+
None,
174+
)
175+
.unwrap();
176+
}
177+
178+
pub fn commit(self: &Self, message: &String) -> Result<Oid, git2::Error> {
179+
let mut index = self.repo.index().unwrap();
180+
let oid = index.write_tree().unwrap();
181+
let signature = self.repo.signature().unwrap();
182+
let parent_commit = self.repo.head().unwrap().peel_to_commit().unwrap();
183+
let tree = self.repo.find_tree(oid).unwrap();
184+
self.repo.commit(
185+
Some("HEAD"),
186+
&signature,
187+
&signature,
188+
&message,
189+
&tree,
190+
&[&parent_commit],
191+
)
192+
}
193+
194+
pub fn push(self: &Self) {
195+
run_commands(&vec![vec!["git push".to_owned()]]);
196+
}
197+
}
198+
199+
fn paths_to_git_paths(paths: &Vec<String>) -> Vec<String> {
200+
if paths.is_empty() {
201+
return vec![".".to_owned()];
202+
}
203+
paths
204+
.iter()
205+
.map(|entry| Path::new(entry))
206+
.map(|entry| {
207+
entry
208+
.into_iter()
209+
.map(|entry| entry.to_str().unwrap().to_owned())
210+
.filter(|entry| entry != &".")
211+
.collect::<Vec<String>>()
212+
.join("/")
213+
})
214+
.collect()
151215
}

0 commit comments

Comments
 (0)