#![deny(warnings)]
extern crate git2;
extern crate docopt;
#[macro_use]
extern crate serde_derive;
use docopt::Docopt;
use git2::{Repository, RemoteCallbacks, AutotagOption, FetchOptions};
use std::io::{self, Write};
use std::str;
#[derive(Deserialize)]
struct Args {
arg_remote: Option<String>,
}
fn run(args: &Args) -> Result<(), git2::Error> {
let repo = try!(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 = try!(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);
try!(remote.download(&[], 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();
try!(remote.update_tips(None, true,
AutotagOption::Unspecified, None));
Ok(())
}
fn main() {
const USAGE: &'static str = "
usage: fetch [options] [<remote>]
Options:
-h, --help show this message
";
let args = Docopt::new(USAGE).and_then(|d| d.deserialize())
.unwrap_or_else(|e| e.exit());
match run(&args) {
Ok(()) => {}
Err(e) => println!("error: {}", e),
}
}