Merge pull request #6 from msfjarvis/thaw-repos

This commit is contained in:
Harsh Shandilya 2020-10-21 08:59:36 +05:30 committed by GitHub
commit 9e4abab5d2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,6 +1,11 @@
use git2::Repository;
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, fs, path::Path};
use std::{
collections::HashMap,
fs,
path::{Path, PathBuf},
process::Command,
};
use walkdir::WalkDir;
#[derive(Debug, Serialize, Deserialize)]
@ -10,14 +15,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 <dir>\n");
println!("Usage:\n gitice <command> <dir>\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 <command> <dir>\n");
Ok(())
}
}
}
fn freeze_repos(dir: String) -> anyhow::Result<()> {
let mut repos: HashMap<String, PersistableRepo> = HashMap::new();
for entry in WalkDir::new(dir.clone()).into_iter().filter_map(|e| e.ok()) {
if entry.file_type().is_dir() {
@ -60,3 +77,27 @@ 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<()> {
let lockfile = fs::read_to_string("gitice.lock").expect("unable to read lockfile!");
let repos: HashMap<String, PersistableRepo> = toml::from_str(&lockfile)?;
for (name, repo) in repos {
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(())
}