From 4970bf0311793bebf2a8fc33227fcd79dbbd89de Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 13 Mar 2017 15:03:53 +0300 Subject: [PATCH] Allow to generate lockfile without registry update --- src/bin/generate_lockfile.rs | 6 ++++++ src/cargo/sources/registry/remote.rs | 15 +++++++++++++-- src/cargo/util/config.rs | 10 ++++++++++ 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/bin/generate_lockfile.rs b/src/bin/generate_lockfile.rs index f545b1e26f7..0ef89552447 100644 --- a/src/bin/generate_lockfile.rs +++ b/src/bin/generate_lockfile.rs @@ -13,6 +13,7 @@ pub struct Options { flag_color: Option, flag_frozen: bool, flag_locked: bool, + flag_offline: bool, } pub const USAGE: &'static str = " @@ -29,6 +30,8 @@ Options: --color WHEN Coloring: auto, always, never --frozen Require Cargo.lock and cache are up to date --locked Require Cargo.lock is up to date + --offline Do not download crates.io registry and use + a potentially stale local copy "; pub fn execute(options: Options, config: &Config) -> CliResult { @@ -38,6 +41,9 @@ pub fn execute(options: Options, config: &Config) -> CliResult { &options.flag_color, options.flag_frozen, options.flag_locked)?; + if options.flag_offline { + config.disable_registry_update(); + } let root = find_root_manifest_for_wd(options.flag_manifest_path, config.cwd())?; let ws = Workspace::new(&root, config)?; diff --git a/src/cargo/sources/registry/remote.rs b/src/cargo/sources/registry/remote.rs index 67e60a074c9..bda0ad4504c 100644 --- a/src/cargo/sources/registry/remote.rs +++ b/src/cargo/sources/registry/remote.rs @@ -60,7 +60,9 @@ impl<'cfg> RegistryData for RemoteRegistry<'cfg> { // // This way if there's a problem the error gets printed before we even // hit the index, which may not actually read this configuration. - ops::http_handle(self.config)?; + if self.config.registry_update_allowed() { + ops::http_handle(self.config)?; + } // Then we actually update the index self.index_path.create_dir()?; @@ -69,10 +71,19 @@ impl<'cfg> RegistryData for RemoteRegistry<'cfg> { "the registry index")?; let path = lock.path().parent().unwrap(); + let existing_repo = git2::Repository::open(path); + if !self.config.registry_update_allowed() { + return if let Err(e) = existing_repo { + Err(e.into()) + } else { + Ok(()) + } + } + self.config.shell().status("Updating", format!("registry `{}`", self.source_id.url()))?; - let repo = git2::Repository::open(path).or_else(|_| { + let repo = existing_repo.or_else(|_| { let _ = lock.remove_siblings(); git2::Repository::init(path) })?; diff --git a/src/cargo/util/config.rs b/src/cargo/util/config.rs index a715306637c..0c2a795821c 100644 --- a/src/cargo/util/config.rs +++ b/src/cargo/util/config.rs @@ -33,6 +33,7 @@ pub struct Config { extra_verbose: Cell, frozen: Cell, locked: Cell, + disable_registry_update: Cell, } impl Config { @@ -50,6 +51,7 @@ impl Config { extra_verbose: Cell::new(false), frozen: Cell::new(false), locked: Cell::new(false), + disable_registry_update: Cell::new(false), } } @@ -377,6 +379,14 @@ impl Config { !self.frozen.get() && !self.locked.get() } + pub fn registry_update_allowed(&self) -> bool { + !self.disable_registry_update.get() + } + + pub fn disable_registry_update(&self) { + self.disable_registry_update.set(true) + } + fn load_values(&self) -> CargoResult> { let mut cfg = CV::Table(HashMap::new(), PathBuf::from("."));