Skip to content

Commit 1b82184

Browse files
committed
refactor(package): make a workspace-shared vcs info builder
This is aimed to cache duplicate works like file system or VCS info lookups.
1 parent 0276088 commit 1b82184

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/cargo/ops/cargo_package/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ fn do_package<'a>(
225225

226226
// Packages need to be created in dependency order, because dependencies must
227227
// be added to our local overlay before we can create lockfiles that depend on them.
228+
let mut vcs_info_builder = vcs::VcsInfoBuilder::new(ws, opts);
228229
let sorted_pkgs = deps.sort();
229230
let mut outputs: Vec<(Package, PackageOpts<'_>, FileLock)> = Vec::new();
230231
for (pkg, cli_features) in sorted_pkgs {
@@ -233,7 +234,7 @@ fn do_package<'a>(
233234
to_package: ops::Packages::Default,
234235
..opts.clone()
235236
};
236-
let ar_files = prepare_archive(ws, &pkg, &opts)?;
237+
let ar_files = prepare_archive(ws, &pkg, &opts, &mut vcs_info_builder)?;
237238

238239
if opts.list {
239240
for ar_file in &ar_files {
@@ -368,6 +369,7 @@ fn prepare_archive(
368369
ws: &Workspace<'_>,
369370
pkg: &Package,
370371
opts: &PackageOpts<'_>,
372+
vcs_info_builder: &mut vcs::VcsInfoBuilder<'_, '_>,
371373
) -> CargoResult<Vec<ArchiveFile>> {
372374
let gctx = ws.gctx();
373375
let mut src = PathSource::new(pkg.root(), pkg.package_id().source_id(), gctx);
@@ -386,7 +388,7 @@ fn prepare_archive(
386388
let src_files = src.list_files(pkg)?;
387389

388390
// Check (git) repository state, getting the current commit hash.
389-
let vcs_info = vcs::check_repo_state(pkg, &src_files, gctx, &opts)?;
391+
let vcs_info = vcs_info_builder.build(pkg, &src_files)?;
390392

391393
build_ar_list(ws, pkg, src_files, vcs_info)
392394
}

src/cargo/ops/cargo_package/vcs.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use serde::Serialize;
99
use tracing::debug;
1010

1111
use crate::core::Package;
12+
use crate::core::Workspace;
1213
use crate::CargoResult;
1314
use crate::GlobalContext;
1415

@@ -31,6 +32,28 @@ pub struct GitVcsInfo {
3132
dirty: bool,
3233
}
3334

35+
/// A shared builder for generating [`VcsInfo`] for packages inside the same workspace.
36+
///
37+
/// This is aimed to cache duplicate works like file system or VCS info lookups.
38+
pub struct VcsInfoBuilder<'a, 'gctx> {
39+
ws: &'a Workspace<'gctx>,
40+
opts: &'a PackageOpts<'gctx>,
41+
}
42+
43+
impl<'a, 'gctx> VcsInfoBuilder<'a, 'gctx> {
44+
pub fn new(
45+
ws: &'a Workspace<'gctx>,
46+
opts: &'a PackageOpts<'gctx>,
47+
) -> VcsInfoBuilder<'a, 'gctx> {
48+
VcsInfoBuilder { ws, opts }
49+
}
50+
51+
/// Builds an [`VcsInfo`] for the given `pkg` and its associated `src_files`.
52+
pub fn build(&mut self, pkg: &Package, src_files: &[PathBuf]) -> CargoResult<Option<VcsInfo>> {
53+
check_repo_state(pkg, src_files, self.ws.gctx(), self.opts)
54+
}
55+
}
56+
3457
/// Checks if the package source is in a *git* DVCS repository.
3558
///
3659
/// If *git*, and the source is *dirty* (e.g., has uncommitted changes),

0 commit comments

Comments
 (0)