Skip to content

Use serde_json to generate cargo_vcs_info.json #9865

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

Merged
merged 1 commit into from
Sep 2, 2021
Merged
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
36 changes: 23 additions & 13 deletions src/cargo/ops/cargo_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use cargo_util::paths;
use flate2::read::GzDecoder;
use flate2::{Compression, GzBuilder};
use log::debug;
use serde::Serialize;
use tar::{Archive, Builder, EntryType, Header, HeaderMode};

pub struct PackageOpts<'cfg> {
Expand Down Expand Up @@ -58,8 +59,18 @@ enum GeneratedFile {
Manifest,
/// Generates `Cargo.lock` in some cases (like if there is a binary).
Lockfile,
/// Adds a `.cargo-vcs_info.json` file if in a (clean) git repo.
VcsInfo(String),
/// Adds a `.cargo_vcs_info.json` file if in a (clean) git repo.
VcsInfo(VcsInfo),
}

#[derive(Serialize)]
struct VcsInfo {
git: GitVcsInfo,
}

#[derive(Serialize)]
struct GitVcsInfo {
sha1: String,
}

pub fn package_one(
Expand Down Expand Up @@ -88,7 +99,6 @@ pub fn package_one(
let vcs_info = if !opts.allow_dirty {
// This will error if a dirty repo is found.
check_repo_state(pkg, &src_files, config)?
.map(|h| format!("{{\n \"git\": {{\n \"sha1\": \"{}\"\n }}\n}}\n", h))
} else {
None
};
Expand Down Expand Up @@ -189,7 +199,7 @@ fn build_ar_list(
ws: &Workspace<'_>,
pkg: &Package,
src_files: Vec<PathBuf>,
vcs_info: Option<String>,
vcs_info: Option<VcsInfo>,
) -> CargoResult<Vec<ArchiveFile>> {
let mut result = Vec::new();
let root = pkg.root();
Expand Down Expand Up @@ -386,7 +396,7 @@ fn check_repo_state(
p: &Package,
src_files: &[PathBuf],
config: &Config,
) -> CargoResult<Option<String>> {
) -> CargoResult<Option<VcsInfo>> {
if let Ok(repo) = git2::Repository::discover(p.root()) {
if let Some(workdir) = repo.workdir() {
debug!("found a git repo at {:?}", workdir);
Expand All @@ -398,7 +408,9 @@ fn check_repo_state(
"found (git) Cargo.toml at {:?} in workdir {:?}",
path, workdir
);
return git(p, src_files, &repo);
return Ok(Some(VcsInfo {
git: git(p, src_files, &repo)?,
}));
}
}
config.shell().verbose(|shell| {
Expand All @@ -419,11 +431,7 @@ fn check_repo_state(
// directory is dirty or not, thus we have to assume that it's clean.
return Ok(None);

fn git(
p: &Package,
src_files: &[PathBuf],
repo: &git2::Repository,
) -> CargoResult<Option<String>> {
fn git(p: &Package, src_files: &[PathBuf], repo: &git2::Repository) -> CargoResult<GitVcsInfo> {
// This is a collection of any dirty or untracked files. This covers:
// - new/modified/deleted/renamed/type change (index or worktree)
// - untracked files (which are "new" worktree files)
Expand All @@ -450,7 +458,9 @@ fn check_repo_state(
.collect();
if dirty_src_files.is_empty() {
let rev_obj = repo.revparse_single("HEAD")?;
Ok(Some(rev_obj.id().to_string()))
Ok(GitVcsInfo {
sha1: rev_obj.id().to_string(),
})
} else {
anyhow::bail!(
"{} files in the working directory contain changes that were \
Expand Down Expand Up @@ -562,7 +572,7 @@ fn tar(
let contents = match generated_kind {
GeneratedFile::Manifest => pkg.to_registry_toml(ws)?,
GeneratedFile::Lockfile => build_lock(ws, pkg)?,
GeneratedFile::VcsInfo(s) => s,
GeneratedFile::VcsInfo(ref s) => serde_json::to_string_pretty(s)?,
};
header.set_entry_type(EntryType::file());
header.set_mode(0o644);
Expand Down