Skip to content

Commit 53a2fdf

Browse files
authored
Make --allow-dirty imply --allow-staged (#15013)
Staged changes don't really need protecting, and `--allow-dirty` is stronger than `--allow-staged`, so it can imply `--allow-staged` to make usage of `cargo fix` less verbose. Closes #14176
2 parents 2668a4f + 973cd53 commit 53a2fdf

File tree

8 files changed

+70
-65
lines changed

8 files changed

+70
-65
lines changed

src/bin/cargo/commands/fix.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub fn cli() -> Command {
2121
))
2222
.arg(flag(
2323
"allow-dirty",
24-
"Fix code even if the working directory is dirty",
24+
"Fix code even if the working directory is dirty or has staged changes",
2525
))
2626
.arg(flag(
2727
"allow-staged",
@@ -86,6 +86,8 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
8686
opts.filter = ops::CompileFilter::new_all_targets();
8787
}
8888

89+
let allow_dirty = args.flag("allow-dirty");
90+
8991
ops::fix(
9092
gctx,
9193
&ws,
@@ -94,9 +96,9 @@ pub fn exec(gctx: &mut GlobalContext, args: &ArgMatches) -> CliResult {
9496
edition: args.flag("edition"),
9597
idioms: args.flag("edition-idioms"),
9698
compile_opts: opts,
97-
allow_dirty: args.flag("allow-dirty"),
99+
allow_dirty,
100+
allow_staged: allow_dirty || args.flag("allow-staged"),
98101
allow_no_vcs: args.flag("allow-no-vcs"),
99-
allow_staged: args.flag("allow-staged"),
100102
broken_code: args.flag("broken-code"),
101103
requested_lockfile_path: lockfile_path,
102104
},

src/cargo/ops/fix.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ fn check_version_control(gctx: &GlobalContext, opts: &FixOptions) -> CargoResult
242242
bail!(
243243
"the working directory of this package has uncommitted changes, and \
244244
`cargo fix` can potentially perform destructive changes; if you'd \
245-
like to suppress this error pass `--allow-dirty`, `--allow-staged`, \
245+
like to suppress this error pass `--allow-dirty`, \
246246
or commit the changes to these files:\n\
247247
\n\
248248
{}\n\

src/doc/man/cargo-fix.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ Fix code even if a VCS was not detected.
9393
{{/option}}
9494

9595
{{#option "`--allow-dirty`" }}
96-
Fix code even if the working directory has changes.
96+
Fix code even if the working directory has changes (including staged changes).
9797
{{/option}}
9898

9999
{{#option "`--allow-staged`" }}

src/doc/man/generated_txt/cargo-fix.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ OPTIONS
8484
Fix code even if a VCS was not detected.
8585

8686
--allow-dirty
87-
Fix code even if the working directory has changes.
87+
Fix code even if the working directory has changes (including staged
88+
changes).
8889

8990
--allow-staged
9091
Fix code even if the working directory has staged changes.

src/doc/src/commands/cargo-fix.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ edition.</dd>
8989

9090

9191
<dt class="option-term" id="option-cargo-fix---allow-dirty"><a class="option-anchor" href="#option-cargo-fix---allow-dirty"></a><code>--allow-dirty</code></dt>
92-
<dd class="option-desc">Fix code even if the working directory has changes.</dd>
92+
<dd class="option-desc">Fix code even if the working directory has changes (including staged changes).</dd>
9393

9494

9595
<dt class="option-term" id="option-cargo-fix---allow-staged"><a class="option-anchor" href="#option-cargo-fix---allow-staged"></a><code>--allow-staged</code></dt>

src/etc/man/cargo-fix.1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ Fix code even if a VCS was not detected.
103103
.sp
104104
\fB\-\-allow\-dirty\fR
105105
.RS 4
106-
Fix code even if the working directory has changes.
106+
Fix code even if the working directory has changes (including staged changes).
107107
.RE
108108
.sp
109109
\fB\-\-allow\-staged\fR

tests/testsuite/cargo_fix/help/stdout.term.svg

Lines changed: 56 additions & 54 deletions
Loading

tests/testsuite/fix.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ fn warns_about_dirty_working_directory() {
635635
p.cargo("fix")
636636
.with_status(101)
637637
.with_stderr_data(str![[r#"
638-
[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:
638+
[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`, or commit the changes to these files:
639639
640640
* src/lib.rs (dirty)
641641
@@ -656,7 +656,7 @@ fn warns_about_staged_working_directory() {
656656
p.cargo("fix")
657657
.with_status(101)
658658
.with_stderr_data(str![[r#"
659-
[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:
659+
[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`, or commit the changes to these files:
660660
661661
* src/lib.rs (staged)
662662
@@ -677,7 +677,7 @@ fn errors_about_untracked_files() {
677677
p.cargo("fix")
678678
.with_status(101)
679679
.with_stderr_data(str![[r#"
680-
[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:
680+
[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`, or commit the changes to these files:
681681
682682
* Cargo.toml (dirty)
683683
* src/ (dirty)

0 commit comments

Comments
 (0)