Skip to content

Allow to generate lockfile without registry update #3825

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/bin/generate_lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct Options {
flag_color: Option<String>,
flag_frozen: bool,
flag_locked: bool,
flag_offline: bool,
}

pub const USAGE: &'static str = "
Expand All @@ -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 {
Expand All @@ -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)?;
Expand Down
15 changes: 13 additions & 2 deletions src/cargo/sources/registry/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;
Expand All @@ -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)
})?;
Expand Down
10 changes: 10 additions & 0 deletions src/cargo/util/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub struct Config {
extra_verbose: Cell<bool>,
frozen: Cell<bool>,
locked: Cell<bool>,
disable_registry_update: Cell<bool>,
}

impl Config {
Expand All @@ -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),
}
}

Expand Down Expand Up @@ -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<HashMap<String, ConfigValue>> {
let mut cfg = CV::Table(HashMap::new(), PathBuf::from("."));

Expand Down