@@ -20,6 +20,7 @@ use cargo_util::paths;
20
20
use flate2:: read:: GzDecoder ;
21
21
use flate2:: { Compression , GzBuilder } ;
22
22
use log:: debug;
23
+ use serde:: Serialize ;
23
24
use tar:: { Archive , Builder , EntryType , Header , HeaderMode } ;
24
25
25
26
pub struct PackageOpts < ' cfg > {
@@ -58,8 +59,18 @@ enum GeneratedFile {
58
59
Manifest ,
59
60
/// Generates `Cargo.lock` in some cases (like if there is a binary).
60
61
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 ,
63
74
}
64
75
65
76
pub fn package_one (
@@ -88,7 +99,6 @@ pub fn package_one(
88
99
let vcs_info = if !opts. allow_dirty {
89
100
// This will error if a dirty repo is found.
90
101
check_repo_state ( pkg, & src_files, config) ?
91
- . map ( |h| format ! ( "{{\n \" git\" : {{\n \" sha1\" : \" {}\" \n }}\n }}\n " , h) )
92
102
} else {
93
103
None
94
104
} ;
@@ -189,7 +199,7 @@ fn build_ar_list(
189
199
ws : & Workspace < ' _ > ,
190
200
pkg : & Package ,
191
201
src_files : Vec < PathBuf > ,
192
- vcs_info : Option < String > ,
202
+ vcs_info : Option < VcsInfo > ,
193
203
) -> CargoResult < Vec < ArchiveFile > > {
194
204
let mut result = Vec :: new ( ) ;
195
205
let root = pkg. root ( ) ;
@@ -386,7 +396,7 @@ fn check_repo_state(
386
396
p : & Package ,
387
397
src_files : & [ PathBuf ] ,
388
398
config : & Config ,
389
- ) -> CargoResult < Option < String > > {
399
+ ) -> CargoResult < Option < VcsInfo > > {
390
400
if let Ok ( repo) = git2:: Repository :: discover ( p. root ( ) ) {
391
401
if let Some ( workdir) = repo. workdir ( ) {
392
402
debug ! ( "found a git repo at {:?}" , workdir) ;
@@ -398,7 +408,9 @@ fn check_repo_state(
398
408
"found (git) Cargo.toml at {:?} in workdir {:?}" ,
399
409
path, workdir
400
410
) ;
401
- return git ( p, src_files, & repo) ;
411
+ return Ok ( Some ( VcsInfo {
412
+ git : git ( p, src_files, & repo) ?,
413
+ } ) ) ;
402
414
}
403
415
}
404
416
config. shell ( ) . verbose ( |shell| {
@@ -419,11 +431,7 @@ fn check_repo_state(
419
431
// directory is dirty or not, thus we have to assume that it's clean.
420
432
return Ok ( None ) ;
421
433
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 > {
427
435
// This is a collection of any dirty or untracked files. This covers:
428
436
// - new/modified/deleted/renamed/type change (index or worktree)
429
437
// - untracked files (which are "new" worktree files)
@@ -450,7 +458,9 @@ fn check_repo_state(
450
458
. collect ( ) ;
451
459
if dirty_src_files. is_empty ( ) {
452
460
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
+ } )
454
464
} else {
455
465
anyhow:: bail!(
456
466
"{} files in the working directory contain changes that were \
@@ -562,7 +572,7 @@ fn tar(
562
572
let contents = match generated_kind {
563
573
GeneratedFile :: Manifest => pkg. to_registry_toml ( ws) ?,
564
574
GeneratedFile :: Lockfile => build_lock ( ws, pkg) ?,
565
- GeneratedFile :: VcsInfo ( s) => s ,
575
+ GeneratedFile :: VcsInfo ( ref s) => serde_json :: to_string_pretty ( s ) ? ,
566
576
} ;
567
577
header. set_entry_type ( EntryType :: file ( ) ) ;
568
578
header. set_mode ( 0o644 ) ;
0 commit comments