-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgit.rs
60 lines (50 loc) · 1.53 KB
/
git.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use std::process::Command;
use git2::{Repository, StatusOptions};
/// 把新文件加入到git中
pub async fn push() {
// 解析readme.md文件
let mut r = crate::file::parse_readme().await;
let new_file = get_uncommit_files();
for i in new_file.iter() {
let x = i.trim_end_matches(".rs"); // 去掉后缀
let x = x.trim_start_matches("src/bin/"); // 去掉路径
git_add(i);
r.push(crate::http::get_question_info(x).await);
}
crate::file::write_readme(&mut r).await;
git_add("README.md");
push_to_origin();
}
fn get_uncommit_files() -> Vec<String> {
let mut options = StatusOptions::new();
options.pathspec("src/bin");
options.include_untracked(true);
let repo = Repository::open(".").unwrap();
let statuses = repo.statuses(Some(&mut options)).unwrap();
statuses
.iter()
.map(|x| String::from(x.path().unwrap()))
.collect()
}
fn git_add(file: &str) {
Command::new("git").arg("add").arg(file).output().unwrap();
let output = Command::new("git")
.arg("commit")
.arg("-m")
.arg(file)
.output()
.unwrap();
println!("{}", String::from_utf8(output.stdout).unwrap());
}
pub fn push_to_origin() {
let output = Command::new("git").arg("push").output().unwrap();
println!("{}", String::from_utf8(output.stdout).unwrap());
}
#[cfg(test)]
mod tests {
use crate::git::get_uncommit_files;
#[test]
fn test_get_uncommit_files() {
println!("{:?}", get_uncommit_files());
}
}