From 340f121f983e4d6b81b9761a1d662d0cc6927bda Mon Sep 17 00:00:00 2001 From: Michal Date: Fri, 22 Jul 2022 16:50:54 +0100 Subject: [PATCH] Added new smart_pull option --- example/mlc.toml | 1 + src/internal/structs.rs | 6 +++-- src/operations/pull.rs | 54 +++++++++++++++++++++++++++++++---------- src/workspace/read.rs | 1 + 4 files changed, 47 insertions(+), 15 deletions(-) diff --git a/example/mlc.toml b/example/mlc.toml index 215276d..f1cc194 100644 --- a/example/mlc.toml +++ b/example/mlc.toml @@ -1,6 +1,7 @@ mode = "repository" name = "test" sign = false +smart_pull = true repo = [ "1::amethyst", diff --git a/src/internal/structs.rs b/src/internal/structs.rs index 2e73406..ff8b575 100755 --- a/src/internal/structs.rs +++ b/src/internal/structs.rs @@ -3,8 +3,9 @@ use serde_derive::Deserialize; #[derive(Debug, Deserialize)] pub struct Config { pub mode: String, - pub sign: bool, pub name: Option, + pub sign: bool, + pub smart_pull: bool, pub repo: Vec, } @@ -18,8 +19,9 @@ pub struct Repo { #[derive(Debug, Deserialize)] pub struct UnexpandedConfig { pub mode: String, - pub sign: bool, pub name: Option, + pub sign: bool, + pub smart_pull: bool, pub repo: Vec, pub urls: Vec, } diff --git a/src/operations/pull.rs b/src/operations/pull.rs index daf31e3..671b125 100644 --- a/src/operations/pull.rs +++ b/src/operations/pull.rs @@ -2,24 +2,48 @@ use std::env; use std::process::Command; use crate::info; -use crate::{crash, internal::AppExitCode, log}; +use crate::{crash, internal::AppExitCode, workspace::read_cfg, log}; -fn do_the_pulling(repos: Vec, verbose: bool) { +fn do_the_pulling(repos: Vec, verbose: bool, smart_pull: bool) { for repo in repos { // Set root dir to return after each git pull let root_dir = env::current_dir().unwrap(); log!(verbose, "Root dir: {:?}", root_dir); + info!("Entering working directory: {}", &repo); env::set_current_dir(repo).unwrap(); log!(verbose, "Current dir: {:?}", env::current_dir().unwrap()); - log!(verbose, "Pulling"); - Command::new("git") - .arg("pull") - .spawn() - .unwrap() - .wait() - .unwrap(); + log!(verbose, "Pulling"); + if smart_pull { + Command::new("git") + .args(&["remote", "update"]) + .spawn() + .unwrap() + .wait() + .unwrap(); + let output = Command::new("git") + .arg("status") + .output() + .unwrap(); + if String::from_utf8(output.stdout).unwrap().to_string().contains("Your branch is behind") { + Command::new("git") + .arg("pull") + .spawn() + .unwrap() + .wait() + .unwrap(); + } else { + info!("No changes to pull"); + } + } else { + Command::new("git") + .arg("pull") + .spawn() + .unwrap() + .wait() + .unwrap(); + } // Return to root dir env::set_current_dir(root_dir).unwrap(); log!( @@ -31,13 +55,17 @@ fn do_the_pulling(repos: Vec, verbose: bool) { } pub fn pull(packages: Vec, exclude: Vec, verbose: bool) { + let config = read_cfg(verbose); + log!(verbose, "Config: {:?}", config); // If no packages are specified, imply all let all = packages.is_empty(); log!(verbose, "All: {}", all); + // Read smart_pull from config + let smart_pull = config.smart_pull; + log!(verbose, "Smart pull: {}", smart_pull); - // Read repos from config file - let repos = crate::workspace::read_cfg(verbose) - .repo + // Read repos from config + let repos = config.repo .iter() .map(|x| x.name.clone()) .collect::>(); @@ -63,5 +91,5 @@ pub fn pull(packages: Vec, exclude: Vec, verbose: bool) { // Pull! log!(verbose, "Pulling {:?}", repos_applicable); - do_the_pulling(repos_applicable, verbose); + do_the_pulling(repos_applicable, verbose, smart_pull); } diff --git a/src/workspace/read.rs b/src/workspace/read.rs index 85781fb..9ae9e35 100755 --- a/src/workspace/read.rs +++ b/src/workspace/read.rs @@ -61,6 +61,7 @@ pub fn read_cfg(verbose: bool) -> Config { mode: config.mode, sign: config.sign, name: config.name, + smart_pull: config.smart_pull, repo: expanded_repos, }; log!(verbose, "Config: {:?}", conf);