Skip to content

Add allow staged to cargo fix #5910

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/bin/cargo/commands/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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(())
}
18 changes: 15 additions & 3 deletions src/cargo/ops/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()> {
Expand Down Expand Up @@ -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());
}
Expand Down