File tree Expand file tree Collapse file tree 3 files changed +37
-4
lines changed
crates/cargo-test-support/src Expand file tree Collapse file tree 3 files changed +37
-4
lines changed Original file line number Diff line number Diff line change @@ -175,6 +175,20 @@ where
175
175
( git_project, repo)
176
176
}
177
177
178
+ /// Create a new git repository with a project.
179
+ /// Returns both the Project and the git Repository but does not commit.
180
+ pub fn new_repo_without_add_and_commit < F > ( name : & str , callback : F ) -> ( Project , git2:: Repository )
181
+ where
182
+ F : FnOnce ( ProjectBuilder ) -> ProjectBuilder ,
183
+ {
184
+ let mut git_project = project ( ) . at ( name) ;
185
+ git_project = callback ( git_project) ;
186
+ let git_project = git_project. build ( ) ;
187
+
188
+ let repo = init ( & git_project. root ( ) ) ;
189
+ ( git_project, repo)
190
+ }
191
+
178
192
/// Add all files in the working directory to the git index.
179
193
pub fn add ( repo : & git2:: Repository ) {
180
194
// FIXME(libgit2/libgit2#2514): apparently, `add_all` will add all submodules
Original file line number Diff line number Diff line change @@ -154,11 +154,11 @@ fn check_version_control(config: &Config, opts: &FixOptions) -> CargoResult<()>
154
154
if let Ok ( repo) = git2:: Repository :: discover ( config. cwd ( ) ) {
155
155
let mut repo_opts = git2:: StatusOptions :: new ( ) ;
156
156
repo_opts. include_ignored ( false ) ;
157
- if repo. is_empty ( ) ? {
157
+ if repo. is_empty ( ) ? && !opts . allow_dirty {
158
158
bail ! (
159
159
"no commits found in the git repository, and \
160
160
`cargo fix` can potentially perform destructive changes; if you'd \
161
- like to suppress this error pass `--allow-dirty`, `--allow-staged`, \
161
+ like to suppress this error pass `--allow-dirty`, \
162
162
or commit your changes"
163
163
)
164
164
}
Original file line number Diff line number Diff line change @@ -771,6 +771,25 @@ commit the changes to these files:
771
771
p. cargo ( "fix --allow-staged" ) . run ( ) ;
772
772
}
773
773
774
+ #[ cargo_test]
775
+ fn errors_on_empty_repo ( ) {
776
+ let ( p, _) =
777
+ git:: new_repo_without_add_and_commit ( "foo" , |p| p. file ( "src/lib.rs" , "pub fn foo() {}" ) ) ;
778
+
779
+ p. cargo ( "fix" )
780
+ . with_status ( 101 )
781
+ . with_stderr (
782
+ "\
783
+ error: no commits found in the git repository, \
784
+ and `cargo fix` can potentially perform destructive changes; \
785
+ if you'd like to suppress this error pass `--allow-dirty`, \
786
+ or commit your changes
787
+ " ,
788
+ )
789
+ . run ( ) ;
790
+ p. cargo ( "fix --allow-dirty" ) . run ( ) ;
791
+ }
792
+
774
793
#[ cargo_test]
775
794
fn does_not_warn_about_clean_working_directory ( ) {
776
795
let p = git:: new ( "foo" , |p| p. file ( "src/lib.rs" , "pub fn foo() {}" ) ) ;
@@ -1715,7 +1734,7 @@ fn fix_with_run_cargo_in_proc_macros() {
1715
1734
"src/lib.rs" ,
1716
1735
r#"
1717
1736
use proc_macro::*;
1718
-
1737
+
1719
1738
#[proc_macro]
1720
1739
pub fn foo(_input: TokenStream) -> TokenStream {
1721
1740
let output = std::process::Command::new(env!("CARGO"))
@@ -1725,7 +1744,7 @@ fn fix_with_run_cargo_in_proc_macros() {
1725
1744
eprintln!("{}", std::str::from_utf8(&output.stderr).unwrap());
1726
1745
println!("{}", std::str::from_utf8(&output.stdout).unwrap());
1727
1746
"".parse().unwrap()
1728
- }
1747
+ }
1729
1748
"# ,
1730
1749
)
1731
1750
. file (
You can’t perform that action at this time.
0 commit comments