Skip to content

Commit 89b8a8b

Browse files
committed
Suppress error with --allow-dirty and add a test
Signed-off-by: hi-rustin <rustin.liu@gmail.com>
1 parent a1f2f5a commit 89b8a8b

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

crates/cargo-test-support/src/git.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,20 @@ where
175175
(git_project, repo)
176176
}
177177

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+
178192
/// Add all files in the working directory to the git index.
179193
pub fn add(repo: &git2::Repository) {
180194
// FIXME(libgit2/libgit2#2514): apparently, `add_all` will add all submodules

src/cargo/ops/fix.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,11 @@ 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-
if repo.is_empty()? {
157+
if repo.is_empty()? && !opts.allow_dirty {
158158
bail!(
159159
"no commits found in the git repository, and \
160160
`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`, \
162162
or commit your changes"
163163
)
164164
}

tests/testsuite/fix.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,25 @@ commit the changes to these files:
771771
p.cargo("fix --allow-staged").run();
772772
}
773773

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+
774793
#[cargo_test]
775794
fn does_not_warn_about_clean_working_directory() {
776795
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() {
17151734
"src/lib.rs",
17161735
r#"
17171736
use proc_macro::*;
1718-
1737+
17191738
#[proc_macro]
17201739
pub fn foo(_input: TokenStream) -> TokenStream {
17211740
let output = std::process::Command::new(env!("CARGO"))
@@ -1725,7 +1744,7 @@ fn fix_with_run_cargo_in_proc_macros() {
17251744
eprintln!("{}", std::str::from_utf8(&output.stderr).unwrap());
17261745
println!("{}", std::str::from_utf8(&output.stdout).unwrap());
17271746
"".parse().unwrap()
1728-
}
1747+
}
17291748
"#,
17301749
)
17311750
.file(

0 commit comments

Comments
 (0)