#![deny(warnings)]
use git2::{AutotagOption, FetchOptions, RemoteCallbacks, Repository};
use std::io::{self, Write};
use std::str;
use structopt::StructOpt;
#[derive(StructOpt)]
struct Args {
#[structopt(name = "remote")]
arg_remote: Option<String>,
}
fn run(args: &Args) -> Result<(), git2::Error> {
let repo = Repository::open(".")?;
let remote = args.arg_remote.as_ref().map(|s| &s[..]).unwrap_or("origin");
println!("Fetching {} for repo", remote);
let mut cb = RemoteCallbacks::new();
let mut remote = repo
.find_remote(remote)
.or_else(|_| repo.remote_anonymous(remote))?;
cb.sideband_progress(|data| {
print!("remote: {}", str::from_utf8(data).unwrap());
io::stdout().flush().unwrap();
true
});
cb.update_tips(|refname, a, b| {
if a.is_zero() {
println!("[new] {:20} {}", b, refname);
} else {
println!("[updated] {:10}..{:10} {}", a, b, refname);
}
true
});
cb.transfer_progress(|stats| {
if stats.received_objects() == stats.total_objects() {
print!(
"Resolving deltas {}/{}\r",
stats.indexed_deltas(),
stats.total_deltas()
);
} else if stats.total_objects() > 0 {
print!(
"Received {}/{} objects ({}) in {} bytes\r",
stats.received_objects(),
stats.total_objects(),
stats.indexed_objects(),
stats.received_bytes()
);
}
io::stdout().flush().unwrap();
true
});
let mut fo = FetchOptions::new();
fo.remote_callbacks(cb);
remote.download(&[] as &[&str], Some(&mut fo))?;
{
let stats = remote.stats();
if stats.local_objects() > 0 {
println!(
"\rReceived {}/{} objects in {} bytes (used {} local \
objects)",
stats.indexed_objects(),
stats.total_objects(),
stats.received_bytes(),
stats.local_objects()
);
} else {
println!(
"\rReceived {}/{} objects in {} bytes",
stats.indexed_objects(),
stats.total_objects(),
stats.received_bytes()
);
}
}
remote.disconnect()?;
remote.update_tips(None, true, AutotagOption::Unspecified, None)?;
Ok(())
}
fn main() {
let args = Args::from_args();
match run(&args) {
Ok(()) => {}
Err(e) => println!("error: {}", e),
}
}