Skip to content

Commit

Permalink
Add: 新增 disablePull 控制参数
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiandongx committed Mar 28, 2022
1 parent dfed91a commit 38af273
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 133 deletions.
1 change: 1 addition & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::{collections::HashMap, fs::File};
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateAction {
pub disable_pull: Option<bool>,
pub author_mappings: Option<Vec<AuthorMapping>>,
pub databases: Vec<Database>,
}
Expand Down
58 changes: 10 additions & 48 deletions src/fetcher.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{config, Repository};
use anyhow::Result;
use serde::Deserialize;
use std::{fs::File, path::Path, process::exit};
use tokio::time;
use std::{fs::File, path::Path};
use tokio::{task::JoinHandle, time};

#[derive(Debug, Clone)]
enum GithubConfig {
Expand Down Expand Up @@ -40,7 +40,6 @@ impl RepoFetcher {
async fn fetch_github(&self) -> Result<()> {
println!("start to fetch github repos...");
let now = time::Instant::now();
let mut handles = vec![];

let mut configs = vec![];
for config in self.opts.github_authenticated.clone().unwrap_or_default() {
Expand All @@ -53,65 +52,28 @@ impl RepoFetcher {
configs.push(GithubConfig::Org(config));
}

let mut handles: Vec<JoinHandle<Result<(), anyhow::Error>>> = vec![];
for config in configs {
let config = config.clone();
let handle = tokio::spawn(async move {
let repos = match config {
GithubConfig::Authenticated(ref config) => {
let repos = match GithubRepoFetcher::authenticated_repos(config).await {
Err(e) => {
println!("Fetch github authenticated repos error: {}", e);
exit(1)
}
Ok(repos) => repos,
};
repos
GithubRepoFetcher::authenticated_repos(config).await?
}
GithubConfig::User(ref config) => {
let repos = match GithubRepoFetcher::user_repos(config).await {
Err(e) => {
println!("Fetch github user repos error: {}", e);
exit(1)
}
Ok(repos) => repos,
};
repos
}
GithubConfig::Org(ref config) => {
let repos = match GithubRepoFetcher::org_repos(config).await {
Err(e) => {
println!("Fetch github user repos error: {}", e);
exit(1)
}
Ok(repos) => repos,
};
repos
}
};

let f = match File::create(&config.destination()) {
Err(e) => {
println!(
"Failed to create file '{}', error: {}",
&config.destination(),
e
);
exit(1)
}
Ok(f) => f,
GithubConfig::User(ref config) => GithubRepoFetcher::user_repos(config).await?,
GithubConfig::Org(ref config) => GithubRepoFetcher::org_repos(config).await?,
};

if let Err(e) = serde_yaml::to_writer(f, &repos) {
println!("Failed to unmarshal serde object, error: {}", e);
exit(1)
};
let f = File::create(&config.destination())?;
serde_yaml::to_writer(f, &repos)?;
println!("save database file '{}'", &config.destination());
Ok(())
});
handles.push(handle);
}

for handle in handles {
handle.await?;
handle.await??;
}

println!(
Expand Down
67 changes: 32 additions & 35 deletions src/gitimp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use std::{
collections::{HashMap, HashSet},
fs,
path::Path,
process::exit,
sync::{Arc, Mutex},
time,
};
use tokei::{Config, Languages};
use tokio::task::JoinHandle;

/// 提交记录
#[derive(Debug, Clone, Default, Hash, Eq, PartialEq)]
Expand Down Expand Up @@ -211,16 +211,16 @@ impl Parser {
2 => change.deletion = cap.parse::<usize>().unwrap_or_default(),
3 => {
let p = Path::new(cap);
if p.extension().is_some() {
change.ext = p.extension().unwrap().to_str().unwrap().to_string();
let n = change.ext.len() as usize - 1;
if let Some(cs) = change.ext.chars().nth(n) {
if cs.is_ascii_alphanumeric() {
change.ext.remove(n);
}
if p.extension().is_none() {
change.ext = String::new();
continue;
}
change.ext = p.extension().unwrap().to_str().unwrap().to_string();
let n = change.ext.len() as usize - 1;
if let Some(cs) = change.ext.chars().nth(n) {
if cs.is_ascii_alphanumeric() {
change.ext.remove(n);
}
} else {
change.ext = "".to_string();
}
}
_ => (),
Expand Down Expand Up @@ -294,13 +294,13 @@ impl GitImpl {
let first_ts = DateTime::parse_from_rfc2822(&first_commit.datetime)?.timestamp();
let last_ts = DateTime::parse_from_rfc2822(&last_commit.datetime)?.timestamp();

const DURATION: i64 = 3600 * 24 * 90; // 90days
const DURATION: i64 = 3600 * 24 * 120; // 120days
let data = Self::calc_range(DURATION, first_ts, last_ts);

if data.len() == 1 {
let first = &data[0];
if first.0 == first.1 {
return Ok(vec![("".to_string(), "".to_string())]);
return Ok(vec![(String::new(), String::new())]);
}
}
Ok(data)
Expand Down Expand Up @@ -328,8 +328,8 @@ impl GitImpl {
}

impl GitImpl {
pub async fn clone_or_pull(repos: Vec<Repository>) -> Result<()> {
let mut handles = vec![];
pub async fn clone_or_pull(repos: Vec<Repository>, disable_pull: bool) -> Result<()> {
let mut handles: Vec<JoinHandle<Result<(), anyhow::Error>>> = vec![];
let mutex = Arc::new(Mutex::new(0));
let total = repos.len();

Expand All @@ -340,30 +340,26 @@ impl GitImpl {
let handle = tokio::spawn(async move {
let now = time::Instant::now();
if Path::new(&repo.path).exists() {
if let Err(e) = Git::git_pull(&repo).await {
println!("Failed to execute git pull command, error: {}", e);
exit(1)
};

let mut lock = mutex.lock().unwrap();
*lock += 1;
let n = *lock;
println!(
"[{}/{}] git pull '{}' => elapsed {:#?}",
n,
total,
&repo.name,
now.elapsed(),
)
if !disable_pull {
Git::git_pull(&repo).await?;
let mut lock = mutex.lock().unwrap();
*lock += 1;
let n = *lock;

println!(
"[{}/{}] git pull '{}' => elapsed {:#?}",
n,
total,
&repo.name,
now.elapsed(),
)
}
} else {
if let Err(e) = Git::git_clone(&repo).await {
println!("Failed to execute git clone command, error: {}", e);
exit(1)
};

Git::git_clone(&repo).await?;
let mut lock = mutex.lock().unwrap();
*lock += 1;
let n = *lock;

println!(
"[{}/{}] git clone '{}' => elapsed {:#?}",
n,
Expand All @@ -372,12 +368,13 @@ impl GitImpl {
now.elapsed(),
)
}
Ok(())
});
handles.push(handle);
}

for handle in handles {
handle.await?;
handle.await??;
}
Ok(())
}
Expand Down
Loading

0 comments on commit 38af273

Please sign in to comment.