@@ -81,6 +81,9 @@ struct VcsInfo {
8181#[ derive( Serialize ) ]
8282struct GitVcsInfo {
8383 sha1 : String ,
84+ /// Indicate whether or not the Git worktree is dirty.
85+ #[ serde( skip_serializing_if = "std::ops::Not::not" ) ]
86+ dirty : bool ,
8487}
8588
8689/// Packages a single package in a workspace, returning the resulting tar file.
@@ -235,14 +238,8 @@ fn prepare_archive(
235238 }
236239 let src_files = src. list_files ( pkg) ?;
237240
238- // Check (git) repository state, getting the current commit hash if not
239- // dirty.
240- let vcs_info = if !opts. allow_dirty {
241- // This will error if a dirty repo is found.
242- check_repo_state ( pkg, & src_files, gctx) ?
243- } else {
244- None
245- } ;
241+ // Check (git) repository state, getting the current commit hash.
242+ let vcs_info = check_repo_state ( pkg, & src_files, gctx, & opts) ?;
246243
247244 build_ar_list ( ws, pkg, src_files, vcs_info)
248245}
@@ -559,13 +556,15 @@ fn check_metadata(pkg: &Package, gctx: &GlobalContext) -> CargoResult<()> {
559556}
560557
561558/// Checks if the package source is in a *git* DVCS repository. If *git*, and
562- /// the source is *dirty* (e.g., has uncommitted changes) then `bail!` with an
563- /// informative message. Otherwise return the sha1 hash of the current *HEAD*
564- /// commit, or `None` if no repo is found.
559+ /// the source is *dirty* (e.g., has uncommitted changes), and `--allow-dirty`
560+ /// has not been passed, then `bail!` with an informative message. Otherwise
561+ /// return the sha1 hash of the current *HEAD* commit, or `None` if no repo is
562+ /// found.
565563fn check_repo_state (
566564 p : & Package ,
567565 src_files : & [ PathBuf ] ,
568566 gctx : & GlobalContext ,
567+ opts : & PackageOpts < ' _ > ,
569568) -> CargoResult < Option < VcsInfo > > {
570569 if let Ok ( repo) = git2:: Repository :: discover ( p. root ( ) ) {
571570 if let Some ( workdir) = repo. workdir ( ) {
@@ -585,7 +584,7 @@ fn check_repo_state(
585584 . unwrap_or ( "" )
586585 . replace ( "\\ " , "/" ) ;
587586 return Ok ( Some ( VcsInfo {
588- git : git ( p, src_files, & repo) ?,
587+ git : git ( p, src_files, & repo, & opts ) ?,
589588 path_in_vcs,
590589 } ) ) ;
591590 }
@@ -608,7 +607,12 @@ fn check_repo_state(
608607 // directory is dirty or not, thus we have to assume that it's clean.
609608 return Ok ( None ) ;
610609
611- fn git ( p : & Package , src_files : & [ PathBuf ] , repo : & git2:: Repository ) -> CargoResult < GitVcsInfo > {
610+ fn git (
611+ p : & Package ,
612+ src_files : & [ PathBuf ] ,
613+ repo : & git2:: Repository ,
614+ opts : & PackageOpts < ' _ > ,
615+ ) -> CargoResult < GitVcsInfo > {
612616 // This is a collection of any dirty or untracked files. This covers:
613617 // - new/modified/deleted/renamed/type change (index or worktree)
614618 // - untracked files (which are "new" worktree files)
@@ -633,10 +637,12 @@ fn check_repo_state(
633637 . to_string ( )
634638 } )
635639 . collect ( ) ;
636- if dirty_src_files. is_empty ( ) {
640+ let dirty = !dirty_src_files. is_empty ( ) ;
641+ if !dirty || opts. allow_dirty {
637642 let rev_obj = repo. revparse_single ( "HEAD" ) ?;
638643 Ok ( GitVcsInfo {
639644 sha1 : rev_obj. id ( ) . to_string ( ) ,
645+ dirty,
640646 } )
641647 } else {
642648 anyhow:: bail!(
0 commit comments