diff --git a/src/cargo/ops/cargo_package.rs b/src/cargo/ops/cargo_package.rs index 67be829fa57..48477ea25e0 100644 --- a/src/cargo/ops/cargo_package.rs +++ b/src/cargo/ops/cargo_package.rs @@ -66,6 +66,8 @@ enum GeneratedFile { #[derive(Serialize)] struct VcsInfo { git: GitVcsInfo, + /// Path to the package within repo (empty string if root). / not \ + path_in_vcs: String, } #[derive(Serialize)] @@ -408,8 +410,14 @@ fn check_repo_state( "found (git) Cargo.toml at {:?} in workdir {:?}", path, workdir ); + let path_in_vcs = path + .parent() + .and_then(|p| p.to_str()) + .unwrap_or("") + .replace("\\", "/"); return Ok(Some(VcsInfo { git: git(p, src_files, &repo)?, + path_in_vcs, })); } } diff --git a/src/doc/man/cargo-package.md b/src/doc/man/cargo-package.md index 4e84cf66432..32d936f8422 100644 --- a/src/doc/man/cargo-package.md +++ b/src/doc/man/cargo-package.md @@ -43,6 +43,22 @@ fields in the manifest. See [the reference](../reference/publishing.html) for more details about packaging and publishing. +### .cargo_vcs_info.json format + +Will generate a `.cargo_vcs_info.json` in the following format + +```javascript +{ + "git": { + "sha1": "aac20b6e7e543e6dd4118b246c77225e3a3a1302" + }, + "path_in_vcs": "" +} +``` + +`path_in_vcs` will be set to a repo-relative path for packages +in subdirectories of the version control repository. + ## OPTIONS ### Package Options diff --git a/src/doc/man/generated_txt/cargo-package.txt b/src/doc/man/generated_txt/cargo-package.txt index 018a306381b..b24b8ad3502 100644 --- a/src/doc/man/generated_txt/cargo-package.txt +++ b/src/doc/man/generated_txt/cargo-package.txt @@ -45,6 +45,19 @@ DESCRIPTION for more details about packaging and publishing. + .cargo_vcs_info.json format + Will generate a .cargo_vcs_info.json in the following format + + { + "git": { + "sha1": "aac20b6e7e543e6dd4118b246c77225e3a3a1302" + }, + "path_in_vcs": "" + } + + path_in_vcs will be set to a repo-relative path for packages in + subdirectories of the version control repository. + OPTIONS Package Options -l, --list diff --git a/src/doc/src/commands/cargo-package.md b/src/doc/src/commands/cargo-package.md index b98e746bfc0..2d7ba1f4fa6 100644 --- a/src/doc/src/commands/cargo-package.md +++ b/src/doc/src/commands/cargo-package.md @@ -43,6 +43,22 @@ fields in the manifest. See [the reference](../reference/publishing.html) for more details about packaging and publishing. +### .cargo_vcs_info.json format + +Will generate a `.cargo_vcs_info.json` in the following format + +```javascript +{ + "git": { + "sha1": "aac20b6e7e543e6dd4118b246c77225e3a3a1302" + }, + "path_in_vcs": "" +} +``` + +`path_in_vcs` will be set to a repo-relative path for packages +in subdirectories of the version control repository. + ## OPTIONS ### Package Options diff --git a/src/etc/man/cargo-package.1 b/src/etc/man/cargo-package.1 index c7a7336a1d6..6813a7868a4 100644 --- a/src/etc/man/cargo-package.1 +++ b/src/etc/man/cargo-package.1 @@ -67,6 +67,22 @@ fields in the manifest. .sp See \fIthe reference\fR for more details about packaging and publishing. +.SS ".cargo_vcs_info.json format" +Will generate a \fB\&.cargo_vcs_info.json\fR in the following format +.sp +.RS 4 +.nf +{ + "git": { + "sha1": "aac20b6e7e543e6dd4118b246c77225e3a3a1302" + }, + "path_in_vcs": "" +} +.fi +.RE +.sp +\fBpath_in_vcs\fR will be set to a repo\-relative path for packages +in subdirectories of the version control repository. .SH "OPTIONS" .SS "Package Options" .sp diff --git a/tests/testsuite/package.rs b/tests/testsuite/package.rs index 600cbd1fbd8..a284a1e3c9c 100644 --- a/tests/testsuite/package.rs +++ b/tests/testsuite/package.rs @@ -140,8 +140,8 @@ fn package_verbose() { let repo = git::repo(&root) .file("Cargo.toml", &basic_manifest("foo", "0.0.1")) .file("src/main.rs", "fn main() {}") - .file("a/Cargo.toml", &basic_manifest("a", "0.0.1")) - .file("a/src/lib.rs", "") + .file("a/a/Cargo.toml", &basic_manifest("a", "0.0.1")) + .file("a/a/src/lib.rs", "") .build(); cargo_process("build").cwd(repo.root()).run(); @@ -167,7 +167,8 @@ See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for r#"{{ "git": {{ "sha1": "{}" - }} + }}, + "path_in_vcs": "" }} "#, repo.revparse_head() @@ -187,7 +188,7 @@ See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for println!("package sub-repo"); cargo_process("package -v --no-verify") - .cwd(repo.root().join("a")) + .cwd(repo.root().join("a/a")) .with_stderr( "\ [WARNING] manifest has no description[..] @@ -200,6 +201,29 @@ See https://doc.rust-lang.org/cargo/reference/manifest.html#package-metadata for ", ) .run(); + + let f = File::open(&repo.root().join("a/a/target/package/a-0.0.1.crate")).unwrap(); + let vcs_contents = format!( + r#"{{ + "git": {{ + "sha1": "{}" + }}, + "path_in_vcs": "a/a" +}} +"#, + repo.revparse_head() + ); + validate_crate_contents( + f, + "a-0.0.1.crate", + &[ + "Cargo.toml", + "Cargo.toml.orig", + "src/lib.rs", + ".cargo_vcs_info.json", + ], + &[(".cargo_vcs_info.json", &vcs_contents)], + ); } #[cargo_test]