Skip to content

Commit

Permalink
Auto merge of #11400 - hi-rustin:rustin-patch-fix-error, r=epage
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
bors committed Nov 28, 2022
2 parents a004f94 + 21b2fe9 commit a2ea66b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/cargo/ops/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ fn check_version_control(config: &Config, opts: &FixOptions) -> CargoResult<()>
if let Ok(repo) = git2::Repository::discover(config.cwd()) {
let mut repo_opts = git2::StatusOptions::new();
repo_opts.include_ignored(false);
repo_opts.include_untracked(true);
for status in repo.statuses(Some(&mut repo_opts))?.iter() {
if let Some(path) = status.path() {
match status.status() {
Expand Down
34 changes: 30 additions & 4 deletions tests/testsuite/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use cargo::core::Edition;
use cargo_test_support::compare::assert_match_exact;
use cargo_test_support::git;
use cargo_test_support::git::{self, init};
use cargo_test_support::paths::{self, CargoPathExt};
use cargo_test_support::registry::{Dependency, Package};
use cargo_test_support::tools;
Expand Down Expand Up @@ -771,6 +771,32 @@ commit the changes to these files:
p.cargo("fix --allow-staged").run();
}

#[cargo_test]
fn errors_about_untracked_files() {
let mut git_project = project().at("foo");
git_project = git_project.file("src/lib.rs", "pub fn foo() {}");
let p = git_project.build();
let _ = init(&p.root());

p.cargo("fix")
.with_status(101)
.with_stderr(
"\
error: the working directory of this package has uncommitted changes, \
and `cargo fix` can potentially perform destructive changes; if you'd \
like to suppress this error pass `--allow-dirty`, `--allow-staged`, or \
commit the changes to these files:
* Cargo.toml (dirty)
* src/ (dirty)
",
)
.run();
p.cargo("fix --allow-dirty").run();
}

#[cargo_test]
fn does_not_warn_about_clean_working_directory() {
let p = git::new("foo", |p| p.file("src/lib.rs", "pub fn foo() {}"));
Expand Down Expand Up @@ -1405,7 +1431,7 @@ fn fix_in_existing_repo_weird_ignore() {
let p = git::new("foo", |project| {
project
.file("src/lib.rs", "")
.file(".gitignore", "foo\ninner\n")
.file(".gitignore", "foo\ninner\nCargo.lock\ntarget\n")
.file("inner/file", "")
});

Expand Down Expand Up @@ -1715,7 +1741,7 @@ fn fix_with_run_cargo_in_proc_macros() {
"src/lib.rs",
r#"
use proc_macro::*;
#[proc_macro]
pub fn foo(_input: TokenStream) -> TokenStream {
let output = std::process::Command::new(env!("CARGO"))
Expand All @@ -1725,7 +1751,7 @@ fn fix_with_run_cargo_in_proc_macros() {
eprintln!("{}", std::str::from_utf8(&output.stderr).unwrap());
println!("{}", std::str::from_utf8(&output.stdout).unwrap());
"".parse().unwrap()
}
}
"#,
)
.file(
Expand Down

0 comments on commit a2ea66b

Please sign in to comment.