| 1 |
/* |
| 2 |
* libgit2 "clone" example |
| 3 |
* |
| 4 |
* Written by the libgit2 contributors |
| 5 |
* |
| 6 |
* To the extent possible under law, the author(s) have dedicated all copyright |
| 7 |
* and related and neighboring rights to this software to the public domain |
| 8 |
* worldwide. This software is distributed without any warranty. |
| 9 |
* |
| 10 |
* You should have received a copy of the CC0 Public Domain Dedication along |
| 11 |
* with this software. If not, see |
| 12 |
* <http://creativecommons.org/publicdomain/zero/1.0/>. |
| 13 |
*/ |
| 14 |
|
| 15 |
#![deny(warnings)] |
| 16 |
|
| 17 |
use clap::Parser; |
| 18 |
use git2::build::{CheckoutBuilder, RepoBuilder}; |
| 19 |
use git2::{FetchOptions, Progress, RemoteCallbacks}; |
| 20 |
use std::cell::RefCell; |
| 21 |
use std::io::{self, Write}; |
| 22 |
use std::path::{Path, PathBuf}; |
| 23 |
|
| 24 |
#[derive(Parser)] |
| 25 |
struct Args { |
| 26 |
#[structopt(name = "url")] |
| 27 |
arg_url: String, |
| 28 |
#[structopt(name = "path")] |
| 29 |
arg_path: String, |
| 30 |
} |
| 31 |
|
| 32 |
struct State { |
| 33 |
progress: Option<Progress<'static>>, |
| 34 |
total: usize, |
| 35 |
current: usize, |
| 36 |
path: Option<PathBuf>, |
| 37 |
newline: bool, |
| 38 |
} |
| 39 |
|
| 40 |
fn print(state: &mut State) { |
| 41 |
let stats = state.progress.as_ref().unwrap(); |
| 42 |
let network_pct = (100 * stats.received_objects()) / stats.total_objects(); |
| 43 |
let index_pct = (100 * stats.indexed_objects()) / stats.total_objects(); |
| 44 |
let co_pct = if state.total > 0 { |
| 45 |
(100 * state.current) / state.total |
| 46 |
} else { |
| 47 |
0 |
| 48 |
}; |
| 49 |
let kbytes = stats.received_bytes() / 1024; |
| 50 |
if stats.received_objects() == stats.total_objects() { |
| 51 |
if !state.newline { |
| 52 |
println!(); |
| 53 |
state.newline = true; |
| 54 |
} |
| 55 |
print!( |
| 56 |
"Resolving deltas {}/{}\r", |
| 57 |
stats.indexed_deltas(), |
| 58 |
stats.total_deltas() |
| 59 |
); |
| 60 |
} else { |
| 61 |
print!( |
| 62 |
"net {:3}% ({:4} kb, {:5}/{:5}) / idx {:3}% ({:5}/{:5}) \ |
| 63 |
/ chk {:3}% ({:4}/{:4}) {}\r", |
| 64 |
network_pct, |
| 65 |
kbytes, |
| 66 |
stats.received_objects(), |
| 67 |
stats.total_objects(), |
| 68 |
index_pct, |
| 69 |
stats.indexed_objects(), |
| 70 |
stats.total_objects(), |
| 71 |
co_pct, |
| 72 |
state.current, |
| 73 |
state.total, |
| 74 |
state |
| 75 |
.path |
| 76 |
.as_ref() |
| 77 |
.map(|s| s.to_string_lossy().into_owned()) |
| 78 |
.unwrap_or_default() |
| 79 |
) |
| 80 |
} |
| 81 |
io::stdout().flush().unwrap(); |
| 82 |
} |
| 83 |
|
| 84 |
fn run(args: &Args) -> Result<(), git2::Error> { |
| 85 |
let state = RefCell::new(State { |
| 86 |
progress: None, |
| 87 |
total: 0, |
| 88 |
current: 0, |
| 89 |
path: None, |
| 90 |
newline: false, |
| 91 |
}); |
| 92 |
let mut cb = RemoteCallbacks::new(); |
| 93 |
cb.transfer_progress(|stats| { |
| 94 |
let mut state = state.borrow_mut(); |
| 95 |
state.progress = Some(stats.to_owned()); |
| 96 |
print(&mut *state); |
| 97 |
true |
| 98 |
}); |
| 99 |
|
| 100 |
let mut co = CheckoutBuilder::new(); |
| 101 |
co.progress(|path, cur, total| { |
| 102 |
let mut state = state.borrow_mut(); |
| 103 |
state.path = path.map(|p| p.to_path_buf()); |
| 104 |
state.current = cur; |
| 105 |
state.total = total; |
| 106 |
print(&mut *state); |
| 107 |
}); |
| 108 |
|
| 109 |
let mut fo = FetchOptions::new(); |
| 110 |
fo.remote_callbacks(cb); |
| 111 |
RepoBuilder::new() |
| 112 |
.fetch_options(fo) |
| 113 |
.with_checkout(co) |
| 114 |
.clone(&args.arg_url, Path::new(&args.arg_path))?; |
| 115 |
println!(); |
| 116 |
|
| 117 |
Ok(()) |
| 118 |
} |
| 119 |
|
| 120 |
fn main() { |
| 121 |
let args = Args::parse(); |
| 122 |
match run(&args) { |
| 123 |
Ok(()) => {} |
| 124 |
Err(e) => println!("error: {}", e), |
| 125 |
} |
| 126 |
} |