diff --git a/src/bin/cargo/commands/fix.rs b/src/bin/cargo/commands/fix.rs index 4cbf9b9565e..39b83ba8e4e 100644 --- a/src/bin/cargo/commands/fix.rs +++ b/src/bin/cargo/commands/fix.rs @@ -30,6 +30,11 @@ pub fn cli() -> App { .arg_target_dir() .arg_manifest_path() .arg_message_format() + .arg( + Arg::with_name("allow-staged") + .long("allow-staged") + .help("Fix code that has been staged for committing"), + ) .arg( Arg::with_name("broken-code") .long("broken-code") @@ -136,6 +141,7 @@ pub fn exec(config: &mut Config, args: &ArgMatches) -> CliResult { allow_dirty: args.is_present("allow-dirty"), allow_no_vcs: args.is_present("allow-no-vcs"), broken_code: args.is_present("broken-code"), + allow_staged: args.is_present("allow-staged"), })?; Ok(()) } diff --git a/src/cargo/ops/fix.rs b/src/cargo/ops/fix.rs index 275a471bd17..0eec7ffee99 100644 --- a/src/cargo/ops/fix.rs +++ b/src/cargo/ops/fix.rs @@ -34,6 +34,7 @@ pub struct FixOptions<'a> { pub allow_dirty: bool, pub allow_no_vcs: bool, pub broken_code: bool, + pub allow_staged: bool, } pub fn fix(ws: &Workspace, opts: &mut FixOptions) -> CargoResult<()> { @@ -93,10 +94,21 @@ fn check_version_control(opts: &FixOptions) -> CargoResult<()> { let mut dirty_files = Vec::new(); if let Ok(repo) = git2::Repository::discover(config.cwd()) { - let mut opts = git2::StatusOptions::new(); - opts.include_ignored(false); - for status in repo.statuses(Some(&mut opts))?.iter() { + let mut git_opts = git2::StatusOptions::new(); + git_opts.include_ignored(false); + for status in repo.statuses(Some(&mut git_opts))?.iter() { if status.status() != git2::Status::CURRENT { + if opts.allow_staged { + if status.status() == git2::Status::INDEX_NEW + || status.status() == git2::Status::INDEX_MODIFIED + || status.status() == git2::Status::INDEX_DELETED + || status.status() == git2::Status::INDEX_RENAMED + || status.status() == git2::Status::INDEX_TYPECHANGE + { + continue; + } + } + if let Some(path) = status.path() { dirty_files.push(path.to_string()); }