@@ -33,6 +33,7 @@ pub struct FixOptions<'a> {
3333 pub compile_opts : CompileOptions < ' a > ,
3434 pub allow_dirty : bool ,
3535 pub allow_no_vcs : bool ,
36+ pub allow_staged : bool ,
3637 pub broken_code : bool ,
3738}
3839
@@ -87,39 +88,57 @@ fn check_version_control(opts: &FixOptions) -> CargoResult<()> {
8788 error pass `--allow-no-vcs`")
8889 }
8990
90- if opts. allow_dirty {
91+ if opts. allow_dirty && opts . allow_staged {
9192 return Ok ( ( ) )
9293 }
9394
9495 let mut dirty_files = Vec :: new ( ) ;
96+ let mut staged_files = Vec :: new ( ) ;
9597 if let Ok ( repo) = git2:: Repository :: discover ( config. cwd ( ) ) {
96- let mut opts = git2:: StatusOptions :: new ( ) ;
97- opts. include_ignored ( false ) ;
98- for status in repo. statuses ( Some ( & mut opts) ) ?. iter ( ) {
99- if status. status ( ) != git2:: Status :: CURRENT {
100- if let Some ( path) = status. path ( ) {
101- dirty_files. push ( path. to_string ( ) ) ;
102- }
98+ let mut repo_opts = git2:: StatusOptions :: new ( ) ;
99+ repo_opts. include_ignored ( false ) ;
100+ for status in repo. statuses ( Some ( & mut repo_opts) ) ?. iter ( ) {
101+ if let Some ( path) = status. path ( ) {
102+ match status. status ( ) {
103+ git2:: Status :: CURRENT => ( ) ,
104+ git2:: Status :: INDEX_NEW |
105+ git2:: Status :: INDEX_MODIFIED |
106+ git2:: Status :: INDEX_DELETED |
107+ git2:: Status :: INDEX_RENAMED |
108+ git2:: Status :: INDEX_TYPECHANGE =>
109+ if !opts. allow_staged {
110+ staged_files. push ( path. to_string ( ) )
111+ } ,
112+ _ =>
113+ if !opts. allow_dirty {
114+ dirty_files. push ( path. to_string ( ) )
115+ } ,
116+ } ;
103117 }
104118
105119 }
106120 }
107121
108- if dirty_files. is_empty ( ) {
122+ if dirty_files. is_empty ( ) && staged_files . is_empty ( ) {
109123 return Ok ( ( ) )
110124 }
111125
112126 let mut files_list = String :: new ( ) ;
113127 for file in dirty_files {
114128 files_list. push_str ( " * " ) ;
115129 files_list. push_str ( & file) ;
116- files_list. push_str ( "\n " ) ;
130+ files_list. push_str ( " (dirty)\n " ) ;
131+ }
132+ for file in staged_files {
133+ files_list. push_str ( " * " ) ;
134+ files_list. push_str ( & file) ;
135+ files_list. push_str ( " (staged)\n " ) ;
117136 }
118137
119- bail ! ( "the working directory of this project is detected as dirty , and \
138+ bail ! ( "the working directory of this project has uncommitted changes , and \
120139 `cargo fix` can potentially perform destructive changes; if you'd \
121- like to suppress this error pass `--allow-dirty`, or commit the \
122- changes to these files:\n \
140+ like to suppress this error pass `--allow-dirty`, `--allow-staged`, \
141+ or commit the changes to these files:\n \
123142 \n \
124143 {}\n \
125144 ", files_list) ;
0 commit comments