From 0ca6e2911cf5243dacaaeac48d3d5b2ff7e63875 Mon Sep 17 00:00:00 2001 From: jan Michal Date: Thu, 24 Feb 2022 18:34:04 +0000 Subject: [PATCH] reworked init function to pull missing repos on re-run --- src/main.rs | 6 --- src/operations/init.rs | 106 ++++++++++++++++------------------------- src/operations/mod.rs | 4 -- 3 files changed, 40 insertions(+), 76 deletions(-) diff --git a/src/main.rs b/src/main.rs index 571ecb7..10a3ade 100755 --- a/src/main.rs +++ b/src/main.rs @@ -77,8 +77,6 @@ fn main() { .subcommand(SubCommand::with_name("init").about( "Clones all git repositories from mlc.toml branching from current directory", )) - .subcommand(SubCommand::with_name("reinit")) - .about("Removes all subdirectories and reinitialises") .subcommand( SubCommand::with_name("pull") .alias("update") @@ -136,10 +134,6 @@ fn main() { operations::init(); } - if let true = matches.is_present("reinit") { - operations::reinit(); - } - if let true = matches.is_present("build") { operations::build(&matches); } diff --git a/src/operations/init.rs b/src/operations/init.rs index ddce2f7..496d316 100644 --- a/src/operations/init.rs +++ b/src/operations/init.rs @@ -1,80 +1,54 @@ use crate::{crash, info, workspace}; use std::process::Command; -pub fn reinit() { +pub fn init() { let config = workspace::read_cfg(); - let out = Command::new("bash") - .args(&["-c", "ls -A"]) - .output() - .unwrap() - .stdout; - let dirs_to_s = String::from_utf8_lossy(&*out); - let mut dirs = dirs_to_s.lines().collect::>(); + if config.mode == "workspace" || config.mode == "repository" { + let dirs_raw = Command::new("ls").arg("-1").output().unwrap().stdout; + let dirs_string = String::from_utf8_lossy(&dirs_raw); + let mut dirs = dirs_string.lines().collect::>(); - let name = config.name.unwrap(); + dirs.retain(|x| *x != "mlc.toml"); + dirs.sort_unstable(); - dirs.retain(|x| *x != "mlc.toml"); - dirs.retain(|x| *x != ".git"); - if config.mode == "repository" { - dirs.retain(|x| *x != "out"); - dirs.retain(|x| *x != name); - } + let repos = &config.repo; + let mut repos = repos + .iter() + .map(|x| x.split('/').last().unwrap()) + .collect::>(); - info("Removing all repo directories to reinitialise".to_string()); + repos.sort_unstable(); - Command::new("rm") - .args(&["-rf", &dirs.join(" ")]) - .spawn() - .unwrap() - .wait() - .unwrap(); + let mut diff = repos.clone(); + diff.retain(|x| !dirs.contains(x)); - if config.mode == "workspace" { - for r in config.repo { - info(format!("Cloning (workspace mode): {}", r)); - Command::new("git") - .args(&["clone", &r]) - .spawn() - .unwrap() - .wait() - .unwrap(); - } - } else if config.mode == "repository" { - for r in config.repo { - info(format!("Cloning (repository mode): {}", r)); - Command::new("git") - .args(&["clone", &r]) - .spawn() - .unwrap() - .wait() - .unwrap(); - } - } else { - crash("Invalid mode in mlc.toml".to_string(), 1); - } -} + let mut diff_matches = vec![]; -pub fn init() { - let config = workspace::read_cfg(); - if config.mode == "workspace" { - for r in config.repo { - info(format!("Cloning (workspace mode): {}", r)); - Command::new("git") - .args(&["clone", &r]) - .spawn() - .unwrap() - .wait() - .unwrap(); + for &x in &diff { + for y in config.repo.iter() { + if x == y.split('/').last().unwrap() { + diff_matches.push(y); + } + } } - } else if config.mode == "repository" { - for r in config.repo { - info(format!("Cloning (repository mode): {}", r)); - Command::new("git") - .args(&["clone", &r]) - .spawn() - .unwrap() - .wait() - .unwrap(); + + if diff.is_empty() { + info("All repos are already cloned".to_string()); + } else { + info(format!("New/missing repos to clone: {}", diff.join(", "))); + for r in diff_matches { + info(format!( + "Cloning ({} mode): {}", + config.mode, + r.split('/').last().unwrap() + )); + Command::new("git") + .args(&["clone", r]) + .spawn() + .unwrap() + .wait() + .unwrap(); + } } } else { crash("Invalid mode in mlc.toml".to_string(), 1); diff --git a/src/operations/mod.rs b/src/operations/mod.rs index d091514..eee2d4b 100644 --- a/src/operations/mod.rs +++ b/src/operations/mod.rs @@ -6,10 +6,6 @@ mod init; mod prune; mod pull; -pub fn reinit() { - init::reinit(); -} - pub fn init() { init::init(); }