From 1bcbccf30b2eadcd9aeb62cc3d23681e911ab3eb Mon Sep 17 00:00:00 2001 From: ATechnoHazard Date: Tue, 20 Oct 2020 18:12:45 +0530 Subject: [PATCH 1/3] Start implementing thawing of repos Signed-off-by: ATechnoHazard --- src/main.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 09b3e6a..fd33032 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,14 +10,26 @@ struct PersistableRepo { } fn main() -> anyhow::Result<()> { - let dir = match std::env::args().nth(1) { + let dir = match std::env::args().nth(2) { Some(d) => d, None => { - println!("Usage:\n gitice \n"); + println!("Usage:\n gitice \n"); return Ok(()); } }; + // temporary solution to support both freezing and thawing + match std::env::args().nth(1).as_ref().map(|s| &s[..]) { + Some("freeze") => freeze_repos(dir), + Some("thaw") => thaw_repos(dir), + _ => { + println!("Usage:\n gitice \n"); + Ok(()) + } + } +} + +fn freeze_repos(dir: String) -> anyhow::Result<()> { let mut repos: HashMap = HashMap::new(); for entry in WalkDir::new(dir.clone()).into_iter().filter_map(|e| e.ok()) { if entry.file_type().is_dir() { @@ -60,3 +72,7 @@ fn main() -> anyhow::Result<()> { fs::write("gitice.lock", toml::to_string(&repos)?).expect("could not write to lockfile!"); Ok(()) } + +fn thaw_repos(_dir: String) -> anyhow::Result<()> { + Ok(()) +} \ No newline at end of file From 15b143cbf6506e819bd2728b2982b62e6de8a4bc Mon Sep 17 00:00:00 2001 From: ATechnoHazard Date: Tue, 20 Oct 2020 19:05:18 +0530 Subject: [PATCH 2/3] Implement thawing for frozen repos Signed-off-by: ATechnoHazard --- src/main.rs | 46 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index fd33032..5e29c93 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,10 @@ -use git2::Repository; +use git2::{Cred, RemoteCallbacks, Repository}; use serde::{Deserialize, Serialize}; -use std::{collections::HashMap, fs, path::Path}; +use std::{ + collections::HashMap, + fs, + path::{Path, PathBuf}, +}; use walkdir::WalkDir; #[derive(Debug, Serialize, Deserialize)] @@ -73,6 +77,40 @@ fn freeze_repos(dir: String) -> anyhow::Result<()> { Ok(()) } -fn thaw_repos(_dir: String) -> anyhow::Result<()> { +fn thaw_repos(dir: String) -> anyhow::Result<()> { + let lockfile = fs::read_to_string("gitice.lock").expect("unable to read lockfile!"); + let repos: HashMap = toml::from_str(&lockfile)?; + + let mut callbacks = RemoteCallbacks::new(); + callbacks.credentials(|_url, username_from_url, _allowed_types| { + Cred::ssh_key( + username_from_url.unwrap(), + None, + Path::new(&format!( + "{}/.ssh/id_rsa", + std::env::var("HOME").expect("unable to find homedir!") + )), + // TODO: implement for keys that require a passphrase + None, + ) + }); + + let mut fo = git2::FetchOptions::new(); + fo.remote_callbacks(callbacks); + + let mut builder = git2::build::RepoBuilder::new(); + builder.fetch_options(fo); + + for (name, repo) in repos { + match builder.clone(&repo.remote_url, PathBuf::from(&dir).join(&name).as_path()) { + Ok(_) => { + println!("Thawed {}", name); + } + Err(e) => { + println!("Error thawing {}: {}. Continuing...", name, e); + } + }; + } + Ok(()) -} \ No newline at end of file +} From f035df8153edfacca8b95dd11de00bbb1f58bfc4 Mon Sep 17 00:00:00 2001 From: ATechnoHazard Date: Wed, 21 Oct 2020 08:41:28 +0530 Subject: [PATCH 3/3] Shell out to git for thawing repos Signed-off-by: ATechnoHazard --- src/main.rs | 45 ++++++++++++++++----------------------------- 1 file changed, 16 insertions(+), 29 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5e29c93..523a7ec 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,10 @@ -use git2::{Cred, RemoteCallbacks, Repository}; +use git2::Repository; use serde::{Deserialize, Serialize}; use std::{ collections::HashMap, fs, path::{Path, PathBuf}, + process::Command, }; use walkdir::WalkDir; @@ -81,35 +82,21 @@ fn thaw_repos(dir: String) -> anyhow::Result<()> { let lockfile = fs::read_to_string("gitice.lock").expect("unable to read lockfile!"); let repos: HashMap = toml::from_str(&lockfile)?; - let mut callbacks = RemoteCallbacks::new(); - callbacks.credentials(|_url, username_from_url, _allowed_types| { - Cred::ssh_key( - username_from_url.unwrap(), - None, - Path::new(&format!( - "{}/.ssh/id_rsa", - std::env::var("HOME").expect("unable to find homedir!") - )), - // TODO: implement for keys that require a passphrase - None, - ) - }); - - let mut fo = git2::FetchOptions::new(); - fo.remote_callbacks(callbacks); - - let mut builder = git2::build::RepoBuilder::new(); - builder.fetch_options(fo); - for (name, repo) in repos { - match builder.clone(&repo.remote_url, PathBuf::from(&dir).join(&name).as_path()) { - Ok(_) => { - println!("Thawed {}", name); - } - Err(e) => { - println!("Error thawing {}: {}. Continuing...", name, e); - } - }; + let output = Command::new("git") + .args(&[ + "clone", + &repo.remote_url, + PathBuf::from(&dir).join(&name).to_str().unwrap(), + ]) + .output() + .expect("Failed to run `git clone`. Perhaps git is not installed?"); + + if output.status.success() { + println!("Thawed {} successfully.", name) + } else { + println!("{}", std::str::from_utf8(&output.stderr)?) + } } Ok(())