Skip to content

Commit

Permalink
refactor(package): make a workspace-shared vcs info builder
Browse files Browse the repository at this point in the history
This is aimed to cache duplicate works like file system or VCS info lookups.
  • Loading branch information
weihanglo committed Dec 26, 2024
1 parent 0276088 commit 1b82184
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/cargo/ops/cargo_package/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ fn do_package<'a>(

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

if opts.list {
for ar_file in &ar_files {
Expand Down Expand Up @@ -368,6 +369,7 @@ fn prepare_archive(
ws: &Workspace<'_>,
pkg: &Package,
opts: &PackageOpts<'_>,
vcs_info_builder: &mut vcs::VcsInfoBuilder<'_, '_>,
) -> CargoResult<Vec<ArchiveFile>> {
let gctx = ws.gctx();
let mut src = PathSource::new(pkg.root(), pkg.package_id().source_id(), gctx);
Expand All @@ -386,7 +388,7 @@ fn prepare_archive(
let src_files = src.list_files(pkg)?;

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

build_ar_list(ws, pkg, src_files, vcs_info)
}
Expand Down
23 changes: 23 additions & 0 deletions src/cargo/ops/cargo_package/vcs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use serde::Serialize;
use tracing::debug;

use crate::core::Package;
use crate::core::Workspace;
use crate::CargoResult;
use crate::GlobalContext;

Expand All @@ -31,6 +32,28 @@ pub struct GitVcsInfo {
dirty: bool,
}

/// A shared builder for generating [`VcsInfo`] for packages inside the same workspace.
///
/// This is aimed to cache duplicate works like file system or VCS info lookups.
pub struct VcsInfoBuilder<'a, 'gctx> {
ws: &'a Workspace<'gctx>,
opts: &'a PackageOpts<'gctx>,
}

impl<'a, 'gctx> VcsInfoBuilder<'a, 'gctx> {
pub fn new(
ws: &'a Workspace<'gctx>,
opts: &'a PackageOpts<'gctx>,
) -> VcsInfoBuilder<'a, 'gctx> {
VcsInfoBuilder { ws, opts }
}

/// Builds an [`VcsInfo`] for the given `pkg` and its associated `src_files`.
pub fn build(&mut self, pkg: &Package, src_files: &[PathBuf]) -> CargoResult<Option<VcsInfo>> {
check_repo_state(pkg, src_files, self.ws.gctx(), self.opts)
}
}

/// Checks if the package source is in a *git* DVCS repository.
///
/// If *git*, and the source is *dirty* (e.g., has uncommitted changes),
Expand Down

0 comments on commit 1b82184

Please sign in to comment.