Skip to content

Commit 4970bf0

Browse files
committed
Allow to generate lockfile without registry update
1 parent 4a3c0a6 commit 4970bf0

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

src/bin/generate_lockfile.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub struct Options {
1313
flag_color: Option<String>,
1414
flag_frozen: bool,
1515
flag_locked: bool,
16+
flag_offline: bool,
1617
}
1718

1819
pub const USAGE: &'static str = "
@@ -29,6 +30,8 @@ Options:
2930
--color WHEN Coloring: auto, always, never
3031
--frozen Require Cargo.lock and cache are up to date
3132
--locked Require Cargo.lock is up to date
33+
--offline Do not download crates.io registry and use
34+
a potentially stale local copy
3235
";
3336

3437
pub fn execute(options: Options, config: &Config) -> CliResult {
@@ -38,6 +41,9 @@ pub fn execute(options: Options, config: &Config) -> CliResult {
3841
&options.flag_color,
3942
options.flag_frozen,
4043
options.flag_locked)?;
44+
if options.flag_offline {
45+
config.disable_registry_update();
46+
}
4147
let root = find_root_manifest_for_wd(options.flag_manifest_path, config.cwd())?;
4248

4349
let ws = Workspace::new(&root, config)?;

src/cargo/sources/registry/remote.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ impl<'cfg> RegistryData for RemoteRegistry<'cfg> {
6060
//
6161
// This way if there's a problem the error gets printed before we even
6262
// hit the index, which may not actually read this configuration.
63-
ops::http_handle(self.config)?;
63+
if self.config.registry_update_allowed() {
64+
ops::http_handle(self.config)?;
65+
}
6466

6567
// Then we actually update the index
6668
self.index_path.create_dir()?;
@@ -69,10 +71,19 @@ impl<'cfg> RegistryData for RemoteRegistry<'cfg> {
6971
"the registry index")?;
7072
let path = lock.path().parent().unwrap();
7173

74+
let existing_repo = git2::Repository::open(path);
75+
if !self.config.registry_update_allowed() {
76+
return if let Err(e) = existing_repo {
77+
Err(e.into())
78+
} else {
79+
Ok(())
80+
}
81+
}
82+
7283
self.config.shell().status("Updating",
7384
format!("registry `{}`", self.source_id.url()))?;
7485

75-
let repo = git2::Repository::open(path).or_else(|_| {
86+
let repo = existing_repo.or_else(|_| {
7687
let _ = lock.remove_siblings();
7788
git2::Repository::init(path)
7889
})?;

src/cargo/util/config.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ pub struct Config {
3333
extra_verbose: Cell<bool>,
3434
frozen: Cell<bool>,
3535
locked: Cell<bool>,
36+
disable_registry_update: Cell<bool>,
3637
}
3738

3839
impl Config {
@@ -50,6 +51,7 @@ impl Config {
5051
extra_verbose: Cell::new(false),
5152
frozen: Cell::new(false),
5253
locked: Cell::new(false),
54+
disable_registry_update: Cell::new(false),
5355
}
5456
}
5557

@@ -377,6 +379,14 @@ impl Config {
377379
!self.frozen.get() && !self.locked.get()
378380
}
379381

382+
pub fn registry_update_allowed(&self) -> bool {
383+
!self.disable_registry_update.get()
384+
}
385+
386+
pub fn disable_registry_update(&self) {
387+
self.disable_registry_update.set(true)
388+
}
389+
380390
fn load_values(&self) -> CargoResult<HashMap<String, ConfigValue>> {
381391
let mut cfg = CV::Table(HashMap::new(), PathBuf::from("."));
382392

0 commit comments

Comments
 (0)