Skip to content

Commit f096357

Browse files
committed
Add allow staged to cargo fix
1 parent 8b6f710 commit f096357

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/bin/cargo/commands/fix.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ pub fn cli() -> App {
3030
.arg_target_dir()
3131
.arg_manifest_path()
3232
.arg_message_format()
33+
.arg(
34+
Arg::with_name("allow-staged")
35+
.long("allow-staged")
36+
.help("Fix code that has been staged for committing"),
37+
)
3338
.arg(
3439
Arg::with_name("broken-code")
3540
.long("broken-code")
@@ -136,6 +141,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult {
136141
allow_dirty: args.is_present("allow-dirty"),
137142
allow_no_vcs: args.is_present("allow-no-vcs"),
138143
broken_code: args.is_present("broken-code"),
144+
allow_staged: args.is_present("allow-staged"),
139145
})?;
140146
Ok(())
141147
}

src/cargo/ops/fix.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub struct FixOptions<'a> {
3434
pub allow_dirty: bool,
3535
pub allow_no_vcs: bool,
3636
pub broken_code: bool,
37+
pub allow_staged: bool,
3738
}
3839

3940
pub fn fix(ws: &Workspace, opts: &mut FixOptions) -> CargoResult<()> {
@@ -93,10 +94,21 @@ fn check_version_control(opts: &FixOptions) -> CargoResult<()> {
9394

9495
let mut dirty_files = Vec::new();
9596
if let Ok(repo) = git2::Repository::discover(config.cwd()) {
96-
let mut opts = git2::StatusOptions::new();
97-
opts.include_ignored(false);
98-
for status in repo.statuses(Some(&mut opts))?.iter() {
97+
let mut git_opts = git2::StatusOptions::new();
98+
git_opts.include_ignored(false);
99+
for status in repo.statuses(Some(&mut git_opts))?.iter() {
99100
if status.status() != git2::Status::CURRENT {
101+
if opts.allow_staged {
102+
if status.status() == git2::Status::INDEX_NEW
103+
|| status.status() == git2::Status::INDEX_MODIFIED
104+
|| status.status() == git2::Status::INDEX_DELETED
105+
|| status.status() == git2::Status::INDEX_RENAMED
106+
|| status.status() == git2::Status::INDEX_TYPECHANGE
107+
{
108+
continue;
109+
}
110+
}
111+
100112
if let Some(path) = status.path() {
101113
dirty_files.push(path.to_string());
102114
}

0 commit comments

Comments
 (0)