@@ -87,8 +87,7 @@ struct VcsInfo {
87
87
88
88
#[ derive( Serialize ) ]
89
89
struct GitVcsInfo {
90
- #[ serde( skip_serializing_if = "Option::is_none" ) ]
91
- sha1 : Option < String > ,
90
+ sha1 : String ,
92
91
/// Indicate whether or not the Git worktree is dirty.
93
92
#[ serde( skip_serializing_if = "std::ops::Not::not" ) ]
94
93
dirty : bool ,
@@ -744,10 +743,12 @@ fn check_repo_state(
744
743
. and_then ( |p| p. to_str ( ) )
745
744
. unwrap_or ( "" )
746
745
. replace ( "\\ " , "/" ) ;
747
- return Ok ( Some ( VcsInfo {
748
- git : git ( p, src_files, & repo, & opts) ?,
749
- path_in_vcs,
750
- } ) ) ;
746
+ let Some ( git) = git ( p, src_files, & repo, & opts) ? else {
747
+ // If the git repo lacks essensial field like `sha1`, and since this field exists from the beginning,
748
+ // then don't generate the corresponding file in order to maintain consistency with past behavior.
749
+ return Ok ( None ) ;
750
+ } ;
751
+ return Ok ( Some ( VcsInfo { git, path_in_vcs } ) ) ;
751
752
}
752
753
}
753
754
gctx. shell ( ) . verbose ( |shell| {
@@ -773,7 +774,7 @@ fn check_repo_state(
773
774
src_files : & [ PathBuf ] ,
774
775
repo : & git2:: Repository ,
775
776
opts : & PackageOpts < ' _ > ,
776
- ) -> CargoResult < GitVcsInfo > {
777
+ ) -> CargoResult < Option < GitVcsInfo > > {
777
778
// This is a collection of any dirty or untracked files. This covers:
778
779
// - new/modified/deleted/renamed/type change (index or worktree)
779
780
// - untracked files (which are "new" worktree files)
@@ -800,14 +801,16 @@ fn check_repo_state(
800
801
. collect ( ) ;
801
802
let dirty = !dirty_src_files. is_empty ( ) ;
802
803
if !dirty || opts. allow_dirty {
804
+ // Must check whetherthe repo has no commit firstly, otherwise `revparse_single` would fail on bare commit repo.
805
+ // Due to lacking the `sha1` field, it's better not record the `GitVcsInfo` for consistency.
803
806
if repo. is_empty ( ) ? {
804
- return Ok ( GitVcsInfo { sha1 : None , dirty } ) ;
807
+ return Ok ( None ) ;
805
808
}
806
809
let rev_obj = repo. revparse_single ( "HEAD" ) ?;
807
- Ok ( GitVcsInfo {
808
- sha1 : Some ( rev_obj. id ( ) . to_string ( ) ) ,
810
+ Ok ( Some ( GitVcsInfo {
811
+ sha1 : rev_obj. id ( ) . to_string ( ) ,
809
812
dirty,
810
- } )
813
+ } ) )
811
814
} else {
812
815
anyhow:: bail!(
813
816
"{} files in the working directory contain changes that were \
0 commit comments