Skip to content

Commit 6d29aaa

Browse files
committed
Improve pushing to remote repo
1 parent 31a5fe1 commit 6d29aaa

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

src/main.rs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use rustc_serialize::Decodable;
3030
use rustc_serialize::json;
3131

3232
use curl::easy::Easy;
33-
use git2::{Repository, Cred};
33+
use git2::{Repository, Cred, CredentialType};
3434
use sha2::{Digest, Sha256};
3535
use clap::{App, Arg};
3636

@@ -44,7 +44,14 @@ use router::Router;
4444
#[derive(Debug, RustcDecodable, Clone)]
4545
struct GitConfig {
4646
upstream_url: String,
47-
origin_url: Option<String>,
47+
origin: Option<OriginConfig>,
48+
}
49+
50+
#[derive(Debug, RustcDecodable, Clone)]
51+
struct OriginConfig {
52+
url: String,
53+
username: Option<String>,
54+
password: Option<String>,
4855
}
4956

5057
#[derive(Debug, RustcDecodable, Clone)]
@@ -344,10 +351,11 @@ fn main() {
344351
.unwrap();
345352

346353

347-
if let Some(remote) = config.registry_config.origin_url.clone() {
354+
if let Some(ref remote) = config.registry_config.origin {
348355
let mut callbacks = git2::RemoteCallbacks::new();
349-
callbacks.credentials(credentials);
350-
repo.remote("local", &remote).unwrap();
356+
callbacks.credentials(|url, user, cred| credentials(url, user, cred, remote));
357+
callbacks.transfer_progress(progress_monitor);
358+
repo.remote("local", &remote.url).unwrap();
351359
let mut remote = repo.find_remote("local").unwrap();
352360
let mut opts = git2::PushOptions::new();
353361
opts.remote_callbacks(callbacks);
@@ -381,7 +389,8 @@ fn main() {
381389
debug!("startup finished");
382390
println!("finish to setup crates.io mirror. Point cargo repository to {}",
383391
config.registry_config
384-
.origin_url
392+
.origin
393+
.map(|o| o.url)
385394
.clone()
386395
.unwrap_or(format!("file://{}", base_dir.to_str().unwrap())));
387396

@@ -390,6 +399,14 @@ fn main() {
390399
.unwrap();
391400
}
392401

402+
fn progress_monitor(progress: git2::Progress) -> bool {
403+
debug!("total :{}, local: {}, remote: {}",
404+
progress.total_objects(),
405+
progress.local_objects(),
406+
progress.received_objects());
407+
true
408+
}
409+
393410

394411
fn credentials(url: &str,
395412
user_from_url: Option<&str>,
@@ -493,17 +510,18 @@ fn try_merge(repo: &Repository,
493510
let sig = try!(repo.signature());
494511
try!(repo.commit(Some("HEAD"), &sig, &sig, "Merge", &tree, &[&parent]));
495512

496-
if let Some(_) = config.registry_config.origin_url {
513+
if let Some(ref remote) = config.registry_config.origin {
497514
let mut callbacks = git2::RemoteCallbacks::new();
498-
callbacks.credentials(credentials);
515+
callbacks.credentials(|url, user, cred| credentials(url, user, cred, remote));
516+
callbacks.transfer_progress(progress_monitor);
499517
let mut remote = try!(repo.find_remote("local"));
500518
let mut opts = git2::PushOptions::new();
501519
opts.remote_callbacks(callbacks);
502520
try!(remote.push(&["refs/heads/master"], Some(&mut opts)));
503521
}
504522
debug!("updated index");
505523
} else {
506-
debug!("Nothing to update");
524+
trace!("Nothing to update");
507525
try!(repo.cleanup_state());
508526
}
509527
Ok(())

0 commit comments

Comments
 (0)