1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
/*
* libgit2 "fetch" example - shows how to fetch remote data
*
* Written by the libgit2 contributors
*
* To the extent possible under law, the author(s) have dedicated all copyright
* and related and neighboring rights to this software to the public domain
* worldwide. This software is distributed without any warranty.
*
* You should have received a copy of the CC0 Public Domain Dedication along
* with this software. If not, see
* <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
#![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");
// Figure out whether it's a named remote or a URL
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
});
// This callback gets called for each remote-tracking branch that gets
// updated. The message we output depends on whether it's a new one or an
// update.
cb.update_tips(|refname, a, b| {
if a.is_zero() {
println!("[new] {:20} {}", b, refname);
} else {
println!("[updated] {:10}..{:10} {}", a, b, refname);
}
true
});
// Here we show processed and total objects in the pack and the amount of
// received data. Most frontends will probably want to show a percentage and
// the download rate.
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
});
// Download the packfile and index it. This function updates the amount of
// received data and the indexer stats which lets you inform the user about
// progress.
let mut fo = FetchOptions::new();
fo.remote_callbacks(cb);
remote.download(&[] as &[&str], Some(&mut fo))?;
{
// If there are local objects (we got a thin pack), then tell the user
// how many objects we saved from having to cross the network.
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()
);
}
}
// Disconnect the underlying connection to prevent from idling.
remote.disconnect()?;
// Update the references in the remote's namespace to point to the right
// commits. This may be needed even if there was no packfile to download,
// which can happen e.g. when the branches have been changed but all the
// needed objects are available locally.
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),
}
}
|