Skip to content

Commit 8da3cf7

Browse files
committed
Auto merge of #9865 - nipunn1313:pkg_in_repo, r=alexcrichton
Use serde_json to generate cargo_vcs_info.json Should make it easier to add fields in the future
2 parents 18751dd + 08abcc7 commit 8da3cf7

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

src/cargo/ops/cargo_package.rs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use cargo_util::paths;
2020
use flate2::read::GzDecoder;
2121
use flate2::{Compression, GzBuilder};
2222
use log::debug;
23+
use serde::Serialize;
2324
use tar::{Archive, Builder, EntryType, Header, HeaderMode};
2425

2526
pub struct PackageOpts<'cfg> {
@@ -58,8 +59,18 @@ enum GeneratedFile {
5859
Manifest,
5960
/// Generates `Cargo.lock` in some cases (like if there is a binary).
6061
Lockfile,
61-
/// Adds a `.cargo-vcs_info.json` file if in a (clean) git repo.
62-
VcsInfo(String),
62+
/// Adds a `.cargo_vcs_info.json` file if in a (clean) git repo.
63+
VcsInfo(VcsInfo),
64+
}
65+
66+
#[derive(Serialize)]
67+
struct VcsInfo {
68+
git: GitVcsInfo,
69+
}
70+
71+
#[derive(Serialize)]
72+
struct GitVcsInfo {
73+
sha1: String,
6374
}
6475

6576
pub fn package_one(
@@ -88,7 +99,6 @@ pub fn package_one(
8899
let vcs_info = if !opts.allow_dirty {
89100
// This will error if a dirty repo is found.
90101
check_repo_state(pkg, &src_files, config)?
91-
.map(|h| format!("{{\n \"git\": {{\n \"sha1\": \"{}\"\n }}\n}}\n", h))
92102
} else {
93103
None
94104
};
@@ -189,7 +199,7 @@ fn build_ar_list(
189199
ws: &Workspace<'_>,
190200
pkg: &Package,
191201
src_files: Vec<PathBuf>,
192-
vcs_info: Option<String>,
202+
vcs_info: Option<VcsInfo>,
193203
) -> CargoResult<Vec<ArchiveFile>> {
194204
let mut result = Vec::new();
195205
let root = pkg.root();
@@ -386,7 +396,7 @@ fn check_repo_state(
386396
p: &Package,
387397
src_files: &[PathBuf],
388398
config: &Config,
389-
) -> CargoResult<Option<String>> {
399+
) -> CargoResult<Option<VcsInfo>> {
390400
if let Ok(repo) = git2::Repository::discover(p.root()) {
391401
if let Some(workdir) = repo.workdir() {
392402
debug!("found a git repo at {:?}", workdir);
@@ -398,7 +408,9 @@ fn check_repo_state(
398408
"found (git) Cargo.toml at {:?} in workdir {:?}",
399409
path, workdir
400410
);
401-
return git(p, src_files, &repo);
411+
return Ok(Some(VcsInfo {
412+
git: git(p, src_files, &repo)?,
413+
}));
402414
}
403415
}
404416
config.shell().verbose(|shell| {
@@ -419,11 +431,7 @@ fn check_repo_state(
419431
// directory is dirty or not, thus we have to assume that it's clean.
420432
return Ok(None);
421433

422-
fn git(
423-
p: &Package,
424-
src_files: &[PathBuf],
425-
repo: &git2::Repository,
426-
) -> CargoResult<Option<String>> {
434+
fn git(p: &Package, src_files: &[PathBuf], repo: &git2::Repository) -> CargoResult<GitVcsInfo> {
427435
// This is a collection of any dirty or untracked files. This covers:
428436
// - new/modified/deleted/renamed/type change (index or worktree)
429437
// - untracked files (which are "new" worktree files)
@@ -450,7 +458,9 @@ fn check_repo_state(
450458
.collect();
451459
if dirty_src_files.is_empty() {
452460
let rev_obj = repo.revparse_single("HEAD")?;
453-
Ok(Some(rev_obj.id().to_string()))
461+
Ok(GitVcsInfo {
462+
sha1: rev_obj.id().to_string(),
463+
})
454464
} else {
455465
anyhow::bail!(
456466
"{} files in the working directory contain changes that were \
@@ -562,7 +572,7 @@ fn tar(
562572
let contents = match generated_kind {
563573
GeneratedFile::Manifest => pkg.to_registry_toml(ws)?,
564574
GeneratedFile::Lockfile => build_lock(ws, pkg)?,
565-
GeneratedFile::VcsInfo(s) => s,
575+
GeneratedFile::VcsInfo(ref s) => serde_json::to_string_pretty(s)?,
566576
};
567577
header.set_entry_type(EntryType::file());
568578
header.set_mode(0o644);

0 commit comments

Comments
 (0)