Skip to content

Commit a2ea66b

Browse files
committed
Auto merge of #11400 - hi-rustin:rustin-patch-fix-error, r=epage
Add error message when `cargo fix` on an empty repo ### What does this PR try to resolve? close #11380 Add error message when `cargo fix` on an empty repo. ### How should we test and review this PR? - [x] unit test ```sh set -eux cargo +nightly new repro cd repro echo "fn main() { let _ = 0.clone(); }" > src/main.rs cargo fix ```
2 parents a004f94 + 21b2fe9 commit a2ea66b

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

src/cargo/ops/fix.rs

+1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ fn check_version_control(config: &Config, opts: &FixOptions) -> CargoResult<()>
154154
if let Ok(repo) = git2::Repository::discover(config.cwd()) {
155155
let mut repo_opts = git2::StatusOptions::new();
156156
repo_opts.include_ignored(false);
157+
repo_opts.include_untracked(true);
157158
for status in repo.statuses(Some(&mut repo_opts))?.iter() {
158159
if let Some(path) = status.path() {
159160
match status.status() {

tests/testsuite/fix.rs

+30-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use cargo::core::Edition;
44
use cargo_test_support::compare::assert_match_exact;
5-
use cargo_test_support::git;
5+
use cargo_test_support::git::{self, init};
66
use cargo_test_support::paths::{self, CargoPathExt};
77
use cargo_test_support::registry::{Dependency, Package};
88
use cargo_test_support::tools;
@@ -771,6 +771,32 @@ commit the changes to these files:
771771
p.cargo("fix --allow-staged").run();
772772
}
773773

774+
#[cargo_test]
775+
fn errors_about_untracked_files() {
776+
let mut git_project = project().at("foo");
777+
git_project = git_project.file("src/lib.rs", "pub fn foo() {}");
778+
let p = git_project.build();
779+
let _ = init(&p.root());
780+
781+
p.cargo("fix")
782+
.with_status(101)
783+
.with_stderr(
784+
"\
785+
error: the working directory of this package has uncommitted changes, \
786+
and `cargo fix` can potentially perform destructive changes; if you'd \
787+
like to suppress this error pass `--allow-dirty`, `--allow-staged`, or \
788+
commit the changes to these files:
789+
790+
* Cargo.toml (dirty)
791+
* src/ (dirty)
792+
793+
794+
",
795+
)
796+
.run();
797+
p.cargo("fix --allow-dirty").run();
798+
}
799+
774800
#[cargo_test]
775801
fn does_not_warn_about_clean_working_directory() {
776802
let p = git::new("foo", |p| p.file("src/lib.rs", "pub fn foo() {}"));
@@ -1405,7 +1431,7 @@ fn fix_in_existing_repo_weird_ignore() {
14051431
let p = git::new("foo", |project| {
14061432
project
14071433
.file("src/lib.rs", "")
1408-
.file(".gitignore", "foo\ninner\n")
1434+
.file(".gitignore", "foo\ninner\nCargo.lock\ntarget\n")
14091435
.file("inner/file", "")
14101436
});
14111437

@@ -1715,7 +1741,7 @@ fn fix_with_run_cargo_in_proc_macros() {
17151741
"src/lib.rs",
17161742
r#"
17171743
use proc_macro::*;
1718-
1744+
17191745
#[proc_macro]
17201746
pub fn foo(_input: TokenStream) -> TokenStream {
17211747
let output = std::process::Command::new(env!("CARGO"))
@@ -1725,7 +1751,7 @@ fn fix_with_run_cargo_in_proc_macros() {
17251751
eprintln!("{}", std::str::from_utf8(&output.stderr).unwrap());
17261752
println!("{}", std::str::from_utf8(&output.stdout).unwrap());
17271753
"".parse().unwrap()
1728-
}
1754+
}
17291755
"#,
17301756
)
17311757
.file(

0 commit comments

Comments
 (0)